Skip to content

Commit d08d8b8

Browse files
ConchylicultorThe visu3d Authors
authored andcommitted
Allow to globally configure the default number of point displayed
PiperOrigin-RevId: 481617719
1 parent de187ff commit d08d8b8

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Changelog follow https://keepachangelog.com/ format.
2323
`points.replace_fig_config(num_samples=10_000)`.
2424
* Object names displayed in plotly are customizable using
2525
`points.replace_fig_config(name='My point cloud')`.
26+
* `v3d.fig_config.num_sample_xyz = 123` to overwrite the default number of
27+
sampled rays, points,... (`None` for no subsampling)
2628
* `DataclassArray.__dca_params__` can be set to `v3d.DataclassParams` to
2729
configure the dataclass options.
2830

visu3d/dc_arrays/point.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from visu3d import array_dataclass
2626
from visu3d import plotly
2727
from visu3d.dc_arrays import transformation
28+
from visu3d.plotly import fig_config_utils
2829
from visu3d.utils import np_utils
2930
from visu3d.utils.lazy_imports import plotly_base
3031

@@ -47,7 +48,11 @@ class Point3d(array_dataclass.DataclassArray):
4748

4849
# Overwrite `v3d.DataclassArray.fig_config`.
4950
fig_config: plotly.TraceConfig = dataclasses.field(
50-
default=plotly.TraceConfig(num_samples=10_000),
51+
default=plotly.TraceConfig(
52+
num_samples=fig_config_utils.LazyValue(
53+
lambda: fig_config_utils.fig_config.num_samples_point3d
54+
)
55+
),
5156
repr=False,
5257
init=False,
5358
)
@@ -105,7 +110,11 @@ class Point2d(array_dataclass.DataclassArray):
105110

106111
# Overwrite `v3d.DataclassArray.fig_config`.
107112
fig_config: plotly.TraceConfig = dataclasses.field(
108-
default=plotly.TraceConfig(num_samples=50_000),
113+
default=plotly.TraceConfig(
114+
num_samples=fig_config_utils.LazyValue(
115+
lambda: fig_config_utils.fig_config.num_samples_point2d
116+
)
117+
),
109118
repr=False,
110119
init=False,
111120
)

visu3d/dc_arrays/ray.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from visu3d import array_dataclass
2626
from visu3d import plotly
2727
from visu3d.dc_arrays import transformation
28+
from visu3d.plotly import fig_config_utils
2829
from visu3d.utils import np_utils
2930
from visu3d.utils.lazy_imports import plotly_base
3031

@@ -50,7 +51,11 @@ class Ray(array_dataclass.DataclassArray):
5051

5152
# Overwrite `v3d.DataclassArray.fig_config`.
5253
fig_config: plotly.TraceConfig = dataclasses.field(
53-
default=plotly.TraceConfig(num_samples=500),
54+
default=plotly.TraceConfig(
55+
num_samples=fig_config_utils.LazyValue(
56+
lambda: fig_config_utils.fig_config.num_samples_ray
57+
)
58+
),
5459
repr=False,
5560
init=False,
5661
)

visu3d/plotly/fig_config_utils.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@
1717
from __future__ import annotations
1818

1919
import dataclasses
20-
from typing import Optional, TypeVar
20+
from typing import Callable, Generic, Optional, TypeVar, Union
2121

2222
from etils import edc
2323

2424
_T = TypeVar('_T')
2525

2626

27+
class LazyValue(Generic[_T]):
28+
"""Lazyly compute a value."""
29+
30+
def __init__(self, value_fn: Callable[[], _T]):
31+
self._value_fn = value_fn
32+
33+
@property
34+
def value(self) -> _T:
35+
return self._value_fn()
36+
37+
def __repr__(self) -> str:
38+
return f'{type(self).__name__}({self.value})'
39+
40+
2741
@edc.dataclass
2842
@dataclasses.dataclass
2943
class FigConfig:
@@ -33,14 +47,25 @@ class FigConfig:
3347
3448
```python
3549
v3d.fig_config.show_zero = False
50+
v3dfig_config..num_samples_point3d = None # Do not subsample point display
3651
```
3752
3853
Attributes:
3954
show_zero: Whether to show the `(0, 0, 0)` origin, otherwise the plot x, y,
4055
z axis adapt to the data.
56+
num_samples_point3d: Max number of `v3d.Point3d` displayed by default (
57+
`None` for all)
58+
num_samples_point2d: Max number of `v3d.Point2d` displayed by default (
59+
`None` for all)
60+
num_samples_ray: Max number of `v3d.Ray` displayed by default (`None` for
61+
all)
4162
"""
4263

4364
show_zero: bool = True
65+
# TODO(epot): Currently this does not support `v3d.make_fig(num_samples_ray=)`
66+
num_samples_point3d: Optional[int] = 10_000
67+
num_samples_point2d: Optional[int] = 50_000
68+
num_samples_ray: Optional[int] = 500
4469

4570
def replace(self: _T, **kwargs) -> _T:
4671
return dataclasses.replace(self, **kwargs)
@@ -62,11 +87,9 @@ class TraceConfig:
6287

6388
# NOTE: When adding new properties here, please also update all
6489
# `.replace_fig_config(` function to get type checking/auto-complete.
65-
# TODO(epot): More dynamic sub-sampling controled in `v3d.make_fig` per
66-
# figures (global control)
6790

6891
name: Optional[str] = None
69-
num_samples: int = -1
92+
num_samples: Union[LazyValue[Optional[int]], Optional[int]] = -1
7093

7194
def replace(self: _T, **kwargs) -> _T:
7295
return dataclasses.replace(self, **kwargs)

visu3d/plotly/fig_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,12 @@ def make_traces(
164164
if is_visualizable(val):
165165
if isinstance(val, dca.DataclassArray):
166166
if has_fig_config(val):
167+
num_samples = val.fig_config.num_samples
168+
if isinstance(num_samples, fig_config_utils.LazyValue):
169+
num_samples = num_samples.value
167170
val = math.subsample(
168171
val,
169-
num_samples=val.fig_config.num_samples,
172+
num_samples=num_samples,
170173
# TODO(epot): Should likely make the seed depend on other
171174
# factors like class name, position in make_traces *args,...
172175
seed=0,

0 commit comments

Comments
 (0)