Skip to content

Commit

Permalink
Don't fail to import whylogs if optional dependencies are missing (#1541
Browse files Browse the repository at this point in the history
)

## Description

Don't fail if optional dependencies are missing.

## Changes

- Ignore import errors; it should be ok if the optional metrics aren't
used

## Related

Relates to #1489
Closes #1489
  • Loading branch information
richard-rogers authored Jul 2, 2024
1 parent d86f268 commit f51888d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions python/whylogs/core/view/column_profile_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,23 @@ def zero(cls, msg: ColumnMessage) -> "ColumnProfileView":
def from_protobuf(cls, msg: ColumnMessage) -> "ColumnProfileView":
# importing to trigger registration of non-standard metrics
import whylogs.experimental.core.metrics.udf_metric # noqa
import whylogs.experimental.extras.embedding_metric # noqa
import whylogs.experimental.extras.nlp_metric # noqa
import whylogs.extras.image_metric # noqa

# These require numpy & PIL, but we assume users will install
# whylogs with the optional dependencies and import the metric
# modules if they want to use them. So it should be safe to
# ignore the import failures.
try:
import whylogs.experimental.extras.embedding_metric # noqa
except ModuleNotFoundError:
pass
try:
import whylogs.experimental.extras.nlp_metric # noqa
except ModuleNotFoundError:
pass
try:
import whylogs.extras.image_metric # noqa
except ModuleNotFoundError:
pass

result_metrics: Dict[str, Metric] = {}
metric_messages: Dict[str, Dict[str, MetricComponentMessage]] = {}
Expand Down Expand Up @@ -144,7 +158,9 @@ def from_protobuf(cls, msg: ColumnMessage) -> "ColumnProfileView":
deserialized_metric = metric_class.from_protobuf(m_msg)
result_metrics[m_name] = deserialized_metric
except Exception as error: # noqa
raise DeserializationError(f"Failed to deserialize metric: {m_name}:{error}")
raise DeserializationError(
f"Failed to deserialize metric: {m_name}:{error}; possibly missing dependencies"
)

return ColumnProfileView(metrics=result_metrics)

Expand Down

0 comments on commit f51888d

Please sign in to comment.