def test_signal_extraction_equivalence(tmp_path, nvols, nmasks, ext, factor): nlsignals = str(tmp_path / "nlsignals.tsv") imsignals = str(tmp_path / "imsignals.tsv") vol_shape = (64, 64, 40) img_fname = str(tmp_path / ("img" + ext)) masks_fname = str(tmp_path / ("masks" + ext)) random_data = np.random.random(size=vol_shape + (nvols,)) * 2000 random_mask_data = np.random.random(size=vol_shape + (nmasks,)) < 0.2 nb.Nifti1Image(random_data, np.eye(4)).to_filename(img_fname) nb.Nifti1Image(random_mask_data.astype(np.uint8), np.eye(4)).to_filename( masks_fname ) se1 = nl.SignalExtraction( in_file=img_fname, label_files=masks_fname, class_labels=["a%d" % i for i in range(nmasks)], out_file=nlsignals, ) se2 = im.SignalExtraction( in_file=img_fname, label_files=masks_fname, class_labels=["a%d" % i for i in range(nmasks)], out_file=imsignals, ) tic = time.time() se1.run() toc = time.time() se2.run() toc2 = time.time() tab1 = np.loadtxt(nlsignals, skiprows=1) tab2 = np.loadtxt(imsignals, skiprows=1) assert np.allclose(tab1, tab2) t1 = toc - tic t2 = toc2 - toc assert t2 < t1 / factor
def test_signal_extraction_equivalence(tmpdir, nvols, nmasks, ext, factor): tmpdir.chdir() vol_shape = (64, 64, 40) img_fname = 'img' + ext masks_fname = 'masks' + ext random_data = np.random.random(size=vol_shape + (nvols, )) * 2000 random_mask_data = np.random.random(size=vol_shape + (nmasks, )) < 0.2 nb.Nifti1Image(random_data, np.eye(4)).to_filename(img_fname) nb.Nifti1Image(random_mask_data.astype(np.uint8), np.eye(4)).to_filename(masks_fname) se1 = nl.SignalExtraction(in_file=img_fname, label_files=masks_fname, class_labels=['a%d' % i for i in range(nmasks)], out_file='nlsignals.tsv') se2 = im.SignalExtraction(in_file=img_fname, label_files=masks_fname, class_labels=['a%d' % i for i in range(nmasks)], out_file='imsignals.tsv') tic = time.time() se1.run() toc = time.time() se2.run() toc2 = time.time() tab1 = np.loadtxt('nlsignals.tsv', skiprows=1) tab2 = np.loadtxt('imsignals.tsv', skiprows=1) assert np.allclose(tab1, tab2) t1 = toc - tic t2 = toc2 - toc assert t2 < t1 / factor
def extract_timeseries(SinkTag="connectivity", wf_name="extract_timeseries", modularise=True): ######################################################################## # Extract timeseries ######################################################################## import nipype.interfaces.nilearn as learn import nipype.pipeline as pe import nipype.interfaces.utility as utility import nipype.interfaces.io as io from nipype.interfaces.utility import Function import PUMI.utils.globals as globals import PUMI.utils.QC as qc import os SinkDir = os.path.abspath(globals._SinkDir_ + "/" + SinkTag) if not os.path.exists(SinkDir): os.makedirs(SinkDir) # Identitiy mapping for input variables inputspec = pe.Node( utility.IdentityInterface(fields=[ 'std_func', 'atlas_file', # nii labelmap (or 4D probmaps) 'labels', # list of short names to regions 'modules' # list of modules of regions ]), name='inputspec') # re-label atlas, so that regions corresponding to the same modules follow each other if modularise: relabel_atls = pe.Node(interface=Function( input_names=['atlas_file', 'modules', 'labels'], output_names=[ 'relabelled_atlas_file', 'reordered_modules', 'reordered_labels', 'newlabels_file' ], function=relabel_atlas), name='relabel_atlas') # Save outputs which are important ds_nii = pe.Node(interface=io.DataSink(), name='ds_relabeled_atlas') ds_nii.inputs.base_directory = SinkDir ds_nii.inputs.regexp_substitutions = [("(\/)[^\/]*$", ".nii.gz")] # Save outputs which are important ds_newlabels = pe.Node(interface=io.DataSink(), name='ds_newlabels') ds_newlabels.inputs.base_directory = SinkDir ds_newlabels.inputs.regexp_substitutions = [("(\/)[^\/]*$", ".tsv")] extract_timesereies = pe.MapNode( interface=learn.SignalExtraction(detrend=False), iterfield=['in_file'], name='extract_timeseries') # Save outputs which are important ds_txt = pe.Node(interface=io.DataSink(), name='ds_txt') ds_txt.inputs.base_directory = SinkDir ds_txt.inputs.regexp_substitutions = [("(\/)[^\/]*$", wf_name + ".tsv")] #QC timeseries_qc = qc.regTimeseriesQC("regional_timeseries", tag=wf_name) outputspec = pe.Node(utility.IdentityInterface(fields=[ 'timeseries_file', 'relabelled_atlas_file', 'reordered_modules', 'reordered_labels' ]), name='outputspec') # Create workflow analysisflow = pe.Workflow(wf_name) analysisflow.connect(inputspec, 'std_func', extract_timesereies, 'in_file') if modularise: analysisflow.connect(inputspec, 'atlas_file', relabel_atls, 'atlas_file') analysisflow.connect(inputspec, 'modules', relabel_atls, 'modules') analysisflow.connect(inputspec, 'labels', relabel_atls, 'labels') analysisflow.connect(relabel_atls, 'relabelled_atlas_file', extract_timesereies, 'label_files') analysisflow.connect(relabel_atls, 'reordered_labels', extract_timesereies, 'class_labels') analysisflow.connect(relabel_atls, 'reordered_modules', timeseries_qc, 'inputspec.modules') analysisflow.connect(relabel_atls, 'relabelled_atlas_file', timeseries_qc, 'inputspec.atlas') analysisflow.connect(relabel_atls, 'relabelled_atlas_file', ds_nii, 'atlas_relabeled') analysisflow.connect(relabel_atls, 'newlabels_file', ds_newlabels, 'atlas_relabeled') analysisflow.connect(relabel_atls, 'relabelled_atlas_file', outputspec, 'relabelled_atlas_file') analysisflow.connect(relabel_atls, 'reordered_labels', outputspec, 'reordered_labels') analysisflow.connect(relabel_atls, 'reordered_modules', outputspec, 'reordered_modules') else: analysisflow.connect(inputspec, 'atlas_file', extract_timesereies, 'label_files') analysisflow.connect(inputspec, 'labels', extract_timesereies, 'class_labels') analysisflow.connect(inputspec, 'modules', timeseries_qc, 'inputspec.modules') analysisflow.connect(inputspec, 'atlas_file', timeseries_qc, 'inputspec.atlas') analysisflow.connect(inputspec, 'atlas_file', outputspec, 'relabelled_atlas_file') analysisflow.connect(inputspec, 'labels', outputspec, 'reordered_labels') analysisflow.connect(inputspec, 'modules', outputspec, 'reordered_modules') analysisflow.connect(extract_timesereies, 'out_file', ds_txt, 'regional_timeseries') analysisflow.connect(extract_timesereies, 'out_file', timeseries_qc, 'inputspec.timeseries') analysisflow.connect(extract_timesereies, 'out_file', outputspec, 'timeseries_file') return analysisflow