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

Normalise Python typing syntax to 3.8+ #2361

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions examples/python/api_demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Run all demos: `examples/python/api_demo/main.py`
* Run specific demo: `examples/python/api_demo/main.py --demo rects`
"""
from __future__ import annotations

import argparse
import logging
Expand Down
25 changes: 16 additions & 9 deletions examples/python/arkitscenes/download_dataset.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Copied from https://github.com/apple/ARKitScenes/blob/main/download_data.py
# Licensing information: https://github.com/apple/ARKitScenes/blob/main/LICENSE
from __future__ import annotations

import math
import os
import subprocess
import zipfile
from pathlib import Path
from typing import Final, List, Optional
from typing import Final

import pandas as pd

Expand Down Expand Up @@ -62,7 +64,7 @@
]


def raw_files(video_id: str, assets: List[str], metadata: pd.DataFrame) -> List[str]:
def raw_files(video_id: str, assets: list[str], metadata: pd.DataFrame) -> list[str]:
file_names = []
for asset in assets:
if HIGRES_DEPTH_ASSET_NAME == asset:
Expand Down Expand Up @@ -153,7 +155,7 @@ def download_laser_scanner_point_clouds_for_video(video_id: str, metadata: pd.Da
download_laser_scanner_point_clouds(point_cloud_id, visit_id, download_dir)


def laser_scanner_point_clouds_for_visit_id(visit_id: int, download_dir: Path) -> List[str]:
def laser_scanner_point_clouds_for_visit_id(visit_id: int, download_dir: Path) -> list[str]:
point_cloud_to_visit_id_mapping_filename = "laser_scanner_point_clouds_mapping.csv"
if not os.path.exists(point_cloud_to_visit_id_mapping_filename):
point_cloud_to_visit_id_mapping_url = (
Expand Down Expand Up @@ -209,11 +211,11 @@ def get_metadata(dataset: str, download_dir: Path) -> pd.DataFrame:

def download_data(
dataset: str,
video_ids: List[str],
dataset_splits: List[str],
video_ids: list[str],
dataset_splits: list[str],
download_dir: Path,
keep_zip: bool,
raw_dataset_assets: Optional[List[str]] = None,
raw_dataset_assets: list[str] | None = None,
should_download_laser_scanner_point_cloud: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -307,15 +309,20 @@ def ensure_recording_available(video_id: str, include_highres: bool) -> Path:
Returns the path to the recording for a given video_id.

Args:
video_id (str): Identifier for the recording.
----
video_id (str):
Identifier for the recording.
include_highres (bool):
Whether to include the high resolution recording.

Returns
-------
Path: Path object representing the path to the recording.
Path: Path object representing the path to the recording.

Raises
------
AssertionError: If the recording path does not exist.
AssertionError:
If the recording path does not exist.
"""
recording_path = ensure_recording_downloaded(video_id, include_highres)
assert recording_path.exists(), f"Recording path {recording_path} does not exist."
Expand Down
40 changes: 25 additions & 15 deletions examples/python/arkitscenes/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import json
import os
from pathlib import Path, PosixPath
from typing import Any, Dict, List, Tuple
from typing import Any, Tuple

import cv2
import matplotlib.pyplot as plt
Expand All @@ -30,13 +32,13 @@
assert set(ORIENTATION.keys()) == set(AVAILABLE_RECORDINGS)


def load_json(js_path: Path) -> Dict[str, Any]:
with open(js_path, "r") as f:
json_data = json.load(f) # type: Dict[str, Any]
def load_json(js_path: Path) -> dict[str, Any]:
with open(js_path) as f:
json_data: dict[str, Any] = json.load(f)
return json_data


def log_annotated_bboxes(annotation: Dict[str, Any]) -> Tuple[npt.NDArray[np.float64], List[str], List[Color]]:
def log_annotated_bboxes(annotation: dict[str, Any]) -> tuple[npt.NDArray[np.float64], list[str], list[Color]]:
"""
Logs annotated oriented bounding boxes to Rerun.

Expand Down Expand Up @@ -229,11 +231,11 @@ def project_3d_bboxes_to_2d_keypoints(
def log_camera(
intri_path: Path,
frame_id: str,
poses_from_traj: Dict[str, rr.TranslationRotationScale3D],
poses_from_traj: dict[str, rr.TranslationRotationScale3D],
entity_id: str,
bboxes: npt.NDArray[np.float64],
bbox_labels: List[str],
colors: List[Color],
bbox_labels: list[str],
colors: list[Color],
) -> None:
"""Logs camera transform and 3D bounding boxes in the image frame."""
w, h, fx, fy, cx, cy = np.loadtxt(intri_path)
Expand All @@ -256,12 +258,14 @@ def log_camera(
rr.log_pinhole(f"{entity_id}", child_from_parent=intrinsic, width=w, height=h)


def read_camera_from_world(traj_string: str) -> Tuple[str, rr.TranslationRotationScale3D]:
def read_camera_from_world(traj_string: str) -> tuple[str, rr.TranslationRotationScale3D]:
"""
Reads out camera_from_world transform from trajectory string.

