-
Notifications
You must be signed in to change notification settings - Fork 8
Inequality operators #176
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
Merged
Merged
Inequality operators #176
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8428d2e
allow a change in sentinel when applying operators
elvinpoole bfc017f
turn off in-place computation of np.ufunc when a new sentinel is prov…
elvinpoole 235ed54
added overloads of > >= < <=
elvinpoole b81140d
added overload of eq and nq, falls back to standard eq if maps are th…
elvinpoole c732e53
formatting
elvinpoole 841b8ab
Merge branch 'LSSTDESC:main' into ineq_operators
elvinpoole 31a5478
removed bit packed output from inequality operators
elvinpoole 4835660
removed hash redefinition
elvinpoole 33b3753
added eq and neq between two hsp maps
elvinpoole 41dc423
fix flake
elvinpoole f6c77cd
added test for comparison operators
elvinpoole 7f28aa2
added documentation on comparison operators
elvinpoole 107f238
fixed flake
elvinpoole c7b6e82
fixed typo in docstring
elvinpoole 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 hidden or 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 |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -2358,7 +2420,7 @@ def __invert__(self): | |
| sentinel=self._sentinel, | ||
| ) | ||
|
|
||
| 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. | ||
|
|
||
|
|
@@ -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` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ------- | ||
|
|
@@ -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] | ||
|
|
@@ -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. | ||
|
|
||
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.
Note: this does expand the full sparse map, but I think the apply_operator function already did this with
self._sparse_map.copy()