{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Compare Metrics Distances to The Human Models" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from GraphType import GraphStat\n", "from GraphType import GraphCollection\n", "from scipy import stats\n", "from ipywidgets import interact, fixed, interactive\n", "import readCSV as reader\n", "import ipywidgets as widgets\n", "import matplotlib.pyplot as plt\n", "import random\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Record the average distances of different metrics for a model to the human models " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class GraphDistance:\n", " #init with a graph stat and a collection of graph stats\n", " def __init__(self, graphStat, collection):\n", " self.graph = graphStat\n", " self.collection = collection\n", " self.out_d_distance = average_ks_distance(collection.out_ds, graphStat.out_d)\n", " self.na_distance = average_ks_distance(collection.nas, graphStat.na)\n", " self.mpc_distance = average_ks_distance(collection.mpcs, graphStat.mpc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Calculate the average ks distance" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def average_ks_distance(targets, sample):\n", " distance = 0.0\n", " for target in targets:\n", " value, p = stats.ks_2samp(target, sample)\n", " distance += value\n", " \n", " distance = distance / len(targets)\n", " return distance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Plot Diagram" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# metric_selector: GraphDistance -> float\n", "def plot(infos, lines, id, metric_selector,colors, title):\n", " metric_distances = retrive_info_from_list(metric_selector, list(infos.values()))\n", " x = retrive_info_from_list(lambda a : a.graph.num_nodes, list(infos.values()))\n", " graph = plt.figure(id,figsize=(18, 10))\n", " plt.title(title)\n", " plt.plot(x, metric_distances, color='red', linestyle='', marker='o',alpha=0.7)\n", " for i in range(0, len(lines)):\n", " line_infos = retrive_info_from_list(lambda a: infos[a], lines[i])\n", " line_y = retrive_info_from_list(metric_selector, line_infos)\n", " line_x = retrive_info_from_list(lambda a : a.graph.num_nodes, line_infos)\n", " plt.plot(line_x, line_y, marker='o', color=colors[i])\n", " #graph.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Retrieve information from a list " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def retrive_info_from_list(selector, distances):\n", " return list(map(selector, distances))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read Models" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "human = GraphCollection('../statistics/humanOutput/', 300, 'Human')\n", "file_names = reader.readmultiplefiles('../statistics/viatraEvolve/', 1000, False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate Distances" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Progress Widge\n", "w2 = widgets.FloatProgress(\n", " value=0,\n", " min=0,\n", " max=1.0,\n", " step=0.1,\n", " description='Loading Files...:',\n", " bar_style='info',\n", " orientation='horizontal'\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "60e3295a164f48428bc882868e68f26f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FloatProgress(value=0.0, bar_style='info', description='Loading Files...:', max=1.0)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "infos = []\n", "# read all files\n", "counter = 0.0\n", "size = len(file_names)\n", "#display progress bar\n", "display(w2)\n", "for name in file_names:\n", " infos.append(GraphStat(name))\n", "\n", "info_dic = {}\n", "for info in infos:\n", " w2.value = (counter/size)\n", " counter+=1\n", " info = GraphDistance(info, human)\n", " info_dic[info.graph.id] = info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot Graphs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* widget for select trajectory" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "filenames = reader.readmultiplefiles('../statistics/trajectories/', 10, False)\n", "trajectories = {}\n", "for name in filenames:\n", " trajectories[name] = reader.readTrajectory(name)\n", "\n", "w = widgets.SelectMultiple(\n", " options = trajectories,\n", " value = [trajectories[filenames[0]]],\n", " description='Trajectory:',\n", " disabled=False,\n", ")\n", "\n", "#generate random color for each line\n", "colors = []\n", "\n", "for i in range(0, len(trajectories)):\n", " color = \"#%06x\" % random.randint(0, 0xFFFFFF)\n", " colors.append(color)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Out Degree" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f69502eba4af40f19acc09f36681387a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/trajectori…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(info_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out degree')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Node Activity" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0df16294cd86434b8f144ff08702d44a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/trajectori…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(info_dic, lines, 0, lambda a: a.na_distance, colors, 'node activity')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### MPC" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b4e76d41b3d644808e47e3d1d7aaf1a7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/trajectori…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(info_dic, lines, 0, lambda a: a.mpc_distance, colors, 'MPC')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "../statistics/viatraEvolve\\state_735.csv\n" ] } ], "source": [ "for name in file_names:\n", " contents = reader.readcsvfile(name)\n", " if(contents['State Id'][0] == 1032396643):\n", " print(name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }