Skip to content

remeta.configuration.Configuration

Configuration for the ReMeta toolbox

Usage
cfg = remeta.configuration
cfg.<some_setting> = <some_value>
rem = remeta.ReMeta(cfg)

To change parameters use:

cfg.param_<param_name>.<param_attribute> = <value>

See Parameter for more information.

Parameters:

Name Type Description Default
normalize_stimuli_by_max bool

If True, normalize provided stimuli by their maximum value to the range [-1; 1]. Note that stimuli should be roughly in the range [-1; 1] for optimal parameter estimation.

False
type2_noise_type str

Whether the model considers noise at readout, report or for the estimation of type 1 noise ("temperature"). Possible values: 'readout', 'report', 'temperature'.

'report'
skip_type2 bool

If True, only fit type 1 data. No confidence data needs to be passed to fit() in this case.

False
optim_type1_gridsearch bool

If True, perform an initial gridsearch search for type 1 parameter optimization, based on the grid_range attributes of Parameters.

False
optim_type1_minimize_along_grid bool

If True, do sqlqp minimization for type 1 parameter optimization at each grid point.

False
optim_type1_global_minimization str

Use one of 'shgo', 'dual_annealing' 'differential_evolution' to perform type 1 likelihood minimization with a global minimizer.

None
optim_type1_scipy_solvers str | list[str] | tuple[str, ...]

Set scipy.optimize.minimize solver method for type 1 parameter optimization.. If provided as tuple/list, test different solvers and take the best.

'trust-constr'
optim_type2_gridsearch bool

If True, perform an initial gridsearch search for type 2 parameter optimization, based on the grid_range attributes of Parameters.

False
optim_type2_minimize_along_grid bool

If True, do sqlqp minimization for type 2 parameter optimization at each grid point.

False
optim_type2_global_minimization str

Use one of 'shgo', 'dual_annealing' 'differential_evolution' to perform type 2 likelihood minimization with a global minimizer.

None
optim_type2_scipy_solvers str | list[str] | tuple[str, ...]

Set scipy.optimize.minimize solver method for type 2 parameter optimization.. If provided as tuple/list, test different solvers and take the best.

('slsqp', 'Nelder-Mead')
optim_type2_slsqp_epsilon float

Set parameter epsilon parameter for the SLSQP optimization method (type 2). If provided as tuple/list, test different eps parameters and take the best

None
optim_num_cores int

Number of cores used for parameter estimation (-1 for all cores minus 1).

1
param_type1_noise Parameter

Type 1 noise.

Parameter enable: 1 guess: 0.5 bounds: (0.001, 10) grid_range: [0.1 0.22857143 0.35714286 0.48571429 0.61428571 0.74285714 0.87142857 1. ] group: None prior: None preset: None default: 0.01 model: normal _definition_changed: False
param_type1_thresh Parameter

Type 1 threshold.

Parameter enable: 0 guess: 0 bounds: (0, 1) grid_range: [0. 0.05 0.1 0.15 0.2 ] group: None prior: None preset: None default: 0 model: None _definition_changed: False
param_type1_bias Parameter

Type 1 bias.

Parameter enable: 1 guess: 0 bounds: (-1, 1) grid_range: [-0.2 -0.14285714 -0.08571429 -0.02857143 0.02857143 0.08571429 0.14285714 0.2 ] group: None prior: None preset: None default: 0 model: None _definition_changed: False
param_type1_nonlinear_gain Parameter

Gain parameter for nonlinear encoding (higher values -> stronger nonlinearity).

Parameter enable: 0 guess: 0 bounds: (-0.8888888888888888, 10) grid_range: [-0.5 -0.125 0.25 0.625 1. ] group: None prior: None preset: None default: 0 model: None _definition_changed: False
param_type1_nonlinear_scale Parameter

Scale parameter for the nonlinearity (higher values -> non-linearity kicks in later).

Parameter enable: 0 guess: 1 bounds: (0.01, 10) grid_range: [0.01 0.5075 1.005 1.5025 2. ] group: None prior: None preset: None default: None model: None _definition_changed: False
param_type1_noise_heteroscedastic Parameter

