mne_rt.protocols.TransferProtocol#
- class mne_rt.protocols.TransferProtocol(fname: str | Path, modality: str, direction: str = 'up', zscore_threshold: float = 0.5, adapt_rate: float = 0.0, smoothing: float = 0.0)#
Bases:
objectCross-session transfer NF protocol seeded from a prior-session file.
Loads a previous session’s NF feature time-series from a BIDS behavioural JSON file (
beh/*.json), computes the prior mean and standard deviation, and initialises Welford’s online algorithm as if those samples had already been observed. Subsequent calls toevaluate()continue updating the running statistics, but because the prior already contributes, no warmup period is needed.The JSON file must follow MNE-RT’s BIDS behavioural format:
{ "meta": {"modalities": ["sensor_power"], ...}, "data": {"sensor_power": [0.1, 0.2, ...]} }
- Parameters:
- fname
str|Path Path to a prior-session BIDS behavioural JSON file.
- modality
str Key inside
datafrom which to read the prior time-series (e.g.,"sensor_power").- direction{“up”, “down”}, default “up”
“up” -> reward when z-score >
zscore_threshold. “down” -> reward when z-score < -zscore_threshold.- zscore_threshold
float, default 0.5 Minimum absolute z-score required to issue a reward. Must be >= 0.
- adapt_rate
float, default 0.0 Controls how quickly the running statistics adapt to the new session. Set to
0.0to freeze the prior statistics (pure transfer); higher values let the statistics drift toward the current session. Implemented as an EMA blend between the Welford update and the frozen prior: the effective weight of each new sample isadapt_rate(must be in[0, 1)).- smoothing
float, default 0.0 EMA smoothing coefficient applied to the raw input before z-scoring. Must be in
[0, 1).0.0disables smoothing.
- fname
- Raises:
FileNotFoundErrorIf
fnamedoes not point to an existing file.KeyErrorIf the
"data"key is absent from the JSON ormodalityis not found insidedata.ValueErrorIf any numerical parameter is outside its valid range, or if the prior data array has fewer than 2 elements (insufficient for std).
Notes
The Welford accumulator is initialised with:
n = len(prior_data) mean = mean(prior_data) M2 = var(prior_data, ddof=1) * (n - 1)
so the running
std_on the first call toevaluate()equals the prior standard deviation. Whenadapt_rate > 0each new sample contributes weightadapt_rateto the mean and M2, while the existing accumulator retains weight1 - adapt_rate.Examples
Seed today’s session from yesterday’s baseline:
from mne_rt.protocols.transfer import TransferProtocol proto = TransferProtocol( fname="sub-01/ses-01/beh/sub-01_ses-01_task-nf_beh.json", modality="sensor_power", direction="up", zscore_threshold=0.5, ) for value in nf_stream: crossed, magnitude = proto.evaluate(value) if crossed: send_reward(magnitude)
Added in version 1.0.0.
- __init__(fname: str | Path, modality: str, direction: str = 'up', zscore_threshold: float = 0.5, adapt_rate: float = 0.0, smoothing: float = 0.0) None[source]#
Methods
__init__(fname, modality[, direction, ...])evaluate(value)Evaluate one NF value and return (crossed, magnitude).
reset()Reset to the prior statistics without re-reading the file.
Attributes
Current running mean (prior-seeded).
Number of values evaluated since init or last
reset().Number of samples in the prior session data (read-only).
Mean of the prior session data (read-only).
Standard deviation of the prior session data (read-only).
Current running standard deviation (prior-seeded).
Z-score computed during the most recent
evaluate()call.- evaluate(value: float) tuple[bool, float][source]#
Evaluate one NF value and return (crossed, magnitude).
Updates the running baseline statistics (seeded from the prior) with the (optionally smoothed) input, computes the z-score against the running mean and standard deviation, and determines whether the reward criterion is met.
- Parameters:
- value
float Current NF feature value.
- value
- Returns:
Notes
When
adapt_rate = 0.0the running statistics are frozen at the prior values: every evaluation is z-scored against the prior mean/std. Whenadapt_rate > 0new samples gradually shift the running statistics toward the current session distribution.
- property mean_: float#
Current running mean (prior-seeded).
Before any adaptations this equals
prior_mean.
- reset() None[source]#
Reset to the prior statistics without re-reading the file.
Restores the Welford accumulators to the values computed from the prior data during
__init__, clears the smoothed state, and resets the evaluation counter. All constructor parameters are preserved.
- property std_: float#
Current running standard deviation (prior-seeded).
Before any adaptations this equals
prior_std.
- property zscore: float#
Z-score computed during the most recent
evaluate()call.Returns
0.0before any evaluation.