Skip to main content

Timeseries variance analysis for fMRI

Project description

Tsvarana

PyPI version | Build Status | GitHub | GitHub top language

About

Tsvarana performs (t)ime(s)eries (var)iance (ana)lysis for fMRI data, written in Python 3. It is a diagnostic tool intended to detect abnormal fluctuations in MRI signals, that you may wish to remove from further analysis.

The logic behind Tsvarana is based on a simple observation: abnormal signal intensities in fMRI data caused by non-physiological sources of noise stand out by being very different from the median signal intensity across a timeseries. A well-designed fMRI experiment will elicit BOLD signal fluctuations in the order of 1-4% signal change, with changes outside this range likely attributable to non-physiological sources of noise. Thus, by computing the voxelwise variance between signal intensity at every timepoint against the median signal intensity, we can detect abnormal voxels, slices or volumes for further inspection or removal.

Tsvarana is a spiritual successor to tsdiffana, written by Matthew Brett. The two tools differ in some important aspects. First, signal assessment is carried out by comparing the signal variance in the time domain against the median signal amplitude. This approach is preferable to a sliding window approach, as it is more robust to cases of sustained abnormal intensities spanning consecutive timepoints. Second, timepoint removal or 'scrubbing' is performed iteratively. The iteration algorithm is defined as follows; (a) check all timepoint-to-median variances against a user-defined maximum variance threhold, (b) remove all timepoints failing the test, (c) replace removed timepoints with the mean of the first preceeding and first following retained timepoints, where available. Steps (a)-(c) are repeated until no further timepoints are marked for removal.

Tsvarana is experimental software and is provided with no warranty of any kind.

Installation

Install with pip

pip install tsvarana

Tsvarana has the following dependencies: NumPy, SciPy, Bokeh, Nibabel

Usage

There are two ways to use Tsvarana. To perform a basic timeseries variance analysis and scrubbing, you can invoke tsvarana from the shell with

python -m tsvarana --data <4d_nifti>

Alternatively, you can use the tsvarana library from within a Python environment

import tsvarana

See below for a step-by-step tutorial.

Tutorial

# Load library
import tsvarana

# Start by loading a 4D BOLD timeseries to the workspace. We will use the nibabel library for this
# This example dataset contains (x,y,z,t) dimensions
import nibabel as nib
header = nib.load('my_4D_data.nii.gz')
data = header.get_fdata()

# Define a variance analysis object
varana = tsvarana.classes.varana()

# First, we will define the spatial unit. Variance will be calculated at the specified spatial level, 
# which can be 'voxel', 'slice' or 'volume'. If slice is selected, you must specify the dimension 
# axis along which slices are taken
# For our example dataset with (x,y,z,t) dimensions, we'll pick slices along the z dimension
varana.spatial_unit = 'slice'
varana.slice_axis = 2

# We can also modify the axis along which time is stored
# In our example of (x,y,z,t) data, that will be the 3rd axis
varana.time_axis = 3

# Next, define the variance threshold, expressed in normalised variance units
# A value in the range of 5-10 is a reasonable starting point for fMRI data
# Note this threshold must be selected while considering the nature of your data acquisition
# protocol, and the type of signal abnormalities you wish to detect
varana.var_threshold = 5

# Perform diagnostics
# Calculate the timepoint-to-median variance using the settings specified above, and compare the
# empirical normalised variance against the previously-specified variance threshold
varana.detect(data)

# Plot the resulting variance diagnostics
tsvarana.plot_diagnostic(varana, show=True)

# Perform iterative scrubbing
# This step iterates the timepoint removal algorithm, until no timepoints display normalised
# variance-to-median above the specified threshold. Note that timepoints removed are replaced
# with the mean of the first valid preceeding and first valid following timepoint. If ony one
# is valid, a copy of that timepoint is used for replacement
varana.scrub_iterative(data)

# Plot every step of the diagnostic variance calculation
# This will create a HTML page, containing one figure for each iteration pass
tsvarana.plot_diagnostic(varana, outfile='variance.html')

# Plot every step of the binary thresholding
# As above, This will make one figure for each iteration pass
tsvarana.plot_regressor(varana, outfile='regressors.html')

# We can also obtain a regressor matrix, with all timepoints excluded flagged as 1s and timepoints
# that were never excluded as 0s
regressor = varana.get_final_regressor()

# Finally, we pull out the scrubbed timeseries data, and save it to a NIFTI file
data_scrub = varana.get_data_scrub()
img = nib.Nifti1Image(data_scrub, header.affine)
nib.save(img, 'my_scrubbed_data.nii.gz')

Development

Tsvarana was created and maintained by Ivan Alvarez. To see the code or report a bug, please visit the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tsvarana-0.2.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

tsvarana-0.2.1-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file tsvarana-0.2.1.tar.gz.

File metadata

  • Download URL: tsvarana-0.2.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for tsvarana-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e47f24094585f4d4d75f7da6de0661f45e79bb745efec9b2db1c4662aa5a85ca
MD5 9ef6a4d515643140bc8824e66469847c
BLAKE2b-256 6b3f35130cdcf0430d3934aa150389df311237fb21d43377a228ca1316821bfe

See more details on using hashes here.

File details

Details for the file tsvarana-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: tsvarana-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for tsvarana-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eafa0b85509738029235f4477d86ef2ee18519abcada7d6e78e2dea843ff70d2
MD5 bb40db76a366c1858907ff61fc363ae1
BLAKE2b-256 d9293e471db8270ed06bf37e46d0e2c0882859bf1c4e0ff6632324a1d11a6eec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page