Skip to content

Commit

Permalink
ENH(catalog,io): move slow healpy import into functions (#67)
Browse files Browse the repository at this point in the history
Move `import healpy` statements from the top of modules into functions
where `healpy` functionality is not central.

This speeds up importing these modules massively (several seconds).

Closes: #60
  • Loading branch information
ntessore authored Nov 30, 2023
1 parent 1d05cd3 commit 60d9b01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions heracles/catalog/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import warnings

import healpy as hp
import numpy as np


Expand Down Expand Up @@ -65,8 +64,10 @@ class FootprintFilter:

def __init__(self, footprint, lon, lat):
"""Filter using the given footprint map and position columns."""
from healpy import get_nside

self._footprint = footprint
self._nside = hp.get_nside(footprint)
self._nside = get_nside(footprint)
self._lonlat = (lon, lat)

@property
Expand All @@ -87,7 +88,9 @@ def __repr__(self):
def __call__(self, page):
"""filter catalog page"""

from healpy import ang2pix

lon, lat = self._lonlat
ipix = hp.ang2pix(self._nside, page[lon], page[lat], lonlat=True)
ipix = ang2pix(self._nside, page[lon], page[lat], lonlat=True)
exclude = np.where(self._footprint[ipix] == 0)[0]
page.delete(exclude)
6 changes: 5 additions & 1 deletion heracles/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from weakref import WeakValueDictionary

import fitsio
import healpy as hp
import numpy as np

from .core import TocDict, toc_match
Expand Down Expand Up @@ -76,6 +75,8 @@ def _read_metadata(hdu):
def _write_map(fits, ext, m, *, names=None):
"""write HEALPix map to FITS table"""

import healpy as hp

# prepare column data and names
cols = list(np.atleast_2d(m))
if names is None:
Expand Down Expand Up @@ -199,6 +200,9 @@ def _read_twopoint(fits, ext):

def read_vmap(filename, nside=None, field=0):
"""read visibility map from a HEALPix map file"""

import healpy as hp

vmap = hp.read_map(filename, field=field, dtype=float)

# set unseen pixels to zero
Expand Down

0 comments on commit 60d9b01

Please sign in to comment.