Skip to content

Commit

Permalink
Add class HipsTileMeta and update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
adl1995 committed Jun 22, 2017
1 parent 6250e9e commit 616340b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 63 deletions.
Binary file removed hips/tiles/tests/data/Npix30889.fits
Binary file not shown.
Binary file removed hips/tiles/tests/data/Npix30889.jpg
Binary file not shown.
60 changes: 30 additions & 30 deletions hips/tiles/tests/test_tile.py
@@ -1,34 +1,34 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from astropy.tests.helper import remote_data

from ..tile import HipsTile
from ..tile_meta import HipsTileMeta


class TestHipsTile:
def setup(self):
self.hips_tile_meta_fits = HipsTileMeta(order=6, ipix=30889, format='fits')
self.hips_tile_meta_jpg = HipsTileMeta(order=6, ipix=30889, format='jpg')

@remote_data
def test_fetch_read_write(self):
fits_tile = HipsTile.fetch(self.hips_tile_meta_fits,
'http://alasky.unistra.fr/2MASS/H/Norder6/Dir30000/Npix30889.fits')
jpg_tile = HipsTile.fetch(self.hips_tile_meta_jpg,
'http://alasky.unistra.fr/2MASS/H/Norder6/Dir30000/Npix30889.jpg')

data_precomp = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
assert fits_tile.data.shape == (512, 512)
assert jpg_tile.data.shape == (512, 512, 3)
assert list(fits_tile.data[510][:12]) == data_precomp

jpg_tile.write('Npix30889.jpg')
fits_tile.write('Npix30889.fits')

fits_tile = HipsTile.read(self.hips_tile_meta_fits, 'Npix30889.fits')
jpg_tile = HipsTile.read(self.hips_tile_meta_jpg, 'Npix30889.jpg')

@remote_data
def test_fetch():
fits_tile = HipsTile.fetch('fits', 'http://alasky.unistra.fr/2MASS/H/Norder6/Dir30000/Npix30889.fits')
jpg_tile = HipsTile.fetch('jpg', 'http://alasky.unistra.fr/2MASS/H/Norder6/Dir30000/Npix30889.jpg')

shape_fits_precomp = (512, 512)
shape_jpg_precomp = (512, 512, 3)
data_precomp = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
assert fits_tile.data.shape == shape_fits_precomp
assert jpg_tile.data.shape == shape_jpg_precomp
assert list(fits_tile.data[510][:12]) == data_precomp

def test_read_write():
fits_tile = HipsTile.read('fits', 'Npix30889.fits')
jpg_tile = HipsTile.read('jpg', 'Npix30889.jpg')

shape_fits_precomp = (512, 512)
shape_jpg_precomp = (512, 512, 3)
data_precomp = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
assert (fits_tile.data.shape) == shape_fits_precomp
assert (jpg_tile.data.shape) == shape_jpg_precomp
assert list(fits_tile.data[510][:12]) == data_precomp

fits_tile.write('test_file.fits')
jpg_tile.write('test_file.jpg')

path_fits = fits_tile.path / 'test_file.fits'
path_jpg = jpg_tile.path / 'test_file.jpg'
assert True == path_fits.is_file()
assert True == path_jpg.is_file()
data_precomp = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
assert fits_tile.data.shape == (512, 512)
assert jpg_tile.data.shape == (512, 512, 3)
assert list(fits_tile.data[510][:12]) == data_precomp
69 changes: 36 additions & 33 deletions hips/tiles/tile.py
@@ -1,16 +1,21 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from astropy.io.fits.header import Header
from astropy.io import fits
from pathlib import Path
from io import BytesIO
from PIL import Image
import urllib.request
from io import BytesIO

import numpy as np
from PIL import Image
from astropy.io import fits
from astropy.io.fits.header import Header

from .tile_meta import HipsTileMeta
from ..utils.tile import tile_path

__all__ = [
'HipsTile',
]

__doctest_skip__ = ['HipsTile']


class HipsTile:
"""HiPS tile container.
Expand All @@ -19,21 +24,23 @@ class HipsTile:
Parameters
----------
hips_tile_meta : `HipsTileMeta`
Metadata of HiPS tile
format : `str`
Format of HiPS tile
data : `int`
data : `np.ndarray`
Data containing HiPS tile
header : `format`
header : `astropy.io.fits.Header`
Header of HiPS tile
Examples
--------
::
>>> import urllib.request
>>> from hips.tiles import HipsTile
>>> from hips.tiles import HipsTileMeta
>>> from astropy.tests.helper import remote_data
>>> text = urllib.request.urlopen('https://raw.githubusercontent.com/hipspy/hips/master/hips/tiles/tests/data/properties.txt').read() # doctest: +REMOTE_DATA
>>> hips_tile = HipsTile.read('fits', 'Npix30889.fits')
>>> hips_tile_metadata = HipsTileMeta(order=6, ipix=30889, format='fits')
>>> hips_tile = HipsTile.read(hips_tile_metadata, 'Npix30889.fits')
>>> hips_tile.data
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
Expand All @@ -44,57 +51,53 @@ class HipsTile:
[0, 0, 0, ..., 1, 0, 1]], dtype=int16)
"""

