Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

simulator

simulator

Tools to simulate multivariate brain and grid data for testing analysis pipelines.

Classes:

NameDescription
SimulateGridSimulate 2D grid data for testing statistical methods.
SimulatorSimulate fMRI data with realistic spatial and temporal characteristics.

Classes

SimulateGrid

SimulateGrid(*, grid_width = 100, signal_width = 20, n_subjects = 20, sigma = 1, signal_amplitude = None, random_state = None)

Simulate 2D grid data for testing statistical methods.

Creates a 2D grid (e.g., 100x100 pixels) with optional embedded signal regions and Gaussian noise. Useful for testing multiple comparison correction methods, threshold selection, and visualization of statistical maps.

Parameters:

NameTypeDescriptionDefault
grid_widthWidth/height of the square grid (default: 100).100
signal_widthWidth of the embedded signal region (default: 20).20
n_subjectsNumber of simulated subjects (default: 20).20
sigmaStandard deviation of the Gaussian noise (default: 1).1
signal_amplitudeAmplitude of the embedded signal. If None, no signal is added.None
random_stateRandom seed or numpy RandomState for reproducibility.None

Attributes:

NameTypeDescription
dataThe simulated data array of shape (n_subjects, grid_width, grid_width).
t_valuesT-statistic values after fitting.
p_valuesP-values after fitting.
thresholdedThresholded statistical map.
isfitWhether fit() has been called.

Methods:

NameDescription
add_signalAdd a rectangular signal to self.data.
create_maskCreate a mask for where the signal is located in grid.
fitRun a one-sample t-test on self.data.
plot_grid_simulationCreate a plot of the simulations.
run_multiple_simulationsRun multiple simulations to calculate the overall false positive rate.
threshold_simulationThreshold the fitted simulation.

Examples:

>>> from nltools.data.simulator import SimulateGrid
>>> sim = SimulateGrid(signal_amplitude=0.5, random_state=42)
>>> sim.fit()
>>> sim.plot()

Methods

add_signal
add_signal(signal_width = 20, signal_amplitude = 1)

Add a rectangular signal to self.data.

Parameters:

NameTypeDescriptionDefault
signal_widthintwidth of signal box20
signal_amplitudeintintensity of signal1
create_mask
create_mask(signal_width)

Create a mask for where the signal is located in grid.

fit
fit()

Run a one-sample t-test on self.data.

plot_grid_simulation
plot_grid_simulation(threshold, threshold_type, n_simulations = 100, correction = None)

Create a plot of the simulations.

run_multiple_simulations
run_multiple_simulations(threshold, threshold_type, n_simulations = 100, correction = None)

Run multiple simulations to calculate the overall false positive rate.

threshold_simulation
threshold_simulation(threshold, threshold_type, correction = None)

Threshold the fitted simulation.

Parameters:

NameTypeDescriptionDefault
thresholdfloatthreshold to apply to simulationrequired
threshold_typestrtype of threshold to use can be a specific t-value or p-value [‘t’, ‘p’, ‘q’]required

Simulator

Simulator(*, brain_mask = None, output_dir = None, random_state = None)

Simulate fMRI data with realistic spatial and temporal characteristics.

This class provides methods for generating synthetic fMRI data with controlled signal patterns, including Gaussian blobs, multi-subject datasets, and various noise structures. Useful for testing analysis pipelines and power analyses.

Parameters:

NameTypeDescriptionDefault
brain_maskPath to a NIfTI brain mask file, a nibabel image object, or None to use the default MNI template mask.None
output_dirDirectory for saving generated data. Defaults to the current working directory.None
random_stateRandom seed or numpy RandomState for reproducibility.None

Attributes:

NameTypeDescription
brain_maskThe brain mask image used for simulation.
output_dirOutput directory path.
random_stateRandom state for reproducible simulations.

Methods:

