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 +++++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100644 Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb (limited to 'Metrics/Metrics-Calculation/metrics_plot/model_evolve_comparison/src/metrics_distance.ipynb') 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 +} -- cgit v1.2.3-54-g00ecf