unitx 0.16.1__tar.gz → 0.16.2__tar.gz
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.
- {unitx-0.16.1 → unitx-0.16.2}/.github/consumers/arrays.py +17 -1
- {unitx-0.16.1 → unitx-0.16.2}/CHANGELOG.md +12 -0
- {unitx-0.16.1 → unitx-0.16.2}/PKG-INFO +1 -1
- {unitx-0.16.1 → unitx-0.16.2}/docs/numpy/selection.md +18 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/numpy/statistics.md +30 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/numpy/writes.md +1 -1
- {unitx-0.16.1 → unitx-0.16.2}/docs/numpy.md +8 -7
- {unitx-0.16.1 → unitx-0.16.2}/pyproject.toml +1 -1
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_numerics/numpy.py +32 -18
- {unitx-0.16.1 → unitx-0.16.2}/tests/numpy_typing/check_numpy.py +10 -1
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_affine.py +2 -0
- unitx-0.16.2/tests/test_numpy_nan_reductions.py +260 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_outputs.py +1 -1
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_products.py +23 -23
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_statistics.py +13 -3
- {unitx-0.16.1 → unitx-0.16.2}/.github/consumers/pyrightconfig.json +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/.github/consumers/scalars.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/.github/workflows/ci.yml +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/.github/workflows/pages.yml +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/.github/workflows/release.yml +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/.gitignore +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/.readthedocs.yaml +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/CONTRIBUTING.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/LICENSE +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/README.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/benchmarks/README.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/benchmarks/numpy_writes.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/benchmarks/scalars.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/conftest.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/README.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/affine.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/architecture.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/catalogues/README.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/catalogues/cgs.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/catalogues/imperial.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/catalogues/si.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/custom-units.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/display.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/images/blackbody.svg +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/javascripts/mermaid.mjs +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/logarithmic.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/migration.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/numpy/calculus.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/requirements.txt +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/scalars.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/serialization.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/stdlib.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/typing.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/docs/units.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/README.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/arrhenius_fit.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/blackbody.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/damped_oscillator.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/gaussian_wavepacket.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/impedance.md +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/impedance.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/optical_attenuation.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/pendulum_uncertainty.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/examples/smith_chart.ipynb +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/mkdocs.yml +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/pyrightconfig.json +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_constnamespace.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/affine.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/definition.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/logarithmic.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/logarithms.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/points.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_coordinates/resolve.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_electromagnetism/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_electromagnetism/coefficients.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_electromagnetism/conversion.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_formatting/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_formatting/render.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_formatting/unicode.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_immutable.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_numerics/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_numerics/conversion.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_numerics/numpy_typing.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_numerics/scaling.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_operands.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_registry/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_registry/builtin_units.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_registry/prefixes.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/_registry/reconstruction.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/affinequantity.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/affineunit.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/cgs/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/cgs/units.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/dimension.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/exceptions.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/factor.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/imperial/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/imperial/units.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/logquantity.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/logunit.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/math.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/monomial.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/py.typed +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/quantity.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/serialization.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/si/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/si/bases.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/si/constants.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/si/units.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/singleunit.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/time.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/unit.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/unitcatalog.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/unitdefinition.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/unitparser.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/src/unitx/unitregistry.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/__init__.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/_notebooks.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/numpy_typing/check_asarray.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/numpy_typing/check_cgs.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/numpy_typing/check_logarithmic.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/numpy_typing/check_math.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_affine.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_affinequantity.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_builtin_definitions.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_catalog_collection.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_cgs.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_cgs_numpy.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_cgs_systems.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_decibel_call.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_definition_scope.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_dimension.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_display.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_examples.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_factor.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_immutable.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_impedance_example.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_imperial.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_import.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_linear_boundaries.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_logarithmic_model.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_math.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_math_numpy.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_monomial.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numeric.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_differentiation.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_integration.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_interpolation.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_multi_outputs.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_mutation.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_nan_statistics.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_selection.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_numpy_weighted_statistics.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_quantity.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_registry.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_registry_scope.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_serialization.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_si.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_singleunit.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_siunitx_display.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_time.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_typing.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_unicode.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_unicode_units.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_unit.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_unit_definition.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_unitparser.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_unitregistry.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/test_warnings.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_affine.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_cgs.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_impedance_example.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_imperial.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_logarithmic.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_math_time.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_quantity.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_registry.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_serialization.py +0 -0
- {unitx-0.16.1 → unitx-0.16.2}/tests/typing/check_si.py +0 -0
|
@@ -94,11 +94,27 @@ def check_arrays(values: NDArray[np.float64]) -> None:
|
|
|
94
94
|
np.testing.assert_allclose(median_target.value, 2000)
|
|
95
95
|
|
|
96
96
|
gapped = Quantity(np.array([1.0, np.nan, 3]), si.m)
|
|
97
|
-
for operation in (
|
|
97
|
+
for operation in (
|
|
98
|
+
np.nanmean,
|
|
99
|
+
np.nanmedian,
|
|
100
|
+
np.nanstd,
|
|
101
|
+
np.nanvar,
|
|
102
|
+
np.nanmin,
|
|
103
|
+
np.nanmax,
|
|
104
|
+
np.nansum,
|
|
105
|
+
):
|
|
98
106
|
result = cast(Quantity[Array], operation(gapped))
|
|
99
107
|
assert isinstance(result, Quantity)
|
|
100
108
|
assert result.unit == (si.m**2 if operation is np.nanvar else si.m)
|
|
101
109
|
np.testing.assert_allclose(result.value, operation(gapped.value))
|
|
110
|
+
product = cast(Quantity[Array], np.nanprod(gapped))
|
|
111
|
+
assert isinstance(product, Quantity) and product.unit == si.m**2
|
|
112
|
+
np.testing.assert_allclose(product.value, 3)
|
|
113
|
+
for operation in (np.nanargmin, np.nanargmax):
|
|
114
|
+
indices = np.empty((), dtype=np.intp)
|
|
115
|
+
assert operation(gapped, out=indices) is indices
|
|
116
|
+
assert_type(indices.dtype, np.dtype[np.intp])
|
|
117
|
+
np.testing.assert_array_equal(indices, operation(gapped.value))
|
|
102
118
|
for operation, probability in ((np.nanquantile, 0.5), (np.nanpercentile, 50)):
|
|
103
119
|
result = cast(Quantity[Array], operation(gapped, probability))
|
|
104
120
|
assert isinstance(result, Quantity) and result.unit == si.m
|
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.16.2] - 2026-10-25
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Support NumPy `nanmin`, `nanmax`, `nanargmin` and `nanargmax`, preserving
|
|
10
|
+
units or point coordinates for extrema and returning bare indices. Retain
|
|
11
|
+
NumPy's all-NaN warnings/errors and stage output writes before mutation.
|
|
12
|
+
- Support `nansum` with the existing sum unit, initial and mask rules, and
|
|
13
|
+
`nanprod` with unit powers determined by selected non-NaN element counts.
|
|
14
|
+
Dimensional product slices require equal counts; dimensionless products
|
|
15
|
+
normalize to coherent values and may have different counts.
|
|
16
|
+
|
|
5
17
|
## [0.16.1] - 2026-10-25
|
|
6
18
|
|
|
7
19
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: unitx
|
|
3
|
-
Version: 0.16.
|
|
3
|
+
Version: 0.16.2
|
|
4
4
|
Summary: Physical units, quantities, and unit conversion with exact unit algebra, type hints, and NumPy integration.
|
|
5
5
|
Project-URL: Homepage, https://github.com/DaitTHU/unitx
|
|
6
6
|
Project-URL: Documentation, https://daitthu.github.io/unitx/
|
|
@@ -50,9 +50,27 @@ Empty reductions, axes, NaNs and `keepdims` follow NumPy. These reductions
|
|
|
50
50
|
write the exact result shape to `out`; elementwise selection and clipping
|
|
51
51
|
allow the ufunc output broadcasting rules.
|
|
52
52
|
|
|
53
|
+
`np.nanmin/max` share the same unit, point, mask, initial and output rules,
|
|
54
|
+
while omitting NaNs. An all-NaN slice returns NaN and emits RuntimeWarning;
|
|
55
|
+
an empty reduction still needs an initial value. A warning promoted to an
|
|
56
|
+
exception leaves `out` unchanged.
|
|
57
|
+
|
|
58
|
+
`np.argmin/argmax` and `np.nanargmin/nanargmax` return bare indices and accept
|
|
59
|
+
one integer axis or `axis=None`, `keepdims`, and a bare integer ndarray `out`
|
|
60
|
+
of exactly the result shape. They also accept affine and logarithmic points
|
|
61
|
+
and operate on stored readings. The NaN variants omit NaNs and raise
|
|
62
|
+
ValueError for all-NaN slices before writing `out`. NumPy's caveat remains:
|
|
63
|
+
`nanargmin` results cannot be trusted for slices containing only NaNs and
|
|
64
|
+
positive infinity; the corresponding `nanargmax` case is NaNs and negative
|
|
65
|
+
infinity. unitx delegates these numerical choices to NumPy.
|
|
66
|
+
|
|
53
67
|
See NumPy's [where](https://numpy.org/doc/stable/reference/generated/numpy.where.html),
|
|
54
68
|
[clip](https://numpy.org/doc/stable/reference/generated/numpy.clip.html) and
|
|
55
69
|
[minimum](https://numpy.org/doc/stable/reference/generated/numpy.minimum.html)
|
|
56
70
|
references for the underlying numerical behavior.
|
|
71
|
+
See also [nanmin](https://numpy.org/doc/stable/reference/generated/numpy.nanmin.html),
|
|
72
|
+
[nanargmin](https://numpy.org/doc/stable/reference/generated/numpy.nanargmin.html)
|
|
73
|
+
and [nanargmax](https://numpy.org/doc/stable/reference/generated/numpy.nanargmax.html)
|
|
74
|
+
for NaN and infinity behavior.
|
|
57
75
|
|
|
58
76
|
[NumPy overview and support matrix](../numpy.md) · [Documentation index](../README.md)
|
|
@@ -191,6 +191,22 @@ described here and uses the installed NumPy kernel.
|
|
|
191
191
|
|
|
192
192
|
## NaN statistics
|
|
193
193
|
|
|
194
|
+
`nansum` omits NaN samples and otherwise shares `sum`'s unit, `axis`, `dtype`,
|
|
195
|
+
`keepdims`, `where`, scalar `initial` and staged `out` rules. All-NaN, empty
|
|
196
|
+
and fully masked slices return zero in the input unit, or the converted
|
|
197
|
+
initial when supplied. Affine and logarithmic point sums remain undefined.
|
|
198
|
+
|
|
199
|
+
`nanprod` shares `prod`'s options and initial-unit rules. For dimensional
|
|
200
|
+
input U, its unit is `U**N * initial.unit`, where N counts elements selected
|
|
201
|
+
by `where` that are **not NaN**. Zero and infinity still count. All result
|
|
202
|
+
slices must have equal N, even when a zero initial makes their magnitudes
|
|
203
|
+
equal; otherwise DimensionError is raised before writing `out`. All-NaN,
|
|
204
|
+
empty and fully masked slices have N = 0, with value 1 and unit `Unit()`
|
|
205
|
+
before applying initial. When the output has no elements, use `prod`'s
|
|
206
|
+
nominal-extent convention. Dimensionless inputs normalize to coherent values
|
|
207
|
+
first, so unequal non-NaN counts are allowed. Point products remain undefined.
|
|
208
|
+
NaN initial values are passed through to NumPy; they are not omitted.
|
|
209
|
+
|
|
194
210
|
`nanmean`, `nanmedian`, `nanstd`, `nanvar`, `nanquantile`, and `nanpercentile`
|
|
195
211
|
omit NaN samples. Their units, supported options, parameter normalization and
|
|
196
212
|
staged `out` writes match the corresponding functions above. Sample units,
|
|
@@ -235,11 +251,25 @@ assert variances.unit == Unit('m2')
|
|
|
235
251
|
np.testing.assert_allclose(variances.value, [2, 2])
|
|
236
252
|
medians = np.nanpercentile(gapped, 50, axis=1)
|
|
237
253
|
np.testing.assert_allclose(medians.value, [2, 6])
|
|
254
|
+
totals = np.nansum(gapped, axis=1)
|
|
255
|
+
assert totals.unit == Unit('m')
|
|
256
|
+
np.testing.assert_array_equal(totals.value, [4, 12])
|
|
257
|
+
areas = np.nanprod(gapped, axis=1)
|
|
258
|
+
assert areas.unit == Unit('m2')
|
|
259
|
+
np.testing.assert_array_equal(areas.value, [3, 35])
|
|
260
|
+
minima = np.nanmin(gapped, axis=1)
|
|
261
|
+
assert minima.unit == Unit('m')
|
|
262
|
+
np.testing.assert_array_equal(minima.value, [1, 5])
|
|
263
|
+
np.testing.assert_array_equal(np.nanargmax(gapped, axis=1), [2, 2])
|
|
238
264
|
```
|
|
239
265
|
|
|
240
266
|
See [NumPy nanmean](https://numpy.org/doc/stable/reference/generated/numpy.nanmean.html),
|
|
241
267
|
[nanstd](https://numpy.org/doc/stable/reference/generated/numpy.nanstd.html) and
|
|
242
268
|
[nanquantile](https://numpy.org/doc/stable/reference/generated/numpy.nanquantile.html)
|
|
243
269
|
for the numerical definitions and warnings.
|
|
270
|
+
The [nansum](https://numpy.org/doc/stable/reference/generated/numpy.nansum.html)
|
|
271
|
+
and [nanprod](https://numpy.org/doc/stable/reference/generated/numpy.nanprod.html)
|
|
272
|
+
kernels determine NaN identities and accumulator behavior, with unitx's
|
|
273
|
+
additional single-unit rules above.
|
|
244
274
|
|
|
245
275
|
[NumPy overview and support matrix](../numpy.md) · [Documentation index](../README.md)
|
|
@@ -125,7 +125,7 @@ length target. `Unit` factors participate in the conversion. Every numeric
|
|
|
125
125
|
result, including a coherent or scaled dimensionless result, requires a Quantity
|
|
126
126
|
target; writing it to a bare ndarray would discard its unit. Boolean predicates
|
|
127
127
|
and comparisons instead accept only a bare boolean ndarray output.
|
|
128
|
-
`argmin` and `
|
|
128
|
+
`argmin`, `argmax`, `nanargmin` and `nanargmax` accept only a bare integer ndarray output, with the
|
|
129
129
|
same exact shape requirement as other function outputs.
|
|
130
130
|
|
|
131
131
|
Final casting uses `casting='same_kind'` by default, or the explicitly requested
|
|
@@ -116,18 +116,18 @@ functions, including exp/log. Python scalar numerical semantics remain native.
|
|
|
116
116
|
| --- | --- | --- |
|
|
117
117
|
| reshape, transpose | Preserve unit | shape/order/copy; axes |
|
|
118
118
|
| concatenate, stack | Convert to first Quantity's unit; bare arrays remain dimensionless | axis, dtype, casting, out; dtype and out are mutually exclusive |
|
|
119
|
-
| sum | Preserve unit | axis, dtype, keepdims, initial, where, out |
|
|
119
|
+
| sum, nansum | Preserve unit | axis, dtype, keepdims, initial, where, out |
|
|
120
120
|
| mean, nanmean | Preserve unit or point coordinate | axis, dtype, keepdims, where, out |
|
|
121
|
-
| min/amin, max/amax | Preserve unit or point coordinate | axis, keepdims, initial, where, out |
|
|
121
|
+
| min/amin/nanmin, max/amax/nanmax | Preserve unit or point coordinate | axis, keepdims, initial, where, out |
|
|
122
122
|
| where | Select between compatible quantities or points | condition, x, y, all positional; no out |
|
|
123
123
|
| clip | Preserve input unit or point coordinate | a_min/a_max or min/max, out; ufunc call options |
|
|
124
|
-
| prod | Product unit rules below | axis, dtype, keepdims, initial, where, out |
|
|
124
|
+
| prod, nanprod | Product unit rules below; nanprod counts selected non-NaN elements | axis, dtype, keepdims, initial, where, out |
|
|
125
125
|
| median, nanmedian | Preserve unit or point coordinate | axis, keepdims, out; overwrite_input=False |
|
|
126
126
|
| std/nanstd, var/nanvar | Difference unit / square difference unit | axis, dtype, ddof, keepdims, where, out |
|
|
127
127
|
| average | Preserve sample unit; returned weight sum is a dimensionless Quantity | axis, weights, returned, keepdims; linear Quantity only |
|
|
128
128
|
| quantile, percentile, nanquantile, nanpercentile | Preserve sample unit; dimensionless q and weights | axis, out, method, keepdims, weights; overwrite_input=False; linear Quantity only |
|
|
129
129
|
| cumsum | Preserve unit | axis, dtype, out |
|
|
130
|
-
| argmin, argmax | Bare indices | axis, keepdims, out |
|
|
130
|
+
| argmin/nanargmin, argmax/nanargmax | Bare indices | axis, keepdims, out |
|
|
131
131
|
| interp | Align x to xp's unit; retain fp's unit | finite strictly increasing 1-D xp; scalar left/right, period=None; no out |
|
|
132
132
|
| trapezoid | Multiply ordinate and coordinate units | x, scalar dx, axis; no out |
|
|
133
133
|
| diff | Preserve input unit | n, axis, prepend, append; no out |
|
|
@@ -138,9 +138,9 @@ Supply the first magnitude argument positionally; concatenate/stack take a
|
|
|
138
138
|
sequence. Options follow each supported signature, including positional out
|
|
139
139
|
checks. Function and reduction outputs follow the rules below;
|
|
140
140
|
overwrite_input=True is rejected. Reduction/statistical `where` is supported
|
|
141
|
-
for sum, mean/nanmean, std/nanstd, var/nanvar, add.reduce, prod,
|
|
142
|
-
and min/max; `initial` is supported for
|
|
143
|
-
and min/max. Precomputed `mean`,
|
|
141
|
+
for sum/nansum, mean/nanmean, std/nanstd, var/nanvar, add.reduce, prod/nanprod,
|
|
142
|
+
multiply.reduce, and min/max/nanmin/nanmax; `initial` is supported for
|
|
143
|
+
sum/nansum/add.reduce, prod/nanprod/multiply.reduce, and min/max/nanmin/nanmax. Precomputed `mean`,
|
|
144
144
|
`correction`, and other unsupported keywords raise TypeError.
|
|
145
145
|
Indexed assignment and augmented arithmetic on ndarrays follow the fixed-target
|
|
146
146
|
rules below. Python and NumPy scalar magnitudes retain ordinary result/rebinding
|
|
@@ -150,6 +150,7 @@ behavior for augmented arithmetic.
|
|
|
150
150
|
|
|
151
151
|
See [selection and clipping](numpy/selection.md) for `where`, `clip`,
|
|
152
152
|
`minimum/maximum`, and `min/max`, including coordinate and bound rules.
|
|
153
|
+
The NaN extrema and index functions share those coordinate rules and omit NaNs.
|
|
153
154
|
|
|
154
155
|
## Indexed assignment and augmented arithmetic
|
|
155
156
|
|
|
@@ -4,7 +4,7 @@ build-backend = 'hatchling.build'
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = 'unitx'
|
|
7
|
-
version = '0.16.
|
|
7
|
+
version = '0.16.2'
|
|
8
8
|
description = 'Physical units, quantities, and unit conversion with exact unit algebra, type hints, and NumPy integration.'
|
|
9
9
|
readme = 'README.md'
|
|
10
10
|
requires-python = '>=3.12'
|
|
@@ -403,7 +403,7 @@ def _dispatch_ufunc(
|
|
|
403
403
|
}:
|
|
404
404
|
return NotImplemented
|
|
405
405
|
handler = _sum if ufunc is np.add else _prod
|
|
406
|
-
return handler(inputs[0], **{'axis': 0, **kwargs})
|
|
406
|
+
return handler(ufunc.reduce, inputs[0], **{'axis': 0, **kwargs})
|
|
407
407
|
if method == 'accumulate' and ufunc is np.add:
|
|
408
408
|
if kwargs.keys() - {'axis', 'dtype'}:
|
|
409
409
|
return NotImplemented
|
|
@@ -606,6 +606,8 @@ def dispatch_function(
|
|
|
606
606
|
np.transpose,
|
|
607
607
|
np.argmin,
|
|
608
608
|
np.argmax,
|
|
609
|
+
np.nanargmin,
|
|
610
|
+
np.nanargmax,
|
|
609
611
|
np.median,
|
|
610
612
|
np.mean,
|
|
611
613
|
np.std,
|
|
@@ -618,6 +620,8 @@ def dispatch_function(
|
|
|
618
620
|
np.max,
|
|
619
621
|
np.amin,
|
|
620
622
|
np.amax,
|
|
623
|
+
np.nanmin,
|
|
624
|
+
np.nanmax,
|
|
621
625
|
):
|
|
622
626
|
raise TypeError(
|
|
623
627
|
'This NumPy function is undefined for affine points; convert explicitly.'
|
|
@@ -961,6 +965,7 @@ def _quantile(
|
|
|
961
965
|
|
|
962
966
|
|
|
963
967
|
def _sum(
|
|
968
|
+
func: Any,
|
|
964
969
|
q: Any,
|
|
965
970
|
axis: Any = None,
|
|
966
971
|
dtype: Any = None,
|
|
@@ -975,33 +980,34 @@ def _sum(
|
|
|
975
980
|
initial_value = None
|
|
976
981
|
if initial is not None:
|
|
977
982
|
if not _is_supported_operand(initial):
|
|
978
|
-
|
|
983
|
+
error_message = 'initial must be a numeric scalar or scalar Quantity.'
|
|
984
|
+
raise TypeError(error_message)
|
|
979
985
|
initial_value, initial_unit = _linear_parts(initial)
|
|
980
986
|
if np.ndim(initial_value) != 0:
|
|
981
|
-
|
|
987
|
+
error_message = 'initial must be a numeric scalar or scalar Quantity.'
|
|
988
|
+
raise TypeError(error_message)
|
|
982
989
|
if not compatible(initial_unit, unit):
|
|
983
990
|
# Bare zero is the additive identity in any dimension. A Quantity
|
|
984
991
|
# zero still has its declared dimension and must be compatible.
|
|
985
992
|
if isinstance(initial, (Quantity, _Point)) or initial != 0:
|
|
986
|
-
|
|
993
|
+
error_message = 'initial has an incompatible dimension.'
|
|
994
|
+
raise DimensionError(error_message)
|
|
987
995
|
else:
|
|
988
996
|
initial_value = scale_magnitude(initial_value, initial_unit.factor / unit.factor)
|
|
989
997
|
result = _wrap(
|
|
990
|
-
|
|
991
|
-
value, axis=axis, dtype=dtype, keepdims=keepdims, initial=initial_value, where=where
|
|
992
|
-
),
|
|
998
|
+
func(value, axis=axis, dtype=dtype, keepdims=keepdims, initial=initial_value, where=where),
|
|
993
999
|
unit,
|
|
994
1000
|
)
|
|
995
1001
|
return _write_output(result, out)
|
|
996
1002
|
|
|
997
1003
|
|
|
998
1004
|
def _prod(
|
|
1005
|
+
func: Any,
|
|
999
1006
|
q: Any,
|
|
1000
1007
|
axis: Any = None,
|
|
1001
1008
|
dtype: Any = None,
|
|
1002
1009
|
out: Any = None,
|
|
1003
1010
|
keepdims: bool = False,
|
|
1004
|
-
*,
|
|
1005
1011
|
initial: Any = 1,
|
|
1006
1012
|
where: Any = True,
|
|
1007
1013
|
) -> Any:
|
|
@@ -1010,10 +1016,12 @@ def _prod(
|
|
|
1010
1016
|
initial_value, initial_unit = None, Unit()
|
|
1011
1017
|
if initial is not None:
|
|
1012
1018
|
if not _is_supported_operand(initial):
|
|
1013
|
-
|
|
1019
|
+
error_message = 'initial must be a numeric scalar or scalar Quantity.'
|
|
1020
|
+
raise TypeError(error_message)
|
|
1014
1021
|
initial_value, initial_unit = _linear_parts(initial)
|
|
1015
1022
|
if np.ndim(initial_value) != 0:
|
|
1016
|
-
|
|
1023
|
+
error_message = 'initial must be a numeric scalar or scalar Quantity.'
|
|
1024
|
+
raise TypeError(error_message)
|
|
1017
1025
|
|
|
1018
1026
|
value, unit = _linear_parts(q)
|
|
1019
1027
|
mask = np.broadcast_to(where, np.shape(value))
|
|
@@ -1021,7 +1029,7 @@ def _prod(
|
|
|
1021
1029
|
if unit.dimension == DIMENSIONLESS:
|
|
1022
1030
|
value = scale_magnitude(value, unit.factor, where=mask)
|
|
1023
1031
|
unit = Unit()
|
|
1024
|
-
result =
|
|
1032
|
+
result = func(
|
|
1025
1033
|
value, axis=axis, dtype=dtype, keepdims=keepdims, initial=initial_value, where=where
|
|
1026
1034
|
)
|
|
1027
1035
|
if unit.dimension != DIMENSIONLESS:
|
|
@@ -1029,16 +1037,17 @@ def _prod(
|
|
|
1029
1037
|
axes = range(len(shape)) if axis is None else axis if isinstance(axis, tuple) else (axis,)
|
|
1030
1038
|
# NumPy has already validated axes, including its scalar 0/-1 special case.
|
|
1031
1039
|
count = math.prod(shape[i] for i in axes) if shape else 1
|
|
1040
|
+
if func is np.nanprod:
|
|
1041
|
+
mask = mask & ~np.isnan(value)
|
|
1032
1042
|
if np.ndim(where) == 0:
|
|
1033
1043
|
count = count if bool(where) else 0
|
|
1034
|
-
|
|
1044
|
+
if np.ndim(where) != 0 or func is np.nanprod:
|
|
1035
1045
|
counts = np.sum(mask, axis=axis)
|
|
1036
1046
|
if np.size(counts):
|
|
1037
1047
|
count = int(np.asarray(counts).flat[0])
|
|
1038
1048
|
if np.any(counts != count):
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
)
|
|
1049
|
+
error_message = 'Product reductions require equal element counts per result.'
|
|
1050
|
+
raise DimensionError(error_message)
|
|
1042
1051
|
# With no output elements, retain the nominal reduction extent.
|
|
1043
1052
|
unit = unit**count
|
|
1044
1053
|
assert isinstance(unit, Unit) and isinstance(initial_unit, Unit)
|
|
@@ -1191,7 +1200,6 @@ def _extremum(
|
|
|
1191
1200
|
axis: Any = None,
|
|
1192
1201
|
out: Any = None,
|
|
1193
1202
|
keepdims: bool = False,
|
|
1194
|
-
*,
|
|
1195
1203
|
initial: Any = _UNSET,
|
|
1196
1204
|
where: Any = True,
|
|
1197
1205
|
) -> Any:
|
|
@@ -1210,15 +1218,19 @@ _QUANTITY_FUNCTIONS = {
|
|
|
1210
1218
|
np.transpose: _transpose,
|
|
1211
1219
|
np.argmin: partial(_argmin_max, np.argmin),
|
|
1212
1220
|
np.argmax: partial(_argmin_max, np.argmax),
|
|
1221
|
+
np.nanargmin: partial(_argmin_max, np.nanargmin),
|
|
1222
|
+
np.nanargmax: partial(_argmin_max, np.nanargmax),
|
|
1213
1223
|
np.median: partial(_median, np.median),
|
|
1214
1224
|
np.nanmedian: partial(_median, np.nanmedian),
|
|
1215
1225
|
np.cumsum: _cumsum,
|
|
1216
|
-
np.prod: _prod,
|
|
1226
|
+
np.prod: partial(_prod, np.multiply.reduce),
|
|
1227
|
+
np.nanprod: partial(_prod, np.nanprod),
|
|
1217
1228
|
np.std: partial(_std_var, np.std),
|
|
1218
1229
|
np.var: partial(_std_var, np.var),
|
|
1219
1230
|
np.nanstd: partial(_std_var, np.nanstd),
|
|
1220
1231
|
np.nanvar: partial(_std_var, np.nanvar),
|
|
1221
|
-
np.sum: _sum,
|
|
1232
|
+
np.sum: partial(_sum, np.add.reduce),
|
|
1233
|
+
np.nansum: partial(_sum, np.nansum),
|
|
1222
1234
|
np.mean: partial(_mean, np.mean),
|
|
1223
1235
|
np.nanmean: partial(_mean, np.nanmean),
|
|
1224
1236
|
np.average: _average,
|
|
@@ -1230,4 +1242,6 @@ _QUANTITY_FUNCTIONS = {
|
|
|
1230
1242
|
np.max: partial(_extremum, np.max),
|
|
1231
1243
|
np.amin: partial(_extremum, np.amin),
|
|
1232
1244
|
np.amax: partial(_extremum, np.amax),
|
|
1245
|
+
np.nanmin: partial(_extremum, np.nanmin),
|
|
1246
|
+
np.nanmax: partial(_extremum, np.nanmax),
|
|
1233
1247
|
}
|
|
@@ -125,8 +125,16 @@ def output_contracts(a: npt.NDArray[np.float64]) -> None:
|
|
|
125
125
|
q.__array_ufunc__(
|
|
126
126
|
np.add, 'reduce', q, out=(scalar_target,), where=mask, initial=Quantity(2, 'cm')
|
|
127
127
|
)
|
|
128
|
-
for operation in (np.sum, np.mean, np.std, np.nanmean, np.nanstd):
|
|
128
|
+
for operation in (np.sum, np.nansum, np.mean, np.std, np.nanmean, np.nanstd):
|
|
129
129
|
q.__array_function__(operation, (Quantity,), (q,), {'where': mask, 'out': scalar_target})
|
|
130
|
+
for operation in (np.nanmin, np.nanmax):
|
|
131
|
+
q.__array_function__(
|
|
132
|
+
operation, (Quantity,), (q,), {'initial': Quantity(0, 'cm'), 'out': scalar_target}
|
|
133
|
+
)
|
|
134
|
+
for operation in (np.nanargmin, np.nanargmax):
|
|
135
|
+
indices = np.empty((), dtype=np.intp)
|
|
136
|
+
q.__array_function__(operation, (Quantity,), (q,), {'out': indices})
|
|
137
|
+
assert_type(indices.dtype, np.dtype[np.intp])
|
|
130
138
|
q.__array_function__(np.nanmedian, (Quantity,), (q,), {'out': scalar_target})
|
|
131
139
|
for operation in (np.var, np.nanvar):
|
|
132
140
|
q.__array_function__(
|
|
@@ -144,6 +152,7 @@ def output_contracts(a: npt.NDArray[np.float64]) -> None:
|
|
|
144
152
|
(q,),
|
|
145
153
|
{'initial': Quantity(2, 's'), 'out': Quantity(np.empty(()), 'm2 s')},
|
|
146
154
|
)
|
|
155
|
+
q.__array_function__(np.nanprod, (Quantity,), (q,), {'where': mask, 'out': product_target})
|
|
147
156
|
assert_type(product_target.value.dtype, np.dtype[np.float64])
|
|
148
157
|
assert_type(scalar_target.value.dtype, np.dtype[np.float64])
|
|
149
158
|
assert_type(widened.value, Array)
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""NaN extrema, indices, sums and products retain their unit contracts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import warnings
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
np = pytest.importorskip('numpy')
|
|
12
|
+
|
|
13
|
+
from unitx import AffineQuantity, LogQuantity, Quantity, Unit # noqa: E402
|
|
14
|
+
from unitx.exceptions import DimensionError # noqa: E402
|
|
15
|
+
from unitx.factor import Factor # noqa: E402
|
|
16
|
+
from unitx.imperial import imperial # noqa: E402
|
|
17
|
+
from unitx.si import si # noqa: E402
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from numpy.typing import DTypeLike
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
EXTREMA = [np.nanmin, np.nanmax]
|
|
24
|
+
INDICES = [np.nanargmin, np.nanargmax]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.mark.parametrize('operation', [*EXTREMA, np.nansum])
|
|
28
|
+
@pytest.mark.parametrize('axis', [None, 0, -1, (), (0, -1)])
|
|
29
|
+
@pytest.mark.parametrize('dtype', [np.int32, np.float32, np.longdouble, np.complex128])
|
|
30
|
+
@pytest.mark.parametrize('unit', [Unit('cm', scale=Factor(2)), Unit('km/m')])
|
|
31
|
+
def test_numpy_nan_reduction_axes(
|
|
32
|
+
operation: Callable[..., Any],
|
|
33
|
+
axis: int | tuple[int, ...] | None,
|
|
34
|
+
dtype: DTypeLike,
|
|
35
|
+
unit: Unit,
|
|
36
|
+
) -> None:
|
|
37
|
+
values = np.arange(48, dtype=dtype).reshape(2, 3, 8)[..., ::2]
|
|
38
|
+
if values.dtype.kind in 'fc':
|
|
39
|
+
values[0, :, 0] = np.nan
|
|
40
|
+
saved = values.copy()
|
|
41
|
+
values.flags.writeable = False
|
|
42
|
+
samples = Quantity(values, unit)
|
|
43
|
+
for keepdims in (False, True):
|
|
44
|
+
with warnings.catch_warnings():
|
|
45
|
+
warnings.simplefilter('ignore', RuntimeWarning) # axis=() retains all-NaN slices.
|
|
46
|
+
result = operation(samples, axis=axis, keepdims=keepdims)
|
|
47
|
+
expected = operation(values, axis=axis, keepdims=keepdims)
|
|
48
|
+
assert result.unit == unit
|
|
49
|
+
np.testing.assert_array_equal(result.value, expected)
|
|
50
|
+
assert np.asarray(result.value).dtype == np.asarray(expected).dtype
|
|
51
|
+
np.testing.assert_array_equal(values, saved)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@pytest.mark.parametrize('operation', EXTREMA)
|
|
55
|
+
def test_numpy_nan_extremum_initial(operation: Callable[..., Any]) -> None:
|
|
56
|
+
samples = Quantity(np.array([[np.nan, 2, 3], [4, np.nan, 6]]), 'm')
|
|
57
|
+
mask = np.array([True, True, False])
|
|
58
|
+
target = Quantity(np.empty((2, 1)), 'cm')
|
|
59
|
+
expected = operation(samples.value, 1, None, True, 2.5, mask)
|
|
60
|
+
assert operation(samples, 1, target, True, Quantity(250, 'cm'), mask) is target
|
|
61
|
+
np.testing.assert_allclose(target.value, expected * 100)
|
|
62
|
+
empty = Quantity(np.empty((2, 0)), 'm')
|
|
63
|
+
np.testing.assert_array_equal(operation(empty, axis=1, initial=Quantity(2, 'm')).value, 2)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@pytest.mark.parametrize('operation', EXTREMA)
|
|
67
|
+
def test_numpy_nan_extremum_warning(operation: Callable[..., Any]) -> None:
|
|
68
|
+
samples = Quantity(np.full((2, 3), np.nan), 'm')
|
|
69
|
+
with pytest.warns(RuntimeWarning, match='All-NaN'):
|
|
70
|
+
result = operation(samples, axis=1)
|
|
71
|
+
assert result.unit == si.m
|
|
72
|
+
np.testing.assert_array_equal(result.value, np.nan)
|
|
73
|
+
target = Quantity(np.full(2, -123.0), 'cm')
|
|
74
|
+
with warnings.catch_warnings():
|
|
75
|
+
warnings.simplefilter('error', RuntimeWarning)
|
|
76
|
+
with pytest.raises(RuntimeWarning, match='All-NaN'):
|
|
77
|
+
operation(samples, axis=1, out=target)
|
|
78
|
+
np.testing.assert_array_equal(target.value, -123)
|
|
79
|
+
with pytest.raises(ValueError, match='zero-size'):
|
|
80
|
+
operation(Quantity(np.empty((2, 0)), 'm'), axis=1, out=target)
|
|
81
|
+
np.testing.assert_array_equal(target.value, -123)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@pytest.mark.parametrize('operation', [*EXTREMA, *INDICES])
|
|
85
|
+
@pytest.mark.parametrize(
|
|
86
|
+
'point_type,coordinate,target_coordinate',
|
|
87
|
+
[
|
|
88
|
+
(AffineQuantity, si.degC, imperial.degF),
|
|
89
|
+
(LogQuantity, si.dBm, si.dBW),
|
|
90
|
+
],
|
|
91
|
+
)
|
|
92
|
+
def test_numpy_nan_selection_points(
|
|
93
|
+
operation: Callable[..., Any], point_type: type, coordinate: Any, target_coordinate: Any
|
|
94
|
+
) -> None:
|
|
95
|
+
samples = point_type(np.array([[np.nan, 0, 20], [30, 50, np.nan]]), coordinate)
|
|
96
|
+
expected = operation(samples.value, axis=1)
|
|
97
|
+
if operation in INDICES:
|
|
98
|
+
target = np.full(2, -123, dtype=np.intp)
|
|
99
|
+
result = operation(samples, axis=1)
|
|
100
|
+
np.testing.assert_array_equal(result, expected)
|
|
101
|
+
assert operation(samples, 1, target) is target
|
|
102
|
+
np.testing.assert_array_equal(target, expected)
|
|
103
|
+
else:
|
|
104
|
+
result = operation(samples, axis=1)
|
|
105
|
+
assert type(result) is point_type and result.unit is coordinate
|
|
106
|
+
np.testing.assert_array_equal(result.value, expected)
|
|
107
|
+
target = point_type(np.empty(2), target_coordinate)
|
|
108
|
+
assert operation(samples, axis=1, out=target) is target
|
|
109
|
+
np.testing.assert_allclose(target.value, result.value_in(target_coordinate))
|
|
110
|
+
initial = point_type(10, coordinate).to(target_coordinate)
|
|
111
|
+
bounded = operation(samples, axis=1, initial=initial, where=True)
|
|
112
|
+
np.testing.assert_allclose(bounded.value, operation(samples.value, axis=1, initial=10))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.mark.parametrize('operation', INDICES)
|
|
116
|
+
@pytest.mark.parametrize('axis', [None, 0, -1])
|
|
117
|
+
def test_numpy_nan_index_axes(operation: Callable[..., Any], axis: int | None) -> None:
|
|
118
|
+
values = np.array([[np.nan, 2, np.inf, 2], [-np.inf, 5, 5, np.nan]])[:, ::-1]
|
|
119
|
+
samples = Quantity(values, 'km/m')
|
|
120
|
+
for keepdims in (False, True):
|
|
121
|
+
expected = operation(values, axis=axis, keepdims=keepdims)
|
|
122
|
+
target = np.full(np.shape(expected), -123, dtype=np.intp)
|
|
123
|
+
assert operation(samples, axis, target, keepdims=keepdims) is target
|
|
124
|
+
np.testing.assert_array_equal(target, expected)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@pytest.mark.parametrize('operation', INDICES)
|
|
128
|
+
@pytest.mark.parametrize('values', [np.full((2, 3), np.nan), np.empty((2, 0))])
|
|
129
|
+
def test_numpy_nan_index_invalid_slice(operation: Callable[..., Any], values: Any) -> None:
|
|
130
|
+
target = np.full(2, -123, dtype=np.intp)
|
|
131
|
+
with pytest.raises(ValueError):
|
|
132
|
+
operation(Quantity(values, 'm'), axis=1, out=target)
|
|
133
|
+
np.testing.assert_array_equal(target, -123)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@pytest.mark.parametrize('operation', EXTREMA)
|
|
137
|
+
@pytest.mark.parametrize('initial', [0, Quantity(0, 's'), 0 @ si.degC])
|
|
138
|
+
def test_numpy_nan_extremum_invalid_initial(operation: Callable[..., Any], initial: Any) -> None:
|
|
139
|
+
target = Quantity(np.array(-123.0), 'm')
|
|
140
|
+
with pytest.raises((TypeError, DimensionError)):
|
|
141
|
+
operation(Quantity(np.array([1.0, np.nan]), 'm'), initial=initial, out=target)
|
|
142
|
+
assert target.value == -123
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@pytest.mark.parametrize('operation', EXTREMA)
|
|
146
|
+
def test_numpy_nan_extremum_invalid_convention(operation: Callable[..., Any]) -> None:
|
|
147
|
+
samples = LogQuantity(np.array([np.nan, 10.0]), si.dBW)
|
|
148
|
+
target = LogQuantity(np.array(-123.0), si.dBm)
|
|
149
|
+
with pytest.raises(DimensionError, match='same convention'):
|
|
150
|
+
operation(samples, initial=0 @ si.dB(si.W, kind='amplitude'), out=target)
|
|
151
|
+
assert target.value == -123
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@pytest.mark.parametrize('operation', [*EXTREMA, *INDICES, np.nansum, np.nanprod])
|
|
155
|
+
@pytest.mark.parametrize('invalid', ['shape', 'dtype', 'readonly', 'wrapper'])
|
|
156
|
+
def test_numpy_nan_reduction_invalid_out(operation: Callable[..., Any], invalid: str) -> None:
|
|
157
|
+
samples = Quantity(np.array([[np.nan, 1, 3], [4, np.nan, 6]]), 'm')
|
|
158
|
+
indices = operation in INDICES
|
|
159
|
+
dtype = np.intp if indices else np.float64
|
|
160
|
+
unit = si.m**2 if operation is np.nanprod else si.m
|
|
161
|
+
storage = np.full(3 if invalid == 'shape' else 2, -123, dtype=dtype)
|
|
162
|
+
if invalid == 'dtype':
|
|
163
|
+
storage = storage.astype(float if indices else int)
|
|
164
|
+
if invalid == 'readonly':
|
|
165
|
+
storage.flags.writeable = False
|
|
166
|
+
if (invalid == 'wrapper') == indices:
|
|
167
|
+
target = Quantity(storage, unit)
|
|
168
|
+
else:
|
|
169
|
+
target = storage
|
|
170
|
+
with pytest.raises((TypeError, ValueError)):
|
|
171
|
+
operation(samples, axis=1, out=target)
|
|
172
|
+
np.testing.assert_array_equal(storage, -123)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@pytest.mark.parametrize('operation', [*EXTREMA, *INDICES, np.nansum, np.nanprod])
|
|
176
|
+
def test_numpy_nan_reduction_out_alias(operation: Callable[..., Any]) -> None:
|
|
177
|
+
dtype = np.intp if operation in INDICES else np.float64
|
|
178
|
+
storage = np.array([[1, 3, 2], [4, 6, 5]], dtype=dtype)
|
|
179
|
+
if operation not in INDICES:
|
|
180
|
+
storage[:, -1] = np.nan
|
|
181
|
+
saved = storage.copy()
|
|
182
|
+
samples = Quantity(storage, Unit('m', scale=Factor(2)))
|
|
183
|
+
unit = samples.unit**2 if operation is np.nanprod else samples.unit
|
|
184
|
+
target = storage[:, 0] if operation in INDICES else Quantity(storage[:, 0], unit)
|
|
185
|
+
expected = operation(saved, axis=1)
|
|
186
|
+
assert operation(samples, axis=1, out=target) is target
|
|
187
|
+
saved[:, 0] = expected
|
|
188
|
+
np.testing.assert_array_equal(storage, saved)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_numpy_nansum_mask_and_empty() -> None:
|
|
192
|
+
samples = Quantity(np.array([[1.0, np.nan, 3], [np.nan, 5, 7]]), 'm')
|
|
193
|
+
mask = np.array([[True, True, False], [False, True, True]])
|
|
194
|
+
target = Quantity(np.empty(2), 'cm')
|
|
195
|
+
assert np.nansum(samples, 1, np.float32, target, False, Quantity(50, 'cm'), mask) is target
|
|
196
|
+
np.testing.assert_array_equal(target.value, [150, 1250])
|
|
197
|
+
for values in (np.full((2, 3), np.nan), np.empty((2, 0))):
|
|
198
|
+
result = np.nansum(Quantity(values, 'm'), axis=1)
|
|
199
|
+
assert result.unit == si.m
|
|
200
|
+
np.testing.assert_array_equal(result.value, 0)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@pytest.mark.parametrize('axis,count', [(None, 4), (0, 1), (-1, 2), ((0, 1), 4)])
|
|
204
|
+
def test_numpy_nanprod_counts(axis: int | tuple[int, ...] | None, count: int) -> None:
|
|
205
|
+
values = np.array([[2.0, np.nan, 3, np.nan], [np.nan, 4, np.nan, 5]])
|
|
206
|
+
samples = Quantity(values, Unit('cm', scale=Factor(2)))
|
|
207
|
+
for keepdims in (False, True):
|
|
208
|
+
result = np.nanprod(samples, axis=axis, keepdims=keepdims, initial=Quantity(2, 's'))
|
|
209
|
+
assert result.unit == samples.unit**count * si.s
|
|
210
|
+
np.testing.assert_array_equal(
|
|
211
|
+
result.value, np.nanprod(values, axis=axis, keepdims=keepdims, initial=2)
|
|
212
|
+
)
|
|
213
|
+
target = Quantity(np.empty(2), 'm2 s')
|
|
214
|
+
assert np.nanprod(samples, 1, None, target, False, Quantity(2, 's'), True) is target
|
|
215
|
+
np.testing.assert_allclose(target.value, np.nanprod(values, axis=1) * 0.02**2 * 2)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def test_numpy_nanprod_mask() -> None:
|
|
219
|
+
samples = Quantity(np.array([[2.0, np.nan, 3], [np.nan, 4, 5]]), 'm')
|
|
220
|
+
mask = np.array([True, True, False])
|
|
221
|
+
result = np.nanprod(samples, axis=1, where=mask)
|
|
222
|
+
assert result.unit == si.m
|
|
223
|
+
np.testing.assert_array_equal(result.value, [2, 4])
|
|
224
|
+
samples = Quantity(np.array([[0.0, np.nan, 3], [np.inf, 4, np.nan]]), 'm')
|
|
225
|
+
assert np.nanprod(samples, axis=1).unit == si.m**2 # Zero and infinity still count.
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
@pytest.mark.parametrize(
|
|
229
|
+
'values,axis',
|
|
230
|
+
[
|
|
231
|
+
(np.full((2, 3), np.nan), 1),
|
|
232
|
+
(np.array(np.nan), None),
|
|
233
|
+
(np.array(np.nan), 0),
|
|
234
|
+
(np.array(np.nan), -1),
|
|
235
|
+
(np.array(np.nan), ()),
|
|
236
|
+
(np.empty((2, 0)), 1),
|
|
237
|
+
],
|
|
238
|
+
)
|
|
239
|
+
def test_numpy_nanprod_identity(values: Any, axis: int | tuple[int, ...] | None) -> None:
|
|
240
|
+
result = np.nanprod(Quantity(values, 'm'), axis=axis, initial=Quantity(2, 's'))
|
|
241
|
+
assert result.unit == si.s
|
|
242
|
+
np.testing.assert_array_equal(result.value, 2)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
@pytest.mark.parametrize('unit', ['m', 'dB'])
|
|
246
|
+
@pytest.mark.parametrize('axis', [1, ()])
|
|
247
|
+
def test_numpy_nanprod_invalid_counts(unit: str, axis: int | tuple[int, ...]) -> None:
|
|
248
|
+
samples = Quantity(np.array([[2.0, np.nan, 3], [4, 5, 6]]), unit)
|
|
249
|
+
target = Quantity(np.full((2,) if axis == 1 else (2, 3), -123.0), unit)
|
|
250
|
+
with pytest.raises(DimensionError, match='equal element counts'):
|
|
251
|
+
np.nanprod(samples, axis=axis, initial=0, out=target)
|
|
252
|
+
np.testing.assert_array_equal(target.value, -123)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
@pytest.mark.parametrize('unit', ['km/m', 'deg', ''])
|
|
256
|
+
def test_numpy_nanprod_dimensionless(unit: str) -> None:
|
|
257
|
+
samples = Quantity(np.array([[2.0, np.nan, 3], [4, 5, 6]]), unit)
|
|
258
|
+
result = np.nanprod(samples, axis=1, initial=Quantity(2, 's'))
|
|
259
|
+
assert result.unit == si.s
|
|
260
|
+
np.testing.assert_allclose(result.value, np.nanprod(samples.value_in(''), axis=1, initial=2))
|