-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Plans for vectorisation / multi-region objects #497
Comments
I think this is a great idea, as this is quite often my main use case for regions (i.e., making many and combining them). @larrybradley have you given this any further consideration? |
Thanks @keflavich! Just to show my initial motivation for this, here's a dummy example of checking if some set coordinates lie within a set of regions. With my implementation above, going vectorised gives ~10x speedup: import numpy as np
from astropy.io import fits
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.wcs import WCS
from regions import RectangleSkyRegions, RectangleSkyRegion
# Define a few rectangular regions and some coordinates
centers=SkyCoord([1, 2, 3], [1, 2, 3], unit="deg")
widths=np.array([1, 2, 3]) * u.deg
heights=np.array([1, 2, 3]) * u.deg
angles=np.array([1, 2, 3]) * u.deg
coords = SkyCoord([1, 2, 4], [1, 2, 4], unit="deg")
# Create a dummy header with a WCS
header = fits.Header(
{
"NAXIS": 2,
"NAXIS1": 10,
"NAXIS2": 10,
"CTYPE1": "RA---TAN",
"CRVAL1": 0,
"CRPIX1": 5,
"CDELT1": -0.1,
"CUNIT1": "deg",
"CTYPE2": "DEC--TAN",
"CRVAL2": 0,
"CRPIX2": 5,
"CDELT2": 0.1,
"CUNIT2": "deg",
}
)
wcs = WCS(header) Benchmarking with loops (currently required): %%timeit
# Current
rectangles = [
RectangleSkyRegion(
center=center, width=width, height=height, angle=angle
) for center, width, height, angle in zip(centers, widths, heights, angles)
]
rec_pix = [r.to_pixel(wcs) for r in rectangles]
coord_check = [r.contains(c, wcs) for r, c in zip(rectangles, coords)]
And with vectors: %%timeit
# Vectorised
rectangles = RectangleSkyRegions(
centers=centers, widths=widths, heights=heights, angles=angles
)
rec_pix = rectangles.to_pixel(wcs)
coord_check = rectangles.contains(coords)
|
Hi all!
Thanks for the work on this neat module! I've run into a couple of situations where I want to do operations with many regions e.g. if
SkyCoord
falls inside a 'list' of regions. 'Air quotes' on the 'list' here, as it looks like there aren't vectorised versions of regions (yet). So, I was wondering, what are the plans were for supporting vectors of regions?I've got a little sketch of what support for vectorised operations could look like, and I'd be interested to get the maintainers thoughts. To my mind, there are two ways of going about to a) reworking the current regions objects to support vectorisation (making the current scalar form just the 0 length case), or b) producing vectorised versions of the objects with the appropriate functionality. In either case a lot of the core functionality is already in place from
np.ndarray
s,u.Quantity
s,SkyCoords
etc. Cases like plotting will need some careful consideration - only briefly thinking about it I think loops would be needed if the vectorised case was supported.I've got a quick sketch for option b) which I've been playing with in my fork. Here's an example of one possible class in
regions/shapes/rectangles.py
:and the extras to
regions/core/attributes.py
:Let me know what you think of this, and I'm happy to start a PR if this wold be useful :)
The text was updated successfully, but these errors were encountered: