coordax 0.2.2__tar.gz → 0.2.3__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.
Files changed (25) hide show
  1. {coordax-0.2.2 → coordax-0.2.3}/PKG-INFO +1 -1
  2. {coordax-0.2.2 → coordax-0.2.3}/coordax/__init__.py +1 -1
  3. {coordax-0.2.2 → coordax-0.2.3}/coordax/coordinate_systems.py +36 -1
  4. {coordax-0.2.2 → coordax-0.2.3}/coordax/coordinate_systems_test.py +21 -0
  5. {coordax-0.2.2 → coordax-0.2.3}/coordax/coords.py +1 -0
  6. {coordax-0.2.2 → coordax-0.2.3}/coordax.egg-info/PKG-INFO +1 -1
  7. {coordax-0.2.2 → coordax-0.2.3}/pyproject.toml +1 -1
  8. {coordax-0.2.2 → coordax-0.2.3}/LICENSE +0 -0
  9. {coordax-0.2.2 → coordax-0.2.3}/README.md +0 -0
  10. {coordax-0.2.2 → coordax-0.2.3}/coordax/doctest_test.py +0 -0
  11. {coordax-0.2.2 → coordax-0.2.3}/coordax/experimental.py +0 -0
  12. {coordax-0.2.2 → coordax-0.2.3}/coordax/fields.py +0 -0
  13. {coordax-0.2.2 → coordax-0.2.3}/coordax/fields_test.py +0 -0
  14. {coordax-0.2.2 → coordax-0.2.3}/coordax/jax_datetime_integration_test.py +0 -0
  15. {coordax-0.2.2 → coordax-0.2.3}/coordax/named_axes.py +0 -0
  16. {coordax-0.2.2 → coordax-0.2.3}/coordax/named_axes_test.py +0 -0
  17. {coordax-0.2.2 → coordax-0.2.3}/coordax/ndarrays.py +0 -0
  18. {coordax-0.2.2 → coordax-0.2.3}/coordax/testing.py +0 -0
  19. {coordax-0.2.2 → coordax-0.2.3}/coordax/utils.py +0 -0
  20. {coordax-0.2.2 → coordax-0.2.3}/coordax/xarray_test.py +0 -0
  21. {coordax-0.2.2 → coordax-0.2.3}/coordax.egg-info/SOURCES.txt +0 -0
  22. {coordax-0.2.2 → coordax-0.2.3}/coordax.egg-info/dependency_links.txt +0 -0
  23. {coordax-0.2.2 → coordax-0.2.3}/coordax.egg-info/requires.txt +0 -0
  24. {coordax-0.2.2 → coordax-0.2.3}/coordax.egg-info/top_level.txt +0 -0
  25. {coordax-0.2.2 → coordax-0.2.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coordax
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Coordinate axes for scientific computing in JAX
5
5
  Author-email: Google LLC <noreply@google.com>
6
6
  License-Expression: Apache-2.0
@@ -53,4 +53,4 @@ from coordax.ndarrays import (
53
53
  )
54
54
  import coordax.testing # pylint: disable=unused-import
55
55
 
56
- __version__ = '0.2.2' # keep sync with pyproject.toml
56
+ __version__ = '0.2.3' # keep sync with pyproject.toml
@@ -25,7 +25,7 @@ import dataclasses
25
25
  import functools
26
26
  import itertools
27
27
  import typing
28
- from typing import Any, Self, TYPE_CHECKING, TypeAlias, TypeVar
28
+ from typing import Any, Self, TYPE_CHECKING, Type, TypeAlias, TypeVar
29
29
  import warnings
30
30
 
31
31
  from coordax import utils
@@ -636,6 +636,41 @@ def replace_axes(
636
636
  return compose(*axes)
637
637
 
638
638
 
639
+ @functools.partial(utils.export, module='coordax.coords')
640
+ def extract(
641
+ coord: Coordinate,
642
+ component_type: Type[Coordinate] | tuple[Type[Coordinate], ...],
643
+ ) -> Coordinate:
644
+ """Extracts component of type `component_type` from the `coord`.
645
+
646
+ Args:
647
+ coord: The coordinate system to search.
648
+ component_type: The type(s) of coordinate to extract.
649
+
650
+ Returns:
651
+ The single coordinate component matching the given type(s).
652
+
653
+ Raises:
654
+ ValueError: If there is not exactly one component of the given type(s).
655
+
656
+ Examples:
657
+ >>> import coordax as cx
658
+ >>> import numpy as np
659
+ >>> x = cx.LabeledAxis('x', np.linspace(0, np.pi, 4))
660
+ >>> y = cx.SizedAxis('y', 3)
661
+ >>> cx.coords.extract(cx.coords.compose(x, y), cx.SizedAxis)
662
+ coordax.SizedAxis('y', size=3)
663
+ """
664
+ components = canonicalize(coord)
665
+ of_type = [c for c in components if isinstance(c, component_type)]
666
+ if len(of_type) != 1:
667
+ raise ValueError(
668
+ f'Expected exactly one instance of {component_type}, found {of_type}'
669
+ )
670
+ [result] = of_type
671
+ return result
672
+
673
+
639
674
  @functools.partial(utils.export, module='coordax.coords')
640
675
  def from_xarray(
641
676
  data_array: xarray.DataArray,
@@ -459,6 +459,27 @@ class CoordinateSystemsTest(parameterized.TestCase):
459
459
  ):
460
460
  cx.CartesianProduct((x, y))
461
461
 
462
+ def test_extract(self):
463
+ x = cx.SizedAxis('x', 2)
464
+ y = cx.SizedAxis('y', 3)
465
+ z = cx.LabeledAxis('z', np.arange(4))
466
+ xy = cx.coords.compose(x, y)
467
+ xz = cx.coords.compose(x, z)
468
+
469
+ self.assertEqual(cx.coords.extract(x, cx.SizedAxis), x)
470
+ self.assertEqual(cx.coords.extract(z, cx.LabeledAxis), z)
471
+ self.assertEqual(cx.coords.extract(xz, cx.LabeledAxis), z)
472
+ self.assertEqual(cx.coords.extract(z, (cx.SizedAxis, cx.LabeledAxis)), z)
473
+
474
+ with self.assertRaisesRegex(ValueError, 'Expected exactly one instance'):
475
+ cx.coords.extract(xy, cx.SizedAxis)
476
+
477
+ with self.assertRaisesRegex(ValueError, 'Expected exactly one instance'):
478
+ cx.coords.extract(xz, (cx.SizedAxis, cx.LabeledAxis))
479
+
480
+ with self.assertRaisesRegex(ValueError, 'Expected exactly one instance'):
481
+ cx.coords.extract(x, cx.LabeledAxis)
482
+
462
483
  def test_deprecated_aliases(self):
463
484
  with self.assertWarnsRegex(
464
485
  DeprecationWarning,
@@ -22,6 +22,7 @@ from coordax.coordinate_systems import (
22
22
  insert_axes as insert_axes,
23
23
  replace_axes as replace_axes,
24
24
  from_xarray as from_xarray,
25
+ extract as extract,
25
26
  ArrayKey as ArrayKey,
26
27
  NoCoordinateMatch as NoCoordinateMatch,
27
28
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coordax
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Coordinate axes for scientific computing in JAX
5
5
  Author-email: Google LLC <noreply@google.com>
6
6
  License-Expression: Apache-2.0
@@ -7,7 +7,7 @@ packages = ["coordax"]
7
7
 
8
8
  [project]
9
9
  name = "coordax"
10
- version = "0.2.2" # keep sync with __init__.py
10
+ version = "0.2.3" # keep sync with __init__.py
11
11
  description = "Coordinate axes for scientific computing in JAX"
12
12
  authors = [
13
13
  {name = "Google LLC", email = "noreply@google.com"},
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes