Test computing time of large time series-checkpoint.ipynb 24.4 KB
Newer Older
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PSEUDo vs. DTW: EEG Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this experiment we will compare the LSH algorithm of PSEUDo to DTW using an EEG dataset. The metrics we will be comparing these two algorithms with are **computing time**, **recall** and **precision**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We first load the EEG data and convert it to a numpy array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
28
   "outputs": [],
29
30
31
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
32
    "from time import time\n",
33
34
35
    "\n",
    "datafile = 'data/21.csv'\n",
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
36
37
38
    "N = 100\n",
    "T = 100\n",
    "M = 100000\n",
39
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
40
    "data = np.random.uniform(size=(M, T, N))\n",
41
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
42
43
    "#and convert it to numpy array:\n",
    "data = np.array(data, dtype = \"float32\")"
44
45
46
47
   ]
  },
  {
   "cell_type": "code",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
48
   "execution_count": 2,
49
   "metadata": {},
50
   "outputs": [],
51
52
53
   "source": [
    "from sklearn import preprocessing\n",
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
54
    "\n"
55
56
57
58
59
60
61
62
63
64
65
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We sample a number of subwindows which will be used as query for the search algorithms"
   ]
  },
  {
   "cell_type": "code",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
66
   "execution_count": 3,
67
   "metadata": {},
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
68
69
70
71
72
73
74
75
76
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18816, 57332, 185890, 63757, 164364, 111536, 111858, 37823, 45128, 143695]\n"
     ]
    }
   ],
77
78
   "source": [
    "import random\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
79
    "from time import time\n",
80
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
81
    "targets = random.sample(list(range(len(data))), 10)\n",
82
83
84
85
86
87
88
    "print(targets)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
89
90
91
92
93
94
95
96
97
    "## SAX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
98
    "# from tslearn.piecewise import SymbolicAggregateApproximation\n",
99
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
100
101
102
103
104
    "# t0 = time()\n",
    "# sax = SymbolicAggregateApproximation(n_segments=T, alphabet_size_avg=10)\n",
    "# sax_data = sax.fit_transform(data)\n",
    "# print('Done! Took {:.2f} seconds ({:.1f} minutes).'.format(time() - t0, (time() - t0) / 60))\n",
    "# sax_preprocess_time = time() - t0"
105
106
107
108
109
110
111
112
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
113
114
115
116
117
118
119
120
121
122
    "# t0 = time()\n",
    "# all_sax_candidates = []\n",
    "# for i, target in enumerate(targets):\n",
    "#     t1 = time()\n",
    "#     query = sax_data[target]\n",
    "#     sax_distances = [np.linalg.norm(query - window) for window in sax_data]\n",
    "#     print('Target #{} done! Took {:.2f} seconds ({:.1f} minutes).'.format(i, time() - t1, (time() - t1) / 60))\n",
    "#     sax_candidates = sorted(range(len(sax_distances)), key=lambda k: sax_distances[k])\n",
    "#     all_sax_candidates.append(sax_candidates)\n",
    "# sax_time = time() - t0"
123
124
125
126
127
128
129
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PSEUDo"
130
131
132
133
134
135
136
137
138
139
140
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the LSH algorithm some preprocessing is done to find the right LSH parameters."
   ]
  },
  {
   "cell_type": "code",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
141
   "execution_count": 4,
142
   "metadata": {},
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
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
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preprocessing:\n",
      "r = 50\n",
      "r = 25.0\n",
      "r = 37.5\n",
      "r = 18.75\n",
      "r = 28.125\n",
      "r = 42.1875\n",
      "r = 21.09375\n",
      "r = 31.640625\n",
      "r = 15.8203125\n",
      "r = 23.73046875\n",
      "r = 35.595703125\n",
      "r = 17.7978515625\n",
      "r = 26.69677734375\n",
      "r = 40.045166015625\n",
      "r = 20.0225830078125\n",
      "r = 30.03387451171875\n",
      "r = 15.016937255859375\n",
      "r = 22.525405883789062\n",
      "r = 33.788108825683594\n",
      "r = 16.894054412841797\n",
      "r = 25.341081619262695\n",
      "r = 38.01162242889404\n",
      "r = 19.00581121444702\n",
      "r = 28.508716821670532\n",
      "r = 42.7630752325058\n",
      "r = 21.3815376162529\n",
      "r = 32.07230642437935\n",
      "r = 16.036153212189674\n",
      "r = 24.05422981828451\n",
      "r = 36.08134472742677\n",
      "r = 18.040672363713384\n",
      "r = 27.061008545570076\n",
      "r = 40.59151281835511\n",
      "r = 20.295756409177557\n",
      "r = 30.443634613766335\n",
      "r = 15.221817306883167\n",
      "r = 22.83272596032475\n",
      "r = 34.24908894048713\n",
      "r = 17.124544470243563\n",
      "r = 25.686816705365345\n",
      "r = 38.53022505804802\n",
      "r = 19.26511252902401\n",
      "r = 28.897668793536013\n",
      "Mean: 28.90310172004373\n",
      "Stdev: 0.16262486215758712\n",
      "Ratio mean: 0.9852851438976057\n",
      "Ratio stdev: 0.006220007778511879\n",
      "Theta: 28.483529575677153\n",
      "r: 2.6557769397251842\n",
      "Preprocessing time: 34.78849124908447\n",
      "Preprocessing done. Took 34.79 seconds (0.6 minutes).\n"
     ]
    }
   ],
