Skip to content

Commit

Permalink
Temporarily ignore 2 mypy type-var errors
Browse files Browse the repository at this point in the history
caused by the more precise type annotation of ``numpy.number`` comparison
operators introduced in NumPy 2.1, see:

python/typeshed#12562

Fix the following errors when running test_galaxy_packages on Python >=3.10:

```
galaxy/tool_util/verify/__init__.py:503: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "floating[Any]"  [type-var]
                iou_list.append(max(cc1_iou_list))
                                ^~~~~~~~~~~~~~~~~
galaxy/tool_util/verify/__init__.py:532: error: Value of type variable "SupportsRichComparisonT" of "min" cannot be "floating[Any]"  [type-var]
        return min(_multiobject_intersection_over_union(mask1, mask2, pin_labels))
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
  • Loading branch information
nsoranzo committed Aug 30, 2024
1 parent fdb61ac commit cb069ef
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/galaxy/tool_util/verify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ def files_contains(file1, file2, attributes=None):


def _singleobject_intersection_over_union(
mask1: "numpy.typing.NDArray",
mask2: "numpy.typing.NDArray",
mask1: "numpy.typing.NDArray[numpy.bool]",
mask2: "numpy.typing.NDArray[numpy.bool]",
) -> "numpy.floating":
return numpy.logical_and(mask1, mask2).sum() / numpy.logical_or(mask1, mask2).sum()

Expand All @@ -483,7 +483,7 @@ def _multiobject_intersection_over_union(
pin_labels: Optional[List[int]] = None,
repeat_reverse: bool = True,
) -> List["numpy.floating"]:
iou_list = []
iou_list: List[numpy.floating] = []
for label1 in numpy.unique(mask1):
cc1 = mask1 == label1

Expand All @@ -494,13 +494,13 @@ def _multiobject_intersection_over_union(

# Otherwise, use the object with the largest IoU value, excluding the pinned labels.
else:
cc1_iou_list = []
cc1_iou_list: List[numpy.floating] = []
for label2 in numpy.unique(mask2[cc1]):
if pin_labels is not None and label2 in pin_labels:
continue
cc2 = mask2 == label2
cc1_iou_list.append(_singleobject_intersection_over_union(cc1, cc2))
iou_list.append(max(cc1_iou_list))
iou_list.append(max(cc1_iou_list)) # type: ignore[type-var] # https://github.com/python/typeshed/issues/12562

if repeat_reverse:
iou_list.extend(_multiobject_intersection_over_union(mask2, mask1, pin_labels, repeat_reverse=False))
Expand All @@ -511,7 +511,7 @@ def _multiobject_intersection_over_union(
def intersection_over_union(
mask1: "numpy.typing.NDArray", mask2: "numpy.typing.NDArray", pin_labels: Optional[List[int]] = None
) -> "numpy.floating":
"""Compute the intersection over union (IoU) for the objects in two masks containing lables.
"""Compute the intersection over union (IoU) for the objects in two masks containing labels.
The IoU is computed for each uniquely labeled image region (object), and the overall minimum value is returned (i.e. the worst value).
To compute the IoU for each object, the corresponding object in the other mask needs to be determined.
Expand All @@ -529,7 +529,7 @@ def intersection_over_union(
count = sum(label in mask for mask in (mask1, mask2))
count_str = {1: "one", 2: "both"}
assert count == 2, f"Label {label} is pinned but missing in {count_str[2 - count]} of the images."
return min(_multiobject_intersection_over_union(mask1, mask2, pin_labels))
return min(_multiobject_intersection_over_union(mask1, mask2, pin_labels)) # type: ignore[type-var] # https://github.com/python/typeshed/issues/12562


def _parse_label_list(label_list_str: Optional[str]) -> List[int]:
Expand Down

0 comments on commit cb069ef

Please sign in to comment.