{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Measuremments with Representative" ] }, { "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", "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": 2, "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": 3, "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": 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", " #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", " #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": "code", "execution_count": 6, "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": 7, "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": "markdown", "metadata": {}, "source": [ "## Read Models" ] }, { "cell_type": "code", "execution_count": 9, "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\n", "# Read generated models\n", "viatra_no_con_stats = readStats('../statistics/viatra_nocon_output/', 5000)\n", "viatra_con_stats = readStats('../statistics/viatra_con_output/',5000)\n", "random_stats = readStats('../statistics/random_output/',5000)\n", "con_viatra_stats = readStats('../statistics/controled_viatra/',300)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## calculate distribution distantces" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "viatra_no_con_dic = calDistanceDic(viatra_no_con_stats, human_rep)\n", "viatra_con_dic = calDistanceDic(viatra_con_stats, human_rep)\n", "random_dic = calDistanceDic(random_stats, human_rep)\n", "con_viatra_dic = calDistanceDic(con_viatra_stats, human_rep)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "filenames = reader.readmultiplefiles('../statistics/viatra_nocon_output/trajectories/', 15, 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": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "868a437468d24144926f1390cbf2acb8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/viatra_noc…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 12, "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')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e8b74fe96a45445f8062468ddf2597bf", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/viatra_noc…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 13, "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')\n", "interact(plot_out_na, lines=w)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c6e7e31f454a48169dac12c8aac70eef", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/viatra_noc…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 14, "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')\n", "interact(plot_out_mpc, lines=w)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cebc359548f74cc8b7540ecc3876c9ee", "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": 15, "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')\n", "interact(plot_out_degree, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "682beae42eef4676b11b6fe23127a44e", "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": 16, "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')\n", "interact(plot_na, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6893b8c6e03441f89fc35bf784992ae9", "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": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_mpc(lines):\n", " plot(viatra_con_dic, lines, 0, lambda a: a.mpc_distance, colors, 'MPC')\n", "interact(plot_mpc, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ff0e1991c69a4d77a40f57225f90295a", "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": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(random_dic, lines, 0, lambda a: a.out_d_distance, colors, 'out degree')\n", "interact(plot_out_degree, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "838570f20bed4d8d9c618305984d19ef", "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": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(random_dic, lines, 0, lambda a: a.na_distance, colors, 'out degree')\n", "interact(plot_out_degree, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f4825f6257a74bce9dd22aac8a98effa", "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": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_out_degree(lines):\n", " plot(random_dic, lines, 0, lambda a: a.mpc_distance, colors, 'out degree')\n", "interact(plot_out_degree, lines=[[]])" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "con_viatra_stats = readStats('../statistics/controled_viatra/',5000)\n", "con_viatra_dic = calDistanceDic(con_viatra_stats, human_rep)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trajectories for controlled viatra solver" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "filenames = reader.readmultiplefiles('../statistics/controled_viatra/trajectories/', 25, 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", ")" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4b60ae3859e343299badf29272f67d21", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/controled_…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 57, "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')\n", "interact(plot_out_degree, lines=w)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8e7965d793a146d4bbc268554262eb58", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/controled_…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 58, "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')\n", "interact(plot_na, lines=w)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "55a1209d0b924a39b4729228e81ee3ab", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(SelectMultiple(description='Trajectory:', index=(0,), options={'../statistics/controled_…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def plot_mpc(lines):\n", " plot(con_viatra_dic, lines, 0, lambda a: a.mpc_distance, colors, 'mpc')\n", "interact(plot_mpc, lines=w)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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 }