def run_bet( skip_existing: bool = True ): full_pattern = os.path.join(DATA_DIR, PATTERN) scans = glob.iglob(full_pattern, recursive=True) for scan in scans: print(f'\nCurrent series: {scan}') if skip_existing: print('Checking for existing skull-stripping output...', end='\t') dest = get_default_destination(scan) if skip_existing and os.path.isfile(dest): print(f'\u2714') continue print(f'\u2718') print('Running skull-stripping with BET...') try: bet = Node(BET(robust=True), name='bet_node') bet.inputs.in_file = scan bet.inputs.out_file = dest bet.run() print(f'\u2714\tDone!') except Exception as e: print(f'\u2718') print(e.args) break
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 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 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 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 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_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 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 test_grab_entities_subjects(self): subject_outputs = [{'subject': '01'}, {'subject': '02'}, {'subject': '03'}, {'subject': '04'}, {'subject': '05'}, {'subject': '06'}, {'subject': '07'}, {'subject': '08'}, {'subject': '09'}, {'subject': '10'}, {'subject': '11'}, {'subject': '12'}, {'subject': '13'}] bl = Node(BIDSGrab(bids_dir=TEST_DATASETS['NEW_FMRIPREP_DUMMY']), name="TestBIDSGrab") outs = bl.run() bl_subjects = [{'subject': element['subject']} for element in outs.outputs.entities] self.assertEqual(bl_subjects, subject_outputs) # if __name__ == '__main__': # import cProfile # bl = Node(BIDSGrab(bids_dir=TEST_DATASETS['NEW_FMRIPREP_DUMMY']), name="TestBIDSGrab") # bl.run() # cProfile.run('bl.run()')
def testNodes(): #Node that calls a simple function print("\n ** debug PIPELINE : simple function Node.\n") simpleNode = Node(Function(input_names=["inputParam"], output_names=["simpleResult"], function=simpleFunction), name='simpleNode') simpleNode.inputs.inputParam = 5 tempResult = simpleNode.run() #print(tempResult.outputs) -- this is equvalent to below print(simpleNode.result.outputs) #Node that calls a shell function TEST_DIR = abspath('/home/chidi/') print("\n ** debug PIPELINE : shell function Node.\n") shellPopNode = Node(Function(input_names=["shellCommandList"], output_names=["shellResult"], function=shellFunction), name='shellPopNode') shellPopNode.inputs.shellCommandList = ['ls', TEST_DIR, '-a'] tempResult = shellPopNode.run() #print(tempResult.outputs) -- this is equivalent to below print(shellPopNode.result.outputs) #Node that calls a python module that prints to std print("\n ** debug PIPELINE : python external module Node.\n") PYTHON='python' SCRIPT=abspath('/home/chidi/uabiomed/develop/nipype/nkutils.py') pythonExtNode = Node(Function(input_names=["shellCommandList"], output_names=["shellResult"], function=shellFunction), name='pythonExtNode') pythonExtNode.inputs.shellCommandList = [PYTHON, SCRIPT] tempResult = pythonExtNode.run() #print(tempResult.outputs) -- this is equivalent to below print(pythonExtNode.result.outputs) #Node that calls an imported python module print("\n ** debug PIPELINE : python imported module Node.\n") pythonIntNode = Node(Function(input_names=["r","x","y"], output_names=["shellResult"], function=getArray), name='pythonIntNode') pythonIntNode.inputs.r = 15 pythonIntNode.inputs.x = 3 pythonIntNode.inputs.y = 5 tempResult = pythonIntNode.run() #print(tempResult.outputs) -- this is equivalent to below print(pythonIntNode.inputs) print(pythonIntNode.outputs) print(pythonIntNode.result.outputs)
class CurrentInterface(object): def __init__(self, interface, name): self.nn = Node(interface=interface, name=name) self.output = {} def run(self, inputs, base_dir, dir_nm_el): self.nn.base_dir = os.path.join(base_dir, dir_nm_el) for key, val in inputs.items(): key = key.split(".")[-1] setattr(self.nn.inputs, key, val) #have to set again self._output_dir in case of mapper self.nn._output_dir = os.path.join(self.nn.base_dir, self.nn.name) res = self.nn.run() return res
def rawdataChecker(input_file): # If the input is a single DCM-file instead of a multi-dim-NifTI, we have to fetch all the other files in the series if input_file.endswith('.dcm'): from nipype.interfaces.io import DataFinder from os import path from nipype import Node # Setup a datafinder to find the paths to the specific DICOM files t1FinderNode = Node(DataFinder(), name='t1Finder') t1FinderNode.inputs.match_regex = '.*\.dcm' t1FinderNode.inputs.root_paths = path.split(input_file)[0] return t1FinderNode.run().outputs.out_paths else: return input_file # If other datatype just return the same path
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 rawdataChecker(input_file): # If the input is a single DCM-file instead of a multi-dim-NifTI, we have to fetch all the other files in the series if input_file.endswith('.dcm'): from nipype.interfaces.io import DataFinder from os import path from nipype import Node # Setup a datafinder to find the paths to the specific DICOM files t1FinderNode = Node(DataFinder(), name = 't1Finder') t1FinderNode.inputs.match_regex = '.*\.dcm' t1FinderNode.inputs.root_paths = path.split(input_file)[0] return t1FinderNode.run().outputs.out_paths else: return input_file # If other datatype just return the same path
def selectFile(rootPath=r'D:\其他\老舅财务\allData'): templates = {'path': '*\\*\\*.txt'} # Create SelectFiles node sf = Node(SelectFiles(templates), name='selectfiles') # Location of the dataset folder sf.inputs.base_directory = rootPath # Feed {}-based placeholder strings with values # sf.inputs.subject_id1 = '00[1,2]' # sf.inputs.subject_id2 = '01' # sf.inputs.ses_name = "retest" # sf.inputs.task_name = 'covertverb' path = sf.run().outputs.__dict__['path'] return path
def selectFile( rootPath=r'I:\Data_Code\insomnia\workstation_MVPA_2018_05\FunImgARW1'): templates = {'path': '*\\sw*.nii'} # Create SelectFiles node sf = Node(SelectFiles(templates), name='selectfiles') # Location of the dataset folder sf.inputs.base_directory = rootPath # Feed {}-based placeholder strings with values # sf.inputs.subject_id1 = '00[1,2]' # sf.inputs.subject_id2 = '01' # sf.inputs.ses_name = "retest" # sf.inputs.task_name = 'covertverb' path = sf.run().outputs.__dict__['path'] return path
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 selectFile(rootPath=r'I:\Data_Code\Doctor\RealignParameter'): templates = {'path': '{folder}\\{id}'} # Create SelectFiles node sf = Node(SelectFiles(templates), name='selectfiles') # Location of the dataset folder sf.inputs.base_directory = rootPath # Feed {}-based placeholder strings with values sf.inputs.folder = '*_*' sf.inputs.id = 'FD_Jenkinson_*' # sf.inputs.subject_id2 = '01' # sf.inputs.ses_name = "retest" # sf.inputs.task_name = 'covertverb' path = sf.run().outputs.__dict__['path'] return path
def init_fmridenoise_wf(bids_dir, derivatives: t.List[str], task: t.List[str], session: t.List[str], subject: t.List[str], runs: t.List[int], pipelines_paths: t.Set[str], high_pass=0.008, low_pass=0.08, base_dir='/tmp/fmridenoise', name='fmridenoise_wf'): pipelines_paths = list(pipelines_paths) bids_validate = Node(BIDSValidate(bids_dir=bids_dir, derivatives=derivatives, tasks=task, sessions=session, subjects=subject, runs=runs, pipelines=pipelines_paths), name='BidsValidate') result = bids_validate.run() builder = WorkflowBuilder(bids_dir=bids_dir, subjects=result.outputs.subjects, tasks=result.outputs.tasks, conf_raw=result.outputs.conf_raw, conf_json=result.outputs.conf_json, tr_dic=result.outputs.tr_dict, pipelines_paths=pipelines_paths, high_pass=high_pass, low_pass=low_pass) if result.outputs.fmri_prep: builder.use_fmri_prep(result.outputs.fmri_prep) if result.outputs.fmri_prep_aroma: builder.use_fmri_prep_aroma(result.outputs.fmri_prep_aroma) if result.outputs.sessions: builder.with_sessions(result.outputs.sessions) if result.outputs.runs: builder.with_runs(result.outputs.runs) return builder.build(name, base_dir)
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)
return SC_cap_row_filename, SC_dist_row_filename import numpy as np debugPath = '/Users/srothmei/Desktop/charite/toronto/Adalberto/debug/' roi = 68 subid = 'Adalberto' tracksPath = debugPath wmBorder_file = debugPath + 'wmborder.npy' wmborder = np.load(wmBorder_file) affine_matrix_file = debugPath + 'affine_matrix.npy' affine_matrix = np.load(affine_matrix_file) from nipype import Node from nipype.interfaces.io import DataFinder tckFinder = Node(DataFinder(match_regex='.*\.npy', root_paths=tracksPath), name='tckFinder') res = tckFinder.run() track_files = res.outputs.out_paths # compute_connectivity_row(roi, subid, affine_matrix, wmborder, tracksPath, track_files)
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)
path = f"{base_dir}/derivatives/fmridenoise/sub-{sub_num}" os.makedirs(path, exist_ok=True) out_fname = f"{path}/{basename}_{self.inputs.suffix}{ext}" # TODO: Fix lonely dot if no extension copyfile(in_file, out_fname, copy=True) out_files.append(out_fname) return {'out_file': out_files} # --- TESTS if __name__ == '__main__': from nipype import Node # selector = Node(BIDSSelect(), name="pipeline_selector") # selector.inputs.bids_dir = '/home/finc/Dropbox/Projects/fitlins/BIDS/' # selector.inputs.derivatives = True # selector.inputs.entities = [{'subject': '01'}] # results = selector.run() # print(results.outputs) ds = Node(BIDSDataSink( base_directory= '/home/siegfriedwagner/Documents/git/fmridenoise/dummy_bids', in_file= '/home/siegfriedwagner/Documents/git/fmridenoise/dummy_bids/derivatives/sub-1/test', entities=[{ 'subject': '1' }], suffix='d'), name='ds') ds.run() #print(ds.result)
# Releasing Mr Loggins... dangerZone.setLevel('NOTSET') print('Done!') return SC_cap_row_filename, SC_dist_row_filename import numpy as np debugPath = '/Users/srothmei/Desktop/charite/toronto/Adalberto/debug/' roi = 68 subid = 'Adalberto' tracksPath = debugPath wmBorder_file = debugPath + 'wmborder.npy' wmborder = np.load(wmBorder_file) affine_matrix_file = debugPath + 'affine_matrix.npy' affine_matrix = np.load(affine_matrix_file) from nipype import Node from nipype.interfaces.io import DataFinder tckFinder = Node(DataFinder(match_regex = '.*\.npy', root_paths = tracksPath), name = 'tckFinder') res = tckFinder.run() track_files = res.outputs.out_paths # compute_connectivity_row(roi, subid, affine_matrix, wmborder, tracksPath, track_files)
if __name__ == "__main__": calc_residuals = Node(Function(function=calc_residuals, input_names=[ 'in_file', 'motion_file', 'csf_mask_path', 'wm_mask_path', 'global_signal_flag', 'const', 'check_orthogonality' ], output_names=['out_file_list']), name='calc_residuals') calc_residuals.inputs.in_file = '/mnt/project1/home1/varunk/fMRI/results/resultsABIDE1_1/preprocess/motion_correction_bet/_subject_id_0050009/applyMask/sub-0050009_task-rest_run-1_bold_roi_st_mcf.nii_brain.nii.gz' calc_residuals.inputs.motion_file = '/mnt/project1/home1/varunk/fMRI/results/resultsABIDE1_1/preprocess/_subject_id_0050009/mcflirt/sub-0050009_task-rest_run-1_bold_roi_st_mcf.nii.par' calc_residuals.inputs.csf_mask_path = '/mnt/project1/home1/varunk/fMRI/'+\ 'results/resultsABIDE1_1/preprocess/motion_correction_bet/coreg_reg/'+\ 'atlas_resize_reg_directory/wf_main_masks/wf_tissue_masks/'+\ '_subject_id_0050009/csf_mask/fast__pve_0_flirt_thresh_masked.nii.gz' calc_residuals.inputs.wm_mask_path = '/mnt/project1/home1/varunk/fMRI/'+\ 'results/resultsABIDE1_1/preprocess/motion_correction_bet/coreg_reg/'+\ 'atlas_resize_reg_directory/wf_main_masks/wf_tissue_masks/'+\ '_subject_id_0050009/threshold_wm/fast__pve_2_flirt_thresh.nii.gz' calc_residuals.inputs.global_signal_flag = True calc_residuals.inputs.const = True calc_residuals.inputs.check_orthogonality = True calc_residuals.run()
motscrubdat2 = np.delete(motscrubdat,scrub_vols,axis=3) array_img = nib.Nifti1Image(motscrubdat2, nib.load(cur_dir).affine) nib.save(array_img, '%s/rest.nii.gz'%(output_dir)) else: nib.save(nib.load(cur_dir), '%s/rest.nii.gz'%(output_dir)) ########## 2: Remove first 5 vols of func data os.system("fslroi %s/rest.nii.gz %s/rest_delvol.nii.gz 5 350"%(output_dir,output_dir)) ########## 3: Bias Field Correction inu_n4 = Node(N4BiasFieldCorrection( dimension=3, input_image = '%s/%s/session_1/anat_1/anat.nii.gz'%(data_dir,subnum), output_image = '%s/mprage_inu.nii.gz'%(output_dir)) , name='inu_n4') res1 = inu_n4.run() ########## 4: Skullstripping #anat bet_anat = Node(fsl.BET(frac=0.5, robust=True, output_type='NIFTI_GZ', in_file= '%s/mprage_inu.nii.gz'%(output_dir), out_file = '%s/mprage_inu_bet.nii.gz'%(output_dir)), name="bet_anat") res = bet_anat.run() #func bet_func = Node(fsl.BET(frac=0.5, output_type='NIFTI_GZ', in_file= '%s/rest_delvol.nii.gz'%(output_dir), out_file = '%s/rest_delvol_bet.nii.gz'%(output_dir),
# Import BET from the FSL interface from nipype.interfaces.fsl import BET # Import the Node module from nipype import Node # Create Node bet = Node(BET(frac=0.3), name='bet_node') # Specify node inputs bet.inputs.in_file = '/data/ds000114/sub-01/ses-test/anat/sub-01_ses-test_T1w.nii.gz' bet.inputs.out_file = '/output/node_T1w_bet.nii.gz' res = bet.run()
# Setting up the second level analysis model node level2design = Node(fsl.MultipleRegressDesign(contrasts=contrastList, regressors=dictReg), name='level2design') dofCalc = Node(interface=Function(input_names=['targetDir', 'desMtx'], output_names=['fileOut'], function=DOFCalc), name='dofCalc') # creating datasink to collect outputs datasink = Node( DataSink(base_directory=os.path.join(outDir, 'DOFCalculation')), name='datasink') # directory where we create a DOF file DOFCalc.inputs.targetDir = datasink.inputs.base_directory # passing on the design matrix to the DOF calculation file DOFCalc.inputs.desMtx = dictReg # passing # creating the workflow dofCalc = Workflow(name="dofCalc", base_dir=outDir) # connecting nodes dofCalc.connect(DOFCalc, 'fileOut', datasink, 'fileOut') # running the workflow dofCalc.run()
# String-based templates templates = {'anat': 'vol001/t1/0*nii', 'func': 'vol001/tap*/00*nii'} #templates = {'anat': 'openneuro/ds000254-me/sub-1*/anat/sub*nii', # 'func': 'openneuro/ds000254-me/sub-1*/func/sub*nii'} # Create SelectFiles node sf = Node(SelectFiles(templates), name='selectfiles') #Specify the unchanging pieces of the path sf.inputs.base_directory = proj_dir filelist=sf.run().outputs filelist.func # Magic! Build the default dict with lambda to achieve a callable nested dict easily. img_dict=lambda:defaultdict(img_dict) results = img_dict() for f in filelist.func: print(f) tsnr=TSNR() tsnr.inputs.in_file=f
class PipelineSelector(SimpleInterface): input_spec = PipelineSelectorInputSpecification output_spec = PipelineSelectorOutPutSpecification def _run_interface(self, runtime): js = load_pipeline_from_json(self.inputs.pipeline_path) if not is_valid(js): raise ValueError(""" Json file {} is not a valid pipeline, check schema at fmridenoise.utils.json_validator.py """.format(os.path.basename(self.inputs.pipeline_path))) self._results['pipeline'] = js self._results['pipeline_name'] = js['name'] return runtime # rudimentary test # TODO: Move to this to proper unittests if __name__ == '__main__': from nipype import Node import glob reader = Node(PipelineSelector(), name="pipeline_selector") for path in glob.glob("../pipelines/*"): path = os.path.abspath(path) print(path) reader.inputs.pipeline_path = path pipeline = reader.run() print(pipeline.outputs)
ds.inputs.base_directory = '/data/ds000114' # 使用%02d来设计模板 ds.inputs.template = 'sub-%02d/ses-test/func/*fingerfootlips*.nii.gz' ds.inputs.sort_filelist = True # 指定subject_id的具体数值 ds.inputs.subject_id = [1, 7] results = ds.run() results.outputs # another method from nipype import SelectFiles, Node templates = { 'func': 'sub{subject_id1}\\ses-{ses_name}\\func\\sub-{subject_id2}_ses-{ses_name}_task-{task_name}.gz' } # Create SelectFiles node sf = Node(SelectFiles(templates), name='selectfiles') # Location of the dataset folder sf.inputs.base_directory = r'D:\myCodes\MVPA_LC\Python\study\workstation' # Feed {}-based placeholder strings with values sf.inputs.subject_id1 = '00[1,2]' sf.inputs.subject_id2 = '01' sf.inputs.ses_name = "retest" sf.inputs.task_name = 'covertverb' path = sf.run().outputs.__dict__