saba000farahani commited on
Commit
04f14aa
·
verified ·
1 Parent(s): 099971c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -14
app.py CHANGED
@@ -92,30 +92,54 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
92
  for i in range(contamination_levels.shape[1])
93
  ]).T
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Plot the graph
96
  fig, ax = plt.subplots(figsize=(12, 8))
97
 
98
- lidar_names = ['F/L', 'F/R', 'Left', 'Right', 'Roof', 'Rear']
99
  for i in range(simulated_contamination_levels.shape[1]):
100
  ax.plot(time_intervals, simulated_contamination_levels[:, i], label=f'{lidar_names[i]}')
101
  ax.axhline(y=0.4, color='r', linestyle='--', label='Contamination Threshold' if i == 0 else "")
 
 
102
 
103
  ax.set_title('Contamination Levels Over Time for Each Lidar')
104
  ax.set_xlabel('Time (seconds)')
105
  ax.set_ylabel('Contamination Level')
106
  ax.legend()
107
  ax.grid(True)
108
-
 
109
  plot_output = fig
110
-
111
  contamination_output = [f"{val * 100:.2f}%" for val in contamination_levels[0]]
112
  gradients_output = [f"{val:.4f}" for val in gradients[0]]
113
- cleaning_time_output = [f"{600:.2f}" for _ in range(6)] # Placeholder cleaning times
114
 
115
  return [plot_output] + contamination_output + gradients_output + cleaning_time_output
116
 
117
  except Exception as e:
118
- print(f"Error in prediction and plotting: {e}")
119
  return [plt.figure()] + ["Error"] * 18
120
 
121
  inputs = [
@@ -162,19 +186,20 @@ with gr.Blocks() as demo:
162
  gr.Markdown("### Input Parameters")
163
  for inp in inputs:
164
  inp.render()
165
- # Submit and Clear Buttons under the inputs
166
- with gr.Row():
167
- gr.Button(value="Submit", variant="primary").click(
168
- fn=predict_and_plot,
169
- inputs=inputs,
170
- outputs=[gr.Plot(label="Contamination Levels Over Time")] + contamination_outputs + gradients_outputs + cleaning_time_outputs
171
- )
172
- gr.Button(value="Clear").click(fn=lambda: None)
173
 
174
  with gr.Column(scale=1):
175
  gr.Image(image_path)
176
 
177
- # Middle Section: Outputs (Three columns)
 
 
 
 
 
 
 
 
 
178
  with gr.Row():
179
  with gr.Column(scale=2):
180
  gr.Markdown("### Contamination Predictions")
@@ -191,4 +216,10 @@ with gr.Blocks() as demo:
191
  for out in cleaning_time_outputs:
192
  out.render()
193
 
 
 
 
 
 
 
194
  demo.launch()
 
92
  for i in range(contamination_levels.shape[1])
93
  ]).T
94
 
95
+ # Function to calculate cleaning time using linear interpolation
96
+ def calculate_cleaning_time(time_intervals, contamination_levels, threshold=0.4):
97
+ cleaning_times = []
98
+ for i in range(contamination_levels.shape[1]):
99
+ levels = contamination_levels[:, i]
100
+ for j in range(1, len(levels)):
101
+ if levels[j-1] <= threshold <= levels[j]:
102
+ # Linear interpolation
103
+ t1, t2 = time_intervals[j-1], time_intervals[j]
104
+ c1, c2 = levels[j-1], levels[j]
105
+ cleaning_time = t1 + (threshold - c1) * (t2 - t1) / (c2 - c1)
106
+ cleaning_times.append(cleaning_time)
107
+ break
108
+ else:
109
+ cleaning_times.append(time_intervals[-1]) # If threshold is not reached
110
+ return cleaning_times
111
+
112
+ # Calculate cleaning times for all 6 lidars
113
+ cleaning_times = calculate_cleaning_time(time_intervals, simulated_contamination_levels)
114
+
115
+ # Lidar names
116
+ lidar_names = ['F/L', 'F/R', 'Left', 'Right', 'Roof', 'Rear']
117
+
118
  # Plot the graph
119
  fig, ax = plt.subplots(figsize=(12, 8))
120
 
 
121
  for i in range(simulated_contamination_levels.shape[1]):
122
  ax.plot(time_intervals, simulated_contamination_levels[:, i], label=f'{lidar_names[i]}')
123
  ax.axhline(y=0.4, color='r', linestyle='--', label='Contamination Threshold' if i == 0 else "")
124
+ if i < len(cleaning_times):
125
+ ax.scatter(cleaning_times[i], 0.4, color='k') # Mark the cleaning time point
126
 
127
  ax.set_title('Contamination Levels Over Time for Each Lidar')
128
  ax.set_xlabel('Time (seconds)')
129
  ax.set_ylabel('Contamination Level')
130
  ax.legend()
131
  ax.grid(True)
132
+
133
+ # Flatten the results into a single list of 19 outputs (1 plot + 6 contamination + 6 gradients + 6 cleaning times)
134
  plot_output = fig
 
135
  contamination_output = [f"{val * 100:.2f}%" for val in contamination_levels[0]]
136
  gradients_output = [f"{val:.4f}" for val in gradients[0]]
137
+ cleaning_time_output = [f"{val:.2f}" for val in cleaning_times]
138
 
139
  return [plot_output] + contamination_output + gradients_output + cleaning_time_output
140
 
141
  except Exception as e:
142
+ print(f"Error in Gradio interface: {e}")
143
  return [plt.figure()] + ["Error"] * 18
144
 
145
  inputs = [
 
186
  gr.Markdown("### Input Parameters")
187
  for inp in inputs:
188
  inp.render()
 
 
 
 
 
 
 
 
189
 
190
  with gr.Column(scale=1):
191
  gr.Image(image_path)
192
 
193
+ # Middle Section: Submit and Clear Buttons
194
+ with gr.Row():
195
+ gr.Button(value="Submit", variant="primary").click(
196
+ fn=predict_and_plot,
197
+ inputs=inputs,
198
+ outputs=[gr.Plot(label="Contamination Levels Over Time")] + contamination_outputs + gradients_outputs + cleaning_time_outputs
199
+ )
200
+ gr.Button(value="Clear").click(fn=lambda: None)
201
+
202
+ # Bottom Section: Outputs (Three columns) and Plot Below
203
  with gr.Row():
204
  with gr.Column(scale=2):
205
  gr.Markdown("### Contamination Predictions")
 
216
  for out in cleaning_time_outputs:
217
  out.render()
218
 
219
+ # Graph below the outputs
220
+ with gr.Row():
221
+ with gr.Column():
222
+ gr.Markdown("### Contamination Levels Over Time")
223
+ gr.Plot(label="Contamination Levels Over Time")
224
+
225
  demo.launch()