-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalanced_structure_sample_gen.py
60 lines (46 loc) · 1.57 KB
/
balanced_structure_sample_gen.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
#
# sample generator, for N nodes
# A balanced Sample:
# create node names 1 to N
# pick a mix ratio of group A to B of between .2 to .5
# generate following edges
# every combo pair from A
# every combo pair in B
# every combo of 1 from A and 1 from B
# shuffle them and output sample
# do above but change 1 ++ to -- or 1 -- to ++ in sample
import random
import itertools
names = [s.strip() for s in open('10000names.txt', 'r').readlines()] # base 10,000 names
def gen_edges(n, balanced=True, randomize=True, numbers=False):
global names
if n > 10000:
print('*** sample limited to 10000 names ***')
if numbers:
sample = [str(i) for i in range(1,n+1)]
else:
sample = random.sample(names, n)
i = int(random.randrange(int(n/4), int(n/4*3)))
group_a = sample[:i]
group_b = sample[i:]
a_edges = itertools.combinations(group_a, 2)
b_edges = itertools.combinations(group_b, 2)
cross_edges = itertools.product(group_a, group_b)
edges = [f'{n1} ++ {n2}' for n1, n2 in a_edges]
edges.extend([f'{n1} ++ {n2}' for n1, n2 in b_edges])
edges.extend([f'{n1} -- {n2}' for n1, n2 in cross_edges])
if not balanced: # change one sign
print("BALANCED:\n")
i = random.randrange(len(edges))
if '++' in edges[i]:
edges[i].replace('++', '--')
else:
edges[i].replace('--', '++')
else:
print("NOT BALANCED:\n")
if randomize:
random.shuffle(edges)
print(n, len(edges))
for l in edges:
print(l)
gen_edges(10,balanced=False, numbers=False, )