elli-teu commited on
Commit
7762bc9
·
1 Parent(s): 39a6a86

App kind of works:)

Browse files
Files changed (1) hide show
  1. app.py +67 -15
app.py CHANGED
@@ -76,33 +76,67 @@ def get_buses():
76
 
77
  def plot_graph(plot_df):
78
  #Nu vill vi plotta!
79
- categories = {0 : 'EMPTY',
80
- 1: 'MANY_SEATS_AVAILABLE',
81
- 2:'FEW_SEATS_AVAILABLE',
82
- 3:'STANDING_ROOM_ONLY',
83
- 4:'CRUSHED_STANDING_ROOM_ONLY',
84
- 5: 'FULL'}
85
- #Steg 2, fixa sa date ersätts av stop genom att mappa location till stop
86
 
87
- plot_df = plot_df[["datetime", "vehicle_occupancystatus"]]
 
88
  st.write(plot_df.head())
89
  st.write(plot_df.tail())
90
  #plot_df = plot_df.set_index("datetime")
91
- plot_df["Y_category"] = plot_df["vehicle_occupancystatus"].map(categories)
 
 
 
 
92
  #st.line_chart(plot_df)
93
  # Create the Altair chart
94
- chart = alt.Chart(plot_df).mark_line(point=True).encode(
95
- x=alt.X('datetime:T', title="Datetime"), # Use column name as string
96
- y=alt.Y('Y_category:N', title="Vehicle Occupancy Status (Categories)"), # Treat Y as categorical
97
- tooltip=['datetime', 'Y_category'] # Add tooltips for interactivity
98
  ).properties(
99
  title="Vehicle Occupancy Status Over Time"
100
  )
101
  st.altair_chart(chart, use_container_width=True)
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  # Streamlit UI
104
  def main():
105
- st.title("Hopsworks Feature Group Explorer")
106
 
107
  # Initialize session state
108
  if "hopsworks_project" not in st.session_state:
@@ -163,6 +197,19 @@ def main():
163
  st.write("### Selected Bus")
164
  st.write(f"{search}: {bus}")
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  today = datetime.now()
167
  tomorrow = today + timedelta(days=1)
168
  today = today.date()
@@ -189,6 +236,8 @@ def main():
189
  trip_ids = list(trips["trip_id"])
190
  plot_df = st.session_state.data[st.session_state.data["trip_id"]==trip_ids[0]]
191
 
 
 
192
  print(f"start time {type(start_time)}")
193
  print(f"end time {type(end_time)}")
194
  print(f"day {type(day_choice)}")
@@ -196,11 +245,14 @@ def main():
196
  if start_time != None and end_time != None:
197
  #TODO hur filtrera på tid?
198
  st.write(f"Displaying buses between {start_time.strftime('%H:%M')} and {end_time.strftime('%H:%M')} the {day_choice}")
199
- selected_trips = bus_trips[(bus_trips["datetime"] >= datetime.combine(date_options[day_choice], start_time)) & (bus_trips["datetime"] <= datetime.combine(date_options[day_choice], end_time))]
 
 
200
  trip_ids = list(pd.unique(selected_trips["trip_id"]))
201
  st.write(f"Length {len(trip_ids)}")
202
  for id in trip_ids:
203
  plot_graph(st.session_state.data[st.session_state.data["trip_id"]==id])
 
204
 
205
 
206
 
 
76
 
77
  def plot_graph(plot_df):
78
  #Nu vill vi plotta!
79
+ categories = {0 : 'Empty',
80
+ 1: 'Many seats available',
81
+ 2:'Few seats available',
82
+ 3:'Standing room only',
83
+ 4:'Crushed standing room',
84
+ 5: 'Full'}
 
85
 
86
+ plot_df = plot_df[["datetime", "vehicle_occupancystatus", "stop_name"]]
87
+ plot_df = plot_df.sort_values("datetime")
88
  st.write(plot_df.head())
89
  st.write(plot_df.tail())
90
  #plot_df = plot_df.set_index("datetime")
91
+ plot_df["Occupancy"] = plot_df["vehicle_occupancystatus"].map(categories)
92
+ # Explicitly set the order for Y_category
93
+ category_order = list(categories.values()) # ['Empty', 'Many seats available', ..., 'Full']
94
+ category_order.reverse()
95
+
96
  #st.line_chart(plot_df)
