differt 0.0.16__tar.gz → 0.0.18__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 (32) hide show
  1. {differt-0.0.16 → differt-0.0.18}/.gitignore +7 -0
  2. {differt-0.0.16 → differt-0.0.18}/PKG-INFO +29 -19
  3. {differt-0.0.16 → differt-0.0.18}/pyproject.toml +22 -11
  4. {differt-0.0.16 → differt-0.0.18}/src/differt/em/constants.py +1 -1
  5. {differt-0.0.16 → differt-0.0.18}/src/differt/em/special.py +14 -15
  6. {differt-0.0.16 → differt-0.0.18}/src/differt/em/utd.py +2 -2
  7. {differt-0.0.16 → differt-0.0.18}/src/differt/em/utils.py +9 -7
  8. differt-0.0.18/src/differt/geometry/paths.py +187 -0
  9. differt-0.0.18/src/differt/geometry/triangle_mesh.py +397 -0
  10. differt-0.0.18/src/differt/geometry/utils.py +403 -0
  11. {differt-0.0.16 → differt-0.0.18}/src/differt/plotting/__init__.py +15 -12
  12. differt-0.0.18/src/differt/plotting/_core.py +592 -0
  13. {differt-0.0.16 → differt-0.0.18}/src/differt/plotting/_utils.py +176 -127
  14. {differt-0.0.16 → differt-0.0.18}/src/differt/rt/fermat.py +37 -25
  15. {differt-0.0.16 → differt-0.0.18}/src/differt/rt/image_method.py +153 -66
  16. differt-0.0.18/src/differt/rt/utils.py +463 -0
  17. {differt-0.0.16 → differt-0.0.18}/src/differt/scene/sionna.py +23 -16
  18. differt-0.0.18/src/differt/scene/triangle_scene.py +284 -0
  19. {differt-0.0.16 → differt-0.0.18}/src/differt/utils.py +21 -21
  20. differt-0.0.16/src/differt/geometry/triangle_mesh.py +0 -240
  21. differt-0.0.16/src/differt/geometry/utils.py +0 -148
  22. differt-0.0.16/src/differt/plotting/_core.py +0 -474
  23. differt-0.0.16/src/differt/rt/utils.py +0 -278
  24. differt-0.0.16/src/differt/scene/triangle_scene.py +0 -112
  25. {differt-0.0.16 → differt-0.0.18}/LICENSE.md +0 -0
  26. {differt-0.0.16 → differt-0.0.18}/README.md +0 -0
  27. {differt-0.0.16 → differt-0.0.18}/src/differt/__init__.py +0 -0
  28. {differt-0.0.16 → differt-0.0.18}/src/differt/em/__init__.py +0 -0
  29. {differt-0.0.16 → differt-0.0.18}/src/differt/geometry/__init__.py +0 -0
  30. {differt-0.0.16 → differt-0.0.18}/src/differt/rt/__init__.py +0 -0
  31. {differt-0.0.16 → differt-0.0.18}/src/differt/scene/__init__.py +0 -0
  32. {differt-0.0.16 → differt-0.0.18}/src/differt/version.py +0 -0
@@ -10,9 +10,12 @@ target/
10
10
  # Docs
11
11
 
12
12
  docs/build/
