Skip to content

Commit

Permalink
Introduce precise drawing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
adl1995 committed Aug 1, 2017
1 parent 1d2dd43 commit 45bbaf8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
29 changes: 16 additions & 13 deletions hips/draw/paint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
]


# TODO for Adeel: add option "precise".
# Should be default "true" eventually, for now, can use "false" if it's very slow.
class HipsPainter:
"""Paint a sky image from HiPS image tiles.
Expand All @@ -34,6 +32,8 @@ class HipsPainter:
HiPS survey properties
tile_format : {'fits', 'jpg', 'png'}
Format of HiPS tile
precise : bool
Use the precise drawing algorithm
Examples
--------
Expand All @@ -56,10 +56,11 @@ class HipsPainter:
(1000, 2000)
"""

def __init__(self, geometry: WCSGeometry, hips_survey: Union[str, HipsSurveyProperties], tile_format: str) -> None:
def __init__(self, geometry: WCSGeometry, hips_survey: Union[str, HipsSurveyProperties], tile_format: str, precise: bool = False) -> None:
self.geometry = geometry
self.hips_survey = HipsSurveyProperties.make(hips_survey)
self.tile_format = tile_format
self.precise = precise
self._tiles = None
self.float_image = None
self._stats: Dict[str, Any] = {}
Expand Down Expand Up @@ -161,21 +162,23 @@ def run(self) -> np.ndarray:
# Other useful info: number of tiles fetched

def make_tile_list(self):
# Fetch all tiles needed
parent_tiles = self.tiles

# TODO for Adeel: implement distortion correction here.
# Create new list of tiles, where some have been replaced by 4 children.
# Suggestion: for now, just split once, no recursion.
# Leave TODO, to discuss with Thomas next week.
# See also: https://github.com/hipspy/hips/issues/92

# For now, we just create a 1:1 copy
tiles = []
for tile in parent_tiles:
tiles.append(tile)

self.draw_tiles = tiles
if self.precise == True:
tiles = []
for tile in parent_tiles:
corners = tile.meta.skycoord_corners.to_pixel(self.geometry.wcs)
if is_tile_distorted(corners):
tiles.append(tile.children)
else:
tiles.append(tile)
self.draw_tiles = [tile for children in tiles for tile in children]
else:
self.draw_tiles = parent_tiles

def _make_empty_sky_image(self):
shape = compute_image_shape(
Expand Down Expand Up @@ -207,7 +210,7 @@ def plot_mpl_hips_tile_grid(self) -> None:
not something end-users will call or need to know about.
"""
import matplotlib.pyplot as plt
for tile in self.tiles:
for tile in self.draw_tiles:
corners = tile.meta.skycoord_corners.transform_to(self.geometry.celestial_frame)
ax = plt.subplot(projection=self.geometry.wcs)
opts = dict(color='red', lw=1, )
Expand Down
22 changes: 18 additions & 4 deletions hips/draw/tests/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
data_2=2296,
data_sum=8756493140,
dtype='>i2',
repr='HipsDrawResult(width=1000, height=2000, channels=2, dtype=>i2, format=fits)'
repr='HipsDrawResult(width=1000, height=2000, channels=2, dtype=>i2, format=fits)',
precise=False
),
dict(
file_format='jpg',
Expand All @@ -26,7 +27,19 @@
data_2=[137, 116, 114],
data_sum=828908873,
dtype='uint8',
repr='HipsDrawResult(width=1000, height=2000, channels=3, dtype=uint8, format=jpg)'
repr='HipsDrawResult(width=1000, height=2000, channels=3, dtype=uint8, format=jpg)',
precise=False
),
dict(
file_format='jpg',
shape=(1000, 2000, 3),
url='https://raw.githubusercontent.com/hipspy/hips-extra/master/datasets/samples/FermiColor/properties',
data_1=[139, 114, 115],
data_2=[142, 113, 110],
data_sum=825148172,
dtype='uint8',
repr='HipsDrawResult(width=1000, height=2000, channels=3, dtype=uint8, format=jpg)',
precise=True
),
dict(
file_format='png',
Expand All @@ -36,7 +49,8 @@
data_2=[227, 217, 205, 255],
data_sum=1635622838,
dtype='uint8',
repr='HipsDrawResult(width=1000, height=2000, channels=3, dtype=uint8, format=png)'
repr='HipsDrawResult(width=1000, height=2000, channels=3, dtype=uint8, format=png)',
precise=False
),
]

Expand All @@ -47,7 +61,7 @@ def test_make_sky_image(tmpdir, pars):
hips_survey = HipsSurveyProperties.fetch(url=pars['url'])
geometry = make_test_wcs_geometry()

result = make_sky_image(geometry=geometry, hips_survey=hips_survey, tile_format=pars['file_format'])
result = make_sky_image(geometry=geometry, hips_survey=hips_survey, tile_format=pars['file_format'], precise=pars['precise'])

assert result.image.shape == pars['shape']
assert result.image.dtype == pars['dtype']
Expand Down
6 changes: 4 additions & 2 deletions hips/draw/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def make_sky_image(geometry: WCSGeometry, hips_survey: Union[str, 'HipsSurveyProperties'],
tile_format: str) -> 'HipsDrawResult':
tile_format: str, precise: bool = False) -> 'HipsDrawResult':
"""Make sky image: fetch tiles and draw.
The example for this can be found on the :ref:`gs` page.
Expand All @@ -29,13 +29,15 @@ def make_sky_image(geometry: WCSGeometry, hips_survey: Union[str, 'HipsSurveyPro
tile_format : {'fits', 'jpg', 'png'}
Format of HiPS tile to use
(some surveys are available in several formats, so this extra argument is needed)
precise : bool
Use the precise drawing algorithm
Returns
-------
result : `~hips.HipsDrawResult`
Result object
"""
painter = HipsPainter(geometry, hips_survey, tile_format)
painter = HipsPainter(geometry, hips_survey, tile_format, precise)
painter.run()
return HipsDrawResult.from_painter(painter)

Expand Down
10 changes: 5 additions & 5 deletions hips/tiles/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from astropy.coordinates import SkyCoord
from astropy.io import fits
from ..utils import healpix_pixel_corners
from .io import tile_default_url, tile_default_path

__all__ = [
'HipsTileMeta',
Expand Down Expand Up @@ -231,10 +230,10 @@ def children(self) -> List['HipsTile']:
"""Create four children tiles from parent tile."""
w = self.data.shape[0] // 2
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]
self.data[0: w, 0: w],
self.data[w: w * 2, w: w * 2],
self.data[0: w, w: w * 2]
]

tiles = []
Expand All @@ -243,7 +242,8 @@ def children(self) -> List['HipsTile']:
self.meta.order + 1,
self.meta.ipix * 4 + idx,
self.meta.file_format,
self.meta.frame
self.meta.frame,
len(data[0])
)
tile = self.from_numpy(meta, data[idx])
tiles.append(tile)
Expand Down

0 comments on commit 45bbaf8

Please sign in to comment.