def run_freesurfer(subject_id, T1_images, subjects_dir, T2_image=None): """Run freesurfer, convert to nidm and extract stats """ from nipype import freesurfer as fs from nipype import Node from fs_dir_to_graph import to_graph from query_convert_fs_stats import get_collections, process_collection recon = Node(fs.ReconAll(), name='recon') recon.inputs.T1_files = T1_images recon.inputs.subject_id = subject_id recon.inputs.subjects_dir = subjects_dir recon.inputs.openmp = 4 if T2_image: recon.inputs.T2_file = T2_image recon.base_dir = os.path.abspath(os.path.join('working', subject_id)) results = recon.run() provgraph = results.provenance newgraph = to_graph( os.path.join(results.outputs.subjects_dir, results.outputs.subject_id)) provgraph.add_bundle(newgraph) provgraph.rdf().serialize('test1.ttl', format='turtle') results = get_collections(provgraph.rdf()) collections = [] for row in results: collections.append(str(row[0])) if len(collections) > 1: raise ValueError('More than one freesurfer directory collection found') provgraph, termsrdf = process_collection(provgraph, collections.pop()) rdfgraph = provgraph.rdf() + termsrdf return provgraph, rdfgraph
def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir): """ """ import nipype if with_prov: from nipype import config config.enable_provenance() from nipype import Node from nipype.interfaces.dcm2nii import Dcm2niix item_dicoms = list(map(op.abspath, item_dicoms)) # absolute paths dicom_dir = op.dirname(item_dicoms[0]) if item_dicoms else None convertnode = Node(Dcm2niix(), name='convert') convertnode.base_dir = tmpdir convertnode.inputs.source_dir = dicom_dir convertnode.inputs.out_filename = op.basename(op.dirname(prefix)) if nipype.__version__.split('.')[0] == '0': # deprecated since 1.0, might be needed(?) before convertnode.inputs.terminal_output = 'allatonce' else: convertnode.terminal_output = 'allatonce' convertnode.inputs.bids_format = bids eg = convertnode.run() # prov information prov_file = prefix + '_prov.ttl' if with_prov else None if prov_file: safe_copyfile( op.join(convertnode.base_dir, convertnode.name, 'provenance.ttl'), prov_file) return eg, prov_file
def test_neuropythy_atlas(): n = Node(function_neuropythy_atlas, 'atlas') n.base_dir = str(ANALYSIS_PATH) n.inputs.subject_id = 'sub-delft' n.inputs.subjects_dir = str(FREESURFER_PATH) n.run()
def embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids, prov_file, scaninfo, tempdirs, with_prov): """ Enhance sidecar information file with more information from DICOMs Parameters ---------- bids_options item_dicoms outname outname_bids prov_file scaninfo tempdirs with_prov Returns ------- """ from nipype import Node, Function tmpdir = tempdirs(prefix='embedmeta') # We need to assure that paths are absolute if they are relative item_dicoms = list(map(op.abspath, item_dicoms)) embedfunc = Node(Function(input_names=[ 'dcmfiles', 'niftifile', 'infofile', 'bids_info', ], function=embed_dicom_and_nifti_metadata), name='embedder') embedfunc.inputs.dcmfiles = item_dicoms embedfunc.inputs.niftifile = op.abspath(outname) embedfunc.inputs.infofile = op.abspath(scaninfo) embedfunc.inputs.bids_info = load_json( op.abspath(outname_bids)) if (bids_options is not None) else None embedfunc.base_dir = tmpdir cwd = os.getcwd() lgr.debug("Embedding into %s based on dicoms[0]=%s for nifti %s", scaninfo, item_dicoms[0], outname) try: if op.lexists(scaninfo): # TODO: handle annexed file case if not op.islink(scaninfo): set_readonly(scaninfo, False) res = embedfunc.run() set_readonly(scaninfo) if with_prov: g = res.provenance.rdf() g.parse(prov_file, format='turtle') g.serialize(prov_file, format='turtle') set_readonly(prov_file) except Exception as exc: lgr.error("Embedding failed: %s", str(exc)) os.chdir(cwd)
def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir): """ """ import nipype if with_prov: from nipype import config config.enable_provenance() from nipype import Node from nipype.interfaces.dcm2nii import Dcm2niix item_dicoms = list(map(op.abspath, item_dicoms)) # absolute paths dicom_dir = op.dirname(item_dicoms[0]) if item_dicoms else None convertnode = Node(Dcm2niix(), name='convert') convertnode.base_dir = tmpdir convertnode.inputs.source_names = item_dicoms convertnode.inputs.out_filename = op.basename(op.dirname(prefix)) if nipype.__version__.split('.')[0] == '0': # deprecated since 1.0, might be needed(?) before convertnode.inputs.terminal_output = 'allatonce' else: convertnode.terminal_output = 'allatonce' convertnode.inputs.bids_format = bids eg = convertnode.run() # prov information prov_file = prefix + '_prov.ttl' if with_prov else None if prov_file: safe_copyfile(op.join(convertnode.base_dir, convertnode.name, 'provenance.ttl'), prov_file) return eg, prov_file
def run_freesurfer(subject_id, T1_images, subjects_dir, T2_image=None): """Run freesurfer, convert to nidm and extract stats """ from nipype import freesurfer as fs from nipype import Node from fs_dir_to_graph import to_graph from query_convert_fs_stats import get_collections, process_collection recon = Node(fs.ReconAll(), name='recon') recon.inputs.T1_files = T1_images recon.inputs.subject_id = subject_id recon.inputs.subjects_dir = subjects_dir recon.inputs.openmp = 4 if T2_image: recon.inputs.T2_file = T2_image recon.base_dir = os.path.abspath(os.path.join('working', subject_id)) results = recon.run() provgraph = results.provenance newgraph = to_graph(os.path.join(results.outputs.subjects_dir, results.outputs.subject_id)) provgraph.add_bundle(newgraph) provgraph.rdf().serialize('test1.ttl', format='turtle') results = get_collections(provgraph.rdf()) collections = [] for row in results: collections.append(str(row[0])) if len(collections) > 1: raise ValueError('More than one freesurfer directory collection found') provgraph, termsrdf = process_collection(provgraph, collections.pop()) rdfgraph = provgraph.rdf() + termsrdf return provgraph, rdfgraph
def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconfig=None): """ Converts DICOMs grouped from heuristic using Nipype's Dcm2niix interface. Parameters ---------- item_dicoms : List DICOM files to convert prefix : String Heuristic output path with_prov : Bool Store provenance information bids_options : List or None If not None then output BIDS sidecar JSONs List may contain bids specific options tmpdir : Directory Conversion working directory dcmconfig : File (optional) JSON file used for additional Dcm2niix configuration """ import nipype if with_prov: from nipype import config config.enable_provenance() from nipype import Node from nipype.interfaces.dcm2nii import Dcm2niix item_dicoms = list(map(op.abspath, item_dicoms)) # absolute paths fromfile = dcmconfig if dcmconfig else None if fromfile: lgr.info("Using custom config file %s", fromfile) convertnode = Node(Dcm2niix(from_file=fromfile), name='convert') convertnode.base_dir = tmpdir convertnode.inputs.source_names = item_dicoms convertnode.inputs.out_filename = prefix if nipype.__version__.split('.')[0] == '0': # deprecated since 1.0, might be needed(?) before convertnode.inputs.terminal_output = 'allatonce' else: convertnode.terminal_output = 'allatonce' convertnode.inputs.bids_format = bids_options is not None eg = convertnode.run() # prov information prov_file = prefix + '_prov.ttl' if with_prov else None if prov_file: safe_copyfile( op.join(convertnode.base_dir, convertnode.name, 'provenance.ttl'), prov_file) return eg, prov_file
def embed_metadata_from_dicoms(bids, item_dicoms, outname, outname_bids, prov_file, scaninfo, tempdirs, with_prov, min_meta): """ Enhance sidecar information file with more information from DICOMs Parameters ---------- bids item_dicoms outname outname_bids prov_file scaninfo tempdirs with_prov min_meta Returns ------- """ from nipype import Node, Function tmpdir = tempdirs(prefix='embedmeta') embedfunc = Node(Function(input_names=[ 'dcmfiles', 'niftifile', 'infofile', 'bids_info', 'force', 'min_meta' ], output_names=['outfile', 'meta'], function=embed_nifti), name='embedder') embedfunc.inputs.dcmfiles = item_dicoms embedfunc.inputs.niftifile = op.abspath(outname) embedfunc.inputs.infofile = op.abspath(scaninfo) embedfunc.inputs.min_meta = min_meta if bids: embedfunc.inputs.bids_info = load_json(op.abspath(outname_bids)) else: embedfunc.inputs.bids_info = None embedfunc.inputs.force = True embedfunc.base_dir = tmpdir cwd = os.getcwd() try: if op.lexists(scaninfo): # TODO: handle annexed file case if not op.islink(scaninfo): set_readonly(scaninfo, False) res = embedfunc.run() set_readonly(scaninfo) if with_prov: g = res.provenance.rdf() g.parse(prov_file, format='turtle') g.serialize(prov_file, format='turtle') set_readonly(prov_file) except Exception as exc: lgr.error("Embedding failed: %s", str(exc)) os.chdir(cwd)
def run_bet(T1_image, workdir): """Run freesurfer, convert to nidm and extract stats """ from nipype import fsl from nipype import Node from fs_dir_to_graph import to_graph from query_convert_fs_stats import get_collections, process_collection strip = Node(fsl.BET(), name='skullstripper') strip.inputs.in_file = T1_image strip.base_dir = workdir results = strip.run() provgraph = results.provenance return provgraph
def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir): """ """ if with_prov: from nipype import config config.enable_provenance() from nipype import Node from nipype.interfaces.dcm2nii import Dcm2niix item_dicoms = list(map(op.abspath, item_dicoms)) # absolute paths convertnode = Node(Dcm2niix(), name='convert') convertnode.base_dir = tmpdir convertnode.inputs.source_names = item_dicoms convertnode.inputs.out_filename = op.basename(op.dirname(prefix)) convertnode.inputs.bids_format = bids convertnode.inputs.anon_bids = False return convertnode.run()
def extract_region(atlas_file, label): from nipype.interfaces.base import CommandLine from nipype.pipeline.engine import Node import os from glob import glob node = Node(CommandLine('fslmaths %s -thr %s -uthr %s region_%s.nii.gz' % (atlas_file, label, label, label)), name='extract_roi') cwd = os.getcwd() print("cwd = ", cwd) node.base_dir = cwd node.config = {'execution': {'keep_unnecessary_outputs': 'true'}} node.run() single_region = os.path.realpath( os.path.join(cwd, 'extract_roi', 'region_%s.nii.gz' % label)) print('single region mask file: ', single_region) assert os.path.exists(single_region) return single_region, label, atlas_file
def convert(items, anonymizer=None, symlink=True, converter=None): prov_files = [] tmpdir = mkdtemp() for item in items: if isinstance(item[1], (list, tuple)): outtypes = item[1] else: outtypes = [item[1]] prefix = item[0] print('Converting %s' % prefix) dirname = os.path.dirname(prefix + '.ext') print(dirname) if not os.path.exists(dirname): os.makedirs(dirname) for outtype in outtypes: print(outtype) if outtype == 'dicom': dicomdir = prefix + '_dicom' if os.path.exists(dicomdir): shutil.rmtree(dicomdir) os.mkdir(dicomdir) for filename in item[2]: outfile = os.path.join(dicomdir, os.path.split(filename)[1]) if not os.path.islink(outfile): if symlink: os.symlink(filename, outfile) else: os.link(filename, outfile) elif outtype in ['nii', 'nii.gz']: outname = prefix + '.' + outtype scaninfo = prefix + '_scaninfo.json' if not os.path.exists(outname): from nipype import config config.enable_provenance() from nipype import Function, Node from nipype.interfaces.base import isdefined print converter if converter == 'mri_convert': from nipype.interfaces.freesurfer.preprocess import MRIConvert convertnode = Node(MRIConvert(), name = 'convert') convertnode.base_dir = tmpdir if outtype == 'nii.gz': convertnode.inputs.out_type = 'niigz' convertnode.inputs.in_file = item[2][0] convertnode.inputs.out_file = outname #cmd = 'mri_convert %s %s' % (item[2][0], outname) #print(cmd) #os.system(cmd) res=convertnode.run() elif converter == 'dcm2nii': from nipype.interfaces.dcm2nii import Dcm2nii convertnode = Node(Dcm2nii(), name='convert') convertnode.base_dir = tmpdir convertnode.inputs.source_names = item[2] convertnode.inputs.gzip_output = outtype == 'nii.gz' convertnode.inputs.terminal_output = 'allatonce' res = convertnode.run() if isinstance(res.outputs.converted_files, list): print("Cannot convert dicom files - series likely has multiple orientations: ", item[2]) continue else: shutil.copyfile(res.outputs.converted_files, outname) if isdefined(res.outputs.bvecs): outname_bvecs = prefix + '.bvecs' outname_bvals = prefix + '.bvals' shutil.copyfile(res.outputs.bvecs, outname_bvecs) shutil.copyfile(res.outputs.bvals, outname_bvals) prov_file = prefix + '_prov.ttl' shutil.copyfile(os.path.join(convertnode.base_dir, convertnode.name, 'provenance.ttl'), prov_file) prov_files.append(prov_file) embedfunc = Node(Function(input_names=['dcmfiles', 'niftifile', 'infofile', 'force'], output_names=['outfile', 'meta'], function=embed_nifti), name='embedder') embedfunc.inputs.dcmfiles = item[2] embedfunc.inputs.niftifile = outname embedfunc.inputs.infofile = scaninfo embedfunc.inputs.force = True embedfunc.base_dir = tmpdir res = embedfunc.run() g = res.provenance.rdf() g.parse(prov_file, format='turtle') g.serialize(prov_file, format='turtle') #out_file, meta_dict = embed_nifti(item[2], outname, force=True) os.chmod(outname, 0440) os.chmod(scaninfo, 0440) os.chmod(prov_file, 0440) shutil.rmtree(tmpdir)
def embed_metadata_from_dicoms(bids, item_dicoms, outname, outname_bids, prov_file, scaninfo, tempdirs, with_prov, min_meta): """ Enhance sidecar information file with more information from DICOMs Parameters ---------- bids item_dicoms outname outname_bids prov_file scaninfo tempdirs with_prov min_meta Returns ------- """ from nipype import Node, Function tmpdir = tempdirs(prefix='embedmeta') # We need to assure that paths are absolute if they are relative item_dicoms = list(map(op.abspath, item_dicoms)) embedfunc = Node(Function(input_names=['dcmfiles', 'niftifile', 'infofile', 'bids_info', 'force', 'min_meta'], output_names=['outfile', 'meta'], function=embed_nifti), name='embedder') embedfunc.inputs.dcmfiles = item_dicoms embedfunc.inputs.niftifile = op.abspath(outname) embedfunc.inputs.infofile = op.abspath(scaninfo) embedfunc.inputs.min_meta = min_meta if bids: embedfunc.inputs.bids_info = load_json(op.abspath(outname_bids)) else: embedfunc.inputs.bids_info = None embedfunc.inputs.force = True embedfunc.base_dir = tmpdir cwd = os.getcwd() lgr.debug("Embedding into %s based on dicoms[0]=%s for nifti %s", scaninfo, item_dicoms[0], outname) try: if op.lexists(scaninfo): # TODO: handle annexed file case if not op.islink(scaninfo): set_readonly(scaninfo, False) res = embedfunc.run() set_readonly(scaninfo) if with_prov: g = res.provenance.rdf() g.parse(prov_file, format='turtle') g.serialize(prov_file, format='turtle') set_readonly(prov_file) except Exception as exc: lgr.error("Embedding failed: %s", str(exc)) os.chdir(cwd)
contrasts = [ GainRisk_cond, GainAmb_cond, LossRisk_cond, LossAmb_cond, Risk_vs_Amb, Gain_vs_Loss, gain_total ] modelspec = Node(interface=model.SpecifySPMModel(), name="modelspec") modelspec.inputs.concatenate_runs = False modelspec.inputs.input_units = 'scans' modelspec.inputs.output_units = 'scans' #modelspec.inputs.outlier_files = '/media/Data/R_A_PTSD/preproccess_data/sub-1063_ses-01_task-3_bold_outliers.txt' modelspec.inputs.time_repetition = 1. # make sure its with a dot modelspec.inputs.high_pass_filter_cutoff = 128. ################################################ #modelspec.inputs.subject_info = subjectinfo(subject_id) # run per subject modelspec.base_dir = '/media/Data/work' level1design = pe.Node(interface=spm.Level1Design(), name="level1design") #, base_dir = '/media/Data/work') level1design.inputs.timing_units = modelspec.inputs.output_units level1design.inputs.interscan_interval = 1. level1design.inputs.bases = {'hrf': {'derivs': [0, 0]}} level1design.inputs.model_serial_correlations = 'AR(1)' ####################################################################################################################### # Initiation of a workflow wf = Workflow(name="l1run", base_dir="/media/Data/work") wf.connect([ (infosource, datasource, [('subject_id', 'subject_id')]), (datasource, gunzip, [('func', 'in_file')]), (gunzip, modelspec, [('out_file', 'functional_runs')]),