Skip to content

Commit

Permalink
pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
czaloom committed Dec 12, 2024
1 parent 2f3ccab commit f07a71e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 3 additions & 6 deletions src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 7 additions & 5 deletions src/valor_lite/classification/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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,
)

Expand Down
5 changes: 2 additions & 3 deletions src/valor_lite/classification/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

@dataclass
class Filter:
indices: NDArray[np.int32]
indices: NDArray[np.intp]
label_metadata: NDArray[np.int32]
n_datums: int

Expand Down Expand Up @@ -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,
Expand Down
32 changes: 18 additions & 14 deletions src/valor_lite/object_detection/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -907,4 +911,4 @@ def compute_confusion_matrix(
confusion_matrix,
unmatched_predictions,
unmatched_ground_truths,
)
) # type: ignore[reportReturnType]
7 changes: 4 additions & 3 deletions src/valor_lite/object_detection/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/valor_lite/object_detection/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion src/valor_lite/semantic_segmentation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

@dataclass
class Filter:
indices: NDArray[np.int32]
indices: NDArray[np.intp]
label_metadata: NDArray[np.int32]
n_pixels: int

Expand Down

0 comments on commit f07a71e

Please sign in to comment.