def test_get_regfrom_supplied(): """ Test a pre-supplied regfrom is preferred to anything else """ wsp = get_wsp() user_regfrom = Image(np.random.rand(5, 5, 5)) wsp.regfrom = user_regfrom reg.get_regfrom(wsp) assert (np.allclose(user_regfrom.data, wsp.reg.regfrom.data))
def test_get_regfrom_calib(): """ Test brain extracted calibration image is used for differenced data """ wsp = get_wsp() wsp.asldata = AslImage(np.random.rand(5, 5, 5, 4), tis=[1, 2], iaf="diff", ibf="rpt") reg.get_regfrom(wsp) calib_brain = brain.brain(wsp, wsp.calib, thresh=0.2) assert (np.allclose(calib_brain.data, wsp.reg.regfrom.data))
def test_get_regfrom_asldata_mean_ct(): """ Test brain extracted ASL mean is used for CT data """ wsp = get_wsp() wsp.asldata = AslImage(np.random.rand(5, 5, 5, 4), tis=[1, 2], iaf="ct", ibf="rpt") reg.get_regfrom(wsp) meanasl_brain = brain.brain(wsp, wsp.asldata.mean(), thresh=0.2) assert (np.allclose(meanasl_brain.data, wsp.reg.regfrom.data))
def generate_mask(wsp): """ Generate mask for ASL data - If a ready-made mask image is provided or has already been generated, this is returned - If a structural image is provided this will be used. Brain extraction and registration will be performed if required - If a calibration image is provided, this is used. It is assumed to be in the same space as the ASL data - If none of the above are present, the ASL data itself is averaged and brain extracted to produce the mask Required workspace attributes ----------------------------- Formally there are no required attributes, however at least one image must be provided which enables a mask to be generated. Optional workspace attributes ----------------------------- - ``asldata`` : ASL data image - ``mask`` : Existing brain mask - ``struc`` : Structural image (wholehead) - ``struc_brain``: Already brain-extracted structural image - ``asl2struc`` : Existring ASL->Structural space transformation matrix - ``calib`` : Calibration image - ``regfrom`` : ASL registration source image """ if wsp.rois is not None and wsp.rois.mask is not None: return wsp.sub("rois") # Reporting page = wsp.report.page("mask") page.heading("Mask generation", level=0) if wsp.mask is not None: wsp.rois.mask_src = "user" mask_source = "provided by user (assumed to be ASL space): %s" % wsp.mask.name wsp.rois.mask = wsp.mask elif wsp.structural.struc is not None: # Preferred option is to use brain extracted structural wsp.rois.mask_src = "struc" struc.init(wsp) page.heading("Brain extracted structural image", level=1) page.image( "struc_brain", LightboxImage(wsp.structural.brain, bgimage=wsp.structural.struc)) brain_mask_asl = reg.struc2asl(wsp, wsp.structural.brain_mask) wsp.rois.mask = Image(sp.ndimage.morphology.binary_fill_holes( (brain_mask_asl.data > 0.25)).astype(np.int), header=brain_mask_asl.header) mask_source = "generated from brain extracting structural image and registering to ASL space" else: # Alternatively, use registration image (which will be BETed calibration or mean ASL image) reg.get_regfrom(wsp) wsp.rois.mask_src = "regfrom" wsp.rois.mask = Image((wsp.reg.regfrom.data != 0).astype(np.int), header=wsp.reg.regfrom.header) mask_source = "generated from brain extracted registration ASL image" wsp.log.write("\nGenerated ASL data mask\n") wsp.log.write(" - Mask %s\n" % mask_source) page.heading("Masked ASL brain image", level=1) page.text("Mask was %s" % mask_source) page.text("PW ASL image masked by ASL-space mask") if wsp.asldata.iaf in ("diff", "tc", "ct"): page.image( "mask_outline", LightboxImage(wsp.rois.mask, bgimage=wsp.asldata.perf_weighted(), outline=True)) else: page.image( "mask_outline", LightboxImage(wsp.rois.mask, bgimage=wsp.asldata.mean(), outline=True))