deepof_model_evaluation.ipynb 27.9 KB
Newer Older
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
5
   "execution_count": null,
6
7
8
9
10
11
12
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
13
14
  {
   "cell_type": "code",
15
   "execution_count": null,
16
17
18
19
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
20
    "\n",
21
22
23
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# deepOF model evaluation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given a dataset and a trained model, this notebook allows the user to \n",
    "\n",
    "* Load and inspect the different models (encoder, decoder, grouper, gmvaep)\n",
    "* Visualize reconstruction quality for a given model\n",
    "* Visualize a static latent space\n",
    "* Visualize trajectories on the latent space for a given video\n",
    "* sample from the latent space distributions and generate video clips showcasing generated data"
   ]
  },
  {
   "cell_type": "code",
46
   "execution_count": null,
47
48
49
50
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
51
    "\n",
52
53
54
55
56
    "os.chdir(os.path.dirname(\"../\"))"
   ]
  },
  {
   "cell_type": "code",
57
   "execution_count": null,
58
59
60
61
62
63
64
   "metadata": {},
   "outputs": [],
   "source": [
    "import deepof.data\n",
    "import deepof.utils\n",
    "import numpy as np\n",
    "import pandas as pd\n",
65
    "import re\n",
66
    "import tensorflow as tf\n",
67
    "from collections import Counter\n",
68
69
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
70
71
72
73
74
    "from sklearn.manifold import TSNE\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis\n",
    "import umap\n",
    "\n",
75
    "from ipywidgets import interactive, interact, HBox, Layout, VBox\n",
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    "from IPython import display\n",
    "from matplotlib.animation import FuncAnimation\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Define and run project"
   ]
  },
  {
   "cell_type": "code",
93
   "execution_count": null,
94
95
96
97
   "metadata": {},
   "outputs": [],
   "source": [
    "path = os.path.join(\"..\", \"..\", \"Desktop\", \"deepoftesttemp\")\n",
98
99
100
    "trained_network = os.path.join(\"..\", \"..\", \"Desktop\", \"trained_weights\")\n",
    "exclude_bodyparts = tuple([\"\"])\n",
    "window_size = 24"
101
102
103
104
   ]
  },
  {
   "cell_type": "code",
105
   "execution_count": null,
106
   "metadata": {},
107
   "outputs": [],
108
109
110
   "source": [
    "%%time\n",
    "proj = deepof.data.project(\n",
111
    "    path=path, smooth_alpha=0.999, exclude_bodyparts=exclude_bodyparts, arena_dims=[380],\n",
112
113
114
115
116
    ")"
   ]
  },
  {
   "cell_type": "code",
117
   "execution_count": null,
118
   "metadata": {},
119
   "outputs": [],
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
   "source": [
    "%%time\n",
    "proj = proj.run(verbose=True)\n",
    "print(proj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Load pretrained deepof model"
   ]
  },
  {
   "cell_type": "code",
135
   "execution_count": null,
136
137
138
   "metadata": {},
   "outputs": [],
   "source": [
139
140
141
142
    "coords = proj.get_coords(center=\"Center\", align=\"Spine_1\", align_inplace=True)\n",
    "data_prep = coords.preprocess(test_videos=0, window_step=1, window_size=window_size, shuffle=True)[\n",
    "    0\n",
    "]"
143
144
145
146
   ]
  },
  {
   "cell_type": "code",
147
   "execution_count": null,
148
   "metadata": {},
149
   "outputs": [],
150
151
152
153
154
155
   "source": [
    "[i for i in os.listdir(trained_network) if i.endswith(\"h5\")]"
   ]
  },
  {
   "cell_type": "code",
156
   "execution_count": null,
157
   "metadata": {},
158
   "outputs": [],
159
160
161
162
163
164
165
   "source": [
    "deepof_weights = [i for i in os.listdir(trained_network) if i.endswith(\"h5\")][-1]\n",
    "deepof_weights"
   ]
  },
  {
   "cell_type": "code",
166
   "execution_count": null,
167
168
169
   "metadata": {},
   "outputs": [],
   "source": [
170
171
172
173
174
175
    "# Set model parameters\n",
    "encoding = int(re.findall(\"encoding=(\\d+)_\", deepof_weights)[0])\n",
    "k = int(re.findall(\"k=(\\d+)_\", deepof_weights)[0])\n",
    "loss = re.findall(\"loss=(.+?)_\", deepof_weights)[0]\n",
    "pheno = 0\n",
    "predictor = 0"
176
177
178
179
   ]
  },
  {
   "cell_type": "code",
180
   "execution_count": null,
181
   "metadata": {},
lucas_miranda's avatar
lucas_miranda committed
182
   "outputs": [],
183
   "source": [
184
185
186
187
188
189
190
191
    "(\n",
    "    encode_to_vector,\n",
    "    decoder,\n",
    "    grouper,\n",
    "    gmvaep,\n",
    "    prior,\n",
    "    posterior,\n",
    ") = deepof.models.SEQ_2_SEQ_GMVAE(\n",
192
193
194
195
196
197
    "    loss=loss,\n",
    "    number_of_components=k,\n",
    "    compile_model=True,\n",
    "    encoding=encoding,\n",
    "    predictor=predictor,\n",
    "    phenotype_prediction=pheno,\n",
198
199
200
    ").build(\n",
    "    data_prep.shape\n",
    ")\n",
201
    "\n",
202
    "gmvaep.load_weights(os.path.join(trained_network, deepof_weights))"
203
204
205
206
   ]
  },
  {
   "cell_type": "code",
207
   "execution_count": null,
208
209
210
211
212
213
214
215
216
217
218
219
220
221
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Uncomment to see model summaries\n",
    "# encoder.summary()\n",
    "# decoder.summary()\n",
    "# grouper.summary()\n",
    "# gmvaep.summary()"
   ]
  },
  {
   "cell_type": "code",
222
   "execution_count": null,
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
   "metadata": {},
   "outputs": [],
   "source": [
    "# Uncomment to plot model structure\n",
    "def plot_model(model, name):\n",
    "    tf.keras.utils.plot_model(\n",
    "        model,\n",
    "        to_file=os.path.join(\n",
    "            path,\n",
    "            \"deepof_{}_{}.png\".format(name, datetime.now().strftime(\"%Y%m%d-%H%M%S\")),\n",
    "        ),\n",
    "        show_shapes=True,\n",
    "        show_dtype=False,\n",
    "        show_layer_names=True,\n",
    "        rankdir=\"TB\",\n",
    "        expand_nested=True,\n",
    "        dpi=200,\n",
    "    )\n",
    "\n",
    "\n",
    "# plot_model(encoder, \"encoder\")\n",
    "# plot_model(decoder, \"decoder\")\n",
    "# plot_model(grouper, \"grouper\")\n",
    "# plot_model(gmvaep, \"gmvaep\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
253
    "### 4. Evaluate reconstruction (to be incorporated into deepof.evaluate)"
254
255
256
257
   ]
  },
  {
   "cell_type": "code",
258
   "execution_count": null,
259
260
261
   "metadata": {},
   "outputs": [],
   "source": [
262
263
264
265
266
267
268
269
270
271
272
273
    "# Auxiliary animation functions\n",
    "\n",
    "\n",
    "def plot_mouse_graph(instant_x, instant_y, instant_rec_x, instant_rec_y, ax, edges):\n",
    "    \"\"\"Generates a graph plot of the mouse\"\"\"\n",
    "    plots = []\n",
    "    rec_plots = []\n",
    "    for edge in edges:\n",
    "        (temp_plot,) = ax.plot(\n",
    "            [float(instant_x[edge[0]]), float(instant_x[edge[1]])],\n",
    "            [float(instant_y[edge[0]]), float(instant_y[edge[1]])],\n",
    "            color=\"#006699\",\n",
274
    "            linewidth=2.0,\n",
275
276
277
278
    "        )\n",
    "        (temp_rec_plot,) = ax.plot(\n",
    "            [float(instant_rec_x[edge[0]]), float(instant_rec_x[edge[1]])],\n",
    "            [float(instant_rec_y[edge[0]]), float(instant_rec_y[edge[1]])],\n",
279
280
    "            color=\"red\",\n",
    "            linewidth=2.0,\n",
281
282
283
    "        )\n",
    "        plots.append(temp_plot)\n",
    "        rec_plots.append(temp_rec_plot)\n",
284
    "    return plots, rec_plots\n",
285
286
    "\n",
    "\n",
287
    "def update_mouse_graph(x, y, rec_x, rec_y, plots, rec_plots, edges):\n",
288
289
290
291
292
293
    "    \"\"\"Updates the graph plot to enable animation\"\"\"\n",
    "\n",
    "    for plot, edge in zip(plots, edges):\n",
    "        plot.set_data(\n",
    "            [float(x[edge[0]]), float(x[edge[1]])],\n",
    "            [float(y[edge[0]]), float(y[edge[1]])],\n",
294
295
296
297
298
    "        )\n",
    "    for plot, edge in zip(rec_plots, edges):\n",
    "        plot.set_data(\n",
    "            [float(rec_x[edge[0]]), float(rec_x[edge[1]])],\n",
    "            [float(rec_y[edge[0]]), float(rec_y[edge[1]])],\n",
299
300
301
302
303
    "        )"
   ]
  },
  {
   "cell_type": "code",
304
   "execution_count": null,
305
306
307
   "metadata": {
    "scrolled": false
   },
308
   "outputs": [],
309
310
311
   "source": [
    "# Display a video with the original data superimposed with the reconstructions\n",
    "\n",
312
    "coords = proj.get_coords(center=\"Center\", align=\"Spine_1\", align_inplace=True)\n",
313
    "random_exp = np.random.choice(list(coords.keys()), 1)[0]\n",
314
    "print(random_exp)\n",
315
316
    "\n",
    "\n",
317
    "def animate_mice_across_time(random_exp):\n",
318
    "\n",
319
    "    # Define canvas\n",
320
321
    "    fig, ax = plt.subplots(1, 1, figsize=(10, 10))\n",
    "\n",
322
    "    # Retrieve body graph\n",
323
324
325
    "    edges = deepof.utils.connect_mouse_topview()\n",
    "\n",
    "    for bpart in exclude_bodyparts:\n",
326
327
    "        if bpart:\n",
    "            edges.remove_node(bpart)\n",
328
329
330
    "\n",
    "    for limb in [\"Left_fhip\", \"Right_fhip\", \"Left_bhip\", \"Right_bhip\"]:\n",
    "        edges.remove_edge(\"Center\", limb)\n",
331
332
    "        if (\"Tail_base\", limb) in edges.edges():\n",
    "            edges.remove_edge(\"Tail_base\", limb)\n",
333
334
335
    "\n",
    "    edges = edges.edges()\n",
    "\n",
336
337
338
339
340
341
342
343
344
    "    # Compute observed and predicted data to plot\n",
    "    data = coords[random_exp]\n",
    "    coords_rec = coords.filter_videos([random_exp])\n",
    "    data_prep = coords_rec.preprocess(\n",
    "        test_videos=0, window_step=1, window_size=window_size, shuffle=False\n",
    "    )[0]\n",
    "\n",
    "    data_rec = gmvaep.predict(data_prep)\n",
    "    data_rec = pd.DataFrame(coords_rec._scaler.inverse_transform(data_rec[:, 6, :]))\n",
345
    "    data_rec.columns = data.columns\n",
346
347
    "    data = pd.DataFrame(coords_rec._scaler.inverse_transform(data_prep[:, 6, :]))\n",
    "    data.columns = data_rec.columns\n",
348
    "\n",
349
    "    # Add Central coordinate, lost during alignment\n",
350
351
352
353
354
    "    data[\"Center\", \"x\"] = 0\n",
    "    data[\"Center\", \"y\"] = 0\n",
    "    data_rec[\"Center\", \"x\"] = 0\n",
    "    data_rec[\"Center\", \"y\"] = 0\n",
    "\n",
355
    "    # Plot!\n",
356
357
358
359
360
    "    init_x = data.xs(\"x\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "    init_y = data.xs(\"y\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "    init_rec_x = data_rec.xs(\"x\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "    init_rec_y = data_rec.xs(\"y\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "\n",
361
362
363
364
365
366
    "    plots, rec_plots = plot_mouse_graph(\n",
    "        init_x, init_y, init_rec_x, init_rec_y, ax, edges\n",
    "    )\n",
    "    scatter = ax.scatter(\n",
    "        x=np.array(init_x), y=np.array(init_y), color=\"#006699\", label=\"Original\"\n",
    "    )\n",
367
    "    rec_scatter = ax.scatter(\n",
368
369
370
371
    "        x=np.array(init_rec_x),\n",
    "        y=np.array(init_rec_y),\n",
    "        color=\"red\",\n",
    "        label=\"Reconstruction\",\n",
372
373
374
375
376
377
378
379
380
    "    )\n",
    "\n",
    "    # Update data in main plot\n",
    "    def animation_frame(i):\n",
    "        # Update scatter plot\n",
    "        x = data.xs(\"x\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        y = data.xs(\"y\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        rec_x = data_rec.xs(\"x\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        rec_y = data_rec.xs(\"y\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
381
    "\n",
382
    "        scatter.set_offsets(np.c_[np.array(x), np.array(y)])\n",
383
384
    "        rec_scatter.set_offsets(np.c_[np.array(rec_x), np.array(rec_y)])\n",
    "        update_mouse_graph(x, y, rec_x, rec_y, plots, rec_plots, edges)\n",
385
386
387
    "\n",
    "        return scatter\n",
    "\n",
388
    "    animation = FuncAnimation(fig, func=animation_frame, frames=250, interval=50,)\n",
389
    "\n",
390
    "    ax.set_title(\"Original versus reconstructed data\")\n",
391
392
393
394
    "    ax.set_ylim(-100, 60)\n",
    "    ax.set_xlim(-60, 60)\n",
    "    ax.set_xlabel(\"x\")\n",
    "    ax.set_ylabel(\"y\")\n",
395
    "    plt.legend()\n",
396
397
398
399
    "\n",
    "    video = animation.to_html5_video()\n",
    "    html = display.HTML(video)\n",
    "    display.display(html)\n",
400
401
402
403
    "    plt.close()\n",
    "\n",
    "\n",
    "animate_mice_across_time(random_exp)"
404
405
406
407
408
409
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
410
    "### 5. Evaluate latent space (to be incorporated into deepof.evaluate)"
411
412
413
414
   ]
  },
  {
   "cell_type": "code",
415
   "execution_count": null,
416
417
   "metadata": {},
   "outputs": [],
418
419
420
421
422
423
   "source": [
    "# Get encodings and groupings for the same random video as above\n",
    "data_prep = coords.preprocess(\n",
    "    test_videos=0, window_step=1, window_size=window_size, shuffle=False\n",
    ")[0]\n",
    "\n",
424
    "encodings = encode_to_vector.predict(data_prep)\n",
425
426
427
    "groupings = grouper.predict(data_prep)\n",
    "hard_groups = np.argmax(groupings, axis=1)"
   ]
428
  },
429
430
  {
   "cell_type": "code",
431
   "execution_count": null,
432
   "metadata": {},
433
   "outputs": [],
434
435
   "source": [
    "@interact(minimum_confidence=(0.0, 1.0, 0.01))\n",
436
    "def plot_cluster_population(minimum_confidence):\n",
437
438
439
440
441
442
443
    "    plt.figure(figsize=(12, 8))\n",
    "\n",
    "    groups = hard_groups[np.max(groupings, axis=1) > minimum_confidence].flatten()\n",
    "    groups = np.concatenate([groups, np.arange(25)])\n",
    "    sns.countplot(groups)\n",
    "    plt.xlabel(\"Cluster\")\n",
    "    plt.title(\"Training instances per cluster\")\n",
444
    "    plt.ylim(0, hard_groups.shape[0] * 1.1)\n",
445
446
447
448
449
450
451
452
453
454
455
456
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The slider in the figure above lets you set the minimum confidence the model may yield when assigning a training instance to a cluster in order to be visualized."
   ]
  },
  {
   "cell_type": "code",
457
   "execution_count": null,
458
   "metadata": {},
459
   "outputs": [],
460
461
462
   "source": [
    "# Plot real data in the latent space\n",
    "\n",
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    "samples = np.random.choice(range(encodings.shape[0]), 10000)\n",
    "sample_enc = encodings[samples, :]\n",
    "sample_grp = groupings[samples, :]\n",
    "sample_hgr = hard_groups[samples]\n",
    "k = sample_grp.shape[1]\n",
    "\n",
    "umap_reducer = umap.UMAP(n_components=2)\n",
    "pca_reducer = PCA(n_components=2)\n",
    "tsne_reducer = TSNE(n_components=2)\n",
    "lda_reducer = LinearDiscriminantAnalysis(n_components=2)\n",
    "\n",
    "umap_enc = umap_reducer.fit_transform(sample_enc)\n",
    "pca_enc = pca_reducer.fit_transform(sample_enc)\n",
    "tsne_enc = tsne_reducer.fit_transform(sample_enc)\n",
    "try:\n",
    "    lda_enc = lda_reducer.fit_transform(sample_enc, sample_hgr)\n",
    "except ValueError:\n",
    "    warnings.warn(\n",
    "        \"Only one class found. Can't use LDA\", DeprecationWarning, stacklevel=2\n",
    "    )\n",
    "\n",
484
485
486
    "\n",
    "@interact(\n",
    "    minimum_confidence=(0.0, 0.99, 0.01),\n",
487
    "    dim_red=[\"PCA\", \"LDA\", \"umap\", \"tSNE\"],\n",
488
489
    "    highlight_clusters=False,\n",
    "    selected_cluster=(0, k-1),\n",
490
    ")\n",
491
492
493
494
495
496
    "def plot_static_latent_space(\n",
    "    minimum_confidence, dim_red, highlight_clusters, selected_cluster\n",
    "):\n",
    "\n",
    "    global sample_enc, sample_grp, sample_hgr\n",
    "\n",
497
    "    if dim_red == \"umap\":\n",
498
    "        enc = umap_enc\n",
499
    "    elif dim_red == \"LDA\":\n",
500
    "        enc = lda_enc\n",
501
    "    elif dim_red == \"PCA\":\n",
502
    "        enc = pca_enc\n",
503
    "    else:\n",
504
    "        enc = tsne_enc\n",
505
    "\n",
506
507
508
    "    enc = enc[np.max(sample_grp, axis=1) > minimum_confidence]\n",
    "    hgr = sample_hgr[np.max(sample_grp, axis=1) > minimum_confidence].flatten()\n",
    "    grp = sample_grp[np.max(sample_grp, axis=1) > minimum_confidence]\n",
509
510
511
    "\n",
    "    plt.figure(figsize=(12, 8))\n",
    "\n",
512
513
514
    "    sns.scatterplot(\n",
    "        x=enc[:, 0],\n",
    "        y=enc[:, 1],\n",
515
516
    "        hue=hgr,\n",
    "        size=np.max(grp, axis=1),\n",
517
    "        sizes=(1, 100),\n",
518
    "        palette=sns.color_palette(\"husl\", len(set(hgr))),\n",
519
    "    )\n",
520
521
522
523
524
525
526
527
    "    \n",
    "    if highlight_clusters:\n",
    "        sns.kdeplot(\n",
    "            enc[hgr == selected_cluster, 0],\n",
    "            enc[hgr == selected_cluster, 1],\n",
    "            color=\"red\",\n",
    "        )\n",
    "    \n",
528
529
    "    plt.xlabel(\"{} 1\".format(dim_red))\n",
    "    plt.ylabel(\"{} 2\".format(dim_red))\n",
530
    "    plt.suptitle(\"Static view of trained latent space\")\n",
531
532
    "    plt.show()"
   ]
533
  },
534
535
  {
   "cell_type": "code",
536
   "execution_count": null,
537
538
   "metadata": {},
   "outputs": [],
539
   "source": [
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
    "def plot_mouse_graph(instant_x, instant_y, ax, edges):\n",
    "    \"\"\"Generates a graph plot of the mouse\"\"\"\n",
    "    plots = []\n",
    "    for edge in edges:\n",
    "        (temp_plot,) = ax.plot(\n",
    "            [float(instant_x[edge[0]]), float(instant_x[edge[1]])],\n",
    "            [float(instant_y[edge[0]]), float(instant_y[edge[1]])],\n",
    "            color=\"#006699\",\n",
    "            linewidth=2.0,\n",
    "        )\n",
    "        plots.append(temp_plot)\n",
    "    return plots\n",
    "\n",
    "\n",
    "def update_mouse_graph(x, y, plots, edges):\n",
    "    \"\"\"Updates the graph plot to enable animation\"\"\"\n",
    "\n",
    "    for plot, edge in zip(plots, edges):\n",
    "        plot.set_data(\n",
    "            [float(x[edge[0]]), float(x[edge[1]])],\n",
    "            [float(y[edge[0]]), float(y[edge[1]])],\n",
    "        )"
562
   ]
563
564
565
  },
  {
   "cell_type": "code",
566
   "execution_count": null,
567
568
569
   "metadata": {
    "scrolled": false
   },
570
   "outputs": [],
571
572
573
   "source": [
    "# Plot trajectory of a video in latent space\n",
    "\n",
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
    "samples = np.random.choice(range(encodings.shape[0]), 10000)\n",
    "sample_enc = encodings[samples, :]\n",
    "sample_grp = groupings[samples, :]\n",
    "sample_hgr = hard_groups[samples]\n",
    "k = sample_grp.shape[1]\n",
    "\n",
    "umap_reducer = umap.UMAP(n_components=2)\n",
    "pca_reducer = PCA(n_components=2)\n",
    "tsne_reducer = TSNE(n_components=2)\n",
    "lda_reducer = LinearDiscriminantAnalysis(n_components=2)\n",
    "\n",
    "umap_enc = umap_reducer.fit_transform(sample_enc)\n",
    "pca_enc = pca_reducer.fit_transform(sample_enc)\n",
    "tsne_enc = tsne_reducer.fit_transform(sample_enc)\n",
    "try:\n",
    "    lda_enc = lda_reducer.fit_transform(sample_enc, sample_hgr)\n",
    "except ValueError:\n",
    "    warnings.warn(\n",
    "        \"Only one class found. Can't use LDA\", DeprecationWarning, stacklevel=2\n",
    "    )\n",
    "\n",
595
596
    "\n",
    "@interact(\n",
597
    "    trajectory=(100, 500), trace=False, dim_red=[\"PCA\", \"LDA\", \"umap\", \"tSNE\"],\n",
598
    ")\n",
599
600
601
602
    "def plot_dynamic_latent_pace(trajectory, trace, dim_red):\n",
    "\n",
    "    global sample_enc, sample_grp, sample_hgr\n",
    "\n",
603
    "    if dim_red == \"umap\":\n",
604
    "        enc = umap_enc\n",
605
    "    elif dim_red == \"LDA\":\n",
606
    "        enc = lda_enc\n",
607
    "    elif dim_red == \"PCA\":\n",
608
    "        enc = pca_enc\n",
609
    "    else:\n",
610
    "        enc = tsne_enc\n",
611
    "\n",
612
613
614
    "    traj_enc = enc[:trajectory, :]\n",
    "    traj_grp = enc[:trajectory, :]\n",
    "    traj_hgr = enc[:trajectory]\n",
615
616
617
618
619
620
621
622
    "\n",
    "    # Define two figures arranged horizontally\n",
    "    fig, (ax, ax2) = plt.subplots(\n",
    "        1, 2, figsize=(12, 8), gridspec_kw={\"width_ratios\": [3, 1.5]}\n",
    "    )\n",
    "\n",
    "    # Plot the animated embedding trajectory on the left\n",
    "    sns.scatterplot(\n",
623
624
    "        x=enc[:, 0],\n",
    "        y=enc[:, 1],\n",
625
626
627
    "        hue=sample_hgr,\n",
    "        size=np.max(sample_grp, axis=1),\n",
    "        sizes=(1, 100),\n",
628
    "        palette=sns.color_palette(\"husl\", len(set(sample_hgr))),\n",
629
630
631
    "        ax=ax,\n",
    "    )\n",
    "\n",
632
    "    traj_init = traj_enc[0, :]\n",
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
    "    scatter = ax.scatter(\n",
    "        x=[traj_init[0]], y=[traj_init[1]], s=100, color=\"red\", edgecolor=\"black\"\n",
    "    )\n",
    "    (lineplt,) = ax.plot([traj_init[0]], [traj_init[1]], color=\"red\", linewidth=2.0)\n",
    "    tracking_line_x = []\n",
    "    tracking_line_y = []\n",
    "\n",
    "    # Plot the initial data (before feeding it to the encoder) on the right\n",
    "    edges = deepof.utils.connect_mouse_topview()\n",
    "\n",
    "    for bpart in exclude_bodyparts:\n",
    "        if bpart:\n",
    "            edges.remove_node(bpart)\n",
    "\n",
    "    for limb in [\"Left_fhip\", \"Right_fhip\", \"Left_bhip\", \"Right_bhip\"]:\n",
    "        edges.remove_edge(\"Center\", limb)\n",
    "        if (\"Tail_base\", limb) in list(edges.edges()):\n",
    "            edges.remove_edge(\"Tail_base\", limb)\n",
    "\n",
    "    edges = edges.edges()\n",
    "\n",
    "    inv_coords = coords._scaler.inverse_transform(data_prep)[:, window_size // 2, :]\n",
    "    data = pd.DataFrame(inv_coords, columns=coords[random_exp].columns)\n",
    "\n",
    "    data[\"Center\", \"x\"] = 0\n",
    "    data[\"Center\", \"y\"] = 0\n",
    "\n",
    "    init_x = data.xs(\"x\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "    init_y = data.xs(\"y\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "\n",
    "    plots = plot_mouse_graph(init_x, init_y, ax2, edges)\n",
    "    track = ax2.scatter(x=np.array(init_x), y=np.array(init_y), color=\"#006699\",)\n",
    "\n",
    "    # Update data in both plots\n",
    "    def animation_frame(i):\n",
    "        # Update scatter plot\n",
669
    "        offset = traj_enc[i, :]\n",
670
671
672
673
674
675
676
677
678
679
    "\n",
    "        prev_t = scatter.get_offsets()[0]\n",
    "\n",
    "        if trace:\n",
    "            tracking_line_x.append([prev_t[0], offset[0]])\n",
    "            tracking_line_y.append([prev_t[1], offset[1]])\n",
    "            lineplt.set_xdata(tracking_line_x)\n",
    "            lineplt.set_ydata(tracking_line_y)\n",
    "\n",
    "        scatter.set_offsets(np.c_[np.array(offset[0]), np.array(offset[1])])\n",
680
    "\n",
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
    "        x = data.xs(\"x\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        y = data.xs(\"y\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        track.set_offsets(np.c_[np.array(x), np.array(y)])\n",
    "        update_mouse_graph(x, y, plots, edges)\n",
    "\n",
    "        return scatter\n",
    "\n",
    "    animation = FuncAnimation(\n",
    "        fig, func=animation_frame, frames=trajectory, interval=75,\n",
    "    )\n",
    "\n",
    "    ax.set_xlabel(\"{} 1\".format(dim_red))\n",
    "    ax.set_ylabel(\"{} 2\".format(dim_red))\n",
    "\n",
    "    ax2.set_xlabel(\"x\")\n",
    "    ax2.set_xlabel(\"y\")\n",
    "    ax2.set_ylim(-90, 60)\n",
    "    ax2.set_xlim(-60, 60)\n",
    "\n",
    "    plt.tight_layout()\n",
    "\n",
    "    video = animation.to_html5_video()\n",
    "    html = display.HTML(video)\n",
    "    display.display(html)\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. Sample from latent space (to be incorporated into deepof.evaluate)"
   ]
  },
  {
   "cell_type": "code",
717
   "execution_count": null,
lucas_miranda's avatar
lucas_miranda committed
718
   "metadata": {},
719
   "outputs": [],
lucas_miranda's avatar
lucas_miranda committed
720
   "source": [
721
    "# Get prior distribution\n",
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
    "\n",
    "means = prior.components_distribution.mean().numpy()\n",
    "stddevs = prior.components_distribution.stddev().numpy()\n",
    "\n",
    "samples = []\n",
    "for i in range(means.shape[0]):\n",
    "    samples.append(\n",
    "        np.random.normal(means[i, :], stddevs[i, :], size=(500, means.shape[1]))\n",
    "    )\n",
    "samples = np.concatenate(samples)\n",
    "decodings = decoder.predict(samples)\n",
    "\n",
    "umap_reducer = umap.UMAP(n_components=2)\n",
    "pca_reducer = PCA(n_components=2)\n",
    "tsne_reducer = TSNE(n_components=2)\n",
    "lda_reducer = LinearDiscriminantAnalysis(n_components=2)\n",
    "\n",
    "umap_enc = umap_reducer.fit_transform(samples)\n",
    "pca_enc = pca_reducer.fit_transform(samples)\n",
    "tsne_enc = tsne_reducer.fit_transform(samples)\n",
    "lda_enc = lda_reducer.fit_transform(samples, np.repeat(range(means.shape[0]), 500))\n",
    "\n",
    "\n",
    "@interact(dim_red=[\"PCA\", \"LDA\", \"umap\", \"tSNE\"], selected_cluster=(1, k))\n",
    "def sample_from_prior(dim_red, selected_cluster):\n",
    "\n",
    "    if dim_red == \"umap\":\n",
    "        sample_enc = umap_enc\n",
    "    elif dim_red == \"LDA\":\n",
    "        sample_enc = lda_enc\n",
    "    elif dim_red == \"PCA\":\n",
    "        sample_enc = pca_enc\n",
    "    else:\n",
    "        sample_enc = tsne_enc\n",
    "\n",
    "    fig, (ax, ax2) = plt.subplots(\n",
    "        1, 2, figsize=(12, 8), gridspec_kw={\"width_ratios\": [3, 1.5]}\n",
    "    )\n",
    "\n",
    "    hue = np.repeat(range(means.shape[0]), 500)\n",
    "\n",
    "    # Plot the animated embedding trajectory on the left\n",
    "    sns.scatterplot(\n",
    "        x=sample_enc[:, 0],\n",
    "        y=sample_enc[:, 1],\n",
    "        hue=hue,\n",
    "        palette=sns.color_palette(\"husl\", k),\n",
    "        ax=ax,\n",
    "    )\n",
    "\n",
    "    sns.kdeplot(\n",
    "        sample_enc[hue == selected_cluster, 0],\n",
    "        sample_enc[hue == selected_cluster, 1],\n",
    "        color=\"red\",\n",
    "        ax=ax,\n",
    "    )\n",
    "\n",
    "    # Get reconstructions from samples of a given cluster\n",
    "    decs = decodings[hue == selected_cluster][np.random.randint(0, 500, 5)]\n",
    "\n",
    "    # Plot the initial data (before feeding it to the encoder) on the right\n",
    "    edges = deepof.utils.connect_mouse_topview()\n",
    "\n",
    "    for bpart in exclude_bodyparts:\n",
    "        if bpart:\n",
    "            edges.remove_node(bpart)\n",
    "\n",
    "    for limb in [\"Left_fhip\", \"Right_fhip\", \"Left_bhip\", \"Right_bhip\"]:\n",
    "        edges.remove_edge(\"Center\", limb)\n",
    "        if (\"Tail_base\", limb) in list(edges.edges()):\n",
    "            edges.remove_edge(\"Tail_base\", limb)\n",
    "\n",
    "    edges = edges.edges()\n",
    "\n",
    "    inv_coords = coords._scaler.inverse_transform(decs).reshape(\n",
    "        decs.shape[0] * decs.shape[1], decs.shape[2]\n",
    "    )\n",
    "    data = pd.DataFrame(inv_coords, columns=coords[random_exp].columns)\n",
    "\n",
    "    data[\"Center\", \"x\"] = 0\n",
    "    data[\"Center\", \"y\"] = 0\n",
    "\n",
    "    init_x = data.xs(\"x\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "    init_y = data.xs(\"y\", level=1, axis=1, drop_level=False).iloc[0, :]\n",
    "\n",
    "    plots = plot_mouse_graph(init_x, init_y, ax2, edges)\n",
    "    track = ax2.scatter(x=np.array(init_x), y=np.array(init_y), color=\"#006699\",)\n",
    "\n",
    "    # Update data in both plots\n",
    "    def animation_frame(i):\n",
    "        # Update scatter plot\n",
    "\n",
    "        x = data.xs(\"x\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        y = data.xs(\"y\", level=1, axis=1, drop_level=False).iloc[i, :]\n",
    "        track.set_offsets(np.c_[np.array(x), np.array(y)])\n",
    "        update_mouse_graph(x, y, plots, edges)\n",
    "\n",
    "    animation = FuncAnimation(\n",
    "        fig, func=animation_frame, frames=5 * window_size, interval=50,\n",
    "    )\n",
    "\n",
    "    # Plot samples as video on the right\n",
    "\n",
    "    ax.set_xlabel(\"{} 1\".format(dim_red))\n",
    "    ax.set_ylabel(\"{} 2\".format(dim_red))\n",
    "    ax.get_legend().remove()\n",
    "\n",
    "    ax2.set_xlabel(\"x\")\n",
    "    ax2.set_xlabel(\"y\")\n",
    "    ax2.set_ylim(-90, 60)\n",
    "    ax2.set_xlim(-60, 60)\n",
    "\n",
    "    plt.tight_layout()\n",
    "    \n",
    "    video = animation.to_html5_video()\n",
    "    html = display.HTML(video)\n",
    "    display.display(html)\n",
    "    plt.close()"
lucas_miranda's avatar
lucas_miranda committed
840
   ]
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
  }
 ],
 "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.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}