How to use the pynets.registration.reg_utils.applyxfm function in pynets

To help you get started, we’ve selected a few pynets examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dPys / PyNets / tests / test_reg_utils.py View on Github external
def test_applyxfm():
    """
    Test applyxfm functionality
    """
    base_dir = str(Path(__file__).parent/"examples")
    anat_dir = base_dir + '/003/anat'
    
    ## First test: Apply xfm from test_align to orig anat img. 
    inp = anat_dir + '/sub-003_T1w_brain.nii.gz'
    ref = anat_dir + '/MNI152_T1_2mm_brain.nii.gz'
    xfm = anat_dir + '/highres2standard.mat'
    aligned = anat_dir + '/highres2standard_2.nii.gz'
    reg_utils.applyxfm(ref, inp, xfm, aligned, interp='trilinear', dof=6)
    # Check test_applyfxm = test_align outputs
    out_applyxfm = nib.load(aligned)
    out_applyxfm_data = out_applyxfm.get_data()
    out_align_file = anat_dir + '/highres2standard.nii.gz'
    out_align = nib.load(out_align_file)
    out_align_data = out_align.get_data()
    check_eq_arrays = np.array_equal(out_applyxfm_data, out_align_data)
    assert check_eq_arrays is True
    
    ## Second test: Apply xfm to standard space roi (invert xfm first) >> native space roi.
    # ref is native space anat image
    ref = anat_dir + '/sub-003_T1w.nii.gz'
    # input is standard space precuneus mask
    inp = anat_dir + '/precuneous_thr_bin.nii.gz'
    # xfm is standard2native from convert_xfm -omat standard2highres.mat highres2standard.mat
    xfm = anat_dir + '/standard2highres.mat'
github dPys / PyNets / pynets / registration / register.py View on Github external
cost='mutualinfo', searchrad=True, interp="spline", out=None)

        # Create transform to align roi to mni and T1w using flirt
        regutils.applyxfm(self.input_mni_brain, self.mni_vent_loc, self.xfm_roi2mni_init, self.vent_mask_mni)

        if self.simple is False:
            # Apply warp resulting from the inverse MNI->T1w created earlier
            regutils.apply_warp(self.t1w_brain, self.vent_mask_mni, self.vent_mask_t1w, warp=self.mni2t1w_warp,
                                interp='nn', sup=True)

            regutils.apply_warp(self.t1w_brain, self.corpuscallosum, self.corpuscallosum_mask_t1w,
                                warp=self.mni2t1w_warp, interp="nn", sup=True)

        else:
            regutils.applyxfm(self.vent_mask_mni, self.t1w_brain, self.mni2t1_xfm, self.vent_mask_t1w)
            regutils.applyxfm(self.corpuscallosum, self.t1w_brain, self.mni2t1_xfm, self.corpuscallosum_mask_t1w)

        # Applyxfm tissue maps to dwi space
        regutils.applyxfm(self.fa_path, self.vent_mask_t1w, self.t1wtissue2dwi_xfm, self.vent_mask_dwi)
        regutils.applyxfm(self.fa_path, self.csf_mask, self.t1wtissue2dwi_xfm, self.csf_mask_dwi)
        regutils.applyxfm(self.fa_path, self.gm_mask, self.t1wtissue2dwi_xfm, self.gm_in_dwi)
        regutils.applyxfm(self.fa_path, self.wm_mask, self.t1wtissue2dwi_xfm, self.wm_in_dwi)
        regutils.applyxfm(self.fa_path, self.corpuscallosum_mask_t1w, self.t1wtissue2dwi_xfm, self.corpuscallosum_dwi)

        # Threshold WM to binary in dwi space
        thr_img = nib.load(self.wm_in_dwi)
        thr_img = math_img('img > 0.1', img=thr_img)
        nib.save(thr_img, self.wm_in_dwi_bin)

        # Threshold GM to binary in dwi space
        thr_img = nib.load(self.gm_in_dwi)
        thr_img = math_img('img > 0.2', img=thr_img)
