Skip to content

Commit

Permalink
Apply ruff formatter and ditch black
Browse files Browse the repository at this point in the history
  • Loading branch information
amrit110 committed Feb 18, 2024
1 parent e1ca767 commit 2cda268
Show file tree
Hide file tree
Showing 59 changed files with 636 additions and 447 deletions.
12 changes: 4 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ repos:
- id: check-yaml
- id: check-toml

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.2.1'
rev: 'v0.2.2'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
types_or: [python, jupyter]
- id: ruff-format
types_or: [python, jupyter]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
Expand All @@ -34,15 +31,14 @@ repos:
entry: python3 -m mypy --config-file pyproject.toml
language: system
types: [python]
exclude: "use_cases|tests|cyclops/(models|monitor|report/plot)"
exclude: "tests|cyclops/(models|monitor|report/plot)"

- repo: local
hooks:
- id: nbstripout
name: nbstripout
language: system
entry: python3 -m nbstripout
exclude: ^docs/source/tutorials/gemini/.*\.ipynb$

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.7.1
Expand Down
3 changes: 2 additions & 1 deletion cyclops/data/df/handle_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,8 @@ def collect_indicators(
# Get categories
data[cat] = np.argmax(data[indicators].values, axis=1)
indicator_names = [
indicator[len(cat) + 1 :] for indicator in indicators # noqa: E203
indicator[len(cat) + 1 :]
for indicator in indicators # noqa: E203
]
map_dict = {
i: (name if name != MISSING_CATEGORY else np.nan)
Expand Down
3 changes: 2 additions & 1 deletion cyclops/data/df/vectorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def split_vectorized(
)

splits = [
vec.split_by_indices(axes_list[i], index_splits) for i, vec in enumerate(vecs) # type: ignore
vec.split_by_indices(axes_list[i], index_splits)
for i, vec in enumerate(vecs) # type: ignore
]

return tuple(splits)
Expand Down
15 changes: 9 additions & 6 deletions cyclops/data/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ class SliceSpec:
... {"feature_1": {"value": ["value_1", "value_2"]}},
... {"feature_1": {"value": "value_1", "negate": True, "keep_nulls": True}},
... {"feature_1": {"min_value": "2020-01-01", "max_value": "2020-12-31"}},
... {"feature_1": {
... "min_value": 5,
... "max_value": 60,
... "min_inclusive": False,
... "max_inclusive": False}
... {
... "feature_1": {
... "min_value": 5,
... "max_value": 60,
... "min_inclusive": False,
... "max_inclusive": False,
... }
... },
... {"feature_1": {"year": [2020, 2021, 2022]}},
... {"feature_1": {"month": [6, 7, 8]}},
Expand All @@ -110,7 +112,8 @@ class SliceSpec:
... {
... "feature_1": {"value": "value_1"},
... "feature_2": {
... "min_value": "2020-01-01", "keep_nulls": False,
... "min_value": "2020-01-01",
... "keep_nulls": False,
... },
... "feature_3": {"year": ["2000", "2010", "2020"]},
... },
Expand Down
3 changes: 2 additions & 1 deletion cyclops/evaluate/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ def _compute_metrics(
stacklevel=1,
)
metric_output: Dict[str, Array] = {
metric_name: float("NaN") for metric_name in metrics # type: ignore[attr-defined,misc]
metric_name: float("NaN")
for metric_name in metrics # type: ignore[attr-defined,misc]
}
elif (
batch_size is None or batch_size < 0
Expand Down
3 changes: 2 additions & 1 deletion cyclops/evaluate/fairness/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ def _compute_metrics( # noqa: C901, PLR0912
if len(dataset) == 0:
warnings.warn(empty_dataset_msg, RuntimeWarning, stacklevel=1)
results: Dict[str, Any] = {
metric_name: float("NaN") for metric_name in metrics # type: ignore[attr-defined]
metric_name: float("NaN")
for metric_name in metrics # type: ignore[attr-defined]
}
elif (
batch_size is None or batch_size <= 0
Expand Down
18 changes: 10 additions & 8 deletions cyclops/evaluate/metrics/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ class MulticlassAccuracy(MulticlassStatScores, registry_key="multiclass_accuracy
array([1. , 0. , 0.66666667])
>>> metric.reset_state()
>>> target = [[0, 1, 2], [2, 1, 0]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]]]
>>> preds = [
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]],
... ]
>>> for t, p in zip(target, preds):
... metric.update_state(t, p)
>>> metric.compute()
Expand Down Expand Up @@ -184,8 +186,7 @@ class MultilabelAccuracy(MultilabelStatScores, registry_key="multilabel_accuracy
array([1., 1., 0.])
>>> metric.reset_state()
>>> target = [[[0, 1, 1], [1, 0, 0]], [[1, 0, 0], [0, 1, 1]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]], [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> for t, p in zip(target, preds):
... metric.update_state(t, p)
>>> metric.compute()
Expand Down Expand Up @@ -291,8 +292,10 @@ class Accuracy(Metric, registry_key="accuracy", force_register=True):
array([1. , 0. , 0.66666667])
>>> metric.reset_state()
>>> target = [[0, 1, 2], [2, 1, 0]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]]]
>>> preds = [
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]],
... ]
>>> for t, p in zip(target, preds):
... metric.update_state(t, p)
>>> metric.compute()
Expand All @@ -307,8 +310,7 @@ class Accuracy(Metric, registry_key="accuracy", force_register=True):
array([1., 1., 0.])
>>> metric.reset_state()
>>> target = [[[0, 1, 1], [1, 0, 0]], [[1, 0, 0], [0, 1, 1]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]], [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> for t, p in zip(target, preds):
... metric.update_state(t, p)
>>> metric.compute()
Expand Down
28 changes: 20 additions & 8 deletions cyclops/evaluate/metrics/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,21 @@ class MulticlassAUROC(MulticlassPrecisionRecallCurve, registry_key="multiclass_a
--------
>>> from cyclops.evaluate.metrics import MulticlassAUROC
>>> target = [0, 1, 2, 0]
>>> preds = [[0.9, 0.05, 0.05], [0.05, 0.89, 0.06],
... [0.05, 0.01, 0.94], [0.9, 0.05, 0.05]]
>>> preds = [
... [0.9, 0.05, 0.05],
... [0.05, 0.89, 0.06],
... [0.05, 0.01, 0.94],
... [0.9, 0.05, 0.05],
... ]
>>> metric = MulticlassAUROC(num_classes=3)
>>> metric(target, preds)
array([1., 1., 1.])
>>> metric.reset_state()
>>> target = [[0, 1, 0], [1, 0, 1]]
>>> preds = [[[0.1, 0.9, 0.0], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]],
... [[0.1, 0.1, 0.8], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]]]
>>> preds = [
... [[0.1, 0.9, 0.0], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]],
... [[0.1, 0.1, 0.8], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]],
... ]
>>> for t, p in zip(target, preds):
... metric.update_state(t, p)
>>> metric.compute()
Expand Down Expand Up @@ -278,15 +284,21 @@ class AUROC(Metric, registry_key="auroc", force_register=True):
>>> # (multiclass)
>>> from cyclops.evaluate.metrics import MulticlassAUROC
>>> target = [0, 1, 2, 0]
>>> preds = [[0.9, 0.05, 0.05], [0.05, 0.89, 0.06],
... [0.05, 0.01, 0.94], [0.9, 0.05, 0.05]]
>>> preds = [
... [0.9, 0.05, 0.05],
... [0.05, 0.89, 0.06],
... [0.05, 0.01, 0.94],
... [0.9, 0.05, 0.05],
... ]
>>> metric = MulticlassAUROC(num_classes=3)
>>> metric(target, preds)
array([1., 1., 1.])
>>> metric.reset_state()
>>> target = [[0, 1, 0], [1, 0, 1]]
>>> preds = [[[0.1, 0.9, 0.0], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]],
... [[0.1, 0.1, 0.8], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]]]
>>> preds = [
... [[0.1, 0.9, 0.0], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]],
... [[0.1, 0.1, 0.8], [0.7, 0.2, 0.1], [0.2, 0.3, 0.5]],
... ]
>>> for t, p in zip(target, preds):
... metric.update_state(t, p)
>>> metric.compute()
Expand Down
9 changes: 5 additions & 4 deletions cyclops/evaluate/metrics/experimental/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ class MulticlassAccuracy(
Array(0.6, dtype=float32)
>>> metric.reset()
>>> target = [[0, 1, 2], [2, 1, 0]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]]]
>>> preds = [
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]],
... ]
>>> for t, p in zip(target, preds):
... metric.update(anp.asarray(t), anp.asarray(p))
>>> metric.compute()
Expand Down Expand Up @@ -160,8 +162,7 @@ class MultilabelAccuracy(
Array(0.6666667, dtype=float32)
>>> metric.reset()
>>> target = [[[0, 1, 1], [1, 0, 0]], [[1, 0, 0], [0, 1, 1]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]], [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> for t, p in zip(target, preds):
... metric.update(anp.asarray(t), anp.asarray(p))
>>> metric.compute()
Expand Down
21 changes: 14 additions & 7 deletions cyclops/evaluate/metrics/experimental/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def _compute_metric(self) -> Array: # type: ignore[override]
if self.thresholds is None
else self.confmat # type: ignore[attr-defined]
)
return _binary_auroc_compute(state, thresholds=self.thresholds, max_fpr=self.max_fpr) # type: ignore
return _binary_auroc_compute(
state,
thresholds=self.thresholds,
max_fpr=self.max_fpr,
) # type: ignore


class MulticlassAUROC(MulticlassPrecisionRecallCurve, registry_key="multiclass_auroc"):
Expand Down Expand Up @@ -117,12 +121,15 @@ class MulticlassAUROC(MulticlassPrecisionRecallCurve, registry_key="multiclass_a
>>> from cyclops.evaluate.metrics.experimental import MulticlassAUROC
>>> target = anp.asarray([0, 1, 2, 0, 1, 2])
>>> preds = anp.asarray(
... [[0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44],
... [0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44]])
... [
... [0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44],
... [0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44],
... ]
... )
>>> auroc = MulticlassAUROC(num_classes=3, average="macro", thresholds=None)
>>> auroc(target, preds)
Array(0.33333334, dtype=float32)
Expand Down
23 changes: 15 additions & 8 deletions cyclops/evaluate/metrics/experimental/average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,19 @@ class MulticlassAveragePrecision(
>>> from cyclops.evaluate.metrics.experimental import MulticlassAveragePrecision
>>> target = anp.asarray([0, 1, 2, 0, 1, 2])
>>> preds = anp.asarray(
... [[0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44],
... [0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44]])
... [
... [0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44],
... [0.11, 0.22, 0.67],
... [0.84, 0.73, 0.12],
... [0.33, 0.92, 0.44],
... ]
... )
>>> metric = MulticlassAveragePrecision(
... num_classes=3, thresholds=None, average=None,
... num_classes=3,
... thresholds=None,
... average=None,
... )
>>> metric(target, preds)
Array([0.33333334, 0.5 , 0.5 ], dtype=float32)
Expand Down Expand Up @@ -224,7 +229,9 @@ class MultilabelAveragePrecision(
... [[0.11, 0.22, 0.67], [0.84, 0.73, 0.12], [0.33, 0.92, 0.44]],
... )
>>> metric = MultilabelAveragePrecision(
... num_labels=3, thresholds=None, average=None,
... num_labels=3,
... thresholds=None,
... average=None,
... )
>>> metric(target, preds)
Array([1. , 0.5833334, 0.5 ], dtype=float32)
Expand Down
20 changes: 14 additions & 6 deletions cyclops/evaluate/metrics/experimental/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ class BinaryConfusionMatrix(
Array([[2, 1],
[1, 2]], dtype=int64)
>>> target = anp.asarray([[[0, 1], [1, 0], [0, 1]], [[1, 1], [0, 0], [1, 0]]])
>>> preds = anp.asarray([[[0.59, 0.91], [0.91, 0.99], [0.63, 0.04]],
... [[0.38, 0.04], [0.86, 0.780], [0.45, 0.37]]])
>>> preds = anp.asarray(
... [
... [[0.59, 0.91], [0.91, 0.99], [0.63, 0.04]],
... [[0.38, 0.04], [0.86, 0.780], [0.45, 0.37]],
... ]
... )
"""

Expand Down Expand Up @@ -205,10 +209,14 @@ class MulticlassConfusionMatrix(Metric, registry_key="multiclass_confusion_matri
[0, 1, 0],
[0, 0, 1]], dtype=int64)
>>> target = anp.asarray([2, 1, 0, 0])
>>> preds = anp.asarray([[0.16, 0.26, 0.58],
... [0.22, 0.61, 0.17],
... [0.71, 0.09, 0.20],
... [0.05, 0.82, 0.13]])
>>> preds = anp.asarray(
... [
... [0.16, 0.26, 0.58],
... [0.22, 0.61, 0.17],
... [0.71, 0.09, 0.20],
... [0.05, 0.82, 0.13],
... ]
... )
>>> metric = MulticlassConfusionMatrix(num_classes=3)
>>> metric(target, preds)
Array([[1, 1, 0],
Expand Down
18 changes: 10 additions & 8 deletions cyclops/evaluate/metrics/experimental/f_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ class MulticlassFBetaScore(
Array(0.6, dtype=float32)
>>> metric.reset()
>>> target = [[0, 1, 2], [2, 1, 0]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]]]
>>> preds = [
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]],
... ]
>>> for t, p in zip(target, preds):
... metric.update(anp.asarray(t), anp.asarray(p))
>>> metric.compute()
Expand Down Expand Up @@ -219,8 +221,7 @@ class MultilabelFBetaScore(
Array(0.6666667, dtype=float32)
>>> metric.reset()
>>> target = [[[0, 1, 1], [1, 0, 0]], [[1, 0, 0], [0, 1, 1]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]], [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> for t, p in zip(target, preds):
... metric.update(anp.asarray(t), anp.asarray(p))
>>> metric.compute()
Expand Down Expand Up @@ -359,8 +360,10 @@ class MulticlassF1Score(MulticlassFBetaScore, registry_key="multiclass_f1_score"
Array(0.6, dtype=float32)
>>> metric.reset()
>>> target = [[0, 1, 2], [2, 1, 0]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]]]
>>> preds = [
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1], [0.2, 0.6, 0.2]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0], [0.2, 0.6, 0.2]],
... ]
>>> for t, p in zip(target, preds):
... metric.update(anp.asarray(t), anp.asarray(p))
>>> metric.compute()
Expand Down Expand Up @@ -433,8 +436,7 @@ class MultilabelF1Score(
Array(0.6666667, dtype=float32)
>>> metric.reset()
>>> target = [[[0, 1, 1], [1, 0, 0]], [[1, 0, 0], [0, 1, 1]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> preds = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]], [[0.1, 0.8, 0.1], [0.05, 0.95, 0]]]
>>> for t, p in zip(target, preds):
... metric.update(anp.asarray(t), anp.asarray(p))
>>> metric.compute()
Expand Down
Loading

0 comments on commit 2cda268

Please sign in to comment.