{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Measuremments with Representative" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os, sys\n", "lib_path = os.path.abspath(os.path.join('..', '..', 'utils'))\n", "sys.path.append(lib_path)\n", "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", "import numpy as np\n", "import constants\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Record the distances of different metrics using a representative" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class GraphDistanceWithRep:\n", " #init with a graph stat and a collection of graph stats\n", " def __init__(self, graphStat, rep):\n", " self.graph = graphStat\n", " self.rep = rep\n", " self.out_d_distance, _ = stats.ks_2samp(graphStat.out_d, rep.out_d)\n", " self.na_distance,_ = stats.ks_2samp(graphStat.na, rep.na)\n", " self.mpc_distance,_ = stats.ks_2samp(graphStat.mpc, rep.mpc)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Methods\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Find the median ks distance of the same number of nodes" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def find_median(x, metric_distances):\n", " distance_dic = {}\n", " for index, num_of_nodes in enumerate(x):\n", " if num_of_nodes[0] not in distance_dic:\n", " distance_dic[num_of_nodes[0]] = []\n", " distance_dic[num_of_nodes[0]].append(metric_distances[index])\n", " median_x = []\n", " y = []\n", " for num_of_nodes, distances in distance_dic.items():\n", " median_x.append(num_of_nodes)\n", " y.append(np.median(distances))\n", " order = np.argsort(median_x)\n", " median_x = np.array(median_x)[order]\n", " median_y = np.array(y)[order]\n", " return median_x, median_y\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Plot Diagram" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# metric_selector: GraphDistance -> float\n", "def plot(infos, lines, id, metric_selector,colors, title, foldername):\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", " #plot ks distance median\n", " median_x, median_y = find_median(x, metric_distances)\n", " plt.plot(median_x, median_y, color='black',marker='o')\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", " mkdir_p(foldername)\n", " plt.savefig(fname = foldername+title+'.jpg', dpi=150)\n", " #graph.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Retrieve information from a list " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def retrive_info_from_list(selector, distances):\n", " return list(map(selector, distances))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def readStats(path, numModels):\n", " names = reader.readmultiplefiles(path, numModels, False)\n", " stats = []\n", " for name in names:\n", " stats.append(GraphStat(name))\n", " return stats" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def calDistanceDic(stats, rep):\n", " dic = {}\n", " for info in stats:\n", " info = GraphDistanceWithRep(info, rep)\n", " dic[info.graph.id] = info\n", " return dic" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def createRandomColors(size):\n", " #generate random color for each line\n", " colors = []\n", "\n", " for i in range(0, size):\n", " color = \"#%06x\" % random.randint(0, 0xFFFFFF)\n", " colors.append(color)\n", " return colors" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def createSelectionWidge(options):\n", " w = widgets.SelectMultiple(\n", " options = options,\n", " value = [],\n", " description='Trajectory:',\n", " disabled=False,\n", " )\n", " return w" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "def mkdir_p(mypath):\n", " '''Creates a directory. equivalent to using mkdir -p on the command line'''\n", "\n", " from errno import EEXIST\n", " from os import makedirs,path\n", "\n", " try:\n", " makedirs(mypath)\n", " except OSError as exc: # Python >2.5\n", " if exc.errno == EEXIST and path.isdir(mypath):\n", " pass\n", " else: raise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Metrics During GenerationPlots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read Human Representatives" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "### Read Models\n", "#read representative\n", "human_rep = GraphStat(constants.HUMAN_OUT_D_REP)\n", "human_na = GraphStat(constants.HUMAN_NA_REP)\n", "human_mpc = GraphStat(constants.HUMAN_MPC_REP)\n", "\n", "# assign rep distributions to human_rep\n", "human_rep.na = human_na.na\n", "human_rep.mpc = human_mpc.mpc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Viatra No Constraint" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# Read generated models\n", "viatra_no_con_stats = readStats('../input/viatra_nocon_output/', 5000)\n", "viatra_no_con_dic = calDistanceDic(viatra_no_con_stats, human_rep)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "filenames = reader.readmultiplefiles('../input/viatra_nocon_output/trajectories/', 15, False)\n", "trajectories = {}\n", "for name in filenames:\n", " trajectories[name] = reader.readTrajectory(name)\n", "w = createSelectionWidge(trajectories)\n", "colors = createRandomColors(len(trajectories))" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9519be563fbc41c28921c77ef6481b17", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(viatra_no_con_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out degree', '../output/viatra_no_constraints/')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c896725e542c4bf8a1bc76ba66819b20", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_na(lines):\n", " plot(viatra_no_con_dic, lines, 0, lambda a: a.na_distance, colors, 'node activity', '../output/viatra_no_constraints/')\n", "interact(plot_out_na, lines=w)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "880410d675624545ab73977a463bb5c9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_mpc(lines):\n", " plot(viatra_no_con_dic, lines, 0, lambda a: a.mpc_distance, colors, 'MPC', '../output/viatra_no_constraints/')\n", "interact(plot_out_mpc, lines=w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Viatra with constraints" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "viatra_con_stats = readStats('../input/viatra_con_output/',5000)\n", "viatra_con_dic = calDistanceDic(viatra_con_stats, human_rep)\n", "\n", "# trajectories and colors\n", "trajectories = {}\n", "w = createSelectionWidge(trajectories)\n", "colors = createRandomColors(len(trajectories))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0d04d6db770a49f4a160ff55cc7131f6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='lines', options=([],), value=[]), Output()), _dom_classes=('widget…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(viatra_con_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out degree', '../output/viatra_constraints/')\n", "interact(plot_out_degree, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "96eebad1f6274d79ad377c8c54b44615", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='lines', options=([],), value=[]), Output()), _dom_classes=('widget…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_na(lines):\n", " plot(viatra_con_dic, lines, 0, lambda a: a.na_distance, colors, 'node activity', '../output/viatra_constraints/')\n", "interact(plot_na, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4fc2714a3cd3440daf5014bb4b942b9a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='lines', options=([],), value=[]), Output()), _dom_classes=('widget…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_mpc(lines):\n", " plot(viatra_con_dic, lines, 0, lambda a: a.mpc_distance, colors, 'MPC', '../output/viatra_constraints/')\n", "interact(plot_mpc, lines=[[]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Controlled RandomEMF" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "random_emf_stats = readStats('../input/random_emf_output/',5000)\n", "random_emf_dic = calDistanceDic(random_emf_stats, human_rep)\n", "\n", "# trajectories and colors\n", "trajectories = {}\n", "w = createSelectionWidge(trajectories)\n", "colors = createRandomColors(len(trajectories))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4401931533b5497f864f146d7b4dcd3c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='lines', options=([],), value=[]), Output()), _dom_classes=('widget…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(random_emf_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out degree', '../output/random_emf/')\n", "interact(plot_out_degree, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fb7bdedff841420bb8f817013f565020", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='lines', options=([],), value=[]), Output()), _dom_classes=('widget…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_node_activity(lines):\n", " plot(random_emf_dic, lines, 0, lambda a: a.na_distance, colors, 'node activity', '../output/random_emf/')\n", "interact(plot_node_activity, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6b0c349c4a3b4813825513f739ea30da", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='lines', options=([],), value=[]), Output()), _dom_classes=('widget…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_mpc(lines):\n", " plot(random_emf_dic, lines, 0, lambda a: a.mpc_distance, colors, 'mpc', '../output/random_emf/')\n", "interact(plot_mpc, lines=[[]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Controlled Viatra with MPC" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "con_viatra_stats = readStats('../input/controled_viatra_mpc/',5000)\n", "con_viatra_dic = calDistanceDic(con_viatra_stats, human_rep)\n", "\n", "# trajectories and colors\n", "trajectories = {}\n", "w = createSelectionWidge(trajectories)\n", "colors = createRandomColors(len(trajectories))" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b76901ba9d44433984032e0dc5679fa9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(con_viatra_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out_degree', '../output/controled_viatra_with_mpc/')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9e0d61e29b02467cb52618860a1bde7f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_na(lines):\n", " plot(con_viatra_dic, lines, 0, lambda a: a.na_distance, colors, 'Node Activity', '../output/controled_viatra_with_mpc/')\n", "interact(plot_na, lines=w)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "70074805fee44a1aa5b9ccb3770b5c0c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_mpc(lines):\n", " plot(con_viatra_dic, lines, 0, lambda a: a.mpc_distance, colors, 'mpc', '../output/controled_viatra_with_mpc/')\n", "interact(plot_mpc, lines=w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (Pseudo) Random EMF instantiator" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "random_emf_stats = readStats('../input/real_random_output/',5000)\n", "random_emf_dic = calDistanceDic(random_emf_stats, human_rep)\n", "\n", "# trajectories and colors\n", "trajectories = {}\n", "w = createSelectionWidge(trajectories)\n", "colors = createRandomColors(len(trajectories))" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "912ba2fdfd7c46848065f174aa6177e0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(random_emf_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out_degree', '../output/random_emf_instantiator/')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0ba621dd0e7d4957aaff2cf209bba165", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_na(lines):\n", " plot(random_emf_dic, lines, 0, lambda a: a.na_distance, colors, 'Node Activity', '../output/random_emf_instantiator/')\n", "interact(plot_na, lines=w)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d432bbae1c6f48c3acd1767f2e2b13c7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', options={}, value=()), Output()), _dom_classes…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_mpc(lines):\n", " plot(random_emf_dic, lines, 0, lambda a: a.mpc_distance, colors, 'mpc', '../output/random_emf_instantiator/')\n", "interact(plot_mpc, lines=w)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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 }