-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmix_bag.py
508 lines (407 loc) · 13.3 KB
/
mix_bag.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from colorama import Fore, Style
import random
class Bunch(object):
def __init__(self, **kwds):
self.__dict__.update(kwds)
class Sampler(object):
"""Sampler class - Takes a sample from a dataset.
Parameters
----------
dataset: a list of datapoints
percentage: relative size of sample w.r.t. size of the dataset.
"""
def __init__(self, set_to_sample_from, percentage):
self.dataset = set(set_to_sample_from)
self.samplesize = int(len(set_to_sample_from) * percentage)
self.take_sample_from_set()
self.resample = self.take_sample_from_set
def take_sample_from_set(self):
"""
Return a sample from the dataset.
Parameters
----------
testset: list of tuples.
i.e.: [(0, 'nonCancer'), (1, 'nonCancer'),
(1208, 'cancer'), (2431, 'cancer'),
(2, 'nonCancer')]
percentage: sample size in terms of a percentage of the testset.
Return
------
random subset of testset.
"""
self.sample = set(random.sample(self.dataset, self.samplesize))
self.remainder = set(self.dataset) - self.sample
assert len(self.sample.intersection(self.remainder)) == 0
assert len(self.sample) + len(self.remainder) == len(self.dataset)
class Node(object):
"""
A Node class
"""
def __init__(self, name):
self.name = name
def __str__(self):
return str(self.name)
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def is_function(self):
"""
Returns
-------
False (this method is overridden in the subclass Function)
"""
return False
def is_cancer_protein(self):
"""
Returns
-------
False (this method is overridden in the subclass CancerProtein)
"""
return False
def degree(self, graph):
"""
Parameters
----------
graph: A <class 'networkx.classes.graph.Graph'> object
Returns
-------
Degree of the node in the graph
"""
return graph.degree(self)
def neighbors(self, graph):
"""
Parameters
----------
graph: A <class 'networkx.classes.graph.Graph'> object
Returns
-------
Neighbors of the node.
"""
return graph.neighbors(self)
def neighbors_iter(self, graph):
"""
Parameters
----------
graph: A <class 'networkx.classes.graph.Graph'> object
Returns
-------
Neighbors of the node.
"""
return graph.neighbors_iter(self)
def cp_neighbors(self, graph):
"""
Returns
-------
cps: a list of cancerous protein neighbors
"""
nbrs = self.neighbors(graph)
cps = [p for p in nbrs if p.is_cancer_protein()]
return cps
def cp_degree(self, hgraph):
"""
Returns
-------
number of cancerous protein neighbors
"""
return len(self.cp_neighbors(hgraph))
class Function(Node):
"""
Subclass of Node.
"""
def __init__(self, name):
self.name = name
def __repr__(self):
string = "%s"
subst = (str(self.name),)
return Fore.GREEN + string % subst + Style.BRIGHT + Fore.WHITE
def color(self):
return 'g'
def is_function(self):
"""
Returns
-------
True. (overrrides the Node.is_function() method.)
"""
return True
def has_cps(self, fgraph):
"""
Returns
-------
True if the function contains cancerous proteins.
"""
if self.cp_neighbors(fgraph) == 0:
return False
else:
return True
def cp_p_ratio(self, fgraph):
"""
Returns
-------
The ratio of (cancerous proteins)/(proteins) of the function.
"""
number_of_proteins = fgraph.degree(self)
return float(self.cp_degree(fgraph)) / number_of_proteins
def cancerweight(self, hgraph, fgraph):
"""
Return
------
sum of cp_degrees of all cp's in this function
"""
weight = 0
for cp in self.cp_neighbors(fgraph):
weight = weight + cp.cp_degree(hgraph)
return weight
class Protein(Node):
def __repr__(self):
string = "P%s"
subst = (str(self.name),)
return string % subst
def color(self):
return 'w'
def functions(self, fgraph):
"""
Returns
-------
Neighbors of nodes
"""
if self in fgraph.nodes():
return self.neighbors(fgraph)
else:
return []
def fn_degree(self, fgraph):
"""
Returns
-------
The length of a given graph using the functions method
"""
return len(self.functions(fgraph))
def cpp_ratio(self, hgraph):
"""
Return The ratio of (neighbor cancerous proteins)/(neighbor proteins)
of the protein.
"""
return float(self.cp_degree(hgraph)) / number_of_nbrs
def shared_cp_functions(self, hgraph, fgraph):
"""
Returns a list of functions that shares with neighbors
cancerous proteins.
"""
shared_fns = set()
for cp_nbr in self.cp_neighbors(hgraph):
if cp_nbr in fgraph.nodes():
cp_nbr_fns = set(cp_nbr.neighbors(fgraph))
shared_fns = shared_fns.union(cp_nbr_fns)
else:
pass
return list(shared_fns)
def fn_cp_degree(self, hgraph, fgraph):
"""Returns the number of functions that protein shares with neighbors cancerous proteins."""
return len(self.shared_cp_functions(hgraph, fgraph))
def fn_cp_weight(self, hgraph, fgraph):
"""
Returns a weight based on the cancerweight of the functions that
the protein shares with cancerous neighbors
"""
weight = 0
for fn in self.shared_cp_functions(hgraph, fgraph):
weight = weight + fn.cancerweight(hgraph, fgraph)
return round(weight, 2)
def cp_degree_of_neighbors(self, hgraph):
"""Returns the sum of cp_degrees of each cancerous protein neighbors """
nbrs = self.neighbors(hgraph)
total = 0
for i in nbrs:
total += i.cp_degree(hgraph)
return total
def cancerweight(self, hgraph, fgraph):
"""
Returns a weight to determine the likelihood a protein is a cancerous
protein
"""
return self.fn_cp_weight(hgraph, fgraph)
class CancerProtein(Protein):
"""
Subclass of Protein"""
def __repr__(self):
string = "P%s"
subst = (str(self.name),)
return Fore.RED + string % subst + Fore.WHITE
def color(self):
return 'r'
def is_cancer_protein(self):
"""
True (overrides protein.is_cancer_protein)
"""
return True
class Results(object):
""" Helper Results class - Container for a prediction set. Analyses
predictions and calculates Precision, Recall and F-Measure.
Parameters
----------
protein_answer = list of tuples = [ (protein, answer),
(123, 'cancer'),
... , ]
predictions: list of predictions = ['cancer','nonCancer', ... ]
"""
def __init__(self, protein_answer, predictions):
self.true_positives = 0
self.true_negatives = 0
self.false_positives = 0
self.false_negatives = 0
self.proteins, self.answers = zip(*list(protein_answer))
self.predictions = predictions
for i, prediction in enumerate(predictions):
if self.answers[i] == prediction and \
prediction == 'nonCancer':
outcome = "True Negative"
self.true_negatives += 1
elif self.answers[i] == prediction and \
prediction == 'cancer':
outcome = "True Positive"
self.true_positives += 1
elif self.answers[i] != prediction and \
prediction == 'nonCancer':
outcome = "False Negative"
self.false_negatives += 1
else:
outcome = "False Positive"
self.false_positives += 1
def print_results(self):
print("Protein Answer Prediction Outcome \n" +
"--------------------------------------------")
line = "%s %s %s %s"
for i, prediction in enumerate(self.predictions):
if self.answers[i] == prediction and \
prediction == 'nonCancer':
outcome = "True Negative"
elif self.answers[i] == prediction and \
prediction == 'cancer':
outcome = "True Positive"
elif self.answers[i] != prediction and \
prediction == 'nonCancer':
outcome = "False Negative"
else:
outcome = "False Positive"
substitutions = (str(self.proteins[i]).ljust(10), str(
self.answers[i]).ljust(10), prediction.ljust(13), outcome)
print(line % substitutions)
def print_confusion_matrix(self):
string = "\n" + \
"Confusion Matrix \n\n" + \
" Tp | Fn %s | %s \n" + \
"------------- ------------------\n" + \
" Fp | Tn %s | %s \n\n" + \
"Precision: %f Recall: %f F-Measure: %f"
substitutions = (str(self.true_positives).ljust(3),
str(self.false_negatives),
str(self.false_positives).ljust(3),
str(self.true_negatives),
self.precision, self.recall, self.f_measure)
print(string % substitutions)
def write_to_file(self):
"""Write the results into a file"""
pass
@property
def precision(self):
"""
Parameters
----------
true_positives: number of true positives (int)
false_positives: number of false positives (int)
Returns
-------
Precision, which is defined as:
# True_pos
Precision = --------------------------
(#True_pos + #False_pos)
"""
Tp = self.true_positives
Fp = self.false_positives
if Tp == 0:
return 0
else:
return float(Tp) / (Tp + Fp)
@property
def recall(self):
"""
Parameters
----------
true_positives: number of true positives (int)
false_negatives: number of false negatives (int)
Returns
-------
Recall, which is defined as:
# True_pos
Recall = --------------------------
(#True_pos + #False_neg)
"""
Tp = self.true_positives
Fn = self.false_negatives
if Tp == 0:
return 0
else:
return float(Tp) / (Tp + Fn)
@property
def f_measure(self):
"""
Parameters
----------
p: precision (float)
r: recall (float)
Returns
-------
F-Measure, which is defined as:
2 * precision * recall
F-Measure = ------------------------
( precision + recall )
"""
p = self.precision
r = self.recall
if (p or r) == 0:
return 0
else:
return float(2 * p * r) / (p + r)
class Table(object):
"""Helper Class to facilitate table formatting"""
def __init__(self, name="Table_name"):
self.name = name
self.separator = ', '
self.colwidth = 15
self.arrays = {}
def add(self, array_name, elem):
""" Add element to array """
if array_name in self.arrays:
self.arrays[array_name].append(elem)
else:
self.arrays[array_name] = [elem]
def get_column_order(self):
""" Order the columns and return it. """
try:
columns = self.order
except AttributeError:
columns = self.arrays.keys()
return columns
def columns_names(self):
""" Helper function: Join the ordered columns and join it toegether """
columns = self.get_column_order()
sep = self.separator
string = sep.join(c.ljust(self.colwidth) for c in columns) + '\n'
return string
def lines_iter(self):
"""
Iterator for lines to be written. Assumes all arrays are
of equal length.
Return Iterator
"""
width = self.colwidth
sep = self.separator
arrays = self.arrays
columns = self.get_column_order()
arraylength = len(self.arrays.values()[0])
i = 0
while i < arraylength:
line = sep.join([str(arrays[c][i]).ljust(width)
for c in columns]) + '\n'
i += 1
yield line