def reorganize_preproc_files(outdir, sid, proto, image_basename, tmp_capsul, overwrite=False): """ Perform the tree reorganization """ #describe reorganization processing_struct = { "1-BET": ["{}_brain_mask.nii.gz", "{}_brain.nii.gz"], "5-SliceTiming": ["au{}.nii", "pyscript_slicetiming.m"], "6-Realign": [ "rp_au{}.txt", "rau{}.nii", "pyscript_realign.m", "meanau{}.nii", "au{}.mat" ], "8-Normalize": ["wmeanau{}.nii", "pyscript_normalize.m", "meanau{}_sn.mat"], "9-Normalize": ["wau{}.nii", ["pyscript_normalize.m", "pyscript_apply_normalize.m"]], "10-Smooth": ["pyscript_smooth.m", "swau{}.nii"] } # clean target entry gzdestpath = os.path.join(outdir, sid, proto) if os.path.exists(gzdestpath): if overwrite: shutil.rmtree(gzdestpath) else: raise Exception("Directory already exists: %s" % gzdestpath) os.makedirs(gzdestpath) # loop on items for step_name, step_fnames in processing_struct.items(): for base in step_fnames: if type(base) == list: basename = base[0] rename = base[1] else: basename = base rename = base basename = basename.format(image_basename) rename = rename.format(image_basename) srcpath = os.path.join(tmp_capsul, step_name, basename) if srcpath.endswith(".nii"): gzip_file(srcpath, prefix="", output_directory=gzdestpath) print "****gzip ", srcpath, gzdestpath else: destpath = os.path.join(gzdestpath, rename) print "****mv ", srcpath, destpath if os.path.exists(srcpath): shutil.move(srcpath, destpath) print "rm ", tmp_capsul shutil.rmtree(tmp_capsul)
def test_specific_functions(self): """ Test functions execution follow up. """ # element_to_list tests out_list = element_to_list("element_test") self.assertIsInstance(out_list, list) self.assertEqual(out_list, ["element_test"]) # list_to_element tests with self.assertRaises(ValueError): list_to_element([1, 2, 3]) with self.assertRaises(ValueError): list_to_element("no_list") out_element = list_to_element(["singleton_element"]) self.assertIn(out_element, ["singleton_element"]) self.assertIsInstance(out_element, str) # ungzip_file tests out_file = ungzip_file(os.path.join(os.path.dirname( os.path.realpath(__file__)), "test_file.gz"), output_directory=self.outdir, prefix="u") self.assertTrue(os.path.isfile(out_file)) self.assertEqual("u", os.path.basename(out_file)[0]) with self.assertRaises(ValueError): ungzip_file("no_file", output_directory=self.outdir, prefix="u") with self.assertRaises(ValueError): ungzip_file(os.path.join(os.path.dirname( os.path.realpath(__file__)), "test_file.gz"), output_directory="non_existent", prefix="u") # gzip file tests out_file = gzip_file(os.path.join(os.path.dirname( os.path.realpath(__file__)), "test_file.txt"), output_directory=self.outdir, prefix="g") self.assertTrue(os.path.isfile(out_file)) self.assertEqual("g", os.path.basename(out_file)[0]) with self.assertRaises(ValueError): ungzip_file("no_file", output_directory=self.outdir, prefix="g") with self.assertRaises(ValueError): ungzip_file(os.path.join(os.path.dirname( os.path.realpath(__file__)), "test_file.gz"), output_directory="non_existent", prefix="g")
def test_specific_functions(self): """ Test functions execution follow up. """ # element_to_list tests out_list = element_to_list("element_test") self.assertIsInstance(out_list, list) self.assertEqual(out_list, ["element_test"]) # list_to_element tests with self.assertRaises(ValueError): list_to_element([1, 2, 3]) with self.assertRaises(ValueError): list_to_element("no_list") out_element = list_to_element(["singleton_element"]) self.assertIn(out_element, ["singleton_element"]) self.assertIsInstance(out_element, str) # ungzip_file tests out_file = ungzip_file(os.path.join( os.path.dirname(os.path.realpath(__file__)), "test_file.gz"), output_directory=self.outdir, prefix="u") self.assertTrue(os.path.isfile(out_file)) self.assertEqual("u", os.path.basename(out_file)[0]) with self.assertRaises(ValueError): ungzip_file("no_file", output_directory=self.outdir, prefix="u") with self.assertRaises(ValueError): ungzip_file(os.path.join( os.path.dirname(os.path.realpath(__file__)), "test_file.gz"), output_directory="non_existent", prefix="u") # gzip file tests out_file = gzip_file(os.path.join( os.path.dirname(os.path.realpath(__file__)), "test_file.txt"), output_directory=self.outdir, prefix="g") self.assertTrue(os.path.isfile(out_file)) self.assertEqual("g", os.path.basename(out_file)[0]) with self.assertRaises(ValueError): ungzip_file("no_file", output_directory=self.outdir, prefix="g") with self.assertRaises(ValueError): ungzip_file(os.path.join( os.path.dirname(os.path.realpath(__file__)), "test_file.gz"), output_directory="non_existent", prefix="g")
# .m files mfiles = glob.glob(os.path.join(capsulwd, "*", "pyscript*.m")) for mfile in mfiles: shutil.copy(mfile, soutdir) # reg file regfile = os.path.join(capsulwd, "9-covars", "complete_reg_file.txt") shutil.copy(regfile, os.path.join(soutdir, "rp_nuisance_extended.txt")) # EstimateModel niftis estimate_model_dir = os.path.join(capsulwd, "13-EstimateModel") images = [ os.path.join(estimate_model_dir, fname) for fname in ("mask.nii", "RPV.nii", "ResMS.nii") ] for image in images: gzip_file(image, prefix="", output_directory=soutdir, remove_original_file=False) # betas betas = glob.glob(os.path.join(estimate_model_dir, "beta*.nii")) with tarfile.open(os.path.join(soutdir, "beta_files.tar.gz"), 'w:gz') as f: for beta in betas: f.add(beta, arcname=os.path.basename(beta)) # SPM.mat spmmatfile = os.path.join(capsulwd, "14-EstimateContrast", "SPM.mat") gzip_file(spmmatfile, prefix="", output_directory=soutdir, remove_original_file=False) # nii_spmF_images_dir niftis nii_spmF_images_dir = os.path.join(capsulwd, "15-nii_spmF_images") nii_spmF_images_files = []
"bet.fsl_bet": ["{}_brain_mask.nii.gz", "{}_brain.nii.gz"], "spm_slicer": ["au{}.nii", "pyscript_slicetiming.m"], "realign": ["rp_au{}.txt", "rau{}.nii", "pyscript_realign.m", "meanau{}.nii", "au{}.mat"], "spm_normalize_template": ["wmeanau{}.nii", "pyscript_normalize.m", "meanau{}_sn.mat"], "spm_funcnormalize_template": ["wau{}.nii", "pyscript_normalize.m"], "smoothing": ["pyscript_smooth.m", "swau{}.nii"], } else: raise NotImplementedError("Can't reorganize the data.") image_basename = os.path.basename(funcfile).split(".")[0] for step_name, step_fnames in processing_struct.items(): for basename in step_fnames: basename = basename.format(image_basename) srcpath = os.path.join(tmp_capsul, step_name, basename) if srcpath.endswith(".nii"): gzip_file(srcpath, prefix="", output_directory=outdir) else: if (step_name == "spm_funcnormalize_template") and (basename == "pyscript_normalize.m"): destpath = os.path.join(outdir, "pyscript_apply_normalize.m") elif step_name == "": destpath = os.path.join(logdir, "{0}_{1}".format(sid, basename)) else: destpath = os.path.join(outdir, basename) shutil.move(srcpath, destpath) shutil.rmtree(tmp_capsul) """ Create a snap to chack the registration result """ if args.template_mask is not None: snap = os.path.join(outdir, image_basename + ".pdf")
use_spm=True, spm_exec=args.spmbin, spm_standalone=True, use_nipype=True, output_directory=capsulwd) print " ... done." # Get the pipeline pipeline = get_process_instance("clinfmri.utils.spm_new_segment_only.xml") # Configure the pipeline pipeline.channel_files = [args.t1file] #to find the template TPM.nii from the standalone distrib pipeline.spm_dir = args.spmdir # Execute the pipeline study_config.run(pipeline, verbose=1) # Keep only data of interest batch = os.path.join(capsulwd, "3-NewSegment", "pyscript_newsegment.m") images = glob.glob(os.path.join(capsulwd, "1-ungzipfnames", "*.nii")) images = [item for item in images if not os.path.basename(item).startswith(("u", "iy_"))] mat = glob.glob(os.path.join(capsulwd, "1-ungzipfnames", "*.mat"))[0] for path in images: gzip_file(path, prefix="", output_directory=soutdir, remove_original_file=False) shutil.copy(mat, soutdir) shutil.rmtree(capsulwd)