Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing errors. #72

Merged
merged 10 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ install_requires =
matplotlib
nox
numba
numpy>=1.21.5
numpy
opencv-python
pandas
pyarrow
Expand Down
34 changes: 17 additions & 17 deletions src/av2/datasets/motion_forecasting/eval/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import numpy as np

from av2.utils.typing import NDArrayBool, NDArrayFloat, NDArrayNumber
from av2.utils.typing import NDArrayBool, NDArrayFloat


def compute_ade(forecasted_trajectories: NDArrayNumber, gt_trajectory: NDArrayNumber) -> NDArrayFloat:
def compute_ade(forecasted_trajectories: NDArrayFloat, gt_trajectory: NDArrayFloat) -> NDArrayFloat:
"""Compute the average displacement error for a set of K predicted trajectories (for the same actor).

Args:
Expand All @@ -16,12 +16,12 @@ def compute_ade(forecasted_trajectories: NDArrayNumber, gt_trajectory: NDArrayNu
Returns:
(K,) Average displacement error for each of the predicted trajectories.
"""
displacement_errors = np.linalg.norm(forecasted_trajectories - gt_trajectory, axis=2) # type: ignore
displacement_errors = np.linalg.norm(forecasted_trajectories - gt_trajectory, axis=2)
ade: NDArrayFloat = np.mean(displacement_errors, axis=1)
return ade


def compute_fde(forecasted_trajectories: NDArrayNumber, gt_trajectory: NDArrayNumber) -> NDArrayFloat:
def compute_fde(forecasted_trajectories: NDArrayFloat, gt_trajectory: NDArrayFloat) -> NDArrayFloat:
"""Compute the final displacement error for a set of K predicted trajectories (for the same actor).

Args:
Expand All @@ -32,14 +32,14 @@ def compute_fde(forecasted_trajectories: NDArrayNumber, gt_trajectory: NDArrayNu
(K,) Final displacement error for each of the predicted trajectories.
"""
# Compute final displacement error for all K trajectories
fde_vector = (forecasted_trajectories - gt_trajectory)[:, -1] # type: ignore
fde: NDArrayFloat = np.linalg.norm(fde_vector, axis=-1) # type: ignore
fde_vector = (forecasted_trajectories - gt_trajectory)[:, -1]
fde: NDArrayFloat = np.linalg.norm(fde_vector, axis=-1)
return fde


def compute_is_missed_prediction(
forecasted_trajectories: NDArrayNumber,
gt_trajectory: NDArrayNumber,
forecasted_trajectories: NDArrayFloat,
gt_trajectory: NDArrayFloat,
miss_threshold_m: float = 2.0,
) -> NDArrayBool:
"""Compute whether each of K predicted trajectories (for the same actor) missed by more than a distance threshold.
Expand All @@ -53,14 +53,14 @@ def compute_is_missed_prediction(
(K,) Bools indicating whether prediction missed by more than specified threshold.
"""
fde = compute_fde(forecasted_trajectories, gt_trajectory)
is_missed_prediction = fde > miss_threshold_m # type: ignore
is_missed_prediction = fde > miss_threshold_m
return is_missed_prediction


