-
Notifications
You must be signed in to change notification settings - Fork 0
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
Support HEALpix-indexed AGASC HDF5 files and more #155
Merged
Changes from 13 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
a518a2b
Initial commit to support HEALpix-indexed AGASC HDF5 files
taldcroft 799e19f
Fix typo
taldcroft f9e2d23
Minimal modernization of create_mini_agasc_h5.py
taldcroft 0be1563
Refactor and modernize
taldcroft e9fef74
Apply black formatting
taldcroft f64aebd
Apply black to create_near_neighbor_ids
taldcroft 45b1533
Improvements to miniagasc creation helper scripts
taldcroft cbccf43
Support for caching and columns in get_agasc_cone
taldcroft 0805567
Working get_agasc_cone for healpix
taldcroft c90685b
Remove columns as an option since slows performance
taldcroft cad21fe
Fix a couple of issues in create_near_neighbor_ids
taldcroft 07b069b
Change epoch for computing near-neighbor to 2024.0
taldcroft fe9b554
Some fixes / improvements in file creation scripts
taldcroft 065ed43
Implement new convention for agasc_file kwarg
taldcroft 601fafa
Doc fixes
taldcroft 3d11988
Fix tests
taldcroft 9744866
Rework the file selection code
taldcroft 238454c
Update filename resolution for latest spec
taldcroft df155b2
Update conf.py for numpydoc and autodoc typehints
taldcroft 145567c
Flake8
taldcroft 6176521
Add test of get_agasc_filename and fix bug in that function
taldcroft be7145e
Require either .h5 or * at end of `agasc_file` + other fixes
taldcroft 350d91f
Rename and overhaul create_mini_agasc_h5
taldcroft a5d38c5
Add option to allow RC files for testing
taldcroft 8ea5a53
Add version kwarg and fix tests
taldcroft 3bfa5a2
Test get_agasc_cone, get_star, get_stars for HEALpix
taldcroft e314a63
Flake8
taldcroft 743285d
Add new dev script for profiling
taldcroft ca4613b
Reduce memory from supplement
taldcroft 775e156
Add dev scripts for memory profiling
taldcroft 4a5f473
Fix name
taldcroft 0194306
Add documentation on HEALpix ordering
taldcroft 476e183
Add agasc_file to stars meta
taldcroft c41fe20
Add dev script to profile caching performance
taldcroft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
""" | ||
Provide functions for working with HEALPix-indexed AGASC HDF5 files. | ||
|
||
Functions | ||
--------- | ||
is_healpix(agasc_file) | ||
Return True if `agasc_file` is a HEALPix file (otherwise dec-sorted). | ||
get_stars_from_healpix_h5(ra, dec, radius, agasc_file) | ||
Return a table of stars within a given radius around a given sky position. | ||
""" | ||
|
||
import functools | ||
from pathlib import Path | ||
|
||
import astropy.units as u | ||
import astropy_healpix as hpx | ||
import numpy as np | ||
import tables | ||
from astropy.table import Table | ||
|
||
|
||
__all__ = ["is_healpix", "get_stars_from_healpix_h5"] | ||
|
||
|
||
@functools.lru_cache() | ||
def is_healpix(agasc_file): | ||
"""Return True if ``agasc_file`` is a healpix file (otherwise dec-sorted)""" | ||
with tables.open_file(agasc_file, mode="r") as h5: | ||
return "healpix_index" in h5.root | ||
|
||
|
||
@functools.lru_cache(maxsize=12) | ||
def get_healpix(nside): | ||
""" | ||
Returns a HEALPix object with the specified nside and nested order. | ||
|
||
Parameters | ||
----------- | ||
nside : int | ||
The nside parameter for the HEALPix object. | ||
|
||
Returns: | ||
-------- | ||
hpx : HEALPix object | ||
A HEALPix object with the specified nside and order. | ||
""" | ||
return hpx.HEALPix(nside=nside, order="nested") | ||
|
||
|
||
@functools.lru_cache(maxsize=8) | ||
def get_healpix_info(agasc_file: str | Path) -> tuple[dict[int, tuple[int, int]], int]: | ||
""" | ||
Get the healpix index table for an AGASC file. | ||
|
||
The healpix index table is a table with columns ``healpix``, ``idx0`` and ``idx1``. | ||
This corresponds to row ranges in the main ``data`` table in the HDF5 file. | ||
|
||
Parameters | ||
---------- | ||
agasc_file : str or Path | ||
Path to the AGASC HDF5 file. | ||
|
||
Returns | ||
------- | ||
healpix_index : dict | ||
Dictionary of healpix index to row range. | ||
nside : int | ||
HEALPix nside parameter. | ||
""" | ||
with tables.open_file(agasc_file, mode="r") as h5: | ||
tbl = h5.root.healpix_index[:] | ||
nside = h5.root.healpix_index.attrs["nside"] | ||
|
||
out = {row["healpix"]: (row["row0"], row["row1"]) for row in tbl} | ||
|
||
return out, nside | ||
|
||
|
||
def get_stars_from_healpix_h5( | ||
ra: float, | ||
dec: float, | ||
radius: float, | ||
agasc_file: str | Path, | ||
cache: bool = False, | ||
) -> Table: | ||
""" | ||
Returns a table of stars within a given radius around a given sky position (RA, Dec), | ||
using the AGASC data stored in a HDF5 file with a HEALPix index. | ||
|
||
Parameters | ||
---------- | ||
ra : float | ||
Right ascension of the center of the search cone, in degrees. | ||
dec : float | ||
Declination of the center of the search cone, in degrees. | ||
radius : float | ||
Radius of the search cone, in degrees. | ||
agasc_file : str or Path | ||
Path to the HDF5 file containing the AGASC data with a HEALPix index. | ||
cache : bool, optional | ||
Whether to cache the AGASC data in memory. Default is False. | ||
|
||
Returns | ||
------- | ||
stars : astropy.table.Table | ||
Table of stars within the search cone, with columns from the AGASC data table. | ||
""" | ||
from agasc import sphere_dist, read_h5_table | ||
|
||
# Table of healpix, idx0, idx1 where idx is the index into main AGASC data table | ||
healpix_index_map, nside = get_healpix_info(agasc_file) | ||
hp = get_healpix(nside) | ||
|
||
# Get healpix index for ever pixel that intersects the cone. | ||
healpix_indices = hp.cone_search_lonlat( | ||
ra * u.deg, dec * u.deg, radius=radius * u.deg | ||
) | ||
|
||
stars_list = [] | ||
|
||
def make_stars_list(h5_file): | ||
for healpix_index in healpix_indices: | ||
idx0, idx1 = healpix_index_map[healpix_index] | ||
stars = read_h5_table(h5_file, row0=idx0, row1=idx1, cache=cache) | ||
stars_list.append(stars) | ||
|
||
if cache: | ||
make_stars_list(agasc_file) | ||
else: | ||
with tables.open_file(agasc_file) as h5: | ||
make_stars_list(h5) | ||
|
||
stars = Table(np.concatenate(stars_list)) | ||
dists = sphere_dist(ra, dec, stars["RA"], stars["DEC"]) | ||
stars = stars[dists <= radius] | ||
|
||
# Sort in DEC order for back-compatibility with the AGASC ordering before v1.8. | ||
stars.sort("DEC") | ||
|
||
return stars |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, these agasc changes are very well tested and documented, but I'm not sure if there is a cache kwarg test or use case documented. Given the impact that seems fine -- I think the plan is this would only be used by the advanced user to increase speed at the expense of memory. Though I'm not sure about the magnitude of benefit or cost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The performance gains are now documented in the description with a new profiling script in the
dev
directory. One use case is to replaceget_agasc_cone_fast
inkady
(of course that requires thematlab_pm_bug
so who knows). In retrospect it probably wasn't worth the effort but it is done now, let's be positive! 😄