rtik007 commited on
Commit
be8b1b9
·
verified ·
1 Parent(s): 074a5a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -20
app.py CHANGED
@@ -140,17 +140,24 @@ def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples
140
 
141
 
142
  # Function for anomaly examples (Optional feature row)
143
- def plot_anomaly_examples(input_data, n_samples, outliers_fraction):
144
- n_outliers = int(outliers_fraction * n_samples)
145
- rng = np.random.RandomState(42)
146
- data, _ = make_blobs(n_samples=n_samples, random_state=0)
147
- outliers = rng.uniform(low=-6, high=6, size=(n_outliers, 2))
148
- plt.figure(figsize=(5, 5))
149
- plt.scatter(data[:, 0], data[:, 1], alpha=0.8, c="blue", s=20, label="Inliers")
150
- plt.scatter(outliers[:, 0], outliers[:, 1], alpha=0.8, c="red", s=20, label="Outliers")
151
- plt.title("Anomaly Examples")
152
- plt.legend()
153
- return plt.gcf()
 
 
 
 
 
 
 
154
 
155
 
156
  # Gradio Interface
@@ -206,15 +213,21 @@ with gr.Blocks() as demo:
206
  outputs=scatter_plot,
207
  )
208
 
209
- # Anomaly Examples Visualization
210
- gr.Markdown("### 3. Anomaly Examples Visualization")
211
- anomaly_examples_button = gr.Button("Generate Anomaly Examples")
212
- anomaly_plot = gr.Plot(label="Anomaly Examples")
213
-
214
- anomaly_examples_button.click(
215
- fn=plot_anomaly_examples,
216
- inputs=[input_data, n_samples, outliers_fraction],
217
- outputs=anomaly_plot,
 
 
 
 
 
 
218
  )
219
 
220
  demo.launch(debug=True)
 
140
 
141
 
142
  # Function for anomaly examples (Optional feature row)
143
+ def get_anomaly_samples():
144
+ """Returns formatted top, middle, and bottom 10 records based on anomaly score."""
145
+ sorted_df = df.sort_values("Anomaly_Score", ascending=False)
146
+
147
+ # Top 10 anomalies
148
+ top_10 = sorted_df[sorted_df["Anomaly_Label"] == "Anomaly"].head(10)
149
+
150
+ # Middle 10 (mix of anomalies and normal)
151
+ mid_start = len(sorted_df) // 2 - 50 # Get a broader middle slice
152
+ middle_section = sorted_df.iloc[mid_start: mid_start + 100] # Consider a larger middle slice
153
+ middle_anomalies = middle_section[middle_section["Anomaly_Label"] == "Anomaly"].sample(n=5, random_state=42)
154
+ middle_normals = middle_section[middle_section["Anomaly_Label"] == "Normal"].sample(n=5, random_state=42)
155
+ middle_10 = pd.concat([middle_anomalies, middle_normals]).sort_values("Anomaly_Score", ascending=False)
156
+
157
+ # Bottom 10 normal records
158
+ bottom_10 = sorted_df[sorted_df["Anomaly_Label"] == "Normal"].tail(10)
159
+
160
+ return top_10, middle_10, bottom_10
161
 
162
 
163
  # Gradio Interface
 
213
  outputs=scatter_plot,
214
  )
215
 
216
+ with gr.Tab("Anomaly Samples"):
217
+ gr.HTML("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Top 10 Records (Anomalies)</h3>")
218
+ top_table = gr.Dataframe(label="Top 10 Records")
219
+
220
+ gr.HTML("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Middle 10 Records (Mixed)</h3>")
221
+ middle_table = gr.Dataframe(label="Middle 10 Records")
222
+
223
+ gr.HTML("<h3 style='text-align: center; font-size: 18px; font-weight: bold;'>Bottom 10 Records (Normal)</h3>")
224
+ bottom_table = gr.Dataframe(label="Bottom 10 Records")
225
+
226
+ anomaly_samples_button = gr.Button("Show Anomaly Samples")
227
+ anomaly_samples_button.click(
228
+ get_anomaly_samples,
229
+ outputs=[top_table, middle_table, bottom_table]
230
+ )
231
  )
232
 
233
  demo.launch(debug=True)