From f51888d07f226cadae0e9447e3f43a473ef05cd8 Mon Sep 17 00:00:00 2001 From: richard-rogers <93153899+richard-rogers@users.noreply.github.com> Date: Tue, 2 Jul 2024 12:29:30 -0700 Subject: [PATCH] Don't fail to import whylogs if optional dependencies are missing (#1541) ## 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 whylabs/whylogs#1489 Closes whylabs/whylogs#1489 --- .../whylogs/core/view/column_profile_view.py | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/python/whylogs/core/view/column_profile_view.py b/python/whylogs/core/view/column_profile_view.py index 0a90df2697..1c0a2d3d52 100644 --- a/python/whylogs/core/view/column_profile_view.py +++ b/python/whylogs/core/view/column_profile_view.py @@ -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]] = {} @@ -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)