def __init__(self, format: str, data: np.ndarray=None, header: Header=None) -> None:
def __init__(self, hips_tile_meta: HipsTileMeta, data: np.ndarray = None, header: Header = None) -> None:
self.hips_tile_meta = hips_tile_meta
self.format = format
self.data = data
self.header = header

@property
def path(self) -> Path:
"""Default path for tile storage (`Path`)."""
return Path('hips', 'tiles', 'tests', 'data')

@classmethod
def fetch(cls, format: str, url: str) -> HipsTile:
def fetch(cls, hips_tile_meta: HipsTileMeta, url: str) -> 'HipsTile':
"""Fetch HiPS tile and load into memory (`HipsTile`).
Parameters
----------
format : `str`
Format of HiPS tile
hips_tile_meta : `HipsTileMeta`
Metadata of HiPS tile
url : `str`
URL containing HiPS tile
"""
raw_image = BytesIO(urllib.request.urlopen(url).read())
if format == 'fits':
if hips_tile_meta.format == 'fits':
hdulist = fits.open(raw_image)
data = np.array(hdulist[0].data)
header = hdulist[0].header
return cls(format, data, header)
return cls(hips_tile_meta, data, header)
else:
data = np.array(Image.open(raw_image))
return cls(format, data)
return cls(hips_tile_meta, data)

@classmethod
def read(cls, format: str, filename: str) -> 'HipsTile':
def read(cls, hips_tile_meta: HipsTileMeta, filename: str) -> 'HipsTile':
"""Read HiPS tile data from a directory and load into memory (`HipsTile`).
Parameters
----------
format : `str`
Format of HiPS tile
hips_tile_meta : `HipsTileMeta`
Metadata of HiPS tile
filename : `str`
File name of HiPS tile
"""
path = Path('hips', 'tiles', 'tests', 'data') / filename
if format == 'fits':
path = tile_path().absolute() / filename
if hips_tile_meta.format == 'fits':
hdulist = fits.open(path)
data = np.array(hdulist[0].data)
header = hdulist[0].header
return cls(format, data, header)
return cls(hips_tile_meta, data, header)
else:
data = np.array(Image.open(str(path)))
return cls(format, data)
return cls(hips_tile_meta, data)

def write(self, filename: str) -> None:
"""Write HiPS tile by a given filename (`None`).
Expand All @@ -104,11 +107,11 @@ def write(self, filename: str) -> None:
filename : `str`
Name of the file
"""
path = self.path / filename
if self.format == 'fits':
path = tile_path().absolute() / filename
if self.hips_tile_meta.format == 'fits':
hdu = fits.PrimaryHDU(self.data, header=self.header)
hdulist = fits.HDUList([hdu])
hdulist.writeto(path)
hdulist.writeto(str(path))
hdulist.close()
else:
Image.fromarray(self.data).save(str(path))
29 changes: 29 additions & 0 deletions hips/tiles/tile_meta.py
@@ -0,0 +1,29 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

__all__ = [
'HipsTileMeta',
]


class HipsTileMeta:
"""HiPS tile metadata container.
This class stores HiPS tile meta attributes.
Parameters
----------
order : `int`
Order of HiPS tile
ipix : `int`
Pixel number of HiPS tile
format : `str`
Format of the HiPS tile
tile_width : `int`
HiPS tile width
"""

def __init__(self, order: int, ipix: int, format: str, tile_width: int = 512) -> None:
self.order = format
self.ipix = ipix
self.format = format
self.tile_width = tile_width
18 changes: 18 additions & 0 deletions hips/utils/tile.py
@@ -0,0 +1,18 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""HEALpy wrapper functions.
This module contains wrapper functions around HEALPix utilizing
the healpy library.
"""

from pathlib import Path

__all__ = [
'tile_path',
]


def tile_path() -> Path:
"""Return default path of HiPS tile storage."""
path = Path('hips', 'tiles', 'tests', 'data')
return path

0 comments on commit 616340b

Please sign in to comment.