-
Notifications
You must be signed in to change notification settings - Fork 0
/
feng.py
336 lines (256 loc) · 11.8 KB
/
feng.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import imagen as ig
import numpy as np
import scipy.io as sio
import perturbation as pert
from holoviews.core import BoundingBox
import param
from imagen.patterngenerator import PatternGenerator, Composite
from imagen.patternfn import float_error_ignore
# ##################### SineGrating
def SineG(x, y, BlackBackground, phase_shift, Bound,frames):
# orientations = [0]
orientations = [0, np.pi/4]
frequencies = [2]
phase = np.pi/2
paras = [[[ori, fre, phase]] for ori in orientations for fre in frequencies]
if phase_shift:
paras = pert.modulation_of_phase(paras,'SG',frames)
else:
frames = 1
# generates images and saved into a numpy array
SG = np.empty([len(paras), frames], dtype=np.object)
idx_proto = 0
for pars in paras:
idx_shift = 0
for p in pars:
SG[idx_proto][idx_shift] = ig.SineGrating(bounds=Bound, phase=p[2], orientation=p[0],
frequency=p[1], xdensity=x, ydensity=y)()
if BlackBackground:
assert SG[idx_proto][idx_shift].max() <= 1.0 and SG[idx_proto][idx_shift].min() >= 0
SG[idx_proto][idx_shift] = 1 - SG[idx_proto][idx_shift]
idx_shift += 1
idx_proto += 1
return SG
# ##################### HyperbolicGrating
class HyperbolicGrating(PatternGenerator):
"""
Concentric rectangular hyperbolas with Gaussian fall-off which share the same asymptotes.
abs(x^2/a^2 - y^2/a^2) = 1, where a mod size = 0
"""
aspect_ratio = param.Number(default=1.0,bounds=(0.0,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Ratio of width to height.")
size = param.Number(default=0.5,bounds=(0.0,None),softbounds=(0.0,2.0),
precedence=0.62,doc="Size as distance of inner hyperbola vertices from the centre.")
phase = param.Number(default=0.0,bounds=(0.0,None),softbounds=(0.0,2.0*np.pi),
precedence=0.51,doc="Phase shift from the centre.")
def function(self,p):
aspect_ratio = p.aspect_ratio
x = self.pattern_x/aspect_ratio
y = self.pattern_y
size = p.size
phase = p.phase
distance_from_vertex_middle = np.fmod(np.sqrt(np.absolute(x**2 - y**2))+phase*size/(2*np.pi),size)
distance_from_vertex_middle = np.minimum(distance_from_vertex_middle,size - distance_from_vertex_middle)
return (np.cos(distance_from_vertex_middle/size*2*np.pi)+1)/2
def HyperG(x, y, BlackBackground, phase_shift, Bound,frames):
orientations = [0]
sizes = [1/s for s in [2]]
paras = [[[o,s,0.0]] for o in orientations for s in sizes]
if phase_shift:
paras = pert.modulation_of_phase(paras,'HG',frames)
else:
frames = 1
HG = np.empty([len(paras),frames],dtype=np.object)
idx_proto = 0
for pars in paras:
idx_shift = 0
for p in pars:
HG[idx_proto][idx_shift] = HyperbolicGrating(bounds=Bound, orientation=p[0],size=p[1],
phase=p[2], xdensity=x, ydensity=y)()
if BlackBackground:
assert HG[idx_proto][idx_shift].max() <= 1.0 and HG[idx_proto][idx_shift].min() >= 0
HG[idx_proto][idx_shift] = 1 - HG[idx_proto][idx_shift]
idx_shift += 1
idx_proto += 1
return HG
# ##################### SpiralGrating
class Spiral(PatternGenerator):
aspect_ratio = param.Number(default=1.0,bounds=(0.0,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Ratio of width to height.")
outer_parts = param.Number(default=0.02,bounds=(0.0,None),softbounds=(0.0,0.5),
precedence=0.60,doc="Thickness (line width) of the spiral.")
turning = param.Number(default=0.05,bounds=(0.01,None),softbounds=(0.01,2.0),
precedence=0.62,doc="Density of turnings; turning*angle gives the actual radius.")
def function(self,p):
aspect_ratio = p.aspect_ratio
x = self.pattern_x/aspect_ratio
y = self.pattern_y
turning = p.turning
spacing = turning*2*np.pi
thickness = float(spacing)/p.outer_parts/2
distance_from_origin = np.sqrt(x**2+y**2)
distance_from_spiral_middle = np.fmod(spacing + distance_from_origin - turning*np.arctan2(y,x),spacing)
distance_from_spiral_middle = np.minimum(distance_from_spiral_middle,spacing - distance_from_spiral_middle)
get_index = distance_from_spiral_middle > thickness
my_spiral = (np.cos(distance_from_spiral_middle/thickness*np.pi)+1)/2
my_spiral[get_index] = 0
return my_spiral
class SpiralGrating(Composite):
parts = param.Integer(default=2,bounds=(1,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Number of parts in the grating.")
turning = param.Number(default=0.05,bounds=(0.01,None),softbounds=(0.01,2.0),
precedence=0.62,doc="Density of turnings; turning*angle gives the actual radius.")
def function(self, p):
o=2*np.pi/p.parts
gens = [Spiral(turning=p.turning,outer_parts=p.parts,
orientation=i*2*np.pi/p.parts) for i in range(p.parts)]
return Composite(generators=gens, bounds=p.bounds, orientation=p.orientation,
xdensity=p.xdensity, ydensity=p.ydensity)()
def SpiralG(x, y, BlackBackground, phase_shift, Bound, frames):
parts = [8]
C_target = [2]
paras = [[[0, part/(C_t*2*np.pi), part]] for part in parts for C_t in C_target]
if phase_shift:
paras = pert.modulation_of_phase(paras,'SpG',frames)
else:
frames = 1
SpG = np.empty([len(paras),frames],dtype=np.object)
idx_proto = 0
for pars in paras:
idx_shift = 0
for p in pars:
SpG[idx_proto][idx_shift] = SpiralGrating(bounds=Bound,parts=p[2],turning=p[1],
orientation=p[0],xdensity=x,ydensity=y)()
if BlackBackground:
assert SpG[idx_proto][idx_shift].max() <= 1.0 and SpG[idx_proto][idx_shift].min() >= 0
SpG[idx_proto][idx_shift] = 1-SpG[idx_proto][idx_shift]
idx_shift += 1
idx_proto += 1
return SpG
# ##################### RadialGrating
class Wedge(PatternGenerator):
aspect_ratio = param.Number(default=1.0,bounds=(0.0,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Ratio of width to height.")
outer_parts = param.Integer(default=4,bounds=(1,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Number of parts in the grating.")
def function(self,p):
aspect_ratio = p.aspect_ratio
x = self.pattern_x/aspect_ratio
y = self.pattern_y
thickness = np.pi/p.outer_parts
angle = np.absolute(np.arctan2(y,x))
my_wedge = (np.cos(np.pi*angle/thickness)+1)/2
get_index = angle > thickness
my_wedge[get_index] = 0
return my_wedge
class RadialGrating(Composite):
parts = param.Integer(default=4,bounds=(1,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Number of parts in the grating.")
def function(self, p):
gens = [Wedge(outer_parts=p.parts,orientation=i*2*np.pi/p.parts) for i in range(p.parts)]
return Composite(generators=gens, bounds=p.bounds, orientation=p.orientation,
xdensity=p.xdensity, ydensity=p.ydensity)()
def RadialG(x, y, BlackBackground, phase_shift, Bound,frames):
parts = [8,24]
orien = len(parts)*[np.pi/2]
paras = zip(orien,parts)
paras = [[par] for par in paras]
if phase_shift:
paras = pert.modulation_of_phase(paras,'RG',frames)
else:
frames = 1
RG = np.empty([len(paras),frames],dtype=np.object)
idx_proto = 0
for pars in paras:
idx_shift = 0
for p in pars:
RG[idx_proto][idx_shift] = ig.RadialGrating(bounds=Bound,parts=p[1],
orientation=p[0],xdensity=x,ydensity=y)()
if BlackBackground:
assert RG[idx_proto][idx_shift].max() <= 1.0 and RG[idx_proto][idx_shift].min() >= 0
RG[idx_proto][idx_shift] = 1-RG[idx_proto][idx_shift]
idx_shift += 1
idx_proto += 1
return RG
class ConcentricRings(PatternGenerator):
"""
Concentric rings with linearly increasing radius.
Gaussian fall-off at the edges.
"""
aspect_ratio = param.Number(default=1.0,bounds=(0.0,None),softbounds=(0.0,2.0),
precedence=0.31,doc="Ratio of width to height.")
size = param.Number(default=0.4,bounds=(0.01,None),softbounds=(0.1,2.0),
precedence=0.62,doc="Radius difference of neighbouring rings.")
phase = param.Number(default=0.0,bounds=(0.0,None),softbounds=(0.0,2.0*np.pi),
precedence=0.51,doc="Phase shift from the centre.")
def function(self,p):
aspect_ratio = p.aspect_ratio
x = self.pattern_x/aspect_ratio
y = self.pattern_y
size = p.size
phase = p.phase
distance_from_origin = np.sqrt(x**2+y**2)
distance_from_ring_middle = np.fmod(distance_from_origin + phase*size/(2*np.pi),size)
distance_from_ring_middle = np.minimum(distance_from_ring_middle,size - distance_from_ring_middle)
return (np.cos(distance_from_ring_middle/size*2*np.pi)+1)/2
# #################### Targets/ ConcentricRings
def Targets(x, y, BlackBackground, phase_shift, Bound,frames):
sizes = [1.0/s for s in [2]]
paras = [[[0, s, 1, 0.0]] for s in sizes]
if phase_shift:
paras = pert.modulation_of_phase(paras,'T',frames)
else:
frames = 1
T = np.empty([len(paras),frames], dtype=np.object)
idx_proto = 0
for pars in paras:
idx_shift = 0
for p in pars:
T[idx_proto][idx_shift] = ConcentricRings(bounds=Bound, aspect_ratio=p[2],
size=p[1], orientation=p[0], phase=p[3], xdensity=x, ydensity=y)()
if BlackBackground:
assert T[idx_proto][idx_shift].max() <= 1.0 and T[idx_proto][idx_shift].min() >= 0
T[idx_proto][idx_shift] = 1 - T[idx_proto][idx_shift]
idx_shift += 1
idx_proto += 1
return T
if __name__ == '__main__':
# image size
x = 24
y = 24
Bound = BoundingBox(radius= 20.4)
BlackBackground = True # convert image to black background
phase_shift = False # generate different phases
############ waves
SG = SineG(x, y, BlackBackground, phase_shift, Bound, frames=24)
SpG = SpiralG(x, y, BlackBackground, phase_shift, Bound, frames=24)
T = Targets(x, y, BlackBackground, phase_shift, Bound, frames=24)
HG = HyperG(x, y, BlackBackground, phase_shift, Bound, frames=24)
RG = RadialG(x, y, BlackBackground, phase_shift, Bound, frames=24)
# ############## save images to .mat file
if phase_shift:
if BlackBackground:
sio.savemat('SG_shift_b.mat', {'imgs': SG})
sio.savemat('HG_shift_b.mat', {'imgs': HG})
sio.savemat('SpG_shift_b.mat', {'imgs': SpG})
sio.savemat('RG_shift_b.mat', {'imgs': RG})
sio.savemat('T_shift_b.mat', {'imgs': T})
else:
sio.savemat('SG_shift_w.mat', {'imgs': SG})
sio.savemat('HG_shift_w.mat', {'imgs': HG})
sio.savemat('SpG_shift_w.mat', {'imgs': SpG})
sio.savemat('RG_shift_w.mat', {'imgs': RG})
sio.savemat('T_shift_w.mat', {'imgs': T})
else:
if BlackBackground:
sio.savemat('SG_b.mat', {'imgs': SG})
sio.savemat('HG_b.mat', {'imgs': HG})
sio.savemat('SpG_b.mat', {'imgs': SpG})
sio.savemat('RG_b.mat', {'imgs': RG})
sio.savemat('T_b.mat', {'imgs': T})
else:
sio.savemat('SG_w.mat', {'imgs': SG})
sio.savemat('HG_w.mat', {'imgs': HG})
sio.savemat('SpG_w.mat', {'imgs': SpG})
sio.savemat('RG_w.mat', {'imgs': RG})
sio.savemat('T_w.mat', {'imgs': T})