aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/metrics_plot/src/Metrics Comparison .ipynb
blob: 17ad1253562a22deb17d1f6b941545f1c6835afb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Metric comparison preperation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import readCSV as reader\n",
    "import glob\n",
    "import random \n",
    "from sklearn.manifold import MDS\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculateKSMatrix(dists):\n",
    "    dist = []\n",
    "\n",
    "    for i in range(len(dists)):\n",
    "        dist = dist + dists[i]\n",
    "    matrix = np.empty((len(dist),len(dist)))\n",
    "\n",
    "    for i in range(len(dist)):\n",
    "        matrix[i,i] = 0\n",
    "        for j in range(i+1, len(dist)):\n",
    "            value, p = stats.ks_2samp(dist[i], dist[j])\n",
    "            matrix[i, j] = value\n",
    "            matrix[j, i] = value\n",
    "            value, p = stats.ks_2samp(dist[j], dist[i])\n",
    "    return matrix\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculateMDS(dissimilarities):\n",
    "    embedding = MDS(n_components=2, dissimilarity='precomputed')\n",
    "    trans = embedding.fit_transform(X=dissimilarities)\n",
    "    return trans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot(names, coords, index = 0, title=''):\n",
    "    half_length = int(coords.shape[0] / len(names))\n",
    "    color = ['blue', 'red', 'green']\n",
    "    graph = plt.figure(index)\n",
    "    plt.title(title)\n",
    "    for i in range(len(names)):\n",
    "        x = (coords[(i*half_length):((i+1)*half_length), 0].tolist())\n",
    "        y = (coords[(i*half_length):((i+1)*half_length), 1].tolist())\n",
    "        plt.plot(x, y, color=color[i], marker='o', label = names[i], linestyle='', alpha=0.7)\n",
    "    plt.legend(loc='upper right')\n",
    "    plt.savefig(fname = title+'.png', dpi=150)\n",
    "    #graph.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read Files\n",
    "1. define class for metric reading of each graph type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class GraphType:\n",
    "    \n",
    "    # init with path contrain files and number of files to read reader is imported from (readCSV)\n",
    "    def __init__(self, path, number):\n",
    "        self.out_ds = []\n",
    "        self.nas = []\n",
    "        self.mpcs = []\n",
    "        models = reader.readmultiplefiles(path, number)\n",
    "        for i in range(len(models)):\n",
    "            out_d, na, mpc = reader.getmetrics(models[i])\n",
    "            self.out_ds.append(out_d)\n",
    "            self.nas.append(na)\n",
    "            self.mpcs.append(mpc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. read metrics for each graph type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "human = GraphType('../statistics/humanOutput/', 300)\n",
    "viatra30 = GraphType('../statistics/viatraOutput30/', 300)\n",
    "viatra100 = GraphType('../statistics/viatraOutput100/', 300)\n",
    "random = GraphType('../statistics/randomOutput/', 300)\n",
    "alloy = GraphType('../statistics/alloyOutput/', 300)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* outdegree comparison for human, Viatra30, and alloy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-5692e29d4679>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mout_d_coords\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcalculateMDS\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcalculateKSMatrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mviatra30\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mout_ds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malloy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mout_ds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhuman\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mout_ds\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Viatra (30 nodes)'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Alloy (30 nodes)'\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;34m'Human'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mout_d_coords\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Out Degree'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-3-37c4264e6073>\u001b[0m in \u001b[0;36mcalculateKSMatrix\u001b[1;34m(dists)\u001b[0m\n\u001b[0;32m     12\u001b[0m             \u001b[0mmatrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m             \u001b[0mmatrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m             \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstats\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mks_2samp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdist\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdist\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mmatrix\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\scipy\\stats\\stats.py\u001b[0m in \u001b[0;36mks_2samp\u001b[1;34m(data1, data2)\u001b[0m\n\u001b[0;32m   4854\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4855\u001b[0m     \"\"\"\n\u001b[1;32m-> 4856\u001b[1;33m     \u001b[0mdata1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4857\u001b[0m     \u001b[0mdata2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4858\u001b[0m     \u001b[0mn1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\numpy\\core\\fromnumeric.py\u001b[0m in \u001b[0;36msort\u001b[1;34m(a, axis, kind, order)\u001b[0m\n\u001b[0;32m    932\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    933\u001b[0m         \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0masanyarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"K\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 934\u001b[1;33m     \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mkind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    935\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    936\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "out_d_coords = calculateMDS(calculateKSMatrix([viatra30.out_ds, alloy.out_ds, human.out_ds]))\n",
    "plot(['Viatra (30 nodes)', 'Alloy (30 nodes)' , 'Human'], out_d_coords,0, 'Out Degree')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* outdegree comparison for human, Viatra30, and alloy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_d_coords = calculateMDS(calculateKSMatrix([viatra30.nas, alloy.nas, human.nas]))\n",
    "plot(['Viatra (30 nodes)', 'Alloy (30 nodes)' , 'Human'], out_d_coords,0, 'Node Activity')"
   ]
  },
  {
   "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
}