rtik007 commited on
Commit
6d3308f
·
verified ·
1 Parent(s): b6e0d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -148,25 +148,79 @@ def get_anomaly_samples(input_data, n_samples, outliers_fraction, model_name):
148
 
149
  return top_10, middle_10, bottom_10
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  # Gradio Interface
152
  with gr.Blocks() as demo:
 
153
  gr.Markdown("## 🕵️‍♀️ Anomaly Detection App 🕵️‍♂️")
 
 
 
 
154
  input_data = gr.Radio(
155
  choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
156
  value="Moons",
157
  label="Dataset"
158
  )
 
 
159
  n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples")
160
- outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers")
161
- model_dropdown = gr.Dropdown(choices=["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"], label="Select Model")
162
 
163
- # Anomaly Samples Output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  top_table = gr.Dataframe(label="Top 10 Anomalies")
165
  middle_table = gr.Dataframe(label="Middle 10 Records")
166
  bottom_table = gr.Dataframe(label="Bottom 10 Normals")
167
  anomaly_samples_button = gr.Button("Show Anomaly Samples")
 
168
  anomaly_samples_button.click(
169
- fn=get_anomaly_samples,
170
  inputs=[input_data, n_samples, outliers_fraction, model_dropdown],
171
  outputs=[top_table, middle_table, bottom_table],
172
  )
 
148
 
149
  return top_10, middle_10, bottom_10
150
 
151
+ # Function to generate feature scatter plots
152
+ def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples):
153
+ data, _ = prepare_data(input_data, n_samples)
154
+ x_data = data[:, 0] if feature_x == "Feature1" else data[:, 1]
155
+ y_data = data[:, 1] if feature_y == "Feature2" else data[:, 0]
156
+
157
+ # Generate scatter plot
158
+ plt.figure(figsize=(6, 6))
159
+ plt.scatter(x_data, y_data, alpha=0.8, c="blue", s=20, label="Features")
160
+ plt.title(f"Feature Interaction Scatter Plot - {feature_x} vs {feature_y}")
161
+ plt.xlabel(feature_x)
162
+ plt.ylabel(feature_y)
163
+ plt.legend()
164
+ return plt.gcf()
165
+
166
  # Gradio Interface
167
  with gr.Blocks() as demo:
168
+ # App Title and Description
169
  gr.Markdown("## 🕵️‍♀️ Anomaly Detection App 🕵️‍♂️")
170
+ gr.Markdown("Explore anomaly detection models, feature interactions, and anomaly examples.")
171
+
172
+ # Interactive Feature Scatter Plot
173
+ gr.Markdown("### 1. Interactive Feature Scatter Plot")
174
  input_data = gr.Radio(
175
  choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
176
  value="Moons",
177
  label="Dataset"
178
  )
179
+ feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
180
+ feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
181
  n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples")
182
+ scatter_plot_button = gr.Button("Generate Scatter Plot")
183
+ scatter_plot = gr.Plot(label="Feature Scatter Plot")
184
 
185
+ scatter_plot_button.click(
186
+ fn=plot_interactive_feature_scatter,
187
+ inputs=[input_data, feature_x, feature_y, n_samples],
188
+ outputs=scatter_plot,
189
+ )
190
+
191
+ # Compare Anomaly Detection Algorithms
192
+ gr.Markdown("### 2. Compare Anomaly Detection Algorithms")
193
+ outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers")
194
+ input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
195
+ plots = []
196
+ with gr.Row():
197
+ for model_name in input_models:
198
+ plot = gr.Plot(label=model_name)
199
+ plots.append((model_name, plot))
200
+
201
+ def update_anomaly_comparison(input_data, outliers_fraction, n_samples):
202
+ results = []
203
+ for clf_name, plot in plots:
204
+ fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
205
+ results.append(fig)
206
+ return results
207
+
208
+ anomaly_inputs = [input_data, outliers_fraction, n_samples]
209
+ anomaly_outputs = [plot for _, plot in plots]
210
+ input_data.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
211
+ n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
212
+ outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
213
+
214
+ # Anomaly Samples Tab
215
+ gr.Markdown("### 3. Example Anomaly Records")
216
+ model_dropdown = gr.Dropdown(choices=input_models, value="Isolation Forest", label="Select Model")
217
  top_table = gr.Dataframe(label="Top 10 Anomalies")
218
  middle_table = gr.Dataframe(label="Middle 10 Records")
219
  bottom_table = gr.Dataframe(label="Bottom 10 Normals")
220
  anomaly_samples_button = gr.Button("Show Anomaly Samples")
221
+
222
  anomaly_samples_button.click(
223
+ fn=get_anomaly_samples,
224
  inputs=[input_data, n_samples, outliers_fraction, model_dropdown],
225
  outputs=[top_table, middle_table, bottom_table],
226
  )