Beispiel #1
0
def test_rect(ds):
    roi = glue.core.roi.RectangularROI(xmin=1, xmax=4, ymin=0, ymax=3)
    state = glue.core.subset.RoiSubsetState(roi=roi,
                                            xatt=ComponentID('x'),
                                            yatt=ComponentID('y'))
    selection = translate_subset_state(ds, state)
    assert ds.evaluate_selection_mask(selection).tolist() == [0, 0, 1, 0, 0]
Beispiel #2
0
def test_polygon(ds):
    xmin = 1
    xmax = 4
    ymin = 0
    ymax = 3
    vx = [xmin, xmax, xmax, xmin, xmin]
    vy = [ymin, ymin, ymax, ymax, ymin]
    roi = glue.core.roi.PolygonalROI(vx=vx, vy=vy)
    state = glue.core.subset.RoiSubsetState(roi=roi,
                                            xatt=ComponentID('x'),
                                            yatt=ComponentID('y'))
    selection = translate_subset_state(ds, state)
    assert ds.evaluate_selection_mask(selection).tolist() == [0, 1, 1, 0, 0]
Beispiel #3
0
 def test_invalid_plot(self):
     layer = self.add_data_and_attributes()
     assert self.layer_drawn(layer)
     c = ComponentID('bad id')
     self.client.xatt = c
     assert not self.layer_drawn(layer)
     self.client.xatt = self.ids[0]
Beispiel #4
0
 def __init__(self, ds):
     super(DataVaex, self).__init__()
     self.ds = ds
     self.id = ComponentIDDict(self)
     self._main_components = [
         ComponentID(label=str(k), parent=self)
         for k in self.ds.get_column_names(strings=False, virtual=True)
     ]
Beispiel #5
0
 def main_components(self):
     main = []
     for cid in self._original_data.main_components:
         if cid not in self._original_cid_to_cid:
             cid_new = ComponentID(label=cid.label, parent=self)
             self._original_cid_to_cid[cid] = cid_new
             self._cid_to_original_cid[cid_new] = cid
         main.append(self._original_cid_to_cid[cid])
     return main
Beispiel #6
0
    def test_component_replaced(self):
        # regression test for 508
        self.client.register_to_hub(self.collect.hub)
        self.client.add_layer(self.data)
        self.client.component = self.data.components[0]

        test = ComponentID('test')
        self.data.update_id(self.client.component, test)
        assert self.client.component is test
Beispiel #7
0
def pandas_to_glue(df, label='data', name_map=default_name_map):
    """Convert dataframe to glue.core.data.Data. Glue categorical variables require hashing,
    store array of unhashable components in ComponentID._unhashable. Override column names
    in name_map with dictionary values.

    """

    data = Data(label=label)
    for c in df.columns:
        c_name = map_column_names(c)
        try:
            data.add_component(df[c], c_name)
        except TypeError:
            # pd.factorize error with int list input to CategoricalComponent
            r = ['%09d' % i for i in range(len(df[c]))]
            cc = CategoricalComponent(r)
            c_id = ComponentID(c_name)
            c_id._unhashable = np.array(df[c])
            data.add_component(cc, c_id)
    return data
Beispiel #8
0
    def test_component_replaced(self):

        # regression test for 508 - if a component ID is replaced, we should
        # make sure that the component ID is selected if the old component ID
        # was selected

        self.viewer.add_data(self.data)
        self.viewer.state.x_att = self.data.components[0]
        test = ComponentID('test')
        self.data.update_id(self.viewer.state.x_att, test)
        assert self.viewer.state.x_att is test
        assert combo_as_string(self.viewer.options_widget().ui.combodata_x_att) == 'test:y'
Beispiel #9
0
    def test_component_replaced(self):

        # regression test for 508 - if a component ID is replaced, we should
        # make sure that the component ID is selected if the old component ID
        # was selected

        self.viewer.add_data(self.data)
        self.viewer.state.x_att = self.data.id['x']
        test = ComponentID('test')
        self.data.update_id(self.viewer.state.x_att, test)
        assert self.viewer.state.x_att is test
        assert combo_as_string(self.viewer.options_widget().ui.combosel_x_att) == 'Main components:test:y:z:Coordinate components:Pixel Axis 0 [x]:World 0'
Beispiel #10
0
 def test_redraw_called_on_invalid_plot(self):
     """ Plot should be updated when given invalid data,
     to sync layers' disabled/invisible states"""
     ctr = MagicMock()
     layer = self.add_data_and_attributes()
     assert self.layer_drawn(layer)
     c = ComponentID('bad id')
     self.client._redraw = ctr
     ct0 = ctr.call_count
     self.client.xatt = c
     ct1 = ctr.call_count
     ncall = ct1 - ct0
     expected = len(self.client.artists)
     assert ncall >= expected
     self.client.xatt = self.ids[0]