203
204
205
206
207
208
209
210
211
212
213
214
215
   "source": [
    "import sys\n",
    "\n",
    "sys.path.insert(0, '../Flaskserver')\n",
    "import importlib\n",
    "from pseudo import preprocess\n",
    "import _lsh\n",
    "\n",
    "topk_dtw = []\n",
    "\n",
    "print('Preprocessing:')\n",
    "t0 = time()\n",
    "r,a,sd = preprocess(data, data.shape[2])\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
216
217
    "print('Preprocessing done. Took {:.2f} seconds ({:.1f} minutes).'.format(time() - t0, (time() - t0) / 60))\n",
    "pseudo_preprocess_time = time() - t0"
218
219
220
221
222
223
224
225
226
227
228
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we run the LSH algorithm for all targets and calculate the most similar subwindows"
   ]
  },
  {
   "cell_type": "code",
229
   "execution_count": null,
230
   "metadata": {},
231
   "outputs": [],
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
   "source": [
    "from collections import defaultdict\n",
    "t0 = time()\n",
    "total_lsh_times = []\n",
    "all_lsh_candidates = []\n",
    "for i, target in enumerate(targets):\n",
    "    t1 = time()\n",
    "    query = data[target]\n",
    "    print('doing lsh')\n",
    "    lsh_candidates, lsh_distances, _ = _lsh.lsh(data, query, r, a, sd, 0)\n",
    "#     topk_dtw.append(candidates)\n",
    "    dict = defaultdict(int)\n",
    "    for l in range(len(lsh_candidates)):\n",
    "        for k in range(len(lsh_candidates[0])):\n",
    "            for a in range(len(lsh_candidates[0][0])):\n",
    "                dict[lsh_candidates[l][k][a]] += lsh_distances[l][k][a]\n",
    "    sorted_dict = {k: v for k, v in sorted(dict.items(), key=lambda item: item[1])}\n",
    "    candidates = list(sorted_dict.keys())\n",
    "    total_lsh_times.append(time()-t1)\n",
    "    print('Target #{} done! Took {:.2f} seconds ({:.1f} minutes).'.format(i, time() - t1, (time() - t1) / 60))\n",
    "    all_lsh_candidates.append(candidates)\n",
    "    \n",
    "# print(candidates[0:10])\n",
    "print('Done! Took {:.2f} seconds ({:.1f} minutes).'.format(time() - t0, (time() - t0) / 60))"
   ]
  },
  {
   "cell_type": "code",
260
   "execution_count": null,
261
   "metadata": {},
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "doing lsh\n",
      "Target #0 done! Took 12.94 seconds (0.2 minutes).\n",
      "doing lsh\n",
      "Target #1 done! Took 13.33 seconds (0.2 minutes).\n",
      "doing lsh\n",
      "Target #2 done! Took 13.10 seconds (0.2 minutes).\n",
      "doing lsh\n",
      "Target #3 done! Took 13.03 seconds (0.2 minutes).\n",
      "doing lsh\n",
      "Target #4 done! Took 13.21 seconds (0.2 minutes).\n",
      "doing lsh\n"
     ]
    }
   ],
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
   "source": [
    "from collections import defaultdict\n",
    "t0 = time()\n",
    "total_lsh_times_ed = []\n",
    "all_lsh_candidates_ed = []\n",
    "for i, target in enumerate(targets):\n",
    "    t1 = time()\n",
    "    query = data[target]\n",
    "    print('doing lsh')\n",
    "    lsh_candidates, lsh_distances, _ = _lsh.lsh(data, query, r, a, sd, 1)\n",
    "#     topk_dtw.append(candidates)\n",
    "    dict = defaultdict(int)\n",
    "    for l in range(len(lsh_candidates)):\n",
    "        for k in range(len(lsh_candidates[0])):\n",
    "            for a in range(len(lsh_candidates[0][0])):\n",
    "                dict[lsh_candidates[l][k][a]] += lsh_distances[l][k][a]\n",
    "    sorted_dict = {k: v for k, v in sorted(dict.items(), key=lambda item: item[1])}\n",
    "    candidates = list(sorted_dict.keys())\n",
    "    total_lsh_times_ed.append(time()-t1)\n",
    "    print('Target #{} done! Took {:.2f} seconds ({:.1f} minutes).'.format(i, time() - t1, (time() - t1) / 60))\n",
    "    all_lsh_candidates_ed.append(candidates)\n",
    "    \n",
    "# print(candidates[0:10])\n",
    "print('Done! Took {:.2f} seconds ({:.1f} minutes).'.format(time() - t0, (time() - t0) / 60))"
   ]
  },
