def metaread(nifti): """ Combines metadata read from the header, populates the SPM slice timing correction inputs and outputs the time corrected epi image. Uses dcmstack.lookup to get TR and slice times, and NiftiWrapper to get image dimensions (number of slices is the z [2]). """ from nipype.interfaces import dcmstack from dcmstack.dcmmeta import NiftiWrapper nii = NiftiWrapper.from_filename(nifti) imdims = nii.meta_ext.shape sliceno = imdims[2] mid_slice = int(sliceno / 2) lookup = dcmstack.LookupMeta() lookup.inputs.meta_keys = { 'RepetitionTime': 'TR', 'CsaImage.MosaicRefAcqTimes': 'ST' } lookup.inputs.in_file = nifti lookup.run() slicetimes = [int(lookup.result['ST'][0][x]) for x in range(0, imdims[2])] #Converts slice times to ints. tr = lookup.result['TR'] / 1000 #Converts tr to seconds. ta = tr - (tr / sliceno) return (sliceno, slicetimes, tr, ta, mid_slice)
def _run_interface(self, runtime): src = NiftiWrapper.from_filename(self.inputs.src_file) dest_nii = nb.load(self.inputs.dest_file) dest = NiftiWrapper(dest_nii, make_empty=True) classes = src.meta_ext.get_valid_classes() if self.inputs.include_classes: classes = [ cls for cls in classes if cls in self.inputs.include_classes ] if self.inputs.exclude_classes: classes = [ cls for cls in classes if not cls in self.inputs.exclude_classes ] for cls in classes: src_dict = src.meta_ext.get_class_dict(cls) dest_dict = dest.meta_ext.get_class_dict(cls) dest_dict.update(src_dict) self.out_path = path.join(os.getcwd(), path.basename(self.inputs.dest_file)) dest.to_filename(self.out_path) return runtime
def _run_interface(self, runtime): src = NiftiWrapper.from_filename(self.inputs.src_file) dest_nii = nb.load(self.inputs.dest_file) dest = NiftiWrapper(dest_nii, make_empty=True) classes = src.meta_ext.get_valid_classes() if self.inputs.include_classes: classes = [cls for cls in classes if cls in self.inputs.include_classes ] if self.inputs.exclude_classes: classes = [cls for cls in classes if not cls in self.inputs.exclude_classes ] for cls in classes: src_dict = src.meta_ext.get_class_dict(cls) dest_dict = dest.meta_ext.get_class_dict(cls) dest_dict.update(src_dict) self.out_path = path.join(os.getcwd(), path.basename(self.inputs.dest_file)) dest.to_filename(self.out_path) return runtime
def _run_interface(self, runtime): #If the 'meta_keys' input is a list, covert it to a dict self._make_name_map() nw = NiftiWrapper.from_filename(self.inputs.in_file) self.result = {} for meta_key, out_name in self._meta_keys.iteritems(): self.result[out_name] = nw.meta_ext.get_values(meta_key) return runtime
def _run_interface(self, runtime): # If the 'meta_keys' input is a list, covert it to a dict self._make_name_map() nw = NiftiWrapper.from_filename(self.inputs.in_file) self.result = {} for meta_key, out_name in list(self._meta_keys.items()): self.result[out_name] = nw.meta_ext.get_values(meta_key) return runtime
def get_tr_and_sliceorder(dicom_files): import numpy as np import dcmstack, dicom from dcmstack.dcmmeta import NiftiWrapper nii_wrp = NiftiWrapper.from_filename(dicom_files) sliceorder = np.argsort(nii_wrp.meta_ext.get_values("CsaImage.MosaicRefAcqTimes")[0]).tolist() tr = nii_wrp.meta_ext.get_values("RepetitionTime") return tr / 1000.0, sliceorder
def get_tr_and_sliceorder(dicom_files, convention="french"): import numpy as np import dcmstack, dicom from dcmstack.dcmmeta import NiftiWrapper nii_wrp = NiftiWrapper.from_filename(dicom_files) if convention == "french": sliceorder = np.argsort(np.argsort(nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes')[0])).tolist() elif convention == "SPM": sliceorder = np.argsort(nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes')[0]).tolist() tr = nii_wrp.meta_ext.get_values('RepetitionTime') return tr/1000.,sliceorder
def get_tr_and_sliceorder(nifti_file): import numpy as np import os import dcmstack, dicom from dcmstack.dcmmeta import NiftiWrapper nii = NiftiWrapper.from_filename(nifti_file) f = open("slicetiming.txt", "w") f.write("\n".join(["%g"%(i/1000.) for i in nii.meta_ext.get_values('CsaImage.MosaicRefAcqTimes')[1]]) + "\n") f.close() tr = nii.meta_ext.get_values('RepetitionTime') return tr/1000., os.path.abspath("slicetiming.txt")
def get_tr_and_sliceorder(dicom_files, convention="french"): import numpy as np import dcmstack, dicom from dcmstack.dcmmeta import NiftiWrapper nii_wrp = NiftiWrapper.from_filename(dicom_files) if convention == "french": sliceorder = np.argsort( np.argsort( nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes') [0])).tolist() elif convention == "SPM": sliceorder = np.argsort( nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes') [0]).tolist() tr = nii_wrp.meta_ext.get_values('RepetitionTime') return tr / 1000., sliceorder
print(gtab.bvals) # Printing bvals on console print(gtab.bvecs) # Printing bvecs on console S0s = data[:, :, :, gtab.b0s_mask] # Used to tell which part of the data contains b0s print(S0s.shape) # Verifying the dimensions of S0s to determine the number of b0s nib.save(nib.Nifti1Image(S0s, img.affine), subject+'_b0_AP.nii.gz') # Just for necessary src = join(home, '.dipy', subject + '_b0_AP.nii.gz') # Where the source file is dst = join(dname, subject + "_b0_AP.nii.gz") # The new destination for the file shutil.move(src, dst) # Moving the file to the right directory ############################################################# # Merging # ############################################################# fb0AP = join(dname, subject + '_b0_AP.nii.gz') # The b0AP image is saved in this variable fb0PA = join(dname, subject + '_DWI_b0_PA.nii.gz') # The b0PA image is saved in this variable b0AP = NiftiWrapper.from_filename(fb0AP) # Loading the image in this variable b0PA = NiftiWrapper.from_filename(fb0PA) # Loading the image in this variable print(b0AP.nii_img.get_shape()) # Size of data print(b0PA.nii_img.get_shape()) # Size of data print(b0AP.get_meta('EchoTime')) # Echo time print(b0PA.get_meta('EchoTime')) # Echo time b0APPA = NiftiWrapper.from_sequence([b0AP, b0PA]) # Merging of the two b0 files print(b0APPA.nii_img.get_shape()) # Size of data print(b0APPA.get_meta('EchoTime', index = (0, 0, 0, 0))) # Echo time print(b0APPA.get_meta('EchoTime', index = (0, 0, 0, 1))) # Echo time ############################################################# # Topup # #############################################################
def get_tr_and_sliceorder(dicom_files, convention="french"): import numpy as np import dcmstack, dicom from dcmstack.dcmmeta import NiftiWrapper nii_wrp = NiftiWrapper.from_filename(dicom_files) if convention == "french": sliceorder = np.argsort(np.argsort(nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes')[0])).tolist() elif convention == "SPM": sliceorder = np.argsort(nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes')[0]).tolist() tr = nii_wrp.meta_ext.get_values('RepetitionTime') return tr/1000.,sliceorder get_meta = pe.Node(util.Function(input_names=['dicom_files', 'convention'], output_names=['tr', 'sliceorder'], function=get_tr_and_sliceorder), name="get_meta") get_meta.inputs.convention = "french" wf.connect(datagrabber, "resting_nifti", get_meta, "dicom_files") preproc = create_rest_prep(name="bips_resting_preproc", fieldmap=False) zscore = preproc.get_node('z_score') preproc.remove_nodes([zscore]) mod_realign = preproc.get_node("mod_realign") mod_realign.plugin_args = {"submit_specs":"request_memory=4000\n"} # inputs preproc.inputs.inputspec.motion_correct_node = 'nipy' ad = preproc.get_node('artifactdetect') preproc.disconnect(mod_realign,'parameter_source', ad,'parameter_source') ad.inputs.parameter_source = "NiPy" preproc.inputs.inputspec.realign_parameters = {"loops":[5], "speedup":[5]} preproc.inputs.inputspec.do_whitening = False preproc.inputs.inputspec.timepoints_to_remove = 4 preproc.inputs.inputspec.smooth_type = 'susan' preproc.inputs.inputspec.do_despike = False preproc.inputs.inputspec.surface_fwhm = 0.0 preproc.inputs.inputspec.num_noise_components = 6 preproc.inputs.inputspec.regress_before_PCA = False preproc.get_node('fwhm_input').iterables = ('fwhm', [5]) preproc.get_node('take_mean_art').get_node('strict_artifact_detect').inputs.save_plot = True #preproc.get_node('take_mean_art').get_node('strict_artifact_detect').overwrite=True preproc.inputs.inputspec.ad_normthresh = 1 preproc.inputs.inputspec.ad_zthresh = 3 preproc.inputs.inputspec.do_slicetime = True preproc.inputs.inputspec.compcor_select = [True, True] preproc.inputs.inputspec.filter_type = 'fsl' preproc.get_node('bandpass_filter').iterables = [('highpass_freq', [0.01]), ('lowpass_freq', [0.1])] # preproc.inputs.inputspec.highpass_freq = 0.01 # preproc.inputs.inputspec.lowpass_freq = 0.1 #[motion_params, composite_norm, compcorr_components, global_signal, art_outliers, motion derivatives] preproc.inputs.inputspec.reg_params = [True, True, True, False, True, False] preproc.inputs.inputspec.fssubject_dir = freesurferdir wf.connect(get_meta, "tr", preproc, "inputspec.tr") wf.connect(get_meta, "sliceorder", preproc, "inputspec.sliceorder") wf.connect(subject_id_infosource, "subject_id", preproc, 'inputspec.fssubject_id') wf.connect(datagrabber, "resting_nifti", preproc, "inputspec.func") # report_wf = create_preproc_report_wf(resultsdir + "/reports") # report_wf.inputs.inputspec.fssubjects_dir = preproc.inputs.inputspec.fssubject_dir def pick_full_brain_ribbon(l): import os for path in l: if os.path.split(path)[1] == "ribbon.mgz": return path # wf.connect(preproc,"artifactdetect.plot_files", report_wf, "inputspec.art_detect_plot") # wf.connect(preproc,"take_mean_art.weighted_mean.mean_image", report_wf, "inputspec.mean_epi") # wf.connect(preproc,("getmask.register.out_reg_file", list_to_filename), report_wf, "inputspec.reg_file") # wf.connect(preproc,("getmask.fssource.ribbon",pick_full_brain_ribbon), report_wf, "inputspec.ribbon") # wf.connect(preproc,("CompCor.tsnr.tsnr_file", list_to_filename), report_wf, "inputspec.tsnr_file") # wf.connect(subject_id_infosource, "subject_id", report_wf, "inputspec.subject_id") ds = pe.Node(nio.DataSink(), name="datasink", overwrite=True) ds.inputs.base_directory = os.path.join(resultsdir, "volumes") wf.connect(preproc, 'bandpass_filter.out_file', ds, "preprocessed_resting") wf.connect(preproc, 'getmask.register.out_fsl_file', ds, "func2anat_transform") wf.connect(preproc, 'outputspec.mask', ds, "epi_mask") wf.write_graph() wf.run(plugin="MultiProc")
def get_tr_and_sliceorder(dicom_files, convention="french"): import numpy as np import dcmstack, dicom from dcmstack.dcmmeta import NiftiWrapper nii_wrp = NiftiWrapper.from_filename(dicom_files) if convention == "french": sliceorder = np.argsort( np.argsort( nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes') [0])).tolist() elif convention == "SPM": sliceorder = np.argsort( nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes') [0]).tolist() tr = nii_wrp.meta_ext.get_values('RepetitionTime') return tr / 1000., sliceorder get_meta = pe.Node(util.Function(input_names=['dicom_files', 'convention'], output_names=['tr', 'sliceorder'], function=get_tr_and_sliceorder), name="get_meta") get_meta.inputs.convention = "french" wf.connect(datagrabber, "resting_nifti", get_meta, "dicom_files") preproc = create_rest_prep(name="bips_resting_preproc", fieldmap=False) zscore = preproc.get_node('z_score') preproc.remove_nodes([zscore]) mod_realign = preproc.get_node("mod_realign") mod_realign.plugin_args = {"submit_specs": "request_memory=4000\n"} # inputs preproc.inputs.inputspec.motion_correct_node = 'nipy' ad = preproc.get_node('artifactdetect') preproc.disconnect(mod_realign, 'parameter_source', ad, 'parameter_source') ad.inputs.parameter_source = "NiPy" preproc.inputs.inputspec.realign_parameters = { "loops": [5], "speedup": [5] } preproc.inputs.inputspec.do_whitening = False preproc.inputs.inputspec.timepoints_to_remove = 4 preproc.inputs.inputspec.smooth_type = 'susan' preproc.inputs.inputspec.do_despike = False preproc.inputs.inputspec.surface_fwhm = 0.0 preproc.inputs.inputspec.num_noise_components = 6 preproc.inputs.inputspec.regress_before_PCA = False preproc.get_node('fwhm_input').iterables = ('fwhm', [5]) preproc.get_node('take_mean_art').get_node( 'strict_artifact_detect').inputs.save_plot = True #preproc.get_node('take_mean_art').get_node('strict_artifact_detect').overwrite=True preproc.inputs.inputspec.ad_normthresh = 1 preproc.inputs.inputspec.ad_zthresh = 3 preproc.inputs.inputspec.do_slicetime = True preproc.inputs.inputspec.compcor_select = [True, True] preproc.inputs.inputspec.filter_type = 'fsl' preproc.get_node('bandpass_filter').iterables = [('highpass_freq', [0.01]), ('lowpass_freq', [0.1])] # preproc.inputs.inputspec.highpass_freq = 0.01 # preproc.inputs.inputspec.lowpass_freq = 0.1 #[motion_params, composite_norm, compcorr_components, global_signal, art_outliers, motion derivatives] preproc.inputs.inputspec.reg_params = [ True, True, True, False, True, False ] preproc.inputs.inputspec.fssubject_dir = freesurferdir wf.connect(get_meta, "tr", preproc, "inputspec.tr") wf.connect(get_meta, "sliceorder", preproc, "inputspec.sliceorder") wf.connect(subject_id_infosource, "subject_id", preproc, 'inputspec.fssubject_id') wf.connect(datagrabber, "resting_nifti", preproc, "inputspec.func") # report_wf = create_preproc_report_wf(resultsdir + "/reports") # report_wf.inputs.inputspec.fssubjects_dir = preproc.inputs.inputspec.fssubject_dir def pick_full_brain_ribbon(l): import os for path in l: if os.path.split(path)[1] == "ribbon.mgz": return path # wf.connect(preproc,"artifactdetect.plot_files", report_wf, "inputspec.art_detect_plot") # wf.connect(preproc,"take_mean_art.weighted_mean.mean_image", report_wf, "inputspec.mean_epi") # wf.connect(preproc,("getmask.register.out_reg_file", list_to_filename), report_wf, "inputspec.reg_file") # wf.connect(preproc,("getmask.fssource.ribbon",pick_full_brain_ribbon), report_wf, "inputspec.ribbon") # wf.connect(preproc,("CompCor.tsnr.tsnr_file", list_to_filename), report_wf, "inputspec.tsnr_file") # wf.connect(subject_id_infosource, "subject_id", report_wf, "inputspec.subject_id") ds = pe.Node(nio.DataSink(), name="datasink", overwrite=True) ds.inputs.base_directory = os.path.join(resultsdir, "volumes") wf.connect(preproc, 'bandpass_filter.out_file', ds, "preprocessed_resting") wf.connect(preproc, 'getmask.register.out_fsl_file', ds, "func2anat_transform") wf.connect(preproc, 'outputspec.mask', ds, "epi_mask") wf.write_graph() wf.run(plugin="MultiProc")