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

import pandas as pd
import json
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme()
sns.set(rc={'figure.figsize':(20, 10)})
#(11.7,8.27)
pd.options.mode.chained_assignment = None

df = pd.read_json('real_experiences_m_ch.jsonl', lines=True)

df.head()

# # Episode index 1

# Visualize order timestamp and revenue for episode index one.

episode_one = df.loc[df.episode_index == 1]

episode_one.head()

episode_one.shift_start.nunique()

episode_one.shift_end.nunique()

# All entries for episode_index one regarding shift start and end is the same. Order timestamp and revenue increments suggests those are variables to explore further.

# +
from datetime import datetime

time = datetime.strptime(episode_one.order_timestamp[0], "%Y-%m-%dT%H:%M:%S")
# -

sns.scatterplot(data=episode_one.head(50), x='order_timestamp', y='revenue')
plt.xticks(rotation = 45)
plt.title("First 50 rows of data", fontsize=16)
plt.show()

# We can notice that the are a few breaking points for revenue, this suggests it is depended on other information.
#
# **Hypothesis**: dependency on vehicle-id

# Extracting vehicle_id from route_update column for every row.

vehicle_ids = []
for row in range(len(episode_one)):
    vehicle_ids.append(episode_one.route_update.values[row]['vehicle_id'])

episode_one['vehicle_id'] = vehicle_ids

# After creating a new row for vehicles_id, let's see a preview of the dataset.

episode_one.head()

# Plotting scatterplot but this time vehicle_id will be used as seperator.

sns.scatterplot(data=episode_one.head(50), x='order_timestamp', y='revenue', hue='vehicle_id')
plt.xticks(rotation = 45)
plt.title("First 50 rows of data", fontsize=16)
plt.show()

# How many of unique vehicles are present in the whole dataset?

uniques = []
for episode in range(df.episode_index.nunique()):
    episode_index = df.loc[df.episode_index == episode]
    for i in range(len(episode_index)):
        id = episode_index.route_update.values[i]['vehicle_id']
        if id not in uniques:
            uniques.append(id)

uniques

# For further EDA, order_timestamp will be simplified to only represent hour and minutes (i.e. 16:30) thus making plot for each vehicle_id more representable.

time_df = pd.to_datetime(episode_one.order_timestamp, infer_datetime_format=True)

episode_one['order_time'] = time_df.apply(lambda x: "{:d}:{:02d}".format(x.hour, x.minute))

after_shift_orders = episode_one.loc[(episode_one.order_time >= '20:00') & (episode_one.order_time <= '23:59')]

sns.scatterplot(data=after_shift_orders, x='order_time', y='revenue', hue='vehicle_id')
plt.xticks(rotation=45, fontsize=9)
plt.title("Revenue increments for each vehicle after shift (8pm) until midnight", fontsize=16)
plt.show()

# Why does revenue increase even after shift? Does this mean drops are still made after shift hours as overtime?

# Let's see what happens to order which come after 00:00

episode_one['order_hour'] = pd.to_datetime(episode_one.order_time, format='%H:%M').dt.hour
order_after_midnight = episode_one.loc[(episode_one.order_hour > 0) & (episode_one.order_hour < 8)]

sns.scatterplot(data=order_after_midnight, x='order_time', y='revenue', hue='vehicle_id')
plt.xticks(rotation=45, fontsize=9)
plt.title("Revenue increments for each vehicle after midnight until shift start", fontsize=16)
plt.legend(loc=4)
plt.show()

# Why new orders coming around 2am show no incremental revenue increase for 'blue' vehicle?

# Each vehicle plots

for vehicle_id in uniques:
    each_vehicle = episode_one.loc[episode_one.vehicle_id == vehicle_id]
    sns.scatterplot(data=each_vehicle, x='order_time', y='revenue')
    plt.xticks(rotation=45, fontsize=9)
    plt.title(f"Revenue increments for vehicle: {vehicle_id}", fontsize=16)
    plt.show()

# It is clear that in the episode 1 most of the orders arrive between 16:00 and 03:00 (next day). 

# Quick preview of other episode index suggests similar order time range, revenue, and we figure before that 3 vehicle_ids are the repetitive in all 20 episode indices. Therefore, EDA with respect to order time and revenues will not differentiate significantly.

# # Route

# Each route comes a list of all 3 vehicle for every row.

# Source for visualization of the JSON tree http://jsonviewer.stack.hu/
#
# Visualization below represents first row of episode index 1.

# ![Route schema](https://i.imgur.com/jtyVoF2.png)

# ## Route update

# ![Route_update schema](https://i.imgur.com/ph3lBMH.jpg)

# P.S. Route update can also have loaded drops! Check episode_one.route_update.values[150]

# ![Route update 150 row](https://i.imgur.com/24liTTE.jpg)

# **Comparing both routes and route_update for each vehicle I have noticed that route_update is already incorporated in loaded_drops for route**
#
# Next, I will check what is the case when route_update contains loaded_drops as well.

# When route_update contains non-empty loaded and unloaded drops, they are the same as routes loaded and unloaded drops. However, I have noticed that routes contain the whole fleet, therefore, other vehicles have mulptile loaded and unloaded drops too.

# +
# episode_one.iloc[150:151, :].routes.values[0]

# +
# episode_one.iloc[150:151, :].route_update.values[0]
# -

df.loc[df.episode_index ==2 ]

StarcoderdataJupyterScriptsDedupFiltered