def test_nrrd2nifti(self): PREFIX = pjoin(outdir, 'nrrd2nifti_prefix') # run nrrd2nifti conversion conversion.nifti_write(REFERENCE_NRRD, PREFIX) # load converted output converted_nifti = load(PREFIX + '.nii.gz') converted_nifti_data = converted_nifti.get_data() converted_nifti_affine = converted_nifti.affine converted_nifti_bvals = conversion.read_bvals(PREFIX + '.bval') converted_nifti_bvecs = conversion.read_bvecs(PREFIX + '.bvec') # load converted reference converted_nifti = load(CONVERTED_NIFTI) reference_nifti_data = converted_nifti.get_data() reference_nifti_affine = converted_nifti.affine reference_nifti_bvals = conversion.read_bvals(CONVERTED_BVAL) reference_nifti_bvecs = conversion.read_bvecs(CONVERTED_BVEC) # test equality testing.assert_array_equal(reference_nifti_data, converted_nifti_data) testing.assert_array_equal(reference_nifti_affine, converted_nifti_affine) testing.assert_array_equal(reference_nifti_bvals, converted_nifti_bvals) testing.assert_array_equal(reference_nifti_bvecs, converted_nifti_bvecs)
def nrrd2nifti(imgPath): if imgPath.endswith('.nrrd') or imgPath.endswith('.nhdr'): niftiImgPrefix = imgPath.split('.')[0] nifti_write(imgPath, niftiImgPrefix) return niftiImgPrefix + '.nii.gz' else: return imgPath
def pre_process(input_file, target_list, b0_threshold=50.): from conversion import nifti_write, read_bvals from subprocess import Popen if path.isfile(input_file): # convert NRRD/NHDR to NIFIT as the first step # extract bse.py from just NIFTI later if input_file.endswith(SUFFIX_NRRD) | input_file.endswith(SUFFIX_NHDR): inPrefix = input_file.split('.')[0] nifti_write(input_file) input_file = inPrefix + '.nii.gz' inPrefix = input_file.split('.nii')[0] b0_nii = path.join(inPrefix + '_bse.nii.gz') dwi = nib.load(input_file) if len(dwi.shape) > 3: print("Extracting b0 volume...") bvals = np.array(read_bvals(input_file.split('.nii')[0] + '.bval')) where_b0 = np.where(bvals <= b0_threshold)[0] b0 = dwi.get_data()[..., where_b0].mean(-1) else: print("Loading b0 volume...") b0 = dwi.get_fdata() np.nan_to_num(b0).clip(min=0., out=b0) nib.Nifti1Image(b0, affine=dwi.affine, header=dwi.header).to_filename(b0_nii) target_list.append((b0_nii)) else: print("File not found ", input_file) sys.exit(1)