# ---
# 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
# ---

# # Wrangle Volume Data
#
# We wrangle the traffic volume data into a workable format, and extract just the site and detector we are interested in.

# ## Import data

import pandas as pd
f = pd.read_csv('../data/VSDATA_20150819.csv')

# ## Filter data
#
# Filter to site 2433 (mid-way along segment of Princes freeway monitored by bluetooth detector sites). Detectors 4-6 are in the outbound/westbound lanes.

vols = f[(f["NB_SCATS_SITE"] == 2433) & f["NB_DETECTOR"].between(4,6)]
vols

# ## Date range
#
# Extract date from CSV data

import datetime
start_date = vols["QT_INTERVAL_COUNT"].iloc[0]
start_datetime = datetime.datetime.strptime(start_date, '%Y-%m-%d 00:00:00')
date_range = pd.date_range(start_datetime, periods=96, freq='15T')
date_range[:10] # show first 10 rows

# ## Transform data
#
# Transpose table. Label by time rather than interval. Use detector number as headers.

dets = vols.T
dets.columns = dets.loc["NB_DETECTOR"].values
dets = dets.loc['V00':'V95']
dets.index=date_range
dets.head()

# ## Export data
#
# Extract just detector 6 (the rightmost lane)

d6 = dets[6]
d6.head()

# ## Plots

import numpy as np
# %matplotlib inline
import matplotlib.pyplot as plt
bins = np.linspace(0, max(d6), 51)
plt.hist(d6, bins=bins)
plt.show()

plt.figure(figsize=(16,8))
plt.scatter(np.arange(len(d6)), d6.values)
plt.title("Volume Site 2433 Detector 6 (Outbound along Princes Highway). Wed 19 Aug 2015.")
plt.ylabel("Travel Time (seconds)")
plt.xlabel("Time Leave (15 min offset)")
plt.xlim([0,95])
plt.ylim([0,None])
plt.show()

StarcoderdataJupyterScriptsDedupFiltered