calc_tsnr 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/python
  2. """
  3. Compute a tSNR volume given an input time series image.
  4. """
  5. import os
  6. import sys
  7. if not len(sys.argv) == 2:
  8. print __doc__
  9. sys.exit(1)
  10. import nibabel as nb
  11. from mvpa2.mappers.detrend import poly_detrend
  12. from mvpa2.datasets.mri import fmri_dataset
  13. from mvpa2.datasets.mri import map2nifti
  14. infilename = sys.argv[1]
  15. outfilename = os.path.splitext(infilename)[0]
  16. if outfilename.endswith('.nii'):
  17. outfilename = os.path.splitext(outfilename)[0]
  18. outfilename += '_tsnr.nii.gz'
  19. ds = fmri_dataset(infilename)
  20. # clip the value range, can only be negative due to interpolation
  21. # regime
  22. ds.samples[ds.samples < 0] = 0
  23. # first capture mean -- detrending will bring it close to zero
  24. m = ds.samples.mean(axis=0)
  25. # now detrend
  26. poly_detrend(ds, polyord=1)
  27. # and get the std after linear trend removal
  28. std = ds.samples.std(axis=0)
  29. # compute tSNR
  30. m[std > 0] /= std[std > 0]
  31. # protect against ultra-low noise voxels (images are zero'ed at the edges)
  32. m[m > 1000] = 1000
  33. map2nifti(ds, m).to_filename(outfilename)