Skip to content

Commit

Permalink
Properly update world components if Data.coords is set and world comp…
Browse files Browse the repository at this point in the history
…onents already exist
  • Loading branch information
astrofrog committed Dec 14, 2016
1 parent e330ee5 commit 2d34c11
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
28 changes: 26 additions & 2 deletions glue/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ def __init__(self, label="", **kwargs):
Extra array-like keywords are extracted into components
"""
# Coordinate conversion object
self.coords = Coordinates()

self._shape = ()

# Components
self._components = OrderedDict()
self._pixel_component_ids = ComponentIDList()
self._world_component_ids = ComponentIDList()

# Coordinate conversion object
self.coords = Coordinates()

self.id = ComponentIDDict(self)

# Metadata
Expand Down Expand Up @@ -110,6 +112,19 @@ def __init__(self, label="", **kwargs):
# uniquely identify them.
self.uuid = str(uuid.uuid4())

@property
def coords(self):
"""
The coordinates object for the data.
"""
return self._coords

@coords.setter
def coords(self, value):
self._coords = value
if len(self.components) > 0:
self._update_world_components()

@property
def subsets(self):
"""
Expand Down Expand Up @@ -442,12 +457,21 @@ def add_component_link(self, link, label=None):
return dc

def _create_pixel_and_world_components(self):
self._update_pixel_components()
self._update_world_components()

def _update_pixel_components(self):
for i in range(self.ndim):
comp = CoordinateComponent(self, i)
label = pixel_label(i, self.ndim)
cid = PixelComponentID(i, "Pixel Axis %s" % label, hidden=True)
self.add_component(comp, cid)
self._pixel_component_ids.append(cid)

def _update_world_components(self):
for cid in self._world_component_ids[:]:
self.remove_component(cid)
self._world_component_ids.remove(cid)
if self.coords:
for i in range(self.ndim):
comp = CoordinateComponent(self, i, world=True)
Expand Down
16 changes: 16 additions & 0 deletions glue/core/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,19 @@ def test_update_values_from_data_invalid():
with pytest.raises(ValueError) as exc:
d1.update_values_from_data(d2)
assert exc.value.args[0] == "Non-unique component labels in new data"


def test_update_coords():

# Make sure that when overriding coords, the world coordinate components
# are updated.

x = Data(x=[1, 2, 3])

class CustomCoordinates(Coordinates):
def axis_label(self, axis):
return 'Custom {0}'.format(axis)

x.coords = CustomCoordinates()

assert sorted(cid.label for cid in x.world_component_ids) == ['Custom 0']

0 comments on commit 2d34c11

Please sign in to comment.