Skip to content

Commit

Permalink
Minimize HipsTile classs functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
adl1995 committed Jun 22, 2017
1 parent 12f0b4f commit 6250e9e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 111 deletions.
87 changes: 30 additions & 57 deletions hips/tiles/tests/test_tile.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,34 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from astropy.utils.data import get_pkg_data_filename
from astropy.tests.helper import remote_data
from ..description import HipsDescription
from ..tile import HipsTile
from pathlib import Path
import numpy as np

class TestHipsTile:
@classmethod
def setup_class(cls):
filename = get_pkg_data_filename('data/properties.txt')
hips_description = HipsDescription.read(filename)

cls.fits_tile = HipsTile(hips_description=hips_description, order=6,
ipix=30889, format='fits', tile_width=512)
cls.jpg_tile = HipsTile(hips_description=hips_description, order=6,
ipix=30889, format='jpg', tile_width=512)
cls.fits_tile.base_url = 'http://alasky.unistra.fr/2MASS/H'
cls.jpg_tile.base_url = 'http://alasky.unistra.fr/2MASS/H'

def test_base_url(self):
assert self.fits_tile.base_url == 'http://alasky.unistra.fr/2MASS/H'
assert self.jpg_tile.base_url == 'http://alasky.unistra.fr/2MASS/H'

def test_tile_url(self):
assert self.fits_tile.tile_url == 'http://alasky.unistra.fr/2MASS/H/Norder6/Dir30000/Npix30889.fits'
assert self.jpg_tile.tile_url == 'http://alasky.unistra.fr/2MASS/H/Norder6/Dir30000/Npix30889.jpg'

@remote_data
def test_fetch(self):
self.fits_tile.fetch()
self.jpg_tile.fetch()

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 self.fits_tile.data.shape == shape_fits_precomp
assert self.jpg_tile.data.shape == shape_jpg_precomp
assert list(self.fits_tile.data[510][:12]) == data_precomp

def test_read(self):
self.fits_tile.read()
self.jpg_tile.read()

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 (self.fits_tile.data.shape) == shape_fits_precomp
assert (self.jpg_tile.data.shape) == shape_jpg_precomp
assert list(self.fits_tile.data[510][:12]) == data_precomp

def test_write(self):
filename = 'test_file'
self.fits_tile.write(filename)
self.jpg_tile.write(filename)

path_fits = self.fits_tile.path.joinpath(''.join([filename, '.', self.fits_tile.format]))
path_jpg = self.jpg_tile.path.joinpath(''.join([filename, '.', self.jpg_tile.format]))
assert True == path_fits.is_file()
assert True == path_jpg.is_file()
@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()
100 changes: 46 additions & 54 deletions hips/tiles/tile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from .description import HipsDescription
from astropy.io.fits.header import Header
from astropy.io import fits
from pathlib import Path
from io import BytesIO
Expand All @@ -15,39 +15,25 @@
class HipsTile:
"""HiPS tile container.
This class provides methods for fetching, reading,
and writing a HiPS tile. It also contains a few
getters and setters around frequently used
HiPS tile attributes.
This class provides methods for fetching, reading, and writing a HiPS tile.
Parameters
----------
hips_description : `HipsDescription`
Class HipsDescription contains HiPS properties
order : `int`
Order of the HiPS tile
ipix : `int`
HEALPix pixel number
format : `format`
format : `str`
Format of HiPS tile
data : `numpy.ndarray`
Pixel values of HiPS tile
tile_width : `int`
Width of HiPS tile
base_url : `str`
Base URL of HiPS tile
data : `int`
Data containing HiPS tile
header : `format`
Header of HiPS tile
Examples
--------
::
>>> import urllib.request
>>> from hips.tiles import HipsDescription
>>> 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_description = HipsDescription.parse(str(text))
>>> hips_tile = HipsTile(hips_description=hips_description, order=6, ipix=30889, format='fits', tile_width=512)
>>> hips_tile.read()
>>> hips_tile = HipsTile.read('fits', 'Npix30889.fits')
>>> hips_tile.data
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
Expand All @@ -58,51 +44,57 @@ class HipsTile:
[0, 0, 0, ..., 1, 0, 1]], dtype=int16)
"""

def __init__(self, hips_description: HipsDescription, order: int, ipix: int, format: str,
data: np.ndarray=None, tile_width: int=512, base_url: str=None) -> None:
self.hips_description = hips_description
self.order = order
self.ipix = ipix
self.tile_width = tile_width
def __init__(self, format: str, data: np.ndarray=None, header: Header=None) -> None:
self.format = format
self.data = data
self.base_url = base_url

@staticmethod
def _directory(ipix: int) -> int:
"""Directory of the HiPS tile (`int`)."""
return np.around(ipix, decimals=-(len(str(ipix)) - 1))
self.header = header

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

@property
def tile_url(self) -> str:
"""HiPS tile url (`str`)."""
return ''.join([self.base_url, '/Norder', str(self.order), '/Dir',
str(self._directory(self.ipix)), '/Npix', str(self.ipix), '.', self.format])
@classmethod
def fetch(cls, format: str, url: str) -> HipsTile:
"""Fetch HiPS tile and load into memory (`HipsTile`).
def fetch(self) -> None:
"""Fetch HiPS tile and load into memory (`None`)."""
raw_image = BytesIO(urllib.request.urlopen(self.tile_url).read())
if self.format == 'fits':
Parameters
----------
format : `str`
Format of HiPS tile
url : `str`
URL containing HiPS tile
"""
raw_image = BytesIO(urllib.request.urlopen(url).read())
if format == 'fits':
hdulist = fits.open(raw_image)
self.data = np.array(hdulist[0].data)
self.header = hdulist[0].header
data = np.array(hdulist[0].data)
header = hdulist[0].header
return cls(format, data, header)
else:
self.data = np.array(Image.open(raw_image))
data = np.array(Image.open(raw_image))
return cls(format, data)

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

def write(self, filename: str) -> None:
"""Write HiPS tile by a given filename (`None`).
Expand All @@ -112,7 +104,7 @@ def write(self, filename: str) -> None:
filename : `str`
Name of the file
"""
path = self.path / (''.join([filename, '.', self.format]))
path = self.path / filename
if self.format == 'fits':
hdu = fits.PrimaryHDU(self.data, header=self.header)
hdulist = fits.HDUList([hdu])
Expand Down

0 comments on commit 6250e9e

Please sign in to comment.