def create_links(self, cids_left, cids_right, forwards=None, backwards=None): if self.cids is None: raise Exception( "MultiLink.__init__ was not called before creating links") if forwards is None and backwards is None: raise TypeError("Must supply either forwards or backwards") if forwards is not None: for i, r in enumerate(cids_right): func = PartialResult(forwards, i, name_prefix=self.__class__.__name__ + ".") self.append(ComponentLink(cids_left, r, func)) if backwards is not None: for i, l in enumerate(cids_left): func = PartialResult(backwards, i, name_prefix=self.__class__.__name__ + ".") self.append(ComponentLink(cids_right, l, func))
def __init__(self, cid1, cid2, forwards, backwards): """ Return 2 links that connect input ComponentIDs in both directions :param cid1: First ComponentID to link :param cid2: Second ComponentID to link :param forwards: Function which maps cid1 to cid2 (e.g. cid2=f(cid1)) :param backwards: Function which maps cid2 to cid1 (e.g. cid1=f(cid2)) :returns: Two :class:`~glue.core.component_link.ComponentLink` instances, specifying the link in each direction """ self.append(ComponentLink([_toid(cid1)], _toid(cid2), forwards)) self.append(ComponentLink([_toid(cid2)], _toid(cid1), backwards))
def test_cancel(self): # Make sure that changes aren't saved if dialog is cancelled # This is a bit more detailed test that checks that things update # correctly as we change various settings link1 = ComponentLink([self.data1.id['x']], self.data2.id['c']) self.data_collection.add_link(link1) dialog = LinkEditor(self.data_collection) dialog.show() link_widget = dialog.link_widget link_widget.state.data1 = self.data1 link_widget.state.data2 = self.data2 link_widget.state.current_link.x = self.data1.id['y'] assert link_widget.combos1.itemAtPosition(0, 1).widget().currentText() == 'y' add_identity_link = get_action(link_widget, 'identity') add_identity_link.trigger() assert link_widget.listsel_current_link.count() == 2 dialog.reject() links = self.data_collection.external_links assert len(links) == 1 assert isinstance(links[0], ComponentLink) assert links[0].get_from_ids()[0] is self.data1.id['x'] assert links[0].get_to_id() is self.data2.id['c']
def app(dataxyz, dataxz, data_volume, data_image): link1 = ['dataxyz.x'], ['dataxz.x'], lambda x: x link2 = ['dataxyz.y'], ['dataxz.z'], lambda y: y + 1, lambda z: z - 1 app = gj.jglue(dataxyz=dataxyz, dataxz=dataxz, links=[link1, link2]) app.add_data(data_volume=data_volume) app.add_data(data_image=data_image) link1 = ComponentLink([data_image.id['Pixel Axis 0 [y]']], dataxyz.id['y']) link2 = ComponentLink([data_image.id['Pixel Axis 1 [x]']], dataxyz.id['x']) app.data_collection.add_link([link1, link2]) link1 = ComponentLink([data_volume.id['Pixel Axis 0 [z]']], dataxyz.id['z']) link2 = ComponentLink([data_volume.id['Pixel Axis 1 [y]']], dataxyz.id['y']) link3 = ComponentLink([data_volume.id['Pixel Axis 2 [x]']], dataxyz.id['x']) app.data_collection.add_link([link1, link2, link3]) return app
def __init__(self, cids_left, cids_right, forwards=None, backwards=None): cids_left = list(map(_toid, cids_left)) cids_right = list(map(_toid, cids_right)) if forwards is None and backwards is None: raise TypeError("Must supply either forwards or backwards") if forwards is not None: for i, r in enumerate(cids_right): func = PartialResult(forwards, i, name_prefix=self.__class__.__name__ + ".") self.append(ComponentLink(cids_left, r, func)) if backwards is not None: for i, l in enumerate(cids_left): func = PartialResult(backwards, i, name_prefix=self.__class__.__name__ + ".") self.append(ComponentLink(cids_right, l, func))
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 __init__(self, cids1=None, cids2=None, data1=None, data2=None): super(BaseMultiLink, self).__init__(data1=data1, data2=data2, cids1=cids1, cids2=cids2) links = [] if self.forwards is not None: if self.forwards is identity: links.append(ComponentLink(cids1, cids2[0])) elif len(cids2) == 1: links.append(ComponentLink(cids1, cids2[0], self.forwards)) else: for i, r in enumerate(cids2): func = PartialResult(self.forwards, i, name_prefix=self.__class__.__name__ + ".") links.append(ComponentLink(cids1, r, using=func, input_names=self.labels1)) if self.backwards is not None: if self.backwards is identity: links.append(ComponentLink(cids2, cids1[0])) elif len(cids1) == 1: links.append(ComponentLink(cids2, cids1[0], self.backwards)) else: for i, l in enumerate(cids1): func = PartialResult(self.backwards, i, name_prefix=self.__class__.__name__ + ".") links.append(ComponentLink(cids2, l, using=func, input_names=self.labels1)) self._links[:] = links
def link(self): """ Return a `glue.core.component_link.ComponentLink` object. """ if self._function is not None: cids_in = [getattr(self, name) for name in self.input_names] cid_out = getattr(self, self.output_names[0]) return ComponentLink(cids_in, cid_out, using=self._function, inverse=self._inverse) else: cids_in = [getattr(self, name) for name in self.input_names] cids_out = [getattr(self, name) for name in self.output_names] return self._helper_class(cids1=cids_in, cids2=cids_out, data1=self.data_in, data2=self.data_out)
def setup_method(self): self.data1 = Data(x=[1, 2, 3], y=[3.5, 4.5, -1.0], z=['a', 'r', 'w']) self.data2 = Data(a=[3, 4, 1], b=[1.5, -2.0, 3.5], c=['y', 'e', 'r']) # Add a derived component so that we can test how we deal with existing ones components = dict((cid.label, cid) for cid in self.data2.components) pc = ParsedCommand('{a}', components) link = ParsedComponentLink(ComponentID('d'), pc) self.data2.add_component_link(link) self.data_collection = DataCollection([self.data1, self.data2]) link = ComponentLink([self.data1.id['x']], self.data2.id['a']) self.data_collection.add_link(link) self.listener1 = ChangeListener(self.data1) self.listener2 = ChangeListener(self.data2)
def link(self): """ Return a `glue.core.component_link.ComponentLink` object. """ if self._function is not None: cids1 = [getattr(self, name) for name in self.names1] cid_out = getattr(self, self.names2[0]) return ComponentLink(cids1, cid_out, using=self._function, inverse=self._inverse) else: cids1 = [getattr(self, name) for name in self.names1] cids2 = [getattr(self, name) for name in self.names2] return self._helper_class(cids1=cids1, cids2=cids2, data1=self.data1, data2=self.data2)
def test_preexisting_links_twodata(self): # Regression test for an issue that occurred specifically if there were # exactly two datasets and pre-existing links (since this means that # the window opens with a current_link selected by default) data1 = Data(x=[1, 2, 3], y=[2, 3, 4], z=[6, 5, 4], label='data1') data2 = Data(a=[2, 3, 4], b=[4, 5, 4], c=[3, 4, 1], label='data2') data_collection = DataCollection([data1, data2]) link1 = ComponentLink([data1.id['x']], data2.id['c']) data_collection.add_link(link1) dialog = LinkEditor(data_collection) dialog.show() dialog.accept()
def test_enabled_layers(self): data2 = Data(label='d1', y=np.arange(24).reshape((3, 4, 2))) 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 __init__(self, cid1, cid2): self.append(ComponentLink([_toid(cid1)], _toid(cid2)))
def deg_arcsec(degree, arcsecond): return [ComponentLink([degree], arcsecond, using=lambda d: d * 3600), ComponentLink([arcsecond], degree, using=lambda a: a / 3600)]
def test_preexisting_links(self): # Check that things work properly if there are pre-existing links app = get_qapp() link1 = ComponentLink([self.data1.id['x']], self.data2.id['c']) def add(x, y): return x + y def double(x): return x * 2 def halve(x): return x / 2 link2 = ComponentLink([self.data2.id['a'], self.data2.id['b']], self.data3.id['j'], using=add) link3 = ComponentLink([self.data3.id['i']], self.data2.id['c'], using=double, inverse=halve) self.data_collection.add_link(link1) self.data_collection.add_link(link2) self.data_collection.add_link(link3) dialog = LinkEditor(self.data_collection) dialog.show() link_widget = dialog.link_widget link_widget.state.data1 = self.data1 link_widget.state.data2 = self.data2 assert link_widget.listsel_current_link.count() == 1 assert link_widget.link_details.text() == '' assert non_empty_rows_count(get_link_io(link_widget)) == 5 assert get_link_io(link_widget).itemAtPosition(1, 1).widget().currentText() == 'x' assert get_link_io(link_widget).itemAtPosition(4, 1).widget().currentText() == 'c' link_widget.state.data1 = self.data3 assert link_widget.listsel_current_link.count() == 2 assert link_widget.link_details.text() == '' assert non_empty_rows_count(get_link_io(link_widget)) == 6 assert get_link_io(link_widget).itemAtPosition(1, 1).widget().currentText() == 'a' assert get_link_io(link_widget).itemAtPosition(2, 1).widget().currentText() == 'b' assert get_link_io(link_widget).itemAtPosition(5, 1).widget().currentText() == 'j' link_widget.state.current_link = type(link_widget.state).current_link.get_choices(link_widget.state)[1] assert link_widget.listsel_current_link.count() == 2 assert link_widget.link_details.text() == '' assert non_empty_rows_count(get_link_io(link_widget)) == 5 assert get_link_io(link_widget).itemAtPosition(1, 1).widget().currentText() == 'i' assert get_link_io(link_widget).itemAtPosition(4, 1).widget().currentText() == 'c' dialog.accept() links = self.data_collection.external_links assert len(links) == 3 assert isinstance(links[0], ComponentLink) assert links[0].get_from_ids()[0] is self.data1.id['x'] assert links[0].get_to_id() is self.data2.id['c'] assert links[0].get_using() is identity assert isinstance(links[1], ComponentLink) assert links[1].get_from_ids()[0] is self.data2.id['a'] assert links[1].get_from_ids()[1] is self.data2.id['b'] assert links[1].get_to_id() is self.data3.id['j'] assert links[1].get_using() is add assert isinstance(links[2], ComponentLink) assert links[2].get_from_ids()[0] is self.data3.id['i'] assert links[2].get_to_id() is self.data2.id['c'] assert links[2].get_using() is double assert links[2].get_inverse() is halve
def test_preexisting_links(self): # Check that things work properly if there are pre-existing links link1 = ComponentLink([self.data1.id['x']], self.data2.id['c']) def add(x, y): return x + y def double(x): return x * 2 def halve(x): return x / 2 link2 = ComponentLink([self.data2.id['a'], self.data2.id['b']], self.data3.id['j'], using=add) link3 = ComponentLink([self.data3.id['i']], self.data2.id['c'], using=double, inverse=halve) # Test using a LinkHelper link since that caused a bug earlier link4 = LinkSame(self.data1.id['z'], self.data2.id['c']) self.data_collection.add_link(link1) self.data_collection.add_link(link2) self.data_collection.add_link(link3) self.data_collection.add_link(link4) dialog = LinkEditor(self.data_collection) dialog.show() link_widget = dialog.link_widget link_widget.state.data1 = self.data1 link_widget.state.data2 = self.data2 assert link_widget.listsel_current_link.count() == 2 assert link_widget.link_details.text() == '' assert non_empty_rows_count(link_widget.combos1) == 1 assert non_empty_rows_count(link_widget.combos2) == 1 assert link_widget.combos1.itemAtPosition(0, 1).widget().currentText() == 'x' assert link_widget.combos2.itemAtPosition(0, 1).widget().currentText() == 'c' link_widget.state.current_link = type(link_widget.state).current_link.get_choices(link_widget.state)[1] assert link_widget.link_details.text() == '' assert non_empty_rows_count(link_widget.combos1) == 1 assert non_empty_rows_count(link_widget.combos2) == 1 assert link_widget.combos1.itemAtPosition(0, 1).widget().currentText() == 'z' assert link_widget.combos2.itemAtPosition(0, 1).widget().currentText() == 'c' link_widget.state.data1 = self.data3 assert link_widget.listsel_current_link.count() == 2 assert link_widget.link_details.text() == '' assert non_empty_rows_count(link_widget.combos1) == 1 assert non_empty_rows_count(link_widget.combos2) == 2 assert link_widget.combos1.itemAtPosition(0, 1).widget().currentText() == 'j' assert link_widget.combos2.itemAtPosition(0, 1).widget().currentText() == 'a' assert link_widget.combos2.itemAtPosition(1, 1).widget().currentText() == 'b' link_widget.state.current_link = type(link_widget.state).current_link.get_choices(link_widget.state)[1] assert link_widget.listsel_current_link.count() == 2 assert link_widget.link_details.text() == '' assert non_empty_rows_count(link_widget.combos1) == 1 assert non_empty_rows_count(link_widget.combos2) == 1 assert link_widget.combos1.itemAtPosition(0, 1).widget().currentText() == 'i' assert link_widget.combos2.itemAtPosition(0, 1).widget().currentText() == 'c' dialog.accept() links = self.data_collection.external_links assert len(links) == 4 assert isinstance(links[0], ComponentLink) assert links[0].get_from_ids()[0] is self.data1.id['x'] assert links[0].get_to_id() is self.data2.id['c'] assert links[0].get_using() is identity assert isinstance(links[1], ComponentLink) assert links[1].get_from_ids()[0] is self.data2.id['a'] assert links[1].get_from_ids()[1] is self.data2.id['b'] assert links[1].get_to_id() is self.data3.id['j'] assert links[1].get_using() is add assert isinstance(links[2], ComponentLink) assert links[2].get_from_ids()[0] is self.data3.id['i'] assert links[2].get_to_id() is self.data2.id['c'] assert links[2].get_using() is double assert links[2].get_inverse() is halve assert isinstance(links[3], LinkSame) assert len(links[3].cids1) == 1 assert links[3].cids1[0] is self.data1.id['z'] assert len(links[3].cids2) == 1 assert links[3].cids2[0] is self.data2.id['c'] assert links[3].forwards is identity
def __init__(self, cid1, cid2): super(LinkSame, self).__init__() self._cid1 = cid1 self._cid2 = cid2 self.append(ComponentLink([_toid(cid1)], _toid(cid2)))
def test_basic(self, accept): # Set up an existing link link1 = ComponentLink([self.data1.id['x']], self.data2.id['c']) self.data_collection.add_link(link1) # Set up two suggested links def add(x, y): return x + y def double(x): return x * 2 def halve(x): return x / 2 link2 = ComponentLink([self.data2.id['a'], self.data2.id['b']], self.data3.id['j'], using=add) link3 = ComponentLink([self.data3.id['i']], self.data2.id['c'], using=double, inverse=halve) suggested_links = [link2, link3] dialog = AutoLinkPreview('test autolinker', self.data_collection, suggested_links) dialog.show() link_widget = dialog.link_widget link_widget.state.data1 = self.data1 link_widget.state.data2 = self.data2 assert link_widget.listsel_current_link.count() == 1 link_widget.state.data1 = self.data3 assert link_widget.listsel_current_link.count() == 2 if accept: dialog.accept() links = self.data_collection.external_links assert len(links) == 3 assert isinstance(links[0], ComponentLink) assert links[0].get_from_ids()[0] is self.data1.id['x'] assert links[0].get_to_id() is self.data2.id['c'] assert links[0].get_using() is identity assert isinstance(links[1], ComponentLink) assert links[1].get_from_ids()[0] is self.data2.id['a'] assert links[1].get_from_ids()[1] is self.data2.id['b'] assert links[1].get_to_id() is self.data3.id['j'] assert links[1].get_using() is add assert isinstance(links[2], ComponentLink) assert links[2].get_from_ids()[0] is self.data3.id['i'] assert links[2].get_to_id() is self.data2.id['c'] assert links[2].get_using() is double assert links[2].get_inverse() is halve else: dialog.reject() links = self.data_collection.external_links assert len(links) == 1 assert isinstance(links[0], ComponentLink) assert links[0].get_from_ids()[0] is self.data1.id['x'] assert links[0].get_to_id() is self.data2.id['c'] assert links[0].get_using() is identity