unxt 0.1.dev463__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.
Potentially problematic release.
This version of unxt might be problematic. Click here for more details.
- unxt/__init__.py +79 -0
- unxt/_interop/__init__.py +18 -0
- unxt/_interop/optional_deps.py +13 -0
- unxt/_interop/unxt_interop_astropy/__init__.py +5 -0
- unxt/_interop/unxt_interop_astropy/custom_types.py +12 -0
- unxt/_interop/unxt_interop_astropy/dimensions.py +53 -0
- unxt/_interop/unxt_interop_astropy/quantity.py +308 -0
- unxt/_interop/unxt_interop_astropy/units.py +111 -0
- unxt/_interop/unxt_interop_gala/__init__.py +5 -0
- unxt/_interop/unxt_interop_gala/unitsystems.py +108 -0
- unxt/_interop/unxt_interop_mpl/__init__.py +81 -0
- unxt/_src/__init__.py +6 -0
- unxt/_src/dimensions.py +400 -0
- unxt/_src/experimental.py +234 -0
- unxt/_src/quantity/__init__.py +24 -0
- unxt/_src/quantity/angle.py +58 -0
- unxt/_src/quantity/base.py +1160 -0
- unxt/_src/quantity/base_angle.py +89 -0
- unxt/_src/quantity/base_parametric.py +290 -0
- unxt/_src/quantity/flag.py +137 -0
- unxt/_src/quantity/mixins.py +238 -0
- unxt/_src/quantity/quantity.py +123 -0
- unxt/_src/quantity/register_api.py +383 -0
- unxt/_src/quantity/register_conversions.py +95 -0
- unxt/_src/quantity/register_dispatches.py +246 -0
- unxt/_src/quantity/register_primitives.py +5075 -0
- unxt/_src/quantity/static_quantity.py +90 -0
- unxt/_src/quantity/unchecked.py +45 -0
- unxt/_src/quantity/value.py +289 -0
- unxt/_src/units.py +103 -0
- unxt/_src/unitsystems/__init__.py +40 -0
- unxt/_src/unitsystems/base.py +258 -0
- unxt/_src/unitsystems/builtin.py +190 -0
- unxt/_src/unitsystems/builtin_dimensions.py +21 -0
- unxt/_src/unitsystems/compare.py +13 -0
- unxt/_src/unitsystems/core.py +307 -0
- unxt/_src/unitsystems/flags.py +80 -0
- unxt/_src/unitsystems/realizations.py +73 -0
- unxt/_src/unitsystems/utils.py +95 -0
- unxt/_src/utils.py +112 -0
- unxt/_version.py +34 -0
- unxt/_version.pyi +2 -0
- unxt/dims.py +19 -0
- unxt/py.typed +0 -0
- unxt/quantity.py +172 -0
- unxt/setup_package.py +60 -0
- unxt/units.py +19 -0
- unxt/unitsystems.py +67 -0
- unxt-0.1.dev463.dist-info/METADATA +533 -0
- unxt-0.1.dev463.dist-info/RECORD +52 -0
- unxt-0.1.dev463.dist-info/WHEEL +4 -0
- unxt-0.1.dev463.dist-info/licenses/LICENSE +29 -0
unxt/dims.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Working with dimensions.
|
|
2
|
+
|
|
3
|
+
The main features are:
|
|
4
|
+
|
|
5
|
+
- ``unxt.dims.dimension``: a function to construct a dimension object.
|
|
6
|
+
- ``unxt.dims.dimension_of``: a function to get the dimensions of an object.
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
__all__ = ("dimension", "dimension_of", "AbstractDimension")
|
|
11
|
+
|
|
12
|
+
from .setup_package import install_import_hook
|
|
13
|
+
|
|
14
|
+
with install_import_hook("unxt.dims"):
|
|
15
|
+
from ._src.dimensions import AbstractDimension
|
|
16
|
+
from unxt_api import dimension, dimension_of
|
|
17
|
+
|
|
18
|
+
# Clean up the namespace
|
|
19
|
+
del install_import_hook
|
unxt/py.typed
ADDED
|
File without changes
|
unxt/quantity.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""Quantities in JAX.
|
|
2
|
+
|
|
3
|
+
This module provides JAX-compatible quantity types with automatic unit handling,
|
|
4
|
+
conversions, and a rich set of mathematical operations that preserve dimensional
|
|
5
|
+
correctness.
|
|
6
|
+
|
|
7
|
+
## Core Classes
|
|
8
|
+
|
|
9
|
+
- **`Quantity`**: The main quantity class with dimension parametrization and
|
|
10
|
+
full unit checking. Aliased as `Q` for convenience.
|
|
11
|
+
- **`StaticQuantity`**: Parametric quantity with static NumPy values for JAX
|
|
12
|
+
static arguments.
|
|
13
|
+
- **`BareQuantity`**: Lightweight quantity without dimension parametrization.
|
|
14
|
+
- **`Angle`**: Specialized quantity type for angular measurements with wrapping
|
|
15
|
+
support.
|
|
16
|
+
|
|
17
|
+
## Key Functions
|
|
18
|
+
|
|
19
|
+
- **`uconvert`**: Convert a quantity to different units.
|
|
20
|
+
- **`ustrip`**: Extract the numerical value from a quantity.
|
|
21
|
+
- **`is_unit_convertible`**: Check if two units are convertible to each other.
|
|
22
|
+
- **`wrap_to`**: Wrap angular quantities to a specified range.
|
|
23
|
+
|
|
24
|
+
## Examples
|
|
25
|
+
|
|
26
|
+
### Creating quantities with units:
|
|
27
|
+
|
|
28
|
+
>>> import unxt as u
|
|
29
|
+
>>> import jax
|
|
30
|
+
>>> import jax.numpy as jnp
|
|
31
|
+
|
|
32
|
+
>>> distance = u.Quantity(100, "m")
|
|
33
|
+
>>> distance
|
|
34
|
+
Quantity(Array(100, dtype=int32, ...), unit='m')
|
|
35
|
+
|
|
36
|
+
Create from arrays
|
|
37
|
+
|
|
38
|
+
>>> velocities = u.Quantity([10, 20, 30], "m/s")
|
|
39
|
+
>>> velocities
|
|
40
|
+
Quantity(Array([10, 20, 30], dtype=int32), unit='m / s')
|
|
41
|
+
|
|
42
|
+
Unit conversions and arithmetic:
|
|
43
|
+
|
|
44
|
+
Convert units
|
|
45
|
+
|
|
46
|
+
>>> distance_km = u.uconvert("km", distance)
|
|
47
|
+
>>> distance_km
|
|
48
|
+
Quantity(Array(0.1, dtype=float32, ...), unit='km')
|
|
49
|
+
|
|
50
|
+
Arithmetic preserves units.
|
|
51
|
+
|
|
52
|
+
>>> time = u.Q(5, "s") # use Quantity alias
|
|
53
|
+
>>> velocity = distance / time
|
|
54
|
+
>>> velocity
|
|
55
|
+
Quantity(Array(20., dtype=float32, ...), unit='m / s')
|
|
56
|
+
|
|
57
|
+
Strip units for numerical operations
|
|
58
|
+
|
|
59
|
+
>>> u.ustrip("m", distance)
|
|
60
|
+
Array(100, dtype=int32, ...)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
### Working with angles:
|
|
64
|
+
|
|
65
|
+
Create angle quantities
|
|
66
|
+
|
|
67
|
+
>>> theta = u.Angle(180, "deg")
|
|
68
|
+
>>> theta
|
|
69
|
+
Angle(Array(180, dtype=int32, ...), unit='deg')
|
|
70
|
+
|
|
71
|
+
Convert to radians
|
|
72
|
+
|
|
73
|
+
>>> theta_rad = u.uconvert("rad", theta)
|
|
74
|
+
>>> theta_rad
|
|
75
|
+
Angle(Array(3.1415927, dtype=float32, ...), unit='rad')
|
|
76
|
+
|
|
77
|
+
Wrap angles to a range
|
|
78
|
+
|
|
79
|
+
>>> angle = u.Angle(450, "deg")
|
|
80
|
+
>>> wrapped = u.quantity.wrap_to(angle, u.Angle(0, "deg"), u.Angle(360, "deg"))
|
|
81
|
+
>>> wrapped
|
|
82
|
+
Angle(Array(90, dtype=int32, ...), unit='deg')
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
### Advanced usage with JAX transformations:
|
|
86
|
+
|
|
87
|
+
Quantities work with JAX transformations
|
|
88
|
+
|
|
89
|
+
>>> def kinetic_energy(mass, velocity):
|
|
90
|
+
... return 0.5 * mass * velocity**2
|
|
91
|
+
|
|
92
|
+
>>> mass = u.Q(2.0, "kg")
|
|
93
|
+
>>> vel = u.Q(10.0, "m/s")
|
|
94
|
+
>>> energy = kinetic_energy(mass, vel)
|
|
95
|
+
>>> energy
|
|
96
|
+
Quantity(Array(100., dtype=float32, ...), unit='m2 kg / s2')
|
|
97
|
+
|
|
98
|
+
Convert to standard energy units
|
|
99
|
+
|
|
100
|
+
>>> u.uconvert("J", energy)
|
|
101
|
+
Quantity(Array(100., dtype=float32, ...), unit='m2 kg / s2')
|
|
102
|
+
|
|
103
|
+
JIT compilation works seamlessly
|
|
104
|
+
|
|
105
|
+
>>> @jax.jit
|
|
106
|
+
... def compute_force(mass, accel):
|
|
107
|
+
... return mass * accel
|
|
108
|
+
>>> force = compute_force(u.Q(5.0, "kg"), u.Q(9.8, "m/s^2"))
|
|
109
|
+
>>> force
|
|
110
|
+
Quantity(Array(49., dtype=float32, ...), unit='kg m / s2')
|
|
111
|
+
|
|
112
|
+
"""
|
|
113
|
+
# pylint: disable=duplicate-code
|
|
114
|
+
|
|
115
|
+
__all__ = (
|
|
116
|
+
# Core
|
|
117
|
+
"Quantity",
|
|
118
|
+
"Q", # convenience alias
|
|
119
|
+
"StaticQuantity",
|
|
120
|
+
"StaticValue",
|
|
121
|
+
# Base
|
|
122
|
+
"AbstractQuantity",
|
|
123
|
+
# Fast
|
|
124
|
+
"BareQuantity",
|
|
125
|
+
# Angles
|
|
126
|
+
"AbstractAngle",
|
|
127
|
+
"Angle",
|
|
128
|
+
# Base Parametric
|
|
129
|
+
"AbstractParametricQuantity",
|
|
130
|
+
# Functional
|
|
131
|
+
"uconvert_value",
|
|
132
|
+
"uconvert",
|
|
133
|
+
"ustrip",
|
|
134
|
+
"is_unit_convertible",
|
|
135
|
+
"wrap_to",
|
|
136
|
+
"is_any_quantity",
|
|
137
|
+
"convert_to_quantity_value",
|
|
138
|
+
"AllowValue",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
from .setup_package import install_import_hook
|
|
143
|
+
|
|
144
|
+
with install_import_hook("unxt.quantity"):
|
|
145
|
+
from ._src.quantity.angle import Angle
|
|
146
|
+
from ._src.quantity.base import AbstractQuantity, is_any_quantity
|
|
147
|
+
from ._src.quantity.base_angle import AbstractAngle
|
|
148
|
+
from ._src.quantity.base_parametric import AbstractParametricQuantity
|
|
149
|
+
from ._src.quantity.flag import AllowValue
|
|
150
|
+
from ._src.quantity.quantity import Q, Quantity
|
|
151
|
+
from ._src.quantity.static_quantity import StaticQuantity
|
|
152
|
+
from ._src.quantity.unchecked import BareQuantity
|
|
153
|
+
from ._src.quantity.value import StaticValue, convert_to_quantity_value
|
|
154
|
+
from unxt_api import is_unit_convertible, uconvert, uconvert_value, ustrip, wrap_to
|
|
155
|
+
|
|
156
|
+
# isort: split
|
|
157
|
+
# Register dispatches and conversions
|
|
158
|
+
from ._src.quantity import (
|
|
159
|
+
register_api,
|
|
160
|
+
register_conversions,
|
|
161
|
+
register_dispatches,
|
|
162
|
+
register_primitives,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# Clean up namespace
|
|
166
|
+
del (
|
|
167
|
+
register_conversions,
|
|
168
|
+
register_api,
|
|
169
|
+
register_dispatches,
|
|
170
|
+
register_primitives,
|
|
171
|
+
install_import_hook,
|
|
172
|
+
)
|
unxt/setup_package.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Package setup information.
|
|
2
|
+
|
|
3
|
+
Note that this module is NOT public API nor are any of its contents.
|
|
4
|
+
Stability is NOT guaranteed.
|
|
5
|
+
This module exposes package setup information for the `unxt` package.
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__all__: tuple[str, ...] = ()
|
|
10
|
+
|
|
11
|
+
import contextlib
|
|
12
|
+
import os
|
|
13
|
+
from collections.abc import Sequence
|
|
14
|
+
from typing import Any, Final, Literal
|
|
15
|
+
|
|
16
|
+
from jaxtyping import install_import_hook as _install_import_hook
|
|
17
|
+
|
|
18
|
+
_RUNTIME_TYPECHECKER: str | None | Literal[False]
|
|
19
|
+
match os.getenv("UNXT_ENABLE_RUNTIME_TYPECHECKING", "False"):
|
|
20
|
+
case "False":
|
|
21
|
+
_RUNTIME_TYPECHECKER = False
|
|
22
|
+
case "None":
|
|
23
|
+
_RUNTIME_TYPECHECKER = None
|
|
24
|
+
case str() as _name:
|
|
25
|
+
_RUNTIME_TYPECHECKER = _name
|
|
26
|
+
|
|
27
|
+
RUNTIME_TYPECHECKER: Final[str | None | Literal[False]] = _RUNTIME_TYPECHECKER
|
|
28
|
+
"""Runtime type checking variable "UNXT_ENABLE_RUNTIME_TYPECHECKING".
|
|
29
|
+
|
|
30
|
+
Set to "False" to disable runtime typechecking (default).
|
|
31
|
+
Set to "None" to only enable typechecking for `@jaxtyped`-decorated functions.
|
|
32
|
+
Set to "beartype.beartype" to enable runtime typechecking.
|
|
33
|
+
|
|
34
|
+
See https://docs.kidger.site/jaxtyping/api/runtime-type-checking for more
|
|
35
|
+
information on options.
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def install_import_hook(
|
|
41
|
+
modules: str | Sequence[str], /
|
|
42
|
+
) -> contextlib.AbstractContextManager[Any, None]:
|
|
43
|
+
"""Install the jaxtyping import hook for the given modules.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
modules
|
|
48
|
+
Module name or sequence of module names to install the import hook for.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
contextlib.AbstractContextManager
|
|
53
|
+
Context manager that installs the import hook on entry and removes it on exit.
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
return (
|
|
57
|
+
_install_import_hook(modules, RUNTIME_TYPECHECKER)
|
|
58
|
+
if RUNTIME_TYPECHECKER is not False
|
|
59
|
+
else contextlib.nullcontext()
|
|
60
|
+
)
|
unxt/units.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Working with units.
|
|
2
|
+
|
|
3
|
+
The main features are:
|
|
4
|
+
|
|
5
|
+
- ``unxt.units.unit``: a function to construct units.
|
|
6
|
+
- ``unxt.units.unit_of``: a function to get the units of an object.
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
__all__ = ("unit", "unit_of", "AbstractUnit")
|
|
11
|
+
|
|
12
|
+
from .setup_package import install_import_hook
|
|
13
|
+
|
|
14
|
+
with install_import_hook("unxt.units"):
|
|
15
|
+
from ._src.units import AbstractUnit
|
|
16
|
+
from unxt_api import unit, unit_of
|
|
17
|
+
|
|
18
|
+
# Clean up the namespace
|
|
19
|
+
del install_import_hook
|
unxt/unitsystems.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Systems of units.
|
|
2
|
+
|
|
3
|
+
A unit system is a collection of units that are used together. In a unit system
|
|
4
|
+
there are base units and derived units. Base units are the fundamental units of
|
|
5
|
+
the system and derived units are constructed from the base units. For example,
|
|
6
|
+
in the SI system, the base units are the meter, kilogram, second, ampere,
|
|
7
|
+
kelvin, mole, and candela. Derived units are constructed from these base units,
|
|
8
|
+
for example, the newton is a derived unit of force.
|
|
9
|
+
|
|
10
|
+
`unxt` provides powerful tools for defining and working with unit systems. Unit
|
|
11
|
+
systems can be statically defined (useful for many tools and development
|
|
12
|
+
environments) or dynamically defined (useful for interactive environments and
|
|
13
|
+
Python's general dynamism). Unit systems can be extended, compared, and used for
|
|
14
|
+
decomposing units on quantities. There are many more features and tools for
|
|
15
|
+
working with unit systems in `unxt`.
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
# pylint: disable=duplicate-code
|
|
19
|
+
|
|
20
|
+
__all__ = (
|
|
21
|
+
"unitsystem",
|
|
22
|
+
"unitsystem_of",
|
|
23
|
+
# classes
|
|
24
|
+
"AbstractUnitSystem",
|
|
25
|
+
"UNITSYSTEMS_REGISTRY",
|
|
26
|
+
"DimensionlessUnitSystem",
|
|
27
|
+
"LTMAUnitSystem",
|
|
28
|
+
# unit system instance
|
|
29
|
+
"galactic",
|
|
30
|
+
"dimensionless",
|
|
31
|
+
"solarsystem",
|
|
32
|
+
"si",
|
|
33
|
+
"cgs",
|
|
34
|
+
# unit system alias
|
|
35
|
+
"NAMED_UNIT_SYSTEMS",
|
|
36
|
+
# unit system flags
|
|
37
|
+
"AbstractUSysFlag",
|
|
38
|
+
"StandardUSysFlag",
|
|
39
|
+
"DynamicalSimUSysFlag",
|
|
40
|
+
# functions
|
|
41
|
+
"equivalent",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
from .setup_package import install_import_hook
|
|
45
|
+
|
|
46
|
+
with install_import_hook("unxt.unitsystems"):
|
|
47
|
+
from ._src.unitsystems import (
|
|
48
|
+
NAMED_UNIT_SYSTEMS,
|
|
49
|
+
UNITSYSTEMS_REGISTRY,
|
|
50
|
+
AbstractUnitSystem,
|
|
51
|
+
AbstractUSysFlag,
|
|
52
|
+
DimensionlessUnitSystem,
|
|
53
|
+
DynamicalSimUSysFlag,
|
|
54
|
+
LTMAUnitSystem,
|
|
55
|
+
StandardUSysFlag,
|
|
56
|
+
cgs,
|
|
57
|
+
dimensionless,
|
|
58
|
+
equivalent,
|
|
59
|
+
galactic,
|
|
60
|
+
si,
|
|
61
|
+
solarsystem,
|
|
62
|
+
unitsystem, # TODO: move unitsystem to unxt_api
|
|
63
|
+
)
|
|
64
|
+
from unxt_api import unitsystem_of
|
|
65
|
+
|
|
66
|
+
# Clean up the namespace
|
|
67
|
+
del install_import_hook
|