Skip to content

Commit

Permalink
Expand tests and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Oct 26, 2023
1 parent c625de0 commit d905492
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
19 changes: 12 additions & 7 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
HasCallbackProperties, CallbackList)
from glue.core.state import saver, loader
from glue.core.component_id import PixelComponentID
from glue.core.exceptions import IncompatibleAttribute

__all__ = ['State', 'StateAttributeCacheHelper',
'StateAttributeLimitsHelper', 'StateAttributeSingleValueHelper', 'StateAttributeHistogramHelper']
Expand Down Expand Up @@ -192,6 +193,7 @@ def _update_attribute(self, *args):
self.update_values(attribute=self.component_id, use_default_modifiers=True)

def _update_values(self, **properties):

if hasattr(self, '_in_set'):
if self._in_set:
return
Expand Down Expand Up @@ -427,11 +429,16 @@ def __init__(self, state, attribute, random_subset=10000, max_n_bin=30,
self._common_n_bin = None

def _apply_common_n_bin(self):
print('_apply_common_n_bin')
for att in self._cache:
print('att', att.label, att.parent.label)
if not self.data.get_kind(att) == 'categorical':
self._cache[att]['n_bin'] = self._common_n_bin
for att in list(self._cache):
try:
if not self.data.get_kind(att) == 'categorical':
self._cache[att]['n_bin'] = self._common_n_bin
except IncompatibleAttribute:
# This can indicate that a dataset has been removed from the
# data collection or that the attribute has changed to a
# new dataset that is not compatible with the previous one.
# In this case we should remove the entry
self._cache.pop(att)

def _update_common_n_bin(self, common_n_bin):
if common_n_bin:
Expand All @@ -445,8 +452,6 @@ def _update_common_n_bin(self, common_n_bin):

def update_values(self, force=False, use_default_modifiers=False, **properties):

print('main update_values', properties)

if not force and not any(prop in properties for prop in ('attribute', 'n_bin')) or self.data is None:
self.set()
return
Expand Down
26 changes: 26 additions & 0 deletions glue/viewers/histogram/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,29 @@ def test_remove_data_collection():
app.data_collection.remove(data1)

viewer.state.hist_n_bin = 20


def test_incompatible_datasets():

# Regression test for a bug that caused an IncompatibleAttribute
# error when changing the dataset used in the histogram viewer to one that
# is not linked to the first dataset.

data1 = Data(x=[1, 2, 3], label='data1')
data2 = Data(y=[1, 2, 3], label='data2')

app = Application()
app.data_collection.append(data1)
app.data_collection.append(data2)

viewer = app.new_data_viewer(TestHistogramViewer)
viewer.add_data(data1)
viewer.add_data(data2)

viewer.state.x_att = data1.id['x']

viewer.state.hist_n_bin = 30

viewer.state.x_att = data2.id['y']

viewer.state.hist_n_bin = 20

0 comments on commit d905492

Please sign in to comment.