From f9e43f958f9f3d558cd9f945fee0e26e87a6ce2b Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 3 Dec 2024 21:47:02 +0100 Subject: [PATCH 1/7] Fix todo in pycocotools Adds numpy types. All the types were already there as comments. --- stubs/pycocotools/METADATA.toml | 1 + stubs/pycocotools/pycocotools/coco.pyi | 17 ++++++----------- stubs/pycocotools/pycocotools/cocoeval.pyi | 22 ++++------------------ stubs/pycocotools/pycocotools/mask.pyi | 16 +++++++--------- 4 files changed, 18 insertions(+), 38 deletions(-) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index 3dd21238383c..cd764fe93749 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,2 +1,3 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" +requires = ["numpy>=2.0.0rc1"] diff --git a/stubs/pycocotools/pycocotools/coco.pyi b/stubs/pycocotools/pycocotools/coco.pyi index 3cac74aeda72..dfb4b3ca4c7c 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 -from . import _EncodedRLE +import numpy as np +import numpy.typing as npt -# TODO: Use numpy types when #5768 is resolved. -# import numpy as np -# import numpy.typing as npt +from . import _EncodedRLE -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 5dedb9bf1aea..6cd66978b833 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 -from .coco import COCO +import numpy as np +import numpy.typing as npt -# TODO: Use numpy types when #5768 is resolved. -# import numpy as np -# import numpy.typing as npt +from .coco import COCO -_NDArray: TypeAlias = Incomplete +_NDArray: TypeAlias = npt.NDArray[np.float64] _TIOU: TypeAlias = Literal["segm", "bbox", "keypoints"] class _ImageEvaluationResult(TypedDict): @@ -19,24 +17,17 @@ class _ImageEvaluationResult(TypedDict): dtIds: list[int] gtIds: list[int] dtMatches: _NDArray - # dtMatches: npt.NDArray[np.float64] gtMatches: _NDArray - # gtMatches: npt.NDArray[np.float64] dtScores: list[float] gtIgnore: _NDArray - # gtIgnore: npt.NDArray[np.float64] dtIgnore: _NDArray - # dtIgnore: npt.NDArray[np.float64] 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 class COCOeval: @@ -46,13 +37,11 @@ class COCOeval: eval: _EvaluationResult params: Params stats: _NDArray - # stats: npt.NDArray[np.float64] 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 evaluateImg(self, imgId: int, catId: int, aRng: list[int], maxDet: int) -> _ImageEvaluationResult: ... def accumulate(self, p: Params | None = None) -> None: ... def summarize(self) -> None: ... @@ -61,15 +50,12 @@ class Params: imgIds: list[int] catIds: list[int] iouThrs: _NDArray - # iouThrs: npt.NDArray[np.float64] recThrs: _NDArray - # recThrs: npt.NDArray[np.float64] maxDets: list[int] areaRng: list[list[float]] areaRngLbl: list[str] useCats: int kpt_oks_sigmas: _NDArray - # kpt_oks_sigmas: npt.NDArray[np.float64] 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 37825152c91f..3ce14d3b1f4a 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 -from . import _EncodedRLE +import numpy as np +import numpy.typing as npt -# TODO: Use numpy types when #5768 is resolved. -# import numpy as np -# import numpy.typing as npt +from . import _EncodedRLE -_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], From f9c05a12a7db420c7c6145f1b2782c6e96943bad Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 3 Dec 2024 21:54:20 +0100 Subject: [PATCH 2/7] Bump python version --- stubs/pycocotools/METADATA.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index cd764fe93749..0b4d0c92e407 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,3 +1,4 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" requires = ["numpy>=2.0.0rc1"] +requires_python = ">=3.9" From 03d68965f7acbb1d590bb80297ea67936944437a Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 3 Dec 2024 22:02:59 +0100 Subject: [PATCH 3/7] Fix numpy version --- stubs/pycocotools/METADATA.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index 0b4d0c92e407..ba56d9d33371 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,4 +1,5 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" -requires = ["numpy>=2.0.0rc1"] +requires = ["numpy>=2.0.0"] +# numpy>=2.0.0 requires Python >=3.9 requires_python = ">=3.9" From a83b860e119a8780f09e04e4c6856b4765bd63eb Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 3 Dec 2024 22:10:03 +0100 Subject: [PATCH 4/7] Relax numpy version --- stubs/pycocotools/METADATA.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index ba56d9d33371..7d10e4ee7722 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,5 +1,5 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" -requires = ["numpy>=2.0.0"] +requires = ["numpy"] # numpy>=2.0.0 requires Python >=3.9 requires_python = ">=3.9" From 1e23fec372283d33c57a79eb1d5290098c9c9be0 Mon Sep 17 00:00:00 2001 From: kbaikov Date: Wed, 4 Dec 2024 10:08:35 +0100 Subject: [PATCH 5/7] Rename the ND array to float array --- stubs/pycocotools/pycocotools/cocoeval.pyi | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/stubs/pycocotools/pycocotools/cocoeval.pyi b/stubs/pycocotools/pycocotools/cocoeval.pyi index 6cd66978b833..262b9dc3f8dd 100644 --- a/stubs/pycocotools/pycocotools/cocoeval.pyi +++ b/stubs/pycocotools/pycocotools/cocoeval.pyi @@ -6,7 +6,7 @@ import numpy.typing as npt from .coco import COCO -_NDArray: TypeAlias = npt.NDArray[np.float64] +_NDFloatArray: TypeAlias = npt.NDArray[np.float64] _TIOU: TypeAlias = Literal["segm", "bbox", "keypoints"] class _ImageEvaluationResult(TypedDict): @@ -16,19 +16,19 @@ class _ImageEvaluationResult(TypedDict): maxDet: int dtIds: list[int] gtIds: list[int] - dtMatches: _NDArray - gtMatches: _NDArray + dtMatches: _NDFloatArray + gtMatches: _NDFloatArray dtScores: list[float] - gtIgnore: _NDArray - dtIgnore: _NDArray + gtIgnore: _NDFloatArray + dtIgnore: _NDFloatArray class _EvaluationResult(TypedDict): params: Params counts: list[int] date: str - precision: _NDArray - recall: _NDArray - scores: _NDArray + precision: _NDFloatArray + recall: _NDFloatArray + scores: _NDFloatArray class COCOeval: cocoGt: COCO @@ -36,12 +36,12 @@ class COCOeval: evalImgs: list[_ImageEvaluationResult] eval: _EvaluationResult params: Params - stats: _NDArray + 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) -> _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: ... @@ -49,13 +49,13 @@ class COCOeval: class Params: imgIds: list[int] catIds: list[int] - iouThrs: _NDArray - recThrs: _NDArray + iouThrs: _NDFloatArray + recThrs: _NDFloatArray maxDets: list[int] areaRng: list[list[float]] areaRngLbl: list[str] useCats: int - kpt_oks_sigmas: _NDArray + kpt_oks_sigmas: _NDFloatArray iouType: _TIOU useSegm: int | None def __init__(self, iouType: _TIOU = "segm") -> None: ... From efb1a539b901b686fa062e73693c6b7f96f15ad7 Mon Sep 17 00:00:00 2001 From: kbaikov Date: Wed, 4 Dec 2024 10:09:21 +0100 Subject: [PATCH 6/7] Bump numpy to 2.0.0rc1 --- stubs/pycocotools/METADATA.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index 7d10e4ee7722..e4ce0fee133e 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,5 +1,5 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" -requires = ["numpy"] +requires = ["numpy>=2.0.0rc1"] # numpy>=2.0.0 requires Python >=3.9 requires_python = ">=3.9" From 7f84027eaeea1d8fee73a9c6e464778ccb7ac6a6 Mon Sep 17 00:00:00 2001 From: kbaikov Date: Wed, 4 Dec 2024 11:26:45 +0100 Subject: [PATCH 7/7] Restrict python version in requires too Using semi-colon is important here. --- stubs/pycocotools/METADATA.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index e4ce0fee133e..598dc57162b7 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,5 +1,5 @@ version = "2.0.*" upstream_repository = "https://github.com/ppwwyyxx/cocoapi" -requires = ["numpy>=2.0.0rc1"] +requires = ["numpy>=2.0.0rc1; python_version>='3.9'"] # numpy>=2.0.0 requires Python >=3.9 requires_python = ">=3.9"