97
  # Create the Altair chart
98
+ chart = alt.Chart(plot_df).mark_line(point=True, interpolate="step-after").encode(
99
+ x=alt.X('stop_name:N', title="Stop name"), # Use column name as string
100
+ y=alt.Y('Occupancy:N', title="Vehicle Occupancy Status (Categories)", sort=category_order, scale=alt.Scale(domain=category_order)), # Treat Y as categorical
101
+ tooltip=["datetime", 'stop_name', 'Occupancy'] # Add tooltips for interactivity
102
  ).properties(
103
  title="Vehicle Occupancy Status Over Time"
104
  )
105
  st.altair_chart(chart, use_container_width=True)
106
 
107
+ def visualize(filtered_data):
108
+ import folium
109
+ from streamlit_folium import st_folium
110
+
111
+ categories = {0 : 'Empty',
112
+ 1: 'Many seats available',
113
+ 2:'Few seats available',
114
+ 3:'Standing room only',
115
+ 4:'Crushed standing room',
116
+ 5: 'Full'}
117
+
118
+ # Create a folium map centered around a location
119
+ m = folium.Map(location=[filtered_data.iloc[0]["stop_lat"], filtered_data.iloc[0]["stop_lon"]], zoom_start=12)
120
+
121
+ sw = filtered_data[['stop_lat', 'stop_lon']].min().values.tolist()
122
+ ne = filtered_data[['stop_lat', 'stop_lon']].max().values.tolist()
123
+
124
+ m.fit_bounds([sw, ne])
125
+
126
+ # Add bus stop markers based on filtered data
127
+ for idx, row in filtered_data.iterrows():
128
+ folium.Marker(
129
+ [row['stop_lat'], row['stop_lon']],
130
+ popup=f"Bus stop: {row['stop_name']} Bus occupancy: {categories[row['vehicle_occupancystatus']] }",
131
+ icon = folium.Icon(icon="bus-simple", prefix="fa")
132
+ ).add_to(m)
133
+
134
+ # Display the map
135
+ st_folium(m, width=700, height=500)
136
+
137
  # Streamlit UI
138
  def main():
139
+ st.title("Wheely Fun Times - Bus Occupancy Explorer")
140
 
141
  # Initialize session state
142
  if "hopsworks_project" not in st.session_state:
 
197
  st.write("### Selected Bus")
198
  st.write(f"{search}: {bus}")
199
 
200
+ # Streamlit checkbox to toggle bus direction
201
+ if "direction" not in st.session_state:
202
+ st.session_state.direction = False
203
+
204
+ # Streamlit button to toggle bus direction
205
+ if st.sidebar.button('Change Direction'):
206
+ # Toggle between 'North' and 'South'
207
+ st.session_state.direction = not st.session_state.direction
208
+ print(st.session_state.direction)
209
+
210
+
211
+ #direction = st.sidebar.checkbox('Direction of bus', value=True)
212
+
213
  today = datetime.now()
214
  tomorrow = today + timedelta(days=1)
215
  today = today.date()
 
236
  trip_ids = list(trips["trip_id"])
237
  plot_df = st.session_state.data[st.session_state.data["trip_id"]==trip_ids[0]]
238
 
239
+ #TODO direction
240
+
241
  print(f"start time {type(start_time)}")
242
  print(f"end time {type(end_time)}")
243
  print(f"day {type(day_choice)}")
 
245
  if start_time != None and end_time != None:
246
  #TODO hur filtrera på tid?
247
  st.write(f"Displaying buses between {start_time.strftime('%H:%M')} and {end_time.strftime('%H:%M')} the {day_choice}")
248
+ selected_trips = bus_trips[(bus_trips["datetime"] >= datetime.combine(date_options[day_choice], start_time))
249
+ & (bus_trips["datetime"] <= datetime.combine(date_options[day_choice], end_time))
250
+ & (bus_trips["direction_id"] == st.session_state.direction )]
251
  trip_ids = list(pd.unique(selected_trips["trip_id"]))
252
  st.write(f"Length {len(trip_ids)}")
253
  for id in trip_ids:
254
  plot_graph(st.session_state.data[st.session_state.data["trip_id"]==id])
255
+ visualize(st.session_state.data[st.session_state.data["trip_id"]==id])
256
 
257
 
258