Utilities used by multiple subpackages.
Simulate fMRI data for a single subject.
This code provides a set of functions necessary to produce realistic simulations of neural data.
generate_signal Specify the volume (or volumes) which represent the signal in the neural data.
generate_stimfunction Create a function to describe the stimulus onsets/durations
export_stimfunction: Generate a three column timing file that can be used with software like FSL
double_gamma_hrf Convolve the stimulus function with the HRF to model when events are expected.
apply_signal Combine the volume and the HRF
calc_noise Estimate the noise properties of a given volume
generate_noise Create the noise for this run. This creates temporal, task and white noise. Various parameters can be tuned depending on need
mask_brain Mask the volume to look like a volume. Based on MNI standard space
plot_brain Show the brain as it unfolds over time with a given opacity.
Authors: Cameron Ellis (Princeton) 2016
generate_signal(dimensions, feature_coordinates, feature_size, feature_type, signal_magnitude=, signal_constant=1)¶
Generate volume containing signal
Generate signal in specific regions of the brain with for a single volume. This will then be convolved with the HRF across time
- dimensions (3 length array, int) – What are the dimensions of the volume you wish to create
- feature_coordinates (multidimensional array, int) – What are the feature_coordinates of the signal being created. Be aware of clipping: features far from the centre of the brain will be clipped. If you wish to have multiple features then list these as an features x 3 array. To create a signal of a specific shape then supply all the individual feature_coordinates and set the feature_size to 1.
- feature_size (list, int) – How big is the signal. If m=1 then only one value is accepted, if m>1 then either one value must be supplied or m values
- feature_type (list, string) – What feature_type of signal is being inserted? Options are cube, loop, cavity, sphere. If features = 1 then only one value is accepted, if features > 1 then either one value must be supplied or m values
- signal_magnitude (list, float) – What is the (average) magnitude of the signal being generated? A value of 1 means that the signal is one standard deviation from the noise
- signal_constant (list, bool) – Is the signal constant or is it a random pattern (with the same average magnitude)
volume_static – Creates a single volume containing the signal
3 dimensional array, float
generate_stimfunction(onsets, event_durations, total_time, weights=, timing_file=None)¶
Return the function for the onset of events
When do stimuli onset, how long for and to what extent should you resolve the fMRI time course. There are two ways to create this, either by supplying onset, duration and weight information or by supplying a timing file (in the three column format)
- onsets (list, int) – What are the timestamps for when an event you want to generate onsets?
- event_durations (list, int) – What are the durations of the events you want to generate? If there is only one value then this will be assigned to all onsets
- total_time (int) – How long is the experiment in total.
- weights (list, float) – How large is the box car for each of the onsets. If there is only one value then this will be assigned to all onsets
- timing_file (string) – The filename (with path) to a three column timing file (FSL) to make the events. Still requires tr_duration and total_time
The time course of stimulus related activation
Output a tab separated timing file
This produces a three column tab separated text file, with the three columns representing onset time, event duration and weight, respectively
Useful if you want to run the simulated data through FEAT analyses
- stimfunction (list) – The stimulus function describing the time course of events
- filename (str) – The name of the three column text file to be output
double_gamma_hrf(stimfunction, tr_duration, response_delay=6, undershoot_delay=12, response_dispersion=0.9, undershoot_dispersion=0.9, response_scale=1, undershoot_scale=0.035)¶
Return a double gamma HRF
- stimfunction (list, bool) – What is the time course of events to be modelled in this experiment
- response_delay (float) – How many seconds until the peak of the HRF
- undershoot_delay (float) – How many seconds until the trough of the HRF
- response_dispersion (float) – How wide is the rising peak dispersion
- undershoot_dispersion (float) – How wide is the undershoot dispersion
- response_scale (float) – How big is the response relative to the peak
- undershoot_scale (float) – How big is the undershoot relative to the trough
The time course of the HRF convolved with the stimulus function
one dimensional array
Apply the convolution and stimfunction
Apply the convolution of the HRF and stimulus time course to the volume.
- signal_function (list, float) – The one dimensional timecourse of the signal over time. Found by convolving the HRF with the stimulus time course.
- volume_static (multi dimensional array, float) –
Generates the spatial noise volume for these parameters
multidimensional array, float
Calculates the noise properties of the volume supplied. This estimates what noise properties the volume has. For instance it determines the spatial smoothness, the autoregressive noise, system noise etc. Read the doc string for generate_noise to understand how these different types of noise interact.
- volume (4d numpy array, float) – Take in a functional volume (either the file name or the numpy array) to be used to estimate the noise properties of this
- mask (4d numpy array, float) – The mask to be used, the same size as the volume
- Return a dictionary of the calculated noise parameters of the provided
generate_noise(dimensions, stimfunction, tr_duration, mask=None, noise_dict=None)¶
Generate the noise to be added to the signal. Default noise parameters will create a noise volume with a standard deviation of 0.1 (where the signal defaults to a value of 1). This has built into estimates of how different types of noise mix. All noise values can be set by the user
- dimensions (n length array, int) – What is the shape of the volume to be generated
- stimfunction (Iterable, bool) – When do the stimuli events occur
- tr_duration (float) – What is the duration, in seconds, of each TR?
- mask (4d array, float) – The mask of the brain volume, using
- noise_dict (dictionary, float) – This is a dictionary that must contain the key: overall. If there are no other variables provided then it will default values
Generates the noise volume for these parameters
multidimensional array, float
mask_brain(volume, mask_name=None, mask_threshold=0.1, mask_self=0)¶
Mask the simulated volume This takes in a volume and will output the masked volume. if a one dimensional array is supplied then the output will be a volume of the dimensions specified in the array. The mask can be created from the volume by averaging it. All masks will be bounded to the range of 0 to 1.
- volume (multidimensional array) – Either numpy array of the volume that has been simulated or a tuple describing the dimensions of the mask to be created
- mask_name (str) – What is the path to the mask to be loaded? If empty then it defaults to an MNI152 grey matter mask.
- mask_threshold (float) – What is the threshold (0 -> 1) for including a voxel in the mask?
- mask_self (bool) – If set to 1 then it makes a mask from the volume supplied (by averaging across time points and changing the range).
mask – The masked brain
multidimensional array, float
plot_brain(fig, brain, mask=None, percentile=99)¶
Display the brain that has been generated with a given threshold
- fig (matplotlib object) – The figure to be displayed, generated from matplotlib. import matplotlib.pyplot as plt; fig = plt.figure()
- brain (3d array) – This is a 3d array with the neural data
- mask (3d array) – A binary mask describing the location that you want to specify as
- percentile (float) – What percentage of voxels will be included? Based on the values supplied
ax – Object with the information to be plotted
ReadDesign(fname=None, include_orth=True, include_pols=True)¶
A class which has the ability of reading in design matrix in .1D file, generated by AFNI’s 3dDeconvolve.
- fname (string, the address of the file to read.) –
- include_orth (Boollean, whether to include "orthogonal" regressors in) – the nuisance regressors which are usually head motion parameters. All the columns of design matrix are still going to be read in, but the attribute cols_used will reflect whether these orthogonal regressors are to be included for furhter analysis. Note that these are not entered into design_task attribute which include only regressors related to task conditions.
- include_pols (Boollean, whether to include polynomial regressors in) – the nuisance regressors which are used to capture slow drift of signals.
2d array. The design matrix read in from the csv file.
2d array. The part of design matrix corresponding to – task conditions.
number of total columns in the design matrix.
1d array. the types of each column in the design matrix. – 0 for orthogonal regressors (usually head motion parameters), -1 for polynomial basis (capturing slow drift of signals), values > 0 for stimulus conditions
scalar. The number of polynomial bases in the designn matrix.
scalar. The number of stimulus conditions.
scalar. The number of orthogoanal regressors (usually head – motions)
list. The names of each column in the design matrix.
Construct a numpy array by stacking arrays in a list
- data (list of arrays, arrays have same shape in all but one dimension or) –
- are None (elements) – The list of arrays to be concatenated.
- axis (int, default = 0) – Axis for the concatenation
data_stacked – The resulting concatenated array.
- Calculate the correlation matrix based on a
- covariance matrix
Parameters: cov (2D array) – Returns: corr – correlation converted from the covarince matrix Return type: 2D array
to invert a 2D matrix
Parameters: a (2D array) – Returns: inva – inverse of input matrix a Return type: 2D array Raises:
LinAlgError– If a is singular or not square
- convert a 2D symmetric matrix to an upper
- triangular matrix in 1D format
Parameters: symm (2D array) – Symmetric matrix Returns: tri – Contains elements of upper triangular matrix Return type: 1D array
- convert a upper triangular matrix in 1D format
- to 2D symmetric matrix
- tri (1D array) – Contains elements of upper triangular matrix
- dim (int) – The dimension of target matrix.
symm – Symmetric matrix in shape=[dim, dim]
Compute the sum of exponents for a list of samples
Parameters: data (array, shape=[features, samples]) – A data array containing samples. Returns:
- result_sum (array, shape=[samples,]) – The sum of exponents for each sample divided by the exponent of the maximum feature value in the sample.
- max_value (array, shape=[samples,]) – The maximum feature value for each sample.
- result_exp (array, shape=[features, samples]) – The exponent of each element in each sample divided by the exponent of the maximum feature value in the sample.
This function is more stable than computing the sum(exp(v)). It useful for computing the softmax_i(v)=exp(v_i)/sum(exp(v)) function.