File size: 13,602 Bytes
729628a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb23187
729628a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { useEffect, useState } from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Slider from '@mui/material/Slider';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import Switch from '@mui/material/Switch';
import TextField from '@mui/material/TextField';
import Typography from '@mui/material/Typography';
import Alert from '@mui/material/Alert';
import CircularProgress from '@mui/material/CircularProgress';
import { Save, Play } from 'lucide-react';
import type { AnomalyConfig, EnsembleStrategy } from '../../types/labeleval';
import { getAnomalyConfig, postAnomalyConfig, runPipeline } from '../../services/labeleval';
import { SectionCard } from './shared';
import AnomalyDetectionConfig from './AnomalyDetectionConfig';

const DEFAULT_CONFIG: AnomalyConfig = {
  imbalance: { method: 'smote', minority_boost: 2 },
  detectors: {
    knn: { enabled: true, k: 5 },
    lof: { enabled: true, n_neighbors: 20 },
    isolation_forest: { enabled: true, n_estimators: 100 },
    ocsvm: { enabled: false, nu: 0.05 },
    dbscan: { enabled: false, eps: 0.5, min_samples: 5 },
  },
  deep: {
    autoencoder: { enabled: true, latent_dim: 16, epochs: 20 },
    vae: { enabled: false, latent_dim: 8 },
    gan: { enabled: false },
    reconstruction_threshold: 0.5,
  },
  advanced: {
    few_shot: { enabled: false, support_per_class: 5 },
    ensemble_strategy: 'weighted_average',
    decision_threshold: 0.6,
  },
};

function Column({ title, children }: { title: string; children: React.ReactNode }) {
  return (
    <Box
      sx={{
        flex: '1 1 220px',
        minWidth: 220,
        bgcolor: '#12171d',
        border: '1px solid #232a31',
        borderRadius: 1,
        p: 1.5,
      }}
    >
      <Typography
        variant="caption"
        sx={{ color: '#4fc3f7', fontWeight: 700, letterSpacing: 0.8, display: 'block', mb: 1 }}
      >
        {title}
      </Typography>
      {children}
    </Box>
  );
}

