From bb6bb8c279e0db324efa11b28eb5d77d175aa8a9 Mon Sep 17 00:00:00 2001 From: 20001LastOrder Date: Wed, 5 Jun 2019 14:26:39 -0400 Subject: clean up metric directory --- .../src/metrics_distance.ipynb | 481 +++++++++++ .../src/metrics_distance_with_selector.ipynb | 955 +++++++++++++++++++++ .../src/representative_selector .ipynb | 336 ++++++++ 3 files changed, 1772 insertions(+) create mode 100644 Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb create mode 100644 Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance_with_selector.ipynb create mode 100644 Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/representative_selector .ipynb (limited to 'Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src') 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 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Compare Metrics Distances to The Human Models" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "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 average distances of different metrics for a model to the human models " + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "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": 50, + "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", + " distance = distance / len(targets)\n", + " return distance\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "* Find the median ks distance of the same number of nodes" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "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": 52, + "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": 53, + "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": 54, + "metadata": {}, + "outputs": [], + "source": [ + "human = GraphCollection('../statistics/humanOutput/', 300, 'Human', True)\n", + "file_names = reader.readmultiplefiles('../statistics/viatraEvolve/', 1000, False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Calculate Distances" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "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": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ca7932bce2a741afaff6b919042c42b0", + "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": 57, + "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": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "08da62cb0c3f4e6e9591c7dc811d27cc", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(SelectMultiple(description='Trajectory:', index=(1,), options={'../statistics/trajectori…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 63, + "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": 64, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "a708f43645a24bd2b15b53ea12c7d88f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(SelectMultiple(description='Trajectory:', index=(1,), options={'../statistics/trajectori…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def plot_na(lines):\n", + " plot(info_dic, lines, 0, lambda a: a.na_distance, colors, 'node activity')\n", + "interact(plot_na, lines=w)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### MPC" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "124a0cb0ebfb4225bf4ced24c09032f7", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(SelectMultiple(description='Trajectory:', index=(1,), options={'../statistics/trajectori…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 65, + "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": 19, + "metadata": {}, + "outputs": [], + "source": [ + "for name in file_names:\n", + " contents = reader.readcsvfile(name)\n", + " if(contents['State Id'][0] == 1032396643):\n", + " print(name)" + ] + } + ], + "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" + }, + "pycharm": { + "stem_cell": { + "cell_type": "raw", + "metadata": { + "collapsed": false + }, + "source": [] + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance_with_selector.ipynb b/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance_with_selector.ipynb new file mode 100644 index 00000000..4c7fecb3 --- /dev/null +++ b/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance_with_selector.ipynb @@ -0,0 +1,955 @@ +{ + "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 +} diff --git a/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/representative_selector .ipynb b/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/representative_selector .ipynb new file mode 100644 index 00000000..9653b2a0 --- /dev/null +++ b/Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/representative_selector .ipynb @@ -0,0 +1,336 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Use K-medoid algorithm to find the suitable human model representitives" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "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", + "import readCSV as reader\n", + "from scipy import stats\n", + "from ipywidgets import interact, fixed, interactive\n", + "import ipywidgets as widgets\n", + "from pyclustering.cluster.kmedoids import kmedoids\n", + "from pyclustering.utils.metric import distance_metric, type_metric\n", + "import random" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Define a new distance metric" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def ks_value(dest1, dest2):\n", + " value, p = stats.ks_2samp(dest1, dest2)\n", + " return value\n", + "\n", + "\n", + "ks_metric = distance_metric(type_metric.USER_DEFINED, func=ks_value)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Read Human Models" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1253" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Progress Widge\n", + "w = 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", + ")\n", + "\n", + "\n", + "humanFiles = reader.readmultiplefiles('../input/humanOutput/', 1300, False)\n", + "modelToFileName = {}\n", + "for name in humanFiles:\n", + " modelToFileName[GraphStat(name)] = name\n", + "\n", + "models = list(modelToFileName.keys())\n", + "len(humanFiles)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Find Representative by K-medroid for different dists on GraphStat" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* Returns the index of the representative" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def findRep(graphStats, func):\n", + " out_ds = list(map(func, models))\n", + "\n", + " #choose a random starting point\n", + " start_index = random.randint(0, len(out_ds))\n", + "\n", + " # start with one initial metrid [start_index]\n", + " outdegree_kmedoid = kmedoids(out_ds, [start_index], metric=ks_metric)\n", + "\n", + " outdegree_kmedoid.process()\n", + " centoids = outdegree_kmedoid.get_medoids()\n", + " return centoids[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Find representative for out degree" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* the rep found is ../input/humanOutput\\R_20158_run_1.csv\n", + "* the average distance between it and others is 0.05515988287586802" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "../input/humanOutput\\R_20158_run_1.csv\n", + "../input/humanOutput\\R_20158_run_1.csv\n" + ] + } + ], + "source": [ + "od_rep_index = findRep(models, lambda m: m.out_d)\n", + "print(list(modelToFileName.values())[od_rep_index])\n", + "od_rep_model = models[od_rep_index]\n", + "print(modelToFileName[od_rep_model])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.05515988287586802\n" + ] + } + ], + "source": [ + "total_distance = 0\n", + "count = 0\n", + "for model in models:\n", + " total_distance += ks_value(od_rep_model.out_d, model.out_d)\n", + "print(total_distance / len(models))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Find Representative for node activities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* the rep found is ../input/humanOutput\\R_2016176_run_1.csv\n", + "* the average distance between it and others is 0.05275267434589047" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "../input/humanOutput\\R_2016176_run_1.csv\n", + "../input/humanOutput\\R_2016176_run_1.csv\n" + ] + } + ], + "source": [ + "total_distance = 0\n", + "for model in models:\n", + " total_distance += ks_value(od_rep_model.mpc, model.mpc)\n", + "print(total_distance / len(models))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.05275267434589047\n" + ] + } + ], + "source": [ + "total_distance = 0\n", + "count = 0\n", + "for model in models:\n", + " total_distance += ks_value(od_rep_model.na, model.na)\n", + "print(total_distance / len(models))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Find Representative for MPC" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* the rep found is ../input/humanOutput\\R_2015246_run_1.csv\n", + "* the average distance between it and others is 0.08556632702185384" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "../input/humanOutput\\R_2015246_run_1.csv\n", + "../input/humanOutput\\R_2015246_run_1.csv\n" + ] + } + ], + "source": [ + "mpc_rep_index = findRep(models, lambda m: m.mpc)\n", + "print(list(modelToFileName.values())[mpc_rep_index])\n", + "mpc_rep_model = models[mpc_rep_index]\n", + "print(modelToFileName[mpc_rep_model])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.08556632702185384\n" + ] + } + ], + "source": [ + "total_distance = 0\n", + "count = 0\n", + "for model in models:\n", + " total_distance += ks_value(od_rep_model.mpc, model.mpc)\n", + "print(total_distance / len(models))" + ] + }, + { + "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 +} -- cgit v1.2.3-54-g00ecf