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.
- braincoder/__init__.py +27 -0
- braincoder/barstimuli.py +569 -0
- braincoder/data/szinte2024/dm_gazecenter.npz +0 -0
- braincoder/data/szinte2024/gauss_parameters.tsv +2366 -0
- braincoder/data/szinte2024/grid_coordinates.tsv +3601 -0
- braincoder/data/szinte2024/mask-v1_bold.tsv.gz +0 -0
- braincoder/hrf.py +272 -0
- braincoder/models/__init__.py +21 -0
- braincoder/models/base.py +1004 -0
- braincoder/models/linear.py +100 -0
- braincoder/models/prf_1d.py +369 -0
- braincoder/models/prf_2d.py +824 -0
- braincoder/noise.py +614 -0
- braincoder/optimize/__init__.py +22 -0
- braincoder/optimize/bayesian_fitter.py +467 -0
- braincoder/optimize/gp_prior.py +361 -0
- braincoder/optimize/parameter_fitter.py +666 -0
- braincoder/optimize/residual_fitter.py +563 -0
- braincoder/optimize/stimulus_fitter.py +656 -0
- braincoder/optimize/weight_fitter.py +112 -0
- braincoder/stimuli.py +224 -0
- braincoder/utils/__init__.py +4 -0
- braincoder/utils/backend.py +533 -0
- braincoder/utils/calibration.py +186 -0
- braincoder/utils/cortex.py +141 -0
- braincoder/utils/data.py +252 -0
- braincoder/utils/formatting.py +63 -0
- braincoder/utils/math.py +131 -0
- braincoder/utils/mcmc.py +114 -0
- braincoder/utils/stats.py +267 -0
- braincoder/utils/visualize.py +26 -0
- braincoder-0.7.1.dist-info/METADATA +136 -0
- braincoder-0.7.1.dist-info/RECORD +36 -0
- braincoder-0.7.1.dist-info/WHEEL +5 -0
- braincoder-0.7.1.dist-info/licenses/LICENSE +22 -0
- braincoder-0.7.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import numpy as np
|
|
4
|
+
from tqdm import tqdm
|
|
5
|
+
import keras
|
|
6
|
+
from keras import ops
|
|
7
|
+
from ..utils import norm, format_data, format_paradigm, format_parameters, format_weights, logit, restrict_radians, lognormalpdf_n, von_mises_pdf, lognormal_pdf_mode_fwhm, norm2d
|
|
8
|
+
from ..utils.math import aggressive_softplus, aggressive_softplus_inverse, norm
|
|
9
|
+
import scipy.stats as ss
|
|
10
|
+
from ..stimuli import Stimulus, OneDimensionalRadialStimulus, OneDimensionalGaussianStimulus, OneDimensionalStimulusWithAmplitude, OneDimensionalRadialStimulusWithAmplitude, ImageStimulus, TwoDimensionalStimulus
|
|
11
|
+
from patsy import dmatrix, build_design_matrices
|
|
12
|
+
from .base import EncodingModel, HRFEncodingModel
|
|
13
|
+
|
|
14
|
+
class DiscreteModel(EncodingModel):
|
|
15
|
+
"""One response per discrete stimulus value.
|
|
16
|
+
|
|
17
|
+
``stimulus_values`` lists the values; each becomes one parameter. Use
|
|
18
|
+
:meth:`get_parameters` for the matching parameter table.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, stimulus_values, verbosity=logging.INFO, **kwargs):
|
|
22
|
+
self.stimulus_values = np.asarray(stimulus_values, dtype=np.float32)
|
|
23
|
+
self.parameter_labels = ['stim=={}'.format(p) for p in self.stimulus_values]
|
|
24
|
+
super().__init__(verbosity=verbosity, **kwargs)
|
|
25
|
+
|
|
26
|
+
def get_parameters(self):
|
|
27
|
+
parameters = np.full((len(self.stimulus_values),) * 2, np.nan, dtype=np.float32)
|
|
28
|
+
parameters[np.diag_indices_from(parameters)] = self.stimulus_values
|
|
29
|
+
return pd.DataFrame(parameters, columns=self.parameter_labels)
|
|
30
|
+
|
|
31
|
+
def _basis_predictions(self, paradigm, parameters):
|
|
32
|
+
|
|
33
|
+
parameters_ = ops.diag(parameters[0])
|
|
34
|
+
|
|
35
|
+
return ops.cast(ops.equal(paradigm, parameters_[None, :]), 'float32')
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class LinearModel(EncodingModel):
|
|
39
|
+
"""Identity mapping from paradigm features to voxel responses."""
|
|
40
|
+
|
|
41
|
+
parameter_labels = []
|
|
42
|
+
|
|
43
|
+
def predict(self, paradigm, parameters=None, weights=None):
|
|
44
|
+
|
|
45
|
+
if parameters is not None:
|
|
46
|
+
raise ValueError('LinearModel does not use any parameters!')
|
|
47
|
+
|
|
48
|
+
return super().predict(paradigm, parameters, weights)
|
|
49
|
+
|
|
50
|
+
def _basis_predictions(self, paradigm, parameters):
|
|
51
|
+
return paradigm
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class LinearModelWithBaseline(EncodingModel):
|
|
55
|
+
"""Linear encoding model that adds a voxel-specific baseline parameter."""
|
|
56
|
+
|
|
57
|
+
parameter_labels = ['baseline']
|
|
58
|
+
transformations = ['identity']
|
|
59
|
+
|
|
60
|
+
def _predict(self, paradigm, parameters, weights=None):
|
|
61
|
+
|
|
62
|
+
basis_predictions = self._basis_predictions(paradigm, None)
|
|
63
|
+
|
|
64
|
+
if weights is None:
|
|
65
|
+
return basis_predictions + parameters[..., 0]
|
|
66
|
+
else:
|
|
67
|
+
return ops.tensordot(basis_predictions, weights, axes=[[2], [1]])[:, :, 0, :] + \
|
|
68
|
+
ops.transpose(parameters, axes=[0, 2, 1])
|
|
69
|
+
|
|
70
|
+
def _basis_predictions(self, paradigm, parameters):
|
|
71
|
+
return paradigm
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class LinearModelWithBaselineHRF(LinearModelWithBaseline, HRFEncodingModel):
|
|
75
|
+
"""LinearModelWithBaseline variant that automatically applies an HRF convolution."""
|
|
76
|
+
|
|
77
|
+
def __init__(self, hrf_model=None, verbosity=logging.INFO,
|
|
78
|
+
**kwargs):
|
|
79
|
+
|
|
80
|
+
if hrf_model is None:
|
|
81
|
+
raise ValueError('Please provide HRFModel!')
|
|
82
|
+
|
|
83
|
+
LinearModelWithBaseline.__init__(self, verbosity=verbosity, **kwargs)
|
|
84
|
+
self.hrf_model = hrf_model
|
|
85
|
+
self.flexible_hrf_parameters = False
|
|
86
|
+
|
|
87
|
+
def _predict(self, paradigm, parameters, weights):
|
|
88
|
+
paradigm = ops.convert_to_tensor(paradigm, dtype='float32')
|
|
89
|
+
parameters = ops.convert_to_tensor(parameters, dtype='float32')
|
|
90
|
+
# The baseline is a BOLD offset: convolve the signal, then add it.
|
|
91
|
+
pre_convolve = LinearModelWithBaseline._predict(
|
|
92
|
+
self, paradigm, ops.zeros_like(parameters), weights)
|
|
93
|
+
|
|
94
|
+
return self.hrf_model.convolve(pre_convolve) + ops.transpose(parameters, axes=[0, 2, 1])
|
|
95
|
+
|
|
96
|
+
def _predict_grid(self, stimulus_range, parameters, weights):
|
|
97
|
+
return self._predict_no_hrf(stimulus_range, parameters, weights)
|
|
98
|
+
|
|
99
|
+
def _predict_no_hrf(self, paradigm, parameters, weights):
|
|
100
|
+
return LinearModelWithBaseline._predict(self, paradigm, parameters, weights)
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import numpy as np
|
|
4
|
+
from tqdm import tqdm
|
|
5
|
+
import keras
|
|
6
|
+
from keras import ops
|
|
7
|
+
from ..utils import norm, format_data, format_paradigm, format_parameters, format_weights, logit, restrict_radians, lognormalpdf_n, von_mises_pdf, axial_von_mises_pdf, lognormal_pdf_mode_fwhm, norm2d
|
|
8
|
+
from ..utils.math import aggressive_softplus, aggressive_softplus_inverse, norm
|
|
9
|
+
import scipy.stats as ss
|
|
10
|
+
from ..stimuli import Stimulus, OneDimensionalRadialStimulus, OneDimensionalOrientationStimulus, OneDimensionalGaussianStimulus, OneDimensionalStimulusWithAmplitude, OneDimensionalRadialStimulusWithAmplitude, ImageStimulus, TwoDimensionalStimulus
|
|
11
|
+
from patsy import dmatrix, build_design_matrices
|
|
12
|
+
from .base import EncodingModel, EncodingRegressionModel, HRFEncodingModel
|
|
13
|
+
|
|
14
|
+
class GaussianPRF(EncodingModel):
|
|
15
|
+
"""One-dimensional population receptive field with Gaussian tuning."""
|
|
16
|
+
|
|
17
|
+
parameter_labels = ['mu', 'sd', 'amplitude', 'baseline']
|
|
18
|
+
|
|
19
|
+
def __init__(self, allow_neg_amplitudes=False, verbosity=logging.INFO,
|
|
20
|
+
model_stimulus_amplitude=False,
|
|
21
|
+
**kwargs):
|
|
22
|
+
"""Configure Gaussian pRF with optional stimulus amplitude modeling."""
|
|
23
|
+
|
|
24
|
+
if not hasattr(self, 'transformations'):
|
|
25
|
+
if allow_neg_amplitudes:
|
|
26
|
+
self.transformations = ['identity', 'aggressive_softplus', 'identity', 'identity']
|
|
27
|
+
else:
|
|
28
|
+
self.transformations = ['identity', 'aggressive_softplus', 'aggressive_softplus', 'identity']
|
|
29
|
+
|
|
30
|
+
self.stimulus_type = self._get_stimulus_type(model_stimulus_amplitude=model_stimulus_amplitude)
|
|
31
|
+
self._basis_predictions = self._get_basis_predictions(model_stimulus_amplitude=model_stimulus_amplitude)
|
|
32
|
+
|
|
33
|
+
super().__init__(verbosity=logging.INFO, **kwargs)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _get_stimulus_type(self, model_stimulus_amplitude=False):
|
|
37
|
+
if model_stimulus_amplitude:
|
|
38
|
+
return OneDimensionalStimulusWithAmplitude
|
|
39
|
+
else:
|
|
40
|
+
return Stimulus
|
|
41
|
+
|
|
42
|
+
def _get_basis_predictions(self, model_stimulus_amplitude=False):
|
|
43
|
+
if model_stimulus_amplitude:
|
|
44
|
+
return self._basis_predictions_with_amplitude
|
|
45
|
+
else:
|
|
46
|
+
return self._basis_predictions_without_amplitude
|
|
47
|
+
|
|
48
|
+
def basis_predictions(self, paradigm, parameters):
|
|
49
|
+
paradigm = self.get_paradigm(paradigm)
|
|
50
|
+
parameters = self._get_parameters(parameters)
|
|
51
|
+
|
|
52
|
+
paradigm_ = ops.convert_to_tensor(self._get_paradigm(paradigm)[np.newaxis, ...], dtype='float32')
|
|
53
|
+
parameters_ = ops.convert_to_tensor(parameters.values[np.newaxis, ...], dtype='float32') if parameters is not None else None
|
|
54
|
+
|
|
55
|
+
return self._basis_predictions(paradigm_, parameters_)[0]
|
|
56
|
+
|
|
57
|
+
def get_init_pars(self, data, paradigm, confounds=None):
|
|
58
|
+
|
|
59
|
+
paradigm = self._get_paradigm(paradigm)
|
|
60
|
+
data = format_data(data)
|
|
61
|
+
|
|
62
|
+
if confounds is not None:
|
|
63
|
+
beta = ops.lstsq(confounds, data)
|
|
64
|
+
predictions = (confounds @ beta)
|
|
65
|
+
data -= predictions
|
|
66
|
+
|
|
67
|
+
if hasattr(data, 'values'):
|
|
68
|
+
data = data.values
|
|
69
|
+
|
|
70
|
+
baselines = ops.min(data, axis=0)
|
|
71
|
+
data_ = (data - baselines)
|
|
72
|
+
|
|
73
|
+
mus = ops.sum((data_ * self.stimulus._generate_stimulus(paradigm)), axis=0) / ops.sum(data_, axis=0)
|
|
74
|
+
sds = ops.sqrt(ops.sum(data_ * (self.stimulus._generate_stimulus(paradigm) - mus)
|
|
75
|
+
** 2, axis=0) / ops.sum(data_, axis=0))
|
|
76
|
+
amplitudes = ops.max(data_, axis=0)
|
|
77
|
+
|
|
78
|
+
parameters = ops.concatenate([mus[:, None],
|
|
79
|
+
sds[:, None],
|
|
80
|
+
amplitudes[:, None],
|
|
81
|
+
baselines[:, None]], axis=1)
|
|
82
|
+
|
|
83
|
+
return parameters
|
|
84
|
+
|
|
85
|
+
def _basis_predictions_without_amplitude(self, paradigm, parameters):
|
|
86
|
+
"""Gaussian tuning without stimulus amplitude modulation."""
|
|
87
|
+
return norm(paradigm[..., None, 0],
|
|
88
|
+
parameters[:, None, :, 0],
|
|
89
|
+
parameters[:, None, :, 1]) * \
|
|
90
|
+
parameters[:, None, :, 2] + parameters[:, None, :, 3]
|
|
91
|
+
|
|
92
|
+
def _basis_predictions_with_amplitude(self, paradigm, parameters):
|
|
93
|
+
"""Gaussian tuning optionally scaled by stimulus amplitude channel."""
|
|
94
|
+
return norm(paradigm[..., None, 0],
|
|
95
|
+
parameters[:, None, :, 0],
|
|
96
|
+
parameters[:, None, :, 1]) * \
|
|
97
|
+
parameters[:, None, :, 2] * paradigm[:, :, None, 1] + parameters[:, None, :, 3]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_pseudoWWT(self, stimulus_range, parameters, subtract_baseline=True):
|
|
101
|
+
stimulus_range = stimulus_range.astype(np.float32)
|
|
102
|
+
if subtract_baseline:
|
|
103
|
+
parameters = self._zero_baseline(parameters)
|
|
104
|
+
W = self.basis_predictions(stimulus_range, parameters)
|
|
105
|
+
|
|
106
|
+
pseudoWWT = ops.tensordot(W, W, axes=[[0], [0]])
|
|
107
|
+
return ops.where(ops.isnan(pseudoWWT), ops.zeros_like(pseudoWWT), pseudoWWT)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class RegressionGaussianPRF(EncodingRegressionModel, GaussianPRF):
|
|
112
|
+
"""Gaussian pRF whose parameters are modeled by regression covariates."""
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class VonMisesPRF(GaussianPRF):
|
|
116
|
+
"""Circular pRF with von Mises tuning (e.g., for polar angle stimuli)."""
|
|
117
|
+
|
|
118
|
+
parameter_labels = ['mu', 'kappa', 'amplitude', 'baseline']
|
|
119
|
+
stimulus_type = OneDimensionalRadialStimulus
|
|
120
|
+
|
|
121
|
+
def __init__(self, allow_neg_amplitudes=False,
|
|
122
|
+
model_stimulus_amplitude=False,
|
|
123
|
+
**kwargs):
|
|
124
|
+
|
|
125
|
+
super().__init__(allow_neg_amplitudes=allow_neg_amplitudes,
|
|
126
|
+
model_stimulus_amplitude=model_stimulus_amplitude,
|
|
127
|
+
**kwargs)
|
|
128
|
+
|
|
129
|
+
def _get_stimulus_type(self, model_stimulus_amplitude=False):
|
|
130
|
+
if model_stimulus_amplitude:
|
|
131
|
+
return OneDimensionalRadialStimulusWithAmplitude
|
|
132
|
+
else:
|
|
133
|
+
return OneDimensionalRadialStimulus
|
|
134
|
+
|
|
135
|
+
def _get_stimulus(self, **kwargs):
|
|
136
|
+
return self.stimulus_type()
|
|
137
|
+
|
|
138
|
+
def _basis_predictions_without_amplitude(self, paradigm, parameters):
|
|
139
|
+
"""Von Mises tuning without stimulus amplitude modulation."""
|
|
140
|
+
return von_mises_pdf(paradigm[..., None, 0],
|
|
141
|
+
parameters[:, None, :, 0],
|
|
142
|
+
parameters[:, None, :, 1]) * \
|
|
143
|
+
parameters[:, None, :, 2] + parameters[:, None, :, 3]
|
|
144
|
+
|
|
145
|
+
def _basis_predictions_with_amplitude(self, paradigm, parameters):
|
|
146
|
+
"""Von Mises tuning scaled by a stimulus amplitude channel."""
|
|
147
|
+
return von_mises_pdf(paradigm[..., None, 0],
|
|
148
|
+
parameters[:, None, :, 0],
|
|
149
|
+
parameters[:, None, :, 1]) * \
|
|
150
|
+
parameters[:, None, :, 2] * paradigm[..., None, 1] + parameters[:, None, :, 3]
|
|
151
|
+
|
|
152
|
+
def get_pseudoWWT(self, stimulus_range, parameters, subtract_baseline=True):
|
|
153
|
+
if stimulus_range.ndim == 2:
|
|
154
|
+
stimulus_range = stimulus_range[:, [0]]
|
|
155
|
+
|
|
156
|
+
stimulus_range = np.stack((stimulus_range, np.ones_like(stimulus_range)), axis=1).astype(np.float32)
|
|
157
|
+
if subtract_baseline:
|
|
158
|
+
parameters = self._zero_baseline(parameters)
|
|
159
|
+
W = self.basis_predictions(stimulus_range, parameters)
|
|
160
|
+
|
|
161
|
+
pseudoWWT = ops.tensordot(W, W, axes=[[0], [0]])
|
|
162
|
+
return ops.where(ops.isnan(pseudoWWT), ops.zeros_like(pseudoWWT), pseudoWWT)
|
|
163
|
+
|
|
164
|
+
class AxialVonMisesPRF(VonMisesPRF):
|
|
165
|
+
"""Von Mises pRF for π-periodic (axial) stimuli such as gabor orientations.
|
|
166
|
+
|
|
167
|
+
Gabor gratings are π-periodic: an orientation of 0° and one of 180° are
|
|
168
|
+
identical stimuli. The standard ``VonMisesPRF`` uses ``cos(x − μ)``,
|
|
169
|
+
which is 2π-periodic and therefore treats 0° and 180° as maximally
|
|
170
|
+
*dissimilar* — the opposite of correct.
|
|
171
|
+
|
|
172
|
+
``AxialVonMisesPRF`` uses the axial (doubled-angle) formula::
|
|
173
|
+
|
|
174
|
+
f(x; μ, κ) = exp(κ · cos(2(x − μ))) / (π · I₀(κ))
|
|
175
|
+
|
|
176
|
+
This distribution is π-periodic: it peaks at x = μ **and** at x = μ + π,
|
|
177
|
+
correctly encoding the equivalence of opposite orientations.
|
|
178
|
+
|
|
179
|
+
Parameters
|
|
180
|
+
----------
|
|
181
|
+
Same as ``VonMisesPRF``. ``mu`` should be in [0, π).
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
def _get_stimulus_type(self, model_stimulus_amplitude=False):
|
|
185
|
+
return OneDimensionalOrientationStimulus
|
|
186
|
+
|
|
187
|
+
def _get_stimulus(self, **kwargs):
|
|
188
|
+
return OneDimensionalOrientationStimulus()
|
|
189
|
+
|
|
190
|
+
def _basis_predictions_without_amplitude(self, paradigm, parameters):
|
|
191
|
+
return axial_von_mises_pdf(paradigm[..., None, 0],
|
|
192
|
+
parameters[:, None, :, 0],
|
|
193
|
+
parameters[:, None, :, 1]) * \
|
|
194
|
+
parameters[:, None, :, 2] + parameters[:, None, :, 3]
|
|
195
|
+
|
|
196
|
+
def _basis_predictions_with_amplitude(self, paradigm, parameters):
|
|
197
|
+
return axial_von_mises_pdf(paradigm[..., None, 0],
|
|
198
|
+
parameters[:, None, :, 0],
|
|
199
|
+
parameters[:, None, :, 1]) * \
|
|
200
|
+
parameters[:, None, :, 2] * paradigm[..., None, 1] + parameters[:, None, :, 3]
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class LogGaussianPRF(GaussianPRF):
|
|
204
|
+
"""Log-Gaussian tuning curve with configurable parameterization."""
|
|
205
|
+
|
|
206
|
+
parameter_labels = ['mu', 'sd', 'amplitude', 'baseline']
|
|
207
|
+
|
|
208
|
+
def __init__(self, allow_neg_amplitudes=False, verbosity=logging.INFO,
|
|
209
|
+
model_stimulus_amplitude=False,
|
|
210
|
+
parameterisation='mu_sd_natural',
|
|
211
|
+
**kwargs):
|
|
212
|
+
|
|
213
|
+
if parameterisation == 'mu_sd_natural':
|
|
214
|
+
self.parameter_labels = ['mu', 'sd', 'amplitude', 'baseline']
|
|
215
|
+
self._basis_predictions_without_amplitude = self._basis_predictions_without_amplitude_n
|
|
216
|
+
self._basis_predictions_with_amplitude = self._basis_predictions_with_amplitude_n
|
|
217
|
+
elif parameterisation == 'mode_fwhm_natural':
|
|
218
|
+
self.parameter_labels = ['mode', 'fwhm', 'amplitude', 'baseline']
|
|
219
|
+
self._basis_predictions_without_amplitude = self._basis_predictions_without_amplitude_mode_fwhm
|
|
220
|
+
self._basis_predictions_with_amplitude = self._basis_predictions_with_amplitude_mode_fwhm
|
|
221
|
+
else:
|
|
222
|
+
raise ValueError('Unknown parameterisation! Needs to be in [mu_sd_natural, mode_fwhm_natural]')
|
|
223
|
+
|
|
224
|
+
if allow_neg_amplitudes:
|
|
225
|
+
self.transformations = ['softplus', 'softplus', 'identity', 'identity']
|
|
226
|
+
else:
|
|
227
|
+
self.transformations = ['softplus', 'softplus', 'softplus', 'identity']
|
|
228
|
+
|
|
229
|
+
super().__init__(allow_neg_amplitudes=allow_neg_amplitudes,
|
|
230
|
+
verbosity=verbosity, model_stimulus_amplitude=model_stimulus_amplitude,
|
|
231
|
+
**kwargs)
|
|
232
|
+
|
|
233
|
+
def _basis_predictions_without_amplitude_n(self, paradigm, parameters):
|
|
234
|
+
"""Log-normal tuning (mu/sd) without external amplitude modulation."""
|
|
235
|
+
return lognormalpdf_n(paradigm[..., None, 0],
|
|
236
|
+
parameters[:, None, :, 0],
|
|
237
|
+
parameters[:, None, :, 1]) * \
|
|
238
|
+
parameters[:, None, :, 2] + parameters[:, None, :, 3]
|
|
239
|
+
|
|
240
|
+
def _basis_predictions_with_amplitude_n(self, paradigm, parameters):
|
|
241
|
+
"""Log-normal tuning (mu/sd) scaled by stimulus amplitude."""
|
|
242
|
+
return lognormalpdf_n(paradigm[..., None, 0],
|
|
243
|
+
parameters[:, None, :, 0],
|
|
244
|
+
parameters[:, None, :, 1]) * \
|
|
245
|
+
parameters[:, None, :, 2] * paradigm[..., None, 1] + parameters[:, None, :, 3]
|
|
246
|
+
|
|
247
|
+
def _basis_predictions_without_amplitude_mode_fwhm(self, paradigm, parameters):
|
|
248
|
+
"""Mode/FWHM parameterization without amplitude scaling."""
|
|
249
|
+
return lognormal_pdf_mode_fwhm(paradigm[..., None, 0],
|
|
250
|
+
parameters[:, None, :, 0],
|
|
251
|
+
parameters[:, None, :, 1]) * \
|
|
252
|
+
parameters[:, None, :, 2] + parameters[:, None, :, 3]
|
|
253
|
+
|
|
254
|
+
def _basis_predictions_with_amplitude_mode_fwhm(self, paradigm, parameters):
|
|
255
|
+
"""Mode/FWHM parameterization scaled by stimulus amplitude."""
|
|
256
|
+
return lognormal_pdf_mode_fwhm(paradigm[..., None, 0],
|
|
257
|
+
parameters[:, None, :, 0],
|
|
258
|
+
parameters[:, None, :, 1]) * \
|
|
259
|
+
parameters[:, None, :, 2] * paradigm[..., None, 1] + parameters[:, None, :, 3]
|
|
260
|
+
|
|
261
|
+
class GaussianPRFWithHRF(HRFEncodingModel, GaussianPRF):
|
|
262
|
+
"""Combine Gaussian pRF spatial tuning with an explicit HRF convolution."""
|
|
263
|
+
|
|
264
|
+
def __init__(self, hrf_model=None,
|
|
265
|
+
flexible_hrf_parameters=False, allow_neg_amplitudes=False,
|
|
266
|
+
model_stimulus_amplitude=False, verbosity=logging.INFO, **kwargs):
|
|
267
|
+
GaussianPRF.__init__(self, allow_neg_amplitudes=allow_neg_amplitudes,
|
|
268
|
+
model_stimulus_amplitude=model_stimulus_amplitude,
|
|
269
|
+
verbosity=verbosity, **kwargs)
|
|
270
|
+
HRFEncodingModel.__init__(self, hrf_model=hrf_model,
|
|
271
|
+
flexible_hrf_parameters=flexible_hrf_parameters, **kwargs)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class LogGaussianPRFWithHRF(HRFEncodingModel, LogGaussianPRF):
|
|
275
|
+
"""Log-Gaussian tuning plus HRF parameters."""
|
|
276
|
+
|
|
277
|
+
def __init__(self, hrf_model=None,
|
|
278
|
+
flexible_hrf_parameters=False, allow_neg_amplitudes=False,
|
|
279
|
+
model_stimulus_amplitude=False, parameterisation='mu_sd_natural',
|
|
280
|
+
verbosity=logging.INFO, **kwargs):
|
|
281
|
+
LogGaussianPRF.__init__(self, allow_neg_amplitudes=allow_neg_amplitudes,
|
|
282
|
+
model_stimulus_amplitude=model_stimulus_amplitude,
|
|
283
|
+
parameterisation=parameterisation,
|
|
284
|
+
verbosity=verbosity, **kwargs)
|
|
285
|
+
HRFEncodingModel.__init__(self, hrf_model=hrf_model,
|
|
286
|
+
flexible_hrf_parameters=flexible_hrf_parameters, **kwargs)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class AlphaGaussianPRF(GaussianPRF):
|
|
290
|
+
"""Gaussian pRF with additional alpha parameter controlling asymmetry."""
|
|
291
|
+
|
|
292
|
+
parameter_labels = ['mu', 'sd', 'alpha', 'amplitude', 'baseline']
|
|
293
|
+
|
|
294
|
+
def __init__(self, allow_neg_amplitudes=False, verbosity=logging.INFO,
|
|
295
|
+
model_stimulus_amplitude=False,
|
|
296
|
+
**kwargs):
|
|
297
|
+
|
|
298
|
+
if model_stimulus_amplitude:
|
|
299
|
+
raise NotImplementedError("Modeling stimulus amplitude is not implemented for AlphaGaussianPRF")
|
|
300
|
+
|
|
301
|
+
if allow_neg_amplitudes:
|
|
302
|
+
self.transformations = ['softplus', 'softplus', 'identity', 'identity', 'identity']
|
|
303
|
+
else:
|
|
304
|
+
self.transformations = ['softplus', 'softplus', 'identity', 'softplus', 'identity']
|
|
305
|
+
|
|
306
|
+
super().__init__(allow_neg_amplitudes=allow_neg_amplitudes,
|
|
307
|
+
verbosity=verbosity, model_stimulus_amplitude=model_stimulus_amplitude,
|
|
308
|
+
**kwargs)
|
|
309
|
+
|
|
310
|
+
def _basis_predictions_without_amplitude(self, paradigm, parameters):
|
|
311
|
+
def alpha_transform(x, alpha, eps=1e-6):
|
|
312
|
+
return ops.where(
|
|
313
|
+
ops.abs(alpha) < eps,
|
|
314
|
+
ops.log(x),
|
|
315
|
+
(ops.power(x, alpha) - 1) / alpha
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
def f_x(x, mu_x, sigma_mu, alpha):
|
|
319
|
+
mu_alpha_x = alpha_transform(x, alpha)
|
|
320
|
+
mu_alpha_mu = alpha_transform(mu_x, alpha)
|
|
321
|
+
exponent = -(mu_alpha_x - mu_alpha_mu)**2 / (2 * sigma_mu**2)
|
|
322
|
+
return ops.exp(exponent)
|
|
323
|
+
|
|
324
|
+
return f_x(paradigm[..., None, 0],
|
|
325
|
+
parameters[:, None, :, 0],
|
|
326
|
+
parameters[:, None, :, 1],
|
|
327
|
+
parameters[:, None, :, 2]) * \
|
|
328
|
+
parameters[:, None, :, 3] + parameters[:, None, :, 4]
|
|
329
|
+
|
|
330
|
+
class RegressionAlphaGaussianPRF(EncodingRegressionModel, AlphaGaussianPRF):
|
|
331
|
+
"""Alpha-Gaussian pRF variant whose parameters depend on regressors."""
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
class GaussianPRFOnGaussianSignal(GaussianPRF):
|
|
335
|
+
"""pRF evaluated on Gaussian stimulus summaries (mean + SD)."""
|
|
336
|
+
|
|
337
|
+
stimulus_type = OneDimensionalGaussianStimulus
|
|
338
|
+
|
|
339
|
+
def __init__(self, allow_neg_amplitudes=False,
|
|
340
|
+
stimulus_grid=None, verbosity=logging.INFO,
|
|
341
|
+
**kwargs):
|
|
342
|
+
|
|
343
|
+
if stimulus_grid is None:
|
|
344
|
+
raise Exception('Need stimulus_grid!')
|
|
345
|
+
|
|
346
|
+
self.stimulus_grid = stimulus_grid.astype(np.float32)
|
|
347
|
+
|
|
348
|
+
super().__init__(allow_neg_amplitudes=allow_neg_amplitudes,
|
|
349
|
+
verbosity=logging.INFO, **kwargs)
|
|
350
|
+
|
|
351
|
+
def get_paradigm(self, paradigm):
|
|
352
|
+
if isinstance(paradigm, pd.DataFrame):
|
|
353
|
+
for column in ('mu', 'sd'):
|
|
354
|
+
if column not in paradigm.columns:
|
|
355
|
+
raise ValueError(f'Need {column} of the Gaussian stimulus in the paradigm.')
|
|
356
|
+
paradigm = paradigm[['mu', 'sd']]
|
|
357
|
+
return super().get_paradigm(paradigm)
|
|
358
|
+
|
|
359
|
+
def _basis_predictions(self, paradigm, parameters):
|
|
360
|
+
"""Convolve receptive-field Gaussians with Gaussian stimulus inputs."""
|
|
361
|
+
rf_field = norm(self.stimulus_grid[:, None, None, None],
|
|
362
|
+
parameters[None, :, None, :, 0],
|
|
363
|
+
parameters[None, :, None, :, 1])
|
|
364
|
+
|
|
365
|
+
input_stimulus = norm(self.stimulus_grid[:, None, None, None],
|
|
366
|
+
paradigm[None, ..., 0, None],
|
|
367
|
+
paradigm[None, ..., 1, None])
|
|
368
|
+
|
|
369
|
+
return ops.sum(rf_field * input_stimulus, axis=0)
|