Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when changing the number of bins on a histogram with modified/deleted data #2451

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions glue/core/application_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def new_data_viewer(self, viewer_class, data=None, state=None):
self.add_widget(c)
return c

def add_widget(self, viewer):
pass

@catch_error("Failed to save session")
def save_session(self, path, include_data=False, absolute_paths=True):
"""
Expand Down
16 changes: 12 additions & 4 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
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']
'StateAttributeLimitsHelper', 'StateAttributeSingleValueHelper', 'StateAttributeHistogramHelper']


@saver(CallbackList)
Expand Down Expand Up @@ -428,9 +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):
for att in self._cache:
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 Down
59 changes: 59 additions & 0 deletions glue/viewers/histogram/tests/test_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from glue.viewers.common.viewer import Viewer
from glue.viewers.histogram.state import HistogramViewerState
from glue.core.application_base import Application
from glue.core.data import Data


class TestHistogramViewer(Viewer):
_state_cls = HistogramViewerState


def test_remove_data_collection():

# Regression test for a bug that caused an IncompatibleAttribute
# error when updating the number of bins in a histogram after
# removing a dataset from the DataCollection (this was due to
# a caching issue)

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.hist_n_bin = 30

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
Loading