Skip to content

Commit

Permalink
Merge pull request #374 from Renumics/feature/set-lens-settings-in-py…
Browse files Browse the repository at this point in the history
…thon

Feature/set lens settings in python
  • Loading branch information
neindochoh authored Nov 20, 2023
2 parents 5263bbe + 262f5fd commit 5fa0b61
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 29 deletions.
100 changes: 82 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,17 @@ 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},
)


def rouge_score(column: str, reference_column: str, name: Optional[str] = None) -> Lens:
"""
Add ROUGE score viewer to Spotlight inspector widget.
Supports a pair of string columns.
"""
return Lens(type="RougeScore", columns=[column, reference_column], name=name)
17 changes: 7 additions & 10 deletions src/lenses/MeshLens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ const palettesSelector = (c: ColorsState) => ({
const MeshLens: Lens = ({ values, syncKey }) => {
const meshData = values[0] as ArrayBuffer;

const [colorAttributeName, setColorAttributeName] = useSetting(
'colorAtributeName',
''
);
const [colorAttribute, setColorAttribute] = useSetting('colorAttribute', '');
const [isSynchronized, setIsSynchronized] = useSetting('isSynchronized', true);
const [showWireframe, setShowWireframe] = useSetting('showWireframe', false);
const [transparency, setTransparency] = useSetting('transparency', 0);
Expand All @@ -50,12 +47,12 @@ const MeshLens: Lens = ({ values, syncKey }) => {
const palettes = useColors(palettesSelector);

const colorPalette = useMemo(() => {
const attribute = meshAttributes.find((a) => a.name === colorAttributeName);
const attribute = meshAttributes.find((a) => a.name === colorAttribute);

if (attribute?.type?.kind === 'int') return palettes.categoricalPalette;
if (attribute?.type?.kind === 'float') return palettes.continuousPalette;
return palettes.constantPalette;
}, [meshAttributes, palettes, colorAttributeName]);
}, [meshAttributes, palettes, colorAttribute]);

const handleSelectMorphStyle = useCallback(
(m: MorphStyle) => {
Expand Down Expand Up @@ -85,9 +82,9 @@ const MeshLens: Lens = ({ values, syncKey }) => {

const handleSelectColor = useCallback(
(attributeName: string) => {
setColorAttributeName(attributeName);
setColorAttribute(attributeName);
},
[setColorAttributeName]
[setColorAttribute]
);

const handleToggleSync = useCallback(
Expand All @@ -104,7 +101,7 @@ const MeshLens: Lens = ({ values, syncKey }) => {
<GltfViewer
ref={viewer}
data={meshData}
color={colorAttributeName}
color={colorAttribute}
colorPalette={colorPalette}
showWireframe={showWireframe}
onChange={handleViewerChange}
Expand All @@ -129,7 +126,7 @@ const MeshLens: Lens = ({ values, syncKey }) => {
morphStyle={morphStyle}
morphScale={Math.log(morphScale) * 10}
transparency={transparency}
colorAttributeName={colorAttributeName}
colorAttributeName={colorAttribute}
showWireframe={showWireframe}
onChangeShowWireframe={setShowWireframe}
onReset={resetCamera}
Expand Down
2 changes: 1 addition & 1 deletion src/lenses/RougeScoreLens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const RougeScoreLens: Lens<string> = ({ values }) => {
);
};

RougeScoreLens.key = 'RougeScoreView';
RougeScoreLens.key = 'RougeScore';
RougeScoreLens.dataTypes = ['str'];
RougeScoreLens.defaultHeight = 50;
RougeScoreLens.minHeight = 50;
Expand Down

0 comments on commit 5fa0b61

Please sign in to comment.