Beispiel #11
0
    def add_component_link(self, link, label=None):
        """
        Shortcut method for generating a new
        :class:`~glue.core.component.DerivedComponent` from a ComponentLink
        object, and adding it to a data set.

        Parameters
        ----------
        link : :class:`~glue.core.component_link.ComponentLink`
            The link to use to generate a new component
        label : :class:`~glue.core.component_id.ComponentID` or str
            The ComponentID or label to attach to.

        Returns
        -------
        component : :class:`~glue.core.component.DerivedComponent`
            The component that was added
        """
        if label is not None:
            if not isinstance(label, ComponentID):
                label = ComponentID(label, parent=self)
            link.set_to_id(label)

        if link.get_to_id() is None:
            raise TypeError("Cannot add component_link: "
                            "has no 'to' ComponentID")

        for cid in link.get_from_ids():
            if cid not in self.components:
                raise ValueError(
                    "Can only add internal links with add_component_link "
                    "- use DataCollection.add_link to add inter-data links")

        dc = DerivedComponent(self, link)
        to_ = link.get_to_id()
        self.add_component(dc, label=to_)
        return dc
Beispiel #12
0
def test_component_id_combo_helper_replaced():

    # Make sure that when components are replaced, the equivalent combo index
    # remains selected and an event is broadcast so that any attached callback
    # properties can be sure to pull the latest text/userData.

    callback = MagicMock()

    state = ExampleState()
    state.add_callback('combo', callback)

    dc = DataCollection([])

    helper = ComponentIDComboHelper(state, 'combo', dc)

    assert selection_choices(state, 'combo') == ""

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    callback.reset_mock()

    dc.append(data1)
    helper.append_data(data1)

    callback.assert_called_once_with(0)
    callback.reset_mock()

    assert selection_choices(state, 'combo') == "x:y"

    new_id = ComponentID(label='new')

    data1.update_id(data1.id['x'], new_id)

    callback.assert_called_once_with(0)
    callback.reset_mock()

    assert selection_choices(state, 'combo') == "new:y"
Beispiel #13
0
def test_component_id_combo_helper_replaced():

    # Make sure that when components are replaced, the equivalent combo index
    # remains selected and an event is broadcast so that any attached callback
    # properties can be sure to pull the latest text/userData.

    callback = MagicMock()

    combo = QtWidgets.QComboBox()
    combo.currentIndexChanged.connect(callback)

    dc = DataCollection([])

    helper = ComponentIDComboHelper(combo, dc)

    assert combo_as_string(combo) == ""

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    callback.reset_mock()

    dc.append(data1)
    helper.append_data(data1)

    callback.assert_called_once_with(0)
    callback.reset_mock()

    assert combo_as_string(combo) == "x:y"

    new_id = ComponentID(label='new')

    data1.update_id(data1.id['x'], new_id)

    callback.assert_called_once_with(0)
    callback.reset_mock()

    assert combo_as_string(combo) == "new:y"
Beispiel #14
0
    def __init__(self, full_table):

        super(NOAOSQLData, self).__init__()

        # TODO: shouldn't need this!
        self.coords = None

        self._full_table = full_table

        # Get column names and types
        schema, table = full_table.split('.')
        query = SQL_TABLE_INFO.format(schema=schema, table=table)

        result = self._query_sql(query)
        columns = list(zip(result['column_name'], result['data_type']))

        # For now only keep numerical columns
        columns = [item for item in columns if ('double' in item[1] or 'real' in item[1])]

        self._columns = [item[0] for item in columns]

        self._cids = [ComponentID(label=name, parent=self) for name in self._columns]

        self._kind = {name: 'numerical' for name in self._columns}
Beispiel #15
0
    def add_component_link(self, link, label=None):
        """ Shortcut method for generating a new :class:`~glue.core.component.DerivedComponent`
        from a ComponentLink object, and adding it to a data set.

        :param link: :class:`~glue.core.component_link.ComponentLink`
        :param label: The ComponentID or label to attach to.
        :type label: :class:`~glue.core.component_id.ComponentID` or str

        :returns:
            The :class:`~glue.core.component.DerivedComponent` that was added
        """
        if label is not None:
            if not isinstance(label, ComponentID):
                label = ComponentID(label)
            link.set_to_id(label)

        if link.get_to_id() is None:
            raise TypeError("Cannot add component_link: "
                            "has no 'to' ComponentID")

        dc = DerivedComponent(self, link)
        to_ = link.get_to_id()
        self.add_component(dc, to_)
        return dc
