diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ad82a815c..c46072130 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -29,14 +29,13 @@ repos: args: [--line-length=79] - repo: https://github.com/RobertCraigie/pyright-python - rev: v1.1.376 + rev: v1.1.390 hooks: - id: pyright additional_dependencies: [ "numpy", "pytest", "python-dotenv", - "importlib_metadata; python_version < '3.8'", "tqdm", "packaging", "openai", diff --git a/src/pyproject.toml b/src/pyproject.toml index 938d07c37..1fe15d310 100644 --- a/src/pyproject.toml +++ b/src/pyproject.toml @@ -6,15 +6,12 @@ readme = "README.md" requires-python = ">=3.10" license = { file = "LICENSE" } dependencies = [ + "numpy", + "tqdm", + "shapely", "evaluate", - "importlib_metadata; python_version < '3.8'", "nltk", - "numpy", - "Pillow >= 9.1.0", - "requests", "rouge_score", - "shapely", - "tqdm", ] [project.urls] diff --git a/src/valor_lite/classification/computation.py b/src/valor_lite/classification/computation.py index 4a463a74d..14c84e233 100644 --- a/src/valor_lite/classification/computation.py +++ b/src/valor_lite/classification/computation.py @@ -212,7 +212,7 @@ def _count_with_examples( data: NDArray[np.float64], unique_idx: int | list[int], label_idx: int | list[int], -) -> tuple[NDArray[np.float64], NDArray[np.int32], NDArray[np.int32]]: +) -> tuple[NDArray[np.float64], NDArray[np.int32], NDArray[np.intp]]: """ Helper function for counting occurences of unique detailed pairs. @@ -231,7 +231,7 @@ def _count_with_examples( Examples drawn from the data input. NDArray[np.int32] Unique label indices. - NDArray[np.int32] + NDArray[np.intp] Counts for each unique label index. """ unique_rows, indices = np.unique( @@ -288,12 +288,14 @@ def compute_confusion_matrix( n_labels = label_metadata.shape[0] n_scores = score_thresholds.shape[0] - confusion_matrix = -1 * np.ones( + confusion_matrix = np.full( (n_scores, n_labels, n_labels, 2 * n_examples + 1), - dtype=np.float32, + fill_value=-1.0, + dtype=np.float64, ) - unmatched_ground_truths = -1 * np.ones( + unmatched_ground_truths = np.full( (n_scores, n_labels, n_examples + 1), + fill_value=-1, dtype=np.int32, ) diff --git a/src/valor_lite/classification/manager.py b/src/valor_lite/classification/manager.py index 4f312f3c9..3677114aa 100644 --- a/src/valor_lite/classification/manager.py +++ b/src/valor_lite/classification/manager.py @@ -39,7 +39,7 @@ @dataclass class Filter: - indices: NDArray[np.int32] + indices: NDArray[np.intp] label_metadata: NDArray[np.int32] n_datums: int @@ -170,8 +170,7 @@ def create_filter( label_metadata_per_datum = self._label_metadata_per_datum.copy() label_metadata_per_datum[:, ~mask] = 0 - label_metadata = np.zeros_like(self._label_metadata, dtype=np.int32) - label_metadata = np.transpose( + label_metadata: NDArray[np.int32] = np.transpose( np.sum( label_metadata_per_datum, axis=1, diff --git a/src/valor_lite/object_detection/computation.py b/src/valor_lite/object_detection/computation.py index 6e0e9163f..6d9d48ce1 100644 --- a/src/valor_lite/object_detection/computation.py +++ b/src/valor_lite/object_detection/computation.py @@ -381,9 +381,9 @@ def compute_precion_recall( _, indices_gt_unique = np.unique( tp_candidates[:, [0, 1, 4]], axis=0, return_index=True ) - mask_gt_unique = np.zeros(tp_candidates.shape[0], dtype=bool) + mask_gt_unique = np.zeros(tp_candidates.shape[0], dtype=np.bool_) mask_gt_unique[indices_gt_unique] = True - true_positives_mask = np.zeros(n_rows, dtype=bool) + true_positives_mask = np.zeros(n_rows, dtype=np.bool_) true_positives_mask[mask_tp_inner] = mask_gt_unique # calculate intermediates @@ -452,9 +452,9 @@ def compute_precion_recall( _, indices_gt_unique = np.unique( tp_candidates[:, [0, 1, 4]], axis=0, return_index=True ) - mask_gt_unique = np.zeros(tp_candidates.shape[0], dtype=bool) + mask_gt_unique = np.zeros(tp_candidates.shape[0], dtype=np.bool_) mask_gt_unique[indices_gt_unique] = True - true_positives_mask = np.zeros(n_rows, dtype=bool) + true_positives_mask = np.zeros(n_rows, dtype=np.bool_) true_positives_mask[mask_tp_outer] = mask_gt_unique # count running tp and total for AP @@ -501,8 +501,8 @@ def compute_precion_recall( ) # calculate average precision - running_max_precision = np.zeros((n_ious, n_labels)) - running_max_score = np.zeros((n_labels)) + running_max_precision = np.zeros((n_ious, n_labels), dtype=np.float64) + running_max_score = np.zeros((n_labels), dtype=np.float64) for recall in range(100, -1, -1): # running max precision @@ -528,8 +528,12 @@ def compute_precion_recall( # calculate mAP and mAR if unique_pd_labels.size > 0: - mAP = average_precision[:, unique_pd_labels].mean(axis=1) - mAR = average_recall[:, unique_pd_labels].mean(axis=1) + mAP: NDArray[np.float64] = average_precision[:, unique_pd_labels].mean( + axis=1 + ) + mAR: NDArray[np.float64] = average_recall[:, unique_pd_labels].mean( + axis=1 + ) else: mAP = np.zeros(n_ious, dtype=np.float64) mAR = np.zeros(n_scores, dtype=np.float64) @@ -561,14 +565,14 @@ def compute_precion_recall( accuracy, counts, pr_curve, - ) + ) # type: ignore[reportReturnType] def _count_with_examples( data: NDArray[np.float64], unique_idx: int | list[int], label_idx: int | list[int], -) -> tuple[NDArray[np.float64], NDArray[np.int32], NDArray[np.int32]]: +) -> tuple[NDArray[np.float64], NDArray[np.int32], NDArray[np.intp]]: """ Helper function for counting occurences of unique detailed pairs. @@ -587,7 +591,7 @@ def _count_with_examples( Examples drawn from the data input. NDArray[np.int32] Unique label indices. - NDArray[np.int32] + NDArray[np.intp] Counts for each unique label index. """ unique_rows, indices = np.unique( @@ -681,12 +685,12 @@ def compute_confusion_matrix( confusion_matrix = -1 * np.ones( # (datum idx, gt idx, pd idx, pd score) * n_examples + count (n_ious, n_scores, n_labels, n_labels, 4 * n_examples + 1), - dtype=np.float32, + dtype=np.float64, ) unmatched_predictions = -1 * np.ones( # (datum idx, pd idx, pd score) * n_examples + count (n_ious, n_scores, n_labels, 3 * n_examples + 1), - dtype=np.float32, + dtype=np.float64, ) unmatched_ground_truths = -1 * np.ones( # (datum idx, gt idx) * n_examples + count @@ -907,4 +911,4 @@ def compute_confusion_matrix( confusion_matrix, unmatched_predictions, unmatched_ground_truths, - ) + ) # type: ignore[reportReturnType] diff --git a/src/valor_lite/object_detection/manager.py b/src/valor_lite/object_detection/manager.py index 8758f29b1..6bfdacd6c 100644 --- a/src/valor_lite/object_detection/manager.py +++ b/src/valor_lite/object_detection/manager.py @@ -43,8 +43,8 @@ @dataclass class Filter: - ranked_indices: NDArray[np.int32] - detailed_indices: NDArray[np.int32] + ranked_indices: NDArray[np.intp] + detailed_indices: NDArray[np.intp] label_metadata: NDArray[np.int32] @@ -570,7 +570,8 @@ def add_bounding_boxes( [gt.extrema, pd.extrema] for pd in detection.predictions for gt in detection.groundtruths - ] + ], + dtype=np.float64, ) ).reshape(len(detection.predictions), len(detection.groundtruths)) for detection in detections diff --git a/src/valor_lite/object_detection/utilities.py b/src/valor_lite/object_detection/utilities.py index 9a35e97d6..a78a44971 100644 --- a/src/valor_lite/object_detection/utilities.py +++ b/src/valor_lite/object_detection/utilities.py @@ -137,10 +137,8 @@ def unpack_precision_recall_into_metric_lists( metrics[MetricType.PrecisionRecallCurve] = [ Metric.precision_recall_curve( - precisions=pr_curves[iou_idx, label_idx, :, 0] - .astype(float) - .tolist(), - scores=pr_curves[iou_idx, label_idx, :, 1].astype(float).tolist(), + precisions=pr_curves[iou_idx, label_idx, :, 0].tolist(), # type: ignore[reportArgumentType] + scores=pr_curves[iou_idx, label_idx, :, 1].tolist(), # type: ignore[reportArgumentType] iou_threshold=iou_threshold, label=label, ) diff --git a/src/valor_lite/semantic_segmentation/manager.py b/src/valor_lite/semantic_segmentation/manager.py index b915c8300..6961901b4 100644 --- a/src/valor_lite/semantic_segmentation/manager.py +++ b/src/valor_lite/semantic_segmentation/manager.py @@ -38,7 +38,7 @@ @dataclass class Filter: - indices: NDArray[np.int32] + indices: NDArray[np.intp] label_metadata: NDArray[np.int32] n_pixels: int