-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplexity.py
More file actions
211 lines (175 loc) · 7.58 KB
/
complexity.py
File metadata and controls
211 lines (175 loc) · 7.58 KB
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
import math
import copy
class Complexity:
def __init__(self, graph=[], partition=[]):
self.matrix = [[0] * len(graph[0])] + graph
self.partitions = [[0]] + partition
self.proportions = []
for i in range(0, len(self.matrix)):
self.proportions += [[i, self.matrix[i], 1]]
def print(self):
try:
for num, part in enumerate(self.partitions):
print("Environment") if num == 0 else print("Partition " + str(num))
for i in part:
# print(part)
print(
str(self.proportions[i][0])
+ ": "
+ str(self.proportions[i][1])
+ "; pL(n) = "
+ str(self.proportions[i][2])
)
except:
print("Index error on index: " + str(i))
return
def equals(self, arr1, arr2):
for i in range(0, len(arr1)):
if arr1[i] != arr2[i]:
return False
return True
def pL(self, proportionMatrix=None):
if proportionMatrix == None:
proportionMatrix = self.proportions
else:
for elem in proportionMatrix:
elem[2] = 1
for i in range(0, len(proportionMatrix)):
for j in range(i + 1, len(proportionMatrix)):
if self.equals(proportionMatrix[i][1], proportionMatrix[j][1]):
proportionMatrix[i][2] += 1
proportionMatrix[j][2] += 1
for k in proportionMatrix:
k[2] /= len(proportionMatrix)
def subgraphMatrix(self, node):
# print("=" * 20 + "subgraphMatrix" + "=" * 20)
subGraph = copy.deepcopy(self.proportions)
for index, i in enumerate(subGraph[node][1]):
# print(index, i)
for j in subGraph:
if j[0] == node:
continue
if i == 0:
j[1][index] = 0
# print(*subGraph, sep="\n")
# print("=" * 20 + "subgraphMatrix" + "=" * 20)
return subGraph
def intraMatrix(self, proportionMatrix=None, partitions=None):
if proportionMatrix == None:
proportionMatrix = copy.deepcopy(self.proportions)
if partitions == None:
partitions = copy.deepcopy(self.partitions)
for part in range(1, len(partitions)):
# print(
# *(proportionMatrix[partitions[part][0] : (partitions[part][-1] + 1)]),
# sep="\n",
# )
# print()
# if len(partitions[part]) == 1:
# continue
for edgeBitindex in range(0, len(proportionMatrix[partitions[part][0]][1])):
# print(edgeBitindex)
total = 0
for dist in proportionMatrix[
partitions[part][0] : (partitions[part][-1] + 1)
]:
if dist[1][edgeBitindex] == 1:
total += 1
if total != 2:
for dist in proportionMatrix[
partitions[part][0] : (partitions[part][-1] + 1)
]:
dist[1][edgeBitindex] = 0
# print(
# *(proportionMatrix[partitions[part][0] : (partitions[part][-1] + 1)]),
# sep="\n",
# )
return proportionMatrix
def interMatrix(self, proportionMatrix=None):
if proportionMatrix == None:
proportionMatrix = copy.deepcopy(self.proportions)
intraMatrix = self.intraMatrix()
for edgebit in range(0, len(proportionMatrix[0][1])):
for proportion, x in enumerate(proportionMatrix):
if intraMatrix[proportion][1][edgebit] == 1:
proportionMatrix[proportion][1][edgebit] = 0
return proportionMatrix
def returnMatrix(self, proportionMatrix=None):
if proportionMatrix == None:
proportionMatrix = copy.deepcopy(self.proportions)
if proportionMatrix[0][0] == 0:
return list(map(list, zip(*proportionMatrix)))[1][1:]
return list(map(list, zip(*proportionMatrix)))[1]
def purgeNodes(self, proportions, partitions):
returnProportions = copy.deepcopy(proportions)
returnPartitions = copy.deepcopy(partitions)
for proportion in proportions:
for edgebit in proportion[1]:
if edgebit == 1:
break
if edgebit == 0 and proportion[0] != 0:
# print("Empty row", proportion)
remove = False
for i, part in enumerate(returnPartitions):
for j, node in enumerate(part):
if remove and node != -1:
returnPartitions[i][j] -= 1
if partitions[i][j] == proportion[0]:
remove = True
returnPartitions[i][j] = -1
# +[-1] because env variable is always 0
flattenPartitions = [-1] + [x for sublist in returnPartitions for x in sublist]
for i in range(0, len(flattenPartitions)):
if flattenPartitions[i] == -1:
returnProportions[i][0] = -1
for i in range(0, len(returnPartitions)):
returnPartitions[i] = list(filter(lambda x: x != -1, returnPartitions[i]))
returnProportions = [x for x in returnProportions if (x[0] != -1)]
return (returnProportions, returnPartitions)
def sizeComplexity(self, proportionMatrix=None):
if proportionMatrix == None:
proportionMatrix = self.proportions
total = 0.0
for n in proportionMatrix:
if n[0] == 0:
continue
total += -math.log(n[2], 2)
return total
def genComplexity(self, nodeMatrix, overalMatrix=None):
if overalMatrix == None:
overalMatrix = self.proportions
# nodeMatrix = array of all connectivity matrices of individual nodes
total = 0
for i in range(1, len(nodeMatrix)):
total += self.sizeComplexity(nodeMatrix[i])
total -= self.sizeComplexity(overalMatrix)
return total
# print(*nodeMatrix[i], sep="\n")
# print()
class Coupling(Complexity):
def __init__(self, graph=[], partition=[]):
super().__init__(graph, partition)
def coupComplexity(self, interMatrix):
return self.genComplexity(interMatrix)
class Cohesion(Complexity):
def __init__(self, graph=[], partition=[]):
super().__init__(graph, partition)
def completeMatrix(self, nodesNum):
edgeMatrix = []
for i in range(0, nodesNum):
edgeMatrix += [[0] * round((nodesNum * (nodesNum - 1)) / 2)]
startColumn = 0
edgeCount = nodesNum - 1
for index, row in enumerate(edgeMatrix):
counterRow = index + 1
for i in range(startColumn, startColumn + edgeCount):
edgeMatrix[index][i] = 1
edgeMatrix[counterRow][i] = 1
counterRow += 1
startColumn += edgeCount
edgeCount -= 1
# print(*edgeMatrix, sep="\n")
# print()
return edgeMatrix
def cohComplexity(self, intraMatrix, completeMatrix):
return self.genComplexity(intraMatrix) / self.genComplexity(completeMatrix)