From 6965603b5d2cdb48bfe017040a2593659b6f87c2 Mon Sep 17 00:00:00 2001 From: Franklin <41602287+fcogidi@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:23:04 -0500 Subject: [PATCH] Minor patches (#542) * Fix capitalization of Python * Change ValueError to TypeError and update docstrings --- .../evaluate/metrics/experimental/__init__.py | 2 +- .../metrics/experimental/functional/accuracy.py | 6 +++--- .../metrics/experimental/functional/f_score.py | 12 ++++++------ .../functional/negative_predictive_value.py | 6 +++--- .../experimental/functional/precision_recall.py | 12 ++++++------ .../experimental/functional/specificity.py | 6 +++--- cyclops/evaluate/metrics/experimental/metric.py | 5 +++-- .../evaluate/metrics/experimental/metric_dict.py | 2 +- .../metrics/experimental/utils/validation.py | 4 ++-- cyclops/models/wrappers/utils.py | 2 +- cyclops/report/model_card/base.py | 4 ++-- .../evaluate/metrics/experimental/test_metric.py | 2 +- .../metrics/experimental/test_operator_metric.py | 16 ++++++++-------- 13 files changed, 40 insertions(+), 39 deletions(-) diff --git a/cyclops/evaluate/metrics/experimental/__init__.py b/cyclops/evaluate/metrics/experimental/__init__.py index 0eb77c1ed..ed04db9fa 100644 --- a/cyclops/evaluate/metrics/experimental/__init__.py +++ b/cyclops/evaluate/metrics/experimental/__init__.py @@ -1,4 +1,4 @@ -"""Metrics for arrays that conform to the python array API standard.""" +"""Metrics for arrays that conform to the Python array API standard.""" from cyclops.evaluate.metrics.experimental.accuracy import ( BinaryAccuracy, MulticlassAccuracy, diff --git a/cyclops/evaluate/metrics/experimental/functional/accuracy.py b/cyclops/evaluate/metrics/experimental/functional/accuracy.py index f4617e919..f7d009115 100644 --- a/cyclops/evaluate/metrics/experimental/functional/accuracy.py +++ b/cyclops/evaluate/metrics/experimental/functional/accuracy.py @@ -68,7 +68,7 @@ def binary_accuracy( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -216,7 +216,7 @@ def multiclass_accuracy( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -378,7 +378,7 @@ def multilabel_accuracy( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError diff --git a/cyclops/evaluate/metrics/experimental/functional/f_score.py b/cyclops/evaluate/metrics/experimental/functional/f_score.py index 401d25f5b..6cb74db4d 100644 --- a/cyclops/evaluate/metrics/experimental/functional/f_score.py +++ b/cyclops/evaluate/metrics/experimental/functional/f_score.py @@ -144,7 +144,7 @@ def binary_fbeta_score( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -276,7 +276,7 @@ def multiclass_fbeta_score( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -468,7 +468,7 @@ def multilabel_fbeta_score( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -593,7 +593,7 @@ def binary_f1_score( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -692,7 +692,7 @@ def multiclass_f1_score( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -824,7 +824,7 @@ def multilabel_f1_score( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError diff --git a/cyclops/evaluate/metrics/experimental/functional/negative_predictive_value.py b/cyclops/evaluate/metrics/experimental/functional/negative_predictive_value.py index 81a1fd5ed..2ca4411cc 100644 --- a/cyclops/evaluate/metrics/experimental/functional/negative_predictive_value.py +++ b/cyclops/evaluate/metrics/experimental/functional/negative_predictive_value.py @@ -87,7 +87,7 @@ def binary_npv( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -200,7 +200,7 @@ def multiclass_npv( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -360,7 +360,7 @@ def multilabel_npv( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError diff --git a/cyclops/evaluate/metrics/experimental/functional/precision_recall.py b/cyclops/evaluate/metrics/experimental/functional/precision_recall.py index b4411db31..283b485cc 100644 --- a/cyclops/evaluate/metrics/experimental/functional/precision_recall.py +++ b/cyclops/evaluate/metrics/experimental/functional/precision_recall.py @@ -95,7 +95,7 @@ def binary_precision( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -206,7 +206,7 @@ def multiclass_precision( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -364,7 +364,7 @@ def multilabel_precision( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -478,7 +478,7 @@ def binary_recall( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -589,7 +589,7 @@ def multiclass_recall( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -750,7 +750,7 @@ def multilabel_recall( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError diff --git a/cyclops/evaluate/metrics/experimental/functional/specificity.py b/cyclops/evaluate/metrics/experimental/functional/specificity.py index 328deebc0..da27e0957 100644 --- a/cyclops/evaluate/metrics/experimental/functional/specificity.py +++ b/cyclops/evaluate/metrics/experimental/functional/specificity.py @@ -87,7 +87,7 @@ def binary_specificity( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -199,7 +199,7 @@ def multiclass_specificity( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError @@ -359,7 +359,7 @@ def multilabel_specificity( Raises ------ - ValueError + TypeError If the arrays `target` and `preds` are not compatible with the Python array API standard. ValueError diff --git a/cyclops/evaluate/metrics/experimental/metric.py b/cyclops/evaluate/metrics/experimental/metric.py index d3e69de05..1712fabd3 100644 --- a/cyclops/evaluate/metrics/experimental/metric.py +++ b/cyclops/evaluate/metrics/experimental/metric.py @@ -180,7 +180,7 @@ def add_state_default_factory( """ if not name.isidentifier(): raise ValueError( - f"Argument `name` must be a valid python identifier. Got `{name}`.", + f"Argument `name` must be a valid Python identifier. Got `{name}`.", ) if not callable(default_factory): raise TypeError( @@ -286,12 +286,13 @@ def update(self, *args: Any, **kwargs: Any) -> None: "not yet be defined.", ) xp = apc.get_namespace(*arrays) - self._add_states(xp) # move state variables to device of first array device = apc.device(arrays[0]) self.to_device(device) + self._add_states(xp) + self._computed = None self._update_count += 1 diff --git a/cyclops/evaluate/metrics/experimental/metric_dict.py b/cyclops/evaluate/metrics/experimental/metric_dict.py index 25dfca259..08a833522 100644 --- a/cyclops/evaluate/metrics/experimental/metric_dict.py +++ b/cyclops/evaluate/metrics/experimental/metric_dict.py @@ -49,7 +49,7 @@ class ArrayEncoder(json.JSONEncoder): def default(self, obj: Any) -> Any: """Return a JSON-serializable representation of the object. - Objects conforming to the array API standard are converted to python lists + Objects conforming to the array API standard are converted to Python lists via numpy. Arrays are moved to the CPU before converting to numpy. """ if apc.is_array_api_obj(obj): diff --git a/cyclops/evaluate/metrics/experimental/utils/validation.py b/cyclops/evaluate/metrics/experimental/utils/validation.py index db6122feb..5f02c090a 100644 --- a/cyclops/evaluate/metrics/experimental/utils/validation.py +++ b/cyclops/evaluate/metrics/experimental/utils/validation.py @@ -62,13 +62,13 @@ def _basic_input_array_checks( ) -> None: """Perform basic validation of `target` and `preds`.""" if not apc.is_array_api_obj(target): - raise ValueError( + raise TypeError( "Expected `target` to be an array-API-compatible object, but got " f"{type(target)}.", ) if not apc.is_array_api_obj(preds): - raise ValueError( + raise TypeError( "Expected `preds` to be an array-API-compatible object, but got " f"{type(preds)}.", ) diff --git a/cyclops/models/wrappers/utils.py b/cyclops/models/wrappers/utils.py index 355b1f8f6..f77747333 100644 --- a/cyclops/models/wrappers/utils.py +++ b/cyclops/models/wrappers/utils.py @@ -280,7 +280,7 @@ def set_params(cls, **params): def set_random_seed(seed: int, deterministic: bool = False) -> None: - """Set a random seed for python, numpy and PyTorch globally. + """Set a random seed for Python, numpy and PyTorch globally. Parameters ---------- diff --git a/cyclops/report/model_card/base.py b/cyclops/report/model_card/base.py index 82774f05d..235f37fba 100644 --- a/cyclops/report/model_card/base.py +++ b/cyclops/report/model_card/base.py @@ -141,12 +141,12 @@ def add_field(self, name: str, value: Any) -> None: Raises ------ ValueError - If the field name is not a valid python identifier. + If the field name is not a valid Python identifier. """ if not name.isidentifier() or keyword.iskeyword(name): raise ValueError( - f"Expected `field_name` to be a valid python identifier." + f"Expected `field_name` to be a valid Python identifier." f" Got {name} instead.", ) diff --git a/tests/cyclops/evaluate/metrics/experimental/test_metric.py b/tests/cyclops/evaluate/metrics/experimental/test_metric.py index 39c80fb75..72279f58e 100644 --- a/tests/cyclops/evaluate/metrics/experimental/test_metric.py +++ b/tests/cyclops/evaluate/metrics/experimental/test_metric.py @@ -194,7 +194,7 @@ def custom_fn(xp, _): with pytest.raises( ValueError, - match="Argument `name` must be a valid python identifier. Got `h6!`.", + match="Argument `name` must be a valid Python identifier. Got `h6!`.", ): metric.add_state_default_factory("h6!", list) # type: ignore diff --git a/tests/cyclops/evaluate/metrics/experimental/test_operator_metric.py b/tests/cyclops/evaluate/metrics/experimental/test_operator_metric.py index 0daed27b6..268cf31f8 100644 --- a/tests/cyclops/evaluate/metrics/experimental/test_operator_metric.py +++ b/tests/cyclops/evaluate/metrics/experimental/test_operator_metric.py @@ -139,7 +139,7 @@ def test_metrics_floordiv(second_operand, expected_result): """Test that `floordiv` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 5, # python scalars can only be promoted with floating-point arrays + 5, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -164,7 +164,7 @@ def test_metrics_ge(second_operand, expected_result): """Test that `ge` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 5, # python scalars can only be promoted with floating-point arrays + 5, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -189,7 +189,7 @@ def test_metrics_gt(second_operand, expected_result): """Test that `gt` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 5, # python scalars can only be promoted with floating-point arrays + 5, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -224,7 +224,7 @@ def test_metrics_le(second_operand, expected_result): """Test that `le` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 5, # python scalars can only be promoted with floating-point arrays + 5, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -249,7 +249,7 @@ def test_metrics_lt(second_operand, expected_result): """Test that `lt` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 5, # python scalars can only be promoted with floating-point arrays + 5, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -292,7 +292,7 @@ def test_metrics_mod(second_operand, expected_result): """Test that `mod` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 5, # python scalars can only be promoted with floating-point arrays + 5, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -317,7 +317,7 @@ def test_metrics_mul(second_operand, expected_result): """Test that `mul` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 2, # python scalars can only be promoted with floating-point arrays + 2, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), ) @@ -350,7 +350,7 @@ def test_metrics_ne(second_operand, expected_result): """Test that `!=` operator works and returns an operator metric.""" first_metric = DummyMetric( anp.asarray( - 2, # python scalars can only be promoted with floating-point arrays + 2, # Python scalars can only be promoted with floating-point arrays dtype=anp.float32 if isinstance(second_operand, float) else None, ), )