def main(): try: DATA_PATH = sys.argv[1] except IndexError: raise RuntimeError("Pass data path on command line") N_SLICES = 40 TR = 2.5 SLICE_AXIS = 0 # Figure out slice times. slice_order = np.array(range(0, N_SLICES, 2) + range(1, N_SLICES, 2)) space_to_order = np.argsort(slice_order) time_one_slice = TR / N_SLICES # This time, don't add half a slice - nifti won't accept it. slice_times = space_to_order * time_one_slice subjects = get_subjects(DATA_PATH) for name, subject in subjects.items(): for run in subject['functionals']: fname = run['filename'] print("Fixing functional " + fname) fixnifti.fixup_nifti_file(fname, 'f', TR, SLICE_AXIS, slice_times) # changed filename to fname for anat_fname in subject['anatomicals']: print("Fixing anatomical " + anat_fname) fixnifti.fixup_nifti_file(anat_fname)
def main(): try: DATA_PATH = sys.argv[1] except IndexError: raise RuntimeError("Pass data path on command line") N_SLICES = 40 TR = 2.5 SLICE_AXIS=0 # Figure out slice times. slice_order = np.array(range(0, N_SLICES, 2) + range(1, N_SLICES, 2)) space_to_order = np.argsort(slice_order) time_one_slice = TR / N_SLICES # This time, don't add half a slice - nifti won't accept it. slice_times = space_to_order * time_one_slice subjects = get_subjects(DATA_PATH) for name, subject in subjects.items(): for run in subject['functionals']: fname = run['filename'] print("Fixing functional " + fname) fixnifti.fixup_nifti_file(fname, 'f', TR, SLICE_AXIS, slice_times) for anat_fname in subject['anatomicals']: print("Fixing anatomical " + anat_fname) fixnifti.fixup_nifti_file(anat_fname)
def test_fixup_nifti_file(): # Test fixup_nifti_file function data = np.arange(24).reshape((2, 3, 4)) img = nib.Nifti1Image(data, None) hdr = img.get_header() assert_equal(hdr['pixdim'][4], 1) assert_equal(hdr.get_dim_info(), (None, None, None)) def_aff = img.get_header().get_best_affine() tmpdir = tempfile.mkdtemp() try: fname = os.path.join(tmpdir, 'test.nii') new_fname = os.path.join(tmpdir, 'ftest.nii') nib.save(img, fname) # Just running without params sets the affine fixup_nifti_file(fname) img2 = nib.load(new_fname) assert_almost_equal(img2.get_affine(), def_aff) hdr2 = img2.get_header() assert_almost_equal(hdr2.get_sform(), def_aff) assert_almost_equal(hdr2.get_qform(), def_aff) # set prefix fixup_nifti_file(fname, 'pre') img2 = nib.load(os.path.join(tmpdir, 'pretest.nii')) assert_almost_equal(img2.get_affine(), def_aff) # Set TR fixup_nifti_file(fname, 'f', 2.1) img2 = nib.load(new_fname) hdr2 = img2.get_header() assert_almost_equal(hdr2['pixdim'][4], 2.1, 6) # Set slice axis fixup_nifti_file(fname, 'f', None, 1) img2 = nib.load(new_fname) hdr2 = img2.get_header() assert_equal(hdr2.get_dim_info(), (None, None, 1)) # And slice times times = np.array([0, 1, 2]) * 3 / 2.1 fixup_nifti_file(fname, 'f', 2.1, 1, times) img2 = nib.load(new_fname) hdr2 = img2.get_header() assert_almost_equal(hdr2.get_slice_times(), times) del img, img2 finally: shutil.rmtree(tmpdir)
import os, os.path as op import glob from nipype.utils.filemanip import split_filename import os.path as op import nipype.interfaces.ants as ants import fixnifti # Borrowed from https://github.com/practical-neuroimaging/pna-utils list_to_reg = glob.glob("p0*_maths_rl.nii") for in_file in list_to_reg: path, name, ext = split_filename(in_file) fixnifti.fixup_nifti_file(in_file)