forked from hvannieuwenh/dotCategoryLearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dots.py
200 lines (159 loc) · 6.01 KB
/
dots.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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
from itertools import permutations
import os
def gen_folder_name(name, current_ID = 1):
folder_name = f"{name}_{current_ID}"
c = current_ID
while os.path.isdir(folder_name):
c += 1
folder_name = f"{name}_{c}"
return folder_name
def check_within(new_dot, prototype):
thrs = 2
x_new, y_new = new_dot
for dot in prototype:
x, y = dot
dist = np.linalg.norm([x - x_new, y - y_new], ord=2)
if dist < thrs:
return True
return False
def check_across(P_new,
Ps,
thrs_dot = 2.5,
N_thrs_dots = 3,
llim_avg = -np.inf,
ulim_avg = np.inf
):
N_dots = P_new.shape[0]
perms_iter = permutations(range(0, N_dots))
perms_list = []
for p in perms_iter:
perms_list.append(p)
perms = np.array(perms_list)
for prototype in Ps:
dists = np.zeros(perms.shape)
for (i, dot) in enumerate(prototype):
x, y = dot
for (j, dot_new) in enumerate(P_new):
x_new, y_new = dot_new
d = np.linalg.norm([x - x_new, y - y_new], ord=1)
idx = np.where(perms[:, j] == i)
dists[idx, j] = d
dist_perms = np.sum(dists, 1)
idx_min = np.where(dist_perms == np.min(dist_perms))
dist_min = dists[idx_min, :]
avg_dist = np.sum(dist_min) / N_dots
count_above_thrs = len(np.where(dist_min < thrs_dot)[0])
if (avg_dist <= llim_avg) or (avg_dist >= ulim_avg) or (count_above_thrs > N_thrs_dots):
return True
return False
def gen_prototype(N_dots, sz_grid):
width, height = sz_grid
P = np.zeros((N_dots, 2))
for i in range(N_dots):
x = np.random.randint(0, width)
y = np.random.randint(0, height)
while check_within([x, y], P[:i, :]):
x = np.random.randint(0, width)
y = np.random.randint(0, height)
P[i] = [x, y]
return P
def gen_prototypes(N_prototypes, N_dots, sz_grid, llim, ulim):
Ps = np.zeros((N_prototypes, N_dots, 2))
is_bad_prototypes = True
while is_bad_prototypes:
is_bad_prototypes = False
Ps[0, :, :] = gen_prototype(N_dots, sz_grid)
for i in range(1, N_prototypes):
Ps[i, :, :] = gen_prototype(N_dots, sz_grid)
if not check_across(Ps[i,:,:], Ps[:i,:,:], llim_avg=llim, ulim_avg=ulim):
is_bad_prototypes = True
break
return Ps
def gen_exemplar(prototype, probs):
exemplar = np.zeros(prototype.shape)
for (i, dot) in enumerate(prototype):
offset = np.random.choice(range(1, len(probs) + 1), p = probs)
idx_offset = np.random.choice([0, 1])
dot_exemplar = np.copy(dot)
dot_exemplar[idx_offset] += np.random.choice([-offset, offset])
dot_exemplar[not idx_offset] += np.random.randint(-offset, offset + 1)
while check_within(dot_exemplar, exemplar[:i]):
offset = np.random.choice(range(1, len(probs) + 1), p = probs)
idx_offset = np.random.choice([0, 1])
dot_exemplar = np.copy(dot)
dot_exemplar[idx_offset] += np.random.choice([-offset, offset])
dot_exemplar[not idx_offset] += np.random.randint(-offset, offset + 1)
exemplar[i, :] = dot_exemplar
return exemplar
def gen_exemplars(N_exemplars, prototype, probs):
exs = np.zeros((N_exemplars, prototype.shape[0], prototype.shape[1]))
is_bad_exemplars = True
while is_bad_exemplars:
is_bad_exemplars = False
exs[0, :, :] = gen_exemplar(prototype, probs)
for i in range(1, N_exemplars):
exs[i, :, :] = gen_exemplar(prototype, probs)
if not check_across(exs[i,:,:], exs[:i,:,:], N_thrs_dots=2):
is_bad_exemplars = True
break
return exs
def plot(P, sz_grid, filename, show=False, save=True):
fig, ax = plt.subplots(figsize=(5,5))
dots = [Circle((dot[0], dot[1]), radius=0.5) for dot in P]
c = PatchCollection(dots, color='k')
ax.add_collection(c)
lims = np.array([-8, 8]) + np.array([0, sz_grid[0]]) # assuming square grid
ax.set_xlim(*lims)
ax.set_ylim(*lims)
plt.axis('off')
if save:
plt.savefig(filename + '.png')
if show:
plt.show()
plt.close(fig)
N_prototypes = 4
N_dots = 7
N_exemplars = 100
sz_grid = (16, 16)
llim_easy = 3
ulim_easy = 5
probs_easy = [0.2, 0.3, 0.4, 0.05, 0.05]
llim_hard = 3
ulim_hard = 5
#probs_hard = [0.2, 0.3, 0.4, 0.05, 0.05]
probs_hard = [0.0, 0.24, 0.16, 0.3, 0.3]
Ps = gen_prototypes(N_prototypes, N_dots, sz_grid, llim=llim_easy, ulim=ulim_easy)
exs = np.zeros((N_prototypes, N_exemplars, N_dots, 2))
for i in range(0, N_prototypes):
exs[i,:,:,:] = gen_exemplars(N_exemplars, Ps[i,:,:], probs_easy)
pack_name = gen_folder_name('./stimuli/pack', 1)
os.mkdir(pack_name)
for i in range(0, N_prototypes):
cat_name = gen_folder_name(pack_name + "/cat")
os.mkdir(cat_name)
ID_prototype = cat_name.split("_")[-1]
file_name = cat_name + "/prot_" + ID_prototype
plot(Ps[i,:,:], sz_grid, file_name)
for j in range(0, N_exemplars):
file_name = cat_name + "/ex_" + ID_prototype + f"_{j+1}"
plot(exs[i,j,:,:], sz_grid, file_name)
"""
exs = np.zeros((N_prototypes, N_exemplars, N_dots, 2))
for i in range(0, N_prototypes):
exs[i,:,:,:] = gen_exemplars(N_exemplars, Ps[i,:,:], probs_hard)
pack_name = gen_folder_name('./stimuli/pack', 'hard')
os.mkdir(pack_name)
for i in range(0, N_prototypes):
cat_name = gen_folder_name(pack_name + "/cat")
os.mkdir(cat_name)
ID_prototype = cat_name.split("_")[-1]
file_name = cat_name + "/prot_" + ID_prototype
plot(Ps[i,:,:], sz_grid, file_name)
for j in range(0, N_exemplars):
file_name = cat_name + "/ex_" + ID_prototype + f"_{j+1}"
plot(exs[i,j,:,:], sz_grid, file_name)
"""