Skip to content

Commit

Permalink
Merge pull request #85 from adl1995/children_tiles
Browse files Browse the repository at this point in the history
Add property 'children' for creating four children tiles from parent tile
  • Loading branch information
cdeil committed Jul 20, 2017
2 parents fe19120 + e08ddcd commit 354eb15
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
29 changes: 28 additions & 1 deletion hips/tiles/tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ def test_tile_path(self):

dtype='int16',
shape=(512, 512),
child_order=4,
child_shape=(256, 256),
pix_idx=[[510], [5]],
pix_val=[3047],
child_ipix=[1852, 1853, 1854, 1855],
child_pix_idx=[[0], [255]],
child_pix_val=[2407, 2321, 2465, 2835],
),
dict(
meta=dict(order=3, ipix=463, file_format='jpg'),
Expand All @@ -69,8 +74,13 @@ def test_tile_path(self):

dtype='uint8',
shape=(512, 512, 3),
child_order=4,
child_shape=(256, 256, 3),
pix_idx=[[510], [5]],
pix_val=[[116, 81, 61]],
child_ipix=[1852, 1853, 1854, 1855],
child_pix_idx=[[0], [255]],
child_pix_val=[[[255, 241, 225]], [[109, 95, 86]], [[245, 214, 211]], [[137, 97, 87]]],
),
dict(
meta=dict(order=6, ipix=6112, file_format='png'),
Expand All @@ -79,8 +89,13 @@ def test_tile_path(self):

dtype='uint8',
shape=(512, 512, 4),
child_order=7,
child_shape=(256, 256, 4),
pix_idx=[[253], [5]],
pix_val=[[19, 19, 19, 255]],
child_ipix=[24448, 24449, 24450, 24451],
child_pix_idx=[[0], [255]],
child_pix_val=[[[15, 15, 15, 255]], [[20, 20, 20, 255]], [[17, 17, 17, 255]], [[13, 13, 13, 255]]],
),
]

Expand Down Expand Up @@ -117,7 +132,19 @@ def test_read(self, pars):
data = tile.data
assert data.shape == pars['shape']
assert data.dtype.name == pars['dtype']
assert_equal(tile.data[pars['pix_idx']], pars['pix_val'])
assert_equal(data[pars['pix_idx']], pars['pix_val'])

@requires_hips_extra()
@pytest.mark.parametrize('pars', HIPS_TILE_TEST_CASES)
def test_children(self, pars):
tile = self._read_tile(pars)
child_data = [_.data[pars['child_pix_idx']] for _ in tile.children]
child_ipix = [_.meta.ipix for _ in tile.children]

assert tile.children[0].meta.order == pars['child_order']
assert tile.children[0].data.shape == pars['child_shape']
assert_equal(child_ipix, pars['child_ipix'])
assert_equal(child_data, pars['child_pix_val'])

@remote_data
@requires_hips_extra()
Expand Down
24 changes: 24 additions & 0 deletions hips/tiles/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from astropy.io import fits
from ..utils import healpix_pixel_corners
from .io import tile_default_url, tile_default_path
from typing import List

__all__ = [
'HipsTileMeta',
Expand Down Expand Up @@ -166,6 +167,29 @@ def from_numpy(cls, meta: HipsTileMeta, data: np.ndarray) -> 'HipsTile':

return cls(meta, raw_data)

@property
def children(self) -> List['HipsTile']:
"""Create four children tiles from parent tile."""
children_tiles = []
w = self.data.shape[0] // 2
child_data = [
self.data[0: w, 0: w],
self.data[0: w, w: w * 2],
self.data[w: w * 2, 0: w],
self.data[w: w * 2, w: w * 2]
]

for index, data in enumerate(child_data):
meta = HipsTileMeta(
self.meta.order + 1,
self.meta.ipix * 4 + index,
self.meta.file_format,
self.meta.frame
)
children_tiles.append(self.from_numpy(meta, data))

return children_tiles

@property
def data(self) -> np.ndarray:
"""Tile pixel data (`~numpy.ndarray`)."""
Expand Down

0 comments on commit 354eb15

Please sign in to comment.