Skip to content

feat: auto coerce dataframes #44

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/cev/_embedding_comparison_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from cev._widget_utils import add_ilocs_trait, parse_label
from cev.components import MarkerSelectionIndicator, WidthOptimizer

if typing.TYPE_CHECKING:
import pandas as pd


def _create_titles(
titles: tuple[str, str]
Expand All @@ -46,11 +49,17 @@ def _create_titles(
return spacer, title_widget


def _coerce_to_embedding(embedding: Embedding | pd.DataFrame) -> Embedding:
if isinstance(embedding, Embedding):
return embedding
return Embedding.from_df(embedding)


class EmbeddingComparisonWidget(ipywidgets.VBox):
def __init__(
self,
left_embedding: Embedding,
right_embedding: Embedding,
left_embedding: Embedding | pd.DataFrame,
right_embedding: Embedding | pd.DataFrame,
Comment on lines +61 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this backwards compatible with Python versions before 11?

Copy link
Collaborator Author

@manzt manzt Sep 25, 2023

Choose a reason for hiding this comment

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

Yes, the top of the file uses:

from __future__ import annotations

Which tells Python to treat everything after the : in types as comments. We only use for static type checking, so this allows us to use modern syntax. If we needed to inspect the types at runtime (e.g., using pydantic in this file), we'd want to avoid using __future__ annotations imports and find a typing syntax that is backward compatbile.

row_height: int = 250,
metric: typing.Literal["confusion", "neigbhorhood", "abundance"] = "confusion",
inverted_colormap: bool = False,
Expand All @@ -61,6 +70,8 @@ def __init__(
active_markers: list[str] | typing.Literal["all"] = "all",
**kwargs,
):
left_embedding = _coerce_to_embedding(left_embedding)
right_embedding = _coerce_to_embedding(right_embedding)
pointwise_correspondence = has_pointwise_correspondence(
left_embedding, right_embedding
)
Expand Down