unxt-hypothesis 0.1.dev410__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.
- unxt_hypothesis/__init__.py +7 -0
- unxt_hypothesis/_src/__init__.py +3 -0
- unxt_hypothesis/_src/quantities.py +270 -0
- unxt_hypothesis/_src/units.py +232 -0
- unxt_hypothesis/_src/unitsystems.py +71 -0
- unxt_hypothesis/_version.py +34 -0
- unxt_hypothesis-0.1.dev410.dist-info/METADATA +166 -0
- unxt_hypothesis-0.1.dev410.dist-info/RECORD +9 -0
- unxt_hypothesis-0.1.dev410.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Hypothesis strategies for unxt."""
|
|
2
|
+
|
|
3
|
+
__all__ = ("derived_units", "units", "unitsystems", "quantities", "wrap_to")
|
|
4
|
+
|
|
5
|
+
from unxt_hypothesis._src.quantities import quantities, wrap_to
|
|
6
|
+
from unxt_hypothesis._src.units import derived_units, units
|
|
7
|
+
from unxt_hypothesis._src.unitsystems import unitsystems
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""Hypothesis strategies for Quantity objects."""
|
|
2
|
+
|
|
3
|
+
__all__ = ["quantities", "wrap_to"]
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import jax.numpy as jnp
|
|
8
|
+
import numpy as np
|
|
9
|
+
from hypothesis import strategies as st
|
|
10
|
+
from hypothesis.extra.array_api import make_strategies_namespace
|
|
11
|
+
|
|
12
|
+
import unxt as u
|
|
13
|
+
from .units import units
|
|
14
|
+
|
|
15
|
+
# Create array API strategies namespace for JAX
|
|
16
|
+
xps = make_strategies_namespace(jnp)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
SI_UNITS_STRAT = st.sampled_from(tuple(u.unit(x) for x in u.unitsystems.si))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@st.composite
|
|
23
|
+
def quantities(
|
|
24
|
+
draw: st.DrawFn,
|
|
25
|
+
/,
|
|
26
|
+
unit: str
|
|
27
|
+
| u.AbstractUnit
|
|
28
|
+
| st.SearchStrategy[u.AbstractUnit | u.AbstractDimension]
|
|
29
|
+
| u.AbstractDimension = SI_UNITS_STRAT,
|
|
30
|
+
*,
|
|
31
|
+
quantity_cls: type[u.AbstractQuantity] = u.Quantity,
|
|
32
|
+
dtype: Any | st.SearchStrategy[np.dtype] = jnp.float32,
|
|
33
|
+
shape: int | tuple[int, ...] | st.SearchStrategy[tuple[int, ...]] | None = None,
|
|
34
|
+
elements: st.SearchStrategy[float] | None = None,
|
|
35
|
+
unique: bool = False,
|
|
36
|
+
**kwargs: Any,
|
|
37
|
+
) -> u.AbstractQuantity:
|
|
38
|
+
"""Generate hypothesis strategy for unxt Quantity objects.
|
|
39
|
+
|
|
40
|
+
This strategy combines JAX array generation with unit specifications to
|
|
41
|
+
create valid Quantity objects for property-based testing.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
draw : st.DrawFn
|
|
46
|
+
Hypothesis draw function (automatically provided by @st.composite).
|
|
47
|
+
unit : str | st.SearchStrategy[unxt.AbstractUnit]
|
|
48
|
+
Unit specification for the Quantity. Can be: - str: Fixed unit (e.g.,
|
|
49
|
+
"kpc", "km/s") - unxt.AbstractUnit: Fixed unit object -
|
|
50
|
+
unxt.AbstractDimension: Dimension to derive unit from (uses
|
|
51
|
+
`unxt_hypothesis.units` strategy) - SearchStrategy: Strategy that
|
|
52
|
+
generates units (e.g., from `units()`) or dimensions. The default
|
|
53
|
+
strategy samples from SI base units.
|
|
54
|
+
quantity_cls : type[unxt.AbstractQuantity], optional
|
|
55
|
+
The target quantity class to convert to. Default is unxt.Quantity. Can
|
|
56
|
+
be any AbstractQuantity subclass like Quantity or Angle.
|
|
57
|
+
dtype : Any, optional
|
|
58
|
+
NumPy/JAX dtype for the underlying array. Default is jnp.float32. Can
|
|
59
|
+
also be a SearchStrategy that generates dtypes.
|
|
60
|
+
shape : int | tuple[int, ...] | st.SearchStrategy[tuple[int, ...]] | None
|
|
61
|
+
Shape of the array. Can be: - int: 1D array of that length - tuple:
|
|
62
|
+
fixed shape - SearchStrategy: strategy that generates shapes - None:
|
|
63
|
+
scalar (shape ())
|
|
64
|
+
elements : st.SearchStrategy[float] | None, optional
|
|
65
|
+
Strategy for generating array elements. If None, uses finite floats.
|
|
66
|
+
unique : bool, optional
|
|
67
|
+
Whether array elements should be unique. Default is False.
|
|
68
|
+
**kwargs : Any
|
|
69
|
+
Additional keyword arguments (currently unused, reserved for future
|
|
70
|
+
use).
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
u.Quantity
|
|
75
|
+
A Quantity object with the specified unit and array properties.
|
|
76
|
+
|
|
77
|
+
Examples
|
|
78
|
+
--------
|
|
79
|
+
Basic usage with fixed unit and shape:
|
|
80
|
+
|
|
81
|
+
>>> from hypothesis import given, strategies as st
|
|
82
|
+
>>> import jax.numpy as jnp
|
|
83
|
+
>>> import unxt as u
|
|
84
|
+
>>> import unxt_hypothesis as ust
|
|
85
|
+
|
|
86
|
+
>>> @given(q=ust.quantities("kpc", shape=3))
|
|
87
|
+
... def test_position(q):
|
|
88
|
+
... assert q.unit == u.unit("kpc")
|
|
89
|
+
... assert q.shape == (3,)
|
|
90
|
+
|
|
91
|
+
>>> @given(q=ust.quantities("km/s", shape=()))
|
|
92
|
+
... def test_scalar_velocity(q):
|
|
93
|
+
... assert q.unit == u.unit("km/s")
|
|
94
|
+
... assert q.shape == ()
|
|
95
|
+
|
|
96
|
+
Using a unit strategy to vary units across test runs:
|
|
97
|
+
|
|
98
|
+
>>> @given(q=ust.quantities(ust.units("length"), shape=3))
|
|
99
|
+
... def test_length_with_varying_units(q):
|
|
100
|
+
... # Unit will vary across test runs
|
|
101
|
+
... assert u.dimension_of(q) == u.dimension("length")
|
|
102
|
+
... assert q.shape == (3,)
|
|
103
|
+
|
|
104
|
+
As a convenience, instead of nesting strategies with ``ust.units``, you can
|
|
105
|
+
directly pass the dimension object (not a string) to ``ust.quantities``:
|
|
106
|
+
|
|
107
|
+
>>> @given(q=ust.quantities(u.dimension("velocity"), shape=3))
|
|
108
|
+
... def test_velocity_from_dimension(q):
|
|
109
|
+
... # Will generate different velocity units (m/s, km/s, etc.)
|
|
110
|
+
... assert u.dimension_of(q) == u.dimension("velocity")
|
|
111
|
+
... assert q.shape == (3,)
|
|
112
|
+
|
|
113
|
+
This (and ``ust.units``) support a strategy for dimensions:
|
|
114
|
+
|
|
115
|
+
>>> dim_strat = st.sampled_from([u.dimension("length"), u.dimension("mass")])
|
|
116
|
+
>>> @given(q=ust.quantities(dim_strat, shape=()))
|
|
117
|
+
... def test_mixed_dimensions(q):
|
|
118
|
+
... # Will generate either length or mass quantities
|
|
119
|
+
... dim = u.dimension_of(q)
|
|
120
|
+
... assert dim in (u.dimension("length"), u.dimension("mass"))
|
|
121
|
+
|
|
122
|
+
Using dtype as a strategy:
|
|
123
|
+
|
|
124
|
+
>>> dtype_strat = st.sampled_from([jnp.float32, jnp.float64])
|
|
125
|
+
>>> @given(q=ust.quantities("m", dtype=dtype_strat))
|
|
126
|
+
... def test_varying_dtype(q):
|
|
127
|
+
... # dtype will vary between float32 and float64
|
|
128
|
+
... assert q.dtype in (jnp.float32, jnp.float64)
|
|
129
|
+
|
|
130
|
+
Using elements to constrain value ranges for distances (positive values):
|
|
131
|
+
|
|
132
|
+
>>> @given(
|
|
133
|
+
... q=ust.quantities(
|
|
134
|
+
... "kpc", shape=3, elements=st.floats(min_value=0, max_value=100, width=32)
|
|
135
|
+
... )
|
|
136
|
+
... )
|
|
137
|
+
... def test_positive_distance(q):
|
|
138
|
+
... # All elements will be positive (suitable for distances)
|
|
139
|
+
... assert jnp.all(q.value >= 0)
|
|
140
|
+
... assert jnp.all(q.value <= 100)
|
|
141
|
+
|
|
142
|
+
Using elements for longitude angles (0 to 360 degrees):
|
|
143
|
+
|
|
144
|
+
>>> @given(
|
|
145
|
+
... q=ust.quantities(
|
|
146
|
+
... "deg",
|
|
147
|
+
... shape=(),
|
|
148
|
+
... elements=st.floats(min_value=0, max_value=360, width=32),
|
|
149
|
+
... )
|
|
150
|
+
... )
|
|
151
|
+
... def test_longitude_range(q):
|
|
152
|
+
... # Longitude in [0, 360] degrees
|
|
153
|
+
... assert 0 <= q.value <= 360
|
|
154
|
+
|
|
155
|
+
Using quantity_cls to generate Angle objects:
|
|
156
|
+
|
|
157
|
+
>>> @given(angle=ust.quantities("rad", quantity_cls=u.Angle, shape=3))
|
|
158
|
+
... def test_angle_generation(angle):
|
|
159
|
+
... # Creates Angle instances instead of Quantity
|
|
160
|
+
... assert isinstance(angle, u.Angle)
|
|
161
|
+
... assert angle.unit == u.unit("rad")
|
|
162
|
+
... assert angle.shape == (3,)
|
|
163
|
+
|
|
164
|
+
"""
|
|
165
|
+
# Handle unit specification - draw from strategy if needed
|
|
166
|
+
if isinstance(unit, st.SearchStrategy):
|
|
167
|
+
unit = draw(unit)
|
|
168
|
+
unit_obj = (
|
|
169
|
+
draw(units(unit)) if isinstance(unit, u.AbstractDimension) else u.unit(unit)
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
# Handle shape specification
|
|
173
|
+
if shape is None:
|
|
174
|
+
array_shape = ()
|
|
175
|
+
elif isinstance(shape, int):
|
|
176
|
+
array_shape = (shape,)
|
|
177
|
+
elif isinstance(shape, tuple):
|
|
178
|
+
array_shape = shape
|
|
179
|
+
else:
|
|
180
|
+
# It's a strategy, draw from it
|
|
181
|
+
array_shape = draw(shape)
|
|
182
|
+
|
|
183
|
+
# DType handling
|
|
184
|
+
dtype = draw(dtype) if isinstance(dtype, st.SearchStrategy) else dtype
|
|
185
|
+
|
|
186
|
+
# Default elements strategy if not provided
|
|
187
|
+
if elements is None:
|
|
188
|
+
# Use xps.from_dtype to get the appropriate strategy for the dtype
|
|
189
|
+
elements = xps.from_dtype(dtype, allow_nan=False, allow_infinity=False)
|
|
190
|
+
|
|
191
|
+
# Generate array using JAX array-api strategy
|
|
192
|
+
# For arrays, use the JAX array API strategies
|
|
193
|
+
values = draw(
|
|
194
|
+
xps.arrays(dtype=dtype, shape=array_shape, elements=elements, unique=unique)
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
# Create Quantity with the specified unit
|
|
198
|
+
return quantity_cls(values, unit_obj, **kwargs)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@st.composite
|
|
202
|
+
def wrap_to(
|
|
203
|
+
draw: st.DrawFn,
|
|
204
|
+
quantity: st.SearchStrategy[u.AbstractQuantity],
|
|
205
|
+
/,
|
|
206
|
+
min: u.AbstractQuantity | st.SearchStrategy[u.AbstractQuantity],
|
|
207
|
+
max: u.AbstractQuantity | st.SearchStrategy[u.AbstractQuantity],
|
|
208
|
+
) -> u.AbstractQuantity:
|
|
209
|
+
"""Generate hypothesis strategy for wrapped quantities.
|
|
210
|
+
|
|
211
|
+
This strategy takes a quantity strategy and wraps the generated values
|
|
212
|
+
to the specified [min, max) range using the wrap_to function.
|
|
213
|
+
|
|
214
|
+
Parameters
|
|
215
|
+
----------
|
|
216
|
+
draw : st.DrawFn
|
|
217
|
+
Hypothesis draw function (automatically provided by @st.composite).
|
|
218
|
+
quantity : st.SearchStrategy[unxt.AbstractQuantity]
|
|
219
|
+
Strategy that generates the base quantity to wrap.
|
|
220
|
+
min : unxt.AbstractQuantity | st.SearchStrategy[unxt.AbstractQuantity]
|
|
221
|
+
Minimum value of the wrapping range (inclusive).
|
|
222
|
+
max : unxt.AbstractQuantity | st.SearchStrategy[unxt.AbstractQuantity]
|
|
223
|
+
Maximum value of the wrapping range (exclusive).
|
|
224
|
+
|
|
225
|
+
Returns
|
|
226
|
+
-------
|
|
227
|
+
unxt.AbstractQuantity
|
|
228
|
+
The wrapped quantity in the range [min, max).
|
|
229
|
+
|
|
230
|
+
Examples
|
|
231
|
+
--------
|
|
232
|
+
Wrap angles to 0-360 degree range:
|
|
233
|
+
|
|
234
|
+
>>> from hypothesis import given
|
|
235
|
+
>>> import hypothesis.strategies as st
|
|
236
|
+
>>> import unxt as u
|
|
237
|
+
>>> import unxt_hypothesis as ust
|
|
238
|
+
|
|
239
|
+
>>> @given(
|
|
240
|
+
... angle=ust.wrap_to(
|
|
241
|
+
... ust.quantities("deg", quantity_cls=u.Angle),
|
|
242
|
+
... min=u.Quantity(0, "deg"),
|
|
243
|
+
... max=u.Quantity(360, "deg"),
|
|
244
|
+
... )
|
|
245
|
+
... )
|
|
246
|
+
... def test_wrapped_angle(angle):
|
|
247
|
+
... assert 0 <= angle.value <= 360
|
|
248
|
+
|
|
249
|
+
Wrap angles with dynamic min/max:
|
|
250
|
+
|
|
251
|
+
>>> @given(
|
|
252
|
+
... angle=ust.wrap_to(
|
|
253
|
+
... ust.quantities("rad", quantity_cls=u.Angle),
|
|
254
|
+
... min=st.just(u.Quantity(0, "rad")),
|
|
255
|
+
... max=st.just(u.Quantity(6.28318530718, "rad")),
|
|
256
|
+
... )
|
|
257
|
+
... )
|
|
258
|
+
... def test_wrapped_angle_rad(angle):
|
|
259
|
+
... assert 0 <= angle.value <= 6.28318530718
|
|
260
|
+
|
|
261
|
+
"""
|
|
262
|
+
# Draw the base quantity
|
|
263
|
+
q = draw(quantity)
|
|
264
|
+
|
|
265
|
+
# Draw min/max if they're strategies
|
|
266
|
+
min_val = draw(min) if isinstance(min, st.SearchStrategy) else min
|
|
267
|
+
max_val = draw(max) if isinstance(max, st.SearchStrategy) else max
|
|
268
|
+
|
|
269
|
+
# Wrap the quantity to the specified range
|
|
270
|
+
return u.quantity.wrap_to(q, min_val, max_val)
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"""Unit strategies."""
|
|
2
|
+
|
|
3
|
+
__all__ = ("derived_units", "units")
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import astropy.units as apyu
|
|
8
|
+
from hypothesis import strategies as st
|
|
9
|
+
|
|
10
|
+
import unxt as u
|
|
11
|
+
|
|
12
|
+
SI_DIMS_STRAT = st.sampled_from(tuple(u.dimension_of(x) for x in u.unitsystems.si))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@st.composite
|
|
16
|
+
def derived_units( # pylint: disable=unreachable
|
|
17
|
+
draw: st.DrawFn,
|
|
18
|
+
base: str | u.AbstractUnit | st.SearchStrategy[u.AbstractUnit],
|
|
19
|
+
/,
|
|
20
|
+
*,
|
|
21
|
+
integer_powers: bool = True,
|
|
22
|
+
max_complexity: int = 3,
|
|
23
|
+
) -> u.AbstractUnit:
|
|
24
|
+
"""Generate hypothesis strategy for units from a given base unit.
|
|
25
|
+
|
|
26
|
+
This strategy generates units that are dimensionally equivalent to the input
|
|
27
|
+
base unit by combining the base unit's base units and discovered composed
|
|
28
|
+
forms.
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
draw : st.DrawFn
|
|
33
|
+
Hypothesis draw function (automatically provided by @st.composite).
|
|
34
|
+
base : str | u.AbstractUnit | st.SearchStrategy[u.AbstractUnit]
|
|
35
|
+
Base unit (e.g., "m", "s", "kg") or a `hypothesis` strategy that
|
|
36
|
+
generates such units.
|
|
37
|
+
integer_powers : bool, optional
|
|
38
|
+
If True, only generate units with integer powers of base units. Default
|
|
39
|
+
is True.
|
|
40
|
+
max_complexity : int, optional
|
|
41
|
+
Maximum number of additional base unit factors to combine. Higher
|
|
42
|
+
values create more complex compound units. Default is 3.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
-------
|
|
46
|
+
`unxt.AbstractUnit`
|
|
47
|
+
A unit with the same physical dimension as the input.
|
|
48
|
+
|
|
49
|
+
Examples
|
|
50
|
+
--------
|
|
51
|
+
>>> import hypothesis.strategies as st
|
|
52
|
+
>>> from hypothesis import given
|
|
53
|
+
>>> import unxt_hypothesis as ust
|
|
54
|
+
>>> import unxt as u
|
|
55
|
+
|
|
56
|
+
Generate units derived from a base unit:
|
|
57
|
+
|
|
58
|
+
>>> @given(unit=ust.derived_units("m"))
|
|
59
|
+
... def test_length_units(unit):
|
|
60
|
+
... assert u.dimension_of(unit) == u.dimension("length")
|
|
61
|
+
|
|
62
|
+
Generate units from a strategy:
|
|
63
|
+
|
|
64
|
+
>>> @given(unit=ust.derived_units(st.sampled_from(["km", "m/s^2"])))
|
|
65
|
+
... def test_velocity_derived(unit):
|
|
66
|
+
... assert u.dimension_of(unit) in (
|
|
67
|
+
... u.dimension("velocity"),
|
|
68
|
+
... u.dimension("acceleration"),
|
|
69
|
+
... )
|
|
70
|
+
|
|
71
|
+
Control complexity of generated units:
|
|
72
|
+
|
|
73
|
+
>>> @given(unit=ust.derived_units("kg", max_complexity=1))
|
|
74
|
+
... def test_simple_mass_units(unit):
|
|
75
|
+
... assert u.dimension_of(unit) == u.dimension("mass")
|
|
76
|
+
|
|
77
|
+
Allow non-integer powers:
|
|
78
|
+
|
|
79
|
+
>>> @given(unit=ust.derived_units("m", integer_powers=False))
|
|
80
|
+
... def test_fractional_powers(unit):
|
|
81
|
+
... dim = u.dimension_of(unit)
|
|
82
|
+
... assert dim == u.dimension("length")
|
|
83
|
+
|
|
84
|
+
"""
|
|
85
|
+
# Convert base to a unit if it's a string or draw from strategy
|
|
86
|
+
base_unit = u.unit(draw(base) if isinstance(base, st.SearchStrategy) else base)
|
|
87
|
+
|
|
88
|
+
# Collect all possible unit forms
|
|
89
|
+
# 1. Start with the base unit
|
|
90
|
+
candidates = [base_unit]
|
|
91
|
+
|
|
92
|
+
# 2. Add composed forms (limited to first 20 to avoid huge search space)
|
|
93
|
+
composed = list(base_unit.compose())[:20]
|
|
94
|
+
candidates.extend(composed)
|
|
95
|
+
|
|
96
|
+
# 3. Optionally create compound units by combining cancelling factors
|
|
97
|
+
if max_complexity > 0:
|
|
98
|
+
# Get SI base decomposition
|
|
99
|
+
decomposed = base_unit.decompose()
|
|
100
|
+
si_bases = list(decomposed.bases)
|
|
101
|
+
list(decomposed.powers)
|
|
102
|
+
|
|
103
|
+
# Get a pool of other SI base units to use as cancelling factors
|
|
104
|
+
all_si_bases = [
|
|
105
|
+
apyu.Unit(x) for x in ("m", "s", "kg", "A", "K", "mol", "cd", "rad")
|
|
106
|
+
]
|
|
107
|
+
|
|
108
|
+
# Find SI bases not used in this dimension (for pure cancellation)
|
|
109
|
+
# and those that are used (for modification)
|
|
110
|
+
available_bases = [b for b in all_si_bases if b not in si_bases]
|
|
111
|
+
|
|
112
|
+
if available_bases:
|
|
113
|
+
# Generate a few compound units with cancelling factors
|
|
114
|
+
num_extra = draw(st.integers(min_value=0, max_value=max_complexity))
|
|
115
|
+
|
|
116
|
+
if num_extra > 0:
|
|
117
|
+
# Choose random bases to combine
|
|
118
|
+
extra_bases = draw(
|
|
119
|
+
st.lists(
|
|
120
|
+
st.sampled_from(available_bases),
|
|
121
|
+
min_size=num_extra,
|
|
122
|
+
max_size=num_extra,
|
|
123
|
+
unique=True,
|
|
124
|
+
)
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# For each base, generate a power and its inverse
|
|
128
|
+
compound = base_unit
|
|
129
|
+
for extra_base in extra_bases:
|
|
130
|
+
if integer_powers:
|
|
131
|
+
power = draw(st.integers(min_value=1, max_value=3))
|
|
132
|
+
else:
|
|
133
|
+
power = draw(st.floats(min_value=0.5, max_value=3.0))
|
|
134
|
+
|
|
135
|
+
# Multiply and divide to cancel dimensions
|
|
136
|
+
compound = compound * (extra_base**power) / (extra_base**power)
|
|
137
|
+
|
|
138
|
+
candidates.append(compound)
|
|
139
|
+
|
|
140
|
+
# Choose one of the candidate units
|
|
141
|
+
return draw(st.sampled_from(candidates))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@st.composite
|
|
145
|
+
def units(
|
|
146
|
+
draw: st.DrawFn,
|
|
147
|
+
dimension: str
|
|
148
|
+
| u.AbstractDimension
|
|
149
|
+
| st.SearchStrategy[u.AbstractDimension] = SI_DIMS_STRAT,
|
|
150
|
+
/,
|
|
151
|
+
**kwargs: Any,
|
|
152
|
+
) -> u.AbstractUnit:
|
|
153
|
+
"""Generate hypothesis strategy for units with a given physical dimension.
|
|
154
|
+
|
|
155
|
+
This strategy generates units that are dimensionally equivalent to the input
|
|
156
|
+
dimension by combining the dimension's base units and discovered composed
|
|
157
|
+
forms.
|
|
158
|
+
|
|
159
|
+
Parameters
|
|
160
|
+
----------
|
|
161
|
+
draw : st.DrawFn
|
|
162
|
+
Hypothesis draw function (automatically provided by @st.composite).
|
|
163
|
+
dimension : str | apyu.PhysicalType
|
|
164
|
+
Physical dimension (e.g., "velocity", "length") or a `hypothesis`
|
|
165
|
+
strategy that generates such dimensions. Default is a strategy sampling
|
|
166
|
+
from SI base dimensions.
|
|
167
|
+
**kwargs : Any
|
|
168
|
+
Additional keyword arguments passed to `derived_units()`.
|
|
169
|
+
For example:
|
|
170
|
+
|
|
171
|
+
- integer_powers : bool
|
|
172
|
+
If True, only generate units with integer powers of base units. Default
|
|
173
|
+
is True.
|
|
174
|
+
- max_complexity : int
|
|
175
|
+
Maximum number of additional base unit factors to combine. Higher
|
|
176
|
+
values create more complex compound units. Default is 3.
|
|
177
|
+
|
|
178
|
+
Returns
|
|
179
|
+
-------
|
|
180
|
+
unxt.AbstractUnit
|
|
181
|
+
A unit with the same physical dimension as the input.
|
|
182
|
+
|
|
183
|
+
Examples
|
|
184
|
+
--------
|
|
185
|
+
>>> import hypothesis.strategies as st
|
|
186
|
+
>>> from hypothesis import given
|
|
187
|
+
>>> import unxt_hypothesis as ust
|
|
188
|
+
>>> import unxt as u
|
|
189
|
+
|
|
190
|
+
>>> @given(unit=ust.units("velocity"))
|
|
191
|
+
... def test_velocity_units(unit):
|
|
192
|
+
... assert u.dimension_of(unit) == u.dimension("velocity")
|
|
193
|
+
|
|
194
|
+
>>> @given(unit=ust.units("length", integer_powers=False))
|
|
195
|
+
... def test_length_units(unit):
|
|
196
|
+
... dim = u.dimension_of(unit)
|
|
197
|
+
... assert dim == u.dimension("length")
|
|
198
|
+
|
|
199
|
+
The dimension argument can also be a strategy:
|
|
200
|
+
|
|
201
|
+
>>> from hypothesis import strategies as st
|
|
202
|
+
>>> @given(unit=ust.units(st.sampled_from(["length", "time", "mass"])))
|
|
203
|
+
... def test_multiple_dimensions(unit):
|
|
204
|
+
... dim = u.dimension_of(unit)
|
|
205
|
+
... assert dim in [
|
|
206
|
+
... u.dimension("length"),
|
|
207
|
+
... u.dimension("time"),
|
|
208
|
+
... u.dimension("mass"),
|
|
209
|
+
... ]
|
|
210
|
+
|
|
211
|
+
Notes
|
|
212
|
+
-----
|
|
213
|
+
The strategy works by:
|
|
214
|
+
|
|
215
|
+
1. Getting the canonical unit for the dimension
|
|
216
|
+
2. Decomposing it into base SI units
|
|
217
|
+
3. Finding composed forms using `.compose()`
|
|
218
|
+
4. Generating random combinations by multiplying/dividing
|
|
219
|
+
other units with cancelling dimensions
|
|
220
|
+
|
|
221
|
+
"""
|
|
222
|
+
# Convert to Dimension object, drawing if necessary
|
|
223
|
+
dimension = (
|
|
224
|
+
u.dimension(draw(dimension))
|
|
225
|
+
if isinstance(dimension, st.SearchStrategy)
|
|
226
|
+
else u.dimension(dimension)
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
# Get the canonical unit for this dimension
|
|
230
|
+
base_unit = dimension._unit # noqa: SLF001
|
|
231
|
+
|
|
232
|
+
return draw(derived_units(base_unit, **kwargs))
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Hypothesis strategies for UnitSystem objects."""
|
|
2
|
+
|
|
3
|
+
__all__ = ["unitsystems"]
|
|
4
|
+
|
|
5
|
+
from hypothesis import strategies as st
|
|
6
|
+
|
|
7
|
+
import unxt as u
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@st.composite
|
|
11
|
+
def unitsystems(
|
|
12
|
+
draw: st.DrawFn,
|
|
13
|
+
*units: str | u.AbstractUnit | st.SearchStrategy[u.AbstractUnit],
|
|
14
|
+
) -> u.AbstractUnitSystem:
|
|
15
|
+
"""Generate hypothesis strategy for unxt UnitSystem objects.
|
|
16
|
+
|
|
17
|
+
This strategy supports all the same unit input options as `u.unitsystem()`.
|
|
18
|
+
The additional capability provided here is that any unit input can also be a
|
|
19
|
+
hypothesis strategy, enabling property-based testing with varying units.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
draw : st.DrawFn
|
|
24
|
+
Hypothesis draw function (automatically provided by @st.composite).
|
|
25
|
+
*units : str | unxt.AbstractUnit | st.SearchStrategy[u.AbstractUnit]
|
|
26
|
+
Variable number of unit specifications. Each can be:
|
|
27
|
+
- str: Fixed unit string (e.g., "kpc", "Myr", "Msun")
|
|
28
|
+
- unxt.AbstractUnit: Fixed unit object
|
|
29
|
+
- SearchStrategy: Strategy that generates units
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
u.AbstractUnitSystem
|
|
34
|
+
A UnitSystem object created from the specified units.
|
|
35
|
+
|
|
36
|
+
Examples
|
|
37
|
+
--------
|
|
38
|
+
>>> from hypothesis import given
|
|
39
|
+
>>> import unxt as u
|
|
40
|
+
>>> import unxt_hypothesis as ust
|
|
41
|
+
|
|
42
|
+
Create a unit system with fixed units:
|
|
43
|
+
|
|
44
|
+
>>> @given(usys=ust.unitsystems("m", "s", "kg", "rad"))
|
|
45
|
+
... def test_mks_system(usys):
|
|
46
|
+
... assert isinstance(usys, u.AbstractUnitSystem)
|
|
47
|
+
... assert len(usys) == 4
|
|
48
|
+
|
|
49
|
+
Create a unit system with varying length units:
|
|
50
|
+
|
|
51
|
+
>>> @given(usys=ust.unitsystems(ust.units("length"), "s", "kg", "rad"))
|
|
52
|
+
... def test_varying_length_system(usys):
|
|
53
|
+
... assert isinstance(usys, u.AbstractUnitSystem)
|
|
54
|
+
... assert len(usys) == 4
|
|
55
|
+
... # Length unit will vary, but time is always seconds
|
|
56
|
+
... assert usys["time"] == u.unit("s")
|
|
57
|
+
|
|
58
|
+
Use predefined unit systems by name:
|
|
59
|
+
|
|
60
|
+
>>> @given(usys="galactic") # Predefined
|
|
61
|
+
... def test_galactic_system(usys):
|
|
62
|
+
... assert isinstance(usys, u.AbstractUnitSystem)
|
|
63
|
+
... assert len(usys) == 4
|
|
64
|
+
|
|
65
|
+
"""
|
|
66
|
+
# Process each unit argument
|
|
67
|
+
unit_objs = [
|
|
68
|
+
u.unit(draw(x) if isinstance(x, st.SearchStrategy) else x) for x in units
|
|
69
|
+
]
|
|
70
|
+
# Create and return the unit system
|
|
71
|
+
return u.unitsystem(*unit_objs)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# file generated by setuptools-scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
TYPE_CHECKING = False
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
from typing import Union
|
|
17
|
+
|
|
18
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
20
|
+
else:
|
|
21
|
+
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
23
|
+
|
|
24
|
+
version: str
|
|
25
|
+
__version__: str
|
|
26
|
+
__version_tuple__: VERSION_TUPLE
|
|
27
|
+
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
30
|
+
|
|
31
|
+
__version__ = version = '0.1.dev410'
|
|
32
|
+
__version_tuple__ = version_tuple = (0, 1, 'dev410')
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = None
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: unxt-hypothesis
|
|
3
|
+
Version: 0.1.dev410
|
|
4
|
+
Summary: Hypothesis strategies for unxt
|
|
5
|
+
Project-URL: Bug Tracker, https://github.com/GalacticDynamics/unxt/issues
|
|
6
|
+
Project-URL: Homepage, https://github.com/GalacticDynamics/unxt
|
|
7
|
+
Author-email: GalacticDynamics <nstarman@users.noreply.github.com>, Nathaniel Starkman <nstarman@users.noreply.github.com>
|
|
8
|
+
License: BSD-3-Clause
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Requires-Dist: hypothesis[numpy]>=6.138.14
|
|
24
|
+
Requires-Dist: jax>=0.5.3
|
|
25
|
+
Requires-Dist: unxt
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# unxt-hypothesis
|
|
29
|
+
|
|
30
|
+
Hypothesis strategies for [unxt](https://github.com/GalacticDynamics/unxt) -
|
|
31
|
+
unitful quantities in JAX.
|
|
32
|
+
|
|
33
|
+
This package provides [Hypothesis](https://hypothesis.readthedocs.io/)
|
|
34
|
+
strategies for generating `unxt` objects for for property-based testing.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
uv add unxt-hypothesis
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from hypothesis import given
|
|
46
|
+
|
|
47
|
+
import unxt_hypothesis as ust
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@given(q=ust.quantities(unit="km/s"))
|
|
51
|
+
def test_quantity_property(q):
|
|
52
|
+
# Test some property of quantities
|
|
53
|
+
assert q.value is not None
|
|
54
|
+
assert q.unit is not None
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@given(u=ust.units("length"))
|
|
58
|
+
def test_unit_property(u):
|
|
59
|
+
# Test some property of units
|
|
60
|
+
assert u is not None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@given(sys=ust.unitsystems("m", "s", "kg", "rad"))
|
|
64
|
+
def test_unitsystem_property(sys):
|
|
65
|
+
# Test some property of unit systems
|
|
66
|
+
assert len(sys) == 4
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Custom strategies
|
|
70
|
+
|
|
71
|
+
You can customize the strategies:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from hypothesis import strategies as st
|
|
75
|
+
|
|
76
|
+
import unxt as u
|
|
77
|
+
import unxt_hypothesis as ust
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# Generate quantities with specific shapes
|
|
81
|
+
@given(q=ust.quantities(unit="m", shape=st.just((3, 3))))
|
|
82
|
+
def test_matrix_quantity(q):
|
|
83
|
+
assert q.shape == (3, 3)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# Generate quantities with specific dimensions
|
|
87
|
+
@given(q=ust.quantities(unit=ust.units("length")))
|
|
88
|
+
def test_length_quantity(q):
|
|
89
|
+
assert u.dimension_of(q) == u.dimension("length")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## API
|
|
93
|
+
|
|
94
|
+
### `quantities(draw, *, shape=None, dtype=None, unit=None)`
|
|
95
|
+
|
|
96
|
+
Generate random `Quantity` objects.
|
|
97
|
+
|
|
98
|
+
**Parameters:**
|
|
99
|
+
|
|
100
|
+
- `draw`: Hypothesis draw function
|
|
101
|
+
- `shape`: Strategy for array shapes (optional, defaults to small arrays)
|
|
102
|
+
- `dtype`: Strategy for array dtypes (optional, defaults to float32)
|
|
103
|
+
- `unit`: Strategy for unit strings (optional, defaults to common units)
|
|
104
|
+
|
|
105
|
+
**Returns:** A `unxt.Quantity` instance
|
|
106
|
+
|
|
107
|
+
### `units(draw, dimension=None, *, max_complexity=2, allow_non_integer_powers=False)`
|
|
108
|
+
|
|
109
|
+
Generate random `Unit` objects.
|
|
110
|
+
|
|
111
|
+
**Parameters:**
|
|
112
|
+
|
|
113
|
+
- `draw`: Hypothesis draw function
|
|
114
|
+
- `dimension`: Physical dimension (optional, e.g., `"length"`, `"velocity"`)
|
|
115
|
+
- `max_complexity`: Maximum complexity of compound units (default: 2)
|
|
116
|
+
- `allow_non_integer_powers`: Allow non-integer powers (default: False)
|
|
117
|
+
|
|
118
|
+
**Returns:** A `unxt.AbstractUnit` instance
|
|
119
|
+
|
|
120
|
+
### `unitsystems(*units)`
|
|
121
|
+
|
|
122
|
+
Generate random `UnitSystem` objects.
|
|
123
|
+
|
|
124
|
+
**Parameters:**
|
|
125
|
+
|
|
126
|
+
- `*units`: Variable number of unit specifications. Each can be:
|
|
127
|
+
- `str`: Fixed unit string (e.g., `"m"`, `"kg"`)
|
|
128
|
+
- `unxt.AbstractUnit`: Fixed unit object
|
|
129
|
+
- Strategy: A Hypothesis strategy that generates units
|
|
130
|
+
|
|
131
|
+
**Returns:** A `unxt.AbstractUnitSystem` instance
|
|
132
|
+
|
|
133
|
+
**Example:**
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from hypothesis import given
|
|
137
|
+
|
|
138
|
+
import unxt_hypothesis as ust
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# Fixed unit system
|
|
142
|
+
@given(sys=ust.unitsystems("m", "s", "kg", "rad"))
|
|
143
|
+
def test_mks_system(sys):
|
|
144
|
+
assert len(sys) == 4
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# Varying length unit, other units fixed
|
|
148
|
+
@given(sys=ust.unitsystems(ust.units("length"), "s", "kg", "rad"))
|
|
149
|
+
def test_varying_length(sys):
|
|
150
|
+
assert len(sys) == 4
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Documentation
|
|
154
|
+
|
|
155
|
+
For comprehensive documentation, examples, and guides, see the
|
|
156
|
+
[unxt documentation](https://unxt.readthedocs.io/en/latest/guides/testing.html).
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
BSD 3-Clause License. See [LICENSE](../../LICENSE) for details.
|
|
161
|
+
|
|
162
|
+
## Contributing
|
|
163
|
+
|
|
164
|
+
Contributions are welcome! Please see the main
|
|
165
|
+
[unxt repository](https://github.com/GalacticDynamics/unxt) for contributing
|
|
166
|
+
guidelines.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
unxt_hypothesis/__init__.py,sha256=anB8eOu1GYe4m4niAw-vlh3KamiVvxUGxcu997ez11Y,298
|
|
2
|
+
unxt_hypothesis/_version.py,sha256=Ss_CbS_Tl7UvclfamUPNubb1a2t-RKWozSOGJGBpTnw,716
|
|
3
|
+
unxt_hypothesis/_src/__init__.py,sha256=_dimZ3uIzK2bfNvHWIMKf3s7HO1rI8kKbpk4IRUWBME,69
|
|
4
|
+
unxt_hypothesis/_src/quantities.py,sha256=QbR2VjrLPPJCLNYXfVew-_CFq8_biiungoHWo2bTlTs,9541
|
|
5
|
+
unxt_hypothesis/_src/units.py,sha256=XRgUHqI4rs9tPrU7nMopmbHWjV7ZA6delEo9eJC-Mf4,7758
|
|
6
|
+
unxt_hypothesis/_src/unitsystems.py,sha256=_zhbLAV0suGS1baLgYHSE3SrmthX0vVap7B8YAtz6js,2315
|
|
7
|
+
unxt_hypothesis-0.1.dev410.dist-info/METADATA,sha256=lKSCCl51z5PS8_8yjy13c-Cjm88GOLounyvwwggT9gE,4481
|
|
8
|
+
unxt_hypothesis-0.1.dev410.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
9
|
+
unxt_hypothesis-0.1.dev410.dist-info/RECORD,,
|