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