def reg_bbr(wsp): """ Perform BBR registration :param reg_img: Data to register, e.g. PWI or calibration image. Normally would be brain extracted :param struc_img: Structural image :param struc_brain_img: Brain-extracted structural image Optional keyword arguments: :param inweight: :param init: Initial transform matrix Optional keyword arguments for fieldmap distortion correction: :param fmap: Fieldmap image :param fmapmag: Fieldmap magnitude image :param fmapmagbrain: Fieldmap magnitude image - brain extracted :param pedir: Phase encoding direction (x, -x, y, -y, z, -z) :param echospacing: Echo spacing :return Tuple of registered image, transform matrix """ struc.segment(wsp) wsp.log.write(" - BBR registration using epi_reg...") # Windows can't run epi_reg as it's a batch script. Use our experimental python # implementation but use the standard epi_reg on other platforms until the python # version is better tested if sys.platform.startswith("win"): import oxasl.epi_reg as pyepi result = pyepi.epi_reg(wsp, wsp.reg.nativeref) else: result = epi_reg(epi=wsp.reg.nativeref, t1=wsp.structural.struc, t1brain=wsp.structural.brain, out=fsl.LOAD, wmseg=wsp.structural.wm_seg, init=wsp.reg.asl2struc, inweight=wsp.inweight, log=wsp.fsllog) wsp.log.write(" DONE\n") return result["out%s" % defaultExt()], result["out"]
def get_fieldmap_correction(wsp): """ Get the fieldmap based distortion correction warp Required workspace attributes ----------------------------- - ``pwi`` : Perfusion weighted image (generated by preproc_asl) - ``fmap`` : Fieldmap image - ``fmapmag`` : Fieldmap magnitude image - ``fmapmagbrain`` : Fieldmap magnitude brain image - ``echospacing`` : - ``pedir`` : Optional workspace attributes ----------------------------- - ``nofmapreg`` : If True assume fieldmap in structural space Updated workspace attributes ---------------------------- - ``fmap_warp`` : Fieldmap distortion correction warp image in ASL space """ if wsp.fmap is None or wsp.fmapmag is None or wsp.fmapmagbrain is None: wsp.log.write(" - No fieldmap images for distortion correction\n") return elif wsp.pedir is None or wsp.echospacing is None: wsp.log.write( " -WARNING: Fieldmap images supplied but pedir and echospacing required for distortion correction\n" ) return wsp.sub("fieldmap") wsp.log.write( " - Calculating distortion correction from fieldmap images using EPI_REG\n" ) epi_reg_opts = { "inweight": wsp.inweight, "init": wsp.reg.asl2struc, "fmap": wsp.fmap, "fmapmag": wsp.fmapmag, "fmapmagbrain": wsp.fmapmagbrain, "pedir": wsp.pedir, "echospacing": wsp.echospacing, "nofmapreg": wsp.ifnone("nofmapreg", False), } # Windows can't run epi_reg as it's a batch script. Use our experimental python # implementation but use the standard epi_reg on other platforms until the python # version is better tested if sys.platform.startswith("win"): import oxasl.epi_reg as pyepi result = pyepi.epi_reg(wsp, epi=wsp.reg.nativeref, **epi_reg_opts) else: result = epi_reg(epi=wsp.asldata.perf_weighted(), t1=wsp.structural.struc, t1brain=wsp.structural.brain, out=fsl.LOAD, wmseg=wsp.structural.wm_seg, log=wsp.fsllog, **epi_reg_opts) # Occasionally we end up with NaN in the output of epi_reg and this will ruin the entire distcorr warp. # So remove any NaN values and replace with zero. warp_struc = result["out_warp"] wsp.fieldmap.warp_struc = Image(np.nan_to_num(warp_struc.data, nan=0), header=warp_struc.header) wsp.fieldmap.asl2struc = result["out"] wsp.fieldmap.struc2asl = np.linalg.inv(wsp.fieldmap.asl2struc) result = fsl.convertwarp(out=fsl.LOAD, ref=wsp.reg.nativeref, warp1=wsp.fieldmap.warp_struc, postmat=wsp.fieldmap.struc2asl, rel=True, log=wsp.fsllog) wsp.fieldmap.warp = result["out"] page = wsp.report.page("fmap") page.heading("Fieldmap distortion correction", level=0) page.text("PE direction: %s" % wsp.pedir) page.text("Echo spacing: %f s" % wsp.echospacing) page.heading("Correction warps", level=1) for dim in range(3): img = Image(wsp.fieldmap.warp.data[..., dim], header=wsp.fieldmap.warp.header) page.text("Dimension %i" % dim) page.image("fmap_warp%i" % dim, LightboxImage(img))