307
308
309
310
311
312
313
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DTW"
   ]
  },
314
315
316
317
318
319
320
321
322
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We do the same for DTW"
   ]
  },
  {
   "cell_type": "code",
323
   "execution_count": null,
324
   "metadata": {},
325
   "outputs": [],
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
   "source": [
    "from scipy.spatial.distance import cdist\n",
    "from tslearn.metrics import dtw_path_from_metric\n",
    "from tslearn.metrics import dtw\n",
    "from time import time\n",
    "\n",
    "t0 = time()\n",
    "total_dtw_times = []\n",
    "all_dtw_candidates = []\n",
    "for i, target in enumerate(targets):\n",
    "    t1 = time()\n",
    "    query = data[target]\n",
    "    dtw_distances = [dtw(window, query, global_constraint='sakoe_chiba', sakoe_chiba_radius=int(0.05 * T)) for window in data]\n",
    "    dtw_candidates = sorted(range(len(dtw_distances)), key=lambda k: dtw_distances[k])\n",
    "    print('Target #{} done! Took {:.2f} seconds ({:.1f} minutes).'.format(i, time() - t1, (time() - t1) / 60))\n",
    "    total_dtw_times.append(time()-t1)\n",
    "    all_dtw_candidates.append(dtw_candidates)\n",
    "print('Done! Took {:.2f} seconds ({:.1f} minutes).'.format(time() - t0, (time() - t0) / 60))"
   ]
  },
346
347
348
349
350
351
352
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ED"
   ]
  },