github dPys / PyNets / pynets / registration / register.py View on Github external
if self.simple is False:
            # Apply warp resulting from the inverse MNI->T1w created earlier
            regutils.apply_warp(self.t1w_brain, self.vent_mask_mni, self.vent_mask_t1w, warp=self.mni2t1w_warp,
                                interp='nn', sup=True)

            regutils.apply_warp(self.t1w_brain, self.corpuscallosum, self.corpuscallosum_mask_t1w,
                                warp=self.mni2t1w_warp, interp="nn", sup=True)

        else:
            regutils.applyxfm(self.vent_mask_mni, self.t1w_brain, self.mni2t1_xfm, self.vent_mask_t1w)
            regutils.applyxfm(self.corpuscallosum, self.t1w_brain, self.mni2t1_xfm, self.corpuscallosum_mask_t1w)

        # Applyxfm tissue maps to dwi space
        regutils.applyxfm(self.fa_path, self.vent_mask_t1w, self.t1wtissue2dwi_xfm, self.vent_mask_dwi)
        regutils.applyxfm(self.fa_path, self.csf_mask, self.t1wtissue2dwi_xfm, self.csf_mask_dwi)
        regutils.applyxfm(self.fa_path, self.gm_mask, self.t1wtissue2dwi_xfm, self.gm_in_dwi)
        regutils.applyxfm(self.fa_path, self.wm_mask, self.t1wtissue2dwi_xfm, self.wm_in_dwi)
        regutils.applyxfm(self.fa_path, self.corpuscallosum_mask_t1w, self.t1wtissue2dwi_xfm, self.corpuscallosum_dwi)

        # Threshold WM to binary in dwi space
        thr_img = nib.load(self.wm_in_dwi)
        thr_img = math_img('img > 0.1', img=thr_img)
        nib.save(thr_img, self.wm_in_dwi_bin)

        # Threshold GM to binary in dwi space
        thr_img = nib.load(self.gm_in_dwi)
        thr_img = math_img('img > 0.2', img=thr_img)
        nib.save(thr_img, self.gm_in_dwi_bin)

        # Threshold CSF to binary in dwi space
        thr_img = nib.load(self.csf_mask_dwi)
        thr_img = math_img('img > 0.95', img=thr_img)
github dPys / PyNets / pynets / registration / register.py View on Github external
regutils.apply_warp(self.t1w_brain, uatlas, aligned_atlas_skull,
                                    warp=self.mni2t1w_warp, interp='nn', sup=True)

                if uatlas_parcels is not None:
                    aligned_atlas_t1mni_parcels = "%s%s%s%s" % (self.anat_path, '/', atlas, "_t1w_mni_parcels.nii.gz")
                    aligned_atlas_skull_parcels = "%s%s%s%s" % (self.anat_path, '/', atlas, "_t1w_skull_parcels.nii.gz")
                    regutils.applyxfm(self.t1_aligned_mni, uatlas_parcels, self.atlas2t1mni_xfm_init,
                                      aligned_atlas_t1mni_parcels, interp="nearestneighbour")
                    regutils.apply_warp(self.t1w_brain, aligned_atlas_t1mni_parcels, aligned_atlas_skull_parcels,
                                        warp=self.mni2t1w_warp, interp='nn', sup=True)
                else:
                    aligned_atlas_skull_parcels = None

                # Map atlas in t1w space to dwi space
                if uatlas_parcels is not None:
                    regutils.applyxfm(self.fa_path, aligned_atlas_skull_parcels, self.t1wtissue2dwi_xfm,
                                      dwi_aligned_atlas, interp="nearestneighbour")
                else:
                    regutils.applyxfm(self.fa_path, aligned_atlas_skull, self.t1wtissue2dwi_xfm,
                                      dwi_aligned_atlas, interp="nearestneighbour")
            except:
                print("Warning: Atlas is not in correct dimensions, or input is low quality,\nusing linear template "
                      "registration.")

                # Combine our linear transform from t1w to template with our transform from dwi to t1w space to get a
                # transform from atlas ->(-> t1w ->)-> dwi
                regutils.combine_xfms(self.xfm_atlas2t1w_init, self.t1wtissue2dwi_xfm, self.temp2dwi_xfm)

                if uatlas_parcels is not None:
                    aligned_atlas_t1mni_parcels = "%s%s%s%s" % (self.anat_path, '/', atlas, "_t1w_mni_parcels.nii.gz")
                    regutils.applyxfm(self.t1_aligned_mni, uatlas_parcels, self.atlas2t1mni_xfm_init,
                                      aligned_atlas_t1mni_parcels, interp="nearestneighbour")
