def setup_method(self, method): self.session = simple_session() self.hub = self.session.hub self.collect = self.session.data_collection self.im = core.Data(label='im', x=[[1, 2], [3, 4]], y=[[2, 3], [4, 5]]) self.cube = core.Data(label='cube', x=[[[1, 2], [3, 4]], [[1, 2], [3, 4]]], y=[[[1, 2], [3, 4]], [[1, 2], [3, 4]]]) self.widget = self.widget_cls(self.session) self.connect_to_hub() self.collect.append(self.im) self.collect.append(self.cube)
def test_combo_box_updates(): # Regression test for a bug that caused combo boxes to not be updated # correctly when switching between different datasets. session = simple_session() hub = session.hub dc = session.data_collection data1 = core.Data(label='im1', x=[[1, 2], [3, 4]], y=[[2, 3], [4, 5]]) data2 = core.Data(label='im2', a=[[1, 2], [3, 4]], b=[[2, 3], [4, 5]]) dc.append(data1) dc.append(data2) widget = ImageWidget(session) widget.register_to_hub(hub) widget.add_data(data1) assert widget.client.display_data is data1 assert widget.data.label == 'im1' assert widget.attribute.label == 'x' widget.add_data(data2) assert widget.client.display_data is data2 assert widget.data.label == 'im2' assert widget.attribute.label == 'a' widget.attribute = data2.find_component_id('b') with pytest.raises(ValueError) as exc: widget.attribute = data1.find_component_id('x') assert exc.value.args[0] == "Cannot find data 'x' in combo box" widget.data = data1 assert widget.attribute.label == 'x' widget.attribute = data1.find_component_id('y') with pytest.raises(ValueError) as exc: widget.attribute = data2.find_component_id('a') assert exc.value.args[0] == "Cannot find data 'a' in combo box" assert widget.client.display_data is data1
def setup_method(self, method): s = simple_session() self.hub = s.hub self.d1 = core.Data(x=[1, 2, 3], y=[2, 3, 4], z=[3, 4, 5], w=[4, 5, 6]) self.d1.label = 'd1' self.d2 = core.Data(x=[1, 2, 3], y=[2, 3, 4], z=[3, 4, 5], w=[4, 5, 6]) self.d2.label = 'd2' self.data = [self.d1, self.d2] self.collect = s.data_collection self.collect.append(self.data) self.widget = ScatterWidget(s) self.session = s self.connect_to_hub()
def setup_method(self, method): class TestCoords(Coordinates): def __init__(self): super().__init__(pixel_n_dim=3, world_n_dim=3) def pixel_to_world_values(self, *args): return [a * (i + 1) for i, a in enumerate(args)] def world_to_pixel_values(self, *args): return [a / (i + 1) for i, a in enumerate(args)] @property def axis_correlation_matrix(self): return np.identity(3).astype(bool) data = core.Data() data.add_component(Component(np.zeros((3, 3, 3))), 'test') data.coords = TestCoords() self.data = data self.px = CoordinateComponent(data, 2) self.py = CoordinateComponent(data, 1) self.pz = CoordinateComponent(data, 0) self.wx = CoordinateComponent(data, 2, world=True) self.wy = CoordinateComponent(data, 1, world=True) self.wz = CoordinateComponent(data, 0, world=True)
def test_load_data(self): with patch('glue.app.qt.layer_tree_widget.data_wizard') as wizard: d = core.Data(x=[1]) assert not self.layer_present(d) wizard.return_value = [d] self.widget._load_data() assert self.layer_present(d)
def __init__(self, gluemanager, modal=False, label=None): widgets.Tab.__init__(self); if isinstance(gluemanager, GlueManager): self.gluemanager = gluemanager elif isinstance(gluemanager, gcore.Data): self.gluemanager = GlueManager(gluemanager) else: self.gluemanager = GlueManager(gcore.Data(**gluemanager)) if (label is not None): self.gluemanager.data.label=label self.gluemanager.setParent(self) self.subsets = self.createSubsetsPanel() self.plots = self.createPlotsPanel() self.history = self.createHistoryPanel() self.modal = modal self.debug = self.gluemanager.debug self.children = [self.plots, self.subsets, self.history, self.debug] self.set_title(0, 'Plots') self.set_title(1, 'Subsets') self.set_title(2, 'History') self.set_title(3, 'Debug') if (self.modal): modal_window = Floatview(title = "GMW("+str(id(self))+")", mode = "split-top") with modal_window: display(self) else: display(self)
def test_range_subset(): d = core.Data(x=[1, 2, 3]) s = d.new_subset(label='range') s.subset_state = core.subset.RangeSubsetState(0.5, 2.5, att=d.id['x']) d2 = clone(d) np.testing.assert_array_equal(d2.subsets[0].to_mask(), [True, True, False])
def test_data(): d = core.Data(x=[1, 2, 3], label='testing') d2 = clone(d) assert d2.label == 'testing' np.testing.assert_array_equal(d2['x'], [1, 2, 3]) np.testing.assert_array_equal(d2['Pixel Axis 0 [x]'], [0, 1, 2])
def test_empty_subset(): d = core.Data(x=[1, 2, 3], label='testing') s = d.new_subset(label='abc') s.style.color = 'blue' s2 = clone(s) assert s.style == s2.style assert s2.style.color == 'blue'
def test_nonnumeric_first_component(self): # regression test for #208. Shouldn't complain if # first component is non-numerical data = core.Data() data.add_component(['a', 'b', 'c'], label='c1') data.add_component([1, 2, 3], label='c2') self.data_collection.append(data) self.viewer.add_data(data)
def setup_method(self, method): s = simple_session() self.hub = s.hub self.data = core.Data(label='d1', x=[1, 2, 3]) self.dc = s.data_collection self.dc.append(self.data) self.w = DendroWidget(s)
def test_inequality_subset(): d = core.Data(x=[1, 2, 3], label='testing') s = d.new_subset(label='abc') s.subset_state = d.id['x'] > 1 d2 = clone(d) s2 = d2.subsets[0] assert s.label == s2.label np.testing.assert_array_equal(s2.to_mask(), [False, True, True]) assert s.style == s2.style
def test_view_derived(view): comp = Component(np.array([[1, 2, 3], [2, 3, 4]])) d = core.Data() cid = d.add_component(comp, 'primary') cid2 = ComponentID("derived") link = core.ComponentLink([cid], cid2, using=lambda x: x * 3) dc = DerivedComponent(d, link) np.testing.assert_array_equal(dc[view], comp.data[view] * 3)
def test_complex_state(): d = core.Data(x=[1, 2, 3], y=[2, 4, 8]) s = d.new_subset(label='test') s.subset_state = (d.id['x'] > 2) | (d.id['y'] < 4) s.subset_state = s.subset_state & (d.id['x'] < 4) d2 = clone(d) s2 = d2.subsets[0] np.testing.assert_array_equal(s2.to_mask(), [True, False, True])
def test_box_roi_subset(): d = core.Data(x=[1, 2, 3], y=[2, 4, 8]) s = d.new_subset(label='box') roi = core.roi.RectangularROI(xmin=1.1, xmax=2.1, ymin=2.2, ymax=4.2) s.subset_state = core.subset.RoiSubsetState(xatt=d.id['x'], yatt=d.id['y'], roi=roi) d2 = clone(d) np.testing.assert_array_equal( d2.subsets[0].to_mask(), [False, True, False])
def test_maskify_action(self): d = core.Data(x=[1, 2, 3]) s = d.new_subset() selected = MagicMock() self.maskify_action.selected_layers = selected selected.return_value = [s] self.maskify_action.trigger() assert isinstance(s.subset_state, core.subset.MaskSubsetState)
def test_cube(self): d = core.Data(label='cube', x=np.zeros((2, 2, 2))) dc = core.DataCollection([d]) app = GlueApplication(dc) w = app.new_data_viewer(ImageWidget, d) w.slice = ('x', 'y', 1) assert w.slice == ('x', 'y', 1) c = self.check_clone(app) w2 = c.viewers[0][0] assert w2.ui.slice.slice == w.slice
def test_user_patch_is_called(): @session_patch() def a_patch(session): session["__main__"]["label"] = 'has_changed' d = core.Data(x=[1, 2, 3], label='testing') d2 = clone(d) session_patch._members.pop(-1) # clean registry assert d2.label == 'has_changed'
def setup_method(self, method): LinkSame = core.link_helpers.LinkSame d = core.Data(label='im', x=[[1, 2], [2, 3]], y=[[2, 3], [4, 5]]) d2 = core.Data(label='cat', x=[0, 1, 0, 1], y=[0, 0, 1, 1], z=[1, 2, 3, 4]) dc = core.DataCollection([d, d2]) dc.add_link(LinkSame(d.get_pixel_component_id(0), d2.id['x'])) dc.add_link(LinkSame(d.get_pixel_component_id(1), d2.id['y'])) app = GlueApplication(dc) w = app.new_data_viewer(ImageWidget, data=d) self.d = d self.app = app self.w = w self.d2 = d2 self.dc = dc
def __init__(self, spec, rest_lines): spec_data = gcore.Data(wl=spec.wavelength, flux=spec.flux, unc=spec.uncertainty.array) self.app = app = gj.jglue(spec_data) self.regions = {} # populated with spectral regions by frecord line_selection = ipywidgets.Dropdown(options=rest_lines.keys(), description='Line:') record_button = ipywidgets.Button(description='Record line') result_text = ipywidgets.Text(disabled=True, value='No line recorded') record_box = ipywidgets.HBox( [line_selection, record_button, result_text]) z_text = ipywidgets.FloatText(description='z guess:', value=0) jump_button = ipywidgets.Button(description='Jump to line') line_width_text = ipywidgets.FloatText(description='width(Ang):', value=100) jump_box = ipywidgets.HBox([z_text, jump_button, line_width_text]) def frecord(widget): wls_selected = spec_data['wl'][ spec_data.subsets[0].to_index_list()] reg = specutils.SpectralRegion( np.min(wls_selected) * spec.wavelength.unit, np.max(wls_selected) * spec.wavelength.unit) result_text.value = 'Recorded {}: {} to {}'.format( line_selection.value, reg.lower, reg.upper) self.regions[line_selection.value] = reg z_text.value = specutils.analysis.centroid( spec, reg) / rest_lines[line_selection.value] - 1 record_button.on_click(frecord) self.plot_to_jump = None def fjump(widget): if self.plot_to_jump is None: return rest_linecen = rest_lines[line_selection.value] linecen = rest_linecen * (1 + z_text.value) self.plot_to_jump.state.x_min = linecen.value + line_width_text.value / 2 self.plot_to_jump.state.x_max = linecen.value - line_width_text.value / 2 jump_button.on_click(fjump) self.action_vbox = ipywidgets.VBox([record_box, jump_box])
def test_resize(self): # Regression test for a bug that caused images to not be shown at # full resolution after resizing a widget. # This test only runs correctly on Linux on Travis at the moment, # although it works fine locally on MacOS X. I have not yet tracked # down the cause of the failure, but essentially the first time that # self.widget.client._view_window is accessed below, it is still None. # The issue is made more complicated by the fact that whether the test # succeeds or not (after removing code in ImageWidget) depends on # whether another test is run first - in particular I tried with # test_resize from test_application.py. I was able to then get the # test here to pass if the other test_resize was *not* run first. # This should be investigated more in future, but for now, it's most # important that we get the fix in. # What appears to happen when the test fails is that the QTimer gets # started but basically never ends up triggering the timeout. large = core.Data(label='largeim', x=np.random.random((1024, 1024))) self.collect.append(large) app = get_qapp() self.widget.add_data(large) self.widget.show() self.widget.resize(300, 300) time.sleep(0.5) app.processEvents() extx0, exty0 = self.widget.client._view_window[4:] # While resizing, the view window should not change until we've # waited for a bit, to avoid resampling the data every time. for res in range(10): self.widget.resize(300 + res * 30, 300 + res * 30) app.processEvents() extx, exty = self.widget.client._view_window[4:] assert extx == extx0 assert exty == exty0 time.sleep(0.5) app.processEvents() extx, exty = self.widget.client._view_window[4:] assert extx != extx0 assert exty != exty0 self.widget.close()
def setup_method(self, method): class TestCoords(object): def pixel2world(self, *args): return [a * (i + 1) for i, a in enumerate(args)] data = core.Data() data.add_component(Component(np.zeros((3, 3, 3))), 'test') data.coords = TestCoords() self.data = data self.px = CoordinateComponent(data, 2) self.py = CoordinateComponent(data, 1) self.pz = CoordinateComponent(data, 0) self.wx = CoordinateComponent(data, 2, world=True) self.wy = CoordinateComponent(data, 1, world=True) self.wz = CoordinateComponent(data, 0, world=True)
def test_3d_change_slice(self): d = core.Data(x=np.zeros((3, 4, 5))) s = DataSlice(d) changed = MagicMock() s.slice_changed.connect(changed) s._slices[0].slice_center = 2 assert s.slice == (2, 'y', 'x') assert changed.call_count == 1 s._slices[1].mode = 'slice' s._slices[1].slice_center = 0 assert s.slice == ('y', 0, 'x') assert changed.call_count == 3 s._slices[2].mode = 'slice' assert s.slice == ('y', 'x', 2) assert changed.call_count == 4
def test_apply_roi(self): x = core.Data(x=[1, 2, 3]) s = x.new_subset() dc = self.session.data_collection dc.append(x) r = MagicMock(roi.Roi) apply_roi = MagicMock() cmd = c.ApplyROI(data_collection=dc, roi=r, apply_func=apply_roi) self.stack.do(cmd) apply_roi.assert_called_once_with(r) old_state = s.subset_state s.subset_state = MagicMock(spec_set=core.subset.SubsetState) self.stack.undo() assert s.subset_state is old_state
def test_apply_roi(self): x = core.Data(x=[1, 2, 3]) s = x.new_subset() dc = self.session.data_collection dc.append(x) r = MagicMock(roi.Roi) client = MagicMock(core.client.Client) client.data = dc cmd = c.ApplyROI(client=client, roi=r) self.stack.do(cmd) client.apply_roi.assert_called_once_with(r) old_state = s.subset_state s.subset_state = MagicMock(spec_set=core.subset.SubsetState) self.stack.undo() assert s.subset_state is old_state
def __init__(self, gluemanager, modal=False, label=None, display_console=True): widgets.Tab.__init__(self) if isinstance(gluemanager, GlueManager): self.gluemanager = gluemanager elif isinstance(gluemanager, gcore.Data): self.gluemanager = GlueManager(gluemanager) else: self.gluemanager = GlueManager(gcore.Data(**gluemanager)) self.display_console = display_console if (label is not None): self.gluemanager.data.label = label self.gluemanager.setParent(self) self.subsets = self.createSubsetsPanel() self.plots = self.createPlotsPanel() self.history = self.createHistoryPanel() self.options = self.createOptions() self.modal = modal self.disable_color_update = False self.debug = self.gluemanager.debug self.children = [ widgets.VBox([self.options, self.plots]), widgets.VBox([self.subsets]) ] self.set_title(0, 'Plots') self.set_title(1, 'Subsets') if self.display_console: if (self.modal): modal_window = Floatview(title="GMW(" + str(id(self)) + ")", mode="split-top") with modal_window: display(self) else: display(self) display(self.debug)
def test_3d_change_mode(self): d = core.Data(x=np.zeros((3, 4, 5))) s = DataSlice(d) changed = MagicMock() s.slice_changed.connect(changed) assert s.slice == (1, 'y', 'x') s._slices[1].mode = 'slice' assert s.slice == ('y', 1, 'x') assert changed.call_count == 1 s._slices[2].mode = 'slice' assert s.slice == ('y', 'x', 2) assert changed.call_count == 2 s._slices[2].mode = 'y' assert s.slice == (1, 'x', 'y') assert changed.call_count == 3 s._slices[2].mode = 'x' assert s.slice == (1, 'y', 'x') assert changed.call_count == 4
def test_data_style(): d = core.Data(x=[1, 2, 3]) d.style.color = 'blue' d2 = clone(d) assert d2.style.color == 'blue'
def test_binary_component_link(): d1 = core.Data(x=[1, 2, 3]) d1['y'] = d1.id['x'] + 1 assert_equal(d1['y'], [2, 3, 4]) d2 = clone(d1) assert_equal(d2['y'], [2, 3, 4])
def test_compound_state(): d = core.Data(x=[1, 2, 3]) s = d.new_subset(label='abc') s.subset_state = (d.id['x'] > 2) | (d.id['x'] < 1.5) d2 = clone(d) np.testing.assert_array_equal(d2.subsets[0].to_mask(), [True, False, True])