def compute_brier_ade(
forecasted_trajectories: NDArrayNumber,
gt_trajectory: NDArrayNumber,
forecast_probabilities: NDArrayNumber,
forecasted_trajectories: NDArrayFloat,
gt_trajectory: NDArrayFloat,
forecast_probabilities: NDArrayFloat,
normalize: bool = False,
) -> NDArrayFloat:
"""Compute a probability-weighted (using Brier score) ADE for K predicted trajectories (for the same actor).
Expand All @@ -82,9 +82,9 @@ def compute_brier_ade(


def compute_brier_fde(
forecasted_trajectories: NDArrayNumber,
gt_trajectory: NDArrayNumber,
forecast_probabilities: NDArrayNumber,
forecasted_trajectories: NDArrayFloat,
gt_trajectory: NDArrayFloat,
forecast_probabilities: NDArrayFloat,
normalize: bool = False,
) -> NDArrayFloat:
"""Compute a probability-weighted (using Brier score) FDE for K predicted trajectories (for the same actor).
Expand All @@ -106,8 +106,8 @@ def compute_brier_fde(


def _compute_brier_score(
forecasted_trajectories: NDArrayNumber,
forecast_probabilities: NDArrayNumber,
forecasted_trajectories: NDArrayFloat,
forecast_probabilities: NDArrayFloat,
normalize: bool = False,
) -> NDArrayFloat:
"""Compute Brier score for K predicted trajectories.
Expand Down
4 changes: 2 additions & 2 deletions src/av2/datasets/motion_forecasting/eval/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Final, List, Tuple
from typing import Any, Dict, Final, List, Tuple

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -91,7 +91,7 @@ def from_parquet(cls, submission_file_path: Path) -> ChallengeSubmission:
submission_df.sort_values(by="probability", inplace=True, ascending=False)

# From serialized data, build scenario-track mapping for predictions
submission_dict: Dict[str, ScenarioPredictions] = defaultdict(lambda: defaultdict(dict)) # type: ignore
submission_dict: Dict[str, Any] = defaultdict(lambda: defaultdict(dict))
for (scenario_id, track_id), track_df in submission_df.groupby(["scenario_id", "track_id"]):
predicted_trajectories_x = np.stack(track_df.loc[:, "predicted_trajectory_x"].values.tolist())
predicted_trajectories_y = np.stack(track_df.loc[:, "predicted_trajectory_y"].values.tolist())
Expand Down
6 changes: 3 additions & 3 deletions src/av2/datasets/sensor/av2_sensor_dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def get_colored_sweep(self, log_id: str, lidar_timestamp_ns: int) -> NDArrayByte
lidar_timestamp_ns=lidar_timestamp_ns,
log_id=log_id,
)
uv_valid = np.round(uv[is_valid]).astype(np.int64) # type: ignore
uv_valid = np.round(uv[is_valid]).astype(np.int64)
u = uv_valid[:, 0]
v = uv_valid[:, 1]
img = io_utils.read_img(img_fpath, channel_order="RGB")
Expand Down Expand Up @@ -412,8 +412,8 @@ def get_depth_map_from_lidar(
if is_valid_points is None or is_valid_points.sum() == 0:
return None

u = np.round(uv[:, 0][is_valid_points]).astype(np.int32) # type: ignore
v = np.round(uv[:, 1][is_valid_points]).astype(np.int32) # type: ignore
u = np.round(uv[:, 0][is_valid_points]).astype(np.int32)
v = np.round(uv[:, 1][is_valid_points]).astype(np.int32)
z = points_cam[:, 2][is_valid_points]

depth_map: NDArrayFloat = np.zeros((height_px, width_px), dtype=np.float32)
Expand Down
4 changes: 2 additions & 2 deletions src/av2/evaluation/detection/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def evaluate(
dts_list, gts_list = zip(*outputs)

METRIC_COLUMN_NAMES = cfg.affinity_thresholds_m + TP_ERROR_COLUMNS + ("is_evaluated",)
dts_metrics: NDArrayFloat = np.concatenate(dts_list) # type: ignore
gts_metrics: NDArrayFloat = np.concatenate(gts_list) # type: ignore
dts_metrics: NDArrayFloat = np.concatenate(dts_list)
gts_metrics: NDArrayFloat = np.concatenate(gts_list)
dts.loc[:, METRIC_COLUMN_NAMES] = dts_metrics
gts.loc[:, METRIC_COLUMN_NAMES] = gts_metrics

Expand Down
24 changes: 12 additions & 12 deletions src/av2/evaluation/detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def accumulate(
is_evaluated_gts &= compute_objects_in_roi_mask(gts, city_SE3_ego, avm)

is_evaluated_dts &= compute_evaluated_dts_mask(dts[..., :3], cfg)
is_evaluated_gts &= compute_evaluated_gts_mask(gts[..., :3], gts[..., -1], cfg)
is_evaluated_gts &= compute_evaluated_gts_mask(gts[..., :3], gts[..., -1].astype(int), cfg)

# Initialize results array.
dts_augmented: NDArrayFloat = np.zeros((N, T + E + 1))
Expand All @@ -159,7 +159,7 @@ def accumulate(
gts_augmented[is_evaluated_gts, :-1] = gts_assignments

# Permute the detections according to the original ordering.
outputs: Tuple[NDArrayInt, NDArrayInt] = np.unique(permutation, return_index=True) # type: ignore
outputs: Tuple[NDArrayInt, NDArrayInt] = np.unique(permutation, return_index=True)
_, inverse_permutation = outputs
dts_augmented = dts_augmented[inverse_permutation]
return dts_augmented, gts_augmented
Expand Down Expand Up @@ -193,15 +193,15 @@ def assign(dts: NDArrayFloat, gts: NDArrayFloat, cfg: DetectionCfg) -> Tuple[NDA
affinity_matrix = compute_affinity_matrix(dts[..., :3], gts[..., :3], cfg.affinity_type)

# Get the GT label for each max-affinity GT label, detection pair.
idx_gts = affinity_matrix.argmax(axis=1)[None]
idx_gts: NDArrayInt = affinity_matrix.argmax(axis=1)[None]

# The affinity matrix is an N by M matrix of the detections and ground truth labels respectively.
# We want to take the corresponding affinity for each of the initial assignments using `gt_matches`.
# The following line grabs the max affinity for each detection to a ground truth label.
affinities: NDArrayFloat = np.take_along_axis(affinity_matrix.transpose(), idx_gts, axis=0)[0] # type: ignore
affinities: NDArrayFloat = np.take_along_axis(affinity_matrix.transpose(), idx_gts, axis=0)[0]

# Find the indices of the _first_ detection assigned to each GT.
assignments: Tuple[NDArrayInt, NDArrayInt] = np.unique(idx_gts, return_index=True) # type: ignore
assignments: Tuple[NDArrayInt, NDArrayInt] = np.unique(idx_gts, return_index=True)
idx_gts, idx_dts = assignments

T, E = len(cfg.affinity_thresholds_m), 3
Expand Down Expand Up @@ -313,7 +313,7 @@ def compute_average_precision(
precision = interpolate_precision(precision)

# Evaluate precision at different recalls.
precision_interpolated: NDArrayFloat = np.interp(recall_interpolated, recall, precision, right=0) # type: ignore
precision_interpolated: NDArrayFloat = np.interp(recall_interpolated, recall, precision, right=0)

average_precision: float = np.mean(precision_interpolated)
return average_precision, precision_interpolated
Expand All @@ -334,7 +334,7 @@ def distance(dts: NDArrayFloat, gts: NDArrayFloat, metric: DistanceType) -> NDAr
NotImplementedError: If the distance type is not supported.
"""
if metric == DistanceType.TRANSLATION:
translation_errors: NDArrayFloat = np.linalg.norm(dts - gts, axis=1) # type: ignore
translation_errors: NDArrayFloat = np.linalg.norm(dts - gts, axis=1)
return translation_errors
elif metric == DistanceType.SCALE:
scale_errors: NDArrayFloat = 1 - iou_3d_axis_aligned(dts, gts)
Expand Down Expand Up @@ -397,14 +397,14 @@ def compute_evaluated_dts_mask(
if len(xyz_m_ego) == 0:
is_evaluated = np.zeros((0,), dtype=bool)
return is_evaluated
norm: NDArrayFloat = np.linalg.norm(xyz_m_ego, axis=1) # type: ignore
norm: NDArrayFloat = np.linalg.norm(xyz_m_ego, axis=1)
is_evaluated = norm < cfg.max_range_m

cumsum: NDArrayInt = np.cumsum(is_evaluated)
max_idx_arr: NDArrayInt = np.where(cumsum > cfg.max_num_dts_per_category)[0]
if len(max_idx_arr) > 0:
max_idx = max_idx_arr[0]
is_evaluated[max_idx:] = False # type: ignore
is_evaluated[max_idx:] = False
return is_evaluated


Expand All @@ -431,7 +431,7 @@ def compute_evaluated_gts_mask(
if len(xyz_m_ego) == 0:
is_evaluated = np.zeros((0,), dtype=bool)
return is_evaluated
norm: NDArrayFloat = np.linalg.norm(xyz_m_ego, axis=1) # type: ignore
norm: NDArrayFloat = np.linalg.norm(xyz_m_ego, axis=1)
is_evaluated = np.logical_and(norm < cfg.max_range_m, num_interior_pts > 0)
return is_evaluated

Expand Down Expand Up @@ -471,8 +471,8 @@ def groupby(names: List[str], values: NDArrayFloat) -> Dict[str, NDArrayFloat]:
Returns:
Dictionary mapping the group name to the corresponding group.
"""
outputs: Tuple[NDArrayInt, NDArrayInt] = np.unique(names, return_index=True) # type: ignore
outputs: Tuple[NDArrayInt, NDArrayInt] = np.unique(names, return_index=True)
unique_items, unique_items_indices = outputs
dts_groups: List[NDArrayFloat] = np.split(values, unique_items_indices[1:]) # type: ignore
dts_groups: List[NDArrayFloat] = np.split(values, unique_items_indices[1:])
uuid_to_groups = {unique_items[i]: x for i, x in enumerate(dts_groups)}
return uuid_to_groups
10 changes: 5 additions & 5 deletions src/av2/geometry/camera/pinhole_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def right_clipping_plane(self) -> NDArrayFloat:
(4,) tuple of Hessian normal coefficients.
"""
a, b, c, d = -self.intrinsics.fx_px, 0.0, self.width_px / 2.0, 0.0
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c]) # type: ignore
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c])
return coeffs

