-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from astronomy-commons/sean/moc-alignment
Add MOC filter and alignment methods
- Loading branch information
Showing
8 changed files
with
473 additions
and
18 deletions.
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,68 @@ | ||
import numba | ||
import numpy as np | ||
from mocpy import MOC | ||
from numba import njit | ||
|
||
from hipscat.pixel_tree.pixel_tree import PixelTree | ||
|
||
|
||
def filter_by_moc( | ||
tree: PixelTree, | ||
moc: MOC, | ||
) -> PixelTree: | ||
"""Filters a pixel tree to only include the pixels that overlap with the pixels in the moc | ||
Args: | ||
tree (PixelTree): The tree to perform the filtering on | ||
moc (mocpy.MOC): The moc to use to filter | ||
Returns: | ||
A new PixelTree object with only the pixels from the input tree that overlap with the moc. | ||
""" | ||
moc_ranges = moc.to_depth29_ranges | ||
# Convert tree intervals to order 29 to match moc intervals | ||
tree_29_ranges = tree.tree << (2 * (29 - tree.tree_order)) | ||
tree_mask = perform_filter_by_moc(tree_29_ranges, moc_ranges) | ||
return PixelTree(tree.tree[tree_mask], tree.tree_order) | ||
|
||
|
||
@njit( | ||
numba.bool_[::1]( | ||
numba.int64[:, :], | ||
numba.uint64[:, :], | ||
) | ||
) | ||
def perform_filter_by_moc( | ||
tree: np.ndarray, | ||
moc: np.ndarray, | ||
) -> np.ndarray: # pragma: no cover | ||
"""Performs filtering with lists of pixel intervals | ||
Input interval lists must be at the same order. | ||
Args: | ||
tree (np.ndarray): Array of pixel intervals to be filtered | ||
moc (np.ndarray): Array of pixel intervals to be used to filter | ||
Returns: | ||
A boolean array of dimension tree.shape[0] which masks which pixels in tree overlap with the pixels in | ||
moc | ||
""" | ||
output = np.full(tree.shape[0], fill_value=False, dtype=np.bool_) | ||
tree_index = 0 | ||
moc_index = 0 | ||
while tree_index < len(tree) and moc_index < len(moc): | ||
tree_pix = tree[tree_index] | ||
moc_pix = moc[moc_index] | ||
if tree_pix[0] >= moc_pix[1]: | ||
# Don't overlap, tree pixel ahead so move onto next MOC pixel | ||
moc_index += 1 | ||
continue | ||
if moc_pix[0] >= tree_pix[1]: | ||
# Don't overlap, MOC pixel ahead so move onto next tree pixel | ||
tree_index += 1 | ||
continue | ||
# Pixels overlap, so include current tree pixel and check next tree pixel | ||
output[tree_index] = True | ||
tree_index += 1 | ||
return output |
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
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,59 @@ | ||
import numpy as np | ||
from mocpy import MOC | ||
|
||
from hipscat.pixel_math import HealpixPixel | ||
from hipscat.pixel_tree.moc_filter import filter_by_moc | ||
|
||
|
||
def test_moc_filter(pixel_tree_2): | ||
orders = np.array([1, 1, 2]) | ||
pixels = np.array([45, 46, 128]) | ||
moc = MOC.from_healpix_cells(pixels, orders, 2) | ||
filtered_tree = filter_by_moc(pixel_tree_2, moc) | ||
assert filtered_tree.get_healpix_pixels() == [ | ||
HealpixPixel(2, 128), | ||
HealpixPixel(1, 45), | ||
HealpixPixel(1, 46), | ||
] | ||
|
||
|
||
def test_moc_filter_lower_order(pixel_tree_2): | ||
orders = np.array([0, 1]) | ||
pixels = np.array([11, 32]) | ||
moc = MOC.from_healpix_cells(pixels, orders, 1) | ||
filtered_tree = filter_by_moc(pixel_tree_2, moc) | ||
assert filtered_tree.get_healpix_pixels() == [ | ||
HealpixPixel(2, 128), | ||
HealpixPixel(2, 130), | ||
HealpixPixel(2, 131), | ||
HealpixPixel(1, 44), | ||
HealpixPixel(1, 45), | ||
HealpixPixel(1, 46), | ||
] | ||
|
||
|
||
def test_moc_filter_higher_order(pixel_tree_2): | ||
orders = np.array([1, 3]) | ||
pixels = np.array([40, 520]) | ||
moc = MOC.from_healpix_cells(pixels, orders, 3) | ||
filtered_tree = filter_by_moc(pixel_tree_2, moc) | ||
assert filtered_tree.get_healpix_pixels() == [ | ||
HealpixPixel(2, 130), | ||
HealpixPixel(0, 10), | ||
] | ||
|
||
|
||
def test_moc_filter_empty_moc(pixel_tree_2): | ||
orders = np.array([]) | ||
pixels = np.array([]) | ||
moc = MOC.from_healpix_cells(pixels, orders, 0) | ||
filtered_tree = filter_by_moc(pixel_tree_2, moc) | ||
assert filtered_tree.get_healpix_pixels() == [] | ||
|
||
|
||
def test_moc_filter_empty_result(pixel_tree_2): | ||
orders = np.array([0]) | ||
pixels = np.array([1]) | ||
moc = MOC.from_healpix_cells(pixels, orders, 0) | ||
filtered_tree = filter_by_moc(pixel_tree_2, moc) | ||
assert filtered_tree.get_healpix_pixels() == [] |
Oops, something went wrong.