# import the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
import seaborn as sns
import datetime as dt
import folium
import random
from branca.element import Figure # It rezise the map, It is a spinoff from Folium
df = pd.read_csv('vehicle_clean.csv')
df1 = df.drop(columns=['VEHICLE_LOCATION_ID','VEHICLE_MESSAGE_ID',
'CITY','STATE'])
#uploading the random sample of 50 trucks
sample= pd.read_csv('sample.csv', header=None )
sample= sample[0].tolist()
#subseting the dataframe for the sample
vehicles= df1[df1['VEHICLE_ID'].isin(sample)]
vehicles.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 370078 entries, 196431 to 11394700
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 VEHICLE_ID 370078 non-null int64
1 LATITUDE 370078 non-null float64
2 LONGITUDE 370078 non-null float64
3 LOCATION_DATE_TIME 370078 non-null object
4 MONTH 370078 non-null int64
5 WEEK 370078 non-null int64
dtypes: float64(2), int64(3), object(1)
memory usage: 19.8+ MB
df2= pd.read_csv('faults_clean.csv')
collist=['VEHICLE_ID','LONGITUDE', 'LATITUDE','MESSAGE_DATE_TIME',
'MALFUNCTION_LAMP_STATUS','RED_LAMP_STATUS','YELLOW_LAMP_STATUS',
'PROTECT_LAMP_STATUS']
df3=df2.loc[:,(collist)]
df3.replace(to_replace= {'on':1,'off':0, 'notAvailable':0}, inplace=True)
df3.fillna(0, inplace=True)
coldic = {'MESSAGE_DATE_TIME':'DATE','MALFUNCTION_LAMP_STATUS':'MALFUNCTION',
'RED_LAMP_STATUS':'RED','YELLOW_LAMP_STATUS': 'YELLOW',
'PROTECT_LAMP_STATUS': 'PROTECT'}
df3.rename(columns =coldic, inplace= True)
df3['DATE']= pd.to_datetime(df3['DATE'])
df3['MONTH']= df3['DATE'].dt.month
df3['WEEK']= df3['DATE'].dt.isocalendar().week
# Creating a column that counts all allarms that are not red or yellow
df3['OTHER']= np.where((df3['RED']== 1)|(df3['YELLOW']== 1), 0, 1)
df3.head()
df3.groupby(['WEEK']).sum()
#import the shape file of US states into a gdf
us49 = gpd.read_file('us49/us_49.shp')
sns.reset_defaults()
fig = plt.figure(figsize=(10, 6))
ax = us49.plot(facecolor='LightGray', edgecolor='k', linewidth=.2)
x, y = vehicles['LONGITUDE'].values, vehicles['LATITUDE'].values
ax.scatter(x,y, color='black', alpha=0.7, s=0.5)
plt.show()
STATES =['CA', 'NV', 'UT', 'AZ']
df2 = df[(df['STATE'].isin(STATES))&
(df['VEHICLE_ID'].isin(sample))]
# ploting subset database
fig = plt.figure(figsize=(10, 6))
ax = us49.plot(facecolor='LightGray', edgecolor='k', linewidth=.2)
x, y = df2['LONGITUDE'].values, df2['LATITUDE'].values
ax.scatter(x,y, color='black', alpha=0.7, s=0.5)
plt.show()
vehicles_w15= vehicles[vehicles['WEEK']==15]
#selecting a week
fig = plt.figure(figsize=(10, 6))
ax = us49.plot(facecolor='LightGray', edgecolor='k', linewidth=.2)
x, y = vehicles_w15['LONGITUDE'].values, vehicles_w15['LATITUDE'].values
ax.scatter(x,y, color='green', alpha=0.7, s=0.5)
plt.show()
def select_path(v,w):
"""Create a subset from a dataframe as a list of tupples"""
sub = df1.loc[(df1['VEHICLE_ID']==v)&(df1['WEEK']==w), ('LATITUDE','LONGITUDE')]
return list(sub.to_records(index=False))
def select_alarm(v,w,f):
"""Create a subset from a dataframe as a list of tupples"""
"""v= VEHICLE_ID, W= week number, F=lamp_status column"""
sub2 = df3.loc[(df3['VEHICLE_ID']==v)&(df3['WEEK']==w)&(df3[f]==1), ('LATITUDE','LONGITUDE')]
return list(sub2.to_records(index=False))
# list of all vehicles with red alarm in 2020 in the southwest
all_red= [100139268, 100222721, 100228008, 100229375, 100299676, 100314180, 100256311,
100262353, 100347615, 100350264, 100233932, 100254004, 100254536, 100254962,
100258247, 100269163, 100270479, 100293104, 100309951, 100322706, 100334205,
100369400, 100229625, 100231342, 100231648, 100238360, 100250699, 100267705,
100270284, 100307132, 100308279, 100309928, 100311028, 100383265, 100260140,
100323525, 100237621, 100248663, 100268680, 100309471, 100266778, 100287723,
100300068, 100208091, 100230714, 100232478, 100233334, 100234028, 100247982,
100257964, 100276636, 100280244, 100288872, 100301943, 100305030, 100310845,
100315213, 100316098, 100326189, 100338103, 100340040, 100241773, 100287419,
100242661, 100272854, 100314940, 100235108, 100280727, 100282535, 100296157,
100279283, 100354895, 100298153]
# Setting a random selction of vehicles, and choose the week
samples=random.choices(all_red, k=10)
week= 51
# Here you can adjust the size and where it is centered
fig01=Figure(height=400,width=850)
map_01 = folium.Map(location=[38, -95], zoom_start=4, tiles='cartodbpositron')
df3.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3806564 entries, 0 to 3806563
Data columns (total 11 columns):
# Column Dtype
--- ------ -----
0 VEHICLE_ID int64
1 LONGITUDE float64
2 LATITUDE float64
3 DATE datetime64[ns]
4 MALFUNCTION float64
5 RED float64
6 YELLOW float64
7 PROTECT float64
8 MONTH int64
9 WEEK UInt32
10 OTHER int64
dtypes: UInt32(1), datetime64[ns](1), float64(6), int64(3)
memory usage: 308.6 MB
# A: adding markers for the red alarm
for i in range(len(samples)):
b = (select_alarm(samples[i], week, 'RED'))
if len(b)>0:
for each in b:
folium.Marker(each,icon=folium.Icon(color='red')).add_to(map_01)
# B: adding markers for the yellow alarm
for i in range(len(samples)):
c = (select_alarm(samples[i], week, 'YELLOW'))
if len(c)>0:
for each in c:
folium.Marker(each,icon=folium.Icon(color='orange')).add_to(map_01)
# C: marker for other faults
for i in range(len(samples)):
d = (select_alarm(samples[i], week, 'OTHER'))
if len(d)>0:
for each in d:
folium.Marker(each,icon=folium.Icon(color='lightgray')).add_to(map_01)
# A: converting the location of vehicles (coordinates) into a list of tuples
point_01= select_path(samples[0], week)
point_02= select_path(samples[1], week)
point_03= select_path(samples[2], week)
point_04= select_path(samples[3], week)
point_05= select_path(samples[4], week)
point_06= select_path(samples[5], week)
point_07= select_path(samples[6], week)
point_08= select_path(samples[7], week)
point_09= select_path(samples[8], week)
point_10= select_path(samples[9], week)
# B: Creating lines for the vehicles path
f1= folium.PolyLine(point_01, color="pink", weight=2.5, opacity=1).add_to(map_01)
f2= folium.PolyLine(point_02, color="darkred", weight=2.5, opacity=1).add_to(map_01)
f3= folium.PolyLine(point_03, color="red", weight=2.5, opacity=1).add_to(map_01)
f4= folium.PolyLine(point_04, color="gold", weight=2.5, opacity=1).add_to(map_01)
f5= folium.PolyLine(point_05, color="yellow", weight=2.5, opacity=1).add_to(map_01)
f6= folium.PolyLine(point_06, color="lightgreen", weight=2.5, opacity=1).add_to(map_01)
f5= folium.PolyLine(point_07, color="cyan", weight=2.5, opacity=1).add_to(map_01)
f8= folium.PolyLine(point_08, color="blue", weight=2.5, opacity=1).add_to(map_01)
f9= folium.PolyLine(point_09, color="cadetblue", weight=2.5, opacity=1).add_to(map_01)
f10= folium.PolyLine(point_10, color="purple", weight=2.5, opacity=1).add_to(map_01)
# Step 4: Ploting the map
fig01.add_child(map_01)
# You dont neeed to run the lines bellow, here just to show how to do it
# Adding lines to the different feature groups
line_1=folium.vector_layers.PolyLine(point_01,tooltip='Vehicle_1',color='green',weight=1).add_to(f1)
line_2=folium.vector_layers.PolyLine(point_02,popup='<b>Path of Vehicle_2</b>',tooltip='Vehicle_2',color='blue',weight=1).add_to(f2)
line_3= ...
# Creating feature groups
f1=folium.FeatureGroup("Vehicle 1")
f2=folium.FeatureGroup("Vehicle 2")
f3=folium.FeatureGroup("Vehicle 3")
# Adding to the map and a layer control
f1.add_to(map_01)
f2.add_to(map_01)
f3.add_to(map_01)
folium.LayerControl().add_to(map_01)
map_01