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

# # Organize historic data in easy-to-understand DataFrame

# +
from google.transit import gtfs_realtime_pb2
import numpy as np
import pandas as pd
import gzip
import os
import datetime

directory = '../data/30/'
edgeDelaysDict={}
edgeDelaysDF = pd.DataFrame(columns=['ts','delay','stop_id1','stop_id2'])
edgeDelaysNumpy = np.array([[],[],[]])

# Loop through all files in directory
for filename in os.listdir(directory):
    if (filename != '.DS_Store'):
        
        # Uncompress and parse protobuff-file using gtfs_realtime_pb2
        with gzip.open(directory + filename, 'rb') as file:
            response = file.read()
            feed = gtfs_realtime_pb2.FeedMessage()
            feed.ParseFromString(response)
            
            # Loop through each entity (trip) in each feed / file. Every trip contains many stop_time_updates
            for trip in feed.entity:
                
                # Loop through all stop_time_update(s) of currently looped trip
                for i, stop_time_update in enumerate(trip.trip_update.stop_time_update):
                    
                    # Excluding the last stop as it does not have a "departuring edge"
                    if (i<len(trip.trip_update.stop_time_update)-1):
                        
                        # Store contextual metadata for the unique delay data point
                        ts = int(trip.trip_update.stop_time_update[i+1].arrival.time)
                        stop_id1 = trip.trip_update.stop_time_update[i].stop_id
                        stop_id2 = trip.trip_update.stop_time_update[i+1].stop_id
                        key = int(trip.trip_update.stop_time_update[i].stop_id) + int(trip.trip_update.stop_time_update[i+1].stop_id)
                        
                        # Store the delay data point
                        delay = int(trip.trip_update.stop_time_update[i+1].arrival.delay-trip.trip_update.stop_time_update[i].arrival.delay)
                        
                        #edgeDelaysDF.loc[key]=[int(ts),int(delay),int(stop_id1),int(stop_id2)]
                        edgeDelaysNumpy = np.append(edgeDelaysNumpy,[[key],[ts],[delay]],axis=1)
                        #edgeDelaysDict.setdefault((
                            #trip.trip_update.stop_time_update[i].stop_id,
                            #trip.trip_update.stop_time_update[i+1].stop_id),[]).append(
                            #{'delayDiff':trip.trip_update.stop_time_update[i+1].arrival.delay-trip.trip_update.stop_time_update[i].arrival.delay,
                             'timestamp':trip.trip_update.stop_time_update[i+1].arrival.time})
# -

# ## Present (parts of the) data using pandas

edgeDelaysDF

# ## Scatter plot the data using matplotlib & numpy

# +
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')
ax = fig.add_subplot(111,projection='3d')
ax.scatter(edgeDelaysNumpy[1,:],edgeDelaysNumpy[2,:],edgeDelaysNumpy[0,:],c='red')
ax.set_xlabel('Timestamp [x-axis]')
ax.set_ylabel('Delay [y-axis]')
ax.set_zlabel('Edge [z-axis]')

plt.savefig("demo.png")

# +
fig = plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')

x = edgeDelaysNumpy[0,:] # Different edgeID's
y = edgeDelaysNumpy[1,:] # Timestamp

area = (edgeDelaysNumpy[2,:])*10  # Radius according to delay

plt.scatter(x, y, s=area, alpha=0.5)
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered