diff options
Diffstat (limited to 'Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb')
-rw-r--r-- | Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb | 481 |
1 files changed, 481 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb b/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb new file mode 100644 index 00000000..550e3978 --- /dev/null +++ b/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb | |||
@@ -0,0 +1,481 @@ | |||
1 | { | ||
2 | "cells": [ | ||
3 | { | ||
4 | "cell_type": "markdown", | ||
5 | "metadata": {}, | ||
6 | "source": [ | ||
7 | "# Compare Metrics Distances to The Human Models" | ||
8 | ] | ||
9 | }, | ||
10 | { | ||
11 | "cell_type": "markdown", | ||
12 | "metadata": {}, | ||
13 | "source": [ | ||
14 | "### Imports" | ||
15 | ] | ||
16 | }, | ||
17 | { | ||
18 | "cell_type": "code", | ||
19 | "execution_count": 48, | ||
20 | "metadata": {}, | ||
21 | "outputs": [], | ||
22 | "source": [ | ||
23 | "from GraphType import GraphStat\n", | ||
24 | "from GraphType import GraphCollection\n", | ||
25 | "from scipy import stats\n", | ||
26 | "from ipywidgets import interact, fixed, interactive\n", | ||
27 | "import readCSV as reader\n", | ||
28 | "import ipywidgets as widgets\n", | ||
29 | "import matplotlib.pyplot as plt\n", | ||
30 | "import random\n", | ||
31 | "import numpy as np\n", | ||
32 | "import constants\n" | ||
33 | ] | ||
34 | }, | ||
35 | { | ||
36 | "cell_type": "markdown", | ||
37 | "metadata": {}, | ||
38 | "source": [ | ||
39 | "### Classes" | ||
40 | ] | ||
41 | }, | ||
42 | { | ||
43 | "cell_type": "markdown", | ||
44 | "metadata": {}, | ||
45 | "source": [ | ||
46 | "* Record the average distances of different metrics for a model to the human models " | ||
47 | ] | ||
48 | }, | ||
49 | { | ||
50 | "cell_type": "code", | ||
51 | "execution_count": 49, | ||
52 | "metadata": {}, | ||
53 | "outputs": [], | ||
54 | "source": [ | ||
55 | "class GraphDistance:\n", | ||
56 | " #init with a graph stat and a collection of graph stats\n", | ||
57 | " def __init__(self, graphStat, collection):\n", | ||
58 | " self.graph = graphStat\n", | ||
59 | " self.collection = collection\n", | ||
60 | " self.out_d_distance = average_ks_distance(collection.out_ds, graphStat.out_d)\n", | ||
61 | " self.na_distance = average_ks_distance(collection.nas, graphStat.na)\n", | ||
62 | " self.mpc_distance = average_ks_distance(collection.mpcs, graphStat.mpc)" | ||
63 | ] | ||
64 | }, | ||
65 | { | ||
66 | "cell_type": "markdown", | ||
67 | "metadata": {}, | ||
68 | "source": [ | ||
69 | "### Methods" | ||
70 | ] | ||
71 | }, | ||
72 | { | ||
73 | "cell_type": "markdown", | ||
74 | "metadata": {}, | ||
75 | "source": [ | ||
76 | "* Calculate the average ks distance" | ||
77 | ] | ||
78 | }, | ||
79 | { | ||
80 | "cell_type": "code", | ||
81 | "execution_count": 50, | ||
82 | "metadata": {}, | ||
83 | "outputs": [], | ||
84 | "source": [ | ||
85 | "def average_ks_distance(targets, sample):\n", | ||
86 | " distance = 0.0\n", | ||
87 | " for target in targets:\n", | ||
88 | " value, p = stats.ks_2samp(target, sample)\n", | ||
89 | " distance += value\n", | ||
90 | " distance = distance / len(targets)\n", | ||
91 | " return distance\n" | ||
92 | ] | ||
93 | }, | ||
94 | { | ||
95 | "cell_type": "markdown", | ||
96 | "metadata": { | ||
97 | "pycharm": { | ||
98 | "name": "#%% md\n" | ||
99 | } | ||
100 | }, | ||
101 | "source": [ | ||
102 | "* Find the median ks distance of the same number of nodes" | ||
103 | ] | ||
104 | }, | ||
105 | { | ||
106 | "cell_type": "code", | ||
107 | "execution_count": 51, | ||
108 | "metadata": { | ||
109 | "pycharm": { | ||
110 | "name": "#%%\n" | ||
111 | } | ||
112 | }, | ||
113 | "outputs": [], | ||
114 | "source": [ | ||
115 | "def find_median(x, metric_distances):\n", | ||
116 | " distance_dic = {}\n", | ||
117 | " for index, num_of_nodes in enumerate(x):\n", | ||
118 | " if num_of_nodes[0] not in distance_dic:\n", | ||
119 | " distance_dic[num_of_nodes[0]] = []\n", | ||
120 | " distance_dic[num_of_nodes[0]].append(metric_distances[index])\n", | ||
121 | " median_x = []\n", | ||
122 | " y = []\n", | ||
123 | " for num_of_nodes, distances in distance_dic.items():\n", | ||
124 | " median_x.append(num_of_nodes)\n", | ||
125 | " y.append(np.median(distances))\n", | ||
126 | " order = np.argsort(median_x)\n", | ||
127 | " median_x = np.array(median_x)[order]\n", | ||
128 | " median_y = np.array(y)[order]\n", | ||
129 | " return median_x, median_y\n" | ||
130 | ] | ||
131 | }, | ||
132 | { | ||
133 | "cell_type": "markdown", | ||
134 | "metadata": {}, | ||
135 | "source": [ | ||
136 | "* Plot Diagram" | ||
137 | ] | ||
138 | }, | ||
139 | { | ||
140 | "cell_type": "code", | ||
141 | "execution_count": 52, | ||
142 | "metadata": {}, | ||
143 | "outputs": [], | ||
144 | "source": [ | ||
145 | "# metric_selector: GraphDistance -> float\n", | ||
146 | "def plot(infos, lines, id, metric_selector,colors, title):\n", | ||
147 | " metric_distances = retrive_info_from_list(metric_selector, list(infos.values()))\n", | ||
148 | " x = retrive_info_from_list(lambda a : a.graph.num_nodes, list(infos.values()))\n", | ||
149 | " graph = plt.figure(id,figsize=(18, 10))\n", | ||
150 | " plt.title(title)\n", | ||
151 | " plt.plot(x, metric_distances, color='red', linestyle='', marker='o',alpha=0.7)\n", | ||
152 | " #plot ks distance median\n", | ||
153 | " median_x, median_y = find_median(x, metric_distances)\n", | ||
154 | " plt.plot(median_x, median_y, color='black',marker='o')\n", | ||
155 | " for i in range(0, len(lines)):\n", | ||
156 | " line_infos = retrive_info_from_list(lambda a: infos[a], lines[i])\n", | ||
157 | " line_y = retrive_info_from_list(metric_selector, line_infos)\n", | ||
158 | " line_x = retrive_info_from_list(lambda a : a.graph.num_nodes, line_infos)\n", | ||
159 | " plt.plot(line_x, line_y, marker='o', color=colors[i])\n", | ||
160 | " #graph.show()" | ||
161 | ] | ||
162 | }, | ||
163 | { | ||
164 | "cell_type": "markdown", | ||
165 | "metadata": {}, | ||
166 | "source": [ | ||
167 | "* Retrieve information from a list " | ||
168 | ] | ||
169 | }, | ||
170 | { | ||
171 | "cell_type": "code", | ||
172 | "execution_count": 53, | ||
173 | "metadata": {}, | ||
174 | "outputs": [], | ||
175 | "source": [ | ||
176 | "def retrive_info_from_list(selector, distances):\n", | ||
177 | " return list(map(selector, distances))" | ||
178 | ] | ||
179 | }, | ||
180 | { | ||
181 | "cell_type": "markdown", | ||
182 | "metadata": {}, | ||
183 | "source": [ | ||
184 | "### Read Models" | ||
185 | ] | ||
186 | }, | ||
187 | { | ||
188 | "cell_type": "code", | ||
189 | "execution_count": 54, | ||
190 | "metadata": {}, | ||
191 | "outputs": [], | ||
192 | "source": [ | ||
193 | "human = GraphCollection('../statistics/humanOutput/', 300, 'Human', True)\n", | ||
194 | "file_names = reader.readmultiplefiles('../statistics/viatraEvolve/', 1000, False)" | ||
195 | ] | ||
196 | }, | ||
197 | { | ||
198 | "cell_type": "markdown", | ||
199 | "metadata": {}, | ||
200 | "source": [ | ||
201 | "### Calculate Distances" | ||
202 | ] | ||
203 | }, | ||
204 | { | ||
205 | "cell_type": "code", | ||
206 | "execution_count": 55, | ||
207 | "metadata": {}, | ||
208 | "outputs": [], | ||
209 | "source": [ | ||
210 | "# Progress Widge\n", | ||
211 | "w2 = widgets.FloatProgress(\n", | ||
212 | " value=0,\n", | ||
213 | " min=0,\n", | ||
214 | " max=1.0,\n", | ||
215 | " step=0.1,\n", | ||
216 | " description='Loading Files...:',\n", | ||
217 | " bar_style='info',\n", | ||
218 | " orientation='horizontal'\n", | ||
219 | ")" | ||
220 | ] | ||
221 | }, | ||
222 | { | ||
223 | "cell_type": "code", | ||
224 | "execution_count": 56, | ||
225 | "metadata": {}, | ||
226 | "outputs": [ | ||
227 | { | ||
228 | "data": { | ||
229 | "application/vnd.jupyter.widget-view+json": { | ||
230 | "model_id": "ca7932bce2a741afaff6b919042c42b0", | ||
231 | "version_major": 2, | ||
232 | "version_minor": 0 | ||
233 | }, | ||
234 | "text/plain": [ | ||
235 | "FloatProgress(value=0.0, bar_style='info', description='Loading Files...:', max=1.0)" | ||
236 | ] | ||
237 | }, | ||
238 | "metadata": {}, | ||
239 | "output_type": "display_data" | ||
240 | } | ||
241 | ], | ||
242 | "source": [ | ||
243 | "infos = []\n", | ||
244 | "# read all files\n", | ||
245 | "counter = 0.0\n", | ||
246 | "size = len(file_names)\n", | ||
247 | "#display progress bar\n", | ||
248 | "display(w2)\n", | ||
249 | "for name in file_names:\n", | ||
250 | " infos.append(GraphStat(name))\n", | ||
251 | "\n", | ||
252 | "info_dic = {}\n", | ||
253 | "for info in infos:\n", | ||
254 | " w2.value = (counter/size)\n", | ||
255 | " counter+=1\n", | ||
256 | " info = GraphDistance(info, human)\n", | ||
257 | " info_dic[info.graph.id] = info" | ||
258 | ] | ||
259 | }, | ||
260 | { | ||
261 | "cell_type": "markdown", | ||
262 | "metadata": {}, | ||
263 | "source": [ | ||
264 | "#### Plot Graphs" | ||
265 | ] | ||
266 | }, | ||
267 | { | ||
268 | "cell_type": "markdown", | ||
269 | "metadata": {}, | ||
270 | "source": [ | ||
271 | "* widget for select trajectory" | ||
272 | ] | ||
273 | }, | ||
274 | { | ||
275 | "cell_type": "code", | ||
276 | "execution_count": 57, | ||
277 | "metadata": {}, | ||
278 | "outputs": [], | ||
279 | "source": [ | ||
280 | "filenames = reader.readmultiplefiles('../statistics/trajectories/', 10, False)\n", | ||
281 | "trajectories = {}\n", | ||
282 | "for name in filenames:\n", | ||
283 | " trajectories[name] = reader.readTrajectory(name)\n", | ||
284 | "\n", | ||
285 | "w = widgets.SelectMultiple(\n", | ||
286 | " options = trajectories,\n", | ||
287 | " value = [trajectories[filenames[0]]],\n", | ||
288 | " description='Trajectory:',\n", | ||
289 | " disabled=False,\n", | ||
290 | ")\n", | ||
291 | "\n", | ||
292 | "#generate random color for each line\n", | ||
293 | "colors = []\n", | ||
294 | "\n", | ||
295 | "for i in range(0, len(trajectories)):\n", | ||
296 | " color = \"#%06x\" % random.randint(0, 0xFFFFFF)\n", | ||
297 | " colors.append(color)" | ||
298 | ] | ||
299 | }, | ||
300 | { | ||
301 | "cell_type": "markdown", | ||
302 | "metadata": {}, | ||
303 | "source": [ | ||
304 | "#### Out Degree" | ||
305 | ] | ||
306 | }, | ||
307 | { | ||
308 | "cell_type": "code", | ||
309 | "execution_count": 63, | ||
310 | "metadata": {}, | ||
311 | "outputs": [ | ||
312 | { | ||
313 | "data": { | ||
314 | "application/vnd.jupyter.widget-view+json": { | ||
315 | "model_id": "08da62cb0c3f4e6e9591c7dc811d27cc", | ||
316 | "version_major": 2, | ||
317 | "version_minor": 0 | ||
318 | }, | ||
319 | "text/plain": [ | ||
320 | "interactive(children=(SelectMultiple(description='Trajectory:', index=(1,), options={'../statistics/trajectori…" | ||
321 | ] | ||
322 | }, | ||
323 | "metadata": {}, | ||
324 | "output_type": "display_data" | ||
325 | }, | ||
326 | { | ||
327 | "data": { | ||
328 | "text/plain": [ | ||
329 | "<function __main__.plot_out_degree(lines)>" | ||
330 | ] | ||
331 | }, | ||
332 | "execution_count": 63, | ||
333 | "metadata": {}, | ||
334 | "output_type": "execute_result" | ||
335 | } | ||
336 | ], | ||
337 | "source": [ | ||
338 | "def plot_out_degree(lines):\n", | ||
339 | " plot(info_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out degree')\n", | ||
340 | "interact(plot_out_degree, lines=w)" | ||
341 | ] | ||
342 | }, | ||
343 | { | ||
344 | "cell_type": "markdown", | ||
345 | "metadata": {}, | ||
346 | "source": [ | ||
347 | "#### Node Activity" | ||
348 | ] | ||
349 | }, | ||
350 | { | ||
351 | "cell_type": "code", | ||
352 | "execution_count": 64, | ||
353 | "metadata": { | ||
354 | "scrolled": true | ||
355 | }, | ||
356 | "outputs": [ | ||
357 | { | ||
358 | "data": { | ||
359 | "application/vnd.jupyter.widget-view+json": { | ||
360 | "model_id": "a708f43645a24bd2b15b53ea12c7d88f", | ||
361 | "version_major": 2, | ||
362 | "version_minor": 0 | ||
363 | }, | ||
364 | "text/plain": [ | ||
365 | "interactive(children=(SelectMultiple(description='Trajectory:', index=(1,), options={'../statistics/trajectori…" | ||
366 | ] | ||
367 | }, | ||
368 | "metadata": {}, | ||
369 | "output_type": "display_data" | ||
370 | }, | ||
371 | { | ||
372 | "data": { | ||
373 | "text/plain": [ | ||
374 | "<function __main__.plot_na(lines)>" | ||
375 | ] | ||
376 | }, | ||
377 | "execution_count": 64, | ||
378 | "metadata": {}, | ||
379 | "output_type": "execute_result" | ||
380 | } | ||
381 | ], | ||
382 | "source": [ | ||
383 | "def plot_na(lines):\n", | ||
384 | " plot(info_dic, lines, 0, lambda a: a.na_distance, colors, 'node activity')\n", | ||
385 | "interact(plot_na, lines=w)" | ||
386 | ] | ||
387 | }, | ||
388 | { | ||
389 | "cell_type": "markdown", | ||
390 | "metadata": {}, | ||
391 | "source": [ | ||
392 | "#### MPC" | ||
393 | ] | ||
394 | }, | ||
395 | { | ||
396 | "cell_type": "code", | ||
397 | "execution_count": null, | ||
398 | "metadata": {}, | ||
399 | "outputs": [], | ||
400 | "source": [] | ||
401 | }, | ||
402 | { | ||
403 | "cell_type": "code", | ||
404 | "execution_count": 65, | ||
405 | "metadata": {}, | ||
406 | "outputs": [ | ||
407 | { | ||
408 | "data": { | ||
409 | "application/vnd.jupyter.widget-view+json": { | ||
410 | "model_id": "124a0cb0ebfb4225bf4ced24c09032f7", | ||
411 | "version_major": 2, | ||
412 | "version_minor": 0 | ||
413 | }, | ||
414 | "text/plain": [ | ||
415 | "interactive(children=(SelectMultiple(description='Trajectory:', index=(1,), options={'../statistics/trajectori…" | ||
416 | ] | ||
417 | }, | ||
418 | "metadata": {}, | ||
419 | "output_type": "display_data" | ||
420 | }, | ||
421 | { | ||
422 | "data": { | ||
423 | "text/plain": [ | ||
424 | "<function __main__.plot_out_degree(lines)>" | ||
425 | ] | ||
426 | }, | ||
427 | "execution_count": 65, | ||
428 | "metadata": {}, | ||
429 | "output_type": "execute_result" | ||
430 | } | ||
431 | ], | ||
432 | "source": [ | ||
433 | "def plot_out_degree(lines):\n", | ||
434 | " plot(info_dic, lines, 0, lambda a: a.mpc_distance, colors, 'MPC')\n", | ||
435 | "interact(plot_out_degree, lines=w)" | ||
436 | ] | ||
437 | }, | ||
438 | { | ||
439 | "cell_type": "code", | ||
440 | "execution_count": 19, | ||
441 | "metadata": {}, | ||
442 | "outputs": [], | ||
443 | "source": [ | ||
444 | "for name in file_names:\n", | ||
445 | " contents = reader.readcsvfile(name)\n", | ||
446 | " if(contents['State Id'][0] == 1032396643):\n", | ||
447 | " print(name)" | ||
448 | ] | ||
449 | } | ||
450 | ], | ||
451 | "metadata": { | ||
452 | "kernelspec": { | ||
453 | "display_name": "Python 3", | ||
454 | "language": "python", | ||
455 | "name": "python3" | ||
456 | }, | ||
457 | "language_info": { | ||
458 | "codemirror_mode": { | ||
459 | "name": "ipython", | ||
460 | "version": 3 | ||
461 | }, | ||
462 | "file_extension": ".py", | ||
463 | "mimetype": "text/x-python", | ||
464 | "name": "python", | ||
465 | "nbconvert_exporter": "python", | ||
466 | "pygments_lexer": "ipython3", | ||
467 | "version": "3.7.3" | ||
468 | }, | ||
469 | "pycharm": { | ||
470 | "stem_cell": { | ||
471 | "cell_type": "raw", | ||
472 | "metadata": { | ||
473 | "collapsed": false | ||
474 | }, | ||
475 | "source": [] | ||
476 | } | ||
477 | } | ||
478 | }, | ||
479 | "nbformat": 4, | ||
480 | "nbformat_minor": 2 | ||
481 | } | ||