Signal-dependent type 1 noise. Specify the signal dependency via the .model attribute of the parameter. Default is 'multiplicative', which corresponds to Weber's law with a noise floor. In this case, type1_noise is the noise floor and type1_noise_heteroscedastic is the signal scaling factor.

Parameter enable: 0 guess: 0 bounds: (0, 10) grid_range: [0. 0.25 0.5 0.75 1. ] group: None prior: None preset: None default: 0 model: multiplicative _definition_changed: False
param_type2_noise Parameter

Metacognitive noise. May characterize metacognitive noise of either a noisy-readout, noisy-report or noisy-temperature model.

Parameter enable: 1 guess: 0.1 bounds: (0.05, 2) grid_range: [0.1 0.22857143 0.35714286 0.48571429 0.61428571 0.74285714 0.87142857 1. ] group: None prior: None preset: None default: 0.01 model: None _definition_changed: False
param_type2_evidence_bias Parameter

Parameter for a multiplicative metacognitive bias loading on evidence.

Parameter enable: 0 guess: 1 bounds: (0.5, 2) grid_range: [0.5 0.71428571 0.92857143 1.14285714 1.35714286 1.57142857 1.78571429 2. ] group: None prior: None preset: None default: 1 model: None _definition_changed: False
param_type2_confidence_bias Parameter

Parameter for a power-law metacognitive bias loading on confidence.

Parameter enable: 0 guess: 1 bounds: (0.5, 2) grid_range: [0.5 0.71428571 0.92857143 1.14285714 1.35714286 1.57142857 1.78571429 2. ] group: None prior: None preset: None default: 1 model: None _definition_changed: False
param_type2_criteria Parameter

Confidence criteria.

Parameter enable: 3 guess: equispaced bounds: (1e-08, 1) grid_range: equispaced group: None prior: None preset: None default: equispaced model: None _definition_changed: False
min_type1_like float

Minimum probability used during the type 1 likelihood computation.

1e-10
min_type2_like float

Minimum probability used during the type 2 likelihood computation.

1e-10
min_type2_like_uni bool

Instead of using a minimum probability during the likelihood computation, use a maximum cumulative likelihood based on a uniform 'guessing' model. min_type2_likelihood is ignored in this case.

False
type2_binsize float

Integration bin size for the computation of the likelihood around empirical confidence values. A setting of 0 means that the probability density is assesed instead.

0.01
type2_binsize_wrap bool

Ensure constant window size for likelihood integration at the bounds. Only applies if confidence criteria are disabled and type2_binsize > 0.

False
type1_marg_z int

Number of standard deviations around the mean considered for the marginalization of type 1 uncertainty.

5
type1_marg_steps int

Number of integration steps for the marginalization of type 1 uncertainty.

101
temperature_marg_res float

Quintile resolution for the marginalization of type 1 noise in case of type2_noise_type 'temperature'.

0.001
type1_likel_incongr bool

If True, include incongruent decision values (i.e., sign(actual choice) != sign(decision value)) for the type 2 likelihood computation.

False
true_params dict

Pass true (known) parameter values. This can be useful for testing to compare the likelihood of true and fitted parameters. The likelihood of true parameters is returned (and printed).

None
initilialize_fitting_at_true_params bool

If True, initialize the parameter fitting procedure at the true parameters. True parameters must have been passed via true_params.

False
accept_mispecified_model bool

If True, ignore warnings about user-specified settings.

False
print_configuration bool

If True, print the configuration at instatiation of the ReMeta class (useful for logging).

False
Source code in remeta/configuration.py
 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
