-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhamming.py
More file actions
79 lines (41 loc) · 2.08 KB
/
Copy pathhamming.py
File metadata and controls
79 lines (41 loc) · 2.08 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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 10 11:45:20 2019
@author: daryl
"""
def greedy_hamming(base_partition, new_partition):
names = [j for j in base_partition.parts]
new_names = {}
for i in new_partition.parts:
intersection_sizes = {}
for name in names:
intersection_sizes.update({len(set(base_partition.assignment.parts[name]).intersection(set(new_partition.assignment.parts[i]))): name})
new_names.update({intersection_sizes[max(intersection_sizes.keys())]: i})
names.remove(intersection_sizes[max(intersection_sizes.keys())])
tot_nodes = len(new_partition.assignment)
final_int_sizes = []
for i in base_partition.parts:
x = len(set(new_partition.assignment.parts[new_names[i]]).intersection(set(base_partition.assignment.parts[i])))
final_int_sizes.append(x)
ham_dist = tot_nodes - sum(final_int_sizes)
return new_names, ham_dist;
def greedy_hamming_pop(base_partition, new_partition):
names = [j for j in base_partition.parts]
new_names = {}
for i in new_partition.parts:
intersections = {}
intersection_pops = {}
for name in names:
intersections.update({name: set(base_partition.assignment.parts[name]).intersection(set(new_partition.assignment.parts[i]))})
intersection_pops.update({sum([new_partition.graph.nodes[node]["TOTPOP"] for node in intersections[name]]): name})
new_names.update({intersection_pops[max(intersection_pops.keys())]: i})
names.remove(intersection_pops[max(intersection_pops.keys())])
tot_pop = sum(base_partition["population"].values())
final_int_pops = []
for i in base_partition.parts:
intersection_set = set(new_partition.assignment.parts[new_names[i]]).intersection(set(base_partition.assignment.parts[i]))
intersection_list = list(intersection_set)
intersection_pops = sum([new_partition.graph.nodes[node]["TOTPOP"] for node in intersection_list])
final_int_pops.append(intersection_pops)
ham_dist_pop = tot_pop - sum(final_int_pops)
return new_names, ham_dist_pop;