# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Road and Traffic Analytics - Exploration of Motorways

# ### This Notebook is an exploration of data aimed to find new ways to optimize and understand the motorway network particularly.

# ### Heatmap with Python, SNS to show traffic congestion waves
#
# A function to give an idea about how traffic is flowing through freeway sections.
#
# It is important to note that freeways have certain properties that make the use of this type of heatmap particularly valuable. Congestion builds in waves, so over time it is useful to see how those waves form. Once congestion starts on a freeway and heads past a critical point it leads to much more congestion. Each detector is situated a certain distance along a distance, thus showing how congestion moves through the system. The detector types are either stud detectors, which do break quite often and thus lead to anomalous data, or TIRL detectors, which use a laser / light and break less often but are not as common.

import matplotlib.pyplot as plt
import matplotlib.colors as colors
from matplotlib.mlab import bivariate_normal
from matplotlib.colors import LogNorm
import pandas as pd
from pandas import DataFrame
import seaborn as sns
import math
import numpy as np
# %matplotlib inline

# +
## import matplotlib.pyplot as plt
def mix_palette():
    palette = sns.color_palette("GnBu", 10)
    palette[9] = sns.color_palette("OrRd", 10)[9]
    return palette


def set_ax(iax):
    iax.collections[0].colorbar.set_ticklabels(['10', '30'])
    for text in iax.texts:
        if float(text.get_text()) < 30:
            text.set_text("")
    iax.figure.tight_layout()
    iax.set_title("Occupancy")

    
def set_bx(ibx):
    ibx.fig.suptitle("Occupancy")

def load_data(path):
    initial = pd.read_csv(path)
    columns = list(initial.columns.values)[1:]
    rows = []
    for values in initial.values:
        rng = values[0]
        for column, value in zip(columns, values[1:]):
            rows.append([rng, column, value])
    return pd.DataFrame(data=rows, columns=['Detector', 'Time', 'Occupancy'])


# -

# ### Data input and settings for SNS plot
# This can be turned into a function to be used more generally. The cluster map can show a difference perspective on areas of high congestion and also broken detectors. This example is using occupancy, which is a measurement showing how often the detector has a car over it, which is useful for freeways versus volume or speed as a measurement.

# +
data_set = '1hrwestgate.csv'
data = load_data(data_set)
data = data.pivot("Time", "Detector", "Occupancy")
mi, ma = data.values.min(), data.values.max()


plt.figure(figsize = (12,15))
ax = sns.heatmap(data, cmap=mix_palette(), annot=True, square=True, cbar_kws={'ticks':[10,30]},
                 xticklabels=True, yticklabels=True, norm=LogNorm(vmin=mi, vmax=ma))
set_ax(ax)
plt.savefig('image.png', bbox_inches='tight')
plt.show()

bx = sns.clustermap(data, figsize = [12,15], cmap=mix_palette(), norm=LogNorm(vmin=mi, vmax=ma) )
set_bx(bx)
plt.savefig('cluster.png', bbox_inches='tight')
plt.show()


# -

# ## This is the function to re-purpose this code for more data and different data types (in process)

# +
def read_data(source,sheetname):
    data = pd.read_excel(source, sheet_name= sheetname)
    data = data[:-1]
    times = data['Row Labels'].str.extract('(\d\d):(\d\d)').astype(int)
    times2 = times.rename(columns={0: 'Hour', 1: 'Minute'})
    data2 = pd.concat([data, times2], axis=1)
    del data2['Row Labels']
    data3= data2.set_index(['Hour', 'Minute'])
    return data3


def heatmap(data_set):
    
    plt.figure(figsize = (12,15))
    ax = sns.heatmap(data_set, cmap=mix_palette(), annot=True, square=True, cbar_kws={'ticks':[10,30]},
                 xticklabels=True, yticklabels=True, norm=LogNorm(vmin=mi, vmax=ma))
    set_ax(ax)
    plt.savefig('image.png', bbox_inches='tight')
    plt.show()



# -

# # Managed Motorway Analytics Map exploration

# #### Mapping data using GIS and heatmaps would be allow road operators to understand the motorway as a system and adjust the ramp signals accordingly. This example will just use the detector data, but we will be moving into other data types in a further example, such as bluetooth, fatality data and more from open source API's. 

# This Analysis led into another tool that graphs and shows more data.

import pandas as pd
det_info ="Data.csv"
detector_information = pd.read_csv(det_info)

detector_information.head()

Coordinates = detector_information.set_index("Short Name")[["Y","X"]]

Coordinates.head()

# ### Next step
#
# 1. Clustering
# 2. Example data on detectors or change GeoJson to Detector Sites from 1min data.
# 3. Use that data to produce more visualisations. Visualisation ask <NAME> for critical measurement, at the moment go with congestion, KPI measure, and cost of congestion.
#
# 1. Use Fake Data or use 1 minute data and begin to produce KPI's. (transmax is doing poor job with KPI with how it aggregates data - double check from raw data and with lanes which are currently aggregated.)
#
# 1. More raw Data - process
# 2. Then use data in to recreate excel traffic mat hall
#
# ### Michael - get data going in to leaflet properly, create minimum viable kpi's and dashboard, even with faulty-ish data.
#

# ## How many people travel per day, what distance, etc come into 
#
# #### Formula for cost of congestion based on a variety of inputs like link distance, maybe use google maps for link distance, maybe ask UBER for data, maybe use bluetooth for link distance as well.

token = '<KEY>'

# +
import plotly as py


import plotly.graph_objs as go
py.offline.init_notebook_mode(connected=True)
import pandas as pd



mapbox_access_token = token

df = pd.read_csv('latlondetectors.csv')
site_lat = df.lat
site_lon = df.lon
site_id = df.id
site_name = df.name

data = [
    go.Scattermapbox(
        lat=site_lat,
        lon=site_lon,
        mode='markers',
        marker=go.scattermapbox.Marker(
            size=17,
            color='rgb(255, 0, 0)',
            opacity=0.7
        ),
        hoverinfo='text'
    ),
    go.Scattermapbox(
        lat=site_lat,
        lon=site_lon,
        mode='markers',
        marker=go.scattermapbox.Marker(
            size=8,
            color='rgb(242, 177, 172)',
            opacity=0.7
        ),
        hoverinfo='none'
    )]
        
layout = go.Layout(
    title='Traffic Detectors on Western Freeway',
    autosize=True,
    hovermode='closest',
    showlegend=False,
    mapbox=go.layout.Mapbox(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=go.layout.mapbox.Center(
            lat=-37.82572,
            lon=144.828
        ),
        pitch=0,
        zoom=13,
        style='light'
    ),
)

fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig, filename='Traffic Detectors on Western Freeway')
# -

# ![newplot%20%2813%29.png](attachment:newplot%20%2813%29.png)

StarcoderdataJupyterScriptsDedupFiltered