Skip to content

Commit

Permalink
Switch to ruff with linting (#88)
Browse files Browse the repository at this point in the history
* switch to ruff.

* formatting.

* formatting.

* update.

* formatting.

* formatting.

* formatting.

* formatting.

* fix formatting.
  • Loading branch information
B1ueber2y authored Oct 19, 2024
1 parent 734787e commit 542a9b2
Show file tree
Hide file tree
Showing 152 changed files with 958 additions and 1,163 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/format-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
fi
set +x -euo pipefail
sudo apt-get update && sudo apt-get install -y clang-format-14
python -m pip install black==24.10.0
python -m pip install ruff==0.6.7
./scripts/format/clang_format.sh
./scripts/format/black.sh
./scripts/format/python.sh
git diff --name-only
git diff --exit-code || (echo "Code formatting failed" && exit 1)
37 changes: 18 additions & 19 deletions limap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
sys.path.append("build/limap/_limap")
from _limap import *

from . import base
from . import point2d
from . import line2d
from . import vplib
from . import pointsfm
from . import undistortion

from . import triangulation
from . import merging
from . import evaluation
from . import fitting
from . import util
from . import visualize
from . import structures

from . import features
from . import optimize

from . import runners
from . import (
base,
evaluation,
features,
fitting,
line2d,
merging,
optimize,
point2d,
pointsfm,
runners,
structures,
triangulation,
undistortion,
util,
visualize,
vplib,
)
5 changes: 3 additions & 2 deletions limap/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _limap._base import *
from .functions import *

from .align import *
from .unit_test import *
from .depth_reader_base import *
from .functions import *
from .p3d_reader_base import *
from .unit_test import *
24 changes: 10 additions & 14 deletions limap/base/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def umeyama_alignment(x, y, with_scale=True):
:return: r, t, c - rotation matrix, translation vector and scale factor
"""
if x.shape != y.shape:
assert False, "x.shape not equal to y.shape"
raise AssertionError("x.shape not equal to y.shape")

# m = dimension, n = nr. of data points
m, n = x.shape
Expand Down Expand Up @@ -54,10 +54,7 @@ def umeyama_alignment(x, y, with_scale=True):
def align_imagecols_umeyama(imagecols_src, imagecols_dst):
# assertion check
assert imagecols_src.NumImages() == imagecols_dst.NumImages()
assert (
np.all(imagecols_src.get_img_ids() == imagecols_dst.get_img_ids())
== True
)
assert np.all(imagecols_src.get_img_ids() == imagecols_dst.get_img_ids())

# fit transformation
xyz_src = np.array(imagecols_src.get_locations()).transpose()
Expand All @@ -74,18 +71,17 @@ def align_imagecols_colmap(
max_error=0.01,
tmp_folder="tmp/model_convertion",
):
import os, shutil
import os
import subprocess

import numpy as np
from limap.pointsfm import convert_imagecols_to_colmap

import limap.util.io as limapio
import subprocess
from limap.pointsfm import convert_imagecols_to_colmap

# assertion check
assert imagecols_src.NumImages() == imagecols_dst.NumImages()
assert (
np.all(imagecols_src.get_img_ids() == imagecols_dst.get_img_ids())
== True
)
assert np.all(imagecols_src.get_img_ids() == imagecols_dst.get_img_ids())

limapio.check_makedirs(tmp_folder)
src_folder = os.path.join(tmp_folder, "source")
Expand All @@ -102,7 +98,7 @@ def align_imagecols_colmap(
for img_id in imagecols_src.get_img_ids():
imname = imagecols_src.image_name(img_id)
pos = imagecols_dst.camview(img_id).pose.center()
f.write("{0} {1} {2} {3}\n".format(imname, pos[0], pos[1], pos[2]))
f.write(f"{imname} {pos[0]} {pos[1]} {pos[2]}\n")

# call comlap model aligner
transform_path = os.path.join(tmp_folder, "transform.txt")
Expand Down Expand Up @@ -130,7 +126,7 @@ def align_imagecols_colmap(

# read in transformation
def read_trans(fname):
with open(fname, "r") as f:
with open(fname) as f:
lines = f.readlines()
mat = []
for idx in range(4):
Expand Down
1 change: 1 addition & 0 deletions limap/estimators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from _limap._estimators import *

from .absolute_pose import *
7 changes: 4 additions & 3 deletions limap/estimators/absolute_pose/_pl_estimate_absolute_pose.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from _limap import _ceresbase
import limap.optimize as _optimize
import limap.estimators as _estimators

import limap.base as _base
import numpy as np
import limap.estimators as _estimators
import limap.optimize as _optimize


def _pl_estimate_absolute_pose(
Expand Down
3 changes: 2 additions & 1 deletion limap/features/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _limap._features import *
from .extractors import *

from .extract_line_patches import *
from .extractors import *
14 changes: 5 additions & 9 deletions limap/features/extract_line_patches.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from _limap import _features
import numpy as np
from _limap import _features


def write_patch(fname, patch, dtype="float16"):
Expand All @@ -17,7 +17,7 @@ def write_patch(fname, patch, dtype="float16"):

def load_patch(fname, dtype="float16"):
# return a PatchInfo_f object
patch_info_name = "PatchInfo_f{0}".format(dtype[-2:])
patch_info_name = f"PatchInfo_f{dtype[-2:]}"
with open(fname, "rb") as f:
data = np.load(f, allow_pickle=True)
patch = getattr(_features, patch_info_name)(
Expand All @@ -28,7 +28,7 @@ def load_patch(fname, dtype="float16"):

def get_extractor(cfg, channels):
lpe_options = _features.LinePatchExtractorOptions(cfg)
patch_extractor_name = "LinePatchExtractor_f64_c{0}".format(channels)
patch_extractor_name = f"LinePatchExtractor_f64_c{channels}"
extractor = getattr(_features, patch_extractor_name)(lpe_options)
return extractor

Expand All @@ -39,9 +39,7 @@ def extract_line_patch_oneimage(cfg, track, img_id, camview, feature):
_features.PatchInfo_fx
"""
lpe_options = _features.LinePatchExtractorOptions(cfg)
patch_extractor_name = "LinePatchExtractor_f64_c{0}".format(
feature.shape[2]
)
patch_extractor_name = f"LinePatchExtractor_f64_c{feature.shape[2]}"
extractor = getattr(_features, patch_extractor_name)(lpe_options)
patch = extractor.ExtractOneImage(track, img_id, camview, feature)
return patch
Expand All @@ -53,9 +51,7 @@ def extract_line_patches(cfg, track, p_camviews, p_features):
list of _features.PatchInfo_fx
"""
lpe_options = _features.LinePatchExtractorOptions(cfg)
patch_extractor_name = "LinePatchExtractor_f64_c{0}".format(
p_features[0].shape[2]
)
patch_extractor_name = f"LinePatchExtractor_f64_c{p_features[0].shape[2]}"
extractor = getattr(_features, patch_extractor_name)(lpe_options)
patches = extractor.Extract(track, p_camviews, p_features)
return patches
20 changes: 10 additions & 10 deletions limap/features/extractors.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# [NOTE] modified from the pixel-perfect-sfm project

import os, sys
import torch
import sys
import time

import numpy as np
import PIL

from .models.s2dnet import *
from .models.vggnet import VGGNet
import torch
import torchvision.transforms.functional as tvf
from _limap import _features
from torchvision import transforms
import time

from _limap import _features
from .models.s2dnet import S2DNet
from .models.vggnet import VGGNet

RGB_mean = [0.485, 0.456, 0.406]
RGB_std = [0.229, 0.224, 0.225]
Expand Down Expand Up @@ -44,7 +44,7 @@ class Extractor(torch.nn.Module):
"""

def __init__(self, device: str):
super(Extractor, self).__init__()
super().__init__()
self.device = device
self.num_levels = 0
self.model = None
Expand Down Expand Up @@ -87,7 +87,7 @@ def __init__(self, device: str, *args, **kwargs):
self.num_levels = 1
self.channels = [128] # ,128] #[128,128]
self.l2_normalize = True
if "output_channels" in kwargs.keys():
if "output_channels" in kwargs:
self.channels = [min(kwargs["output_channels"], self.channels[0])]

def extract_featuremaps(self, image_batch: torch.Tensor) -> list:
Expand Down Expand Up @@ -148,7 +148,7 @@ def __init__(self, device: str, *args, **kwargs):
self.num_levels = 1 # 2
self.channels = [64] # [128,128]
self.l2_normalize = True
if "output_channels" in kwargs.keys():
if "output_channels" in kwargs:
self.channels = [min(kwargs["output_channels"], self.channels[0])]

def extract_featuremaps(self, image_batch: torch.Tensor) -> list:
Expand Down
5 changes: 3 additions & 2 deletions limap/features/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"""

from abc import ABCMeta, abstractmethod
from copy import copy

from omegaconf import OmegaConf
from torch import nn
from copy import copy


class BaseModel(nn.Module, metaclass=ABCMeta):
Expand Down Expand Up @@ -77,7 +78,7 @@ def freeze_bn(module):
def forward(self, data):
"""Check the data and call the _forward method of the child model."""
for key in self.required_data_keys:
assert key in data, "Missing key {} in data".format(key)
assert key in data, f"Missing key {key} in data"
return self._forward(data)

@abstractmethod
Expand Down
22 changes: 7 additions & 15 deletions limap/features/models/s2dnet.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import logging
import os
from pathlib import Path
from typing import List
import numpy

import numpy as np
import torch
import torch.nn as nn
from torchvision import models
from pathlib import Path
import logging

from PIL import Image
from .base_model import BaseModel
import os, sys
from torchvision import transforms
import numpy as np
import torch.nn.functional as F

import argparse
import h5py

from time import time

type_dict = {
"uint8_t": torch.cuda.ByteTensor,
Expand Down Expand Up @@ -90,12 +82,12 @@ def __init__(self, hypercolumn_layers: List[str], output_dim: int = 128):
nn.BatchNorm2d(output_dim),
)
self.layers.append(layer)
self.add_module("adap_layer_{}".format(i), layer)
self.add_module(f"adap_layer_{i}", layer)

def forward(self, features: List[torch.tensor]):
"""Apply adaptation layers."""
for i, _ in enumerate(features):
features[i] = getattr(self, "adap_layer_{}".format(i))(features[i])
features[i] = getattr(self, f"adap_layer_{i}")(features[i])
return features


Expand Down
4 changes: 4 additions & 0 deletions limap/features/models/vggnet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import torch
import torch.nn as nn
import torchvision.models as models

from limap.features.models.s2dnet import *


Expand Down
1 change: 1 addition & 0 deletions limap/fitting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from _limap._fitting import *

from .fitting import *
5 changes: 2 additions & 3 deletions limap/fitting/fitting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from _limap import _base, _estimators, _fitting
import os
import numpy as np
from _limap import _estimators, _fitting
from bresenham import bresenham
from hloc.localize_inloc import interpolate_scan

Expand Down Expand Up @@ -64,7 +63,7 @@ def estimate_seg3d_from_points3d(
var2d=5.0,
):
h, w = camview.h(), camview.w()
K, R, T = camview.K(), camview.R(), camview.T()
R, T = camview.R(), camview.T()

# get points and depths
seg1_pts = np.linspace(
Expand Down
4 changes: 3 additions & 1 deletion limap/line2d/DeepLSD/deeplsd.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os

import numpy as np
import torch
from deeplsd.models.deeplsd_inference import DeepLSD

from ..base_detector import BaseDetector, BaseDetectorOptions


class DeepLSDDetector(BaseDetector):
def __init__(self, options=BaseDetectorOptions()):
super(DeepLSDDetector, self).__init__(options)
super().__init__(options)

conf = {
"detect_lines": True,
Expand Down
9 changes: 5 additions & 4 deletions limap/line2d/GlueStick/extractor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import os

import numpy as np
from sklearn.cluster import DBSCAN
import torch
from omegaconf import OmegaConf
from gluestick.models.wireframe import lines_to_wireframe
from omegaconf import OmegaConf

import limap.util.io as limapio
from limap.point2d.superpoint.superpoint import SuperPoint, sample_descriptors

from ..base_detector import BaseDetector, BaseDetectorOptions


class WireframeExtractor(BaseDetector):
def __init__(self, options=BaseDetectorOptions(), device=None):
super(WireframeExtractor, self).__init__(options)
super().__init__(options)
self.device = "cuda" if device is None else device
self.sp = (
SuperPoint({"weight_path": self.weight_path}).eval().to(self.device)
Expand All @@ -28,7 +29,7 @@ def get_module_name(self):
return "wireframe"

def get_descinfo_fname(self, descinfo_folder, img_id):
fname = os.path.join(descinfo_folder, "descinfo_{0}.npz".format(img_id))
fname = os.path.join(descinfo_folder, f"descinfo_{img_id}.npz")
return fname

def save_descinfo(self, descinfo_folder, img_id, descinfo):
Expand Down
Loading

0 comments on commit 542a9b2

Please sign in to comment.