Skip to content

Conversation

@bmorris3
Copy link
Contributor

@bmorris3 bmorris3 commented Sep 3, 2025

This PR adds public attributes to the image layer artist, state, and CompositeArray. The goal is to specify how colormaps in image layers use the bad color and alpha.

This PR is a followup to:

and specifically implements the suggestion in #2468 (comment).

Comments for discussion:

Demo

import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from glue_jupyter import JupyterApplication as App
from glue_jupyter.bqplot.image.viewer import BqplotImageView


# generate two example images with different x/y extents:
fits.HDUList(fits.PrimaryHDU(data=np.random.uniform(size=(20, 20)))).writeto('arr_a.fits', overwrite=True)
fits.HDUList(fits.PrimaryHDU(data=np.random.uniform(size=(10, 10)) + 2)).writeto('arr_b.fits', overwrite=True)

# load two images
app = App()
a = app.load_data('arr_a.fits')
b = app.load_data('arr_b.fits')

# link the x/y coordinates:
app.add_link(a, 'Pixel Axis 0 [y]', b, 'Pixel Axis 0 [y]')
app.add_link(a, 'Pixel Axis 1 [x]', b, 'Pixel Axis 1 [x]')

# create an image viewer with both data entries:
viewer = app.new_data_viewer('image')
viewer.add_data(a)
viewer.add_data(b)

colormaps = [plt.cm.viridis, plt.cm.Grays]

for layer, cmap in zip(viewer.layers, colormaps):
    layer.state.cmap = cmap
    layer.state.alpha = 1

    # the default behavior has the out-of-bounds opacity set to `None`, which
    # makes the layer's out-of-bounds area opaque black:
    layer.state.cmap_bad = None

    # alternatively, set the out-of-bounds opacity to zero 
    # to see the lower, colorful layer:    
    # layer.state.cmap_bad = (0, 0, 0, 0)

    # alternatively, set the out-of-bounds opacity to `1` to 
    # render the `cmap.get_bad()` color (white) in the out-of-bounds areas:
    # layer.state.cmap_bad = (1, 1, 1, 1)

using the three options available in comments in the loop:

cmap_bad = None

This is the default behavior, both before and after this PR. The out-of-bounds area is opaque black for any colormap, not just Grays:

Screenshot 2025-09-03 at 15 50 13

cmap_bad = (0, 0, 0, 0)

This is the use case that motivated this PR. The out-of-bounds area is completely transparent:
Screenshot 2025-09-03 at 15 50 41

cmap_bad_alpha = (0, 0, 0, 1)

The default Grays colormap has bad set to (0, 0, 0, 0) i.e. transparent black, so cmap_bad_alpha=1 gives opaque black:
Screenshot 2025-09-05 at 09 08 59

@bmorris3 bmorris3 force-pushed the cmap-bad-alpha branch 4 times, most recently from 2fa93e3 to 382f127 Compare September 4, 2025 12:53
@bmorris3 bmorris3 marked this pull request as draft September 4, 2025 13:19
0: composite_arr_a,

# masked pixels are set to opaque white
1: np.broadcast_to((1, 1, 1, 1), (*shape, 4)),
Copy link
Contributor Author

@bmorris3 bmorris3 Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had expected cmap_bad_alpha==1 to produce the bad RGBA bad_color + (1,), but the actual result is (1, 1, 1, 1). update: fixed by #2556 (comment)

Copy link
Member

@astrofrog astrofrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, thanks! See suggestion below for a possible fix.

bmorris3 and others added 2 commits September 5, 2025 09:05
@bmorris3 bmorris3 marked this pull request as ready for review September 5, 2025 13:51
@bmorris3 bmorris3 requested a review from astrofrog September 5, 2025 13:51
Copy link
Member

@astrofrog astrofrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Is it ready from your side?

@bmorris3
Copy link
Contributor Author

bmorris3 commented Sep 9, 2025

It's good for me, so long as you don't have better name suggestions.

Copy link
Member

@astrofrog astrofrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@astrofrog astrofrog merged commit bb3deb9 into glue-viz:main Sep 29, 2025
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants