Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rosteen committed Nov 10, 2023
1 parent ba92189 commit 7859456
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
6 changes: 3 additions & 3 deletions jdaviz/configs/default/plugins/plot_options/plot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def apply_RGB_presets(self):
visible_layers = []
for layer in image_layers:
self.layer_selected = layer
if self.image_visible:
if self.image_visible.value:
visible_layers.append(layer)

# Set opacity to something that seems sensible
Expand All @@ -638,7 +638,7 @@ def apply_RGB_presets(self):
if n_visible > 2:
default_opacity = 1 / math.log2(n_visible)
# Sample along a colormap if we have too many layers
if len(visible_layers) > 5:
if n_visible > 5:
cmap = matplotlib.colormaps['gist_rainbow'].resampled(n_visible)

def _rgb_to_hex(rgb):
Expand All @@ -648,7 +648,7 @@ def _rgb_to_hex(rgb):
for i in range(n_visible):
self.layer_selected = visible_layers[i]
self.image_opacity_value = default_opacity
if len(visible_layers) < 6:
if n_visible < 6:
self.image_color_value = preset_colors[i]
else:
self.image_color_value = _rgb_to_hex(cmap(i))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
from astropy import units as u
from astropy.nddata import NDData
import matplotlib
from numpy.testing import assert_allclose
from photutils.datasets import make_4gaussians_image

Expand Down Expand Up @@ -250,3 +251,56 @@ def test_update_knots_mismatched_length():
stretch = SplineStretch()
with pytest.raises(ValueError):
stretch.update_knots(x=[0, 0.1, 0.2], y=[0, 0.05])


def test_apply_presets(imviz_helper):
arr = np.arange(36).reshape(6, 6)
po = imviz_helper.plugins['Plot Options']

preset_colors = [po._obj.swatches_palette[0][0], "#0000FF", "#00FF00",
po._obj.swatches_palette[1][0],]

# Test applying presets with < 6 layers

for i in range(4):
imviz_helper.load_data(arr, data_label=f"array_{i}")

po.image_color_mode = "Monochromatic"
po.apply_RGB_presets()

for i in range(4):
po.layer = f"array_{i}"
assert po.stretch_function.value == "arcsinh"
assert po.stretch_preset.value == 99
assert po.image_color.value == preset_colors[i]

# Test applying presets with > 5 layers

for i in range(4):
imviz_helper.load_data(arr, data_label=f"array_{i+4}")

po.layer = "array_5"
po.image_visible = False
po.layer = "array_3"

po.apply_RGB_presets()

assert po.layer.selected == "array_3"

colorbar_colors = matplotlib.colormaps['gist_rainbow'].resampled(7)
color_ind = 0

def _rgb_to_hex(rgb):
rgb = [int(x * 255) for x in rgb]
return f"#{rgb[0]:02x}{rgb[1]:02x}{rgb[2]:02x}{rgb[3]:02x}"

for i in range(8):
po.layer = f"array_{i}"
if i == 5:
# Make sure this one didn't change
assert po.stretch_function.value == "linear"
else:
assert po.stretch_function.value == "arcsinh"
assert po.stretch_preset.value == 99
assert po.image_color.value == _rgb_to_hex(colorbar_colors(color_ind))
color_ind += 1

0 comments on commit 7859456

Please sign in to comment.