File size: 4,569 Bytes
6f8fd55
 
 
 
 
 
 
 
 
ae3451c
6f8fd55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae3451c
 
 
6f8fd55
ae3451c
6f8fd55
 
ae3451c
6f8fd55
 
 
ae3451c
6f8fd55
 
 
ae3451c
 
6f8fd55
 
 
ae3451c
6f8fd55
 
 
 
 
 
 
 
 
 
ae3451c
6f8fd55
 
 
 
 
 
 
 
 
 
ae3451c
6f8fd55
 
3d19079
 
 
 
ae3451c
 
 
 
 
 
 
 
6f8fd55
 
ae3451c
 
 
 
 
 
 
6f8fd55
ae3451c
 
 
 
 
 
6f8fd55
 
 
 
ae3451c
6f8fd55
 
 
 
 
ae3451c
6f8fd55
 
ae3451c
 
 
 
 
 
 
 
 
6f8fd55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae3451c
 
6f8fd55
 
 
 
 
ae3451c
6f8fd55
 
ae3451c
 
 
 
 
 
 
 
 
3d19079
6f8fd55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy as sp
import sklearn as sk
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import streamlit as st
from src.data_ingestion import remove_previous_view, merge_volumes


def clean_data(df):

    df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d")
    df["day"] = df["date"].dt.day_name()
    df["hour"] = df["time"].str[:2] + ":00"
    df.drop(columns=["motorcycle"], axis=1, inplace=True)
    df["vehicle"] = df["car"] + df["large_vehicle"]

    return df


class HeatMap:
    def __init__(self, counts_df):

        # counts_df = counts_df[counts_df['view'] == view]
        
        self.df = clean_data(counts_df)
        
        new = (
            self.df.groupby(["hour", "day"])
            .mean(numeric_only = True)
            .drop(columns=["car", "large_vehicle"])
            .reset_index()
        )
        
        table = pd.pivot_table(
            new, values="vehicle", index=["day"], columns=["hour"]
        ).reset_index()
        
        self.table = table.reindex([1, 5, 6, 4, 0, 2, 3]).round(1)


    def vehicle_count_bar(self):
        new_df = self.df.groupby(["day"]).mean(numeric_only = True).round(1).reset_index()
        new_df = new_df.reindex([1, 5, 6, 4, 0, 2, 3])

        veh_count_fig = px.bar(
            new_df,
            x="day",
            y="vehicle",
            color="day",
            text_auto=True,
            labels={"day": "Day of the Week", "vehicle": "Vehicle Count"},
        )
        veh_count_fig.update_layout(showlegend=False)

        return veh_count_fig

    def heatmap(self):

        new_table = self.table.iloc[:, 1:].to_numpy()

        hm_fig = px.imshow(
            new_table,
            labels=dict(
                x="Hour of the Day", y="Day of the Week", color="Traffic"
            ),
            x=[
                "00", "01", "02", "03",
                "04", "05", "06", "07",
                "08", "09", "10", "11",
                "12", "13", "14", "15",
                "16",
                "17",
                "18",
                "19",
                "20",
                "21",
                "22",
                "23",
            ],
            y=[
                "Mon",
                "Tue",
                "Wed",
                "Thu",
                "Fri",
                "Sat",
                "Sun",
            ],
            # text_auto=True,
        )
        hm_fig.update_xaxes(side="top", tickmode = 'linear', type = 'category')
        hm_fig.update_layout(yaxis_visible=True, 
                             yaxis_showticklabels=True,
                             yaxis_title = ''
        )
        return hm_fig

    def update_hour_bar_chart(self, hour="08:00"):
        
        fig_hours = px.bar(
            self.table,
            x="day",
            y=str(hour),
            color="day",
            # text_auto=True,
            labels={"day": "Day of the Week"},
        )
        fig_hours.update_layout(showlegend=False)
        fig_hours.update_layout(yaxis_visible=False, 
                             yaxis_showticklabels=False,
                             yaxis_title = False
        )
        fig_hours.update_layout(xaxis_visible=True, 
                             xaxis_showticklabels=True,
                             xaxis_title = ''
        )
        return fig_hours

    def update_day_bar_chart(self, day="Saturday"):

        t = self.table.T
        t.drop("day", inplace=True)
        t.columns = [
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday",
        ]
        t = t.reset_index()

        t['hour'] = t['hour'].str[:2]

        fig_days = px.bar(
            t,
            x="hour",
            y=str(day),
            color=str(day),
            # text_auto=True,
            labels={"hour": "Count of Each Hour"},
        )
        fig_days.update_layout(showlegend=False)
        fig_days.update_layout(yaxis_visible=False, 
                             yaxis_showticklabels=False,
                             yaxis_title = False
        )
        fig_days.update_layout(xaxis_visible=True, 
                             xaxis_showticklabels=True,
                             xaxis_title = '',
        )
        fig_days.update_xaxes(tickangle=0, tickmode = 'linear', type = 'category', categoryorder='category ascending')
        return fig_days