NameDescription
create_cov_dataCreate continuous simulated data with covariance within a single region.
create_dataCreate simulated data with discrete intensity levels.
create_ncov_dataCreate continuous simulated data with covariance across multiple regions.
gaussianCreate a 3D gaussian signal normalized to a given intensity.
n_spheresGenerate a set of spheres in the brain mask space.
normal_noiseProduce a normal noise distribution for all points in the brain mask.
sphereCreate a sphere of given radius at some point p in the brain mask.
to_niftiConvert a numpy matrix to the nifti format and assign it the brain_mask’s affine matrix.

Examples:

>>> from nltools.data.simulator import Simulator
>>> sim = Simulator(random_state=42)
>>> # Create a dataset with signal in specific regions
>>> data = sim.create_data(levels=[1, -1, 1, -1], sigma=1, reps=10)

Methods

create_cov_data
create_cov_data(cor, cov, sigma, *, mask = None, reps = 1, n_sub = 1, output_dir = None)

Create continuous simulated data with covariance within a single region.

Parameters:

NameTypeDescriptionDefault
coramount of covariance between each voxel and Y variablerequired
covamount of covariance between voxelsrequired
sigmaamount of noise to addrequired
maskregion where activations are placed (a single mask image); defaults to a sphere if NoneNone
repsnumber of data repetitions1
n_subnumber of subjects to simulate1
output_dirstring path of directory to output data. If None, no data will be writtenNone
create_data
create_data(levels, sigma, *, radius = 5, center = None, reps = 1, output_dir = None)

Create simulated data with discrete intensity levels.

Parameters:

NameTypeDescriptionDefault
levelsvector of intensities or class labelsrequired
sigmaamount of noise to addrequired
radiusvector of radius. Will create multiple spheres if len(radius) > 15
centercenter(s) of sphere(s) of the form [px, py, pz] or [[px1, py1, pz1], ..., [pxn, pyn, pzn]]None
repsnumber of data repetitions useful for trials or subjects1
output_dirstring path of directory to output data. If None, no data will be writtenNone
create_ncov_data
create_ncov_data(cor, cov, sigma, *, masks = None, reps = 1, n_sub = 1, output_dir = None)

Create continuous simulated data with covariance across multiple regions.

Parameters:

NameTypeDescriptionDefault
coramount of covariance between each voxel and Y variable (an int or a vector)required
covamount of covariance between voxels (an int or a matrix)required
sigmaamount of noise to addrequired
masksregion(s) where we will have activations (list if more than one)None
repsnumber of data repetitions1
n_subnumber of subjects to simulate1
output_dirstring path of directory to output data. If None, no data will be writtenNone
gaussian
gaussian(mu, sigma, i_tot)

Create a 3D gaussian signal normalized to a given intensity.

Parameters:

NameTypeDescriptionDefault
muaverage value of the gaussian signal (usually set to 0)required
sigmastandard deviationrequired
i_totsum total of activation (numerical integral over the gaussian returns this value)required
n_spheres
n_spheres(radius, center)

Generate a set of spheres in the brain mask space.

Parameters:

NameTypeDescriptionDefault
radiusvector of radius. Will create multiple spheres if len(radius) > 1required
centera vector of sphere centers of the form [px, py, pz] or [[px1, py1, pz1], ..., [pxn, pyn, pzn]]required
normal_noise
normal_noise(mu, sigma)

Produce a normal noise distribution for all points in the brain mask.

Parameters:

NameTypeDescriptionDefault
muaverage value of the gaussian signal (usually set to 0)required
sigmastandard deviationrequired
sphere
sphere(r, p)

Create a sphere of given radius at some point p in the brain mask.

Parameters:

NameTypeDescriptionDefault
rradius of the sphererequired
ppoint (in coordinates of the brain mask) of the center of the sphererequired
to_nifti
to_nifti(m)

Convert a numpy matrix to the nifti format and assign it the brain_mask’s affine matrix.

Parameters:

NameTypeDescriptionDefault
mthe 3D numpy matrix we wish to convert to .niirequired

Methods