Args:
traj_string: A space-delimited file where each line represents a camera position at a particular timestamp.
----
traj_string:
A space-delimited file where each line represents a camera position at a particular timestamp.
The file has seven columns:
* Column 1: timestamp
* Columns 2-4: rotation (axis-angle representation in radians)
Expand All @@ -276,7 +280,8 @@ def read_camera_from_world(traj_string: str) -> Tuple[str, rr.TranslationRotatio

Raises
------
AssertionError: If the input string does not contain 7 tokens.
AssertionError:
If the input string does not contain 7 tokens.
"""
tokens = traj_string.split() # Split the input string into tokens
assert len(tokens) == 7, f"Input string must have 7 tokens, but found {len(tokens)}."
Expand All @@ -295,7 +300,7 @@ def read_camera_from_world(traj_string: str) -> Tuple[str, rr.TranslationRotatio
return (ts, camera_from_world)


def find_closest_frame_id(target_id: str, frame_ids: Dict[str, Any]) -> str:
def find_closest_frame_id(target_id: str, frame_ids: dict[str, Any]) -> str:
"""Finds the closest frame id to the target id."""
target_value = float(target_id)
closest_id = min(frame_ids.keys(), key=lambda x: abs(float(x) - target_value))
Expand All @@ -307,11 +312,16 @@ def log_arkit(recording_path: Path, include_highres: bool) -> None:
Logs ARKit recording data using Rerun.

Args:
recording_path (Path): The path to the ARKit recording.
----
recording_path (Path):
The path to the ARKit recording.

include_highres (bool):
Whether to include high resolution data.

Returns
-------
None
None
"""
video_id = recording_path.stem
lowres_image_dir = recording_path / "lowres_wide"
Expand All @@ -330,7 +340,7 @@ def log_arkit(recording_path: Path, include_highres: bool) -> None:

# dict of timestamp to pose which is a tuple of translation and quaternion
camera_from_world_dict = {}
with open(traj_path, "r", encoding="utf-8") as f:
with open(traj_path, encoding="utf-8") as f:
trajectory = f.readlines()

for line in trajectory:
Expand Down
1 change: 1 addition & 0 deletions examples/python/blueprint/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Example of using the blueprint APIs to configure Rerun."""
from __future__ import annotations

import argparse

Expand Down
9 changes: 5 additions & 4 deletions examples/python/car/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3
"""Shows how to use the Rerun SDK."""
from __future__ import annotations

import argparse
from dataclasses import dataclass
from typing import Iterator, Tuple
from typing import Iterator

import cv2
import numpy as np
Expand Down Expand Up @@ -57,8 +58,8 @@ class DummyCar:

def __init__(
self,
center: Tuple[int, int],
size: Tuple[int, int],
center: tuple[int, int],
size: tuple[int, int],
distance_mm: float,
):
self.center: npt.NDArray[np.int32] = np.array(center, dtype=np.int32)
Expand Down Expand Up @@ -157,7 +158,7 @@ class SampleFrame:
camera: CameraParameters
depth_image_mm: npt.NDArray[np.float32]
rgb_image: npt.NDArray[np.float32]
car_bbox: Tuple[npt.NDArray[np.int32], npt.NDArray[np.int32]]
car_bbox: tuple[npt.NDArray[np.int32], npt.NDArray[np.int32]]


class SimpleDepthCamera:
Expand Down
6 changes: 3 additions & 3 deletions examples/python/clock/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env python3

"""
An example showing usage of `log_arrow`.

An analog clock is built with Rerun Arrow3D primitives.
"""
from __future__ import annotations

import argparse
import math
from typing import Final, Tuple
from typing import Final

import numpy as np
import rerun as rr # pip install rerun-sdk
Expand All @@ -23,7 +23,7 @@


def log_clock(steps: int) -> None:
def rotate(angle: float, len: float) -> Tuple[float, float, float]:
def rotate(angle: float, len: float) -> tuple[float, float, float]:
return (
len * math.sin(angle),
len * math.cos(angle),
Expand Down
10 changes: 5 additions & 5 deletions examples/python/colmap/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env python3
"""Example of using Rerun to log and visualize the output of COLMAP's sparse reconstruction."""
from __future__ import annotations

import io
import os
import re
import zipfile
from argparse import ArgumentParser
from pathlib import Path
from typing import Any, Final, Optional, Tuple
from typing import Any, Final

import cv2
import numpy as np
Expand All @@ -22,7 +24,7 @@
FILTER_MIN_VISIBLE: Final = 500


def scale_camera(camera: Camera, resize: Tuple[int, int]) -> Tuple[Camera, npt.NDArray[np.float_]]:
def scale_camera(camera: Camera, resize: tuple[int, int]) -> tuple[Camera, npt.NDArray[np.float_]]:
"""Scale the camera intrinsics to match the resized image."""
assert camera.model == "PINHOLE"
new_width = resize[0]
Expand Down Expand Up @@ -88,9 +90,7 @@ def download_with_progress(url: str) -> io.BytesIO:
return zip_file


def read_and_log_sparse_reconstruction(
dataset_path: Path, filter_output: bool, resize: Optional[Tuple[int, int]]
) -> None:
def read_and_log_sparse_reconstruction(dataset_path: Path, filter_output: bool, resize: tuple[int, int] | None) -> None:
print("Reading sparse COLMAP reconstruction")
cameras, images, points3D = read_model(dataset_path / "sparse", ext=".bin")
print("Building visualization by logging to Rerun")
Expand Down
15 changes: 7 additions & 8 deletions examples/python/colmap/read_write_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# This file is adapted from
# https://github.com/colmap/colmap/blob/bf3e19140f491c3042bfd85b7192ef7d249808ec/scripts/python/read_write_model.py

# Copyright (c) 2023, ETH Zurich and UNC Chapel Hill.
# All rights reserved.
#
Expand Down Expand Up @@ -31,8 +30,8 @@
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)

