Skip to content

Commit 14a7dd7

Browse files
committed
Fix issue with cube collapsing when 2-d and 3-d datasets were mixed in image viewer
1 parent 241edb3 commit 14a7dd7

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

glue/viewers/image/state.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,16 @@ def slice_to_bound(slc, size):
448448

449449
# We now get the fixed resolution buffer
450450

451+
# When using aggregation, the full_view contains slices even for dimensions that will eventually
452+
# be collapsed, so it's easiest to use broadcast=True even though it might be slower.
453+
broadcast = agg_func is not None
454+
451455
if isinstance(self.layer, BaseData):
452456
image = self.layer.compute_fixed_resolution_buffer(full_view, target_data=self.viewer_state.reference_data,
453-
target_cid=self.attribute, broadcast=False, cache_id=self.uuid)
457+
target_cid=self.attribute, broadcast=broadcast, cache_id=self.uuid)
454458
else:
455459
image = self.layer.data.compute_fixed_resolution_buffer(full_view, target_data=self.viewer_state.reference_data,
456-
subset_state=self.layer.subset_state, broadcast=False, cache_id=self.uuid)
460+
subset_state=self.layer.subset_state, broadcast=broadcast, cache_id=self.uuid)
457461

458462
# We apply aggregation functions if needed
459463

glue/viewers/profile/qt/tests/test_profile_tools.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from numpy.testing import assert_allclose
55

66
from glue.core import Data
7+
from glue.core.link_helpers import LinkSame
78
from glue.tests.helpers import PYSIDE2_INSTALLED # noqa
89
from glue.app.qt import GlueApplication
910
from glue.utils import nanmean
@@ -23,12 +24,21 @@ def setup_method(self, method):
2324
self.data.coords = SimpleCoordinates()
2425
self.data['x'] = np.arange(240).reshape((30, 4, 2)).astype(float)
2526

27+
self.data_2d = Data(label='d2')
28+
self.data_2d['x'] = np.arange(8).reshape((4, 2)).astype(float)
29+
2630
self.app = GlueApplication()
2731
self.session = self.app.session
2832
self.hub = self.session.hub
2933

3034
self.data_collection = self.session.data_collection
3135
self.data_collection.append(self.data)
36+
self.data_collection.append(self.data_2d)
37+
38+
link1 = LinkSame(self.data.pixel_component_ids[1], self.data_2d.pixel_component_ids[0])
39+
link2 = LinkSame(self.data.pixel_component_ids[2], self.data_2d.pixel_component_ids[1])
40+
self.data_collection.add_link(link1)
41+
self.data_collection.add_link(link2)
3242

3343
self.viewer = self.app.new_data_viewer(ProfileViewer)
3444
self.viewer.state.function = 'mean'
@@ -214,3 +224,48 @@ def test_collapse_reverse(self, capsys):
214224
out, err = capsys.readouterr()
215225
assert out.strip() == ""
216226
assert err.strip() == ""
227+
228+
def test_collapse_with_2d_in_image(self, capsys):
229+
230+
# Regression test for a bug that occurred when collapsing data and when a 2-d
231+
# dataset was present in the image viewer.
232+
233+
self.viewer.add_data(self.data)
234+
self.viewer.add_data(self.data_2d)
235+
236+
image_viewer = self.app.new_data_viewer(ImageViewer)
237+
image_viewer.add_data(self.data)
238+
image_viewer.add_data(self.data_2d)
239+
240+
self.profile_tools.ui.tabs.setCurrentIndex(2)
241+
242+
# First try in pixel coordinates
243+
244+
self.viewer.state.x_att = self.data.pixel_component_ids[0]
245+
246+
# Force events to be processed to make sure that the callback functions
247+
# for the computation thread are executed (since they rely on signals)
248+
self.viewer.layers[0].wait()
249+
process_events()
250+
251+
x, y = self.viewer.axes.transData.transform([[0.9, 4]])[0]
252+
self.viewer.axes.figure.canvas.button_press_event(x, y, 1)
253+
x, y = self.viewer.axes.transData.transform([[15.1, 4]])[0]
254+
self.viewer.axes.figure.canvas.motion_notify_event(x, y, 1)
255+
256+
process_events()
257+
258+
self.profile_tools.ui.button_collapse.click()
259+
260+
process_events()
261+
262+
assert isinstance(image_viewer.state.slices[0], AggregateSlice)
263+
assert image_viewer.state.slices[0].slice.start == 1
264+
assert image_viewer.state.slices[0].slice.stop == 15
265+
assert image_viewer.state.slices[0].center == 0
266+
assert image_viewer.state.slices[0].function is nanmean
267+
268+
out, err = capsys.readouterr()
269+
270+
assert err.strip() == ""
271+
assert out.strip() == ""

0 commit comments

Comments
 (0)