Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add viewerplot element #3129

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
100 changes: 99 additions & 1 deletion nerfstudio/viewer/viewer_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
GuiButtonHandle,
GuiDropdownHandle,
GuiInputHandle,
GuiPlotlyHandle,
ScenePointerEvent,
ViserServer,
)
Expand All @@ -43,6 +44,8 @@
if TYPE_CHECKING:
from nerfstudio.viewer.viewer import Viewer

import plotly.graph_objects as go

TValue = TypeVar("TValue")
TString = TypeVar("TString", default=str, bound=str)

Expand Down Expand Up @@ -284,7 +287,9 @@ def __init__(
cb_hook: Callable = lambda element: None,
) -> None:
self.name = name
self.gui_handle: Optional[Union[GuiInputHandle[TValue], GuiButtonHandle, GuiButtonGroupHandle]] = None
self.gui_handle: Optional[
Union[GuiInputHandle[TValue], GuiButtonHandle, GuiButtonGroupHandle, GuiPlotlyHandle]
] = None
self.disabled = disabled
self.visible = visible
self.cb_hook = cb_hook
Expand Down Expand Up @@ -710,3 +715,96 @@ def _create_gui_handle(self, viser_server: ViserServer) -> None:
self.gui_handle = viser_server.add_gui_vector3(
self.name, self.default_value, step=self.step, disabled=self.disabled, visible=self.visible, hint=self.hint
)


class ViewerPlot(ViewerElement[go.Figure]):
"""Base class for viewer figures, using plotly backend.
Includes misc wrapper methods for setting plotly figure properties.
"""
gui_handle: GuiPlotlyHandle

_figure: go.Figure
"""Figure to be displayed. Do not access this directly, exists only for initial statekeeping."""
_aspect: float
"""Aspect ratio of the plot (h/w). Default is 1.0."""

def __init__(
self,
figure: Optional[go.Figure] = None,
aspect: float = 1.0,
visible: bool = True,
):
"""
Args:
- figure: The plotly figure to display -- if None, an empty figure is created.
- aspect: Aspect ratio of the plot (h/w). Default is 1.0.
- visible: If the plot is visible.
"""
self._figure = go.Figure() if figure is None else figure
self._aspect = aspect
super().__init__(name="", visible=visible) # plots have no name.

def _create_gui_handle(self, viser_server: ViserServer) -> None:
self.gui_handle = viser_server.add_gui_plotly(
figure=self._figure, visible=self.visible, aspect=self._aspect
)

def install(self, viser_server: ViserServer) -> None:
self._create_gui_handle(viser_server)
assert self.gui_handle is not None

@property
def figure(self):
assert self.gui_handle is not None
return self.gui_handle.figure

@figure.setter
def figure(self, figure: go.Figure):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to automatically apply the dark mode+aspect to new figures as well, so that the user doesn't have to call dark_mode and aspect every time they update/change something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aspect doesn't need to be set for new figures, but you're right about dark_mode. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be addr'ed in ca04cbc.

assert self.gui_handle is not None
self._figure = figure
self.gui_handle.figure = figure

@property
def aspect(self):
return self._aspect

@aspect.setter
def aspect(self, aspect: float):
self._aspect = aspect
if self.gui_handle is not None:
self.gui_handle.aspect = aspect

@staticmethod
def set_margin(figure: go.Figure, margin: int = 0) -> None:
"""Wrapper for setting the margin of a plotly figure."""
# Set margins.
figure.update_layout(
margin=dict(l=margin, r=margin, t=margin, b=margin),
)

# Set automargin for title, so that title doesn't get cut off.
if margin == 0 and getattr(figure.layout, "title", None) is not None:
figure.layout.title.automargin = True # type: ignore

@staticmethod
def set_dark(figure: go.Figure, dark: bool) -> None:
"""Wrapper for setting the dark mode of a plotly figure."""
if dark:
figure.update_layout(template="plotly_dark")
else:
figure.update_layout(template="plotly")

@staticmethod
def plot_line(x: np.ndarray, y: np.ndarray, name: str = "", color: str = "blue") -> go.Scatter:
"""Wrapper for plotting a line in a plotly figure."""
return go.Scatter(x=x, y=y, mode="lines", name=name, line=dict(color=color))

@staticmethod
def plot_scatter(x: np.ndarray, y: np.ndarray, name: str = "", color: str = "blue") -> go.Scatter:
"""Wrapper for plotting a scatter in a plotly figure."""
return go.Scatter(x=x, y=y, mode="markers", name=name, marker=dict(color=color))

@staticmethod
def plot_image(image: np.ndarray, name: str = "") -> go.Image:
"""Wrapper for plotting an image in a plotly figure."""
return go.Image(z=image, name=name)
Loading