-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accept either arrays or maps as input where appropriate (#135)
Co-authored-by: Nabil Freij <[email protected]>
- Loading branch information
Showing
8 changed files
with
153 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Several functions have been updated to accept either numpy array or sunpy map inputs. | ||
The following functions now accept either a numpy array or sunpy map, and return the same data type: | ||
|
||
- `sunkit_image.enhance.mgn` | ||
- `sunkit_image.trace.bandpass_filter` | ||
- `sunkit_image.trace.smooth` | ||
|
||
The following functions now accept either a numpy array or sunpy map, and their return type is unchanged: | ||
|
||
- `sunkit_image.trace.occult2` |
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
12 changes: 7 additions & 5 deletions
12
sunkit_image/tests/figure_hashes_mpl_372_ft_261_sunkit_image_dev_sunpy_500.json
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{ | ||
"sunkit_image.tests.test_enhance.test_mgn": "4ef8a14c6b3290280b6fc9bc0748c8aa170760b2d43a5bbd67bb4d46ffd5408e", | ||
"sunkit_image.tests.test_radial.test_fig_nrgf": "52b448fa9d845841066d71a1fc6e7bfc471a658d726af1cf16403ddb858fcfc0", | ||
"sunkit_image.tests.test_radial.test_fig_fnrgf": "62621426ff8c3ee03cb1de004f9e08f63c4869dbd8b55d440adc73c74bfc5bcc", | ||
"sunkit_image.tests.test_trace.test_occult2_fig": "c5ff1416cb51dc51ad1705174e5b13f8cb02486dc3c58fd79dc1b80534f6caf5" | ||
} | ||
"sunkit_image.tests.test_enhance.test_mgn[array]": "67b17ac7c343a1d568d9dfa2bb12e0041e2fc53e8ca1c0e1c2c7693eda2bd0d8", | ||
"sunkit_image.tests.test_enhance.test_mgn[map]": "4ef8a14c6b3290280b6fc9bc0748c8aa170760b2d43a5bbd67bb4d46ffd5408e", | ||
"sunkit_image.tests.test_radial.test_fig_nrgf": "52b448fa9d845841066d71a1fc6e7bfc471a658d726af1cf16403ddb858fcfc0", | ||
"sunkit_image.tests.test_radial.test_fig_fnrgf": "62621426ff8c3ee03cb1de004f9e08f63c4869dbd8b55d440adc73c74bfc5bcc", | ||
"sunkit_image.tests.test_trace.test_occult2_fig[array]": "c5ff1416cb51dc51ad1705174e5b13f8cb02486dc3c58fd79dc1b80534f6caf5", | ||
"sunkit_image.tests.test_trace.test_occult2_fig[map]": "c5ff1416cb51dc51ad1705174e5b13f8cb02486dc3c58fd79dc1b80534f6caf5" | ||
} |
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,56 @@ | ||
import inspect | ||
from typing import Union, Callable | ||
|
||
import numpy as np | ||
|
||
from sunpy.map import GenericMap, Map | ||
|
||
|
||
def accept_array_or_map(*, arg_name: str, output_to_map=True) -> Callable[[Callable], Callable]: | ||
""" | ||
Decorator that allows a function to accept an array or a | ||
`sunpy.map.GenericMap` as an argument. | ||
This can be applied to functions that: | ||
- Take a single array or map as input | ||
- Return a single array that has the same pixel coordinates | ||
as the input array. | ||
Parameters | ||
---------- | ||
arg_name : `str` | ||
Name of data/map argument in function signature. | ||
output_to_map : `bool` | ||
If `True` (the default), convert the function return to a map if a map | ||
is given as input. For this to work the decorated function must return | ||
an array where pixels have the same coordinates as the input map data. | ||
""" | ||
|
||
def decorate(f: Callable) -> Callable: | ||
sig = inspect.signature(f) | ||
if arg_name not in sig.parameters: | ||
raise RuntimeError(f"Could not find '{arg_name}' in function signature") | ||
|
||
def inner(*args, **kwargs) -> Union[np.ndarray, GenericMap]: | ||
sig_bound = sig.bind(*args, **kwargs) | ||
map_arg = sig_bound.arguments[arg_name] | ||
if isinstance(map_arg, GenericMap): | ||
map_in = True | ||
sig_bound.arguments[arg_name] = map_arg.data | ||
elif isinstance(map_arg, np.ndarray): | ||
map_in = False | ||
else: | ||
raise ValueError(f"'{arg_name}' argument must be a sunpy map or numpy array (got type {type(map_arg)})") | ||
|
||
# Run decorated function | ||
array_out = f(*sig_bound.args, **sig_bound.kwargs) | ||
|
||
if map_in and output_to_map: | ||
return Map(array_out, map_arg.meta) | ||
else: | ||
return array_out | ||
|
||
return inner | ||
|
||
return decorate |