Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RGB tiles #63

Merged
merged 1 commit into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions hips/draw/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
'compute_matching_hips_order',
]


# TODO: Fix type annotation issue
def draw_sky_image(geometry: WCSGeometry, tiles: Generator[HipsTile, Any, Any], hips_survey: HipsSurveyProperties) -> np.ndarray:
def draw_sky_image(geometry: WCSGeometry, tiles: Generator[HipsTile, Any, Any],
hips_survey: HipsSurveyProperties, tile_format: str) -> np.ndarray:
"""Draw sky image using the simple and quick method.

Parameters
Expand All @@ -30,13 +32,21 @@ def draw_sky_image(geometry: WCSGeometry, tiles: Generator[HipsTile, Any, Any],
A list of HipsTile
hips_survey : `~hips.HipsSurveyProperties`
HiPS survey properties
tile_format : `str`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdeil I don't think I can use that. The properties file could contain multiple formats such as 'jpg fits'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, in that case: OK to add a separate parameter, at least for now.

Format of HiPS tile

Returns
-------
np.ndarray
Returns a numpy array containing all HiPS tiles projected onto it
"""
image = np.zeros(geometry.shape)
if tile_format == 'jpg':
shape = (geometry.shape.width, geometry.shape.height, 3)
elif tile_format == 'png':
shape = (geometry.shape.width, geometry.shape.height, 4)
else:
shape = (geometry.shape.width, geometry.shape.height)
image = np.zeros(shape)
for tile in tiles:
painter = SimpleTilePainter(geometry, hips_survey, tile)
image += painter.warp_image()
Expand All @@ -45,7 +55,6 @@ def draw_sky_image(geometry: WCSGeometry, tiles: Generator[HipsTile, Any, Any],

class SimpleTilePainter:
"""Paint a single tile using a simple projective transformation method.

The algorithm implemented is described here: :ref:`drawing_algo`.

Parameters
Expand Down Expand Up @@ -73,6 +82,7 @@ def dst(self) -> np.ndarray:
[0, width - 1],
[0, 0]],
)

@property
def projection(self) -> ProjectiveTransform:
"""Estimate projective transformation on a HiPS tile"""
Expand All @@ -93,7 +103,8 @@ def warp_image(self) -> np.ndarray:
)


def fetch_tiles(healpix_pixel_indices: np.ndarray, order: int, hips_survey: HipsSurveyProperties) -> 'HipsTile':
def fetch_tiles(healpix_pixel_indices: np.ndarray, order: int,
hips_survey: HipsSurveyProperties, tile_format: str) -> 'HipsTile':
"""Fetch HiPS tiles from a remote URL.

Parameters
Expand All @@ -104,18 +115,20 @@ def fetch_tiles(healpix_pixel_indices: np.ndarray, order: int, hips_survey: Hips
Order of the HEALPix map
hips_survey : HipsSurveyProperties
An object of HipsSurveyProperties
tile_format : `str`
Format of HiPS tile

Returns
-------
'HipsTile'
Returns an object of HipsTile
Returns an object of HipsTile
"""
for healpix_pixel_index in healpix_pixel_indices:
tile_meta = HipsTileMeta(
order=order,
ipix=healpix_pixel_index,
frame=hips_survey.astropy_frame,
file_format='fits',
file_format=tile_format,
)
tile = HipsTile.fetch(tile_meta, hips_survey.tile_access_url(order=order, ipix=healpix_pixel_index) + tile_meta.filename)
yield tile
Expand Down Expand Up @@ -174,9 +187,8 @@ def _get_hips_order_for_resolution(tile_width, resolution):
return candidate_tile_order


def make_sky_image(geometry: WCSGeometry, hips_survey: HipsSurveyProperties) -> np.ndarray:
def make_sky_image(geometry: WCSGeometry, hips_survey: HipsSurveyProperties, tile_format: str) -> np.ndarray:
"""Make sky image: fetch tiles and draw.

The example for this can be found on the :ref:`gs` page.

Parameters
Expand All @@ -185,6 +197,8 @@ def make_sky_image(geometry: WCSGeometry, hips_survey: HipsSurveyProperties) ->
Geometry of the output image
hips_survey : `~hips.HipsSurveyProperties`
HiPS survey properties
tile_format : `str`
Format of HiPS tile

Returns
-------
Expand All @@ -198,8 +212,8 @@ def make_sky_image(geometry: WCSGeometry, hips_survey: HipsSurveyProperties) ->
healpix_frame=hips_survey.astropy_frame,
)
# TODO: this isn't a good API. Will become better when we have a cache.
tiles = fetch_tiles(healpix_pixel_indices, order, hips_survey)
tiles = fetch_tiles(healpix_pixel_indices, order, hips_survey, tile_format)

image_data = draw_sky_image(geometry, tiles, hips_survey)
image_data = draw_sky_image(geometry, tiles, hips_survey, tile_format)

return image_data
51 changes: 30 additions & 21 deletions hips/draw/tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,53 @@
import healpy as hp
from astropy.coordinates import SkyCoord
from numpy.testing import assert_allclose
from astropy.utils.data import get_pkg_data_filename
from astropy.tests.helper import remote_data
from ..simple import make_sky_image, draw_sky_image, compute_matching_hips_order, _get_hips_order_for_resolution
from ...tiles import HipsSurveyProperties, HipsTileMeta, HipsTile
from ...utils import WCSGeometry
from ...utils.testing import get_hips_extra_file, make_test_wcs_geometry, requires_hips_extra

def get_test_tiles():
filename = get_pkg_data_filename('../../tiles/tests/data/properties.txt')

def get_test_tiles(file_format, survey, order, ipix_list):
filename = get_hips_extra_file('datasets/samples/' + survey + '/properties')
hips_survey = HipsSurveyProperties.read(filename)

tile1 = HipsTile.read(
meta=HipsTileMeta(order=3, ipix=450, file_format='fits', frame=hips_survey.astropy_frame),
full_path=get_hips_extra_file('datasets/samples/DSS2Red/Norder3/Dir0/Npix450.fits'),
)
tiles = []
for ipix in ipix_list:
tiles.append(HipsTile.read(
meta=HipsTileMeta(order=order, ipix=ipix, file_format=file_format, frame=hips_survey.astropy_frame),
full_path=get_hips_extra_file('datasets/samples/' + survey + hips_survey.tile_path(order=order, ipix=ipix, tile_format=file_format)),
))

tile2 = HipsTile.read(
meta=HipsTileMeta(order=3, ipix=451, file_format='fits', frame=hips_survey.astropy_frame),
full_path=get_hips_extra_file('datasets/samples/DSS2Red/Norder3/Dir0/Npix451.fits'),
)
return tiles

return [tile1, tile2]

draw_sky_image_pars = [
dict(file_format='fits', shape=(2000, 1000), survey='DSS2Red', data_1=2866.0101409848185,
data_2=2563.6916727348043, data_sum=2992643842.729775, order=3, ipix_list=[450, 451]),
dict(file_format='jpg', shape=(2000, 1000, 3), survey='DSS2Red', data_1=[13.040878, 13.040878, 13.040878],
data_2=[17.235874, 17.235874, 17.235874], data_sum=155233709.20236143, order=3, ipix_list=[450, 451]),
dict(file_format='png', shape=(2000, 1000, 4), survey='AKARI-FIS', data_1=[254., 254., 254., 255.],
data_2=[254., 254., 254., 255.], data_sum=586208559.2450126, order=3, ipix_list=[450, 451])
]


@remote_data
@requires_hips_extra()
def test_draw_sky_image():
@pytest.mark.parametrize('pars', draw_sky_image_pars)
def test_draw_sky_image(pars):
geometry = make_test_wcs_geometry(case=2)
tiles = get_test_tiles()
url = 'https://raw.githubusercontent.com/hipspy/hips-extra/master/datasets/samples/DSS2Red/properties'
tiles = get_test_tiles(file_format=pars['file_format'], survey=pars['survey'], order=pars['order'], ipix_list=pars['ipix_list'])
url = 'https://raw.githubusercontent.com/hipspy/hips-extra/master/datasets/samples/' + pars['survey'] + '/properties'
hips_survey = HipsSurveyProperties.fetch(url)

data = draw_sky_image(geometry, tiles, hips_survey)
data = draw_sky_image(geometry=geometry, tiles=tiles, hips_survey=hips_survey, tile_format=pars['file_format'])

assert data.shape == geometry.shape
assert data.shape == pars['shape']
assert data.dtype == np.float64
assert_allclose(np.sum(data), 2992643842.729775)
assert_allclose(data[400, 500], 2866.0101409848185)
assert_allclose(data[400, 501], 2563.6916727348043)
assert_allclose(np.sum(data), pars['data_sum'])
assert_allclose(data[400, 500], pars['data_1'])
assert_allclose(data[400, 501], pars['data_2'])


@remote_data
Expand All @@ -51,9 +59,10 @@ def test_make_sky_image():
url = 'http://alasky.unistra.fr/DSS/DSS2Merged/properties'
hips_survey = HipsSurveyProperties.fetch(url)
geometry = make_test_wcs_geometry(case=2)
data = make_sky_image(geometry, hips_survey)
data = make_sky_image(geometry=geometry, hips_survey=hips_survey, tile_format='fits')
assert data.shape == geometry.shape
assert data.dtype == np.float64
assert_allclose(np.sum(data), 7615817463.1612253)
assert_allclose(data[200, 994], 2213.30874796)
assert_allclose(data[200, 995], 2296.93885940)

Expand Down
14 changes: 14 additions & 0 deletions hips/tiles/surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ def tile_access_url(self, order: int, ipix: int) -> str:
"""
return self.base_url + '/Norder' + str(order) + '/Dir' + str(self.directory(ipix)) + '/'

def tile_path(self, order: int, ipix: int, tile_format: str) -> str:
"""Tile access URL

Parameters
----------
order : int
HiPS order
ipix : int
Index of the HiPS tile
tile_format : str
HiPS tile URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter description isn't correct for tile_format.

Also: maybe add an Examples section with one example?

"""
return '/Norder' + str(order) + '/Dir' + str(self.directory(ipix)) + '/Npix' + str(ipix) + '.' + tile_format

@property
def hips_service_url(self) -> str:
"""HiPS service base URL (`str`)."""
Expand Down