def morphologist_all(t1file, sid, outdir, study="morphologist", waittime=10, somaworkflow=False, spmexec="/i2bm/local/spm8-standalone/run_spm8.sh", spmdir="/i2bm/local/spm8-standalone"): """ Performs all the Morphologist steps. Steps: 1- Ensure image orientation and reorient it if needed (Prepare Subject for Anatomical Pipeline). 2- Computation of a brain mask (Brain Mask Segmentation). 3- Computation of a mask for each hemisphere (Split Brain Mask). 4- A grey/white classification of each hemisphere to perform "Voxel Based Morphometry" (Grey White Classification) and spherical triangulation of cortical hemispheres (Grey White Surface). 5- Spherical triangulation of the external interface of the cortex of one or two hemispheres (Get Spherical Hemi Surface). 6- Computation of a graph representing the cortical fold topography (Cortical Fold Graph). 7- Automatic identification of the cortical sulci (Automatic Sulci Recognition), located in the "sulci" toolbox. The execution is performed with soma_workflow that has to be installed in the bv_env environment. To check the worklow submission, use the 'soma_workflow_gui' command. If the input 't1file' has no the expected extension, an Exception will be raised. If the $outdir/$study/$sid has already been created, an Exception will be raised. Parameters ---------- t1file: str (mandatory) the path to a ".nii.gz" anatomical T1 weighted file. sid: str (mandatory) a subject identifier. outdir: str (mandatory) the morphologist output files will be written in $outdir/$study/$sid. study: str (mandatory) the name of the study. waittime: float (optional, default 10) a delay (in seconds) used to check the worflow status. somaworkflow: bool (optional, default False) if True use somaworkflow for the execution. spmexec: str (optional) the path to the standalone SPM execution file. spmdir: str (optional) the standalone SPM directory. Returns ------- wffile: str a file containing the submitted workflow. wfid: int the submitted workflow identifier. wfstatus: str the submited worflow status afer 'waittime' seconds. """ # Check roughly the input file extension if not t1file.endswith(".nii.gz"): raise Exception("'{0}' is not a COMPRESSED NIFTI file.".format(t1file)) # Create a configuration for the morphologist study study_config = StudyConfig( modules=StudyConfig.default_modules + ["FomConfig", "BrainVISAConfig"]) study_dict = { "name": "morphologist_fom", "input_directory": outdir, "output_directory": outdir, "input_fom": "morphologist-auto-nonoverlap-1.0", "output_fom": "morphologist-auto-nonoverlap-1.0", "shared_fom": "shared-brainvisa-1.0", "spm_directory": spmdir, "use_soma_workflow": True, "use_fom": True, "spm_standalone": True, "use_matlab": False, "volumes_format": "NIFTI gz", "meshes_format": "GIFTI", "use_spm": True, "spm_exec": spmexec, "study_config.somaworkflow_computing_resource": "localhost", "somaworkflow_computing_resources_config": { "localhost": { } } } study_config.set_study_configuration(study_dict) # Create the morphologist pipeline pipeline = get_process_instance( "morphologist.capsul.morphologist.Morphologist") morphologist_pipeline = process_with_fom.ProcessWithFom( pipeline, study_config) morphologist_pipeline.attributes = dict( (trait_name, getattr(morphologist_pipeline, trait_name)) for trait_name in morphologist_pipeline.user_traits()) morphologist_pipeline.attributes["center"] = "morphologist" morphologist_pipeline.attributes["subject"] = sid morphologist_pipeline.create_completion() # Create morphologist expected tree # ToDo: use ImportT1 from axon subjectdir = os.path.join(outdir, study, sid) if os.path.isdir(subjectdir): raise Exception("Folder '{0}' already created.".format(subjectdir)) os.makedirs(os.path.join( subjectdir, "t1mri", "default_acquisition", "default_analysis", "folds", "3.1", "default_session_auto")) os.makedirs(os.path.join( subjectdir, "t1mri", "default_acquisition", "registration")) os.makedirs(os.path.join( subjectdir, "t1mri", "default_acquisition", "segmentation", "mesh")) os.makedirs(os.path.join( subjectdir, "t1mri", "default_acquisition", "tmp")) # Copy T1 file in the morphologist expected location destfile = os.path.join(subjectdir, "t1mri", "default_acquisition", sid + ".nii.gz") shutil.copy(t1file, destfile) # Create source_referential morphologist expected file source_referential = {"uuid": str(soma.uuid.Uuid())} referential_file = os.path.join( subjectdir, "t1mri", "default_acquisition", "registration", "RawT1-{0}_default_acquisition.referential".format(sid)) attributes = "attributes = {0}".format(json.dumps(source_referential)) with open(referential_file, "w") as openfile: openfile.write(attributes) # Create a worflow from the morphologist pipeline workflow = Workflow(name="{0} {1}".format(study, sid), jobs=[]) workflow.root_group = [] # Create the workflow wf = pipeline_workflow.workflow_from_pipeline( morphologist_pipeline.process, study_config=study_config) workflow.add_workflow(wf, as_group="{0}_{1}".format(study, sid)) wffile = os.path.join(subjectdir, "{0}.wf".format(study)) pickle.dump(workflow, open(wffile, "w")) # Execute the workflow with somaworkflow if somaworkflow: controller = WorkflowController() wfid = controller.submit_workflow( workflow=workflow, name="{0}_{1}".format(study, sid)) # Return the worflow status after execution while True: time.sleep(waittime) wfstatus = controller.workflow_status(wfid) if wfstatus not in [ "worklflow_not_started", "workflow_in_progress"]: break # Execute the workflow with subprocess else: # -> construct the ordered list of commands to be executed workflow_repr = workflow.to_dict() graph = Graph() for job in workflow_repr["jobs"]: graph.add_node(GraphNode(job, None)) for link in workflow_repr["dependencies"]: graph.add_link(link[0], link[1]) ordered_nodes = [str(node[0]) for node in graph.topological_sort()] commands = [] jobs = workflow_repr["serialized_jobs"] temporaries = workflow_repr["serialized_temporary_paths"] barriers = workflow_repr["serialized_barriers"] for index in ordered_nodes: if index in jobs: commands.append(jobs[index]["command"]) elif index in barriers: continue else: raise Exception("Unexpected node in workflow.") # -> Go through all commands tmpmap = {} for cmd in commands: # -> deal with temporary files for index, item in enumerate(cmd): if not isinstance(item, basestring): if str(item) not in tmpmap: if str(item) in temporaries: struct = temporaries[str(item)] name = cmd[2].split(";")[1].split()[-1] tmppath = os.path.join( subjectdir, "t1mri", "default_acquisition", "tmp", str(item) + name + struct["suffix"]) tmpmap[str(item)] = tmppath else: raise MorphologistError( "Can't complete command '{0}'.".format( cmd)) cmd[index] = tmpmap[str(item)] # -> execute the command worker = MorphologistWrapper(cmd) worker() if worker.exitcode != 0: raise MorphologistRuntimeError( " ".join(worker.cmd), worker.stderr) wfstatus = "Done" wfid = "subprocess" return wffile, wfid, wfstatus