13
+ docs/jupyter_execute/
13
14
  docs/source/reference/*.rst
14
15
 
15
16
  # Sionna data
17
+
18
+ differt/src/differt/scene/scenes.lock
16
19
  differt/src/differt/scene/scenes/
17
20
 
18
21
  # Notebooks
@@ -20,3 +23,7 @@ differt/src/differt/scene/scenes/
20
23
  .ipynb_checkpoints/
21
24
 
22
25
  # Tools
26
+
27
+ .benchmarks/
28
+ .coverage*
29
+ coverage.xml
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: DiffeRT
3
- Version: 0.0.16
3
+ Version: 0.0.18
4
4
  Summary: Differentiable Ray Tracing Toolbox for Radio Propagation Simulations
5
5
  Project-URL: Changelog, https://github.com/jeertmans/DiffeRT/releases
6
6
  Project-URL: Documentation, https://eertmans.be/DiffeRT
@@ -14,32 +14,34 @@ Keywords: differentiable,jax,propagation,radio,ray tracing
14
14
  Classifier: License :: OSI Approved :: MIT License
15
15
  Classifier: Operating System :: OS Independent
16
16
  Classifier: Programming Language :: Python :: 3
17
- Classifier: Programming Language :: Python :: 3.9
18
17
  Classifier: Programming Language :: Python :: 3.10
19
18
  Classifier: Programming Language :: Python :: 3.11
20
19
  Classifier: Programming Language :: Python :: 3.12
21
20
  Classifier: Topic :: Scientific/Engineering
22
- Requires-Python: >=3.9
21
+ Requires-Python: >=3.10
23
22
  Requires-Dist: beartype>=0.17.2
24
- Requires-Dist: differt-core==0.0.16
25
- Requires-Dist: equinox>=0.11.2
26
- Requires-Dist: jax>=0.4.20
23
+ Requires-Dist: chex>=0.1.86
24
+ Requires-Dist: differt-core==0.0.18
25
+ Requires-Dist: equinox>=0.11.5
26
+ Requires-Dist: filelock>=3.15.4
27
+ Requires-Dist: jax>=0.4.32
27
28
  Requires-Dist: jaxtyping>=0.2.24
28
29
  Requires-Dist: numpy>=1.26.1
29
30
  Requires-Dist: optax>=0.1.7
30
- Requires-Dist: requests>=2.31.0
31
+ Requires-Dist: requests>=2.32.0
31
32
  Requires-Dist: tqdm>=4.66.2
32
33
  Requires-Dist: typing-extensions>=4.9.0; python_version < '3.11'
33
34
  Provides-Extra: all
34
- Requires-Dist: ipympl; extra == 'all'
35
+ Requires-Dist: ipympl>=0.9.4; extra == 'all'
35
36
  Requires-Dist: jupyter-rfb>=0.4.2; extra == 'all'
36
37
  Requires-Dist: matplotlib>=3.8.1; extra == 'all'
37
38
  Requires-Dist: notebook>5; extra == 'all'
38
39
  Requires-Dist: plotly>=5.18.0; extra == 'all'
40
+ Requires-Dist: pyopengl>=3.1.0; (platform_system == 'Darwin') and extra == 'all'
39
41
  Requires-Dist: pyqt6>=6.6.1; extra == 'all'
40
42
  Requires-Dist: vispy>=0.14.1; extra == 'all'
41
43
  Provides-Extra: jupyter
42
- Requires-Dist: ipympl; extra == 'jupyter'
44
+ Requires-Dist: ipympl>=0.9.4; extra == 'jupyter'
43
45
  Requires-Dist: jupyter-rfb>=0.4.2; extra == 'jupyter'
44
46
  Requires-Dist: notebook>5; extra == 'jupyter'
45
47
  Provides-Extra: matplotlib
@@ -47,6 +49,7 @@ Requires-Dist: matplotlib>=3.8.1; extra == 'matplotlib'
47
49
  Provides-Extra: plotly
48
50
  Requires-Dist: plotly>=5.18.0; extra == 'plotly'
49
51
  Provides-Extra: vispy
52
+ Requires-Dist: pyopengl>=3.1.0; (platform_system == 'Darwin') and extra == 'vispy'
50
53
  Requires-Dist: vispy>=0.14.1; extra == 'vispy'
51
54
  Provides-Extra: vispy-backend
52
55
  Requires-Dist: pyqt6>=6.6.1; extra == 'vispy-backend'
@@ -105,12 +108,18 @@ but performant program. It also heavily uses the capabilities brought by
105
108
 
106
109
  To run build this package locally, you need:
107
110
 
108
- - [Python 3.9](https://www.python.org/) or above;
111
+ - [Python 3.10](https://www.python.org/) or above;
109
112
  - [Rust](https://www.rust-lang.org/) stable toolchain;
110
- - `make` (e.g., GNU Make or Make for Windows);
113
+ - any modern C compiler;
114
+ - [just](https://github.com/casey/just) to easily run commands listed in `justfile`s;
111
115
  - [Maturin](https://www.maturin.rs/) for building Python bindings from Rust code;
112
- - and [Rye](https://rye.astral.sh/) to manage this project.
116
+ - and [uv](https://docs.astral.sh/uv/) to manage this project.
113
117
 
118
+ This project contains `justfile`s with recipes[^1] for most common
119
+ use cases, so feel free to use them instead of the commands listed below/
120
+
121
+ [^1]: `just` is as alternative tool to Make, that provides more modern
122
+ user experience. Enter `just` to list all available recipes.
114
123
 
115
124
  ## Local development
116
125
 
@@ -118,7 +127,7 @@ The following commands assume that you installed
118
127
  the project locally with:
119
128
 
120
129
  ```bash
121
- rye sync
130
+ uv sync
122
131
  ```
123
132
 
124
133
  and that you activated the corresponding Python virtual environment:
@@ -132,11 +141,12 @@ and that you activated the corresponding Python virtual environment:
132
141
  To generate the documentation, please run the following:
133
142
 
134
143
  ```bash
135
- cd docs
136
- make html
144
+ just docs/build
137
145
  ```
138
146
 
139
- Finally, you can open `build/html/index.html` to see the generated docs.
147
+ Finally, you can open `docs/build/html/index.html` to see the generated docs.
148
+
149
+ Other recipes are available, and you can list them with `just docs/`.
140
150
 
141
151
  ### Testing
142
152
 
@@ -144,7 +154,7 @@ Both Rust and Python codebases have their own tests and benchmarks.
144
154
 
145
155
  #### Testing Rust code
146
156
 
147
- You can very easily test you code using Cargo:
157
+ You can test Rust code using Cargo:
148
158
 
149
159
  ```bash
150
160
  cargo test
@@ -158,7 +168,7 @@ cargo bench
158
168
 
159
169
  #### Testing Python code
160
170
 
161
- in the same way, you can very test you code with Pytest:
171
+ Similarly, you can test Python code with Pytest:
162
172
 
163
173
  ```bash
164
174
  pytest
@@ -167,7 +177,7 @@ pytest
167
177
  or benchmark it:
168
178
 
169
179
  ```bash
170
- pytest --benchmark-only
180
+ pytest --benchmark-enable differt/tests/benchmarks
171
181
  ```
172
182
 
173
183
  [pypi-version-badge]: https://img.shields.io/pypi/v/DiffeRT?label=DiffeRT&color=blueviolet
@@ -6,7 +6,6 @@ requires = ["hatchling", "hatch-fancy-pypi-readme"]
6
6
  authors = [{name = "Jérome Eertmans", email = "jeertmans@icloud.com"}]
7
7
  classifiers = [
8
8
  "Programming Language :: Python :: 3",
9
- "Programming Language :: Python :: 3.9",
10
9
  "Programming Language :: Python :: 3.10",
11
10
  "Programming Language :: Python :: 3.11",
12
11
  "Programming Language :: Python :: 3.12",
@@ -16,14 +15,16 @@ classifiers = [
16
15
  ]
17
16
  dependencies = [
18
17
  "beartype>=0.17.2",
19
- "differt-core==0.0.16",
20
- "equinox>=0.11.2",
21
- "jax>=0.4.20",
18
+ "chex>=0.1.86",
19
+ "differt-core==0.0.18",
20
+ "equinox>=0.11.5",
21
+ "filelock>=3.15.4",
22
+ "jax>=0.4.32",
22
23
  "jaxtyping>=0.2.24",
23
24
  "numpy>=1.26.1",
24
25
  "optax>=0.1.7",
25
- "typing-extensions>=4.9.0;python_version < '3.11'",
26
- "requests>=2.31.0",
26
+ "typing-extensions>=4.9.0;python_version<'3.11'",
27
+ "requests>=2.32.0",
27
28
  "tqdm>=4.66.2",
28
29
  ]
29
30
  description = "Differentiable Ray Tracing Toolbox for Radio Propagation Simulations"
@@ -31,18 +32,28 @@ dynamic = ["readme", "version"]
31
32
  keywords = ["ray tracing", "differentiable", "propagation", "radio", "jax"]
32
33
  license = "MIT"
33
34
  name = "DiffeRT"
34
- requires-python = ">=3.9"
35
+ requires-python = ">=3.10"
35
36
 
36
37
  [project.optional-dependencies]
37
38
  all = [
38
39
  "differt[jupyter,matplotlib,plotly,vispy,vispy-backend]",
39
40
  ]
40
- jupyter = ["notebook>5", "jupyter-rfb>=0.4.2", "ipympl"]
41
+ jupyter = ["notebook>5", "jupyter-rfb>=0.4.2", "ipympl>=0.9.4"]
41
42
  matplotlib = ["matplotlib>=3.8.1"]
42
43
  plotly = ["plotly>=5.18.0"]
43
- vispy = ["vispy>=0.14.1"]
44
+ vispy = [
45
+ "vispy>=0.14.1",
46
+ # PyOpenGL seems to be needed, see:
47
+ # https://github.com/vispy/vispy/issues/2547
48
+ # https://github.com/vispy/vispy/pull/2549
49
+ # https://github.com/bitcraze/crazyflie-clients-python/pull/708
50
+ "PyOpenGL>=3.1.0;platform_system=='Darwin'",
51
+ ]
44
52
  vispy-backend = ["PyQt6>=6.6.1"]
45
53
 
54
+ [project.scripts]
55
+ "download-sionna-scenes" = "differt.scene.sionna:download_sionna_scenes"
56
+
46
57
  [project.urls]
47
58
  Changelog = "https://github.com/jeertmans/DiffeRT/releases"
48
59
  Documentation = "https://eertmans.be/DiffeRT"
@@ -83,5 +94,5 @@ replacement = '(https://github.com/jeertmans/DiffeRT/blob/main/\1)'
83
94
  path = "../differt-core/Cargo.toml"
84
95
  pattern = '^version = "(?P<version>[^"]+)'
85
96
 
86
- [tool.rye]
87
- managed = true
97
+ [tool.uv.sources]
98
+ differt_core = {workspace = true}
@@ -1,6 +1,6 @@
1
1
  """Physical constants used for EM fields computation."""
2
2
 
3
- __all__ = ("c", "mu_0", "epsilon_0")
3
+ __all__ = ("c", "epsilon_0", "mu_0")
4
4
 
5
5
  c = 299792458.0
6
6
  """The speed of light in vacuum."""
@@ -1,15 +1,15 @@
1
1
  """
2
2
  Special functions.
3
3
 
4
- This module extends the :py:mod:`jax.scipy.special` module
5
- by adding missing function from :py:mod:`scipy.special`,
4
+ This module extends the :mod:`jax.scipy.special` module
5
+ by adding missing function from :mod:`scipy.special`,
6
6
  or by extending already implemented function to the
7
7
  complex domain.
8
8
 
9
9
  Those new implementation are needed to keep the ability
10
10
  of differentating code, otherwise we could just
11
11
  call the SciPy function and wrap their output
12
- with :py:func:`jnp.asarray<jax.numpy.asarray>`.
12
+ with :func:`jnp.asarray<jax.numpy.asarray>`.
13
13
  """
14
14
 
15
15
  import jax
@@ -26,7 +26,7 @@ def erf(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
26
26
  Evaluate the error function at the given points.
27
27
 
28
28
  The current implementation is written using
29
- the real-valued error function :py:func:`jax.scipy.special.erf`
29
+ the real-valued error function :func:`jax.scipy.special.erf`
30
30
  and the approximation as detailed in :cite:`erf-complex`.
31
31
 
32
32
  The output type (real or complex) is determined by the
@@ -34,27 +34,27 @@ def erf(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
34
34
 
35
35
  Warning:
36
36
  Currently, we observe that
37
- this function and :py:data:`scipy.special.erf`
37
+ this function and :data:`scipy.special.erf`
38
38
  starts to diverge for :math:`|z| > 6`. If you know
39
39
  how to avoid this problem, please contact us!
40
40
 
41
41
  Args:
42
42
  z: The array of real or complex points to evaluate.
43
43
 
44
- Return:
44
+ Returns:
45
45
  The values of the error function at the given point.
46
46
 
47
47
  Notes:
48
48
  Regarding performances, there are two possible outputs:
49
49
 
50
50
  1. If ``z`` is real, then this function compiles to
51
- :py:func:`jax.scipy.special.erf`, and will therefore
51
+ :func:`jax.scipy.special.erf`, and will therefore
52
52
  have the same performances (when JIT compilation
53
53
  is done). Compared to the SciPy equivalent, we measured
54
54
  that our implementation is **~ 10 times faster**.
55
55
  2. If ``z`` is complex, then our implementation is
56
56
  **~ 3 times faster** than
57
- :py:data:`scipy.special.erf`.
57
+ :data:`scipy.special.erf`.
58
58
 
59
59
  Those results were measured on centered random uniform arrays
60
60
  with :math:`10^5` elements.
@@ -103,9 +103,9 @@ def erf(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
103
103
  ... ) # doctest: +SKIP
104
104
  >>> fig # doctest: +SKIP
105
105
  """
106
+ # TODO: remove this function as it is not needed anymore
106
107
  if jnp.issubdtype(z.dtype, jnp.floating):
107
108
  return erfx(z)
108
- # https://granite.phys.s.u-tokyo.ac.jp/svn/LCGT/trunk/sensitivity/Matlab/bKAGRA/@double/erfz.pdf
109
109
 
110
110
  if jnp.issubdtype(z.dtype, jnp.complex128): # double precision
111
111
  N = 13 # noqa: N806
@@ -155,8 +155,7 @@ def erf(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
155
155
  h = exp_r_squared * h_sum / (2 * jnp.pi)
156
156
 
157
157
  res = erfx(r) + e + f - exp_2j_r_i * (g + h)
158
- res = jnp.where(z.imag < 0, jnp.conj(res), res)
159
- return res
158
+ return jnp.where(z.imag < 0, jnp.conj(res), res)
160
159
 
161
160
 
162
161
  @jax.jit
@@ -168,12 +167,12 @@ def erfc(z: Inexact[Array, " *batch"]) -> Inexact[Array, " *batch"]:
168
167
  The output type (real or complex) is determined by the
169
168
  input type.
170
169
 
171
- See :py:func:`erf` for more details.
170
+ See :func:`erf` for more details.
172
171
 
173
172
  Args:
174
173
  z: The array of real or complex points to evaluate.
175
174
 
176
- Return:
175
+ Returns:
177
176
  The values of the complementary error function at the given point.
178
177
 
179
178
  Examples:
@@ -201,7 +200,7 @@ def fresnel(
201
200
  Evaluate the two Fresnel integrals at the given points.
202
201
 
203
202
  This current implementation is written using
204
- the error function :py:func:`erf<differt.em.special.erf>`
203
+ the error function :func:`erf<differt.em.special.erf>`
205
204
  see :cite:`fresnel-integrals`.
206
205
 
207
206
  The output type (real or complex) is determined by the
@@ -210,7 +209,7 @@ def fresnel(
210
209
  Args:
211
210
  z: The array of real or complex points to evaluate.
212
211
 
213
- Return:
212
+ Returns:
214
213
  A tuple of two arrays, one for each of the Fresnel integrals.
215
214
 
216
215
  Examples:
@@ -32,7 +32,7 @@ def F(z: Inexact[Array, " *batch"]) -> Complex[Array, " *batch"]: # noqa: N802
32
32
  C(z) - j S(z) = \int\limits_\sqrt{z}^\infty e^{-j u^2} \text{d}u.
33
33
 
34
34
  Because JAX does not provide a XLA implementation of
35
- :py:data:`scipy.special.fresnel`, this implementation relies on a
35
+ :data:`scipy.special.fresnel`, this implementation relies on a
36
36
  custom complex-valued implementation of the error function and
37
37
  the fact that:
38
38
 
@@ -49,7 +49,7 @@ def F(z: Inexact[Array, " *batch"]) -> Complex[Array, " *batch"]: # noqa: N802
49
49
  Args:
50
50
  z: The array of real or complex points to evaluate.
51
51
 
52
- Return:
52
+ Returns:
53
53
  The values of the transition function at the given point.
54
54
 
55
55
  Examples:
@@ -1,12 +1,13 @@
1
1
  """Utilities for working with EM fields."""
2
2
 
3
- from typing import Any, Union
3
+ from typing import Any
4
4
 
5
5
  import jax
6
6
  from beartype import beartype as typechecker
7
- from jaxtyping import Array, Float, jaxtyped
7
+ from jaxtyping import Array, ArrayLike, Float, jaxtyped
8
+
9
+ from differt.geometry.utils import path_lengths
8
10
 
9
- from ..geometry.utils import path_lengths
10
11
  from .constants import c
11
12
 
12
13
 
@@ -14,7 +15,7 @@ from .constants import c
14
15
  @jaxtyped(typechecker=typechecker)
15
16
  def lengths_to_delays(
16
17
  lengths: Float[Array, " *#batch"],
17
- speed: Union[float, Float[Array, " *#batch"]] = c,
18
+ speed: Float[ArrayLike, " *#batch"] = c,
18
19
  ) -> Float[Array, " *#batch"]:
19
20
  """
20
21
  Compute the delay, in seconds, corresponding to each length.
@@ -26,7 +27,7 @@ def lengths_to_delays(
26
27
  an array of speeds. Default is the speed
27
28
  of light in vacuum.
28
29
 
29
- Return:
30
+ Returns:
30
31
  The array of path delays.
31
32
 
32
33
  Examples:
@@ -49,7 +50,8 @@ def lengths_to_delays(
49
50
  @jax.jit
50
51
  @jaxtyped(typechecker=typechecker)
51
52
  def path_delays(
52
- paths: Float[Array, "*batch path_length 3"], **kwargs: Any
53
+ paths: Float[Array, "*batch path_length 3"],
54
+ **kwargs: Any,
53
55
  ) -> Float[Array, " *batch"]:
54
56
  """
55
57
  Compute the path delay, in seconds, of each path.
@@ -61,7 +63,7 @@ def path_delays(
61
63
  kwargs: Keyword arguments passed to
62
64
  :func:`lengths_to_delays`.
63
65
 
64
- Return:
66
+ Returns:
65
67
  The array of path delays.
66
68
 
67
69
  Examples:
@@ -0,0 +1,187 @@
1
+ """Ray paths utilities."""
2
+
3
+ from collections.abc import Callable, Iterator
4
+ from typing import Any
5
+
6
+ import equinox as eqx
7
+ import jax
8
+ import jax.numpy as jnp
9
+ import numpy as np
10
+ from beartype import beartype as typechecker
11
+ from jaxtyping import Array, ArrayLike, Bool, Float, Int, jaxtyped
12
+
13
+ from differt.plotting import draw_paths
14
+
15
+
16
+ @jaxtyped(typechecker=typechecker)
17
+ class Paths(eqx.Module):
18
+ """
19
+ A convenient wrapper class around path vertices and object indices.
20
+
21
+ This class can hold arbitrary many paths, but they must share the same
22
+ length, i.e., the same number of vertices per path.
23
+ """
24
+
25
+ vertices: Float[Array, "*batch path_length 3"] = eqx.field(converter=jnp.asarray)
26
+ """The array of path vertices."""
27
+ objects: Int[Array, "*batch path_length"] = eqx.field(converter=jnp.asarray)
28
+ """The array of object indices.
29
+
30
+ To every path vertex corresponds one object (e.g., a triangle).
31
+ A placeholder value of ``-1`` can be used in specific cases,
32
+ like for transmitter and receiver positions.
33
+ """
34
+ mask: Bool[Array, " *batch"] | None = eqx.field(
35
+ converter=lambda x: jnp.asarray(x) if x is not None else None, default=None
36
+ )
37
+ """An optional mask to indicate which paths are valid and should be used.
38
+
39
+ The mask is kept separately to :attr:`vertices` so that we can keep information
40
+ batch ``*batch`` dimensions, which would not be possible if we were to directly
41
+ store valid paths.
42
+ """
43
+
44
+ @property
45
+ @jaxtyped(typechecker=typechecker)
46
+ def path_length(self) -> int:
47
+ """The length (i.e., number of vertices) of each individual path."""
48
+ return self.objects.shape[-1]
49
+
50
+ @property
51
+ @jax.jit
52
+ @jaxtyped(typechecker=typechecker)
53
+ def num_valid_paths(self) -> Int[ArrayLike, ""]:
54
+ """The number of paths kept by :attr:`mask`.
55
+
56
+ If :attr:`mask` is not :data:`None`, then the output value can be traced by JAX.
57
+ """
58
+ if self.mask is not None:
59
+ return self.mask.sum()
60
+ return self.objects[..., 0].size
61
+
62
+ @property
63
+ @jaxtyped(typechecker=typechecker)
64
+ def masked_vertices(
65
+ self,
66
+ ) -> Float[Array, "{self.num_valid_paths} {self.path_length} 3"]:
67
+ """The array of masked vertices, with batched dimensions flattened into one.
68
+
69
+ If :attr:`mask` is :data:`None`, then the returned array is simply
70
+ :attr:`vertices` with the batch dimensions flattened.
71
+ """
72
+ vertices = self.vertices.reshape((-1, self.path_length, 3))
73
+ if self.mask is not None:
74
+ mask = self.mask.reshape(-1)
75
+ return vertices[mask, ...]
76
+ return vertices
77
+
78
+ @property
79
+ @jaxtyped(typechecker=typechecker)
80
+ def masked_objects(
81
+ self,
82
+ ) -> Int[Array, "{self.num_valid_paths} {self.path_length}"]:
83
+ """The array of masked objects, with batched dimensions flattened into one.
84
+
85
+ Similar to :attr:`masked_vertices`, but for :data:`objects`.
86
+ """
87
+ objects = self.objects.reshape((-1, self.path_length))
88
+ if self.mask is not None:
89
+ mask = self.mask.reshape(-1)
90
+ return objects[mask, ...]
91
+ return objects
92
+
93
+ @jax.jit
94
+ @jaxtyped(typechecker=typechecker)
95
+ def group_by_objects(self) -> Int[Array, " *batch"]:
96
+ """
97
+ Return an array of unique object groups.
98
+
99
+ This function is useful to group paths that
100
+ undergo the same types of interactions.
101
+
102
+ Returns:
103
+ An array of group indices.
104
+
105
+ Examples:
106
+ The following shows how one can group
107
+ paths by object groups. There are two different objects,
108
+ denoted by indices ``0`` and ``1``, and each path is made
109
+ of three vertices.
110
+
111
+ >>> from differt.geometry.paths import Paths
112
+ >>>
113
+ >>> key = jax.random.PRNGKey(1234)
114
+ >>> key_v, key_o = jax.random.split(key, 2)
115
+ >>> *batch, path_length = (2, 6, 3)
116
+ >>> vertices = jax.random.uniform(key_v, (*batch, path_length, 3))
117
+ >>> objects = jax.random.randint(key_o, (*batch, path_length), 0, 2)
118
+ >>> objects
119
+ Array([[[1, 0, 0],
120
+ [1, 0, 0],
121
+ [0, 1, 1],
122
+ [1, 0, 1],
123
+ [0, 1, 0],
124
+ [0, 1, 1]],
125
+ [[1, 0, 1],
126
+ [0, 0, 0],
127
+ [0, 0, 0],
128
+ [1, 1, 0],
129
+ [0, 0, 1],
130
+ [0, 1, 1]]], dtype=int32)
131
+ >>> paths = Paths(vertices, objects)
132
+ >>> groups = paths.group_by_objects()
133
+ >>> groups
134
+ Array([[4, 4, 3, 5, 2, 3],
135
+ [5, 0, 0, 6, 1, 3]], dtype=int32)
136
+ """
137
+ *batch, path_length = self.objects.shape
138
+
139
+ objects = self.objects.reshape((-1, path_length))
140
+ inverse = jnp.unique(
141
+ objects, axis=0, size=objects.shape[0], return_inverse=True
142
+ )[1]
143
+
144
+ return inverse.reshape(batch)
145
+
146
+ def __iter__(self) -> Iterator["Paths"]:
147
+ """Return an iterator over masked paths.
148
+
149
+ Each item of the iterator is itself an instance :class:`Paths`,
150
+ so you can still benefit from convenient methods like :meth:`plot`.
151
+
152
+ Yields:
153
+ Masked paths, one at a time.
154
+ """
155
+ for vertices, objects in zip(
156
+ self.masked_vertices, self.masked_objects, strict=False
157
+ ):
158
+ yield Paths(vertices=vertices, objects=objects, mask=None)
159
+
160
+ @eqx.filter_jit
161
+ @jaxtyped(typechecker=typechecker)
162
+ def reduce(
163
+ self, fun: Callable[[Float[Array, "*batch n"]], Float[Array, " *batch"]]
164
+ ) -> Float[Array, " "]:
165
+ """Apply a function on all paths and accumulate the result into a scalar value.
166
+
167
+ Args:
168
+ fun: The function to be called on all path vertices.
169
+
170
+ Returns:
171
+ The sum of the results, with contributions from
172
+ invalid paths that are set to zero.
173
+ """
174
+ return jnp.sum(fun(self.vertices), where=self.mask)
175
+
176
+ def plot(self, **kwargs: Any) -> Any:
177
+ """
178
+ Plot the (masked) paths on a 3D scene.
179
+
180
+ Args:
181
+ kwargs: Keyword arguments passed to
182
+ :func:`draw_paths<differt.plotting.draw_paths>`.
183
+
184
+ Returns:
185
+ The resulting plot output.
186
+ """
187
+ return draw_paths(np.asarray(self.masked_vertices), **kwargs)