def test_1d_world_link(): x, y = r(10), r(10) d1 = Data(label='d1', x=x) d2 = Data(label='d2', y=y, coords=IdentityCoordinates(n_dim=1)) dc = DataCollection([d1, d2]) dc.add_link(LinkSame(d2.world_component_ids[0], d1.id['x'])) assert d2.world_component_ids[0] in d1.externally_derivable_components np.testing.assert_array_equal(d1[d2.world_component_ids[0]], x) np.testing.assert_array_equal(d1[d2.pixel_component_ids[0]], x)
def test_2d_world_link(): """Should be able to grab pixel coords after linking world""" x, y = r(10), r(10) cat = Data(label='cat', x=x, y=y) im = Data(label='im', inten=r((3, 3)), coords=IdentityCoordinates(n_dim=2)) dc = DataCollection([cat, im]) dc.add_link(LinkSame(im.world_component_ids[0], cat.id['x'])) dc.add_link(LinkSame(im.world_component_ids[1], cat.id['y'])) np.testing.assert_array_equal(cat[im.pixel_component_ids[0]], x) np.testing.assert_array_equal(cat[im.pixel_component_ids[1]], y)
def test_pixel_world_linking(self): data = Data(label='data', x=[[1, 2], [3, 4]], y=[[5, 6], [7, 8]], coords=IdentityCoordinates(ndim=2)) layer_state = ImageLayerState(layer=data, viewer_state=self.state) self.state.layers.append(layer_state) w1, w2 = data.world_component_ids p1, p2 = data.pixel_component_ids self.state.reference_data = data # Setting world components should set the pixel ones self.state.x_att_world = w1 self.state.y_att_world = w2 assert self.state.x_att is p1 assert self.state.y_att is p2 # Setting one component to the same as the other should trigger the other # to flip to prevent them from both being the same self.state.x_att_world = w2 assert self.state.x_att is p2 assert self.state.y_att is p1 assert self.state.y_att_world is w1 self.state.y_att_world = w2 assert self.state.x_att is p1 assert self.state.x_att_world is w1 assert self.state.y_att is p2 # Changing x_att and y_att should change the world equivalents self.state.x_att = p2 assert self.state.x_att_world is w2 assert self.state.y_att is p1 assert self.state.y_att_world is w1 self.state.y_att = p2 assert self.state.y_att_world is w2 assert self.state.x_att is p1 assert self.state.x_att_world is w1
def test_enabled_layers(self): data2 = Data(label='d1', y=np.arange(24).reshape((3, 4, 2)), coords=IdentityCoordinates(n_dim=3)) self.data_collection.append(data2) self.viewer.add_data(self.data) self.viewer.add_data(data2) assert self.viewer.layers[0].enabled assert not self.viewer.layers[1].enabled self.data_collection.add_link( ComponentLink([data2.world_component_ids[1]], self.data.world_component_ids[0], using=lambda x: 2 * x)) assert self.viewer.layers[0].enabled assert self.viewer.layers[1].enabled
def load_data(path, factory=None, **kwargs): """ Use a factory to load a file and assign a label. This is the preferred interface for loading data into Glue, as it logs metadata about how data objects relate to files on disk. Parameters ---------- path : str Path to the file. factory : callable Factory function to use. Defaults to :func:`glue.core.data_factories.auto_data` callback. Extra keywords are passed through to factory functions. """ from glue.qglue import parse_data coord_first = kwargs.pop('coord_first', True) force_coords = kwargs.pop('force_coords', False) def as_data_objects(ds, lbl): # pack other container types like astropy tables # into glue data objects for d in ds: if isinstance(d, BaseData): yield d continue for item in parse_data(d, lbl): yield item if factory is None: factory = find_factory(path, **kwargs) if factory is None: raise KeyError("Don't know how to open file: %s" % path) lbl = data_label(path) d = as_list(factory(path, **kwargs)) d = list(as_data_objects(d, lbl)) log = LoadLog(path, factory, kwargs) for item in d: # NOTE: The LoadLog infrastructure is specifically designed for Data # objects in mind and not more general data classes. if not isinstance(item, Data): continue if item.coords is None and force_coords: item.coords = IdentityCoordinates(n_dim=item.ndim) if not item.label: item.label = lbl log.log(item) # attaches log metadata to item if coord_first: # We just follow the order in which the components are now loaded, # which is coordinate components first, followed by all other # components for cid in item.coordinate_components + item.main_components: log.log(item.get_component(cid)) else: # In this case the first component was the first one that is not a # coordinate component, followed by the coordinate components, # followed by the remaining components. cid = item.main_components[0] log.log(item.get_component(cid)) for icid, cid in enumerate(item.coordinate_components): log.log(item.get_component(cid)) for icid, cid in enumerate(item.main_components[1:]): log.log(item.get_component(cid)) if len(d) == 1: # unpack single-length lists for user convenience return d[0] return d
def test_component_id_combo_helper(): state = ExampleState() dc = DataCollection([]) helper = ComponentIDComboHelper(state, 'combo', dc) assert selection_choices(state, 'combo') == "" data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1') dc.append(data1) helper.append_data(data1) assert selection_choices(state, 'combo') == "x:y" data2 = Data(a=[1, 2, 3], b=['a', 'b', 'c'], label='data2', coords=IdentityCoordinates(n_dim=1)) dc.append(data2) helper.append_data(data2) assert selection_choices(state, 'combo') == "data1:x:y:data2:a:b" helper.categorical = False assert selection_choices(state, 'combo') == "data1:x:y:data2:a" helper.numeric = False assert selection_choices(state, 'combo') == "data1:data2" helper.categorical = True helper.numeric = True helper.pixel_coord = True assert selection_choices( state, 'combo' ) == "data1:main:x:y:coord:Pixel Axis 0 [x]:data2:main:a:b:coord:Pixel Axis 0 [x]" helper.world_coord = True assert selection_choices( state, 'combo' ) == "data1:main:x:y:coord:Pixel Axis 0 [x]:data2:main:a:b:coord:Pixel Axis 0 [x]:World 0" helper.pixel_coord = False assert selection_choices( state, 'combo') == "data1:main:x:y:data2:main:a:b:coord:World 0" helper.world_coord = False dc.remove(data2) assert selection_choices(state, 'combo') == "x:y" data1['z'] = data1.id['x'] + 1 assert selection_choices(state, 'combo') == "main:x:y:derived:z" helper.derived = False assert selection_choices(state, 'combo') == "x:y" data1.id['x'].label = 'z' assert selection_choices(state, 'combo') == "z:y" helper.remove_data(data1) assert selection_choices(state, 'combo') == ""
def setup_method(self, method): self.data_collection = DataCollection() self.array = np.arange(3024).reshape((6, 7, 8, 9)) # The reference dataset. Shape is (6, 7, 8, 9). self.data1 = Data(x=self.array, coords=IdentityCoordinates(ndim=4)) self.data_collection.append(self.data1) # A dataset with the same shape but not linked. Shape is (6, 7, 8, 9). self.data2 = Data(x=self.array) self.data_collection.append(self.data2) # A dataset with the same number of dimesnions but in a different # order, linked to the first. Shape is (9, 7, 6, 8). self.data3 = Data( x=np.moveaxis(self.array, (3, 1, 0, 2), (0, 1, 2, 3))) self.data_collection.append(self.data3) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[0], self.data3.pixel_component_ids[2])) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[1], self.data3.pixel_component_ids[1])) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[2], self.data3.pixel_component_ids[3])) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[3], self.data3.pixel_component_ids[0])) # A dataset with fewer dimensions, linked to the first one. Shape is # (8, 7, 6) self.data4 = Data(x=self.array[:, :, :, 0].transpose()) self.data_collection.append(self.data4) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[0], self.data4.pixel_component_ids[2])) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[1], self.data4.pixel_component_ids[1])) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[2], self.data4.pixel_component_ids[0])) # A dataset with even fewer dimensions, linked to the first one. Shape # is (8, 6) self.data5 = Data(x=self.array[:, 0, :, 0].transpose()) self.data_collection.append(self.data5) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[0], self.data5.pixel_component_ids[1])) self.data_collection.add_link( LinkSame(self.data1.pixel_component_ids[2], self.data5.pixel_component_ids[0])) # A dataset that is not on the same pixel grid and requires reprojection self.data6 = Data() self.data6.coords = SimpleCoordinates() self.array_nonaligned = np.arange(60).reshape((5, 3, 4)) self.data6['x'] = np.array(self.array_nonaligned) self.data_collection.append(self.data6) self.data_collection.add_link( LinkSame(self.data1.world_component_ids[0], self.data6.world_component_ids[1])) self.data_collection.add_link( LinkSame(self.data1.world_component_ids[1], self.data6.world_component_ids[2])) self.data_collection.add_link( LinkSame(self.data1.world_component_ids[2], self.data6.world_component_ids[0])) self.viewer_state = ImageViewerState() self.viewer_state.layers.append( ImageLayerState(viewer_state=self.viewer_state, layer=self.data1)) self.viewer_state.layers.append( ImageLayerState(viewer_state=self.viewer_state, layer=self.data2)) self.viewer_state.layers.append( ImageLayerState(viewer_state=self.viewer_state, layer=self.data3)) self.viewer_state.layers.append( ImageLayerState(viewer_state=self.viewer_state, layer=self.data4)) self.viewer_state.layers.append( ImageLayerState(viewer_state=self.viewer_state, layer=self.data5)) self.viewer_state.layers.append( ImageLayerState(viewer_state=self.viewer_state, layer=self.data6)) self.viewer_state.reference_data = self.data1
def test_slice_label(): d = Data(x=np.zeros((2, 3, 4)), coords=IdentityCoordinates(n_dim=3)) assert _slice_label(d, (0, 'y', 'x')) == 'World 0' assert _slice_label(d, ('y', 0, 'x')) == 'World 1' assert _slice_label(d, ('y', 'x', 0)) == 'World 2'