Skip to content

Instantly share code, notes, and snippets.

@ChrisBeaumont
Last active August 29, 2015 14:10
Show Gist options
  • Save ChrisBeaumont/0bc7713c84d2e141b51f to your computer and use it in GitHub Desktop.
Save ChrisBeaumont/0bc7713c84d2e141b51f to your computer and use it in GitHub Desktop.
Local moment maps

This demo shows how you might use Glue's custom viewer functionality to create a "local moment map". The local moment map behaves like a normal 3D image slicer, but displays the channels offset in front of / behind the current slice as red/blue slices.

The demo uses this 12CO COMPLETE cube of Perseus

You'll need to update Glue in order to display subsets

import shifted_maps
from glue import custom_viewer
import numpy as np
from glue.clients.ds9norm import DS9Normalize
from glue.core.subset import RoiSubsetState
from glue.core.util import color2rgb
viewer = custom_viewer('Local Moments', # name of viewer
center=(0, 256), # slider for velocity channel
width=(1, 10), # slider for RGB channel offset
data='att', # dropbox to choose data attribute to image
redraw_on_settings_change = True # rerender data if settings change
)
@viewer.plot_data
def draw(axes, data, center, width):
"""
Make a 3 color image showing a cube slice (g),
slice - width (r), slice + width (b)
"""
if data is None or data.size == 0 or data.ndim != 3:
return
# extract rgb slices
g = data[center]
r = data[max(center - width, 0)]
b = data[min(center + width, data.shape[0] - 1)]
rgb = np.nan_to_num(np.dstack((r, g, b)))
# manually rescale intensities from 0-1
norm = DS9Normalize()
norm.vmin = 0.1
norm.vmax = 10.
norm.stretch = 'arcsinh'
rgb = norm(rgb)
axes.imshow(rgb, origin='lower')
@viewer.plot_subset
def draw_subset(axes, subset, center, style):
"""
Draw a subset as a semi-transparent box
"""
# get the RGB color of the subset
r, g, b = color2rgb(style.color)
# extract a True/False array of mask, at the center slice
m = subset.to_mask((center,))
# turn into a RGBA array
m = np.dstack((r * m, g * m, b * m, m * 0.7))
m = (255 * m).astype(np.uint8)
# draw it
axes.imshow(m, alpha=0.5, origin='lower', interpolation='nearest')
@viewer.make_selector
def make_selector(roi):
# turn a selection into a subset
# XXX this is brittle, since it relies on the names 'Pixel X'
# and 'Pixel y' for the pixel coordinates. This will probably
# cause problems related to name ambiguities if several cubes
# are loaded into Glue
return RoiSubsetState(xatt='Pixel x', yatt='Pixel y', roi=roi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment