Annikaijak commited on
Commit
000f968
·
verified ·
1 Parent(s): b5c411b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -153
app.py CHANGED
@@ -15,7 +15,7 @@ import random
15
 
16
  # Configuring the web page and setting the page title and icon
17
  st.set_page_config(
18
- page_title='Parking Occupacy Detection',
19
  page_icon='🅿️',
20
  initial_sidebar_state='expanded')
21
 
@@ -23,14 +23,14 @@ st.set_page_config(
23
  warnings.filterwarnings("ignore")
24
 
25
  # Setting the title and adding text
26
- st.title('Parking Occupancy Detection')
27
 
28
  # Creating tabs for the different features of the application
29
  tab1,tab2,tab3,tab4, tab5 = st.tabs(['Parking lot status', 'Magnetic Field Explorer', 'About', 'Dataset and visualisations', 'Model performance'])
30
 
31
  with tab1:
32
  # Logging in to Hopsworks and loading the feature store
33
- project = hopsworks.login(project = "miknie20", api_key_value=os.environ['HOPSWORKS_API_KEY'])
34
  fs = project.get_feature_store()
35
 
36
  col1, col2 = st.columns(2)
@@ -38,75 +38,53 @@ with tab1:
38
  with col1:
39
  st.subheader("Parking place near building:")
40
 
41
- # Function to load the building model
42
 
43
  @st.cache_data()
44
- def get_building_model(project=project):
45
  mr = project.get_model_registry()
46
- building_model = mr.get_model("building_hist_model", version = 2)
47
- building_model_dir = building_model.download()
48
- return joblib.load(building_model_dir + "/building_hist_model.pkl")
49
 
50
  # Retrieving model
51
- building_hist_model = get_building_model()
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Loading the feature group with latest data for building
54
- api_building_newest_fg = fs.get_feature_group(name = 'api_building_newest', version = 1)
55
 
56
  # Function to loading the feature group with latest data for building as a dataset
57
  @st.cache_data()
58
- def retrieve_building(feature_group=api_building_newest_fg):
59
- api_building_newest_fg = feature_group.select(["time", "x", "y", "z"])
60
- df_building = api_building_newest_fg.read(read_options={"use_hive": True})
61
- return df_building
62
 
63
  # Retrieving building data
64
  building_new = retrieve_building()
65
 
66
  # Making the predictions and getting the latest data
67
- building_most_recent_prediction = building_new[['x', 'y', 'z']]
68
- building_most_recent_prediction = building_hist_model.predict(building_most_recent_prediction)
69
- building_new['Status'] = building_most_recent_prediction
70
  building_new['Status'].replace(['detection', 'no_detection'], ['Vehicle detected', 'No vehicle detected'], inplace=True)
71
  building_new = building_new.rename(columns={'time': 'Time'})
72
  building_new = building_new.set_index(['Time'])
73
- st.dataframe(building_new[['Status']].tail(5))
74
 
75
  with col2:
76
  st.subheader("Parking place near bikelane:")
77
 
78
- # Function to load the bikelane model
79
- @st.cache_data()
80
- def get_bikelane_model(project=project):
81
- mr = project.get_model_registry()
82
- bikelane_model = mr.get_model("bikelane_hist_model", version = 1)
83
- bikelane_model_dir = bikelane_model.download()
84
- return joblib.load(bikelane_model_dir + "/bikelane_hist_model.pkl")
85
-
86
- # Retrieving model
87
- bikelane_hist_model = get_bikelane_model()
88
-
89
- # Loading the feature group with latest data for bikelane
90
- api_bikelane_newest_fg = fs.get_feature_group(name = 'api_bikelane_newest', version = 1)
91
-
92
- # Function to loading the feature group with latest data for building as a dataset
93
- @st.cache_data()
94
- def retrieve_bikelane(feature_group=api_bikelane_newest_fg):
95
- api_bikelane_newest_fg = feature_group.select(["time", "x", "y", "z"])
96
- df_bikelane = api_bikelane_newest_fg.read(read_options={"use_hive": True})
97
- return df_bikelane
98
-
99
- # Retrieving building data
100
- bikelane_new = retrieve_bikelane()
101
-
102
- # Making the predictions and getting the latest data
103
- bikelane_most_recent_prediction = bikelane_new[['x', 'y', 'z']]
104
- bikelane_most_recent_prediction = bikelane_hist_model.predict(bikelane_most_recent_prediction)
105
- bikelane_new['Status'] = bikelane_most_recent_prediction
106
- bikelane_new['Status'].replace(['detection', 'no_detection'], ['Vehicle detected', 'No vehicle detected'], inplace=True)
107
- bikelane_new = bikelane_new.rename(columns={'time': 'Time'})
108
- bikelane_new = bikelane_new.set_index(['Time'])
109
- st.dataframe(bikelane_new[['Status']].tail(5))
110
 
111
  # Update button
112
  if st.button("Update application"):
@@ -116,40 +94,6 @@ with tab1:
116
  st.experimental_rerun()
117
 
118
  with tab2:
119
- # Defining a prediction function
120
- def explore_magnetic_field(model, x, y, z):
121
- input_list = [x, y, z]
122
- res = model.predict(np.asarray(input_list).reshape(1,-1))
123
- explorer_prediction = res[0]
124
- if explorer_prediction == 'detection':
125
- label = "Vehicle detected"
126
- else:
127
- label = "No vehicle detected"
128
- return label
129
-
130
- # Creating sliders for building model
131
- st.subheader('Experiment with building model:')
132
- x_input_building = st.slider("Choose your x-value", -232, 909, 0)
133
- y_input_building = st.slider("Choose your y-value", -1112, 435, 0)
134
- z_input_building = st.slider("Choose your z-value", -1648, 226, 0)
135
-
136
- # Making a prediction button for building model
137
- if st.button("Predict building input"):
138
- building_input_prediction = explore_magnetic_field(building_hist_model, x_input_building, y_input_building, z_input_building)
139
- st.write(building_input_prediction)
140
-
141
- st.divider()
142
-
143
- # Creating sliders for bikelane model
144
- st.subheader('Experiment with bikelane model:')
145
- x_input_bikelane = st.slider("Choose your x-value", -547, 288, 0)
146
- y_input_bikelane = st.slider("Choose your y-value", -1007, 786, 0)
147
- z_input_bikelane = st.slider("Choose your z-value", -1475, 16, 0)
148
-
149
- # Making a prediction button for bikelane model
150
- if st.button("Predict bikelane input"):
151
- bikelane_input_prediction = explore_magnetic_field(bikelane_hist_model, x_input_bikelane, y_input_bikelane, z_input_bikelane)
152
- st.write(bikelane_input_prediction)
153
 
154
  with tab3:
155
  st.subheader('About the application:')
@@ -162,74 +106,7 @@ with tab3:
162
  st.markdown('* **Model Performance:** The fifth tab explains how the underlying Machine Learning Model performs and how the predictor works.')
163
 
164
  with tab4:
165
- # Loading the feature group with historic data for building
166
- api_building_detection_features_fg = fs.get_feature_group(name = 'api_building_detection_features', version = 1)
167
-
168
- # Function to loading the feature group with latest data for building as a dataset
169
- @st.cache_data()
170
- def retrieve_building_historic(feature_group=api_building_detection_features_fg):
171
- api_building_detection_features_fg = feature_group.select_all()
172
- df_building_historic = api_building_detection_features_fg.read(read_options={"use_hive": True})
173
- return df_building_historic
174
-
175
- # Retrieving building data
176
- building_historic = retrieve_building_historic()
177
-
178
- # Display historic building dataset overview
179
- st.subheader("Overview of the historic dataframe for the parking place near the building")
180
- st.dataframe(building_historic.head())
181
-
182
- st.markdown('Here we can see an overview of the columns in the historic dataframe for the building parking place. There is some missing data in the radar columns, but that does not affect our detection model, as it is built using the magnetic field data.')
183
- st.markdown('We have looked a bit into when there is most activity in the parking place. Looking at the two visualisations below, we can see that the parking place near the building has most changes between 03:00-06:00 and 10:00-15:00. We can also see that there is most activity on weekdays, which is as expected, as this parking place is outside an office.')
184
- st.image('building_dist_hour.png')
185
- st.image('building_dist_week.png')
186
-
187
- st.markdown('The labels used to train the detection model are made on the basis of UML clustering. The clusters for the building parking place can be seen below.')
188
- st.image('building_cluster.png')
189
-
190
- st.divider()
191
-
192
- # Loading the feature group with historic data for bikelane
193
- api_bikelane_detection_features_fg = fs.get_feature_group(name = 'api_bikelane_detection_features', version = 1)
194
-
195
- # Function to loading the feature group with latest data for building as a dataset
196
- @st.cache_data()
197
- def retrieve_bikelane_historic(feature_group=api_bikelane_detection_features_fg):
198
- api_bikelane_detection_features_fg = feature_group.select_all()
199
- df_bikelane_historic = api_bikelane_detection_features_fg.read(read_options={"use_hive": True})
200
- return df_bikelane_historic
201
-
202
- # Retrieving building data
203
- bikelane_historic = retrieve_bikelane_historic()
204
-
205
- # Display historic building dataset overview
206
- st.subheader("Overview of the historic dataset for the parking place near the bikelane")
207
- st.dataframe(bikelane_historic.head())
208
-
209
- st.markdown('Looking at the historic dataframe for the bikelane parking place, we can see that it is quite similar to the one for the building parking place.')
210
- st.markdown('There is a bit of difference in the hourly distribution of activity, as the parking place near the bikelane has most changes between 03:00-06:00 and 12:00-17:00. However, the weekly distribution shows the same.')
211
- st.image('bikelane_dist_hour.png')
212
- st.image('bikelane_dist_week.png')
213
-
214
- st.markdown('Here we can see the clusters used to label the bikelane parking place.')
215
- st.image('bikelane_cluster.png')
216
-
217
  with tab5:
218
 
219
- # Model performance of building model
220
- st.subheader('Model to predict parking place near building:')
221
- st.markdown('The predictions for the parking place near the building are made on the basis of a KNearestNeighbours model')
222
- st.write(building_hist_model)
223
- st.markdown('The accuracy if the bikelane model is 100%')
224
- st.write('**Confusion matrix:**')
225
- st.image('building_hist_confusion_matrix.png', caption='Confusion matrix for building model')
226
-
227
- st.divider()
228
-
229
- # Model performance of bikelane model
230
- st.subheader('Model to predict parking place near bikelane:')
231
- st.markdown('Just like with the other model, the predictions for the parking place near the bikelane are made on the basis of a KNearestNeighbours model')
232
- st.write(bikelane_hist_model)
233
- st.markdown('The accuracy if the building model is 99%')
234
- st.write('**Confusion matrix:**')
235
- st.image('bikelane_hist_confusion_matrix.png', caption='Confusion matrix for bikelane model')
 
15
 
16
  # Configuring the web page and setting the page title and icon
17
  st.set_page_config(
18
+ page_title='Parking Occupacy Detection System',
19
  page_icon='🅿️',
20
  initial_sidebar_state='expanded')
21
 
 
23
  warnings.filterwarnings("ignore")
24
 
25
  # Setting the title and adding text
26
+ st.title('Parking Occupancy Detection System')
27
 
28
  # Creating tabs for the different features of the application
29
  tab1,tab2,tab3,tab4, tab5 = st.tabs(['Parking lot status', 'Magnetic Field Explorer', 'About', 'Dataset and visualisations', 'Model performance'])
30
 
31
  with tab1:
32
  # Logging in to Hopsworks and loading the feature store
33
+ project = hopsworks.login(project = "annikaijak", api_key_value=os.environ['HOPSWORKS_API_KEY'])
34
  fs = project.get_feature_store()
35
 
36
  col1, col2 = st.columns(2)
 
38
  with col1:
39
  st.subheader("Parking place near building:")
40
 
41
+ # Function to load the building models
42
 
43
  @st.cache_data()
44
+ def get_building_mag_model(project=project):
45
  mr = project.get_model_registry()
46
+ building_mag_model = mr.get_model("building_mag_hist_model", version = 2)
47
+ building_mag_model_dir = building_mag_model.download()
48
+ return joblib.load(building_mag_model_dir + "/building_mag_hist_model.pkl")
49
 
50
  # Retrieving model
51
+ building_mag_hist_model = get_building_mag_model()
52
+
53
+ @st.cache_data()
54
+ def get_building_rad_model(project=project):
55
+ mr = project.get_model_registry()
56
+ building_rad_model = mr.get_model("building_rad_hist_model", version = 2)
57
+ building_rad_model_dir = building_rad_model.download()
58
+ return joblib.load(building_rad_model_dir + "/building_rad_hist_model.pkl")
59
+
60
+ # Retrieving model
61
+ building_rad_hist_model = get_building_rad_model()
62
 
63
  # Loading the feature group with latest data for building
64
+ new_building_fg = fs.get_feature_group(name = 'new_building_fg', version = 1)
65
 
66
  # Function to loading the feature group with latest data for building as a dataset
67
  @st.cache_data()
68
+ def retrieve_building(feature_group=new_building_fg):
69
+ new_building_fg = feature_group.select_all()
70
+ df_building_new = new_building_fg.read(read_options={"use_hive": True})
71
+ return df_building_new
72
 
73
  # Retrieving building data
74
  building_new = retrieve_building()
75
 
76
  # Making the predictions and getting the latest data
77
+ building_mag_most_recent_prediction = building_new[['x', 'y', 'z', 'temperature', 'et0_fao_evapotranspiration']]
78
+ building_mag_most_recent_prediction_mag = building_mag_hist_model.predict(building_mag_most_recent_prediction)
79
+ building_new['Status'] = building_mag_most_recent_prediction
80
  building_new['Status'].replace(['detection', 'no_detection'], ['Vehicle detected', 'No vehicle detected'], inplace=True)
81
  building_new = building_new.rename(columns={'time': 'Time'})
82
  building_new = building_new.set_index(['Time'])
83
+ st.dataframe(building_new[['Status']].tail(3))
84
 
85
  with col2:
86
  st.subheader("Parking place near bikelane:")
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  # Update button
90
  if st.button("Update application"):
 
94
  st.experimental_rerun()
95
 
96
  with tab2:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  with tab3:
99
  st.subheader('About the application:')
 
106
  st.markdown('* **Model Performance:** The fifth tab explains how the underlying Machine Learning Model performs and how the predictor works.')
107
 
108
  with tab4:
109
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  with tab5:
111
 
112
+