From d492d139f7cced0cbaa64953a191cf9f4fefe249 Mon Sep 17 00:00:00 2001 From: Matteo Visconti di Oleggio Castello Date: Fri, 28 Jan 2022 13:04:49 -0800 Subject: [PATCH] ENH,FIX add dropout option to make_svg, fix transparency (#426) * ENH,FIX add dropout option to make_svg, fix transparency * DOC improve description of dropout --- cortex/quickflat/utils.py | 14 ++++++++++--- cortex/quickflat/view.py | 41 +++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/cortex/quickflat/utils.py b/cortex/quickflat/utils.py index 1134301c4..62e4d62b4 100644 --- a/cortex/quickflat/utils.py +++ b/cortex/quickflat/utils.py @@ -320,9 +320,15 @@ def _make_hatch_image(hatch_data, height, sampler='nearest', hatch_space=4, reca space between hatch lines (in pixels) recache : boolean - + Returns + ------- + hatchim : RGBA array + flatmap image with hatches over hatch_data """ - dmap, _ = make_flatmap_image(hatch_data, height=height, sampler=sampler, recache=recache) + dmap, _ = make_flatmap_image( + hatch_data, height=height, sampler=sampler, recache=recache, nanmean=True + ) + mask_nans = np.isnan(dmap) hx, hy = np.meshgrid(range(dmap.shape[1]), range(dmap.shape[0])) hatchpat = (hx+hy)%(2*hatch_space) < 2 @@ -332,7 +338,9 @@ def _make_hatch_image(hatch_data, height, sampler='nearest', hatch_space=4, reca hatchpat = np.logical_or(hatchpat, hatchpat[:,::-1]).astype(float) hatchim = np.dstack([1-hatchpat]*3 + [hatchpat]) - hatchim[:, : ,3] *= np.clip(dmap, 0, 1).astype(float) + hatchim[:, :, 3] *= np.clip(dmap, 0, 1).astype(float) + # Set nans to alpha = 0. for transparency + hatchim[mask_nans, 3] = 0. return hatchim diff --git a/cortex/quickflat/view.py b/cortex/quickflat/view.py index 80afe7823..6307427a0 100644 --- a/cortex/quickflat/view.py +++ b/cortex/quickflat/view.py @@ -319,7 +319,7 @@ def make_png(fname, braindata, recache=False, pixelwise=True, sampler='nearest', plt.close(fig) def make_svg(fname, braindata, with_labels=False, with_curvature=True, layers=['rois'], - height=1024, overlay_file=None, **kwargs): + height=1024, overlay_file=None, with_dropout=False, **kwargs): """Save an svg file of the desired flatmap. This function creates an SVG file with vector graphic ROIs overlaid on a single png image. @@ -341,14 +341,23 @@ def make_svg(fname, braindata, with_labels=False, with_curvature=True, layers=[' height : int Height of PNG in pixels overlay_file : str - Custom ROI overlays file to use - + Custom ROI overlays file to use + with_dropout : bool or Dataview + If True or a cortex.Dataview object, hatches will be overlaid on top of the + flatmap to indicate areas with dropout. If set to True, the dropout areas will + be estimated from the intensity of the reference image. If set to a + cortex.Dataview object, values in the dataset will be considered dropout areas. + The transparency of the hatches is proportional to the intensity of the values + in the dropout dataset. """ fp = io.BytesIO() - from matplotlib.pylab import imsave + from matplotlib.pyplot import imsave ## Render PNG file & retrieve image data arr, extents = make_flatmap_image(braindata, height=height, **kwargs) + # Set nans to alpha = 0. to enable transparency when saving as PNG + mask_nans = np.isnan(arr[..., 3]) + arr[mask_nans, 3] = 0. if hasattr(braindata, 'cmap'): imsave(fp, arr, cmap=braindata.cmap, vmin=braindata.vmin, vmax=braindata.vmax) @@ -372,6 +381,30 @@ def make_svg(fname, braindata, with_labels=False, with_curvature=True, layers=[' fpc.seek(0) image_data = [binascii.b2a_base64(fpc.read()), pngdata] + # Add dropout -- modified from quickflat.view.make_figure + if with_dropout: + dataview = dataset.normalize(braindata) + # Support old api: + if isinstance(with_dropout, dataset.Dataview): + hatch_data = with_dropout + else: + hatch_data = utils.get_dropout(dataview.subject, dataview.xfmname) + sampler = kwargs.get("sampler", "nearest") + recache = kwargs.get("recache", False) + hatch_space = 4 + hatch_color = (0, 0, 0) + hatchim = composite._make_hatch_image( + hatch_data, height, sampler, recache=recache, hatch_space=hatch_space + ) + hatchim[:, :, 0] = hatch_color[0] + hatchim[:, :, 1] = hatch_color[1] + hatchim[:, :, 2] = hatch_color[2] + fpc = io.BytesIO() + imsave(fpc, hatchim) + fpc.seek(0) + # Add dropout above data layer + image_data += [binascii.b2a_base64(fpc.read())] + ## Create and save SVG file roipack = utils.db.get_overlay(braindata.subject, overlay_file) roipack.get_svg(fname, layers=layers, labels=with_labels, with_ims=image_data)