Skip to content

Commit

Permalink
ruff is happy
Browse files Browse the repository at this point in the history
  • Loading branch information
jeertmans committed Jan 4, 2024
1 parent cc79e6e commit 73e1032
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 6,763 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
# -- MyST-nb settings

nb_kernel_rgx_aliases = {".*": "DiffeRT"} # TODO: do not require specific kernel name
nb_merge_streams = True

# By default, MyST-nb chooses the Widget output instead of the 2D snapshot
# so we need to change priorities, because the widget cannot work if Python is
Expand Down
7 changes: 3 additions & 4 deletions docs/source/notebooks/advanced_path_tracing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
"source": [
"from pathlib import Path\n",
"\n",
"import numpy as np\n",
"import jax.numpy as jnp\n",
"import plotly.graph_objects as go\n",
"import numpy as np\n",
"\n",
"import differt.plotting as dplt\n",
"from differt.geometry import TriangleMesh\n",
"from differt.geometry.triangle_mesh import (\n",
" triangles_contain_vertices_assuming_inside_same_plane,\n",
")\n",
"from differt.rt.image_method import (\n",
" image_method,\n",
" consecutive_vertices_are_on_same_side_of_mirrors,\n",
" image_method,\n",
")\n",
"from differt.rt.utils import generate_all_path_candidates, rays_intersect_triangles"
]
Expand Down Expand Up @@ -110,7 +109,7 @@
" t, hit = rays_intersect_triangles(\n",
" ray_origins,\n",
" ray_directions,\n",
" jnp.broadcast_to(all_triangle_vertices, ray_origins.shape + (3,)),\n",
" jnp.broadcast_to(all_triangle_vertices, (*ray_origins.shape, 3)),\n",
" )\n",
" intersect = (t < 0.999) & hit\n",
" intersect = jnp.any(intersect, axis=(0, 2))\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/source/notebooks/plotting_backend.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"source": [
"%matplotlib widget\n",
"\n",
"mesh.plot(backend=\"matplotlib\", alpha=0.5, shade=True, linewidth=0.2, antialiased=True);"
"mesh.plot(backend=\"matplotlib\", alpha=0.5, shade=True, linewidth=0.2, antialiased=True)"
]
},
{
Expand Down
6,750 changes: 14 additions & 6,736 deletions docs/source/notebooks/quickstart.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 31 additions & 12 deletions python/differt/plotting/_core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""
Core plotting implementations.
"""
"""Core plotting implementations."""
from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any

import numpy as np
Expand Down Expand Up @@ -31,7 +29,7 @@ def draw_mesh(
Args:
vertices: The array of triangle vertices.
faces: The array of triangle indices.
triangles: The array of triangle indices.
kwargs: Keyword arguments passed to
:py:class:`Mesh<vispy.scene.visuals.Mesh>`,
:py:meth:`plot_trisurf<mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf>`,
Expand Down Expand Up @@ -77,13 +75,18 @@ def _(vertices, triangles, *args, **kwargs):

@dispatch
def draw_paths(
paths: Float[np.ndarray, "*batch path_length 3"], **kwargs
paths: Float[np.ndarray, "*batch path_length 3"], **kwargs: Any
) -> ReturnType:
"""
Plot a batch of paths of the same length.
Args:
paths: The array of path vertices.
kwargs: Keyword arguments passed to
:py:class:`LinePlot<vispy.scene.visuals.LinePlot>`,
:py:meth:`plot<mpl_toolkits.mplot3d.axes3d.Axes3D.plot>`,
or :py:class:`Scatter3d<plotly.graph_objects.Scatter3d>`, depending on the
backend.
Returns:
The resulting plot output.
Expand Down Expand Up @@ -129,50 +132,66 @@ def _(paths, *args, **kwargs): # type: ignore[no-untyped-def]
def draw_markers(
markers: Float[np.ndarray, "num_markers 3"],
labels: Sequence[str] | None = None,
**kwargs,
text_kwargs: Mapping[str, Any] | None = None,
**kwargs: Any,
) -> ReturnType:
"""
Plot markers and, optionally, their label.
Args:
markers: The array of marker vertices.
labels: The marker labels.
text_kwargs: A mapping of keyword arguments
to be passed to :py:class:`Text<vispy.scene.visuals.Text>`
if VisPy backend is used.
By default, ``font_sise=1000`` is used.
kwargs: Keyword arguments passed to
:py:class:`Markers<vispy.scene.visuals.Markers>`,
or :py:class:`Scatter3d<plotly.graph_objects.Scatter3d>`, depending on the
backend.
Returns:
The resulting plot output.
Warning:
Unsupported backend(s): Matplotlib.
"""


@draw_markers.register("vispy")
def _(markers, labels=None, **kwargs): # type: ignore[no-untyped-def]
def _(markers, labels=None, text_kwargs=None, **kwargs): # type: ignore[no-untyped-def]
from vispy.scene.visuals import Markers, Text

canvas, view = process_vispy_kwargs(kwargs)
view.add(Markers(pos=markers, **kwargs))

if labels:
view.add(Text(text=labels, font_size=1000, pos=markers, **kwargs))
text_kwargs = {"font_size": 1000, **text_kwargs}
view.add(Text(text=labels, pos=markers, **text_kwargs))

view.camera.set_range()

return canvas


@draw_markers.register("matplotlib")
def _(markers, labels=None, **kwargs): # type: ignore[no-untyped-def]
def _(markers, labels=None, text_kwargs=None, **kwargs): # type: ignore[no-untyped-def]
raise NotImplementedError # TODO


@draw_markers.register("plotly")
def _(markers, labels=None, mode=None, **kwargs): # type: ignore[no-untyped-def]
def _(markers, labels=None, text_kwargs=None, **kwargs): # type: ignore[no-untyped-def]
fig = process_plotly_kwargs(kwargs)

if labels:
kwargs = {"mode": "markers+text", **kwargs}

x, y, z = markers.T
return fig.add_scatter3d(
x=x,
y=y,
z=z,
mode=mode or ("markers+text" if labels else None),
text=labels,
**kwargs,
)
4 changes: 1 addition & 3 deletions python/differt/plotting/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Useful decorators for plotting.
"""
"""Useful decorators for plotting."""

from __future__ import annotations

Expand Down
8 changes: 2 additions & 6 deletions python/differt/rt/image_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ def image_method(

@jaxtyped(typechecker=typechecker)
def forward(carry: T, x: tuple[T, T]) -> tuple[T, T]:
"""
Perform forward pass on vertices by computing consecutive images.
"""
"""Perform forward pass on vertices by computing consecutive images."""
vertices = carry
mirror_vertices, mirror_normals = x
images = image_of_vertices_with_respect_to_mirrors(
Expand All @@ -164,9 +162,7 @@ def forward(carry: T, x: tuple[T, T]) -> tuple[T, T]:

@jaxtyped(typechecker=typechecker)
def backward(carry: T, x: tuple[T, T, T]) -> tuple[T, T]:
"""
Perform backward pass on images by computing the intersection with mirrors.
"""
"""Perform backward pass on images by computing the intersection with mirrors."""
vertices = carry
mirror_vertices, mirror_normals, images = x

Expand Down

0 comments on commit 73e1032

Please sign in to comment.