dkist-processing-math 2.2.2__py3-none-any.whl → 2.2.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dkist_processing_math/arithmetic.py +19 -0
- dkist_processing_math/tests/test_arithmetic.py +54 -1
- {dkist_processing_math-2.2.2.dist-info → dkist_processing_math-2.2.3.dist-info}/METADATA +1 -1
- {dkist_processing_math-2.2.2.dist-info → dkist_processing_math-2.2.3.dist-info}/RECORD +6 -6
- {dkist_processing_math-2.2.2.dist-info → dkist_processing_math-2.2.3.dist-info}/WHEEL +0 -0
- {dkist_processing_math-2.2.2.dist-info → dkist_processing_math-2.2.3.dist-info}/top_level.txt +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""Arithmetic."""
|
|
2
2
|
|
|
3
|
+
from datetime import datetime
|
|
3
4
|
from typing import Generator
|
|
4
5
|
from typing import Iterable
|
|
5
6
|
from typing import Union
|
|
@@ -124,3 +125,21 @@ def divide_fits_access_by_array(
|
|
|
124
125
|
for obj in access_objs:
|
|
125
126
|
obj.data = next(divide_arrays_by_array(obj.data, array_to_divide_by))
|
|
126
127
|
yield obj
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def closest_datetime(
|
|
131
|
+
target_datetime: str | datetime, datetimes: list[str | datetime] | str | datetime
|
|
132
|
+
) -> str | datetime:
|
|
133
|
+
"""Find the closest datetime to a target datetime. Return type is of the same type as the datetimes list."""
|
|
134
|
+
if isinstance(target_datetime, str):
|
|
135
|
+
target_datetime = datetime.fromisoformat(target_datetime)
|
|
136
|
+
if isinstance(datetimes, str) or isinstance(datetimes, datetime):
|
|
137
|
+
datetimes = [datetimes]
|
|
138
|
+
converted_datetimes = [
|
|
139
|
+
datetime.fromisoformat(d) if isinstance(d, str) else d for d in datetimes
|
|
140
|
+
]
|
|
141
|
+
closest_index, _ = min(
|
|
142
|
+
enumerate(converted_datetimes),
|
|
143
|
+
key=lambda dt: abs(dt[1] - target_datetime),
|
|
144
|
+
)
|
|
145
|
+
return datetimes[closest_index]
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from datetime import datetime
|
|
1
2
|
from typing import Generator
|
|
2
3
|
|
|
3
4
|
import numpy as np
|
|
@@ -5,6 +6,7 @@ import pytest
|
|
|
5
6
|
from astropy.io import fits
|
|
6
7
|
from dkist_processing_common.models.fits_access import FitsAccessBase
|
|
7
8
|
|
|
9
|
+
from dkist_processing_math.arithmetic import closest_datetime
|
|
8
10
|
from dkist_processing_math.arithmetic import divide_arrays_by_array
|
|
9
11
|
from dkist_processing_math.arithmetic import divide_fits_access_by_array
|
|
10
12
|
from dkist_processing_math.arithmetic import subtract_array_from_arrays
|
|
@@ -18,7 +20,7 @@ def multiple_test_fits_access(request, numpy_arrays):
|
|
|
18
20
|
for i, array in enumerate(numpy_arrays):
|
|
19
21
|
hdu = fits.PrimaryHDU(data=array)
|
|
20
22
|
hdu.header["TEST"] = i
|
|
21
|
-
access_list.append(FitsAccessBase(hdu=
|
|
23
|
+
access_list.append(FitsAccessBase(header=hdu.header, data=array))
|
|
22
24
|
if request.param == "multiple":
|
|
23
25
|
return access_list
|
|
24
26
|
return access_list[0]
|
|
@@ -126,3 +128,54 @@ def test_divide_arrays_by_array_wrong_shape(numpy_arrays_wrong_shape, numpy_arra
|
|
|
126
128
|
"""
|
|
127
129
|
with pytest.raises(ValueError):
|
|
128
130
|
list(divide_arrays_by_array(numpy_arrays_wrong_shape, numpy_array))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@pytest.mark.parametrize(
|
|
134
|
+
"datetimes, expected_result",
|
|
135
|
+
[
|
|
136
|
+
pytest.param(
|
|
137
|
+
["2025-12-31T00:00:00", "2026-01-07T00:00:00"],
|
|
138
|
+
"2025-12-31T00:00:00",
|
|
139
|
+
id="multiple solar gain ops",
|
|
140
|
+
),
|
|
141
|
+
pytest.param("2025-12-31T00:00:00", "2025-12-31T00:00:00", id="single solar gain op"),
|
|
142
|
+
],
|
|
143
|
+
)
|
|
144
|
+
def test_closest_datetime_with_strings(datetimes, expected_result):
|
|
145
|
+
"""
|
|
146
|
+
Given: a target datetime and a list of string datetimes
|
|
147
|
+
When: running the closest_datetime method
|
|
148
|
+
Then: the closest datetime from the list of datetimes is returned
|
|
149
|
+
"""
|
|
150
|
+
result = closest_datetime(
|
|
151
|
+
target_datetime="2026-01-01T00:00:00",
|
|
152
|
+
datetimes=datetimes,
|
|
153
|
+
)
|
|
154
|
+
assert result == expected_result
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
@pytest.mark.parametrize(
|
|
158
|
+
"datetimes, expected_result",
|
|
159
|
+
[
|
|
160
|
+
pytest.param(
|
|
161
|
+
[
|
|
162
|
+
datetime(2025, 12, 31),
|
|
163
|
+
datetime(2026, 1, 7),
|
|
164
|
+
],
|
|
165
|
+
datetime(2025, 12, 31),
|
|
166
|
+
id="multiple solar gain ops",
|
|
167
|
+
),
|
|
168
|
+
pytest.param(datetime(2025, 12, 31), datetime(2025, 12, 31), id="single solar gain op"),
|
|
169
|
+
],
|
|
170
|
+
)
|
|
171
|
+
def test_closest_datetime_with_datetimes(datetimes, expected_result):
|
|
172
|
+
"""
|
|
173
|
+
Given: a target datetime and a list of datetimes
|
|
174
|
+
When: running the closest_datetime method
|
|
175
|
+
Then: the closest datetime from the list of datetimes is returned
|
|
176
|
+
"""
|
|
177
|
+
result = closest_datetime(
|
|
178
|
+
target_datetime=datetime(2026, 1, 1),
|
|
179
|
+
datetimes=datetimes,
|
|
180
|
+
)
|
|
181
|
+
assert result == expected_result
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
changelog/.gitempty,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
dkist_processing_math/__init__.py,sha256=z2HXBhrQyX-yBiLTWnPUgVevMA7QI_3ZdJKn3ev1F8I,252
|
|
3
|
-
dkist_processing_math/arithmetic.py,sha256
|
|
3
|
+
dkist_processing_math/arithmetic.py,sha256=-kLO64IT2RxyykH1jlthvC0pegkhyJpIj9oa_9rdAdY,4457
|
|
4
4
|
dkist_processing_math/feature.py,sha256=xdhPkkoOX9aO50BeMjU_2vKwAOG1AUBA3TvfHzZuP6A,2004
|
|
5
5
|
dkist_processing_math/linear_algebra.py,sha256=yCA6ZuNORFnAyXywj73QMmGCpv9ZC2FSoMEPc8wToQY,4180
|
|
6
6
|
dkist_processing_math/statistics.py,sha256=KkZTy8wdslPSQ5qP4z_We60Vizaxqna7rmMwxkoj36U,3178
|
|
7
7
|
dkist_processing_math/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
8
|
dkist_processing_math/tests/conftest.py,sha256=TV0o-R2lU1MvZleme0w-akcgO5wOTlFNKhjSlmDwkqg,1593
|
|
9
|
-
dkist_processing_math/tests/test_arithmetic.py,sha256=
|
|
9
|
+
dkist_processing_math/tests/test_arithmetic.py,sha256=IzNm6u1ER5w_zlasOgTXlXSZjmpXnXpUmRb20XPx4DM,7320
|
|
10
10
|
dkist_processing_math/tests/test_feature.py,sha256=ZB8r_IUrAyTAlKbxEXwr8Q2zS7KPmlJhaU1l0guK0Xc,2311
|
|
11
11
|
dkist_processing_math/tests/test_linear_algebra.py,sha256=k-XqIIBPsenNMKqo2nQnDkkN-6T1nvVT8BrJnupAIFU,6495
|
|
12
12
|
dkist_processing_math/tests/test_statistics.py,sha256=N0S8C9Bwhd1VgCAkm6RqIZF_Ulwrcet9wqfbndw8KZs,2998
|
|
@@ -24,7 +24,7 @@ docs/landing_page.rst,sha256=JCyR9SRhYS6zeEUlonIzko1kQ0tAFDIfi7pCoQanZeA,251
|
|
|
24
24
|
docs/make.bat,sha256=mBAhtURwhQ7yc95pqwJzlhqBSvRknr1aqZ5s8NKvdKs,4513
|
|
25
25
|
docs/requirements.txt,sha256=Kbl_X4c7RQZw035YTeNB63We6I7pvXFU4T0Uflp2yDY,29
|
|
26
26
|
licenses/LICENSE.rst,sha256=piZaQplkzOMmH1NXg6QIdo9wwo9pPCoHkvm2-DmH76E,1462
|
|
27
|
-
dkist_processing_math-2.2.
|
|
28
|
-
dkist_processing_math-2.2.
|
|
29
|
-
dkist_processing_math-2.2.
|
|
30
|
-
dkist_processing_math-2.2.
|
|
27
|
+
dkist_processing_math-2.2.3.dist-info/METADATA,sha256=rDz6CyzGJ7lTuHHw4-ul1VtpZ7Mozss5SMdWFh8YTEU,4467
|
|
28
|
+
dkist_processing_math-2.2.3.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
29
|
+
dkist_processing_math-2.2.3.dist-info/top_level.txt,sha256=7NeFrOlR4xhZ3LMT4jN99LQugukjr6l9u3w_s6b-uO4,46
|
|
30
|
+
dkist_processing_math-2.2.3.dist-info/RECORD,,
|
|
File without changes
|
{dkist_processing_math-2.2.2.dist-info → dkist_processing_math-2.2.3.dist-info}/top_level.txt
RENAMED
|
File without changes
|