def _run_interface(self, runtime): self._results['out_tmp'] = fname_presuffix(self.inputs.in_boldmask, suffix='_tempmask', newpath=runtime.cwd) self._results['out_mask'] = fname_presuffix(self.inputs.in_boldmask, suffix='_refinemask', newpath=runtime.cwd) b1 = ApplyTransforms() b1.inputs.dimension = 3 b1.inputs.float = True b1.inputs.input_image = self.inputs.in_t1mask b1.inputs.interpolation = 'NearestNeighbor' b1.inputs.reference_image = self.inputs.in_boldmask b1.inputs.transforms = self.inputs.transforms b1.inputs.input_image_type = 3 b1.inputs.output_image = self._results['out_tmp'] b1.run() from nipype.interfaces.fsl import MultiImageMaths mat1 = MultiImageMaths() mat1.inputs.in_file = self._results['out_tmp'] mat1.inputs.op_string = " -mul %s -bin" mat1.inputs.operand_files = self.inputs.in_boldmask mat1.inputs.out_file = self._results['out_mask'] mat1.run() self.inputs.out_mask = os.path.abspath(self._results['out_mask']) return runtime
def fslmath_op_multifile(input_file, op_string, op_files, output_prefix): from nipype.interfaces.fsl import MultiImageMaths import os binM = MultiImageMaths() binM.inputs.in_file = input_file binM.inputs.op_string = op_string binM.inputs.operand_files = op_files print "MultiImageMaths [" + os.path.basename(input_file) + "]:" + binM.cmdline res = binM.run() outfile = d2s.move_to_results(res.outputs.out_file, output_prefix) return outfile
def selectionchange(self): self.mask1 = self.cb.currentText() self.mask2 = self.cb2.currentText() print("mask1 = " + self.mask1) print("mask2 = " + self.mask2) if self.mask1 != 'Select mask 1...' and self.mask2 != 'Select mask 2...': intersec = Intersection(self.mask1, self.mask2) dti = nib.load(self.filenamedti) dti_data = dti.get_data() #pre_glyph = np.full((145,174,145,6), 0) maths = MultiImageMaths() maths.run(in_file='dti.nii', op_string=' -mul %s', operand_files='intersec_img.nii.gz', out_file='pre_glyph_img.nii.gz') filename1 = 0 filename2 = 0 filename3 = 0 filename4 = 0 filename5 = 0 if self.mask1 == 'AF_left': filename2 = 1 if self.mask2 == 'AF_left': filename2 = 1 if self.mask1 == 'AF_right': filename3 = 1 if self.mask2 == 'AF_right': filename3 = 1 if self.mask1 == 'CST_left': filename4 = 1 if self.mask2 == 'CST_left': filename4 = 1 if self.mask1 == 'CST_right': filename5 = 1 if self.mask2 == 'CST_right': filename5 = 1 for i in range(3): twodviews(self, i, self.filename1, filename2, filename3, filename4, filename5)
def _run_interface(self, runtime): cbf, meancbf, att = cbfcomputation(metadata=self.inputs.in_metadata, mask=self.inputs.in_mask, m0file=self.inputs.in_m0file, cbffile=self.inputs.in_cbf) self._results['out_cbf'] = fname_presuffix(self.inputs.in_cbf, suffix='_cbf', newpath=runtime.cwd) self._results['out_mean'] = fname_presuffix(self.inputs.in_cbf, suffix='_meancbf', newpath=runtime.cwd) samplecbf = nb.load(self.inputs.in_m0file) nb.Nifti1Image(cbf, samplecbf.affine, samplecbf.header).to_filename(self._results['out_cbf']) nb.Nifti1Image(meancbf, samplecbf.affine, samplecbf.header).to_filename(self._results['out_mean']) if att is not None: self._results['out_att'] = fname_presuffix(self.inputs.in_cbf, suffix='_att', newpath=runtime.cwd) nb.Nifti1Image(att, samplecbf.affine, samplecbf.header).to_filename( self._results['out_att']) self.inputs.out_att = os.path.abspath(self._results['out_att']) self.inputs.out_cbf = os.path.abspath(self._results['out_cbf']) self.inputs.out_mean = os.path.abspath(self._results['out_mean']) # we dont know why not zeros background $ from nipype.interfaces.fsl import MultiImageMaths mat1 = MultiImageMaths() mat1.inputs.in_file = self.inputs.out_mean mat1.inputs.op_string = " -mul %s " mat1.inputs.operand_files = self.inputs.in_mask mat1.inputs.out_file = self.inputs.out_mean mat1.run() mat1 = MultiImageMaths() mat1.inputs.in_file = self.inputs.out_cbf mat1.inputs.op_string = " -mul %s " mat1.inputs.operand_files = self.inputs.in_mask mat1.inputs.out_file = self.inputs.out_cbf mat1.run() return runtime
smooth_ess = nilearn.image.smooth_img(v[0], 8) print(v[0]) smooth_es.append(smooth_ess) nilearn.plotting.plot_glass_brain(smooth_ess, display_mode='lyrz', colorbar=True, plot_abs=False) #%% plotting the smoothed contrasts nilearn.plotting.plot_glass_brain(nilearn.image.mean_img(smooth_es), display_mode='lyrz', colorbar=True, plot_abs=False) #%% ess_concat = nilearn.image.concat_imgs(smooth_es, auto_resample=True) ess_concat.to_filename("/media/Data/work/custom_modelling_spm/lossTotal.nii.gz") #%% randomize.inputs.in_file = '/media/Data/work/custom_modelling_spm/lossTotal.nii.gz' randomize.inputs.f_only = True fig = nilearn.plotting.plot_stat_map('/media/Data/work/custom_modelling_spm/randomize/randomise_tstat1.nii.gz', alpha=0.7, cut_coords=(-20, -80, 18)) fig.add_contours('/media/Data/work/custom_modelling_spm/randomize/randomise_tfce_corrp_tstat1.nii.gz', levels=[0.95], colors='w') #%% Fliping to see the negative from nipype.interfaces.fsl import MultiImageMaths maths = MultiImageMaths() maths.inputs.in_file = "/media/Data/work/custom_modelling_spm/GainRisk_cope.nii.gz" maths.inputs.op_string = "-add %s -mul -1 -div %s" !fslmaths "/media/Data/work/custom_modelling_spm/negGainRisk_cope.nii.gz" -mul -1 "/media/Data/work/custom_modelling_spm/oppnegGainRisk_cope.nii.gz"
'preprocessed/func/connectivity', 'rest_mask_mni3.nii.gz') rest_list.append(rest_msk) i += 1 if i != 1: gm_str.append('-add %s ') rest_str.append('-mul %s') # define final operational strings to be used gm_str.append(('-div %s' % len(gm_list))) op_string_gm = " ".join((gm_str)) op_string_rest = " ".join((rest_str)) # get group level probabilistic GM by averaging maths = MultiImageMaths() maths.inputs.in_file = gm_list[0] maths.inputs.op_string = op_string_gm maths.inputs.operand_files = gm_list[1:] maths.inputs.out_file = 'gm_prob_mni3_ave.nii.gz' maths.run() #print maths.cmdline # get GM mask (binarize the probabilistic GM map) binarize = MathsCommand() binarize.inputs.args = '-thr 0.30 -bin' binarize.inputs.in_file = 'gm_prob_mni3_ave.nii.gz' binarize.inputs.out_file = 'gm_prob_mni3_ave_mask.nii.gz' binarize.run() # get group level resting mask by multiplying individual ones
operand_files = in_files[1:] op_string = '-add %s ' op_string = len(operand_files) * op_string return in_file, operand_files, op_string # </editor-fold> # <editor-fold desc="Mask processing"> generate_add_masks_lists_n = Node(Function( input_names=['in_files'], output_names=['list_in_file', 'list_operand_files', 'list_op_string'], function=generate_multiimagemaths_lists), name='generate_add_masks_lists_node') add_masks_n = Node(MultiImageMaths(), name="add_masks_node") wf.connect([(bet_n, generate_add_masks_lists_n, [('mask_file', 'in_files')])]) wf.connect([(generate_add_masks_lists_n, add_masks_n, [('list_in_file', 'in_file')])]) wf.connect([(generate_add_masks_lists_n, add_masks_n, [('list_operand_files', 'operand_files')])]) wf.connect([(generate_add_masks_lists_n, add_masks_n, [('list_op_string', 'op_string')])]) # </editor-fold> # # <editor-fold desc="QSM Post processing"> generate_add_qsms_lists_n = Node(Function( input_names=['in_files'], output_names=['list_in_file', 'list_operand_files', 'list_op_string'],
def spm_anat_to_diff_coregistration(wf_name="spm_anat_to_diff_coregistration"): """ Co-register the anatomical image and other images in anatomical space to the average B0 image. This estimates an affine transform from anat to diff space, applies it to the brain mask and an atlas. Nipype Inputs ------------- dti_co_input.avg_b0: traits.File path to the average B0 image from the diffusion MRI. This image should come from a motion and Eddy currents corrected diffusion image. dti_co_input.anat: traits.File path to the high-contrast anatomical image. dti_co_input.tissues: traits.File paths to the NewSegment c*.nii output files, in anatomical space dti_co_input.atlas_anat: traits.File Atlas in subject anatomical space. Nipype Outputs -------------- dti_co_output.anat_diff: traits.File Anatomical image in diffusion space. dti_co_output.tissues_diff: traits.File Tissues images in diffusion space. dti_co_output.brain_mask_diff: traits.File Brain mask for diffusion image. dti_co_output.atlas_diff: traits.File Atlas image warped to diffusion space. If the `atlas_file` option is an existing file and `normalize_atlas` is True. Nipype Workflow Dependencies ---------------------------- This workflow depends on: - spm_anat_preproc Returns ------- wf: nipype Workflow """ # specify input and output fields in_fields = ["avg_b0", "tissues", "anat"] out_fields = [ "anat_diff", "tissues_diff", "brain_mask_diff", ] do_atlas, _ = check_atlas_file() if do_atlas: in_fields += ["atlas_anat"] out_fields += ["atlas_diff"] # input interface dti_input = pe.Node(IdentityInterface(fields=in_fields, mandatory_inputs=True), name="dti_co_input") gunzip_b0 = pe.Node(Gunzip(), name="gunzip_b0") coreg_b0 = setup_node(spm_coregister(cost_function="mi"), name="coreg_b0") # co-registration brain_sel = pe.Node(Select(index=[0, 1, 2]), name="brain_sel") coreg_split = pe.Node(Split(splits=[1, 2], squeeze=True), name="coreg_split") brain_merge = setup_node(MultiImageMaths(), name="brain_merge") brain_merge.inputs.op_string = "-add '%s' -add '%s' -abs -kernel gauss 4 -dilM -ero -kernel gauss 1 -dilM -bin" brain_merge.inputs.out_file = "brain_mask_diff.nii.gz" # output interface dti_output = pe.Node(IdentityInterface(fields=out_fields), name="dti_co_output") # Create the workflow object wf = pe.Workflow(name=wf_name) # Connect the nodes wf.connect([ # co-registration (dti_input, coreg_b0, [("anat", "source")]), (dti_input, brain_sel, [("tissues", "inlist")]), (brain_sel, coreg_b0, [(("out", flatten_list), "apply_to_files")]), (dti_input, gunzip_b0, [("avg_b0", "in_file")]), (gunzip_b0, coreg_b0, [("out_file", "target")]), (coreg_b0, coreg_split, [("coregistered_files", "inlist")]), (coreg_split, brain_merge, [("out1", "in_file")]), (coreg_split, brain_merge, [("out2", "operand_files")]), # output (coreg_b0, dti_output, [("coregistered_source", "anat_diff")]), (coreg_b0, dti_output, [("coregistered_files", "tissues_diff")]), (brain_merge, dti_output, [("out_file", "brain_mask_diff")]), ]) # add more nodes if to perform atlas registration if do_atlas: coreg_atlas = setup_node(spm_coregister(cost_function="mi"), name="coreg_atlas") # set the registration interpolation to nearest neighbour. coreg_atlas.inputs.write_interp = 0 wf.connect([ (dti_input, coreg_atlas, [ ("anat", "source"), ("atlas_anat", "apply_to_files"), ]), (gunzip_b0, coreg_atlas, [("out_file", "target")]), (coreg_atlas, dti_output, [("coregistered_files", "atlas_diff")]), ]) return wf