Skip to content

Commit

Permalink
chore: Add hvplot and seaborn as optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjholland committed Jan 2, 2025
1 parent ae27260 commit 217598c
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 14 deletions.
21 changes: 19 additions & 2 deletions pyprobe/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import numpy as np
import plotly.graph_objects as go
import polars as pl
import seaborn as _sns
from IPython.display import Image, display
from numpy.typing import NDArray
from plotly.express.colors import sample_colorscale
Expand Down Expand Up @@ -55,8 +54,26 @@ def _retrieve_relevant_columns(
return result_obj._get_data_subset(*relevant_columns)


try:
import seaborn as _sns
except ImportError:
_sns = None


def _create_seaborn_wrapper() -> Any:
"""Create wrapped version of seaborn module."""
"""Create a wrapped version of the seaborn package."""
if _sns is None:

class SeabornWrapper:
def __getattr__(self, _: Any) -> None:
"""Raise an ImportError if seaborn is not installed."""
raise ImportError(
"Optional dependency 'seaborn' is not installed. Please install by "
"running 'pip install seaborn'."
)

return SeabornWrapper()

wrapped_sns = type("SeabornWrapper", (), {})()

def wrap_function(func: Callable[..., Any]) -> Callable[..., Any]:
Expand Down
38 changes: 26 additions & 12 deletions pyprobe/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pprint import pprint
from typing import Any, Dict, List, Literal, Optional, Set, Tuple, Union

import hvplot.polars
import numpy as np
import pandas as pd
import polars as pl
Expand All @@ -17,6 +16,11 @@

logger = logging.getLogger(__name__)

try:
import hvplot.polars
except ImportError:
hvplot = None


class PolarsColumnCache:
"""A class to cache columns from a Polars DataFrame.
Expand Down Expand Up @@ -225,18 +229,28 @@ def plot(self, *args: Any, **kwargs: Any) -> None:
"when called on a DataFrame.\n\n" + (plot.__doc__ or "")
)

@wraps(hvplot.hvPlot)
def hvplot(self, *args: Any, **kwargs: Any) -> None:
"""Wrapper for plotting using the hvplot library."""
data_to_plot = _retrieve_relevant_columns(self, args, kwargs)
return data_to_plot.hvplot(*args, **kwargs)
if hvplot is not None:

hvplot.__doc__ = (
"HvPlot is a library for creating fast and interactive plots.\n\n"
"The default backend is bokeh, which can be changed by setting the backend "
"with :code:`hvplot.extension('matplotlib')` or "
":code:`hvplot.extension('plotly')`.\n\n" + (hvplot.__doc__ or "")
)
@wraps(hvplot.hvPlot)
def hvplot(self, *args: Any, **kwargs: Any) -> None:
"""Wrapper for plotting using the hvplot library."""
data_to_plot = _retrieve_relevant_columns(self, args, kwargs)
return data_to_plot.hvplot(*args, **kwargs)

hvplot.__doc__ = (
"HvPlot is a library for creating fast and interactive plots.\n\n"
"The default backend is bokeh, which can be changed by setting the backend "
"with :code:`hvplot.extension('matplotlib')` or "
":code:`hvplot.extension('plotly')`.\n\n" + (hvplot.__doc__ or "")
)
else:

def hvplot(self, *args: Any, **kwargs: Any) -> None:
"""Wrapper for plotting using the hvplot library."""
raise ImportError(
"Optional dependency hvplot is not installed. Please install it via "
"'pip install hvplot'."
)

def _get_data_subset(self, *column_names: str) -> pl.DataFrame:
"""Return a subset of the data with the specified columns.
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ dependencies = [
"sympy>=1.13.3",
]

[project.optional-dependencies]
hvplot = [
"hvplot>=0.11.2",
]
seaborn = [
"seaborn>=0.13.2",
]

[dependency-groups]
format = [
"black>=24.10.0",
Expand Down
Loading

0 comments on commit 217598c

Please sign in to comment.