@cached_property
Expand All @@ -272,7 +272,7 @@ def left_clipping_plane(self) -> NDArrayFloat:
(4,) tuple of Hessian normal coefficients.
"""
a, b, c, d = self.intrinsics.fx_px, 0.0, self.width_px / 2.0, 0.0
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c]) # type: ignore
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c])
return coeffs

@cached_property
Expand All @@ -283,7 +283,7 @@ def top_clipping_plane(self) -> NDArrayFloat:
(4,) tuple of Hessian normal coefficients.
"""
a, b, c, d = 0.0, self.intrinsics.fx_px, self.height_px / 2.0, 0.0
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c]) # type: ignore
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c])
return coeffs

@cached_property
Expand All @@ -294,7 +294,7 @@ def bottom_clipping_plane(self) -> NDArrayFloat:
(4,) tuple of Hessian normal coefficients.
"""
a, b, c, d = 0.0, -self.intrinsics.fx_px, self.height_px / 2.0, 0.0
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c]) # type: ignore
coeffs: NDArrayFloat = np.array([a, b, c, d]) / np.linalg.norm([a, b, c])
return coeffs

def near_clipping_plane(self, near_clip_m: float) -> NDArrayFloat:
Expand Down Expand Up @@ -402,7 +402,7 @@ def compute_pixel_ray_directions(self, uv: Union[NDArrayFloat, NDArrayInt]) -> N
ray_dirs[:, 2] = fx

# elementwise multiplication of scalars requires last dim to match
ray_dirs = ray_dirs / np.linalg.norm(ray_dirs, axis=1, keepdims=True) # type: ignore
ray_dirs = ray_dirs / np.linalg.norm(ray_dirs, axis=1, keepdims=True)
if ray_dirs.shape[1] != 3:
raise RuntimeError("Ray directions must be (N,3)")
return ray_dirs
Expand Down
2 changes: 1 addition & 1 deletion src/av2/geometry/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def compute_interior_points_mask(points_xyz: NDArrayFloat, cuboid_vertices: NDAr
sim_uvw_ref = uvw @ ref_vertex # (3,)

# Only care about the diagonal.
sim_uvw_vertices: NDArrayFloat = np.diag(uvw @ vertices.transpose()) # type: ignore # (3,)
sim_uvw_vertices: NDArrayFloat = np.diag(uvw @ vertices.transpose()) # (3,)

# Check 6 conditions (2 for each of the 3 orthogonal directions).
# Refer to the linked reference for additional information.
Expand Down
2 changes: 1 addition & 1 deletion src/av2/geometry/infinity_norm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ def has_pts_in_infinity_norm_radius(points: NDArrayFloat, window_center: NDArray
# reshape just in case was given column vector
window_center = window_center.reshape(1, 2)

dists = np.linalg.norm(points - window_center, ord=np.inf, axis=1) # type: ignore
dists = np.linalg.norm(points - window_center, ord=np.inf, axis=1)
return bool(dists.min() < window_sz)
16 changes: 8 additions & 8 deletions src/av2/geometry/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def compute_lane_width(left_even_pts: NDArrayFloat, right_even_pts: NDArrayFloat
raise ValueError(
f"Shape of left_even_pts {left_even_pts.shape} did not match right_even_pts {right_even_pts.shape}"
)
lane_width = float(np.mean(np.linalg.norm(left_even_pts - right_even_pts, axis=1))) # type: ignore
lane_width = float(np.mean(np.linalg.norm(left_even_pts - right_even_pts, axis=1)))
return lane_width


Expand All @@ -57,7 +57,7 @@ def compute_mid_pivot_arc(single_pt: NDArrayFloat, arc_pts: NDArrayFloat) -> Tup
"""
num_pts = len(arc_pts)
# form ladder with equal number of vertices on each side
single_pt_tiled = np.tile(single_pt, (num_pts, 1)) # type: ignore
single_pt_tiled = np.tile(single_pt, (num_pts, 1))
# compute midpoint for each rung of the ladder
centerline_pts = (single_pt_tiled + arc_pts) / 2.0
lane_width = compute_lane_width(single_pt_tiled, arc_pts)
Expand Down Expand Up @@ -111,7 +111,7 @@ def compute_midpoint_line(
left_even_pts = interp_arc(num_interp_pts, points=left_ln_boundary)
right_even_pts = interp_arc(num_interp_pts, points=right_ln_boundary)

centerline_pts = (left_even_pts + right_even_pts) / 2.0 # type: ignore
centerline_pts = (left_even_pts + right_even_pts) / 2.0

lane_width = compute_lane_width(left_even_pts, right_even_pts)
return centerline_pts, lane_width
Expand Down Expand Up @@ -151,7 +151,7 @@ def interp_arc(t: int, points: NDArrayFloat) -> NDArrayFloat:
# Compute the chordal arclength of each segment.
# Compute differences between each x coord, to get the dx's
# Do the same to get dy's. Then the hypotenuse length is computed as a norm.
chordlen: NDArrayFloat = np.linalg.norm(np.diff(points, axis=0), axis=1) # type: ignore
chordlen: NDArrayFloat = np.linalg.norm(np.diff(points, axis=0), axis=1)
# Normalize the arclengths to a unit total
chordlen = chordlen / np.sum(chordlen)
# cumulative arclength
Expand All @@ -160,10 +160,10 @@ def interp_arc(t: int, points: NDArrayFloat) -> NDArrayFloat:
cumarc[1:] = np.cumsum(chordlen)

# which interval did each point fall in, in terms of eq_spaced_points? (bin index)
tbins: NDArrayInt = np.digitize(eq_spaced_points, bins=cumarc).astype(int) # type: ignore
tbins: NDArrayInt = np.digitize(eq_spaced_points, bins=cumarc).astype(int)

# #catch any problems at the ends
tbins[np.where((tbins <= 0) | (eq_spaced_points <= 0))] = 1 # type: ignore
tbins[np.where((tbins <= 0) | (eq_spaced_points <= 0))] = 1
tbins[np.where((tbins >= n) | (eq_spaced_points >= 1))] = n - 1

s = np.divide((eq_spaced_points - cumarc[tbins - 1]), chordlen[tbins - 1])
Expand Down Expand Up @@ -198,8 +198,8 @@ def linear_interpolation(
interval = t1 - t0
t = (query_timestamp - t0) / interval

vec = key_translations[1] - key_translations[0] # type: ignore
translation_interp = key_translations[0] + vec * t # type: ignore
vec = key_translations[1] - key_translations[0]
translation_interp = key_translations[0] + vec * t
return translation_interp


Expand Down
2 changes: 1 addition & 1 deletion src/av2/geometry/mesh_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_mesh_grid_as_point_cloud(
ny = max_y - min_y
x = np.linspace(min_x, max_x, math.ceil(nx / downsample_factor) + 1)
y = np.linspace(min_y, max_y, math.ceil(ny / downsample_factor) + 1)
x_grid, y_grid = np.meshgrid(x, y) # type: ignore
x_grid, y_grid = np.meshgrid(x, y)

x_grid = x_grid.flatten()
y_grid = y_grid.flatten()
Expand Down
8 changes: 4 additions & 4 deletions src/av2/geometry/polyline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def get_polyline_length(polyline: NDArrayFloat) -> float:
"""
if polyline.shape[1] not in [2, 3]:
raise RuntimeError("Polyline must have shape (N,2) or (N,3)")
offsets = np.diff(polyline, axis=0) # type: ignore
return float(np.linalg.norm(offsets, axis=1).sum()) # type: ignore
offsets = np.diff(polyline, axis=0)
return float(np.linalg.norm(offsets, axis=1).sum())


def interp_polyline_by_fixed_waypt_interval(polyline: NDArrayFloat, waypt_interval: float) -> Tuple[NDArrayFloat, int]:
Expand Down Expand Up @@ -130,12 +130,12 @@ def centerline_to_polygon(
Numpy array of shape (2N+1,2) or (2N+1,3), with duplicate first and last vertices.
"""
# eliminate duplicates
_, inds = np.unique(centerline, axis=0, return_index=True) # type: ignore
_, inds = np.unique(centerline, axis=0, return_index=True)
# does not return indices in sorted order
inds = np.sort(inds)
centerline = centerline[inds]

grad: NDArrayFloat = np.gradient(centerline, axis=0) # type: ignore
grad: NDArrayFloat = np.gradient(centerline, axis=0)
dx = grad[:, 0]
dy = grad[:, 1]

Expand Down
Loading