def test_numerical_values_changed(self): # Here we slice two of the dimensions and then compare the results to a # manually sliced dataset. derived = IndexedData(self.data, (None, 2, None, 4, None)) data_collection = DataCollection([self.data, derived]) class CustomListener(HubListener): def __init__(self, hub): self.received = 0 hub.subscribe(self, NumericalDataChangedMessage, handler=self.receive_message) def receive_message(self, message): self.received += 1 listener = CustomListener(data_collection.hub) assert listener.received == 0 derived.indices = (None, 3, None, 5, None) assert listener.received == 1 derived.indices = (None, 3, None, 5, None) assert listener.received == 1
def setup_method(self, method): self.data = Data(x=[1, 2, 3, 2, 2, 3, 1]) figure = MagicMock() self.collect = DataCollection() self.client = HistogramClient(self.collect, figure) self.axes = self.client.axes self.hub = self.collect.hub self.connect()
def setup_method(self, method): self.data = Data(x=[0, 0, 0, 1, 2, 3, 3, 10, 20], y=[-1, -1, -1, -2, -2, -2, -3, -5, -7]) self.subset = self.data.new_subset() self.collect = DataCollection(self.data) self.client = HistogramClient(self.collect, FIGURE) self.axes = self.client.axes FIGURE.canvas.draw = MagicMock() assert FIGURE.canvas.draw.call_count == 0
def load_data_files(datafiles): """Load data files and return a DataCollection""" from glue.core.data_collection import DataCollection from glue.core.data_factories import auto_data, load_data dc = DataCollection() for df in datafiles: dc.append(load_data(df, auto_data)) return dc
def test_multi_tab(self): d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) app = GlueApplication(dc) w1 = app.new_data_viewer(HistogramWidget, data=d) app.new_tab() w2 = app.new_data_viewer(HistogramWidget, data=d) assert app.viewers == ((w1, ), (w2, )) self.check_clone(app)
def setup_method(self, method): self.data = Data(y=[-1, -1, -1, -2, -2, -2, -3, -5, -7]) self.data.add_component( CategoricalComponent(['a', 'a', 'a', 'b', 'c', 'd', 'd', 'e', 'f']), 'x') self.subset = self.data.new_subset() self.collect = DataCollection(self.data) self.client = HistogramClient(self.collect, FIGURE) self.axes = self.client.axes FIGURE.canvas.draw = MagicMock() assert FIGURE.canvas.draw.call_count == 0
def __init__(self, application=None, data_collection=None, command_stack=None, hub=None): self.application = application self.data_collection = data_collection or DataCollection() self.hub = self.data_collection.hub self.command_stack = command_stack or CommandStack() self.command_stack.session = self self.edit_subset_mode = EditSubsetMode() self.edit_subset_mode.data_collection = self.data_collection
def test_links(self): d1 = Data(label='x', x=[1, 2, 3]) d2 = Data(label='y', y=[3, 4, 8]) dc = DataCollection([d1, d2]) link = ComponentLink([d1.id['x']], d2.id['y'], doubler) dc.add_link(link) np.testing.assert_array_equal(d1['y'], [2, 4, 6]) app = GlueApplication(dc) self.check_clone(app)
def main(): app = QApplication(sys.argv) win = QMainWindow() data = example_data.simple_image() dc = DataCollection([data]) histo_client = HistogramWidget(dc) hub = Hub(dc, histo_client) win.setCentralWidget(histo_client) win.show() sys.exit(app.exec_())
def test_subset_groups_remain_synced_after_restore(self): # regrssion test for 352 d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) dc.new_subset_group() app = GlueApplication(dc) app2 = clone(app) sg = app2.data_collection.subset_groups[0] assert sg.style.parent is sg sg.style.color = '#112233' assert sg.subsets[0].style.color == '#112233'
def main(): from glue.core.data import Data from glue.core.data_collection import DataCollection import numpy as np x = np.random.random((5, 5)) y = x * 3 data = DataCollection(Data(label='test', x=x, y=y)) CustomComponentWidget.create_component(data) for d in data: print(d.label) for c in d.components: print('\t%s' % c)
def test_histogram(self): d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) app = GlueApplication(dc) w = app.new_data_viewer(HistogramWidget, data=d) self.check_clone(app) dc.new_subset_group() assert len(w.layers) == 2 self.check_clone(app) w.nbins = 7 self.check_clone(app)
def __init__(self, application=None, data_collection=None, command_stack=None, hub=None): # applications can be added after instantiation self.application = application self.data_collection = data_collection or DataCollection() self.hub = self.data_collection.hub self.command_stack = command_stack or CommandStack() self.command_stack.session = self # set the global data_collection for subset updates from glue.core.edit_subset_mode import EditSubsetMode EditSubsetMode().data_collection = self.data_collection
def test_scatter_viewer(self): d = Data(label='x', x=[1, 2, 3, 4, 5], y=[2, 3, 4, 5, 6]) dc = DataCollection([d]) app = GlueApplication(dc) w = app.new_data_viewer(ScatterWidget, data=d) self.check_clone(app) s1 = dc.new_subset_group() s2 = dc.new_subset_group() assert len(w.layers) == 3 l1, l2, l3 = w.layers l1.zorder, l2.zorder = l2.zorder, l1.zorder l3.visible = False assert l3.visible is False copy = self.check_clone(app) assert copy.viewers[0][0].layers[-1].visible is False
def __init__(self, data_collection=None, session=None): if session is not None: self._session = session session.application = self self._data = session.data_collection else: self._data = data_collection or DataCollection() self._session = Session(data_collection=self._data, application=self) self._hub = self._session.hub self._cmds = self._session.command_stack self._cmds.add_callback(self._update_undo_redo_enabled) self._settings = {} for key, value, validator in settings: self._settings[key] = [value, validator]
def setup_method(self, method): self.data = example_data.test_categorical_data() self.ids = [ self.data[0].find_component_id('x1'), self.data[0].find_component_id('y1'), self.data[1].find_component_id('x2'), self.data[1].find_component_id('y2') ] self.roi_limits = (0.5, 0.5, 4, 4) self.roi_points = (np.array([1]), np.array([3])) self.collect = DataCollection() self.hub = self.collect.hub FIGURE.clf() axes = FIGURE.add_subplot(111) self.client = ScatterClient(self.collect, axes=axes) self.connect()
def test_histogram(self): d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) app = GlueApplication(dc) w = app.new_data_viewer(HistogramViewer, data=d) copy1 = self.check_clone(app) dc.new_subset_group() assert len(w.layers) == 2 copy2 = self.check_clone(app) w.nbins = 7 copy3 = self.check_clone(app) app.close() copy1.close() copy2.close() copy3.close()
def setup_method(self, method): self.data = example_data.test_data() self.ids = [ self.data[0].find_component_id('a'), self.data[0].find_component_id('b'), self.data[1].find_component_id('c'), self.data[1].find_component_id('d') ] self.roi_limits = (0.5, 0.5, 1.5, 1.5) self.roi_points = (np.array([1]), np.array([1])) self.collect = DataCollection() EditSubsetMode().data_collection = self.collect self.hub = self.collect.hub FIGURE.clf() axes = FIGURE.add_subplot(111) self.client = ScatterClient(self.collect, axes=axes) self.connect()
def test_deselect_tool_on_viewer_change(self): d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) app = GlueApplication(dc) v1 = app.new_data_viewer(HistogramViewer, data=d) v2 = app.new_data_viewer(HistogramViewer, data=d) assert v1.toolbar.active_tool is None assert v2.toolbar.active_tool is None v2.toolbar.active_tool = 'select:xrange' assert v1.toolbar.active_tool is None assert v2.toolbar.active_tool.tool_id == 'select:xrange' app.current_tab.activateNextSubWindow() assert v1.toolbar.active_tool is None assert v2.toolbar.active_tool is None v1.toolbar.active_tool = 'select:xrange' # Emit a signal without changing the active subWindow to make sure that # the tool doesn't get reset. app.current_tab.subWindowActivated.emit( app.current_tab.activeSubWindow()) assert v1.toolbar.active_tool.tool_id == 'select:xrange' assert v2.toolbar.active_tool is None app.current_tab.activateNextSubWindow() assert v1.toolbar.active_tool is None assert v2.toolbar.active_tool is None app.close()
data.hub.broadcast(msg) else: pc = ParsedCommand(self._state[data][cid_new]['equation']._cmd, components) link = ParsedComponentLink(cid_new, pc) data.add_component_link(link) # Findally, reorder components as needed data.reorder_components(cids_all) super(ArithmeticEditorWidget, self).accept() if __name__ == "__main__": # pragma: nocover from glue.utils.qt import get_qapp app = get_qapp() import numpy as np from glue.core.data import Data from glue.core.data_collection import DataCollection x = np.random.random((5, 5)) y = x * 3 dc = DataCollection() dc.append(Data(label='test1', x=x, y=y)) dc.append(Data(label='test2', a=x, b=y)) widget = ArithmeticEditorWidget(dc) widget.exec_()
def test_pixel_selection_subset_state(): data1 = Data(x=np.ones((2, 4, 3))) data2 = Data(y=np.ones((4, 3, 2))) data3 = Data(z=np.ones((2, 3))) y_id = data2.main_components[0] z_id = data3.main_components[0] slice_1d = [slice(1, 2), slice(None), slice(None)] slice_2d = [slice(None), slice(2, 3), slice(1, 2)] slice_3d = [slice(1, 2), slice(2, 3), slice(1, 2)] state_1d = PixelSubsetState(data1, slice_1d) state_2d = PixelSubsetState(data1, slice_2d) state_3d = PixelSubsetState(data1, slice_3d) states = [state_1d, state_2d, state_3d] dc = DataCollection([data1, data2, data3]) # Calling to_array with reference data should work by default, and not work # with unlinked datasets. for data in dc: for state in states: cid = data.main_components[0] if data is data1: assert_array_equal(state.to_array(data, cid), data[cid][state.slices]) else: with pytest.raises(IncompatibleAttribute): state.to_array(data, cid) # Add one set of links dc.add_link( LinkSame(data1.pixel_component_ids[0], data2.pixel_component_ids[2])) dc.add_link( LinkSame(data1.pixel_component_ids[0], data3.pixel_component_ids[0])) assert_array_equal(state_1d.to_array(data2, y_id), data2[y_id][:, :, 1:2]) with pytest.raises(IncompatibleAttribute): state_2d.to_array(data2, y_id) with pytest.raises(IncompatibleAttribute): state_3d.to_array(data2, y_id) assert_array_equal(state_1d.to_array(data3, z_id), data3[z_id][1:2]) with pytest.raises(IncompatibleAttribute): state_2d.to_array(data3, z_id) with pytest.raises(IncompatibleAttribute): state_3d.to_array(data3, z_id) # Add links with multiple components, in this case linking two cids with two cids def forwards(x, y): return x + y, x - y def backwards(x, y): return 0.5 * (x + y), 0.5 * (x - y) dc.add_link( MultiLink(data1.pixel_component_ids[1:], data2.pixel_component_ids[:2], forwards=forwards, backwards=backwards)) assert_array_equal(state_1d.to_array(data2, y_id), data2[y_id][:, :, 1:2]) assert_array_equal(state_2d.to_array(data2, y_id), data2[y_id][2:3, 1:2, :]) assert_array_equal(state_3d.to_array(data2, y_id), data2[y_id][2:3, 1:2, 1:2]) assert_array_equal(state_1d.to_array(data3, z_id), data3[z_id][1:2]) with pytest.raises(IncompatibleAttribute): state_2d.to_array(data3, z_id) with pytest.raises(IncompatibleAttribute): state_3d.to_array(data3, z_id)
from glue.viewers.image.qt import ImageViewer # create some data data_path = Path("/home/stuart/sunpy/data/iris_glue/") rasters = list(data_path.glob("*raster*")) sji = list(data_path.glob("*SJI*")) raster_data = _parse_iris_raster(read_iris_spectrograph_level2_fits(rasters), 'iris') sji_data = [] for s in sji: sji_data += load_data(s) dc = DataCollection(raster_data + sji_data) ga = GlueApplication(dc) im1 = ga.new_data_viewer(ImageViewer) im1.add_data(raster_data[0]) im2 = ga.new_data_viewer(ImageViewer) im2.add_data(sji_data[0]) class IRISLinker: def __init__(self, im1, im2): self.im_raster = im1 self.im_sji = im2 self.im_raster.state.add_callback("slices", self._raster_update)
def test_data_application(self): dc = DataCollection([Data(label='test', x=[1, 2, 3], y=[2, 3, 4])]) app = GlueApplication(dc) self.check_clone(app)
raise NotImplementedError() self.ui.layerTree[key] = value def __contains__(self, obj): return obj in self.ui.layerTree def __len__(self): return len(self.ui.layerTree) def save_subset(subset): assert isinstance(subset, core.subset.Subset) fname, fltr = QtGui.QFileDialog.getSaveFileName( caption="Select an output name", filter='FITS mask (*.fits);; Fits mask (*.fits)') fname = str(fname) if not fname: return subset.write_mask(fname) if __name__ == "__main__": from glue.core.data_collection import DataCollection collection = DataCollection() from glue.external.qt import get_qapp app = get_qapp() widget = LayerTreeWidget() widget.setup(collection) widget.show() app.exec_()