github dPys / PyNets / pynets / registration / register.py View on Github external
# Apply warp resulting from the inverse MNI->T1w created earlier
            regutils.apply_warp(self.t1w_brain, self.vent_mask_mni, self.vent_mask_t1w, warp=self.mni2t1w_warp,
                                interp='nn', sup=True)

            regutils.apply_warp(self.t1w_brain, self.corpuscallosum, self.corpuscallosum_mask_t1w,
                                warp=self.mni2t1w_warp, interp="nn", sup=True)

        else:
            regutils.applyxfm(self.vent_mask_mni, self.t1w_brain, self.mni2t1_xfm, self.vent_mask_t1w)
            regutils.applyxfm(self.corpuscallosum, self.t1w_brain, self.mni2t1_xfm, self.corpuscallosum_mask_t1w)

        # Applyxfm tissue maps to dwi space
        regutils.applyxfm(self.fa_path, self.vent_mask_t1w, self.t1wtissue2dwi_xfm, self.vent_mask_dwi)
        regutils.applyxfm(self.fa_path, self.csf_mask, self.t1wtissue2dwi_xfm, self.csf_mask_dwi)
        regutils.applyxfm(self.fa_path, self.gm_mask, self.t1wtissue2dwi_xfm, self.gm_in_dwi)
        regutils.applyxfm(self.fa_path, self.wm_mask, self.t1wtissue2dwi_xfm, self.wm_in_dwi)
        regutils.applyxfm(self.fa_path, self.corpuscallosum_mask_t1w, self.t1wtissue2dwi_xfm, self.corpuscallosum_dwi)

        # Threshold WM to binary in dwi space
        thr_img = nib.load(self.wm_in_dwi)
        thr_img = math_img('img > 0.1', img=thr_img)
        nib.save(thr_img, self.wm_in_dwi_bin)

        # Threshold GM to binary in dwi space
        thr_img = nib.load(self.gm_in_dwi)
        thr_img = math_img('img > 0.2', img=thr_img)
        nib.save(thr_img, self.gm_in_dwi_bin)

        # Threshold CSF to binary in dwi space
        thr_img = nib.load(self.csf_mask_dwi)
        thr_img = math_img('img > 0.95', img=thr_img)
        nib.save(thr_img, self.csf_mask_dwi)
