From 0ce397796c93bdf10b3092d3d20931a800929c68 Mon Sep 17 00:00:00 2001 From: Konstantin Baikov Date: Sat, 28 Dec 2024 05:06:19 +0100 Subject: [PATCH] Fix todo in pycocotools (#13182) Adds numpy types. All the types were already there as comments. --- stubs/pycocotools/METADATA.toml | 3 ++ stubs/pycocotools/pycocotools/coco.pyi | 17 +++----- stubs/pycocotools/pycocotools/cocoeval.pyi | 46 ++++++++-------------- stubs/pycocotools/pycocotools/mask.pyi | 16 ++++---- 4 files changed, 32 insertions(+), 50 deletions(-) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index 3dd212383..598dc5716 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,2 +1,5 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" +requires = ["numpy>=2.0.0rc1; python_version>='3.9'"] +# numpy>=2.0.0 requires Python >=3.9 +requires_python = ">=3.9" diff --git a/stubs/pycocotools/pycocotools/coco.pyi b/stubs/pycocotools/pycocotools/coco.pyi index 3cac74aed..dfb4b3ca4 100644 --- a/stubs/pycocotools/pycocotools/coco.pyi +++ b/stubs/pycocotools/pycocotools/coco.pyi @@ -1,17 +1,14 @@ -from _typeshed import Incomplete from collections.abc import Collection, Sequence from pathlib import Path from typing import Generic, Literal, TypedDict, TypeVar, overload from typing_extensions import TypeAlias +import numpy as np +import numpy.typing as npt + from . import _EncodedRLE -# TODO: Use numpy types when #5768 is resolved. -# import numpy as np -# import numpy.typing as npt - -PYTHON_VERSION: Incomplete -_NDArray: TypeAlias = Incomplete +PYTHON_VERSION: int class _Image(TypedDict): id: int @@ -82,13 +79,11 @@ class COCO: def showAnns(self, anns: Sequence[_Annotation], draw_bbox: bool = False) -> None: ... def loadRes(self, resFile: str) -> COCO: ... def download(self, tarDir: str | None = None, imgIds: Collection[int] = []) -> Literal[-1] | None: ... - def loadNumpyAnnotations(self, data: _NDArray) -> list[_Annotation]: ... - # def loadNumpyAnnotations(self, data: npt.NDArray[np.float64]) -> list[_Annotation]: ... + def loadNumpyAnnotations(self, data: npt.NDArray[np.float64]) -> list[_Annotation]: ... @overload def annToRLE(self, ann: _AnnotationG[_RLE]) -> _RLE: ... @overload def annToRLE(self, ann: _AnnotationG[_EncodedRLE]) -> _EncodedRLE: ... @overload def annToRLE(self, ann: _AnnotationG[_TPolygonSegmentation]) -> _EncodedRLE: ... - def annToMask(self, ann: _Annotation) -> _NDArray: ... - # def annToMask(self, ann: _Annotation) -> npt.NDArray[np.uint8]: ... + def annToMask(self, ann: _Annotation) -> npt.NDArray[np.uint8]: ... diff --git a/stubs/pycocotools/pycocotools/cocoeval.pyi b/stubs/pycocotools/pycocotools/cocoeval.pyi index 5dedb9bf1..262b9dc3f 100644 --- a/stubs/pycocotools/pycocotools/cocoeval.pyi +++ b/stubs/pycocotools/pycocotools/cocoeval.pyi @@ -1,14 +1,12 @@ -from _typeshed import Incomplete from typing import Literal, TypedDict from typing_extensions import TypeAlias +import numpy as np +import numpy.typing as npt + from .coco import COCO -# TODO: Use numpy types when #5768 is resolved. -# import numpy as np -# import numpy.typing as npt - -_NDArray: TypeAlias = Incomplete +_NDFloatArray: TypeAlias = npt.NDArray[np.float64] _TIOU: TypeAlias = Literal["segm", "bbox", "keypoints"] class _ImageEvaluationResult(TypedDict): @@ -18,26 +16,19 @@ class _ImageEvaluationResult(TypedDict): maxDet: int dtIds: list[int] gtIds: list[int] - dtMatches: _NDArray - # dtMatches: npt.NDArray[np.float64] - gtMatches: _NDArray - # gtMatches: npt.NDArray[np.float64] + dtMatches: _NDFloatArray + gtMatches: _NDFloatArray dtScores: list[float] - gtIgnore: _NDArray - # gtIgnore: npt.NDArray[np.float64] - dtIgnore: _NDArray - # dtIgnore: npt.NDArray[np.float64] + gtIgnore: _NDFloatArray + dtIgnore: _NDFloatArray class _EvaluationResult(TypedDict): params: Params counts: list[int] date: str - # precision: npt.NDArray[np.float64] - precision: _NDArray - # recall: npt.NDArray[np.float64] - recall: _NDArray - # scores: npt.NDArray[np.float64] - scores: _NDArray + precision: _NDFloatArray + recall: _NDFloatArray + scores: _NDFloatArray class COCOeval: cocoGt: COCO @@ -45,14 +36,12 @@ class COCOeval: evalImgs: list[_ImageEvaluationResult] eval: _EvaluationResult params: Params - stats: _NDArray - # stats: npt.NDArray[np.float64] + stats: _NDFloatArray ious: dict[tuple[int, int], list[float]] def __init__(self, cocoGt: COCO | None = None, cocoDt: COCO | None = None, iouType: _TIOU = "segm") -> None: ... def evaluate(self) -> None: ... def computeIoU(self, imgId: int, catId: int) -> list[float]: ... - def computeOks(self, imgId: int, catId: int) -> _NDArray: ... - # def computeOks(self, imgId: int, catId: int) -> npt.NDArray[np.float64]: ... + def computeOks(self, imgId: int, catId: int) -> _NDFloatArray: ... def evaluateImg(self, imgId: int, catId: int, aRng: list[int], maxDet: int) -> _ImageEvaluationResult: ... def accumulate(self, p: Params | None = None) -> None: ... def summarize(self) -> None: ... @@ -60,16 +49,13 @@ class COCOeval: class Params: imgIds: list[int] catIds: list[int] - iouThrs: _NDArray - # iouThrs: npt.NDArray[np.float64] - recThrs: _NDArray - # recThrs: npt.NDArray[np.float64] + iouThrs: _NDFloatArray + recThrs: _NDFloatArray maxDets: list[int] areaRng: list[list[float]] areaRngLbl: list[str] useCats: int - kpt_oks_sigmas: _NDArray - # kpt_oks_sigmas: npt.NDArray[np.float64] + kpt_oks_sigmas: _NDFloatArray iouType: _TIOU useSegm: int | None def __init__(self, iouType: _TIOU = "segm") -> None: ... diff --git a/stubs/pycocotools/pycocotools/mask.pyi b/stubs/pycocotools/pycocotools/mask.pyi index 37825152c..3ce14d3b1 100644 --- a/stubs/pycocotools/pycocotools/mask.pyi +++ b/stubs/pycocotools/pycocotools/mask.pyi @@ -1,17 +1,15 @@ -from _typeshed import Incomplete from typing import Any, overload from typing_extensions import TypeAlias +import numpy as np +import numpy.typing as npt + from . import _EncodedRLE -# TODO: Use numpy types when #5768 is resolved. -# import numpy as np -# import numpy.typing as npt - -_NPUInt32: TypeAlias = Incomplete # np.uint32 -_NDArrayUInt8: TypeAlias = Incomplete # npt.NDArray[np.uint8] -_NDArrayUInt32: TypeAlias = Incomplete # npt.NDArray[np.uint32] -_NDArrayFloat64: TypeAlias = Incomplete # npt.NDArray[np.float64] +_NPUInt32: TypeAlias = np.uint32 +_NDArrayUInt8: TypeAlias = npt.NDArray[np.uint8] +_NDArrayUInt32: TypeAlias = npt.NDArray[np.uint32] +_NDArrayFloat64: TypeAlias = npt.NDArray[np.float64] def iou( dt: _NDArrayUInt32 | list[float] | list[_EncodedRLE],