# type: ignore
from __future__ import annotations

import argparse
import collections
Expand Down Expand Up @@ -110,7 +109,7 @@ def read_cameras_text(path: Path):
void Reconstruction::ReadCamerasText(const std::string& path)
"""
cameras = {}
with open(path, "r") as fid:
with open(path) as fid:
while True:
line = fid.readline()
if not line:
Expand Down Expand Up @@ -161,7 +160,7 @@ def write_cameras_text(cameras, path):
HEADER = (
"# Camera list with one line of data per camera:\n"
+ "# CAMERA_ID, MODEL, WIDTH, HEIGHT, PARAMS[]\n"
+ "# Number of cameras: {}\n".format(len(cameras))
+ f"# Number of cameras: {len(cameras)}\n"
)
with open(path, "w") as fid:
fid.write(HEADER)
Expand Down Expand Up @@ -195,7 +194,7 @@ def read_images_text(path: Path):
void Reconstruction::WriteImagesText(const std::string& path)
"""
images = {}
with open(path, "r") as fid:
with open(path) as fid:
while True:
line = fid.readline()
if not line:
Expand Down Expand Up @@ -273,7 +272,7 @@ def write_images_text(images, path):
"# Image list with two lines of data per image:\n"
+ "# IMAGE_ID, QW, QX, QY, QZ, TX, TY, TZ, CAMERA_ID, NAME\n"
+ "# POINTS2D[] as (X, Y, POINT3D_ID)\n"
+ "# Number of images: {}, mean observations per image: {}\n".format(len(images), mean_observations)
+ f"# Number of images: {len(images)}, mean observations per image: {mean_observations}\n"
)

with open(path, "w") as fid:
Expand Down Expand Up @@ -317,7 +316,7 @@ def read_points3D_text(path):
void Reconstruction::WritePoints3DText(const std::string& path)
"""
points3D = {}
with open(path, "r") as fid:
with open(path) as fid:
while True:
line = fid.readline()
if not line:
Expand Down Expand Up @@ -375,7 +374,7 @@ def write_points3D_text(points3D, path):
HEADER = (
"# 3D point list with one line of data per point:\n"
+ "# POINT3D_ID, X, Y, Z, R, G, B, ERROR, TRACK[] as (IMAGE_ID, POINT2D_IDX)\n"
+ "# Number of points: {}, mean track length: {}\n".format(len(points3D), mean_track_length)
+ f"# Number of points: {len(points3D)}, mean track length: {mean_track_length}\n"
)

with open(path, "w") as fid:
Expand Down
5 changes: 3 additions & 2 deletions examples/python/deep_sdf/download_dataset.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
from __future__ import annotations

import io
import os
import zipfile
from pathlib import Path
from typing import Final, Optional
from typing import Final

import requests

Expand Down Expand Up @@ -49,7 +50,7 @@ def download_mesh(name: str) -> Path:
raise RuntimeError(f"Unknown mesh named: {name}")


def find_mesh_path_if_downloaded(name: str) -> Optional[Path]:
def find_mesh_path_if_downloaded(name: str) -> Path | None:
for mesh_format in ("obj", "glb"):
for path in DOWNLOADED_DIR.glob(f"{name}/**/*.{mesh_format}"):
return path
Expand Down
Loading