353
354
  {
   "cell_type": "code",
355
   "execution_count": null,
356
   "metadata": {},
357
   "outputs": [],
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
   "source": [
    "t0 = time()\n",
    "all_ed_candidates = []\n",
    "total_ed_times = []\n",
    "for i, target in enumerate(targets):\n",
    "    t1 = time()\n",
    "    query = data[target]\n",
    "    ed_distances = [np.linalg.norm(query-window) for window in data]\n",
    "    print('Target #{} done! Took {:.2f} seconds ({:.1f} minutes).'.format(i, time() - t1, (time() - t1) / 60))\n",
    "    ed_candidates = sorted(range(len(ed_distances)), key=lambda k: ed_distances[k])\n",
    "    total_ed_times.append(time()-t1)\n",
    "    all_ed_candidates.append(ed_candidates)\n",
    "print('Done! Took {:.2f} seconds ({:.1f} minutes).'.format(time() - t0, (time() - t0) / 60))"
   ]
  },
  {
374
   "cell_type": "markdown",
375
376
   "metadata": {},
   "source": [
377
    "## Accuracy Comparison"
378
379
380
381
382
383
384
385
386
387
388
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We compare the LSH candidates to the DTW candidates and test on recall, precision and number of pruned candidates"
   ]
  },
  {
   "cell_type": "code",
389
   "execution_count": null,
390
   "metadata": {},
391
   "outputs": [],
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
   "source": [
    "k = 100\n",
    "total_recall_pseudo = []\n",
    "total_precision_pseudo = []\n",
    "total_precision2_pseudo = []\n",
    "total_pruned_pseudo = []\n",
    "for i in range(len(targets)):\n",
    "    top_10_percent = int(len(all_lsh_candidates[i]) * 0.1)\n",
    "    pruned = int(100*(1-len(all_lsh_candidates[i])/len(all_dtw_candidates[i])))\n",
    "#     print(\"Pruned: \" + str(pruned) + \"%\")\n",
    "    recall = 0\n",
    "    for index in all_dtw_candidates[i][0:k]:\n",
    "        if index in all_lsh_candidates[i]:\n",
    "            recall += 1\n",
    "#     print(\"Recall: \" + str(100*recall/k) + \"%\")\n",
    "\n",
    "    precision = 0\n",
    "    for index in all_dtw_candidates[i][0:k]:\n",
    "        if index in all_lsh_candidates[i][0:k]:\n",
    "            precision += 1\n",
    "#     print(\"Precision: \" + str(100*precision/k) + \"%\")\n",
    "    \n",
    "    precision2 = 0\n",
    "    for index in all_lsh_candidates[i][0:k]:\n",
    "        if index in all_dtw_candidates[i][0:top_10_percent]:\n",
    "            precision2 += 1\n",
    "#     print(\"Precision 10th percentile: \" + str(100*precision2/k) + \"%\")\n",
    "    total_pruned_pseudo.append(pruned)\n",
    "    total_recall_pseudo.append(recall/k)\n",
    "    total_precision_pseudo.append(precision/k)\n",
    "    total_precision2_pseudo.append(precision2/k)\n",
    "    \n",
    "print(\"=================================================\")\n",
    "print(\"Total pruned: \" + str(np.mean(total_pruned_pseudo)) + \"%\")\n",
    "print(\"Total recall: \" + str(100 * np.mean(total_recall_pseudo)) + \"%\")\n",
    "print(\"Total precision: \" + str(100 * np.mean(total_precision_pseudo)) + \"%\")\n",
    "print(\"Total precision 2: \" + str(100 *np.mean(total_precision2_pseudo)) + \"%\")"
   ]
  },
  {
   "cell_type": "code",
433
   "execution_count": null,
434
   "metadata": {},
435
   "outputs": [],
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
   "source": [
    "total_recall_pseudo_ed = []\n",
    "total_precision_pseudo_ed = []\n",
    "total_precision2_pseudo_ed = []\n",
    "total_pruned_pseudo_ed = []\n",
    "for i in range(len(targets)):\n",
    "    top_10_percent = int(len(all_lsh_candidates_ed[i]) * 0.1)\n",
    "    pruned = int(100*(1-len(all_lsh_candidates_ed[i])/len(all_dtw_candidates[i])))\n",
    "#     print(\"Pruned: \" + str(pruned) + \"%\")\n",
    "    recall = 0\n",
    "    for index in all_dtw_candidates[i][0:k]:\n",
    "        if index in all_lsh_candidates_ed[i]:\n",
    "            recall += 1\n",
    "#     print(\"Recall: \" + str(100*recall/k) + \"%\")\n",
    "\n",
    "    precision = 0\n",
    "    for index in all_dtw_candidates[i][0:k]:\n",
    "        if index in all_lsh_candidates_ed[i][0:k]:\n",
    "            precision += 1\n",
    "#     print(\"Precision: \" + str(100*precision/k) + \"%\")\n",
    "    \n",
    "    precision2 = 0\n",
    "    for index in all_lsh_candidates_ed[i][0:k]:\n",
    "        if index in all_dtw_candidates[i][0:top_10_percent]:\n",
    "            precision2 += 1\n",
    "#     print(\"Precision 10th percentile: \" + str(100*precision2/k) + \"%\")\n",
    "    total_pruned_pseudo_ed.append(pruned)\n",
    "    total_recall_pseudo_ed.append(recall/k)\n",
    "    total_precision_pseudo_ed.append(precision/k)\n",
    "    total_precision2_pseudo_ed.append(precision2/k)\n",
    "    \n",
    "print(\"=================================================\")\n",
    "print(\"Total pruned: \" + str(np.mean(total_pruned_pseudo_ed)) + \"%\")\n",
    "print(\"Total recall: \" + str(100 * np.mean(total_recall_pseudo_ed)) + \"%\")\n",
    "print(\"Total precision: \" + str(100 * np.mean(total_precision_pseudo_ed)) + \"%\")\n",
    "print(\"Total precision 2: \" + str(100 *np.mean(total_precision2_pseudo_ed)) + \"%\")"
   ]
  },
  {
   "cell_type": "code",
476
   "execution_count": null,
477
   "metadata": {},
478
   "outputs": [],
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
   "source": [
    "total_recall_ed = []\n",
    "total_precision_ed = []\n",
    "total_precision2_ed = []\n",
    "total_pruned_ed = []\n",
    "for i in range(len(targets)):\n",
    "    top_10_percent = int(len(all_ed_candidates[i]) * 0.1)\n",
    "    pruned = int(100*(1-len(all_ed_candidates[i])/len(all_dtw_candidates[i])))\n",
    "#     print(\"Pruned: \" + str(pruned) + \"%\")\n",
    "    recall = 0\n",
    "    for index in all_dtw_candidates[i][0:k]:\n",
    "        if index in all_ed_candidates[i]:\n",
    "            recall += 1\n",
    "#     print(\"Recall: \" + str(100*recall/k) + \"%\")\n",
    "\n",
    "    precision = 0\n",
    "    for index in all_dtw_candidates[i][0:k]:\n",
    "        if index in all_ed_candidates[i][0:k]:\n",
    "            precision += 1\n",
    "#     print(\"Precision: \" + str(100*precision/k) + \"%\")\n",
    "    \n",
    "    precision2 = 0\n",
    "    for index in all_ed_candidates[i][0:k]:\n",
    "        if index in all_dtw_candidates[i][0:top_10_percent]:\n",
    "            precision2 += 1\n",
    "#     print(\"Precision 10th percentile: \" + str(100*precision2/k) + \"%\")\n",
    "    total_pruned_ed.append(pruned)\n",
    "    total_recall_ed.append(recall/k)\n",
    "    total_precision_ed.append(precision/k)\n",
    "    total_precision2_ed.append(precision2/k)\n",
    "    \n",
    "print(\"=================================================\")\n",
    "print(\"Total pruned: \" + str(np.mean(total_pruned_ed)) + \"%\")\n",
    "print(\"Total recall: \" + str(100 * np.mean(total_recall_ed)) + \"%\")\n",
    "print(\"Total precision: \" + str(100 * np.mean(total_precision_ed)) + \"%\")\n",
    "print(\"Total precision 2: \" + str(100 *np.mean(total_precision2_ed)) + \"%\")"
   ]
  },
  {
   "cell_type": "code",
519
   "execution_count": null,
520
   "metadata": {},
521
522
   "outputs": [],
   "source": [
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    "# total_recall_sax = []\n",
    "# total_precision_sax = []\n",
    "# total_precision2_sax = []\n",
    "# total_pruned_sax = []\n",
    "# for i in range(len(targets)):\n",
    "#     top_10_percent = int(len(all_sax_candidates[i]) * 0.1)\n",
    "#     pruned = int(100*(1-len(all_sax_candidates[i])/len(all_dtw_candidates[i])))\n",
    "# #     print(\"Pruned: \" + str(pruned) + \"%\")\n",
    "#     recall = 0\n",
    "#     for index in all_dtw_candidates[i][0:k]:\n",
    "#         if index in all_sax_candidates[i]:\n",
    "#             recall += 1\n",
    "# #     print(\"Recall: \" + str(100*recall/k) + \"%\")\n",
    "\n",
    "#     precision = 0\n",
    "#     for index in all_dtw_candidates[i][0:k]:\n",
    "#         if index in all_sax_candidates[i][0:k]:\n",
    "#             precision += 1\n",
    "# #     print(\"Precision: \" + str(100*precision/k) + \"%\")\n",
542
    "    \n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
543
544
545
546
547
548
549
550
551
    "#     precision2 = 0\n",
    "#     for index in all_sax_candidates[i][0:k]:\n",
    "#         if index in all_dtw_candidates[i][0:top_10_percent]:\n",
    "#             precision2 += 1\n",
    "# #     print(\"Precision 10th percentile: \" + str(100*precision2/k) + \"%\")\n",
    "#     total_pruned_sax.append(pruned)\n",
    "#     total_recall_sax.append(recall/k)\n",
    "#     total_precision_sax.append(precision/k)\n",
    "#     total_precision2_sax.append(precision2/k)\n",
552
    "    \n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
553
554
555
556
557
    "# print(\"=================================================\")\n",
    "# print(\"Total pruned: \" + str(np.mean(total_pruned_sax)) + \"%\")\n",
    "# print(\"Total recall: \" + str(100 * np.mean(total_recall_sax)) + \"%\")\n",
    "# print(\"Total precision: \" + str(100 * np.mean(total_precision_sax)) + \"%\")\n",
    "# print(\"Total precision 2: \" + str(100 *np.mean(total_precision2_sax)) + \"%\")"
558
559
560
561
562
563
564
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
565
566
567
568
569
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
570
    "labels = ['Recall', 'Precision-50', 'Precision-10%']\n",
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
    "pseudo_values = [\n",
    "    100 * np.mean(total_recall_pseudo), \n",
    "    100 * np.mean(total_precision_pseudo), \n",
    "    100 * np.mean(total_precision2_pseudo)\n",
    "]\n",
    "pseudo_error = [\n",
    "    100 * np.std(total_recall_pseudo), \n",
    "    100 * np.std(total_precision_pseudo), \n",
    "    100 * np.std(total_precision2_pseudo)\n",
    "]\n",
    "pseudo_ed_values = [\n",
    "    100 * np.mean(total_recall_pseudo_ed), \n",
    "    100 * np.mean(total_precision_pseudo_ed), \n",
    "    100 * np.mean(total_precision2_pseudo_ed)\n",
    "]\n",
    "pseudo_ed_error = [\n",
    "    100 * np.std(total_recall_pseudo_ed), \n",
    "    100 * np.std(total_precision_pseudo_ed), \n",
    "    100 * np.std(total_precision2_pseudo_ed)\n",
    "]\n",
    "ed_values = [\n",
    "    100 * np.mean(total_recall_ed), \n",
    "    100 * np.mean(total_precision_ed), \n",
    "    100 * np.mean(total_precision2_ed)\n",
    "]\n",
    "ed_error = [\n",
    "    100 * np.std(total_recall_ed), \n",
    "    100 * np.std(total_precision_ed), \n",
    "    100 * np.std(total_precision2_ed)\n",
    "]\n",
    "\n",
602
603
604
605
606
    "colors = ['#4daf4a', '#377eb8', '#ff7f00',\n",
    "          '#f781bf', '#a65628', '#984ea3',\n",
    "          '#999999', '#e41a1c', '#dede00']\n",
    "\n",
    "x = 1.7 * np.arange(len(labels))  # the label locations\n",
607
608
609
610
    "width = 0.35  # the width of the bars\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "fig.set_size_inches(10, 7)\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
611
612
613
    "rects1 = ax.bar(x - width, pseudo_values, width, yerr=pseudo_error, color=colors[0], capsize=10, label='PSEUDo (DTW)')\n",
    "rects2 = ax.bar(x, pseudo_ed_values, width, yerr=pseudo_ed_error, color=colors[1], capsize=10, label='PSEUDo (ED)')\n",
    "rects3 = ax.bar(x + width, ed_values, width, yerr=ed_error, color=colors[6], capsize=10, label='ED')\n",
614
615
    "\n",
    "ax.set_ylabel('% Relative to DTW')\n",
616
    "ax.set_title('Recall and precision compared to DTW [EEG: M={}, T={}, d={}]'.format(M, T, N))\n",
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
    "ax.set_xticks(x)\n",
    "ax.set_xticklabels(labels)\n",
    "ax.legend()\n",
    "\n",
    "\n",
    "def autolabel(rects):\n",
    "    \"\"\"Attach a text label above each bar in *rects*, displaying its height.\"\"\"\n",
    "    for rect in rects:\n",
    "        height = round(rect.get_height(),0)\n",
    "        ax.annotate('{}'.format(height)+'%',\n",
    "                    xy=(rect.get_x() + rect.get_width() / 2, height),\n",
    "                    xytext=(0, 3),  # 3 points vertical offset\n",
    "                    textcoords=\"offset points\",\n",
    "                    ha='center', va='bottom')\n",
    "\n",
    "\n",
    "autolabel(rects1)\n",
    "autolabel(rects2)\n",
635
    "autolabel(rects3)\n",
636
637
    "\n",
    "fig.tight_layout()\n",
638
    "plt.savefig('images/accuracy_eeg_' + str(M) + '_' + str(T) +'_' + str(N))\n",
639
640
641
    "plt.show()"
   ]
  },
