From a7bcac80c0d5b59d2aed00b2307c5aaad797f5c0 Mon Sep 17 00:00:00 2001 From: Shaohui Liu <b1ueber2y@gmail.com> Date: Mon, 18 Nov 2024 19:57:41 +0100 Subject: [PATCH] Fix linting for UP030 and B006 (#92) * fix B006. * fix UP030. * fix UP030. --- limap/features/models/base_model.py | 4 +++- limap/line2d/LineTR/line_process.py | 4 +++- limap/line2d/SOLD2/model/line_matcher.py | 12 +++++++++--- limap/runners/functions.py | 18 +++++++++--------- limap/runners/functions_structures.py | 4 +++- limap/runners/line_fitnmerge.py | 6 +++--- limap/runners/line_triangulation.py | 2 +- limap/visualize/vis_lines.py | 22 +++++++++++++++------- limap/visualize/vis_utils.py | 8 ++++++-- ruff.toml | 2 +- runners/eth3d/fitnmerge.py | 2 +- runners/eth3d/triangulation.py | 2 +- runners/rome16k/triangulation.py | 2 +- scripts/tnt_align.py | 6 +++--- scripts/tnt_colmap_runner.py | 2 +- 15 files changed, 60 insertions(+), 36 deletions(-) diff --git a/limap/features/models/base_model.py b/limap/features/models/base_model.py index c342d6c0..77672820 100644 --- a/limap/features/models/base_model.py +++ b/limap/features/models/base_model.py @@ -42,8 +42,10 @@ class BaseModel(nn.Module, metaclass=ABCMeta): required_data_keys = [] strict_conf = True - def __init__(self, conf={}): + def __init__(self, conf=None): """Perform some logic and call the _init method of the child model.""" + if conf is None: + conf = {} super().__init__() default_conf = OmegaConf.merge( OmegaConf.create(self.base_default_conf), diff --git a/limap/line2d/LineTR/line_process.py b/limap/line2d/LineTR/line_process.py index 3f0bc8af..79b4312c 100755 --- a/limap/line2d/LineTR/line_process.py +++ b/limap/line2d/LineTR/line_process.py @@ -287,7 +287,9 @@ def change_cv2_T_np(klines_cv): return {"klines": klines, "length_klines": length, "angles": angles} -def preprocess(klines_cv, image_shape, pred_superpoint, mask=None, conf={}): +def preprocess(klines_cv, image_shape, pred_superpoint, mask=None, conf=None): + if conf is None: + conf = {} default_conf = { "min_length": 16, "max_sublines": 256, diff --git a/limap/line2d/SOLD2/model/line_matcher.py b/limap/line2d/SOLD2/model/line_matcher.py index 50073cac..4d9cb457 100644 --- a/limap/line2d/SOLD2/model/line_matcher.py +++ b/limap/line2d/SOLD2/model/line_matcher.py @@ -31,9 +31,11 @@ def __init__( line_detector_cfg, line_matcher_cfg, multiscale=False, - scales=[1.0, 2.0], + scales=None, ): # Get loss weights if dynamic weighting + if scales is None: + scales = [1.0, 2.0] _, loss_weights = get_loss_and_weights(model_cfg, device) self.device = device @@ -162,10 +164,12 @@ def multiscale_line_detection( valid_mask=None, desc_only=False, profile=False, - scales=[1.0, 2.0], + scales=None, aggregation="mean", ): # Restrict input_image to 4D torch tensor + if scales is None: + scales = [1.0, 2.0] if (not len(input_image.shape) == 4) or ( not isinstance(input_image, torch.Tensor) ): @@ -291,8 +295,10 @@ def multiscale_line_detection( return outputs - def __call__(self, images, valid_masks=[None, None], profile=False): + def __call__(self, images, valid_masks=None, profile=False): # Line detection and descriptor inference on both images + if valid_masks is None: + valid_masks = [None, None] if self.multiscale: forward_outputs = [ self.multiscale_line_detection( diff --git a/limap/runners/functions.py b/limap/runners/functions.py index 9c41c294..a6a80dc6 100644 --- a/limap/runners/functions.py +++ b/limap/runners/functions.py @@ -18,11 +18,11 @@ def setup(cfg): folder_load = folder_save cfg["dir_save"] = folder_save cfg["dir_load"] = folder_load - print("[LOG] Output dir: {0}".format(cfg["dir_save"])) - print("[LOG] Loading dir: {0}".format(cfg["dir_load"])) + print("[LOG] Output dir: {}".format(cfg["dir_save"])) + print("[LOG] Loading dir: {}".format(cfg["dir_load"])) if "weight_path" in cfg and cfg["weight_path"] is not None: cfg["weight_path"] = os.path.expanduser(cfg["weight_path"]) - print("[LOG] weight dir: {0}".format(cfg["weight_path"])) + print("[LOG] weight dir: {}".format(cfg["weight_path"])) return cfg @@ -192,7 +192,7 @@ def compute_2d_segs(cfg, imagecols, compute_descinfo=True): weight_path = cfg.get("weight_path", None) if "extractor" in cfg["line2d"]: print( - "[LOG] Start 2D line detection and description (detector = {0}, extractor = {1}, n_images = {2})...".format( + "[LOG] Start 2D line detection and description (detector = {}, extractor = {}, n_images = {})...".format( cfg["line2d"]["detector"]["method"], cfg["line2d"]["extractor"]["method"], imagecols.NumImages(), @@ -200,7 +200,7 @@ def compute_2d_segs(cfg, imagecols, compute_descinfo=True): ) else: print( - "[LOG] Start 2D line detection and description (detector = {0}, n_images = {1})...".format( + "[LOG] Start 2D line detection and description (detector = {}, n_images = {})...".format( cfg["line2d"]["detector"]["method"], imagecols.NumImages() ) ) @@ -282,7 +282,7 @@ def compute_matches(cfg, descinfo_folder, image_ids, neighbors): """ weight_path = cfg.get("weight_path", None) print( - "[LOG] Start matching 2D lines... (extractor = {0}, matcher = {1}, n_images = {2}, n_neighbors = {3})".format( + "[LOG] Start matching 2D lines... (extractor = {}, matcher = {}, n_images = {}, n_neighbors = {})".format( cfg["line2d"]["extractor"]["method"], cfg["line2d"]["matcher"]["method"], len(image_ids), @@ -294,7 +294,7 @@ def compute_matches(cfg, descinfo_folder, image_ids, neighbors): basedir = os.path.join( "line_matchings", cfg["line2d"]["detector"]["method"], - "feats_{0}".format(cfg["line2d"]["extractor"]["method"]), + "feats_{}".format(cfg["line2d"]["extractor"]["method"]), ) extractor = limap.line2d.get_extractor( cfg["line2d"]["extractor"], weight_path=weight_path @@ -333,7 +333,7 @@ def compute_exhausive_matches(cfg, descinfo_folder, image_ids): matches_folder (str): path to store the computed matches """ print( - "[LOG] Start exhausive matching 2D lines... (extractor = {0}, matcher = {1}, n_images = {2})".format( + "[LOG] Start exhausive matching 2D lines... (extractor = {}, matcher = {}, n_images = {})".format( cfg["line2d"]["extractor"]["method"], cfg["line2d"]["matcher"]["method"], len(image_ids), @@ -344,7 +344,7 @@ def compute_exhausive_matches(cfg, descinfo_folder, image_ids): basedir = os.path.join( "line_matchings", cfg["line2d"]["detector"]["method"], - "feats_{0}".format(cfg["line2d"]["extractor"]["method"]), + "feats_{}".format(cfg["line2d"]["extractor"]["method"]), ) extractor = limap.line2d.get_extractor(cfg["line2d"]["extractor"]) se_match = cfg["skip_exists"] or cfg["line2d"]["matcher"]["skip_exists"] diff --git a/limap/runners/functions_structures.py b/limap/runners/functions_structures.py index 0abfc01c..01ecae9b 100644 --- a/limap/runners/functions_structures.py +++ b/limap/runners/functions_structures.py @@ -77,8 +77,10 @@ def compute_colmap_model_with_junctions( def compute_2d_bipartites_from_colmap( - reconstruction, imagecols, all_2d_lines, cfg=dict() + reconstruction, imagecols, all_2d_lines, cfg=None ): + if cfg is None: + cfg = dict() all_bpt2ds = {} cfg_bpt2d = _structures.PL_Bipartite2dConfig(cfg) colmap_cameras, colmap_images, colmap_points = ( diff --git a/limap/runners/line_fitnmerge.py b/limap/runners/line_fitnmerge.py index 75a45b79..d5e475b4 100644 --- a/limap/runners/line_fitnmerge.py +++ b/limap/runners/line_fitnmerge.py @@ -163,7 +163,7 @@ def line_fitnmerge(cfg, imagecols, depths, neighbors=None, ranges=None): ########################################################## # [C] fit 3d segments ########################################################## - fname_fit_segs = "{0}_fit_segs.npy".format( + fname_fit_segs = "{}_fit_segs.npy".format( cfg["line2d"]["detector"]["method"] ) if (not cfg["load_fit"]) and ( @@ -266,7 +266,7 @@ def line_fitnmerge(cfg, imagecols, depths, neighbors=None, ranges=None): limapio.save_obj( os.path.join( cfg["dir_save"], - "fitnmerge_lines_nv{0}.obj".format(cfg["n_visible_views"]), + "fitnmerge_lines_nv{}.obj".format(cfg["n_visible_views"]), ), VisTrack.get_lines_np(n_visible_views=cfg["n_visible_views"]), ) @@ -324,7 +324,7 @@ def line_fitting_with_3Dpoints( ########################################################## # [B] fit 3d segments ########################################################## - fname_fit_segs = "{0}_fit_segs.npy".format( + fname_fit_segs = "{}_fit_segs.npy".format( cfg["line2d"]["detector"]["method"] ) if (not cfg["load_fit"]) and ( diff --git a/limap/runners/line_triangulation.py b/limap/runners/line_triangulation.py index 14cadf9f..72ac719f 100644 --- a/limap/runners/line_triangulation.py +++ b/limap/runners/line_triangulation.py @@ -231,7 +231,7 @@ def line_triangulation(cfg, imagecols, neighbors=None, ranges=None): limapio.save_obj( os.path.join( cfg["dir_save"], - "triangulated_lines_nv{0}.obj".format(cfg["n_visible_views"]), + "triangulated_lines_nv{}.obj".format(cfg["n_visible_views"]), ), VisTrack.get_lines_np(n_visible_views=cfg["n_visible_views"]), ) diff --git a/limap/visualize/vis_lines.py b/limap/visualize/vis_lines.py index 2919cb7c..d5b25cda 100644 --- a/limap/visualize/vis_lines.py +++ b/limap/visualize/vis_lines.py @@ -26,12 +26,14 @@ def pyvista_vis_3d_lines( def open3d_add_points( w, points, - color=[0.0, 0.0, 0.0], + color=None, psize=1.0, name="pcd", ranges=None, scale=1.0, ): + if color is None: + color = [0.0, 0.0, 0.0] if np.array(points).shape[0] == 0: return w import open3d as o3d @@ -54,11 +56,11 @@ def open3d_add_points( return w -def open3d_get_line_set( - lines, color=[0.0, 0.0, 0.0], width=2, ranges=None, scale=1.0 -): +def open3d_get_line_set(lines, color=None, width=2, ranges=None, scale=1.0): import open3d as o3d + if color is None: + color = [0.0, 0.0, 0.0] o3d_points, o3d_lines, o3d_colors = [], [], [] counter = 0 for line in lines: @@ -79,7 +81,7 @@ def open3d_get_line_set( def open3d_add_line_set( w, lines, - color=[0.0, 0.0, 0.0], + color=None, width=2, name="lineset", ranges=None, @@ -87,6 +89,8 @@ def open3d_add_line_set( ): import open3d as o3d + if color is None: + color = [0.0, 0.0, 0.0] o3d_points, o3d_lines, o3d_colors = [], [], [] counter = 0 for line in lines: @@ -110,7 +114,7 @@ def open3d_add_line_set( def open3d_get_cameras( imagecols, - color=[1.0, 0.0, 0.0], + color=None, ranges=None, scale_cam_geometry=1.0, scale=1.0, @@ -119,6 +123,8 @@ def open3d_get_cameras( import open3d as o3d + if color is None: + color = [1.0, 0.0, 0.0] cameras = o3d.geometry.LineSet() camera_lines = {} @@ -150,7 +156,7 @@ def open3d_get_cameras( def open3d_add_cameras( w, imagecols, - color=[1.0, 0.0, 0.0], + color=None, ranges=None, scale_cam_geometry=1.0, scale=1.0, @@ -159,6 +165,8 @@ def open3d_add_cameras( import open3d as o3d + if color is None: + color = [1.0, 0.0, 0.0] camera_lines = {} for cam_id in imagecols.get_cam_ids(): cam = imagecols.cam(cam_id) diff --git a/limap/visualize/vis_utils.py b/limap/visualize/vis_utils.py index 6fcedf87..cfe01f35 100644 --- a/limap/visualize/vis_utils.py +++ b/limap/visualize/vis_utils.py @@ -317,7 +317,9 @@ def test_line_inside_ranges(line, ranges): return test_point_inside_ranges(line.end, ranges) -def compute_robust_range(arr, range_robust=[0.05, 0.95], k_stretch=2.0): +def compute_robust_range(arr, range_robust=None, k_stretch=2.0): + if range_robust is None: + range_robust = [0.05, 0.95] N = arr.shape[0] start_idx = int(round((N - 1) * range_robust[0])) end_idx = int(round((N - 1) * range_robust[1])) @@ -329,7 +331,9 @@ def compute_robust_range(arr, range_robust=[0.05, 0.95], k_stretch=2.0): return start_stretched, end_stretched -def compute_robust_range_lines(lines, range_robust=[0.05, 0.95], k_stretch=2.0): +def compute_robust_range_lines(lines, range_robust=None, k_stretch=2.0): + if range_robust is None: + range_robust = [0.05, 0.95] lines_array = np.array([line.as_array() for line in lines]) x_array = lines_array.reshape(-1, 3)[:, 0] y_array = lines_array.reshape(-1, 3)[:, 1] diff --git a/ruff.toml b/ruff.toml index a5162637..97d44617 100644 --- a/ruff.toml +++ b/ruff.toml @@ -15,7 +15,7 @@ select = [ # isort "I", ] -ignore = ["E501", "F401", "F403", "UP030", "B006", "B008"] +ignore = ["E501", "F401", "F403", "B008"] [lint.per-file-ignores] "limap/line2d/L2D2/RAL_net_cov.py" = ["SIM"] diff --git a/runners/eth3d/fitnmerge.py b/runners/eth3d/fitnmerge.py index a2822a48..6c0d013a 100644 --- a/runners/eth3d/fitnmerge.py +++ b/runners/eth3d/fitnmerge.py @@ -63,7 +63,7 @@ def parse_config(): "precomputed", "eth3d", cfg["reso_type"], - "{0}_cam{1}".format(cfg["scene_id"], cfg["cam_id"]), + "{}_cam{}".format(cfg["scene_id"], cfg["cam_id"]), ) return cfg diff --git a/runners/eth3d/triangulation.py b/runners/eth3d/triangulation.py index 3839a2b3..561e29c7 100644 --- a/runners/eth3d/triangulation.py +++ b/runners/eth3d/triangulation.py @@ -64,7 +64,7 @@ def parse_config(): "precomputed", "eth3d", cfg["reso_type"], - "{0}_cam{1}".format(cfg["scene_id"], cfg["cam_id"]), + "{}_cam{}".format(cfg["scene_id"], cfg["cam_id"]), ) return cfg diff --git a/runners/rome16k/triangulation.py b/runners/rome16k/triangulation.py index 68b8c500..bd4b3187 100644 --- a/runners/rome16k/triangulation.py +++ b/runners/rome16k/triangulation.py @@ -32,7 +32,7 @@ def run_rome16k_triangulation(cfg, bundler_path, list_path, model_path): if comp_id == cfg["comp_id"]: valid_image_ids.append(img_id) print( - "[LOG] Get image subset from component {0}: n_images = {1}".format( + "[LOG] Get image subset from component {}: n_images = {}".format( cfg["comp_id"], len(valid_image_ids) ) ) diff --git a/scripts/tnt_align.py b/scripts/tnt_align.py index caca3eae..a15726d3 100644 --- a/scripts/tnt_align.py +++ b/scripts/tnt_align.py @@ -80,9 +80,9 @@ def main(): # colmap align cmd_list = [] basepath = os.path.join(colmap_output_path, scene_id, "dense") - cmd = "mkdir -p {0}".format(os.path.join(basepath, "aligned")) + cmd = "mkdir -p {}".format(os.path.join(basepath, "aligned")) cmd_list.append(cmd) - cmd = "colmap model_aligner --input_path {0} --output_path {1} --ref_images_path {2} --robust_alignment 1 --robust_alignment_max_error {3} --transform_path {4} --ref_is_gps false".format( + cmd = "colmap model_aligner --input_path {} --output_path {} --ref_images_path {} --robust_alignment 1 --robust_alignment_max_error {} --transform_path {} --ref_is_gps false".format( os.path.join(basepath, "sparse"), os.path.join(basepath, "aligned"), os.path.join(input_meta_path, scene_id, "geo_positions.txt"), @@ -90,7 +90,7 @@ def main(): os.path.join(basepath, "transform.txt"), ) cmd_list.append(cmd) - cmd = "colmap model_converter --input_path {0} --output_path {1} --output_type PLY".format( + cmd = "colmap model_converter --input_path {} --output_path {} --output_type PLY".format( os.path.join(basepath, "aligned"), os.path.join(basepath, "aligned/points.ply"), ) diff --git a/scripts/tnt_colmap_runner.py b/scripts/tnt_colmap_runner.py index 8e8efaa9..a0c3faa3 100644 --- a/scripts/tnt_colmap_runner.py +++ b/scripts/tnt_colmap_runner.py @@ -30,7 +30,7 @@ cmd = f"colmap mapper --database_path {database_path} --image_path {input_folder} --output_path {sparse_folder}" print(cmd) os.system(cmd) - cmd = "colmap image_undistorter --image_path {0} --input_path {1} --output_path {2} --output_type COLMAP".format( + cmd = "colmap image_undistorter --image_path {} --input_path {} --output_path {} --output_type COLMAP".format( input_folder, os.path.join(sparse_folder, "0"), dense_folder ) print(cmd)