@reset_dataclass_on_init
@dataclass
class Configuration(ReprMixin):
    """ Configuration for the ReMeta toolbox

    Usage:
        ```
        cfg = remeta.configuration
        cfg.<some_setting> = <some_value>
        rem = remeta.ReMeta(cfg)
        ```

        To change parameters use:
        ```
        cfg.param_<param_name>.<param_attribute> = <value>
        ```

        See [`Parameter`][remeta.modelspec.Parameter] for more information.


    """

    ### Important settings

    normalize_stimuli_by_max: bool = field(default=False, metadata={'description': """ 
        If True, normalize provided stimuli by their maximum value to the range [-1; 1]. 
        Note that stimuli should be roughly in the range [-1; 1] for optimal parameter estimation."""
    })

    type2_noise_type: str = field(default='report', metadata={'description': """ 
        Whether the model considers noise at readout, report or for the estimation of type 1 noise ("temperature").
        Possible values: `'readout'`, `'report'`, `'temperature'`."""
    })

    skip_type2: bool = field(default=False, metadata={'description': """ 
        If `True`, only fit type 1 data. No confidence data needs to be passed to `fit()` in this case."""
    })



    ### Type 1 optimization

    optim_type1_gridsearch: bool = field(default=False, metadata={'description': """ 
        If `True`, perform an initial gridsearch search for type 1 parameter optimization, based on the `grid_range`
        attributes of Parameters."""
    })

    # optim_type1_fine_gridsearch: bool = field(default=False, metadata={'description': """
    #     Perform a fine-grained grid search for type 1 parameter optimization."""
    # })

    optim_type1_minimize_along_grid: bool = field(default=False, metadata={'description': """ 
        If `True`, do sqlqp minimization for type 1 parameter optimization at each grid point."""
    })

    optim_type1_global_minimization: str = field(default=None, metadata={'description': """ 
        Use one of 'shgo', 'dual_annealing' 'differential_evolution' to perform type 1 likelihood minimization with
        a global minimizer."""
    })

    optim_type1_scipy_solvers: str | list[str] | tuple[str, ...] = field(default='trust-constr', metadata={'description': """ 
        Set scipy.optimize.minimize solver method for type 1 parameter optimization..
        If provided as tuple/list, test different solvers and take the best."""
    })



    ### Type 2 optimization

    optim_type2_gridsearch: bool = field(default=False, metadata={'description': """ 
        If `True`, perform an initial gridsearch search for type 2 parameter optimization, based on the `grid_range`
        attributes of Parameters."""
    })

    # optim_type2_fine_gridsearch: bool = field(default=False, metadata={'description': """
    #     Perform a fine-grained grid search for type 2 parameter optimization."""
    # })

    optim_type2_minimize_along_grid: bool = field(default=False, metadata={'description': """ 
        If `True`, do sqlqp minimization for type 2 parameter optimization at each grid point."""
    })

    optim_type2_global_minimization: str = field(default=None, metadata={'description': """ 
        Use one of 'shgo', 'dual_annealing' 'differential_evolution' to perform type 2 likelihood minimization with
        a global minimizer."""
    })

    optim_type2_scipy_solvers: str | list[str] | tuple[str, ...] = field(default=('slsqp', 'Nelder-Mead'), metadata={'description': """ 
        Set scipy.optimize.minimize solver method for type 2 parameter optimization..
        If provided as tuple/list, test different solvers and take the best."""
    })

    optim_type2_slsqp_epsilon: float = field(default=None, metadata={'description': """ 
        Set parameter epsilon parameter for the SLSQP optimization method (type 2).
        If provided as tuple/list, test different eps parameters and take the best"""
    })

    optim_num_cores: int = field(default=1, metadata={'description': """ 
        Number of cores used for parameter estimation (-1 for all cores minus 1)."""
    })



    ### Parameters

    ## Type 1

    param_type1_noise: Parameter = field(
        default=Parameter(enable=1, guess=0.5, bounds=(0.001, 10), grid_range=np.linspace(0.1, 1, 8), default=0.01, model='normal'),
        metadata={'description': """ 
        Type 1 noise."""
    })
    param_type1_thresh: Parameter = field(
        default=Parameter(enable=0, guess=0, bounds=(0, 1), grid_range=np.linspace(0, 0.2, 5), default=0),
        metadata={'description': """ 
        Type 1 threshold."""
    })
    param_type1_bias: Parameter = field(
        default=Parameter(enable=1, guess=0, bounds=(-1, 1), grid_range=np.linspace(-0.2, 0.2, 8), default=0),
        metadata={'description': """ 
        Type 1 bias."""
    })
    param_type1_nonlinear_gain: Parameter = field(
        default=Parameter(enable=0, guess=0, bounds=(-8 / 9, 10), grid_range=np.linspace(-0.5, 1, 5), default=0),
        metadata={'description': """ 
        Gain parameter for nonlinear encoding (higher values -> stronger nonlinearity)."""
    })
    param_type1_nonlinear_scale: Parameter = field(
        default=Parameter(enable=0, guess=1, bounds=(0.01, 10), grid_range=np.linspace(0.01, 2, 5), default=None),
        metadata={'description': """ 
        Scale parameter for the nonlinearity (higher values -> non-linearity kicks in later)."""
    })
    param_type1_noise_heteroscedastic: Parameter = field(
        default=Parameter(enable=0, guess=0, bounds=(0, 10), grid_range=np.linspace(0, 1, 5), model='multiplicative', default=0),
        metadata={'description': """ 
        Signal-dependent type 1 noise. Specify the signal dependency via the `.model` attribute of the 
        parameter. Default is `'multiplicative'`, which corresponds to Weber's law with a noise floor. In this case, 
        `type1_noise` is the noise floor and `type1_noise_heteroscedastic` is the signal scaling factor."""
    })


    ## Type 2

    param_type2_noise: Parameter = field(
        default=Parameter(enable=1, guess=0.1, bounds=(0.05, 2), grid_range=np.linspace(0.1, 1, 8), default=0.01),
        metadata={'description': """ 
        Metacognitive noise. May characterize metacognitive noise of either a noisy-readout, noisy-report or 
        noisy-temperature model."""
    })
    param_type2_evidence_bias: Parameter = field(
        default=Parameter(enable=0, guess=1, bounds=(0.5, 2), grid_range=np.linspace(0.5, 2, 8), default=1),
        metadata={'description': """ 
        Parameter for a multiplicative metacognitive bias loading on evidence."""
    })
    param_type2_confidence_bias: Parameter = field(
        default=Parameter(enable=0, guess=1, bounds=(0.5, 2), grid_range=np.linspace(0.5, 2, 8), default=1),
        metadata={'description': """ 
        Parameter for a power-law metacognitive bias loading on confidence."""
    })
    param_type2_criteria: Parameter = field(
        default=Parameter(enable=3, guess='equispaced', grid_range='equispaced', default='equispaced', bounds=(1e-8, 1)),
        metadata={'description': """ 
        Confidence criteria."""
    })


    ### Likelihood computation

    min_type1_like: float = field(default=1e-10, metadata={'description': """ 
        Minimum probability used during the type 1 likelihood computation."""
                                                           })

    min_type2_like: float = field(default=1e-10, metadata={'description': """ 
        Minimum probability used during the type 2 likelihood computation."""
                                                           })

    min_type2_like_uni: bool =  field(default=False, metadata={'description': """ 
        Instead of using a minimum probability during the likelihood computation, use a maximum cumulative
        likelihood based on a uniform 'guessing' model. `min_type2_likelihood` is ignored in this case."""
                                                               })

    type2_binsize: float = field(default=0.01, metadata={'description': """ 
        Integration bin size for the computation of the likelihood around empirical confidence values.
        A setting of 0 means that the probability density is assesed instead."""
    })

    type2_binsize_wrap: bool = field(default=False, metadata={'description': """ 
        Ensure constant window size for likelihood integration at the bounds.
        Only applies if confidence criteria are disabled and type2_binsize > 0."""
    })

    type1_marg_z: int = field(default=5, metadata={'description': """ 
        Number of standard deviations around the mean considered for the marginalization of type 1 uncertainty."""
    })

    type1_marg_steps: int = field(default=101, metadata={'description': """ 
        Number of integration steps for the marginalization of type 1 uncertainty."""
    })

    temperature_marg_res: float = field(default=0.001, metadata={'description': """ 
        Quintile resolution for the marginalization of type 1 noise in case of type2_noise_type 'temperature'."""
    })

    type1_likel_incongr: bool = field(default=False, metadata={'description': """ 
        If `True`, include incongruent decision values (i.e., sign(actual choice) != sign(decision value)) for the type 2 
        likelihood computation."""
    })


    ### Useful for testing

    true_params: dict = field(default=None, metadata={'description': """ 
        Pass true (known) parameter values. This can be useful for testing to compare the likelihood of true and
        fitted parameters. The likelihood of true parameters is returned (and printed)."""
    })

    initilialize_fitting_at_true_params: bool = field(default=False, metadata={'description': """ 
        If `True`, initialize the parameter fitting procedure at the true parameters. True parameters must 
        have been passed via `true_params`."""
    })

    accept_mispecified_model: bool = field(default=False, metadata={'description': """ 
        If `True`, ignore warnings about user-specified settings."""
    })

    print_configuration: bool = field(default=False, metadata={'description': """ 
        If True, print the configuration at instatiation of the ReMeta class (useful for logging)."""
    })


    ### Private attributes (do not change)

    _param_type1_noise: Parameter | list[Parameter] = None
    _param_type1_noise_heteroscedastic: Parameter | list[Parameter] = None
    _param_type1_nonlinear_scale: Parameter | list[Parameter] = None
    _param_type1_nonlinear_gain: Parameter | list[Parameter] = None
    _param_type1_thresh: Parameter | list[Parameter] = None
    _param_type1_bias: Parameter | list[Parameter] = None
    _param_type2_noise: Parameter = None
    _param_type2_evidence_bias: Parameter = None
    _param_type2_confidence_bias: list[Parameter] = None
    _param_type2_criteria: list[Parameter] = None

    _paramset_type1: ParameterSet = None
    _paramset_type2: ParameterSet = None
    _paramset: ParameterSet = None

    _n_conf_levels: int = None

    _optim_num_cores: int = None

    _fields: set = None


    def __post_init__(self):
        # Define allowed fields
        self._fields = {f.name for f in fields(self)}


    def __setattr__(self, key, value):
        if self._fields is not None and key not in self._fields:
            raise AttributeError(f"'{type(self).__name__}' object has no attribute '{key}'")
        # Call the base class setattr to actually set the value
        super().__setattr__(key, value)


    def setup(self, generative_mode=False, silence_warnings=False):

        if (self.optim_num_cores > 1) and find_spec('multiprocessing_on_dill') is None:
            if not silence_warnings:
                warnings.warn(f'Multiprocessing on dill is not installed. Setting optim_num_cores to 1.')
            self.optim_num_cores = 1

        if self.optim_num_cores > 1:
            from multiprocessing import cpu_count
            self._optim_num_cores = max(1, (cpu_count() or 1) - 1) if self.optim_num_cores == -1 \
                else self.optim_num_cores
        else:
            self._optim_num_cores = 1

        self._prepare_params_type1()
        if self.skip_type2:
            if self.optim_type2_slsqp_epsilon is None:
                self.optim_type2_slsqp_epsilon = 1e-5
        else:

            if self.param_type1_thresh.enable and \
                (self.optim_type1_scipy_solvers == self.__dataclass_fields__['optim_type1_scipy_solvers'].default):
                self.optim_type1_scipy_solvers = ('trust-constr', 'Powell')


            if self.param_type2_noise.model is None:
                if self.type2_noise_type == 'report':
                    if (self.param_type2_criteria.enable):
                        self.param_type2_noise.model = 'beta_mode'
                    else:
                        self.param_type2_noise.model = 'truncated_normal_mode'
                elif (self.type2_noise_type == 'readout'):
                    if self.param_type2_criteria.enable:
                        self.param_type2_noise.model = 'lognormal_mode_std'
                    else:
                        self.param_type2_noise.model = 'truncated_normal_mode'
                elif self.type2_noise_type == 'temperature':
                    if self.param_type2_criteria.enable:
                        self.param_type2_noise.model = 'lognormal_mode_std'
                    else:
                        self.param_type2_noise.model = 'truncated_normal_mode'

                # if generative_mode and not silence_warnings:
                #         warnings.warn('In generative mode, you should to explicitly specify a type 2 noise distribution. '
                #                       f'Defaulting to "{self.param_type2_noise.model}"')

            self._prepare_params_type2()
            if self.optim_type2_slsqp_epsilon is None:
                self.optim_type2_slsqp_epsilon = 1e-5

            if self.type2_binsize is None:
                self.type2_binsize = 0.01

        self._prepare_params_all()

        self._check_compatibility(generative_mode=generative_mode, silence_warnings=silence_warnings)

        if self.print_configuration:
            self.print()
        # self.setup_called = True

    def _check_compatibility(self, generative_mode=False, silence_warnings=False):

        if not self.accept_mispecified_model:

            if not self.param_type1_noise.enable:
                raise ValueError("Type 1 noise must be enabled.")

            if not self.skip_type2:

                if self.param_type2_criteria.enable and self.param_type2_criteria.group is not None:
                    if not silence_warnings:
                        warnings.warn('It is not recommended to fit criteria as a random effect or a fixed group effect, '
                                      'for conceptual reasons, but also because standard errors are not reliable.')

                if not self.param_type2_noise.enable:
                    if not silence_warnings:
                        warnings.warn(f'Setting type2_param_noise.enable=False was provided -> type2_param_noise is set to its default value '
                                      f'({self._type2_param_noise_default}). You may change this value via the configuration.')

                if (self.type2_noise_type == 'temperature') and self.param_type2_noise._definition_changed and \
                    (self.param_type2_noise.bounds[0] < 1e-5):
                    if not silence_warnings:
                        warnings.warn('You manually changed the lower bound of the type 2 noise parameter for a '
                                      'noisy-temperature model to a very low value (<1e-5). Be warned that this may result '
                                      'in numerical instabilities that severely distort the likelihood computation.')

                if not generative_mode:
                    # If the configuration instance is used for generating data, we should not complain
                    # about fitting issues.

                    if self.param_type2_criteria.enable and self.param_type2_evidence_bias.enable:
                        if not silence_warnings:
                            warnings.warn(
                                'Fitting type2_param_criteria in combination with type2_param_evidence_bias.enable=1\n'
                                'can lead to biased parameter inferences. Use with caution.')

    def _prepare_params_type1(self):
        # if self.paramset_type1 is None:

            param_names_type1 = []
            params_type1 = ('noise', 'noise_heteroscedastic', 'nonlinear_gain', 'nonlinear_scale', 'thresh', 'bias')
            for param in params_type1:
                if getattr(self, f'param_type1_{param}').enable:
                    param_names_type1 += [f'type1_{param}']
                    # if getattr(self, f'_param_type1_{param}') is None:
                    param_definition = getattr(self, f'param_type1_{param}')
                    if getattr(self, f'param_type1_{param}').enable == 2:
                        setattr(self, f'_param_type1_{param}', [param_definition, param_definition])
                    else:
                        setattr(self, f'_param_type1_{param}', param_definition)
                    if self.true_params is not None and self.initilialize_fitting_at_true_params and f'type1_{param}' in self.true_params:
                        if (param_len := getattr(self, f'param_type1_{param}').enable) > 1:
                            for i in range(param_len):
                                getattr(self, f'_param_type1_{param}')[i].guess = self.true_params[f'type1_{param}'][i]
                        else:
                            getattr(self, f'_param_type1_{param}').guess = self.true_params[f'type1_{param}']

            parameters = {k: getattr(self, f"_param_{k}") for k in param_names_type1}
            self._paramset_type1 = ParameterSet(parameters, param_names_type1)

    def _prepare_params_type2(self):

        if self.param_type2_noise.enable and self._param_type2_noise is None and not self.param_type2_noise._definition_changed:

            lb = 0.05
            self.param_type2_noise.bounds = dict(
                report = dict(
                    beta_mean_std=(lb, 0.5),
                    beta_mode_std=(lb, 1 / np.sqrt(12)),
                    truncated_normal_mode_std=(lb, 1 / np.sqrt(12)),
                    truncated_gumbel_mode_std=(lb, 1 / np.sqrt(12)),
                    truncated_lognormal_mode_std=(lb, 1 / np.sqrt(12)),
                    beta_mode=(lb, 1),
                    truncated_normal_mode=(lb, 1),
                    truncated_gumbel_mode=(lb, 1),
                    truncated_lognormal_mode=(lb, 4),
                    truncated_lognormal_mean=(lb, 4),
                    truncated_lognorm=(lb, 4)
                ),
                readout = dict(
                    lognormal_mean=(lb, 1),
                    lognormal_mode=(lb, 1),
                    gamma_mean_std=(lb, 1),
                    lognormal_mean_std=(lb, 2),
                    lognormal_mode_std=(lb, 2),
                    lognormal_median_std=(lb, 2),
                    gamma_mean_cv=(lb, 2),
                    gamma_mean=(lb, 2),
                    gamma_mode_std=(lb, 2),
                    gamma_mode=(lb, 2),
                    betaprime_mean_std=(lb, 2),
                    truncated_normal_mode_std=(lb, 2),
                    truncated_normal_mode=(lb, 2),
                    truncated_gumbel_mode_std=(lb, 2),
                    truncated_gumbel_mode=(lb, 2)
                ),
                temperature = dict(
                    lognormal_mean=(lb, 1),
                    gamma_mean_std=(lb, 1),
                    lognormal_mean_std=(lb, 2),
                    lognormal_median_std=(lb, 2),
                    gamma_mean_cv=(lb, 2),
                    gamma_mean=(lb, 2),
                    gamma_mode_std=(lb, 2),
                    gamma_mode=(lb, 2),
                    betaprime_mean_std=(lb, 2),
                    truncated_normal_mode_std=(lb, 2),
                    truncated_normal_mode=(lb, 2),
                    truncated_gumbel_mode_std=(lb, 2),
                    truncated_gumbel_mode=(lb, 2),
                    lognormal_mode=(lb, 4),
                    lognormal_mode_std=(lb, 10),
                )
            )[self.type2_noise_type][self.param_type2_noise.model]
            self.param_type2_noise.grid_range = np.exp(np.linspace(np.log(self.param_type2_noise.bounds[0]),
                                                                   np.log(self.param_type2_noise.bounds[1]), 10)[1:-1])

        param_names_type2 = []
        params_type2 = ('noise', 'evidence_bias', 'confidence_bias')
        for param in params_type2:
            if getattr(self, f'param_type2_{param}').enable:
                param_names_type2 += [f'type2_{param}']
                # if getattr(self, f'_param_type2_{param}') is None:
                param_definition = getattr(self, f'param_type2_{param}')
                setattr(self, f'_param_type2_{param}', param_definition.copy())
                if self.true_params is not None and self.initilialize_fitting_at_true_params and f'type2_{param}' in self.true_params:
                    getattr(self, f'_param_type2_{param}').guess = self.true_params[f'type2_{param}']


        if self.param_type2_criteria.preset is not None:
            self.param_type2_criteria.enable = 0
            if listlike(self.param_type2_criteria.preset):
                self._n_conf_levels = len(self.param_type2_criteria.preset) + 1
            elif isinstance(self.param_type2_criteria.preset, int):
                self._n_conf_levels = self.param_type2_criteria.preset + 1
                self.param_type2_criteria.preset = np.arange(1/self._n_conf_levels, 1-1e-10, 1/self._n_conf_levels)
            else:
                raise ValueError('param_type2_criteria.preset must either be a list of criteria or '
                                 'an integer indicating the number of (equispaced) criteria.')


        if self.param_type2_criteria.enable:
            self._n_conf_levels = self.param_type2_criteria.enable + 1
            param_names_type2 += [f'type2_criteria']
            initialize_true = (self.initilialize_fitting_at_true_params and
                               self.true_params is not None and 'type2_criteria' in self.true_params)

            # internally, we handle criteria as criterion gaps!
            setattr(self, f'_param_type2_criteria',
                    [Parameter(
                       guess=self.true_params['type2_criteria'][i] if initialize_true
                                else (1 / self._n_conf_levels if self.param_type2_criteria.guess == 'equispaced'
                                      else self.param_type2_criteria_guesses[i]),
                       bounds=self.param_type2_criteria.bounds,
                       grid_range=np.linspace(0.05, 2 / self._n_conf_levels, 4) if
                       self.param_type2_criteria.grid_range == 'equispaced' else self.param_type2_criteria_grid_ranges[i],
                       default=1/self._n_conf_levels if self.param_type2_criteria.default == 'equispaced' else self.param_type2_criteria_default,
                    )
                     for i in range(self._n_conf_levels - 1)]
                    )
            if self.true_params is not None:
                if isinstance(self.true_params, dict):
                    # if 'type2_criteria' not in self.true_params:
                    #     raise ValueError('type2_criteria are missing from cfg.true_params')
                    if 'type2_criteria' in self.true_params:
                        self.true_params.update(
                            # type2_criteria_absolute=[np.sum(self.true_params['type2_criteria'][:i+1]) for i in range(len(self.true_params['type2_criteria']))],
                            type2_criteria_bias=np.mean(self.true_params['type2_criteria']) - 0.5,
                            type2_criteria_bias_sem=0,
                            type2_criteria_confidence_bias=0.5 - np.mean(self.true_params['type2_criteria']),
                            # type2_criteria_bias_mult=np.mean(self.true_params['type2_criteria']) / 0.5,
                            # type2_criteria_confidence_bias_mult=np.mean(self.true_params['type2_criteria']) / 0.5,
                            # type2_criteria_absdev=round(np.abs(np.array(self.true_params['type2_criteria']) -
                            #         np.arange(1/self._n_conf_levels, 1-1e-10, 1/self._n_conf_levels)).mean(), 10)
                        )
                elif isinstance(self.true_params, list):
                    for s in range(len(self.true_params)):
                        # if 'type2_criteria' not in self.true_params[s]:
                        #     raise ValueError(f'type2_criteria are missing from cfg.true_params (subject {s})')
                        if 'type2_criteria' in self.true_params[s]:
                            self.true_params[s].update(
                                # type2_criteria_absolute=[np.sum(self.true_params[s]['type2_criteria'][:i+1]) for i in range(len(self.true_params[s]['type2_criteria']))],
                                type2_criteria_bias=np.mean(self.true_params[s]['type2_criteria']) - 0.5,
                                type2_criteria_bias_sem=0,
                                type2_criteria_confidence_bias=0.5 - np.mean(self.true_params[s]['type2_criteria']),
                                # type2_criteria_absdev=round(np.abs(np.array(self.true_params[s]['type2_criteria']) -
                                #     np.arange(1/self._n_conf_levels, 1-1e-10, 1/self._n_conf_levels)).mean())
                            )

        parameters = {k: getattr(self, f"_param_{k}") for k in param_names_type2}
        self._paramset_type2 = ParameterSet(parameters, param_names_type2)


        self.check_type2_constraints()


    def _prepare_params_all(self):

        if self.skip_type2:
            self._paramset = self._paramset_type1
        else:
            parameters_all = {**self._paramset_type1.parameters, **self._paramset_type2.parameters}
            param_names_all = self._paramset_type1.param_names + self._paramset_type2.param_names
            self._paramset = ParameterSet(parameters_all, param_names_all)
            # for k, attr in self.paramset_type2.__dict__.items():
            #     attr_old = getattr(self.paramset, k)
            #     if isinstance(attr, list):
            #         attr_new = attr_old + attr
            #     elif isinstance(attr, dict):
            #         attr_new = {**attr_old, **attr}
            #     elif isinstance(attr, np.ndarray):
            #         if attr.ndim == 1:
            #             attr_new = np.hstack((attr_old, attr))
            #         else:
            #             attr_new = np.vstack((attr_old, attr))
            #     elif isinstance(attr, int):
            #         attr_new = attr_old + attr
            #     elif attr is None:
            #         if attr_old is None:
            #             attr_new = None
            #         else:
            #             raise ValueError(f'Type 2 attribute is None, but type 1 attribute is not.')
            #     else:
            #         raise ValueError(f'Unexpected type {type(attr)}')
            #     setattr(self.paramset, k, attr_new)




    def print(self):
        # print('***********************')
        print(f'{self.__class__.__name__}')
        for k, v in self.__dict__.items():
            # if not self.skip_type2 or ('type2' not in k):
            print('\n'.join([f'\t{k}: {v}']))
        # print('***********************')

    def __repr__(self):
        txt = f'{self.__class__.__name__}\n'
        txt += '\n'.join([f'\t{k}: {v}' for k, v in self.__dict__.items()])
        return txt

    def check_type2_constraints(self):
        pass