Skip to content

Commit

Permalink
Support lens setting in python layout API
Browse files Browse the repository at this point in the history
  • Loading branch information
druzsan committed Nov 20, 2023
1 parent e6e832d commit 9e73a80
Showing 1 changed file with 73 additions and 18 deletions.
91 changes: 73 additions & 18 deletions renumics/spotlight/layout/lenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
For usage examples, see `renumics.spotlight.layout.inspector`.
"""

from typing_extensions import Literal
import uuid
from typing import List, Optional, Union

Expand Down Expand Up @@ -37,11 +38,15 @@ class Lens(BaseModel, populate_by_name=True):
type: str = Field(..., alias="view")
columns: List[str] = Field(..., alias="columns")
name: Optional[str] = Field(None, alias="name")
settings: Optional[dict] = None
id: Optional[str] = Field(default_factory=lambda: str(uuid.uuid4()), alias="key")


def lens(
internal_type: str, columns: Union[str, List[str]], name: Optional[str] = None
internal_type: str,
columns: Union[str, List[str]],
name: Optional[str] = None,
settings: Optional[dict] = None,
) -> Lens:
"""
Add a viewer to Spotlight inspector widget.
Expand All @@ -52,9 +57,10 @@ def lens(
Prefer to use explicit lens functions defined below.
"""
return Lens(
type=internal_type, # type: ignore
type=internal_type,
columns=[columns] if isinstance(columns, str) else columns,
name=name,
settings=settings,
)


Expand All @@ -65,7 +71,7 @@ def scalar(column: str, name: Optional[str] = None) -> Lens:
Supports a single column of type `bool`, `int`, `float`, `str`,
`datetime.datetime` or `spotlight.Category`.
"""
return Lens(type="ScalarView", columns=[column], name=name) # type: ignore
return Lens(type="ScalarView", columns=[column], name=name)


def text(column: str, name: Optional[str] = None) -> Lens:
Expand All @@ -74,7 +80,7 @@ def text(column: str, name: Optional[str] = None) -> Lens:
Supports a single column of type `str`.
"""
return Lens(type="TextLens", columns=[column], name=name) # type: ignore
return Lens(type="TextLens", columns=[column], name=name)


def html(column: str, name: Optional[str] = None, unsafe: bool = False) -> Lens:
Expand All @@ -84,8 +90,8 @@ def html(column: str, name: Optional[str] = None, unsafe: bool = False) -> Lens:
Supports a single column of type `str`.
"""
if unsafe:
return Lens(type="HtmlLens", columns=[column], name=name) # type: ignore
return Lens(type="SafeHtmlLens", columns=[column], name=name) # type: ignore
return Lens(type="HtmlLens", columns=[column], name=name)
return Lens(type="SafeHtmlLens", columns=[column], name=name)


def markdown(column: str, name: Optional[str] = None) -> Lens:
Expand All @@ -94,7 +100,7 @@ def markdown(column: str, name: Optional[str] = None) -> Lens:
Supports a single column of type `str`.
"""
return Lens(type="MarkdownLens", columns=[column], name=name) # type: ignore
return Lens(type="MarkdownLens", columns=[column], name=name)


def array(column: str, name: Optional[str] = None) -> Lens:
Expand All @@ -104,29 +110,62 @@ def array(column: str, name: Optional[str] = None) -> Lens:
Supports a single column of type `np.ndarray`, `spotlight.Embedding` or
`spotlight.Window`.
"""
return Lens(type="ArrayLens", columns=[column], name=name) # type: ignore
return Lens(type="ArrayLens", columns=[column], name=name)


def sequences(columns: Union[str, List[str]], name: Optional[str] = None) -> Lens:
def sequences(
columns: Union[str, List[str]],
name: Optional[str] = None,
*,
multiple_y_axes: bool = False,
) -> Lens:
"""
Add sequence viewer to Spotlight inspector widget.
Supports one or multiple columns of type `spotlight.Sequence1D`.
"""
return Lens(
type="SequenceView", # type: ignore
type="SequenceView",
columns=[columns] if isinstance(columns, str) else columns,
name=name,
settings={"yAxisMultiple": multiple_y_axes},
)


def mesh(column: str, name: Optional[str] = None) -> Lens:
MeshAnimation = Literal["loop", "oscillate"]


def mesh(
column: str,
name: Optional[str] = None,
*,
color_attribute: Optional[str] = None,
wireframe: bool = False,
transparency: float = 0,
animation: MeshAnimation = "loop",
animation_scale: float = 1.0,
synchronize: bool = True,
) -> Lens:
"""
Add mesh viewer to Spotlight inspector widget.
Supports a single column of type `spotlight.Mesh`.
"""
return Lens(type="MeshView", columns=[column], name=name) # type: ignore
settings: dict = {
"isSynchronized": synchronize,
"showWireframe": wireframe,
"transparency": transparency,
"morphStyle": animation,
"morphScale": animation_scale,
}
if color_attribute is not None:
settings["colorAttribute"] = color_attribute
return Lens(
type="MeshView",
columns=[column],
name=name,
settings=settings,
)


def image(column: str, name: Optional[str] = None) -> Lens:
Expand All @@ -135,7 +174,7 @@ def image(column: str, name: Optional[str] = None) -> Lens:
Supports a single column of type `spotlight.Image`.
"""
return Lens(type="ImageView", columns=[column], name=name) # type: ignore
return Lens(type="ImageView", columns=[column], name=name)


def video(column: str, name: Optional[str] = None) -> Lens:
Expand All @@ -144,11 +183,16 @@ def video(column: str, name: Optional[str] = None) -> Lens:
Supports a single column of type `spotlight.Video`.
"""
return Lens(type="VideoView", columns=[column], name=name) # type: ignore
return Lens(type="VideoView", columns=[column], name=name)


def audio(
column: str, window_column: Optional[str] = None, name: Optional[str] = None
column: str,
window_column: Optional[str] = None,
name: Optional[str] = None,
*,
repeat: bool = False,
autoplay: bool = False,
) -> Lens:
"""
Add audio viewer to Spotlight inspector widget.
Expand All @@ -157,14 +201,24 @@ def audio(
column of type `spotlight.Window`.
"""
return Lens(
type="AudioView", # type: ignore
type="AudioView",
columns=[column] if window_column is None else [column, window_column],
name=name,
settings={"repeat": repeat, "autoplay": autoplay},
)


SpectrogramFrequencyScale = Literal["linear", "logarithmic"]
SpectrogramAmplitudeScale = Literal["decibel", "linear"]


def spectrogram(
column: str, window_column: Optional[str] = None, name: Optional[str] = None
column: str,
window_column: Optional[str] = None,
name: Optional[str] = None,
*,
frequency_scale: SpectrogramFrequencyScale = "linear",
amplitude_scale: SpectrogramAmplitudeScale = "decibel",
) -> Lens:
"""
Add audio spectrogram viewer to Spotlight inspector widget.
Expand All @@ -173,7 +227,8 @@ def spectrogram(
column of type `spotlight.Window`.
"""
return Lens(
type="SpectrogramView", # type: ignore
type="SpectrogramView",
columns=[column] if window_column is None else [column, window_column],
name=name,
settings={"freqScale": frequency_scale, "ampScale": amplitude_scale},
)

0 comments on commit 9e73a80

Please sign in to comment.