export default function RareEventConfig({
  activeDatasetId,
  onRunStarted,
}: {
  activeDatasetId: string | null;
  onRunStarted?: (runId: string) => void;
}) {
  const [config, setConfig] = useState<AnomalyConfig>(DEFAULT_CONFIG);
  const [loading, setLoading] = useState(true);
  const [busy, setBusy] = useState<'save' | 'run' | null>(null);
  const [notice, setNotice] = useState<{ severity: 'success' | 'error' | 'warning'; text: string } | null>(null);

  useEffect(() => {
    let cancelled = false;
    getAnomalyConfig()
      .then((c) => {
        if (!cancelled) setConfig(c);
      })
      .catch(() => {
        /* backend offline — keep defaults, save will still POST */
      })
      .finally(() => {
        if (!cancelled) setLoading(false);
      });
    return () => {
      cancelled = true;
    };
  }, []);

  const save = async () => {
    setBusy('save');
    setNotice(null);
    try {
      const saved = await postAnomalyConfig(config);
      setConfig(saved);
      setNotice({ severity: 'success', text: 'Configuration saved.' });
    } catch (err) {
      setNotice({ severity: 'error', text: err instanceof Error ? err.message : String(err) });
    } finally {
      setBusy(null);
    }
  };

  const rerun = async () => {
    if (!activeDatasetId) {
      setNotice({ severity: 'warning', text: 'Select or generate a dataset first (Datasets page).' });
      return;
    }
    setBusy('run');
    setNotice(null);
    try {
      const res = await runPipeline(activeDatasetId);
      setNotice({ severity: 'success', text: `Evaluation run ${res.run_id} started (${res.status}).` });
      onRunStarted?.(res.run_id);
    } catch (err) {
      setNotice({ severity: 'error', text: err instanceof Error ? err.message : String(err) });
    } finally {
      setBusy(null);
    }
  };

  return (
    <SectionCard
      title="Rare Event Detection Configuration"
      help="The anomaly ensemble, configured in four stages: (1) class-imbalance handling before detection, (2) classical detectors (KNN, LOF, Isolation Forest, OC-SVM, DBSCAN), (3) deep reconstruction detectors (autoencoder/VAE/GAN), (4) fusion — how individual scores combine into one anomaly score and the decision threshold. Save persists the config; Re-run applies it to the active dataset."
      action={
        <Box sx={{ display: 'flex', gap: 1 }}>
          <Button
            size="small"
            variant="outlined"
            startIcon={busy === 'save' ? <CircularProgress size={14} /> : <Save size={14} />}
            disabled={busy !== null || loading}
            onClick={() => void save()}
          >
            Save
          </Button>
          <Button
            size="small"
            variant="contained"
            startIcon={busy === 'run' ? <CircularProgress size={14} /> : <Play size={14} />}
            disabled={busy !== null}
            onClick={() => void rerun()}
          >
            Re-run evaluation
          </Button>
        </Box>
      }
    >
      {notice ? (
        <Alert severity={notice.severity} variant="outlined" onClose={() => setNotice(null)} sx={{ mb: 1 }}>
          {notice.text}
        </Alert>
      ) : null}
      <Box sx={{ display: 'flex', gap: 1.5, flexWrap: 'wrap' }}>
        {/* Column 1: imbalanced data */}
        <Column title="IMBALANCED DATA">
          <Typography variant="caption" sx={{ color: '#8a949e' }}>
            Method
          </Typography>
          <Select
            size="small"
            fullWidth
            value={config.imbalance.method}
            onChange={(e) =>
              setConfig({
                ...config,
                imbalance: { ...config.imbalance, method: e.target.value as AnomalyConfig['imbalance']['method'] },
              })
            }
            sx={{ mb: 2, fontSize: 13 }}
          >
            <MenuItem value="none">None</MenuItem>
            <MenuItem value="smote">SMOTE</MenuItem>
            <MenuItem value="oversample">Oversample</MenuItem>
            <MenuItem value="undersample">Undersample</MenuItem>
            <MenuItem value="class_weights">Class weights</MenuItem>
          </Select>
          <Typography variant="caption" sx={{ color: '#8a949e' }}>
            Minority boost ×{config.imbalance.minority_boost.toFixed(1)}
          </Typography>
          <Slider
            size="small"
            min={1}
            max={10}
            step={0.5}
            value={config.imbalance.minority_boost}
            onChange={(_, v) =>
              setConfig({ ...config, imbalance: { ...config.imbalance, minority_boost: v as number } })
            }
          />
        </Column>

        {/* Column 2: classical anomaly detectors */}
        <Column title="ANOMALY DETECTION">
          <AnomalyDetectionConfig
            detectors={config.detectors}
            onChange={(detectors) => setConfig({ ...config, detectors })}
          />
        </Column>

        {/* Column 3: deep learning */}
        <Column title="DEEP LEARNING">
          <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <Typography variant="body2" sx={{ fontWeight: 600 }}>
              Autoencoder
            </Typography>
            <Switch
              size="small"
              checked={config.deep.autoencoder.enabled}
              onChange={(e) =>
                setConfig({
                  ...config,
                  deep: { ...config.deep, autoencoder: { ...config.deep.autoencoder, enabled: e.target.checked } },
                })
              }
            />
          </Box>
          {config.deep.autoencoder.enabled ? (
            <Box sx={{ display: 'flex', gap: 1, mb: 1 }}>
              <TextField
                size="small"
                type="number"
                label="latent"
                value={config.deep.autoencoder.latent_dim}
                onChange={(e) =>
                  setConfig({
                    ...config,
                    deep: {
                      ...config.deep,
                      autoencoder: { ...config.deep.autoencoder, latent_dim: Number(e.target.value) },
                    },
                  })
                }
                sx={{ flex: 1 }}
              />
              <TextField
                size="small"
                type="number"
                label="epochs"
                value={config.deep.autoencoder.epochs}
                onChange={(e) =>
                  setConfig({
                    ...config,
                    deep: {
                      ...config.deep,
                      autoencoder: { ...config.deep.autoencoder, epochs: Number(e.target.value) },
                    },
                  })
                }
                sx={{ flex: 1 }}
              />
            </Box>
          ) : null}
          <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <Typography variant="body2" sx={{ fontWeight: 600 }}>
              VAE
            </Typography>
            <Switch
              size="small"
              checked={config.deep.vae.enabled}
              onChange={(e) =>
                setConfig({
                  ...config,
                  deep: { ...config.deep, vae: { ...config.deep.vae, enabled: e.target.checked } },
                })
              }
            />
          </Box>
          {config.deep.vae.enabled ? (
            <TextField
              size="small"
              type="number"
              label="latent"
              value={config.deep.vae.latent_dim}
              onChange={(e) =>
                setConfig({
                  ...config,
                  deep: { ...config.deep, vae: { ...config.deep.vae, latent_dim: Number(e.target.value) } },
                })
              }
              sx={{ mb: 1, width: '100%' }}
            />
          ) : null}
          <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <Typography variant="body2" sx={{ fontWeight: 600 }}>
              GAN
            </Typography>
            <Switch
              size="small"
              checked={config.deep.gan.enabled}
              onChange={(e) =>
                setConfig({ ...config, deep: { ...config.deep, gan: { enabled: e.target.checked } } })
              }
            />
          </Box>
          <Typography variant="caption" sx={{ color: '#8a949e' }}>
            Reconstruction threshold {config.deep.reconstruction_threshold.toFixed(2)}
          </Typography>
          <Slider
            size="small"
            min={0}
            max={1}
            step={0.01}
            value={config.deep.reconstruction_threshold}
            onChange={(_, v) =>
              setConfig({ ...config, deep: { ...config.deep, reconstruction_threshold: v as number } })
            }
          />
        </Column>

        {/* Column 4: advanced / ensemble */}
        <Column title="ADVANCED / ENSEMBLE">
          <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <Typography variant="body2" sx={{ fontWeight: 600 }}>
              Few-shot
            </Typography>
            <Switch
              size="small"
              checked={config.advanced.few_shot.enabled}
              onChange={(e) =>
                setConfig({
                  ...config,
                  advanced: {
                    ...config.advanced,
                    few_shot: { ...config.advanced.few_shot, enabled: e.target.checked },
                  },
                })
              }
            />
          </Box>
          {config.advanced.few_shot.enabled ? (
            <TextField
              size="small"
              type="number"
              label="support / class"
              value={config.advanced.few_shot.support_per_class}
              onChange={(e) =>
                setConfig({
                  ...config,
                  advanced: {
                    ...config.advanced,
                    few_shot: { ...config.advanced.few_shot, support_per_class: Number(e.target.value) },
                  },
                })
              }
              sx={{ mb: 1, width: '100%' }}
            />
          ) : null}
          <Typography variant="caption" sx={{ color: '#8a949e' }}>
            Ensemble strategy
          </Typography>
          <Select
            size="small"
            fullWidth
            value={config.advanced.ensemble_strategy}
            onChange={(e) =>
              setConfig({
                ...config,
                advanced: { ...config.advanced, ensemble_strategy: e.target.value as EnsembleStrategy },
              })
            }
            sx={{ mb: 2, fontSize: 13 }}
          >
            <MenuItem value="majority_vote">Majority vote</MenuItem>
            <MenuItem value="weighted_average">Weighted average</MenuItem>
            <MenuItem value="meta_classifier">Meta classifier</MenuItem>
          </Select>
          <Typography variant="caption" sx={{ color: '#8a949e' }}>
            Decision threshold {config.advanced.decision_threshold.toFixed(2)}
          </Typography>
          <Slider
            size="small"
            min={0}
            max={1}
            step={0.01}
            value={config.advanced.decision_threshold}
            onChange={(_, v) =>
              setConfig({ ...config, advanced: { ...config.advanced, decision_threshold: v as number } })
            }
          />
        </Column>
      </Box>
    </SectionCard>
  );
}