braincoder 0.7.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,656 @@
1
+ import pandas as pd
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tqdm.auto import tqdm
5
+ from ..utils import format_data, format_weights
6
+ from ..stimuli import ImageStimulus
7
+ import logging
8
+ from tensorflow_probability import bijectors as tfb
9
+ from ..utils.mcmc import sample_hmc, cleanup_chain, Periodic
10
+
11
+
12
+ class StimulusFitter(object):
13
+
14
+ def __init__(self, data, model, omega, parameters=None, weights=None, dof=None, stimulus=None, mask=None):
15
+
16
+ self.data = format_data(data)
17
+ self.model = model
18
+ self.omega = omega
19
+ self.dof = dof
20
+ self.parameters = model._get_parameters(parameters)
21
+ self.weights = format_weights(weights)
22
+
23
+ if stimulus is None:
24
+ self.stimulus = self.model.stimulus
25
+
26
+ self.single_bijector = not isinstance(self.stimulus.bijectors, list)
27
+
28
+ def fit(self, init_pars=None, fit_vars=None, max_n_iterations=1000, min_n_iterations=100, lag=100, rtol=1e-6, learning_rate=0.01, progressbar=True,
29
+ l2_norm=None,
30
+ l1_norm=None,
31
+ mask=None,
32
+ legacy_adam=False):
33
+
34
+ if progressbar:
35
+ pbar = tqdm(range(max_n_iterations))
36
+ else:
37
+ pbar = range(max_n_iterations)
38
+
39
+ self.costs = np.ones(max_n_iterations) * 1e12
40
+
41
+ if isinstance(init_pars, pd.DataFrame):
42
+ init_pars = init_pars.values
43
+
44
+ if init_pars is None:
45
+ init_pars = self.stimulus.generate_empty_stimulus(len(self.data))
46
+ elif init_pars.shape[0] != len(self.data):
47
+ raise ValueError(
48
+ 'init_pars should have the same number of rows as data')
49
+
50
+ if mask is not None:
51
+
52
+ if mask.ndim == 1:
53
+ assert(len(mask) == init_pars.shape[1]), 'Mask should have the same length as the number of stimulus dimensions'
54
+ mask = np.tile(mask, (len(self.data), 1))
55
+ elif mask.ndim == 2:
56
+ mask = mask.reshape(-1)
57
+ assert(len(mask) == init_pars.shape), 'Mask should have the same shape as number of stimulus dimensions'
58
+ mask = np.tile(mask.reshape(-1), (len(self.data), 1))
59
+ elif mask.ndim == 3:
60
+ assert(len(mask) == self.data.shape[0]), 'Mask should have the same length as the number of datapoints'
61
+ mask = mask.reshape(len(self.data), -1)
62
+ assert(mask.shape[1] == init_pars.shape[1]), 'Mask should have the same shape as number of stimulus dimensions'
63
+
64
+ mask_ix = np.array(np.where(mask)).T
65
+ init_pars = init_pars[mask]
66
+
67
+ else:
68
+ init_pars = np.asarray(init_pars)
69
+
70
+ model_vars = []
71
+ trainable_vars = []
72
+
73
+ if self.single_bijector:
74
+ # Code to handle when self.stimulus.bijectors is not iterable
75
+ bijector = self.stimulus.bijectors
76
+ tf_var = tf.Variable(name='stimulus',
77
+ shape=init_pars.shape,
78
+ initial_value=bijector.inverse(init_pars))
79
+
80
+ model_vars.append(tf_var)
81
+ trainable_vars.append(tf_var)
82
+
83
+ else:
84
+ if mask is not None:
85
+ raise NotImplementedError('Masking not implemented for multiple bijectors')
86
+
87
+ if fit_vars is None:
88
+ fit_vars = self.stimulus.dimension_labels
89
+
90
+ for label, bijector, values in zip(self.stimulus.dimension_labels, self.stimulus.bijectors, init_pars.T):
91
+ assert(np.all(~np.isnan(bijector.inverse(values)))
92
+ ), f'Problem with init values of {label} (nans):\n\r{bijector.inverse(values)}'
93
+ assert(np.all(np.isfinite(bijector.inverse(values)))
94
+ ), f'Problem with init values of {label} (infinite values):\nr{bijector}\nr{values}\n\r{bijector.inverse(values)}'
95
+
96
+ tf_var = tf.Variable(name=label,
97
+ shape=values.shape,
98
+ initial_value=bijector.inverse(values))
99
+
100
+ model_vars.append(tf_var)
101
+
102
+ if label in fit_vars:
103
+ trainable_vars.append(tf_var)
104
+
105
+ if mask is None:
106
+ likelihood = self.build_likelihood()
107
+ else:
108
+ likelihood = self.build_likelihood(use_mask=True, mask_ix=mask_ix)
109
+
110
+ pbar = tqdm(range(max_n_iterations))
111
+ self.costs = np.ones(max_n_iterations) * 1e12
112
+
113
+ if legacy_adam:
114
+ opt = tf.keras.optimizers.legacy.Adam(learning_rate=learning_rate)
115
+ else:
116
+ opt = tf.optimizers.Adam(learning_rate=learning_rate)
117
+
118
+ for step in pbar:
119
+ with tf.GradientTape() as tape:
120
+ if self.single_bijector:
121
+ # n_pars x datapoints
122
+ untransformed_pars = tf.transpose(self.stimulus.bijectors.forward(model_vars[0]))
123
+ else:
124
+ untransformed_pars = [bijector.forward(
125
+ par) for bijector, par in zip(self.stimulus.bijectors, model_vars)]
126
+
127
+ if mask is None:
128
+ ll = likelihood(*untransformed_pars)[0]
129
+ else:
130
+ ll = likelihood(untransformed_pars)[0]
131
+
132
+ cost = -ll
133
+ if l1_norm is not None:
134
+ cost = cost + l1_norm * tf.reduce_sum([tf.reduce_sum(tf.abs(par)) for par in untransformed_pars])
135
+ if l2_norm is not None:
136
+ cost += l2_norm * tf.reduce_sum([tf.reduce_sum(par**2) for par in untransformed_pars])
137
+
138
+ gradients = tape.gradient(cost, trainable_vars)
139
+ opt.apply_gradients(zip(gradients, trainable_vars))
140
+
141
+ if progressbar:
142
+ pbar.set_description(f'LL: {ll:6.4f}')
143
+
144
+ self.costs[step] = cost.numpy()
145
+
146
+ previous_cost = self.costs[np.max((step - lag, 0))]
147
+ if step > min_n_iterations:
148
+ if np.sign(previous_cost) == np.sign(cost):
149
+ if np.sign(cost) == 1:
150
+ if (cost / previous_cost) > 1 - rtol:
151
+ break
152
+ else:
153
+ if (cost / previous_cost) < 1 - rtol:
154
+ break
155
+
156
+ if mask is None:
157
+ fitted_pars_ = np.stack(
158
+ [par.numpy() for par in untransformed_pars], axis=1)
159
+ else:
160
+ fitted_pars_ = self.stimulus.generate_empty_stimulus(len(self.data))
161
+ fitted_pars_[mask] = untransformed_pars.numpy()
162
+
163
+
164
+ fitted_pars = pd.DataFrame(fitted_pars_, columns=self.stimulus.dimension_labels,
165
+ index=self.data.index,
166
+ dtype=np.float32)
167
+ return fitted_pars
168
+
169
+ def build_likelihood(self, use_batch_dimension=False, use_mask=False, mask_ix=None):
170
+
171
+ data = self.data.values[tf.newaxis, ...]
172
+ parameters = self.parameters.values[tf.newaxis, ...]
173
+ weights = None if self.weights is None else self.weights.values[tf.newaxis, ...]
174
+ from ..utils.backend import safe_cholesky
175
+ omega_chol = np.array(safe_cholesky(tf.convert_to_tensor(self.omega, dtype=tf.float32)))
176
+
177
+ if use_mask:
178
+ if use_batch_dimension:
179
+ return NotImplementedError('Masking not implemented for use_batch_dimension=True')
180
+ else:
181
+
182
+ assert(isinstance(self.stimulus, ImageStimulus)), 'Masking only implemented for ImageStimulus'
183
+
184
+ empty_stimulus = self.stimulus.generate_empty_stimulus(len(self.data))
185
+
186
+ @tf.function
187
+ def likelihood(pars):
188
+
189
+ pars = tf.tensor_scatter_nd_add(empty_stimulus, mask_ix, pars)
190
+
191
+ stimulus = self.stimulus._generate_stimulus(pars)[tf.newaxis, ...]
192
+ ll = self.model._likelihood(
193
+ stimulus, data, parameters, weights, omega_chol, dof=self.dof, logp=True)
194
+
195
+ return tf.reduce_sum(ll, 1)
196
+
197
+ else:
198
+ if use_batch_dimension:
199
+ @tf.function
200
+ def likelihood(*pars):
201
+
202
+ pars = tf.stack(pars, axis=1)
203
+ stimulus = self.stimulus._generate_stimulus(pars)[:, tf.newaxis, ...]
204
+ ll = self.model._likelihood(
205
+ stimulus, data, parameters, weights, omega_chol, dof=self.dof, logp=True)
206
+
207
+ return ll
208
+
209
+ else:
210
+ @tf.function
211
+ def likelihood(*pars):
212
+
213
+ pars = tf.stack(pars, axis=1)
214
+ stimulus = self.stimulus._generate_stimulus(pars)[tf.newaxis, ...]
215
+ ll = self.model._likelihood(
216
+ stimulus, data, parameters, weights, omega_chol, dof=self.dof, logp=True)
217
+
218
+ return tf.reduce_sum(ll, 1)
219
+
220
+ return likelihood
221
+
222
+
223
+ def fit_grid(self, *pars):
224
+
225
+ assert(len(pars) == len(self.stimulus.dimension_labels)), 'Need to provide values for all stimulus dimensions: {self.stimulus.dimension_labels}'
226
+
227
+ pars = [np.asarray(par).astype(np.float32) for par in pars]
228
+
229
+ parameters = dict(zip(self.stimulus.dimension_labels, pars))
230
+
231
+ # grid length x n_pars
232
+ grid = pd.MultiIndex.from_product(
233
+ parameters.values(), names=parameters.keys()).to_frame(index=False).astype(np.float32)
234
+
235
+ logging.info(f'Built grid of {len(grid)} possible stimuli...')
236
+
237
+ likelihood = self.build_likelihood(use_batch_dimension=True)
238
+
239
+ stimulus = self.stimulus._generate_stimulus(grid.values)
240
+
241
+ ll = likelihood(*grid.values.T)
242
+ ll = pd.DataFrame(likelihood(*grid.values.T).numpy().T, columns=pd.MultiIndex.from_frame(
243
+ grid), index=self.data.index)
244
+
245
+ best_pars = ll.columns.to_frame().iloc[ll.values.argmax(1)]
246
+ best_pars.index = self.data.index
247
+
248
+ return best_pars
249
+
250
+ class CustomStimulusFitter(StimulusFitter):
251
+
252
+ def __init__(self, data, model, stimulus,
253
+ omega, dof=None,
254
+ baseline_image=None, parameters=None, weights=None):
255
+
256
+ self.data = data
257
+ self.model = model
258
+ self.stimulus = stimulus
259
+ self.omega = omega
260
+ self.omega_chol = tf.linalg.cholesky(omega).numpy()
261
+ self.dof = dof
262
+ self.parameters = model._get_parameters(parameters)
263
+ self.weights = format_weights(weights)
264
+
265
+ if baseline_image is None:
266
+ self.baseline_image = None
267
+ else:
268
+ self.baseline_image = baseline_image.reshape(
269
+ len(self.grid_coordinates))
270
+
271
+ # Parameters is dictionary
272
+ def fit_grid(self, parameters):
273
+
274
+ assert(set(parameters.keys()) == set(self.stimulus.parameter_labels)
275
+ ), f"Provide a dictionary with the following keys: {self.stimulus.parameter_labels}"
276
+
277
+ grid = pd.MultiIndex.from_product(
278
+ parameters.values(), names=parameters.keys()).to_frame(index=False).astype(np.float32)
279
+
280
+ grid = grid[self.stimulus.parameter_labels]
281
+
282
+ logging.info(f'Built grid of {len(grid)} bar settings...')
283
+
284
+ images = self.stimulus.generate_images(grid, return_df=False)[0]
285
+
286
+ return self._fit_grid(grid, images)
287
+
288
+ def _fit_grid(self, grid, images):
289
+
290
+ data = self.data.values
291
+ model = self.model
292
+ parameters = self.parameters.values[np.newaxis, ...]
293
+ weights = None if self.weights is None else self.weights.values[np.newaxis, ...]
294
+
295
+ if hasattr(self.model, 'hrf_model'):
296
+
297
+ images = tf.concat((tf.zeros((images.shape[0],
298
+ 1,
299
+ images.shape[1])),
300
+ images[:, tf.newaxis, :],
301
+ tf.zeros((images.shape[0],
302
+ len(self.model.hrf_model.hrf)-1,
303
+ images.shape[1]))),
304
+ 1)
305
+
306
+ hrf_shift = np.argmax(model.hrf_model.hrf)
307
+
308
+ ts_prediction = model._predict(images, parameters, weights)
309
+
310
+ baseline = ts_prediction[:, 0, :]
311
+
312
+ ts_prediction_summed_over_hrf = tf.reduce_sum(
313
+ ts_prediction - baseline[:, tf.newaxis, :], 1) / tf.reduce_sum(model.hrf_model.hrf) + baseline
314
+
315
+ ll = self.model._likelihood_timeseries(data[tf.newaxis, ...],
316
+ ts_prediction_summed_over_hrf[:,
317
+ tf.newaxis, :],
318
+ self.omega_chol,
319
+ self.dof,
320
+ logp=True,
321
+ normalize=False)
322
+
323
+ ll = tf.concat((tf.roll(ll, -hrf_shift, 1)
324
+ [:, :-hrf_shift], tf.ones((len(images), hrf_shift)) * -1e12), 1)
325
+
326
+ else:
327
+ images = images[:, tf.newaxis, :]
328
+
329
+ ll = self.model._likelihood(images, data[tf.newaxis, ...], parameters, weights,
330
+ self.omega_chol, self.dof, logp=True)
331
+
332
+ ll = pd.DataFrame(ll.numpy().T, columns=pd.MultiIndex.from_frame(grid))
333
+
334
+ best_pars = ll.columns.to_frame().iloc[ll.values.argmax(1)]
335
+ best_pars.index = self.data.index
336
+
337
+ return best_pars
338
+
339
+ def fit(self, init_pars, learning_rate=0.01, max_n_iterations=500, min_n_iterations=100, lag=100,
340
+ fit_vars=None,
341
+ relevant_frames=None, rtol=1e-7, parameterization='xy', include_xy=True):
342
+
343
+ model_vars = []
344
+ trainable_vars = []
345
+
346
+ if fit_vars is None:
347
+ fit_vars = self.stimulus.parameter_labels
348
+
349
+ if hasattr(init_pars, 'values'):
350
+ init_pars = init_pars[self.stimulus.parameter_labels].values
351
+
352
+ if (relevant_frames is not None) and (len(init_pars) > len(relevant_frames)):
353
+ init_pars = init_pars[relevant_frames, :]
354
+
355
+ for label, bijector, values in zip(self.stimulus.parameter_labels, self.stimulus.bijectors, init_pars.T):
356
+ assert(np.all(~np.isnan(bijector.inverse(values)))
357
+ ), f'Problem with init values of {label} (nans)'
358
+ assert(np.all(np.isfinite(bijector.inverse(values)))
359
+ ), f'Problem with init values of {label} (infinte values)'
360
+
361
+ tf_var = tf.Variable(name=label,
362
+ shape=values.shape,
363
+ initial_value=bijector.inverse(values))
364
+
365
+ model_vars.append(tf_var)
366
+
367
+ if label in fit_vars:
368
+ trainable_vars.append(tf_var)
369
+
370
+ likelihood = self.build_likelihood_function(relevant_frames)
371
+
372
+ pbar = tqdm(range(max_n_iterations))
373
+ self.costs = np.ones(max_n_iterations) * 1e12
374
+
375
+ opt = tf.optimizers.Adam(learning_rate=learning_rate)
376
+
377
+ for step in pbar:
378
+ with tf.GradientTape() as tape:
379
+
380
+ untransformed_pars = [bijector.forward(
381
+ par) for bijector, par in zip(self.stimulus.bijectors, model_vars)]
382
+ ll = likelihood(*untransformed_pars)[0]
383
+ cost = -ll
384
+
385
+ gradients = tape.gradient(cost,
386
+ trainable_vars)
387
+
388
+ opt.apply_gradients(zip(gradients, trainable_vars))
389
+
390
+ pbar.set_description(f'LL: {ll:6.4f}')
391
+
392
+ self.costs[step] = cost.numpy()
393
+
394
+ previous_cost = self.costs[np.max((step - lag, 0))]
395
+ if step > min_n_iterations:
396
+ if np.sign(previous_cost) == np.sign(cost):
397
+ if np.sign(cost) == 1:
398
+ if (cost / previous_cost) > 1 - rtol:
399
+ break
400
+ else:
401
+ if (cost / previous_cost) < 1 - rtol:
402
+ break
403
+
404
+ fitted_pars_ = np.concatenate(
405
+ [par.numpy()[:, np.newaxis] for par in untransformed_pars], axis=1)
406
+
407
+ if relevant_frames is None:
408
+ fitted_pars = pd.DataFrame(fitted_pars_, columns=self.stimulus.parameter_labels,
409
+ index=self.data.index,
410
+ dtype=np.float32)
411
+ else:
412
+
413
+ fitted_pars = pd.DataFrame(np.nan * np.zeros((self.data.shape[0], len(model_vars))), columns=self.stimulus.parameter_labels,
414
+ index=self.data.index,
415
+ dtype=np.float32)
416
+ fitted_pars.iloc[relevant_frames, :] = fitted_pars_
417
+
418
+ print(fitted_pars)
419
+
420
+ return fitted_pars
421
+
422
+ def build_likelihood_function(self, relevant_frames=None, falloff_speed=1000., n_batches=1):
423
+
424
+ data = self.data.values[tf.newaxis, ...]
425
+ grid_coordinates = self.stimulus.grid_coordinates.values
426
+ model = self.model
427
+ parameters = self.parameters.values[tf.newaxis, ...]
428
+ weights = None if self.weights is None else self.weights.values[tf.newaxis, ...]
429
+
430
+ if self.baseline_image is None:
431
+ @tf.function
432
+ def add_baseline(images):
433
+ return images
434
+ else:
435
+ print('Including base image (e.g., fixation image) into estimation')
436
+
437
+ @tf.function
438
+ def add_baseline(images):
439
+ images = images + self.baseline_image[tf.newaxis, :]
440
+ images = tf.clip_by_value(images, 0, self.max_intensity)
441
+ return bars
442
+
443
+ if relevant_frames is None:
444
+
445
+ @tf.function
446
+ def likelihood(*pars):
447
+ images = self.stimulus._generate_images(*pars)
448
+ images = add_baseline(images)
449
+ ll = self.model._likelihood(
450
+ images, data, parameters, weights, self.omega_chol, dof=self.dof, logp=True)
451
+
452
+ return tf.reduce_sum(ll, 1)
453
+
454
+ else:
455
+
456
+ relevant_frames = tf.constant(relevant_frames, tf.int32)
457
+
458
+ size_ = (n_batches, data.shape[1], len(grid_coordinates))
459
+ size_ = tf.constant(size_, dtype=tf.int32)
460
+
461
+ time_ix, batch_ix = np.meshgrid(relevant_frames, range(n_batches))
462
+ indices = np.zeros(
463
+ (n_batches, len(relevant_frames), 2), dtype=np.int32)
464
+ indices[..., 0] = batch_ix
465
+ indices[..., 1] = time_ix
466
+
467
+ @tf.function
468
+ def likelihood(*pars):
469
+
470
+ images = self.stimulus._generate_images(*pars)
471
+
472
+ images = tf.scatter_nd(indices,
473
+ images,
474
+ size_)
475
+
476
+ images = add_baseline(images)
477
+
478
+ ll = self.model._likelihood(
479
+ images, data, parameters, weights, self.omega_chol, dof=self.dof, logp=True)
480
+
481
+ return tf.reduce_sum(ll, 1)
482
+
483
+ return likelihood
484
+
485
+
486
+ def sample_posterior(self,
487
+ init_pars,
488
+ n_chains,
489
+ relevant_frames=None,
490
+ step_size=0.0001,
491
+ n_burnin=10,
492
+ n_samples=10,
493
+ max_tree_depth=10,
494
+ unrolled_leapfrog_steps=1,
495
+ falloff_speed=1000.,
496
+ target_accept_prob=0.85,
497
+ parameterization='xy'):
498
+
499
+ init_pars = init_pars.astype(np.float32)
500
+
501
+
502
+ if (relevant_frames is not None) and (len(init_pars) > len(relevant_frames)):
503
+ init_pars = init_pars.iloc[relevant_frames, :]
504
+
505
+ initial_state = list(np.repeat(init_pars.values.T[:, np.newaxis, :], n_chains, 1))
506
+
507
+ bijectors = self.stimulus.bijectors
508
+
509
+
510
+ likelihood = self.build_likelihood_function(relevant_frames, falloff_speed=falloff_speed,
511
+ n_batches=n_chains)
512
+
513
+
514
+
515
+ step_size = [tf.fill([n_chains] + [1] * (len(s.shape) - 1),
516
+ tf.constant(step_size, np.float32)) for s in initial_state]
517
+
518
+ samples, stats = sample_hmc(
519
+ initial_state, step_size, likelihood,
520
+ bijectors,
521
+ num_steps=n_samples, burnin=n_burnin,
522
+ target_accept_prob=target_accept_prob, unrolled_leapfrog_steps=unrolled_leapfrog_steps,
523
+ max_tree_depth=max_tree_depth)
524
+
525
+ if relevant_frames is None:
526
+ frame_index = self.data.index
527
+ else:
528
+ frame_index = self.data.index[relevant_frames]
529
+
530
+ cleaned_up_chains = [cleanup_chain(chain.numpy(), init_pars.columns[ix], frame_index) for ix, chain in enumerate(samples)]
531
+
532
+ samples = pd.concat(cleaned_up_chains, 1)
533
+
534
+ return samples, stats
535
+
536
+
537
+ class SzinteStimulus(object):
538
+
539
+ parameter_labels = ['x', 'width', 'height']
540
+
541
+ def __init__(self, grid_coordinates, max_width=None, max_height=None, intensity=1.0):
542
+
543
+ self.grid_coordinates = pd.DataFrame(
544
+ grid_coordinates, columns=['x', 'y'])
545
+
546
+
547
+ self.image_size = len(self.grid_coordinates['x'].unique()), len(self.grid_coordinates['y'].unique())
548
+
549
+ self.intensity = intensity
550
+
551
+ self.min_x, self.max_x = self.grid_coordinates['x'].min(
552
+ ), self.grid_coordinates['x'].max()
553
+
554
+ if max_width is None:
555
+ max_width = self.max_x - self.min_x
556
+
557
+ if max_height is None:
558
+ max_height = self.grid_coordinates['y'].max(
559
+ ) - self.grid_coordinates['y'].min()
560
+
561
+ self.max_width = max_width
562
+ self.max_height = max_height
563
+
564
+ self.bijectors = [Periodic(low=self.min_x - self.max_width/2.,
565
+ high=self.max_x + self.max_width/2.), # x
566
+ tfb.Sigmoid(low=np.float32(0.0),
567
+ high=self.max_width), # width
568
+ tfb.Sigmoid(low=np.float32(0.0), high=self.max_height)] # height
569
+
570
+ def generate_images(self, parameters, falloff_speed=1000, return_3dimage=True):
571
+
572
+ if not hasattr(parameters, 'values'):
573
+ parameters = pd.DataFrame(parameters, columns=['x', 'width', 'height'])
574
+
575
+ ims = self._generate_images(parameters['x'].values,
576
+ parameters['width'].values,
577
+ parameters['height'].values,
578
+ falloff_speed)
579
+
580
+ if return_3dimage:
581
+ return pd.DataFrame(ims.numpy().reshape(ims.shape[1], width*height),
582
+ index=parameters.index,
583
+ columns=pd.MultiIndex.from_array(self.grid_coordinates,
584
+ names=['x', 'y']))
585
+
586
+ return ims
587
+
588
+ def _generate_images(self, x, width, height, falloff_speed=1000):
589
+ return make_aperture_stimuli(self.grid_coordinates.values,
590
+ x, width, height, falloff_speed, self.intensity)
591
+
592
+ class SzinteStimulus2(object):
593
+
594
+ parameter_labels = ['x', 'height']
595
+
596
+ def __init__(self, grid_coordinates, bar_width=1.4111355368411007,
597
+ max_height=None, intensity=1.0):
598
+
599
+ self.grid_coordinates = pd.DataFrame(
600
+ grid_coordinates, columns=['x', 'y'])
601
+ self.image_size = len(self.grid_coordinates['x'].unique()), len(self.grid_coordinates['y'].unique())
602
+
603
+ self.intensity = intensity
604
+
605
+ self.min_x, self.max_x = self.grid_coordinates['x'].min(
606
+ ), self.grid_coordinates['x'].max()
607
+
608
+
609
+ if max_height is None:
610
+ max_height = self.grid_coordinates['y'].max(
611
+ ) - self.grid_coordinates['y'].min()
612
+
613
+ self.bar_width = np.array([bar_width])
614
+ self.max_height = max_height
615
+
616
+ self.bijectors = [Periodic(low=self.min_x - self.bar_width/2.,
617
+ high=self.max_x + self.bar_width/2.), # x
618
+ tfb.Sigmoid(low=np.float32(0.0), high=self.max_height)] # height
619
+
620
+ def generate_images(self, parameters, falloff_speed=1000, return_df=True):
621
+
622
+ if not hasattr(parameters, 'values'):
623
+ parameters = pd.DataFrame(parameters, columns=['x', 'height'])
624
+
625
+ ims = self._generate_images(parameters['x'].values,
626
+ parameters['height'].values,
627
+ falloff_speed)
628
+
629
+ if return_df:
630
+ return pd.DataFrame(ims.numpy().reshape(ims.shape[1], -1),
631
+ index=parameters.index,
632
+ columns=pd.MultiIndex.from_frame(self.grid_coordinates,
633
+ names=['x', 'y']))
634
+ return ims
635
+
636
+ def _generate_images(self, x, height, falloff_speed=1000):
637
+ return make_aperture_stimuli(self.grid_coordinates.values,
638
+ x, self.bar_width, height, falloff_speed, self.intensity)
639
+
640
+ def make_aperture_stimuli(grid_coordinates, x, width, height, falloff_speed=1000., intensity=1.0):
641
+
642
+ x_ = grid_coordinates[:, 0]
643
+ y_ = grid_coordinates[:, 1]
644
+
645
+ x_distance = tf.abs(x[..., tf.newaxis] - x_[tf.newaxis, tf.newaxis, ...])
646
+ y_distance = tf.abs(y_[tf.newaxis, tf.newaxis, ...])
647
+
648
+ bar_x = tf.math.sigmoid(
649
+ (-x_distance + width[..., tf.newaxis] / 2) *
650
+ falloff_speed)
651
+
652
+ bar_y = tf.math.sigmoid(
653
+ (-y_distance + height[..., tf.newaxis] / 2) *
654
+ falloff_speed)
655
+
656
+ return bar_x*bar_y*intensity