submission.ipynb 23.6 KB
Newer Older
Paul Primus's avatar
Paul Primus committed
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
5
   "execution_count": 144,
Paul Primus's avatar
Paul Primus committed
6
7
8
9
10
   "outputs": [],
   "source": [
    "from pymongo import MongoClient\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
Paul Primus's avatar
Paul Primus committed
11
    "from dcase2020_task2.data_sets.mcm_dataset import INVERSE_CLASS_MAP, TRAINING_ID_MAP, EVALUATION_ID_MAP, CLASS_MAP\n",
Paul Primus's avatar
Paul Primus committed
12
    "from scipy.stats import rankdata\n",
Paul Primus's avatar
Paul Primus committed
13
14
15
    "import os\n",
    "from shutil import copyfile\n",
    "import pandas as pd\n",
Paul Primus's avatar
Paul Primus committed
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
    "\n",
    "baseline_auc = {\n",
    "    'name': 'baseline',\n",
    "    0: {0: 0.5441, 2: 0.7340, 4: 0.6161, 6: 0.7392},\n",
    "    1: {0: 0.6715, 2: 0.6153, 4: 0.8833, 6: 0.7455},\n",
    "    2: {0: 0.9619, 2: 0.7897, 4: 0.9430, 6: 0.6959},\n",
    "    3: {1: 0.8136, 2: 0.8597, 3: 0.6330, 4: 0.8445},\n",
    "    4: {1: 0.7807, 2: 0.6416, 3: 0.7535},\n",
    "    5: {0: 0.6876, 2: 0.6818, 4: 0.7430, 6: 0.5390}\n",
    "    }\n",
    "    \n",
    "baseline_pauc = {\n",
    "    'name': 'baseline',\n",
    "    0: {0: 0.4937, 2: 0.5481, 4: 0.5326, 6: 0.5235},\n",
    "    1: {0: 0.5674, 2: 0.5810, 4: 0.6710, 6: 0.5802},\n",
    "    2: {0: 0.8144, 2: 0.6368, 4: 0.7198, 6: 0.4902},\n",
    "    3: {1: 0.6840, 2: 0.7772, 3: 0.5521, 4: 0.6897},\n",
    "    4: {1: 0.6425, 2: 0.5601, 3: 0.6103},\n",
    "    5: {0: 0.5170, 2: 0.5183, 4: 0.5197, 6: 0.4843}\n",
    "}\n",
    "\n",
    "baseline_both = {}\n",
    "for t in baseline_auc:\n",
    "    if t == 'name':\n",
    "        baseline_both[t] = 'baseline'\n",
    "        continue\n",
    "    else:\n",
    "        baseline_both[t] = {}\n",
    "    for i in baseline_auc[t]:\n",
    "        baseline_both[t][i] = np.array([baseline_auc[t][i], baseline_pauc[t][i]])\n",
    "\n",
    "\n",
    "def get_experiment(runs, name):\n",
    "    experiment_dict = dict()\n",
    "    for i in range(6):\n",
    "        experiment_dict[i] = dict()\n",
    "    \n",
    "    experiment_dict['name'] = name\n",
    "    \n",
    "    for experiment in runs:\n",
    "        if experiment['config'].get('id') == name:\n",
    "            machine_dict = experiment_dict.get(experiment['config']['machine_type'])\n",
    "            result = experiment.get('result')\n",
    "            machine_type = INVERSE_CLASS_MAP[experiment['config']['machine_type']]\n",
    "            machine_id = experiment['config']['machine_id']\n",
    "            \n",
    "            if result:\n",
    "                machine_dict[experiment['config']['machine_id']] = result.get(\n",
    "                    machine_type, {}\n",
    "                ).get(\n",
    "                    f'json://{machine_id}', -1\n",
    "                ).get('py/tuple', [0, 0])[:2]\n",
    "            else:\n",
    "                machine_dict[experiment['config']['machine_id']] = np.array([0, 0])\n",
    "    return experiment_dict\n",
    "\n",
    "\n",
    "def get_record(experiment):\n",
    "    record = []\n",
    "    for i in range(6):\n",
    "        for j in TRAINING_ID_MAP[i]:\n",
    "                v = experiment.get(i)\n",
    "                if v:\n",
    "                    v = v.get(j, [0, 0])\n",
    "                else:\n",
    "                    v = np.array([0, 0])\n",
    "                record.append(np.array(v))\n",
    "    assert len(record) == 23\n",
    "    return  experiment['name'], record"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
96
   "execution_count": 145,
Paul Primus's avatar
Paul Primus committed
97
98
99
100
   "outputs": [
    {
     "name": "stdout",
     "text": [
Paul Primus's avatar
Paul Primus committed
101
      "Loaded 572 runs.\n"
Paul Primus's avatar
Paul Primus committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "client = MongoClient('mongodb://student2.cp.jku.at:27017/')\n",
    "experiments = [r for r in client.resnet_gridsearch.runs.find({\"experiment.name\": \"dcase2020_task2_ClassificationExperiment\"})]\n",
    "print(f'Loaded {len(experiments)} runs.')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
121
   "execution_count": 146,
Paul Primus's avatar
Paul Primus committed
122
123
124
125
   "outputs": [
    {
     "name": "stdout",
     "text": [
Paul Primus's avatar
Paul Primus committed
126
      "Loaded 25 distinct experiments.\n"
Paul Primus's avatar
Paul Primus committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "descriptors = set()\n",
    "for experiment in experiments:\n",
    "    descriptors = descriptors.union(set([experiment['config']['id']]))\n",
    "descriptors = list(descriptors)\n",
    "print(f'Loaded {len(descriptors)} distinct experiments.')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
148
   "execution_count": 147,
Paul Primus's avatar
Paul Primus committed
149
150
151
152
   "outputs": [
    {
     "name": "stdout",
     "text": [
Paul Primus's avatar
Paul Primus committed
153
      "Loaded 13 distinct experiments, without reruns.\n"
Paul Primus's avatar
Paul Primus committed
154
155
156
157
158
159
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "descriptors = [d for d in descriptors if d.split('_')[-1] != 'rerun']\n",
Paul Primus's avatar
Paul Primus committed
160
    "# descriptors = [d for d in descriptors if d.split('_')[2] != '2']\n",
Paul Primus's avatar
Paul Primus committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    "# for descriptor in descriptors:\n",
    "#     print(descriptor)\n",
    "    \n",
    "print(f'Loaded {len(descriptors)} distinct experiments, without reruns.')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
176
   "execution_count": 148,
Paul Primus's avatar
Paul Primus committed
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
   "outputs": [],
   "source": [
    "# Extract Results\n",
    "# Concatenate Baseline Results\n",
    "n, m = get_record(baseline_both)\n",
    "names = [n]\n",
    "metrics = [np.array(m)]\n",
    "\n",
    "for descriptor in descriptors:\n",
    "    n, m = get_record(\n",
    "            get_experiment(\n",
    "                experiments, \n",
    "                descriptor\n",
    "            )\n",
    "        )\n",
    "    names.append(n)\n",
    "    metrics.append(np.array(m))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
205
   "execution_count": 149,
Paul Primus's avatar
Paul Primus committed
206
207
208
209
   "outputs": [
    {
     "name": "stdout",
     "text": [
Paul Primus's avatar
Paul Primus committed
210
211
212
213
214
215
      "Best Model for Machine Type 0: [ 8  2 10  1  5  6 11  4  9  7 13 12  3  0]\n",
      "Best Model for Machine Type 1: [ 8  4  2  6  9 10 13 11  5  1  7  3 12  0]\n",
      "Best Model for Machine Type 2: [ 4 10  1  8  5  9 11 13  6  2 12  3  7  0]\n",
      "Best Model for Machine Type 3: [11  1 12 10  5  2  4  9  8 13  6  3  7  0]\n",
      "Best Model for Machine Type 4: [13  9  1  5  4  6  7  2  3 12 10 11  8  0]\n",
      "Best Model for Machine Type 5: [ 8 12  3 11  7  4 10  6  2 13  5  1  9  0]\n",
Paul Primus's avatar
Paul Primus committed
216
      "00: ID-04 resnet_gridsearch_a_bit_larger_loose_1e-4_100_BCE\n",
Paul Primus's avatar
Paul Primus committed
217
218
219
220
221
222
223
224
225
226
227
228
229
      "01: ID-08 resnet_gridsearch_2_a_bit_larger_loose_1e-4_0.99_100_BCE\n",
      "02: ID-10 resnet_gridsearch_a_bit_larger_loose_1e-4_100_AUC\n",
      "03: ID-01 resnet_gridsearch_a_bit_larger_loose_1e-5_100_BCE\n",
      "04: ID-11 resnet_gridsearch_a_bit_smaller_loose_1e-4_100_BCE\n",
      "05: ID-05 resnet_gridsearch_a_bit_larger_loose_1e-5_100_AUC\n",
      "06: ID-02 resnet_gridsearch_normal_loose_1e-4_100_BCE\n",
      "07: ID-09 resnet_gridsearch_normal_loose_1e-5_100_BCE\n",
      "08: ID-06 resnet_gridsearch_normal_loose_1e-4_100_AUC\n",
      "09: ID-12 resnet_gridsearch_a_bit_smaller_loose_1e-4_100_AUC\n",
      "10: ID-13 resnet_gridsearch_normal_loose_1e-5_100_AUC\n",
      "11: ID-07 resnet_gridsearch_a_bit_smaller_loose_1e-5_100_AUC\n",
      "12: ID-03 resnet_gridsearch_a_bit_smaller_loose_1e-5_100_BCE\n",
      "13: ID-00 baseline\n"
Paul Primus's avatar
Paul Primus committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "data = np.array(metrics)\n",
    "auc_ranks = []\n",
    "pauc_ranks = []\n",
    "idxes = [0, 4, 8, 12, 16, 19, 23]\n",
    "best_idxes = []\n",
    "for type_, (i, j) in enumerate(zip(idxes[:-1], idxes[1:])):\n",
    "    average_auc = data[:, i:j, 0].mean(axis=1)\n",
    "    average_pauc = data[:, i:j, 1].mean(axis=1)\n",
    "    best_idxes.append(\n",
Paul Primus's avatar
Paul Primus committed
244
    "       np.argsort(average_auc + average_pauc)[::-1]\n",
Paul Primus's avatar
Paul Primus committed
245
246
247
248
249
250
251
252
253
254
255
    "    )\n",
    "    print(f'Best Model for Machine Type {type_}: {best_idxes[-1]}')\n",
    "    auc_ranks.append(rankdata(-average_auc))\n",
    "    pauc_ranks.append(rankdata(-average_pauc))\n",
    "\n",
    "\n",
    "ranks = np.stack([np.array(list(zip(*auc_ranks))), np.array(list(zip(*pauc_ranks)))], axis=-1).mean(axis=-1).mean(axis=-1)\n",
    "\n",
    "sorted_model_indices = list(np.argsort(ranks))\n",
    "names = np.array(names)\n",
    "for i, (n, r, j) in enumerate(zip(names[sorted_model_indices], ranks[sorted_model_indices], sorted_model_indices)):\n",
Paul Primus's avatar
Paul Primus committed
256
    "    print(f'{i:02d}: ID-{j:02d} {n}')"
Paul Primus's avatar
Paul Primus committed
257
258
259
260
261
262
263
264
265
266
267
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
268
   "execution_count": 156,
Paul Primus's avatar
Paul Primus committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
   "outputs": [],
   "source": [
    "import sklearn\n",
    "\n",
    "def compute_auc(src):\n",
    "    scores = pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 1]\n",
    "    names = pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 0]\n",
    "    names = np.array([1 if name.split('_')[0] == 'anomaly' else 0 for name in names])\n",
    "    return sklearn.metrics.roc_auc_score(names, scores), sklearn.metrics.roc_auc_score(names, scores, max_fpr=0.1)\n",
    "\n",
    "run_ids = names\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
291
   "execution_count": 157,
Paul Primus's avatar
Paul Primus committed
292
293
294
295
296
297
298
   "outputs": [],
   "source": [
    "# Create Submission 1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
Paul Primus's avatar
Paul Primus committed
299
300
     "name": "#%%\n",
     "is_executing": false
Paul Primus's avatar
Paul Primus committed
301
302
303
304
305
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
306
   "execution_count": 158,
Paul Primus's avatar
Paul Primus committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
   "outputs": [],
   "source": [
    "for machine_type in range(6):\n",
    "    for machine_id in EVALUATION_ID_MAP[machine_type]:\n",
    "        best_model_folder = run_ids[sorted_model_indices[0]]\n",
    "        src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "        src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "        dst_path = os.path.join('..', 'submission_package', 'task2', 'Primus_CP-JKU_task2_1')\n",
    "        dst = os.path.join(dst_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id:02d}.csv')\n",
    "        copyfile(src, dst)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
328
   "execution_count": 159,
Paul Primus's avatar
Paul Primus committed
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
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "\tfan:\n",
      "\t\taveraged_auc: 0.9226685446121019\n",
      "\t\taveraged_pauc: 0.8230212986543588\n",
      "\tpump:\n",
      "\t\taveraged_auc: 0.9297781495399142\n",
      "\t\taveraged_pauc: 0.8722867745313565\n",
      "\tslider:\n",
      "\t\taveraged_auc: 0.9894779962546816\n",
      "\t\taveraged_pauc: 0.9454464813719693\n",
      "\tToyCar:\n",
      "\t\taveraged_auc: 0.9489897900841298\n",
      "\t\taveraged_pauc: 0.8752479934828495\n",
      "\tToyConveyor:\n",
      "\t\taveraged_auc: 0.83764533636162\n",
      "\t\taveraged_pauc: 0.7279798253268154\n",
      "\tvalve:\n",
      "\t\taveraged_auc: 0.9424583333333334\n",
      "\t\taveraged_pauc: 0.8903508771929824\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "for machine_type in range(6):\n",
    "    auc = []\n",
    "    pauc = []\n",
    "    for machine_id in TRAINING_ID_MAP[machine_type]:\n",
    "        best_model_folder = run_ids[sorted_model_indices[0]]\n",
    "        src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "        src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "        a, p = compute_auc(src)\n",
    "        auc.append(a)\n",
    "        pauc.append(p)\n",
    "        \n",
    "    print(f'\\t{INVERSE_CLASS_MAP[machine_type]}:\\n\\t\\taveraged_auc: {np.mean(auc)}\\n\\t\\taveraged_pauc: {np.mean(pauc)}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
379
   "execution_count": 160,
Paul Primus's avatar
Paul Primus committed
380
381
382
383
   "outputs": [],
   "source": [
    "\n",
    "\n",
Paul Primus's avatar
Paul Primus committed
384
385
386
387
388
389
390
391
392
393
394
395
    "# Create Submission 2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% \n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
396
   "execution_count": 161,
Paul Primus's avatar
Paul Primus committed
397
398
399
400
   "outputs": [],
   "source": [
    "for machine_type, idxes in enumerate(best_idxes):\n",
    "    for machine_id in EVALUATION_ID_MAP[machine_type]:\n",
Paul Primus's avatar
Paul Primus committed
401
    "        \n",
Paul Primus's avatar
Paul Primus committed
402
403
404
    "        idx = idxes[0]\n",
    "        best_model_folder = run_ids[idx]\n",
    "\n",
Paul Primus's avatar
Paul Primus committed
405
    "        src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
Paul Primus's avatar
Paul Primus committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    "        src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "        dst_path = os.path.join('..', 'submission_package', 'task2', 'Primus_CP-JKU_task2_2')\n",
    "        dst = os.path.join(dst_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id:02d}.csv')\n",
    "        copyfile(src, dst)\n",
    "        "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "\tfan:\n",
      "\t\taveraged_auc: 0.9286317167841518\n",
      "\t\taveraged_pauc: 0.8352913487070679\n",
      "\tpump:\n",
      "\t\taveraged_auc: 0.9297781495399142\n",
      "\t\taveraged_pauc: 0.8722867745313565\n",
      "\tslider:\n",
      "\t\taveraged_auc: 0.9894779962546816\n",
      "\t\taveraged_pauc: 0.9454464813719693\n",
      "\tToyCar:\n",
      "\t\taveraged_auc: 0.9566950093931226\n",
      "\t\taveraged_pauc: 0.8961968600747151\n",
      "\tToyConveyor:\n",
      "\t\taveraged_auc: 0.8526503235962499\n",
      "\t\taveraged_pauc: 0.7259891865658302\n",
      "\tvalve:\n",
      "\t\taveraged_auc: 0.9776656162464985\n",
      "\t\taveraged_pauc: 0.9357400855078873\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "for machine_type, idxes in enumerate(best_idxes):\n",
    "    auc = []\n",
    "    pauc = []\n",
    "    for machine_id in TRAINING_ID_MAP[machine_type]:\n",
    "        idx = idxes[0]\n",
    "        best_model_folder = run_ids[idx]\n",
    "        src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "        src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "        a, p = compute_auc(src)\n",
    "        auc.append(a)\n",
    "        pauc.append(p)\n",
Paul Primus's avatar
Paul Primus committed
461
    "        \n",
Paul Primus's avatar
Paul Primus committed
462
    "    print(f'\\t{INVERSE_CLASS_MAP[machine_type]}:\\n\\t\\taveraged_auc: {np.mean(auc)}\\n\\t\\taveraged_pauc: {np.mean(pauc)}')"
Paul Primus's avatar
Paul Primus committed
463
464
465
466
467
468
469
470
471
472
473
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
474
   "execution_count": 162,
Paul Primus's avatar
Paul Primus committed
475
476
   "outputs": [],
   "source": [
Paul Primus's avatar
Paul Primus committed
477
    "# Create Submission 3 # median ensemble"
Paul Primus's avatar
Paul Primus committed
478
479
480
481
482
483
484
485
486
487
488
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
489
   "execution_count": 168,
Paul Primus's avatar
Paul Primus committed
490
491
   "outputs": [],
   "source": [
Paul Primus's avatar
Paul Primus committed
492
493
494
495
    "for machine_type, idxes in enumerate(best_idxes):\n",
    "    for machine_id in EVALUATION_ID_MAP[machine_type]:\n",
    "        file_names = []\n",
    "        scores = []\n",
Paul Primus's avatar
Paul Primus committed
496
    "        for idx in idxes[:5]:\n",
Paul Primus's avatar
Paul Primus committed
497
498
499
500
501
502
503
504
505
    "            best_model_folder = run_ids[idx]\n",
    "            src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "            src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "            scores.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 1])\n",
    "            file_names.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 0])\n",
    "        \n",
    "        scores = list(np.median(np.array(scores).T, axis=-1).reshape(-1))\n",
    "        dst_path = os.path.join('..', 'submission_package', 'task2', 'Primus_CP-JKU_task2_3')\n",
    "        dst = os.path.join(dst_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id:02d}.csv')\n",
Paul Primus's avatar
Paul Primus committed
506
    "\n",
Paul Primus's avatar
Paul Primus committed
507
508
509
510
511
512
513
514
515
516
517
518
    "        pd.DataFrame(list(zip(file_names[0], scores))).to_csv(dst, index=False, header=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
519
   "execution_count": 169,
Paul Primus's avatar
Paul Primus committed
520
521
522
523
524
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "\tfan:\n",
Paul Primus's avatar
Paul Primus committed
525
526
      "\t\taveraged_auc: 0.9281888985937587\n",
      "\t\taveraged_pauc: 0.8283523606556178\n",
Paul Primus's avatar
Paul Primus committed
527
      "\tpump:\n",
Paul Primus's avatar
Paul Primus committed
528
529
      "\t\taveraged_auc: 0.9209936334730452\n",
      "\t\taveraged_pauc: 0.8705862272890137\n",
Paul Primus's avatar
Paul Primus committed
530
      "\tslider:\n",
Paul Primus's avatar
Paul Primus committed
531
532
      "\t\taveraged_auc: 0.9858871722846442\n",
      "\t\taveraged_pauc: 0.9268061304947763\n",
Paul Primus's avatar
Paul Primus committed
533
      "\tToyCar:\n",
Paul Primus's avatar
Paul Primus committed
534
535
      "\t\taveraged_auc: 0.9547106714040676\n",
      "\t\taveraged_pauc: 0.8913650442572985\n",
Paul Primus's avatar
Paul Primus committed
536
      "\tToyConveyor:\n",
Paul Primus's avatar
Paul Primus committed
537
538
      "\t\taveraged_auc: 0.8514805262648587\n",
      "\t\taveraged_pauc: 0.7374989794507775\n",
Paul Primus's avatar
Paul Primus committed
539
      "\tvalve:\n",
Paul Primus's avatar
Paul Primus committed
540
541
      "\t\taveraged_auc: 0.9696039915966386\n",
      "\t\taveraged_pauc: 0.9118411838419578\n"
Paul Primus's avatar
Paul Primus committed
542
543
544
545
546
547
548
549
550
551
552
553
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "\n",
    "for machine_type, idxes in enumerate(best_idxes):\n",
    "    auc = []\n",
    "    pauc = []\n",
    "    for machine_id in TRAINING_ID_MAP[machine_type]:\n",
    "        file_names = []\n",
    "        scores = []\n",
Paul Primus's avatar
Paul Primus committed
554
    "        for idx in idxes[:5]:\n",
Paul Primus's avatar
Paul Primus committed
555
556
557
558
559
    "            best_model_folder = run_ids[idx]\n",
    "            src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "            src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "            scores.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 1])\n",
    "            file_names.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 0])\n",
Paul Primus's avatar
Paul Primus committed
560
    "        \n",
Paul Primus's avatar
Paul Primus committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
    "        scores = list(np.median(np.array(scores).T, axis=-1).reshape(-1))\n",
    "        file_names = np.array([1 if name.split('_')[0] == 'anomaly' else 0 for name in file_names[0]])      \n",
    "        a, p = sklearn.metrics.roc_auc_score(file_names, scores), sklearn.metrics.roc_auc_score(file_names, scores, max_fpr=0.1)\n",
    "        auc.append(a)\n",
    "        pauc.append(p)\n",
    "            \n",
    "    print(f'\\t{INVERSE_CLASS_MAP[machine_type]}:\\n\\t\\taveraged_auc: {np.mean(auc)}\\n\\t\\taveraged_pauc: {np.mean(pauc)}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
579
   "execution_count": 165,
Paul Primus's avatar
Paul Primus committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
   "outputs": [],
   "source": [
    "# Create Submission 4 # mean ensemble"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
594
   "execution_count": 175,
Paul Primus's avatar
Paul Primus committed
595
596
597
598
599
600
   "outputs": [],
   "source": [
    "for machine_type, idxes in enumerate(best_idxes):\n",
    "    for machine_id in EVALUATION_ID_MAP[machine_type]:\n",
    "        file_names = []\n",
    "        scores = []\n",
Paul Primus's avatar
Paul Primus committed
601
    "        for idx in idxes[:13]:\n",
Paul Primus's avatar
Paul Primus committed
602
603
604
605
606
    "            best_model_folder = run_ids[idx]\n",
    "            src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "            src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "            scores.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 1])\n",
    "            file_names.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 0])\n",
Paul Primus's avatar
Paul Primus committed
607
    "        \n",
Paul Primus's avatar
Paul Primus committed
608
    "        scores = list(np.median(np.array(scores).T, axis=-1).reshape(-1))\n",
Paul Primus's avatar
Paul Primus committed
609
610
    "        dst_path = os.path.join('..', 'submission_package', 'task2', 'Primus_CP-JKU_task2_4')\n",
    "        dst = os.path.join(dst_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id:02d}.csv')\n",
Paul Primus's avatar
Paul Primus committed
611
    "\n",
Paul Primus's avatar
Paul Primus committed
612
    "        pd.DataFrame(list(zip(file_names[0], scores))).to_csv(dst, index=False, header=False)"
Paul Primus's avatar
Paul Primus committed
613
614
615
616
617
618
619
620
621
622
623
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
Paul Primus's avatar
Paul Primus committed
624
   "execution_count": 176,
Paul Primus's avatar
Paul Primus committed
625
626
   "outputs": [
    {
Paul Primus's avatar
Paul Primus committed
627
628
629
     "name": "stdout",
     "text": [
      "\tfan:\n",
Paul Primus's avatar
Paul Primus committed
630
631
      "\t\taveraged_auc: 0.9230309586203593\n",
      "\t\taveraged_pauc: 0.8285210664235381\n",
Paul Primus's avatar
Paul Primus committed
632
      "\tpump:\n",
Paul Primus's avatar
Paul Primus committed
633
634
      "\t\taveraged_auc: 0.9146627457465693\n",
      "\t\taveraged_pauc: 0.8678065821022478\n",
Paul Primus's avatar
Paul Primus committed
635
      "\tslider:\n",
Paul Primus's avatar
Paul Primus committed
636
637
      "\t\taveraged_auc: 0.9822893258426966\n",
      "\t\taveraged_pauc: 0.9108762073723635\n",
Paul Primus's avatar
Paul Primus committed
638
      "\tToyCar:\n",
Paul Primus's avatar
Paul Primus committed
639
640
      "\t\taveraged_auc: 0.9504648370497427\n",
      "\t\taveraged_pauc: 0.8890240180210388\n",
Paul Primus's avatar
Paul Primus committed
641
      "\tToyConveyor:\n",
Paul Primus's avatar
Paul Primus committed
642
643
      "\t\taveraged_auc: 0.8254031447576784\n",
      "\t\taveraged_pauc: 0.7026976317730064\n",
Paul Primus's avatar
Paul Primus committed
644
      "\tvalve:\n",
Paul Primus's avatar
Paul Primus committed
645
646
      "\t\taveraged_auc: 0.93825\n",
      "\t\taveraged_pauc: 0.8792763157894736\n"
Paul Primus's avatar
Paul Primus committed
647
648
     ],
     "output_type": "stream"
Paul Primus's avatar
Paul Primus committed
649
650
651
652
    }
   ],
   "source": [
    "\n",
Paul Primus's avatar
Paul Primus committed
653
654
655
656
657
658
    "for machine_type, idxes in enumerate(best_idxes):\n",
    "    auc = []\n",
    "    pauc = []\n",
    "    for machine_id in TRAINING_ID_MAP[machine_type]:\n",
    "        file_names = []\n",
    "        scores = []\n",
Paul Primus's avatar
Paul Primus committed
659
    "        for idx in idxes[:13]:\n",
Paul Primus's avatar
Paul Primus committed
660
661
662
663
664
665
    "            best_model_folder = run_ids[idx]\n",
    "            src_path = os.path.join('..', 'experiment_logs', best_model_folder)\n",
    "            src = os.path.join(src_path, f'anomaly_score_{INVERSE_CLASS_MAP[machine_type]}_id_{machine_id}_mean.csv')\n",
    "            scores.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 1])\n",
    "            file_names.append(pd.read_csv(src, names=['file_name', 'score'], index_col=False).to_numpy()[:, 0])\n",
    "        \n",
Paul Primus's avatar
Paul Primus committed
666
    "        scores = list(np.median(np.array(scores).T, axis=-1).reshape(-1))\n",
Paul Primus's avatar
Paul Primus committed
667
668
669
670
671
672
    "        file_names = np.array([1 if name.split('_')[0] == 'anomaly' else 0 for name in file_names[0]])      \n",
    "        a, p = sklearn.metrics.roc_auc_score(file_names, scores), sklearn.metrics.roc_auc_score(file_names, scores, max_fpr=0.1)\n",
    "        auc.append(a)\n",
    "        pauc.append(p)\n",
    "            \n",
    "    print(f'\\t{INVERSE_CLASS_MAP[machine_type]}:\\n\\t\\taveraged_auc: {np.mean(auc)}\\n\\t\\taveraged_pauc: {np.mean(pauc)}')"
Paul Primus's avatar
Paul Primus committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}