Beispiel #16
0
def test_range(ds):
    state = glue.core.subset.RangeSubsetState(lo=2, hi=3, att=ComponentID('x'))
    selection = translate_subset_state(ds, state)
    assert ds.evaluate_selection_mask(selection).tolist() == [0, 0, 1, 1, 0]
Beispiel #17
0
 def test_component_replaced(self):
     # regression test for #508
     data = self.add_data_and_attributes()
     test = ComponentID('test')
     data.update_id(self.client.xatt, test)
     assert self.client.xatt is test
Beispiel #18
0
 def __init__(self, client):
     super(DaskData, self).__init__()
     self.data_cid = ComponentID(label='data', parent=self)
     self.client = client
Beispiel #19
0
    def add_component(self, component, label, hidden=False):
        """ Add a new component to this data set.

        :param component: object to add. Can be a Component,
                          array-like object, or ComponentLink

        :param label:
              The label. If this is a string,
              a new :class:`glue.core.component_id.ComponentID` with this label will be
              created and associated with the Component

        :type component: :class:`~glue.core.component.Component` or
                         array-like
        :type label: :class:`str` or :class:`~glue.core.component_id.ComponentID`

        :raises:

           TypeError, if label is invalid
           ValueError if the component has an incompatible shape

        :returns:

           The ComponentID associated with the newly-added component
        """

        if isinstance(component, ComponentLink):
            component = DerivedComponent(self, component)

        if not isinstance(component, Component):
            component = Component.autotyped(component)

        if isinstance(component, DerivedComponent):
            component.set_parent(self)

        if not (self._check_can_add(component)):
            raise ValueError("The dimensions of component %s are "
                             "incompatible with the dimensions of this data: "
                             "%r vs %r" % (label, component.shape, self.shape))

        if isinstance(label, ComponentID):
            component_id = label
        else:
            component_id = ComponentID(label, hidden=hidden)

        is_present = component_id in self._components
        self._components[component_id] = component

        first_component = len(self._components) == 1
        if first_component:
            if isinstance(component, DerivedComponent):
                raise TypeError("Cannot add a derived component as "
                                "first component")
            self._shape = component.shape
            self._create_pixel_and_world_components()

        if self.hub and (not is_present):
            msg = DataAddComponentMessage(self, component_id)
            self.hub.broadcast(msg)
            msg = ComponentsChangedMessage(self)
            self.hub.broadcast(msg)

        return component_id
Beispiel #20
0
    def add_component(self, component, label):
        """ Add a new component to this data set.

        :param component: object to add. Can be a Component,
                          array-like object, or ComponentLink

        :param label:
              The label. If this is a string,
              a new :class:`glue.core.component_id.ComponentID` with this label will be
              created and associated with the Component

        :type component: :class:`~glue.core.component.Component` or
                         array-like
        :type label: :class:`str` or :class:`~glue.core.component_id.ComponentID`

        :raises:

           TypeError, if label is invalid
           ValueError if the component has an incompatible shape

        :returns:

           The ComponentID associated with the newly-added component
        """

        if isinstance(component, ComponentLink):
            return self.add_component_link(component, label=label)

        if not isinstance(component, Component):
            component = Component.autotyped(component)

        if isinstance(component, DerivedComponent):
            if len(self._components) == 0:
                raise TypeError(
                    "Cannot add a derived component as a first component")
            component.set_parent(self)

        if not (self._check_can_add(component)):
            raise ValueError("The dimensions of component %s are "
                             "incompatible with the dimensions of this data: "
                             "%r vs %r" % (label, component.shape, self.shape))

        if isinstance(label, ComponentID):
            component_id = label
            if component_id.parent is None:
                component_id.parent = self
        else:
            component_id = ComponentID(label, parent=self)

        if len(self._components) == 0:
            # TODO: make sure the following doesn't raise a componentsraised message
            self._create_pixel_and_world_components(ndim=component.ndim)

        # In some cases, such as when loading a session, we actually disable the
        # auto-creation of pixel and world coordinates, so the first component
        # may be a coordinate component with no shape. Therefore we only set the
        # shape once a component has a valid shape rather than strictly on the
        # first component.
        if self._shape == () and component.shape != ():
            self._shape = component.shape

        is_present = component_id in self._components
        self._components[component_id] = component

        if self.hub and not is_present:
            msg = DataAddComponentMessage(self, component_id)
            self.hub.broadcast(msg)
            msg = ComponentsChangedMessage(self)
            self.hub.broadcast(msg)

        return component_id
Beispiel #21
0
 def __init__(self):
     super(RandomData, self).__init__()
     # self.client = client
     self.data_cid = ComponentID(label='data', parent=self)
Beispiel #22
0
def _load_component_id(rec, context):
    return ComponentID(rec['label'])