Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 87 additions & 8 deletions healsparse/healSparseMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,68 @@ def __ipow__(self, other):

return self._apply_operation(other, np.power, in_place=True)

def __lt__(self, other):
"""
Apply less than operator to a map with a constant

Returns boolean map
"""

return self._apply_operation(other, np.less, sentinel=False).as_bit_packed_map()

def __le__(self, other):
"""
Apply less than or equal to operator to a map with a constant

Returns boolean map
"""

return self._apply_operation(other, np.less_equal, sentinel=False).as_bit_packed_map()

def __gt__(self, other):
"""
Apply greater than operator to a map with a constant

Returns boolean map
"""

return self._apply_operation(other, np.greater, sentinel=False).as_bit_packed_map()

def __ge__(self, other):
"""
Apply greater than or equal to operator to a map with a constant

Returns boolean map
"""

return self._apply_operation(other, np.greater_equal, sentinel=False).as_bit_packed_map()

def __eq__(self, other):
"""
Apply equal to operator to a map with a constant

Returns boolean map
"""
if isinstance(other, HealSparseMap):
# fall back to standard __eq__ if other is a map
return NotImplemented

return self._apply_operation(other, np.equal, sentinel=False).as_bit_packed_map()

def __ne__(self, other):
"""
Apply not equal to operator to a map with a constant

Returns boolean map
"""
if isinstance(other, HealSparseMap):
# fall back to standard __eq__ if other is a map
return NotImplemented

return self._apply_operation(other, np.not_equal, sentinel=False).as_bit_packed_map()

__hash__ = object.__hash__

def __and__(self, other):
"""
Perform a bitwise and with a constant.
Expand Down Expand Up @@ -2358,7 +2420,7 @@ def __invert__(self):
sentinel=self._sentinel,
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this does expand the full sparse map, but I think the apply_operator function already did this with self._sparse_map.copy()

def _apply_operation(self, other, func, int_only=False, in_place=False):
def _apply_operation(self, other, func, int_only=False, in_place=False, sentinel=None):
"""
Apply a generic arithmetic function.

Expand All @@ -2374,6 +2436,9 @@ def _apply_operation(self, other, func, int_only=False, in_place=False):
Only accept integer types.
in_place : `bool`, optional
Perform operation in-place.
sentinel : None, `int` or `float`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be int or float or bool?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

sentinel value for the output map, if None will use the same value as self
must be None if in_place is True

Returns
-------
Expand Down Expand Up @@ -2436,6 +2501,7 @@ def _apply_operation(self, other, func, int_only=False, in_place=False):
valid_sparse_pixels = (self._sparse_map != self._sentinel)

if in_place:
assert sentinel is None
if self._is_wide_mask:
for i in range(self._wide_mask_width):
col = self._sparse_map[:, i]
Expand All @@ -2444,15 +2510,28 @@ def _apply_operation(self, other, func, int_only=False, in_place=False):
func(self._sparse_map, other, out=self._sparse_map, where=valid_sparse_pixels)
return self
else:
combinedSparseMap = self._sparse_map.copy()
if self._is_wide_mask:
for i in range(self._wide_mask_width):
col = combinedSparseMap[:, i]
func(col, other_value[i], out=col, where=valid_sparse_pixels)
output_sentinel = self._sentinel if sentinel is None else sentinel
if sentinel is None:
# Compute func in-place
combinedSparseMap = self._sparse_map.copy()
if self._is_wide_mask:
for i in range(self._wide_mask_width):
col = combinedSparseMap[:, i]
func(col, other_value[i], out=col, where=valid_sparse_pixels)
else:
func(combinedSparseMap, other, out=combinedSparseMap, where=valid_sparse_pixels)
else:
func(combinedSparseMap, other, out=combinedSparseMap, where=valid_sparse_pixels)
# Make empty combinedSparseMap with sentinel dtype and fill
combinedSparseMap = np.full_like(self._sparse_map, fill_value=sentinel, dtype=type(sentinel))
if self._is_wide_mask:
for i in range(self._wide_mask_width):
in_col = self._sparse_map[:, i]
out_col = combinedSparseMap[:, i]
func(in_col, other_value[i], out=out_col, where=valid_sparse_pixels)
else:
func(self._sparse_map, other, out=combinedSparseMap, where=valid_sparse_pixels)
return HealSparseMap(cov_map=self._cov_map, sparse_map=combinedSparseMap,
nside_sparse=self._nside_sparse, sentinel=self._sentinel)
nside_sparse=self._nside_sparse, sentinel=output_sentinel)

def _apply_boolean_map_operation(self, other, name, in_place=False):
"""Apply an operation to a boolean mask map.
Expand Down
Loading