def rename_reshape_pdt2_scans(quar_dir, is_whicap_V1=False, is_whicap_V2=False , is_whicap_V3=False): files = os.listdir(quar_dir) nifti_paths = [os.path.join(quar_dir, x) for x in files if is_nifti(x)] if is_whicap_V2: for path in nifti_paths: if os.path.basename(path).startswith('x'): os.unlink(path) continue files = os.listdir(quar_dir) nifti_paths = [os.path.join(quar_dir, x) for x in files if is_nifti(x)] if len(nifti_paths) == 1: shape = getshape(nifti_paths[0]) if shape[3] != 2: raise Bad_Nifti_Error(path, "DWI Nifti has {} timepoints but should have 2".format(shape[3])) split = fslsplit() split.split(nifti_paths[0], os.path.join(quar_dir, 'pdt2sp_')) splits1 = [os.path.join(quar_dir, "pdt2sp_{0:04d}.nii.gz".format(x)) for x in range(0, 30)] os.unlink(nifti_paths[0]) return elif len(nifti_paths) == 3: sizes = [(x, os.stat(x).st_size) for x in nifti_paths] sizes.sort(key=lambda x :x[1]) os.unlink(sizes[2][0]) elif len(nifti_paths) == 2: return else: raise Wrong_Number_Of_Files_Error(quar_dir, "Wrong number of DWI files left after pruning stage, expected 1, got {}".format(len(nifti_paths)))
def rename_reshape_asl_scans(quar_dir, is_whicap_V2=False): files = os.listdir(quar_dir) nifti_paths = [os.path.join(quar_dir, x) for x in files if is_nifti(x)] if len(nifti_paths) == 2: if is_whicap_V2: return else: raise Wrong_Number_Of_Files_Error( series_folder.path, "Wrong number of ASL files to reshape and not whicap, expected 1, got {}" .format(len(nifti_paths))) elif len(nifti_paths) == 1: if is_whicap_V2: shape = getshape(nifti_paths[0]) if shape[3] > 60: raise Bad_Nifti_Error( quar_dir, "nifti has {} timepoints, expected 60".format(shape[3])) if shape[2] == 15: reshapeASL(nifti_paths[0], z_voxel_size_multiplier=0.5) os.unlink(nifti_paths[0]) else: split = fslsplit() merge = fslmerge() split.split(nifti_paths[0], os.path.join(quar_dir, 'aslsp_')) splits1 = [ os.path.join(quar_dir, "aslsp_{0:04d}.nii.gz".format(x)) for x in range(0, 30) ] splits2 = [ os.path.join(quar_dir, "aslsp_{0:04d}.nii.gz".format(x)) for x in range(30, 60) ] merge.merge(os.path.join(quar_dir, 'ASLOne.nii.gz'), splits1) merge.merge(os.path.join(quar_dir, 'ASLTwo.nii.gz'), splits2) [os.unlink(file) for file in splits1] [os.unlink(file) for file in splits2] os.unlink(nifti_paths[0]) else: reshapeASL(nifti_paths[0], z_voxel_size_multiplier=1) os.unlink(nifti_paths[0]) else: raise Wrong_Number_Of_Files_Error( quar_dir, "dcm2nii produced {} nifti paths, expected 1 or 2".format( len(nifti_paths)))
def rename_reshape_pdt2_scans(quar_dir, is_whicap_V1=False, is_whicap_V2=False, is_whicap_V3=False): files = os.listdir(quar_dir) nifti_paths = [os.path.join(quar_dir, x) for x in files if is_nifti(x)] if is_whicap_V2: for path in nifti_paths: if os.path.basename(path).startswith('x'): os.unlink(path) continue files = os.listdir(quar_dir) nifti_paths = [os.path.join(quar_dir, x) for x in files if is_nifti(x)] if len(nifti_paths) == 1: shape = getshape(nifti_paths[0]) if shape[3] != 2: raise Bad_Nifti_Error( path, "DWI Nifti has {} timepoints but should have 2".format( shape[3])) split = fslsplit() split.split(nifti_paths[0], os.path.join(quar_dir, 'pdt2sp_')) splits1 = [ os.path.join(quar_dir, "pdt2sp_{0:04d}.nii.gz".format(x)) for x in range(0, 30) ] os.unlink(nifti_paths[0]) return elif len(nifti_paths) == 3: sizes = [(x, os.stat(x).st_size) for x in nifti_paths] sizes.sort(key=lambda x: x[1]) os.unlink(sizes[2][0]) elif len(nifti_paths) == 2: return else: raise Wrong_Number_Of_Files_Error( quar_dir, "Wrong number of DWI files left after pruning stage, expected 1, got {}" .format(len(nifti_paths)))
def rename_reshape_asl_scans(quar_dir, is_whicap_V2=False): files = os.listdir(quar_dir) nifti_paths = [os.path.join(quar_dir, x) for x in files if is_nifti(x)] if len(nifti_paths) == 2: if is_whicap_V2: return else: raise Wrong_Number_Of_Files_Error( series_folder.path, "Wrong number of ASL files to reshape and not whicap, expected 1, got {}".format(len(nifti_paths)), ) elif len(nifti_paths) == 1: if is_whicap_V2: shape = getshape(nifti_paths[0]) if shape[3] > 60: raise Bad_Nifti_Error(quar_dir, "nifti has {} timepoints, expected 60".format(shape[3])) if shape[2] == 15: reshapeASL(nifti_paths[0], z_voxel_size_multiplier=0.5) os.unlink(nifti_paths[0]) else: split = fslsplit() merge = fslmerge() split.split(nifti_paths[0], os.path.join(quar_dir, "aslsp_")) splits1 = [os.path.join(quar_dir, "aslsp_{0:04d}.nii.gz".format(x)) for x in range(0, 30)] splits2 = [os.path.join(quar_dir, "aslsp_{0:04d}.nii.gz".format(x)) for x in range(30, 60)] merge.merge(os.path.join(quar_dir, "ASLOne.nii.gz"), splits1) merge.merge(os.path.join(quar_dir, "ASLTwo.nii.gz"), splits2) [os.unlink(file) for file in splits1] [os.unlink(file) for file in splits2] os.unlink(nifti_paths[0]) else: reshapeASL(nifti_paths[0], z_voxel_size_multiplier=1) os.unlink(nifti_paths[0]) else: raise Wrong_Number_Of_Files_Error( quar_dir, "dcm2nii produced {} nifti paths, expected 1 or 2".format(len(nifti_paths)) )