API Reference¶
Core Classes¶
The package exposes three primary classes through neurodesign:
from neurodesign import Experiment, Design, Optimisation
The compatibility import path from neurodesign.classes import ... resolves to the same class objects implemented in neurodesign/classes.py.
Public construction and selection routes:
create sampled designs with
Experiment.create_design(...)create fully manual flat one-event designs with
Experiment.create_manual_design(...)run search with
Optimisation.optimise()retrieve reported designs with
Optimisation.selected_design(rank)
Direct Design(...) construction is not a user-facing version-2 workflow.
Experiment¶
- class neurodesign.Experiment(TR, P, C, rho, n_stimuli, stim_duration=None, event_durations=None, trial_start_interval=0.0, post_event_interval=0.0, event_transition_interval=0.0, inter_trial_interval=0.0, rest_every_n_trials=None, rest_interval=0.0, n_trials=None, duration=None, resolution=0.1, FeMax=1, FdMax=1, FcMax=1, FfMax=1, maxrep=None, hardprob=False, confoundorder=3, order=None, trial_templates=None, trials=None, trial_template_probabilities=None, n_conceptual_trials=None, seed=None, ordertype='random', restnum=None, restdur=None, trial_max=None, **kwargs)[source]¶
Bases:
objectStore the experiment specification and generate realized designs.
Version 2 distinguishes conceptual trials from flattened modeled events: conceptual-trial counts govern sampling and trial boundaries, while the realized event axis governs event-level timing and Ff/Fc calculations.
- make_design_rng(salt=0)[source]¶
Create a reproducible RNG derived from the experiment seed.
- Return type:
- sample_template_id(rng)[source]¶
Sample one template identifier from the configured template weights.
- Return type:
- ff_max_for_event_count(event_count)[source]¶
Return the
Ffnormalization constant for a given event count.- Return type:
- fc_max_for_event_count(event_count, confoundorder=None)[source]¶
Return the
Fcnormalization constant for a given event count.- Return type:
- realize_manual_flat_design(order, inter_trial_intervals, all_event_durations=None)[source]¶
Materialize a flat one-event schedule from explicit event inputs.
- create_manual_design(order, inter_trial_intervals, event_durations=None)[source]¶
Wrap a manually specified flat schedule in a
Design.- Return type:
- create_design(seed=None)[source]¶
Sample or realize one design under the active scheduling mode.
- Return type:
- export_specification()[source]¶
Export the experiment specification with separated trial/event counts.
- specification_hash()[source]¶
Return a deterministic hash of the exported experiment specification.
- Return type:
Design¶
- class neurodesign.Design(experiment=None, schedule=None, trial_sequence=None, template_sequence=None)[source]¶
Bases:
objectRepresent one realized fMRI design with explicit event-level timing.
The design stores the flattened modeled-event order, the realized schedule arrays derived from an
Experiment, and the computed efficiency metrics used for reporting and optimisation.- spawn_resampled_timing(rng)[source]¶
Resample timing while preserving the same conceptual-trial structure.
- Return type:
- crossover(other, seed=1234)[source]¶
Create offspring designs by recombining order or template sequences.
- designmatrix()[source]¶
Build event-level and convolved design matrices for this schedule.
Xnonconvrepresents the realized modeled-event occupancy on the scan grid.Xconvis the HRF-convolved event-regressor matrix used by the estimation efficiency metrics.
- FCalc(weights, Aoptimality=True, confoundorder=3)[source]¶
Compute all requested component scores and their weighted objective.
Optimisation¶
- class neurodesign.Optimisation(experiment, weights, preruncycles, cycles, seed=None, I=4, G=20, R=None, q=0.01, Aoptimality=True, folder=None, outdes=3, convergence=1000, max_candidate_attempts=10000, optimisation='GA')[source]¶
Bases:
objectRun the genetic-algorithm design search loop for a configured experiment.
Typical usage is
optimisation.optimise()followed byoptimisation.selected_design(0)to retrieve the best representative design; seeMIGRATION_2.0.mdfor the full recommended workflow.- check_develop(design, weights=None)[source]¶
Validate and score a candidate design before keeping it.
- add_new_designs(weights=None, R=None)[source]¶
Populate the current generation with newly sampled candidate designs.
- to_next_generation(weights=None, seed=1234, optimisation=None)[source]¶
Advance one generation of the configured search strategy.
- clear(weights=None)[source]¶
Reset the current population while preserving the last best design.
The preserved design’s cached
F/Fe/Fd/Fc/Ffwere computed under whichever weight vector was active in the phase that just ended (e.g. a Fe-only or Fd-only calibration prerun). Comparing that stale score directly against designs freshly scored under a different weight vector is invalid – component scores live on different numeric scales, so a leftover Fe-phase score can spuriously outrank every real candidate in a following Fd-phase (or vice versa). Rescoring the preserved design underweights(the vector that will govern the upcoming phase, defaulting toself.weights) keeps it on the same footing as the rest of the population.
- optimise()[source]¶
Run the full optimisation procedure, including normalization passes.
If
Fc/Ffare uncalibrated (FcMax/FfMaxleft at1), they are calibrated analytically first. IfFe/Fdhave positive weight and are uncalibrated (FeMax/FdMaxleft at1), a shortpreruncycles-generation prerun optimising that metric alone is run to estimate its calibration reference. The main search then runs forcyclesgenerations (or untilconvergencetriggers early stopping).- Returns:
self, withdesignsholding the final generation andbestdesignholding the single highest-scoring design found. Callevaluate()(orselected_design(), which calls it automatically) to obtain clustered representative outputs.- Return type:
- selected_design(rank=0)[source]¶
Return one evaluated representative design from the current selected outputs.
This is the recommended public entry point for retrieving a design after
optimise(); it callsevaluate()automatically the first time it is needed.- Parameters:
rank (
int) – Index into theoutdesclustered representative designs (0-indexed). Each cluster’s representative is the highest-scoring design within that cluster; clusters are not necessarily ordered by score, sorank=0is one representative design rather than guaranteed to be the single global best (usebestdesignfor that).- Returns:
The representative design for the requested cluster rank.
- Return type:
- evaluate()[source]¶
Cluster final designs and choose representative reported outputs.
Clusters the final generation’s designs into
outdesgroups via k-means on their convolved design matrices, then within each cluster keeps the highest-scoring design as that cluster’s representative. Reordersself.designsso cluster representatives are retrievable viaself.out(populated here) andselected_design().- Returns:
self, withdesignsreordered by cluster,outholding each cluster’s representative-design index,clusholding each reordered design’s cluster label, andcovholding the pairwise design-signal correlation matrix.- Return type:
Utility Modules¶
generate¶
- neurodesign.generate.order(nstim, ntrials, probabilities, ordertype, seed=None, rng=None)[source]¶
Sample a flat stimulus order for legacy one-event design modes.
Parameters define the number of stimulus categories, desired sequence length, target category probabilities, and the sampling strategy (random, blocked, or msequence).
- neurodesign.generate.iti(ntrials, model, min=None, mean=None, max=None, lam=None, resolution=0.1, seed=None, rng=None)[source]¶
Sample a legacy event-aligned ITI vector.
The returned vector always has length
ntrialsand begins with0so that elementican be interpreted as the gap immediately preceding thei-th event in the historical API.
msequence¶
Generate m sequences.
Loosely translated from http://fmriserver.ucsd.edu/ttliu
- class neurodesign.msequence.Msequence[source]¶
Bases:
objectCreate instance for an order of experimental trials.
- GenMseq(mLen, stimtypeno, seed)[source]¶
Generate a random msequence given the length of the desired sequence and the number of different values.
- Mseq(baseVal, powerVal, shift=None, whichSeq=None, userTaps=None)[source]¶
Generate a specific msequence given the base and power values.