-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
80 lines (67 loc) · 1.8 KB
/
util.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
import config
import random
import hashlib
import subprocess
import networkx as nx
def hash(text):
return hashlib.md5(text.encode('utf8')).hexdigest()
def noise(strength=1):
return (random.random() - 0.5) * strength
def walk_sort(edges):
"""sort nodes by degree, starting from
the highest-degree node, identifying connected nodes,
selecting the highest-degree node from those, etc.
only walks connected nodes so the returned
nodes may be less than specified by the edges"""
g = nx.Graph()
g.add_edges_from(edges)
connected = set()
degree = nx.degree(g)
ordering = []
while degree:
next = max_degree_node(g, degree, connected)
if next is not None:
ordering.append(next)
else:
break
return ordering
def max_degree_node(g, d, connected):
"""select the highest-degree node from
a set of connected nodes"""
if not connected:
deg = d
else:
deg = {k: v for k, v in d.items() if k in connected}
if not deg:
return None
n = max(deg.keys(), key=lambda k: deg[k])
d.pop(n)
for n_ in g.neighbors(n):
connected.add(n_)
return n
def assign_entity_colors(pairs):
"""assign entities colors
based on what pair groups they're part of"""
groups = []
for a, b in pairs:
for grp in groups:
if a in grp or b in grp:
grp.add(a)
grp.add(b)
else:
groups.append(set([a, b]))
colors = {}
for grp in groups:
color = random.choice(config.COLORS)
for id in grp:
colors[id] = color
return colors
def sync():
proc = subprocess.Popen([
'rsync',
'-rv',
'--delete',
'public/',
config.REMOTE
])
proc.communicate()