github dPys / PyNets / pynets / registration / register.py View on Github external
regutils.applyxfm(self.t1_aligned_mni, uatlas, self.atlas2t1mni_xfm_init,
                                      aligned_atlas_t1mni, interp="nearestneighbour")
                    # Apply linear transformation from template to dwi space
                    regutils.applyxfm(self.fa_path, aligned_atlas_t1mni, self.temp2dwi_xfm, dwi_aligned_atlas,
                                      interp="nearestneighbour")
        else:
            # Combine our linear transform from t1w to template with our transform from dwi to t1w space to get a
            # transform from atlas ->(-> t1w ->)-> dwi
            regutils.combine_xfms(self.xfm_atlas2t1w_init, self.t1wtissue2dwi_xfm, self.temp2dwi_xfm)

            if uatlas_parcels is not None:
                aligned_atlas_t1mni_parcels = "%s%s%s%s" % (self.anat_path, '/', atlas, "_t1w_mni_parcels.nii.gz")
                regutils.applyxfm(self.t1_aligned_mni, uatlas_parcels, self.atlas2t1mni_xfm_init,
                                  aligned_atlas_t1mni_parcels, interp="nearestneighbour")
                # Apply linear transformation from template to dwi space
                regutils.applyxfm(self.fa_path, aligned_atlas_t1mni_parcels, self.temp2dwi_xfm, dwi_aligned_atlas,
                                  interp="nearestneighbour")
                aligned_atlas_t1mni = aligned_atlas_t1mni_parcels
            else:
                regutils.applyxfm(self.t1_aligned_mni, uatlas, self.atlas2t1mni_xfm_init,
                                  aligned_atlas_t1mni, interp="nearestneighbour")
                # Apply linear transformation from template to dwi space
                regutils.applyxfm(self.fa_path, aligned_atlas_t1mni, self.temp2dwi_xfm, dwi_aligned_atlas,
                                  interp="nearestneighbour")

        # Set intensities to int
        atlas_img = nib.load(dwi_aligned_atlas)
        t_img = nib.load(self.wm_gm_int_in_dwi)
        mask = math_img('img > 0', img=t_img)
        mask.to_filename(self.wm_gm_int_in_dwi_bin)
        img = nib.Nifti1Image(np.around(np.asarray(atlas_img.dataobj)).astype('uint16'),
                              affine=atlas_img.affine, header=atlas_img.header)
github dPys / PyNets / pynets / registration / register.py View on Github external
def waymask2dwi_align(self, waymask):
        """
        A function to perform alignment of a waymask from MNI space --> T1w --> dwi.
        """

        # Apply warp or transformer resulting from the inverse MNI->T1w created earlier
        if self.simple is False:
            regutils.apply_warp(self.t1w_brain, waymask, self.waymask_in_t1w, warp=self.mni2t1w_warp)
        else:
            regutils.applyxfm(self.t1w_brain, waymask, self.mni2t1_xfm, self.waymask_in_t1w)

        # Apply transform from t1w to native dwi space
        regutils.applyxfm(self.fa_path, self.waymask_in_t1w, self.t1wtissue2dwi_xfm, self.waymask_in_dwi)

        return
github dPys / PyNets / pynets / registration / register.py View on Github external
def waymask2dwi_align(self, waymask):
        """
        A function to perform alignment of a waymask from MNI space --> T1w --> dwi.
        """

        # Apply warp or transformer resulting from the inverse MNI->T1w created earlier
        if self.simple is False:
            regutils.apply_warp(self.t1w_brain, waymask, self.waymask_in_t1w, warp=self.mni2t1w_warp)
        else:
            regutils.applyxfm(self.t1w_brain, waymask, self.mni2t1_xfm, self.waymask_in_t1w)

        # Apply transform from t1w to native dwi space
        regutils.applyxfm(self.fa_path, self.waymask_in_t1w, self.t1wtissue2dwi_xfm, self.waymask_in_dwi)

        return
