From d3934737a121296af8d236c2fa59e2c931a73444 Mon Sep 17 00:00:00 2001 From: Brent Yi Date: Sat, 14 Oct 2023 13:00:17 +0800 Subject: [PATCH] Upgrade pyright (#2526) * Upgrade pyright * Resolve pyright errors * Suppress trimesh error * Run ruff --- nerfstudio/exporter/marching_cubes.py | 2 +- nerfstudio/process_data/hloc_utils.py | 29 ++++++++++++----------- nerfstudio/scripts/render.py | 14 +++++++---- nerfstudio/utils/plotly_utils.py | 1 + nerfstudio/viewer/server/control_panel.py | 2 +- nerfstudio/viewer/server/viewer_state.py | 8 +++---- nerfstudio/viewer/viser/message_api.py | 4 ++-- nerfstudio/viewer_beta/control_panel.py | 9 +++---- nerfstudio/viewer_beta/viewer.py | 2 +- pyproject.toml | 4 ++-- 10 files changed, 42 insertions(+), 33 deletions(-) diff --git a/nerfstudio/exporter/marching_cubes.py b/nerfstudio/exporter/marching_cubes.py index a83307501..ee13422b1 100644 --- a/nerfstudio/exporter/marching_cubes.py +++ b/nerfstudio/exporter/marching_cubes.py @@ -249,7 +249,7 @@ def generate_mesh_with_multires_marching_cubes( ) verts = verts + np.array([x_min, y_min, z_min]) - meshcrop = trimesh.Trimesh(verts, faces, normals) + meshcrop = trimesh.Trimesh(verts, faces, normals) # type: ignore meshes.append(meshcrop) combined_mesh: trimesh.Trimesh = trimesh.util.concatenate(meshes) # type: ignore diff --git a/nerfstudio/process_data/hloc_utils.py b/nerfstudio/process_data/hloc_utils.py index 85049b5bf..a833f51ed 100644 --- a/nerfstudio/process_data/hloc_utils.py +++ b/nerfstudio/process_data/hloc_utils.py @@ -29,7 +29,7 @@ from nerfstudio.utils.rich_utils import CONSOLE try: # TODO(1480) un-hide pycolmap import import pycolmap - from hloc import ( + from hloc import ( # type: ignore extract_features, match_features, pairs_from_exhaustive, @@ -38,6 +38,7 @@ try: ) except ImportError: _HAS_HLOC = False + else: _HAS_HLOC = True @@ -95,24 +96,24 @@ def run_hloc( features = outputs / "features.h5" matches = outputs / "matches.h5" - retrieval_conf = extract_features.confs["netvlad"] - feature_conf = extract_features.confs[feature_type] - matcher_conf = match_features.confs[matcher_type] + retrieval_conf = extract_features.confs["netvlad"] # type: ignore + feature_conf = extract_features.confs[feature_type] # type: ignore + matcher_conf = match_features.confs[matcher_type] # type: ignore references = [p.relative_to(image_dir).as_posix() for p in image_dir.iterdir()] - extract_features.main(feature_conf, image_dir, image_list=references, feature_path=features) + extract_features.main(feature_conf, image_dir, image_list=references, feature_path=features) # type: ignore if matching_method == "exhaustive": - pairs_from_exhaustive.main(sfm_pairs, image_list=references) + pairs_from_exhaustive.main(sfm_pairs, image_list=references) # type: ignore else: - retrieval_path = extract_features.main(retrieval_conf, image_dir, outputs) + retrieval_path = extract_features.main(retrieval_conf, image_dir, outputs) # type: ignore if num_matched >= len(references): num_matched = len(references) - pairs_from_retrieval.main(retrieval_path, sfm_pairs, num_matched=num_matched) - match_features.main(matcher_conf, sfm_pairs, features=features, matches=matches) + pairs_from_retrieval.main(retrieval_path, sfm_pairs, num_matched=num_matched) # type: ignore + match_features.main(matcher_conf, sfm_pairs, features=features, matches=matches) # type: ignore - image_options = pycolmap.ImageReaderOptions(camera_model=camera_model.value) + image_options = pycolmap.ImageReaderOptions(camera_model=camera_model.value) # type: ignore if refine_pixsfm: - sfm = PixSfM( + sfm = PixSfM( # type: ignore conf={ "dense_features": {"use_cache": True}, "KA": {"dense_features": {"use_cache": True}, "max_kps_per_problem": 1000}, @@ -126,20 +127,20 @@ def run_hloc( features, matches, image_list=references, - camera_mode=pycolmap.CameraMode.SINGLE, + camera_mode=pycolmap.CameraMode.SINGLE, # type: ignore image_options=image_options, verbose=verbose, ) print("Refined", refined.summary()) else: - reconstruction.main( + reconstruction.main( # type: ignore sfm_dir, image_dir, sfm_pairs, features, matches, - camera_mode=pycolmap.CameraMode.SINGLE, + camera_mode=pycolmap.CameraMode.SINGLE, # type: ignore image_options=image_options, verbose=verbose, ) diff --git a/nerfstudio/scripts/render.py b/nerfstudio/scripts/render.py index 307bb5adb..5e7a52e01 100644 --- a/nerfstudio/scripts/render.py +++ b/nerfstudio/scripts/render.py @@ -18,12 +18,12 @@ render.py """ from __future__ import annotations +import gzip import json import os -import struct import shutil +import struct import sys -import gzip from contextlib import ExitStack, contextmanager from dataclasses import dataclass, field from pathlib import Path @@ -54,11 +54,14 @@ from nerfstudio.cameras.camera_paths import ( get_spiral_path, ) from nerfstudio.cameras.cameras import Cameras, CameraType, RayBundle +from nerfstudio.data.datamanagers.base_datamanager import ( + VanillaDataManager, + VanillaDataManagerConfig, +) from nerfstudio.data.datasets.base_dataset import Dataset -from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManager, VanillaDataManagerConfig +from nerfstudio.data.scene_box import OrientedBox from nerfstudio.data.utils.dataloaders import FixedIndicesEvalDataloader from nerfstudio.engine.trainer import TrainerConfig -from nerfstudio.data.scene_box import OrientedBox from nerfstudio.model_components import renderers from nerfstudio.pipelines.base_pipeline import Pipeline from nerfstudio.utils import colormaps, install_checks @@ -318,6 +321,9 @@ def get_crop_from_json(camera_json: Dict[str, Any]) -> Optional[CropData]: center = camera_json["crop"]["crop_center"] scale = camera_json["crop"]["crop_scale"] rot = (0.0, 0.0, 0.0) if "crop_rot" not in camera_json["crop"] else tuple(camera_json["crop"]["crop_rot"]) + assert len(center) == 3 + assert len(scale) == 3 + assert len(rot) == 3 return CropData( background_color=torch.Tensor([bg_color["r"] / 255.0, bg_color["g"] / 255.0, bg_color["b"] / 255.0]), obb=OrientedBox.from_params(center, rot, scale), diff --git a/nerfstudio/utils/plotly_utils.py b/nerfstudio/utils/plotly_utils.py index 00cab4760..1ab7da1fb 100644 --- a/nerfstudio/utils/plotly_utils.py +++ b/nerfstudio/utils/plotly_utils.py @@ -239,6 +239,7 @@ def get_cube( if isinstance(side_length, float): pts *= side_length / 2.0 else: + assert isinstance(side_length, torch.Tensor) pts[0] *= side_length[0].item() / 2.0 pts[1] *= side_length[1].item() / 2.0 pts[2] *= side_length[2].item() / 2.0 diff --git a/nerfstudio/viewer/server/control_panel.py b/nerfstudio/viewer/server/control_panel.py index 7039b61c3..0f789a8ae 100644 --- a/nerfstudio/viewer/server/control_panel.py +++ b/nerfstudio/viewer/server/control_panel.py @@ -196,7 +196,7 @@ class ControlPanel: self._split_output_render.set_options(new_options) self._split_output_render.value = new_options[-1] - def add_element(self, e: ViewerElement, additional_tags: Tuple[str] = tuple()) -> None: + def add_element(self, e: ViewerElement, additional_tags: Tuple[str, ...] = tuple()) -> None: """Adds an element to the control panel Args: diff --git a/nerfstudio/viewer/server/viewer_state.py b/nerfstudio/viewer/server/viewer_state.py index ad80822ce..3bbce5e2d 100644 --- a/nerfstudio/viewer/server/viewer_state.py +++ b/nerfstudio/viewer/server/viewer_state.py @@ -198,8 +198,8 @@ class ViewerState: self.viser_server.send_crop_params( crop_enabled=self.control_panel.crop_viewport, crop_bg_color=self.control_panel.background_color, - crop_scale=tuple(crop_scale.tolist()), - crop_center=tuple(crop_center.tolist()), + crop_scale=tuple(crop_scale.tolist()), # type: ignore + crop_center=tuple(crop_center.tolist()), # type: ignore ) if self.camera_message is not None: self.render_statemachine.action(RenderAction("rerender", self.camera_message)) @@ -258,8 +258,8 @@ class ViewerState: scale = np.array(message.crop_scale) crop_min = center - scale / 2.0 crop_max = center + scale / 2.0 - self.control_panel.crop_min = tuple(crop_min.tolist()) - self.control_panel.crop_max = tuple(crop_max.tolist()) + self.control_panel.crop_min = tuple(crop_min.tolist()) # type: ignore + self.control_panel.crop_max = tuple(crop_max.tolist()) # type: ignore def _handle_click_message(self, message: NerfstudioMessage) -> None: """Handle click message from viewer.""" diff --git a/nerfstudio/viewer/viser/message_api.py b/nerfstudio/viewer/viser/message_api.py index 3c8ce752c..bb6449cf8 100644 --- a/nerfstudio/viewer/viser/message_api.py +++ b/nerfstudio/viewer/viser/message_api.py @@ -501,8 +501,8 @@ class MessageApi(abc.ABC): """ self._queue( messages.SceneBoxMessage( - min=tuple(scene_box.aabb[0].tolist()), - max=tuple(scene_box.aabb[1].tolist()), + min=tuple(scene_box.aabb[0].tolist()), # type: ignore + max=tuple(scene_box.aabb[1].tolist()), # type: ignore ) ) diff --git a/nerfstudio/viewer_beta/control_panel.py b/nerfstudio/viewer_beta/control_panel.py index 716181694..a6fdef970 100644 --- a/nerfstudio/viewer_beta/control_panel.py +++ b/nerfstudio/viewer_beta/control_panel.py @@ -19,6 +19,8 @@ from typing import Callable, DefaultDict, List, Tuple, get_args import numpy as np import torch import viser.transforms as vtf +from viser import ViserServer + from nerfstudio.data.scene_box import OrientedBox from nerfstudio.utils.colormaps import ColormapOptions, Colormaps from nerfstudio.viewer_beta.viewer_elements import ( # ViewerButtonGroup, @@ -32,7 +34,6 @@ from nerfstudio.viewer_beta.viewer_elements import ( # ViewerButtonGroup, ViewerSlider, ViewerVec3, ) -from viser import ViserServer class ControlPanel: @@ -147,7 +148,7 @@ class ControlPanel: self._crop_handle = self.viser_server.add_transform_controls("Crop", depth_test=False, line_width=4.0) def update_center(han): - self._crop_handle.position = tuple(p * self.viser_scale_ratio for p in han.value) + self._crop_handle.position = tuple(p * self.viser_scale_ratio for p in han.value) # type: ignore self._crop_center = ViewerVec3( "Crop Center", @@ -175,7 +176,7 @@ class ControlPanel: @self._crop_handle.on_update def _update_crop_handle(han): pos = self._crop_handle.position - self._crop_center.value = tuple(p / self.viser_scale_ratio for p in pos) + self._crop_center.value = tuple(p / self.viser_scale_ratio for p in pos) # type: ignore rpy = vtf.SO3(self._crop_handle.wxyz).as_rpy_radians() self._crop_rot.value = (float(rpy.roll), float(rpy.pitch), float(rpy.yaw)) @@ -280,7 +281,7 @@ class ControlPanel: self._split_output_render.set_options(new_options) self._split_output_render.value = new_options[-1] - def add_element(self, e: ViewerElement, additional_tags: Tuple[str] = tuple()) -> None: + def add_element(self, e: ViewerElement, additional_tags: Tuple[str, ...] = tuple()) -> None: """Adds an element to the control panel Args: diff --git a/nerfstudio/viewer_beta/viewer.py b/nerfstudio/viewer_beta/viewer.py index d4fbf973b..d122cf094 100644 --- a/nerfstudio/viewer_beta/viewer.py +++ b/nerfstudio/viewer_beta/viewer.py @@ -326,7 +326,7 @@ class Viewer: ) @camera_handle.on_click - def _(event: viser.ClickEvent[viser.CameraFrustumHandle]) -> None: + def _(event: viser.SceneNodePointerEvent[viser.CameraFrustumHandle]) -> None: assert self.client is not None with self.client.atomic(): self.client.camera.position = event.target.position diff --git a/pyproject.toml b/pyproject.toml index fc2b0be03..835a569da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,7 @@ dependencies = [ "torchvision>=0.14.1", "torchmetrics[image]>=1.0.1", "typing_extensions>=4.4.0", - "viser==0.1.6", + "viser==0.1.7", "nuscenes-devkit>=1.1.1", "wandb>=0.13.3", "xatlas", @@ -90,7 +90,7 @@ dev = [ "diffusers==0.16.1", "opencv-stubs==0.0.7", "transformers==4.29.2", - "pyright==1.1.308", + "pyright==1.1.331", ] # Documentation related packages