642
643
644
645
646
647
648
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Computing time"
   ]
  },
649
650
651
652
653
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
654
655
656
657
658
659
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "\n",
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
660
661
662
    "labels = ['PSEUDo (DTW)', 'PSEUDo (ED)', 'DTW', 'L2']\n",
    "preprocess_vales = [pseudo_preprocess_time, pseudo_preprocess_time, 0, 0]\n",
    "query_values = np.array([np.sum(total_lsh_times), np.sum(total_lsh_times_ed), np.sum(total_dtw_times), np.sum(total_ed_times)])\n",
663
664
665
666
667
668
669
670
671
672
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
    "\n",
    "x = np.arange(len(labels))\n",
    "width = 0.35\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "fig.set_size_inches(10, 7)\n",
    "rects1 = ax.bar(x - width/2, preprocess_vales, width, color=colors[1], label='Preprocessing')\n",
    "rects2 = ax.bar(x + width/2, query_values, width, color=colors[0], label='Querying')\n",
    "\n",
    "ax.set_ylabel('Time (s)')\n",
    "ax.set_title('Processing times of various search strategies [EEG: M={}, T={}, d={}]'.format(M, T, N))\n",
    "ax.set_xticks(x)\n",
    "ax.set_xticklabels(labels)\n",
    "ax.legend()\n",
    "\n",
    "\n",
    "def autolabel(rects):\n",
    "    \"\"\"Attach a text label above each bar in *rects*, displaying its height.\"\"\"\n",
    "    for rect in rects:\n",
    "        height = round(rect.get_height(),2)\n",
    "        ax.annotate('{}'.format(height),\n",
    "                    xy=(rect.get_x() + rect.get_width() / 2, height),\n",
    "                    xytext=(0, 3),  # 3 points vertical offset\n",
    "                    textcoords=\"offset points\",\n",
    "                    ha='center', va='bottom')\n",
    "\n",
    "\n",
    "autolabel(rects1)\n",
    "autolabel(rects2)\n",
    "\n",
    "fig.tight_layout()\n",
    "plt.savefig('images/time_eeg_' + str(M) + '_' + str(T) +'_' + str(N))\n",
    "\n",
    "plt.show()"
   ]
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}