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 May 7, 2018
1 parent 66ab731 commit 90f2d6c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
26 changes: 24 additions & 2 deletions glue/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def __init__(self, label="", coords=None, **kwargs):
Extra array-like keywords are extracted into components
"""
# Coordinate conversion object
self.coords = coords or Coordinates()

self._shape = ()

# Components
Expand All @@ -83,6 +82,9 @@ def __init__(self, label="", coords=None, **kwargs):
self._pixel_component_ids = ComponentIDList()
self._world_component_ids = ComponentIDList()

# Coordinate conversion object
self.coords = coords or Coordinates()

self.id = ComponentIDDict(self)

# Metadata
Expand Down Expand Up @@ -113,6 +115,19 @@ def __init__(self, label="", coords=None, **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(self.ndim)

@property
def subsets(self):
"""
Expand Down Expand Up @@ -545,14 +560,21 @@ def add_component_link(self, link, label=None):
return dc

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

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

def _update_world_components(self, ndim):
for cid in self._world_component_ids[:]:
self.remove_component(cid)
self._world_component_ids.remove(cid)
if self.coords:
for i in range(ndim):
comp = CoordinateComponent(self, i, world=True)
Expand Down
32 changes: 32 additions & 0 deletions glue/core/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import numpy as np
from numpy.testing import assert_equal
from mock import MagicMock

from glue.external import six
Expand Down Expand Up @@ -785,3 +786,34 @@ class CustomObject(object):
assert data2.meta['a'] == 1
assert data2.meta['b'] == 'test'
assert 'c' not in data2.meta


def test_update_coords():

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

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

assert len(data.components) == 3

assert_equal(data[data.world_component_ids[0]], [0, 1, 2])

class CustomCoordinates(Coordinates):

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

def world2pixel(self, *world):
return tuple([0.4 * w for w in world])

def pixel2world(self, *pixel):
return tuple([2.5 * p for p in pixel])

data.coords = CustomCoordinates()

assert len(data.components) == 3

assert_equal(data[data.world_component_ids[0]], [0, 2.5, 5])

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

0 comments on commit 90f2d6c

Please sign in to comment.