Skip to content

Commit

Permalink
[pre-commit.ci] pre-commit suggestions (#2484)
Browse files Browse the repository at this point in the history
* [pre-commit.ci] pre-commit suggestions

updates:
- [github.com/crate-ci/typos: v1.16.26 → v1.20.0](crate-ci/typos@v1.16.26...v1.20.0)
- [github.com/pre-commit/mirrors-prettier: v3.1.0 → v4.0.0-alpha.8](pre-commit/mirrors-prettier@v3.1.0...v4.0.0-alpha.8)
- [github.com/astral-sh/ruff-pre-commit: v0.3.2 → v0.3.5](astral-sh/ruff-pre-commit@v0.3.2...v0.3.5)

* Apply suggestions from code review
* update & rename

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
Co-authored-by: Jirka <jirka.borovec@seznam.cz>
  • Loading branch information
3 people committed Apr 10, 2024
1 parent 8dba184 commit ca7c8eb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 39 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand All @@ -46,7 +46,7 @@ repos:
exclude: pyproject.toml

- repo: https://github.com/crate-ci/typos
rev: v1.16.26
rev: v1.20.7
hooks:
- id: typos
# empty to do not write fixes
Expand Down Expand Up @@ -112,7 +112,7 @@ repos:
- id: text-unicode-replacement-char

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.2
rev: v0.3.5
hooks:
# try to fix what is possible
- id: ruff
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Change default state of `SpectralAngleMapper` and `UniversalImageQualityIndex` to be tensors ([#2089](https://github.com/Lightning-AI/torchmetrics/pull/2089))
- Use `arange` and repeat for deterministic bincount ([#2184](https://github.com/Lightning-AI/torchmetrics/pull/2184))
- Use `torch` range func and repeat for deterministic bincount ([#2184](https://github.com/Lightning-AI/torchmetrics/pull/2184))

### Removed

Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/classification/cohen_kappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class labels.
As output to ``forward`` and ``compute`` the metric returns the following output:
- ``bck`` (:class:`~torch.Tensor`): A tensor containing cohen kappa score
- ``bc_kappa`` (:class:`~torch.Tensor`): A tensor containing cohen kappa score
Args:
threshold: Threshold for transforming probability to binary (0,1) predictions
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/classification/group_fairness.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class BinaryFairness(_AbstractGroupStatScores):
Args:
num_groups: The number of groups.
task: The task to compute. Can be either ``demographic_parity`` or ``equal_oppotunity`` or ``all``.
task: The task to compute. Can be either ``demographic_parity`` or ``equal_opportunity`` or ``all``.
threshold: Threshold for transforming probability to binary {0,1} predictions.
ignore_index: Specifies a target value that is ignored and does not contribute to the metric calculation
validate_args: bool indicating if input arguments and tensors should be validated for correctness.
Expand Down
58 changes: 29 additions & 29 deletions src/torchmetrics/detection/_mean_ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ def __init__(
self.box_format = box_format
self.iou_thresholds = iou_thresholds or torch.linspace(0.5, 0.95, round((0.95 - 0.5) / 0.05) + 1).tolist()
self.rec_thresholds = rec_thresholds or torch.linspace(0.0, 1.00, round(1.00 / 0.01) + 1).tolist()
max_det_thr, _ = torch.sort(IntTensor(max_detection_thresholds or [1, 10, 100]))
self.max_detection_thresholds = max_det_thr.tolist()
max_det_threshold, _ = torch.sort(IntTensor(max_detection_thresholds or [1, 10, 100]))
self.max_detection_thresholds = max_det_threshold.tolist()
if iou_type not in allowed_iou_types:
raise ValueError(f"Expected argument `iou_type` to be one of {allowed_iou_types} but got {iou_type}")
if iou_type == "segm" and not _PYCOCOTOOLS_AVAILABLE:
Expand Down Expand Up @@ -621,12 +621,12 @@ def _evaluate_image(

@staticmethod
def _find_best_gt_match(
thr: int, gt_matches: Tensor, idx_iou: float, gt_ignore: Tensor, ious: Tensor, idx_det: int
threshold: int, gt_matches: Tensor, idx_iou: float, gt_ignore: Tensor, ious: Tensor, idx_det: int
) -> int:
"""Return id of best ground truth match with current detection.
Args:
thr:
threshold:
Current threshold value.
gt_matches:
Tensor showing if a ground truth matches for threshold ``t`` exists.
Expand All @@ -645,7 +645,7 @@ def _find_best_gt_match(
remove_mask = previously_matched | gt_ignore
gt_ious = ious[idx_det] * ~remove_mask
match_idx = gt_ious.argmax().item()
if gt_ious[match_idx] > thr: # type: ignore[index]
if gt_ious[match_idx] > threshold: # type: ignore[index]
return match_idx # type: ignore[return-value]
return -1

Expand Down Expand Up @@ -679,16 +679,16 @@ def _summarize(
prec = results["precision"]
# IoU
if iou_threshold is not None:
thr = self.iou_thresholds.index(iou_threshold)
prec = prec[thr, :, :, area_inds, mdet_inds]
threshold = self.iou_thresholds.index(iou_threshold)
prec = prec[threshold, :, :, area_inds, mdet_inds]
else:
prec = prec[:, :, :, area_inds, mdet_inds]
else:
# dimension of recall: [TxKxAxM]
prec = results["recall"]
if iou_threshold is not None:
thr = self.iou_thresholds.index(iou_threshold)
prec = prec[thr, :, :, area_inds, mdet_inds]
threshold = self.iou_thresholds.index(iou_threshold)
prec = prec[threshold, :, :, area_inds, mdet_inds]
else:
prec = prec[:, :, area_inds, mdet_inds]

Expand Down Expand Up @@ -723,26 +723,26 @@ def _calculate(self, class_ids: List) -> Tuple[MAPMetricResults, MARMetricResult
num_rec_thrs = len(self.rec_thresholds)
num_classes = len(class_ids)
num_bbox_areas = len(self.bbox_area_ranges)
num_max_det_thrs = len(self.max_detection_thresholds)
num_max_det_thresholds = len(self.max_detection_thresholds)
num_imgs = len(img_ids)
precision = -torch.ones((num_iou_thrs, num_rec_thrs, num_classes, num_bbox_areas, num_max_det_thrs))
recall = -torch.ones((num_iou_thrs, num_classes, num_bbox_areas, num_max_det_thrs))
scores = -torch.ones((num_iou_thrs, num_rec_thrs, num_classes, num_bbox_areas, num_max_det_thrs))
precision = -torch.ones((num_iou_thrs, num_rec_thrs, num_classes, num_bbox_areas, num_max_det_thresholds))
recall = -torch.ones((num_iou_thrs, num_classes, num_bbox_areas, num_max_det_thresholds))
scores = -torch.ones((num_iou_thrs, num_rec_thrs, num_classes, num_bbox_areas, num_max_det_thresholds))

# move tensors if necessary
rec_thresholds_tensor = torch.tensor(self.rec_thresholds)

# retrieve E at each category, area range, and max number of detections
for idx_cls, _ in enumerate(class_ids):
for idx_bbox_area, _ in enumerate(self.bbox_area_ranges):
for idx_max_det_thrs, max_det in enumerate(self.max_detection_thresholds):
for idx_max_det_thresholds, max_det in enumerate(self.max_detection_thresholds):
recall, precision, scores = MeanAveragePrecision.__calculate_recall_precision_scores(
recall,
precision,
scores,
idx_cls=idx_cls,
idx_bbox_area=idx_bbox_area,
idx_max_det_thrs=idx_max_det_thrs,
idx_max_det_thresholds=idx_max_det_thresholds,
eval_imgs=eval_imgs,
rec_thresholds=rec_thresholds_tensor,
max_det=max_det,
Expand All @@ -764,26 +764,26 @@ def _summarize_results(self, precisions: Tensor, recalls: Tensor) -> Tuple[MAPMe
"""
results = {"precision": precisions, "recall": recalls}
map_metrics = MAPMetricResults()
last_max_det_thr = self.max_detection_thresholds[-1]
map_metrics.map = self._summarize(results, True, max_dets=last_max_det_thr)
last_max_det_threshold = self.max_detection_thresholds[-1]
map_metrics.map = self._summarize(results, True, max_dets=last_max_det_threshold)
if 0.5 in self.iou_thresholds:
map_metrics.map_50 = self._summarize(results, True, iou_threshold=0.5, max_dets=last_max_det_thr)
map_metrics.map_50 = self._summarize(results, True, iou_threshold=0.5, max_dets=last_max_det_threshold)
else:
map_metrics.map_50 = torch.tensor([-1])
if 0.75 in self.iou_thresholds:
map_metrics.map_75 = self._summarize(results, True, iou_threshold=0.75, max_dets=last_max_det_thr)
map_metrics.map_75 = self._summarize(results, True, iou_threshold=0.75, max_dets=last_max_det_threshold)
else:
map_metrics.map_75 = torch.tensor([-1])
map_metrics.map_small = self._summarize(results, True, area_range="small", max_dets=last_max_det_thr)
map_metrics.map_medium = self._summarize(results, True, area_range="medium", max_dets=last_max_det_thr)
map_metrics.map_large = self._summarize(results, True, area_range="large", max_dets=last_max_det_thr)
map_metrics.map_small = self._summarize(results, True, area_range="small", max_dets=last_max_det_threshold)
map_metrics.map_medium = self._summarize(results, True, area_range="medium", max_dets=last_max_det_threshold)
map_metrics.map_large = self._summarize(results, True, area_range="large", max_dets=last_max_det_threshold)

mar_metrics = MARMetricResults()
for max_det in self.max_detection_thresholds:
mar_metrics[f"mar_{max_det}"] = self._summarize(results, False, max_dets=max_det)
mar_metrics.mar_small = self._summarize(results, False, area_range="small", max_dets=last_max_det_thr)
mar_metrics.mar_medium = self._summarize(results, False, area_range="medium", max_dets=last_max_det_thr)
mar_metrics.mar_large = self._summarize(results, False, area_range="large", max_dets=last_max_det_thr)
mar_metrics.mar_small = self._summarize(results, False, area_range="small", max_dets=last_max_det_threshold)
mar_metrics.mar_medium = self._summarize(results, False, area_range="medium", max_dets=last_max_det_threshold)
mar_metrics.mar_large = self._summarize(results, False, area_range="large", max_dets=last_max_det_threshold)

return map_metrics, mar_metrics

Expand All @@ -794,7 +794,7 @@ def __calculate_recall_precision_scores(
scores: Tensor,
idx_cls: int,
idx_bbox_area: int,
idx_max_det_thrs: int,
idx_max_det_thresholds: int,
eval_imgs: list,
rec_thresholds: Tensor,
max_det: int,
Expand Down Expand Up @@ -838,7 +838,7 @@ def __calculate_recall_precision_scores(
prec = torch.zeros((num_rec_thrs,))
score = torch.zeros((num_rec_thrs,))

recall[idx, idx_cls, idx_bbox_area, idx_max_det_thrs] = rc[-1] if tp_len else 0
recall[idx, idx_cls, idx_bbox_area, idx_max_det_thresholds] = rc[-1] if tp_len else 0

# Remove zigzags for AUC
diff_zero = torch.zeros((1,), device=pr.device)
Expand All @@ -852,8 +852,8 @@ def __calculate_recall_precision_scores(
inds = inds[:num_inds] # type: ignore[misc]
prec[:num_inds] = pr[inds] # type: ignore[misc]
score[:num_inds] = det_scores_sorted[inds] # type: ignore[misc]
precision[idx, :, idx_cls, idx_bbox_area, idx_max_det_thrs] = prec
scores[idx, :, idx_cls, idx_bbox_area, idx_max_det_thrs] = score
precision[idx, :, idx_cls, idx_bbox_area, idx_max_det_thresholds] = prec
scores[idx, :, idx_cls, idx_bbox_area, idx_max_det_thresholds] = score

return recall, precision, scores

Expand Down
4 changes: 2 additions & 2 deletions src/torchmetrics/detection/mean_ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ def __init__(
"When providing a list of max detection thresholds it should have length 3."
" Got value {len(max_detection_thresholds)}"
)
max_det_thr, _ = torch.sort(torch.tensor(max_detection_thresholds or [1, 10, 100], dtype=torch.int))
self.max_detection_thresholds = max_det_thr.tolist()
max_det_threshold, _ = torch.sort(torch.tensor(max_detection_thresholds or [1, 10, 100], dtype=torch.int))
self.max_detection_thresholds = max_det_threshold.tolist()

if not isinstance(class_metrics, bool):
raise ValueError("Expected argument `class_metrics` to be a boolean")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def binary_fairness(
preds: Tensor with predictions.
target: Tensor with true labels (not required for demographic_parity).
groups: Tensor with group identifiers. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.
task: The task to compute. Can be either ``demographic_parity`` or ``equal_oppotunity`` or ``all``.
task: The task to compute. Can be either ``demographic_parity`` or ``equal_opportunity`` or ``all``.
threshold: Threshold for transforming probability to binary {0,1} predictions.
ignore_index:
Specifies a target value that is ignored and does not contribute to the metric calculation
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/utilities/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
partial(PearsonsContingencyCoefficient, num_classes=5),
_nominal_input,
_nominal_input,
id="pearson contigency coef",
id="pearson contingency coef",
),
pytest.param(partial(TheilsU, num_classes=5), _nominal_input, _nominal_input, id="theils U"),
pytest.param(partial(TschuprowsT, num_classes=5), _nominal_input, _nominal_input, id="tschuprows T"),
Expand Down

0 comments on commit ca7c8eb

Please sign in to comment.