github dPys / PyNets / pynets / registration / register.py View on Github external
interp="nearestneighbour")
        else:
            # Combine our linear transform from t1w to template with our transform from dwi to t1w space to get a
            # transform from atlas ->(-> t1w ->)-> dwi
            regutils.combine_xfms(self.xfm_atlas2t1w_init, self.t1wtissue2dwi_xfm, self.temp2dwi_xfm)

            if uatlas_parcels is not None:
                aligned_atlas_t1mni_parcels = "%s%s%s%s" % (self.anat_path, '/', atlas, "_t1w_mni_parcels.nii.gz")
                regutils.applyxfm(self.t1_aligned_mni, uatlas_parcels, self.atlas2t1mni_xfm_init,
                                  aligned_atlas_t1mni_parcels, interp="nearestneighbour")
                # Apply linear transformation from template to dwi space
                regutils.applyxfm(self.fa_path, aligned_atlas_t1mni_parcels, self.temp2dwi_xfm, dwi_aligned_atlas,
                                  interp="nearestneighbour")
                aligned_atlas_t1mni = aligned_atlas_t1mni_parcels
            else:
                regutils.applyxfm(self.t1_aligned_mni, uatlas, self.atlas2t1mni_xfm_init,
                                  aligned_atlas_t1mni, interp="nearestneighbour")
                # Apply linear transformation from template to dwi space
                regutils.applyxfm(self.fa_path, aligned_atlas_t1mni, self.temp2dwi_xfm, dwi_aligned_atlas,
                                  interp="nearestneighbour")

        # Set intensities to int
        atlas_img = nib.load(dwi_aligned_atlas)
        t_img = nib.load(self.wm_gm_int_in_dwi)
        mask = math_img('img > 0', img=t_img)
        mask.to_filename(self.wm_gm_int_in_dwi_bin)
        img = nib.Nifti1Image(np.around(np.asarray(atlas_img.dataobj)).astype('uint16'),
                              affine=atlas_img.affine, header=atlas_img.header)
        nib.save(img, dwi_aligned_atlas)
        os.system("fslmaths {} -mas {} {}".format(dwi_aligned_atlas, self.wm_gm_int_in_dwi_bin,
                                                  dwi_aligned_atlas_wmgm_int))
        atlas_img.uncache()
github dPys / PyNets / pynets / registration / register.py View on Github external
interp="nearestneighbour")
                    aligned_atlas_t1mni = aligned_atlas_t1mni_parcels
                else:
                    regutils.applyxfm(self.t1_aligned_mni, uatlas, self.atlas2t1mni_xfm_init,
                                      aligned_atlas_t1mni, interp="nearestneighbour")
                    # Apply linear transformation from template to dwi space
                    regutils.applyxfm(self.fa_path, aligned_atlas_t1mni, self.temp2dwi_xfm, dwi_aligned_atlas,
                                      interp="nearestneighbour")
        else:
            # Combine our linear transform from t1w to template with our transform from dwi to t1w space to get a
            # transform from atlas ->(-> t1w ->)-> dwi
            regutils.combine_xfms(self.xfm_atlas2t1w_init, self.t1wtissue2dwi_xfm, self.temp2dwi_xfm)

            if uatlas_parcels is not None:
                aligned_atlas_t1mni_parcels = "%s%s%s%s" % (self.anat_path, '/', atlas, "_t1w_mni_parcels.nii.gz")
                regutils.applyxfm(self.t1_aligned_mni, uatlas_parcels, self.atlas2t1mni_xfm_init,
                                  aligned_atlas_t1mni_parcels, interp="nearestneighbour")
                # Apply linear transformation from template to dwi space
                regutils.applyxfm(self.fa_path, aligned_atlas_t1mni_parcels, self.temp2dwi_xfm, dwi_aligned_atlas,
                                  interp="nearestneighbour")
                aligned_atlas_t1mni = aligned_atlas_t1mni_parcels
            else:
                regutils.applyxfm(self.t1_aligned_mni, uatlas, self.atlas2t1mni_xfm_init,
                                  aligned_atlas_t1mni, interp="nearestneighbour")
                # Apply linear transformation from template to dwi space
                regutils.applyxfm(self.fa_path, aligned_atlas_t1mni, self.temp2dwi_xfm, dwi_aligned_atlas,
                                  interp="nearestneighbour")

        # Set intensities to int
        atlas_img = nib.load(dwi_aligned_atlas)
        t_img = nib.load(self.wm_gm_int_in_dwi)
        mask = math_img('img > 0', img=t_img)