TruncExp#
- class braintools.cogtask.TruncExp(mean, min_val=0, max_val=inf, key=None)[source]#
Truncated exponential distribution for sampling time durations.
Useful for generating variable-duration phases with exponential distribution truncated to a specified range.
Examples
>>> import brainunit as u >>> import jax >>> import numpy as np >>> from braintools.cogtask import ( ... Feature, Fixation, Response, Task, TruncExp, VariableDuration) >>> delay = TruncExp(600 * u.ms, 300 * u.ms, 1500 * u.ms, key=jax.random.PRNGKey(0)) >>> delay TruncExp(mean=600ms, min=300ms, max=1500ms) >>> delay.min_value(), delay.max_value() (Quantity(300, "ms"), Quantity(1500, "ms")) >>> d = delay() # one sample from the sampler's own key >>> bool(300 * u.ms <= d) and bool(d < 1500 * u.ms) True >>> >>> # Typical use: sample in ``trial_init`` with the trial's RNG and >>> # read the value in a ``VariableDuration`` phase. >>> task = Task( ... Fixation(100 * u.ms, inputs={'fixation': 1.0}) ... >> VariableDuration(300 * u.ms, 1500 * u.ms, ctx_key='delay', ... inputs={'fixation': 1.0}) ... >> Response(100 * u.ms, outputs={'label': 1}), ... Feature(1, 'fixation'), Feature(2, 'choice'), ... trial_init=lambda ctx: ctx.update(delay=delay(ctx)), ... dt=10.0 * u.ms, seed=0, ... ) >>> task.max_trial_duration() # 10 + 150 + 10 steps 170 >>> X, Y, info = task.sample_trial(0) >>> X.shape, 30 <= int(np.asarray(info['mask']).sum()) <= 170 ((170, 1), True)