-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
89 lines (74 loc) · 2.53 KB
/
generator.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
import numpy as np
class Generator:
def __init__(self):
pass
class GridGenerator(Generator):
def __init__(self):
super().__init__()
def generate_checkerboard(self, d):
if d % 2 == 0:
v = np.zeros((d, d))
for i in range(d):
v[i, i % 2 + np.arange(0, d, 2)] = 1
return v.flatten()
else:
v = np.zeros(d * d)
v[np.arange(0, d * d, 2)] = 1
return v
def generate_strips(self, d):
if d % 2 == 1:
v = np.zeros((d, d))
for i in range(d):
v[i, i % 2 + np.arange(0, d, 2)] = 1
return v.flatten()
else:
v = np.zeros(d * d)
v[np.arange(0, d * d, 2)] = 1
return v
def generate_halves(self, d):
v = np.zeros((d, d))
k = d // 2
v[:k] = 1
return v.flatten()
def generate_halves_purturbed(self, d, q=0.1):
v = np.zeros((d, d))
k = d // 2
v[:k] = 1
v = v.flatten()
ridx = np.random.choice(v.shape[0], int(q * v.shape[0]), replace=False)
v[ridx] = 1
return v
@staticmethod
def generate_multicat_permutation_configuration(d, cs, ns):
v = np.zeros((d * d))
cidx = np.arange(d * d)
for i, (c, n) in enumerate(zip(cs, ns)):
idx = np.random.choice(cidx.shape[0], n, replace=False)
v[cidx[idx]] = int(c)
cidx = np.delete(cidx, idx)
return v
@staticmethod
def perturb_weight(w_map, r=0.1):
N = w_map.shape[0] * w_map.shape[1]
W = int(np.sum(w_map) * r / 2)
ups = np.random.choice(N, W, replace=False)
downs = np.random.choice(N, W, replace=False)
w_perturbed = np.zeros_like(w_map.flatten()) + w_map.flatten()
w_perturbed[ups] += 1
w_perturbed[downs] -= 1
w_perturbed[w_perturbed == 2] = 1
w_perturbed[w_perturbed == -1] = 0
return w_perturbed.reshape(w_map.shape)
@staticmethod
def offset_weight(w_map, r=0.1):
W = int(np.sum(w_map) * np.abs(r))
if r < 0:
idx = np.where(w_map.flatten() == 1)[0]
else:
idx = np.where(w_map.flatten() == 0)[0]
changes = np.random.choice(idx, W, replace=False)
w_perturbed = np.zeros_like(w_map.flatten()) + w_map.flatten()
w_perturbed[changes] += np.sign(r)
# w_perturbed[w_perturbed == 2] = 1
# w_perturbed[w_perturbed == -1] = 0
return w_perturbed.reshape(w_map.shape)