aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/metrics_plot/src/Metrics Comparison .ipynb
blob: 04af87735b9397bf5d4a3d414291a1df3d21d540 (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
208
209
210
211
212
213
214
215
216
217
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Metric comparison preperation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "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": 15,
   "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": 16,
   "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": 17,
   "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": 18,
   "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": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "too many values to unpack (expected 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-c45dfc2a26c6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhuman\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraphType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'../statistics/humanOutput/'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m300\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[0mviatra30\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraphType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'../statistics/viatraOutput30/'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m300\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mviatra100\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraphType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'../statistics/viatraOutput100/'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m300\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mrandom\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraphType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'../statistics/randomOutput/'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m300\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0malloy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraphType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'../statistics/alloyOutput/'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m300\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-18-556621ada738>\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, path, number)\u001b[0m\n\u001b[0;32m      8\u001b[0m         \u001b[0mmodels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreader\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadmultiplefiles\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodels\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[1;32m---> 10\u001b[1;33m             \u001b[0mout_d\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mna\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmpc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreader\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetmetrics\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodels\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     11\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mout_ds\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mout_d\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mna\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 3)"
     ]
    }
   ],
   "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": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'viatra30' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-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;31mNameError\u001b[0m: name 'viatra30' is not defined"
     ]
    }
   ],
   "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
}