Skip to content

Commit

Permalink
ENH,FIX add dropout option to make_svg, fix transparency (#426)
Browse files Browse the repository at this point in the history
* ENH,FIX add dropout option to make_svg, fix transparency

* DOC improve description of dropout
  • Loading branch information
mvdoc committed Jan 28, 2022
1 parent 56eb1d8 commit d492d13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
14 changes: 11 additions & 3 deletions cortex/quickflat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
41 changes: 37 additions & 4 deletions cortex/quickflat/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d492d13

Please sign in to comment.