Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
Summary: Fix recent flake complaints

Reviewed By: MichaelRamamonjisoa

Differential Revision: D51811912

fbshipit-source-id: 65183f5bc7058da910e4d5a63b2250ce8637f1cc
  • Loading branch information
bottler authored and facebook-github-bot committed Dec 4, 2023
1 parent f74fc45 commit 83bacda
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 85 deletions.
5 changes: 4 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[flake8]
ignore = E203, E266, E501, W503, E221
# B028 No explicit stacklevel argument found.
# B907 'foo' is manually surrounded by quotes, consider using the `!r` conversion flag.
# B905 `zip()` without an explicit `strict=` parameter.
ignore = E203, E266, E501, W503, E221, B028, B905, B907
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4,B9
Expand Down
6 changes: 1 addition & 5 deletions pytorch3d/implicitron/dataset/load_llff.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ def _minify(basedir, path_manager, factors=(), resolutions=()):

imgdir = os.path.join(basedir, "images")
imgs = [os.path.join(imgdir, f) for f in sorted(_ls(path_manager, imgdir))]
imgs = [
f
for f in imgs
if any([f.endswith(ex) for ex in ["JPG", "jpg", "png", "jpeg", "PNG"]])
]
imgs = [f for f in imgs if f.endswith("JPG", "jpg", "png", "jpeg", "PNG")]
imgdir_orig = imgdir

wd = os.getcwd()
Expand Down
2 changes: 1 addition & 1 deletion pytorch3d/implicitron/dataset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def resize_image(
mode: str = "bilinear",
) -> Tuple[torch.Tensor, float, torch.Tensor]:

if type(image) == np.ndarray:
if isinstance(image, np.ndarray):
image = torch.from_numpy(image)

if image_height is None or image_width is None:
Expand Down
2 changes: 1 addition & 1 deletion pytorch3d/io/obj_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ def save_obj(
if path_manager is None:
path_manager = PathManager()

save_texture = all([t is not None for t in [faces_uvs, verts_uvs, texture_map]])
save_texture = all(t is not None for t in [faces_uvs, verts_uvs, texture_map])
output_path = Path(f)

# Save the .obj file
Expand Down
2 changes: 1 addition & 1 deletion pytorch3d/renderer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,6 @@ def parse_image_size(
raise ValueError("Image size can only be a tuple/list of (H, W)")
if not all(i > 0 for i in image_size):
raise ValueError("Image sizes must be greater than 0; got %d, %d" % image_size)
if not all(type(i) == int for i in image_size):
if not all(isinstance(i, int) for i in image_size):
raise ValueError("Image sizes must be integers; got %f, %f" % image_size)
return tuple(image_size)
2 changes: 1 addition & 1 deletion pytorch3d/structures/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ def join_meshes_as_batch(meshes: List[Meshes], include_textures: bool = True) ->
# Now we know there are multiple meshes and they have textures to merge.
all_textures = [mesh.textures for mesh in meshes]
first = all_textures[0]
tex_types_same = all(type(tex) == type(first) for tex in all_textures)
tex_types_same = all(type(tex) == type(first) for tex in all_textures) # noqa: E721

if not tex_types_same:
raise ValueError("All meshes in the batch must have the same type of texture.")
Expand Down
8 changes: 4 additions & 4 deletions pytorch3d/transforms/transform3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,22 +440,22 @@ def transform_normals(self, normals) -> torch.Tensor:

def translate(self, *args, **kwargs) -> "Transform3d":
return self.compose(
Translate(device=self.device, dtype=self.dtype, *args, **kwargs)
Translate(*args, device=self.device, dtype=self.dtype, **kwargs)
)

def scale(self, *args, **kwargs) -> "Transform3d":
return self.compose(
Scale(device=self.device, dtype=self.dtype, *args, **kwargs)
Scale(*args, device=self.device, dtype=self.dtype, **kwargs)
)

def rotate(self, *args, **kwargs) -> "Transform3d":
return self.compose(
Rotate(device=self.device, dtype=self.dtype, *args, **kwargs)
Rotate(*args, device=self.device, dtype=self.dtype, **kwargs)
)

def rotate_axis_angle(self, *args, **kwargs) -> "Transform3d":
return self.compose(
RotateAxisAngle(device=self.device, dtype=self.dtype, *args, **kwargs)
RotateAxisAngle(*args, device=self.device, dtype=self.dtype, **kwargs)
)

def clone(self) -> "Transform3d":
Expand Down
11 changes: 5 additions & 6 deletions tests/implicitron/models/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
class TestUtils(unittest.TestCase):
def test_prepare_inputs_wrong_num_dim(self):
img = torch.randn(3, 3, 3)
with self.assertRaises(ValueError) as context:
text = (
"Model received unbatched inputs. "
+ "Perhaps they came from a FrameData which had not been collated."
)
with self.assertRaisesRegex(ValueError, text):
img, fg_prob, depth_map = preprocess_input(
img, None, None, True, True, 0.5, (0.0, 0.0, 0.0)
)
self.assertEqual(
"Model received unbatched inputs. "
+ "Perhaps they came from a FrameData which had not been collated.",
context.exception,
)

def test_prepare_inputs_mask_image_true(self):
batch, channels, height, width = 2, 3, 10, 10
Expand Down
5 changes: 5 additions & 0 deletions tests/implicitron/test_frame_data_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,22 @@ def test_load_image(self):

def test_load_mask(self):
path = os.path.join(self.dataset_root, self.frame_annotation.mask.path)
path = self.path_manager.get_local_path(path)
mask = load_mask(path)
self.assertEqual(mask.dtype, np.float32)
self.assertLessEqual(np.max(mask), 1.0)
self.assertGreaterEqual(np.min(mask), 0.0)

def test_load_depth(self):
path = os.path.join(self.dataset_root, self.frame_annotation.depth.path)
path = self.path_manager.get_local_path(path)
depth_map = load_depth(path, self.frame_annotation.depth.scale_adjustment)
self.assertEqual(depth_map.dtype, np.float32)
self.assertEqual(len(depth_map.shape), 3)

def test_load_16big_png_depth(self):
path = os.path.join(self.dataset_root, self.frame_annotation.depth.path)
path = self.path_manager.get_local_path(path)
depth_map = load_16big_png_depth(path)
self.assertEqual(depth_map.dtype, np.float32)
self.assertEqual(len(depth_map.shape), 2)
Expand All @@ -245,6 +248,7 @@ def test_load_1bit_png_mask(self):
mask_path = os.path.join(
self.dataset_root, self.frame_annotation.depth.mask_path
)
mask_path = self.path_manager.get_local_path(mask_path)
mask = load_1bit_png_mask(mask_path)
self.assertEqual(mask.dtype, np.float32)
self.assertEqual(len(mask.shape), 2)
Expand All @@ -253,6 +257,7 @@ def test_load_depth_mask(self):
mask_path = os.path.join(
self.dataset_root, self.frame_annotation.depth.mask_path
)
mask_path = self.path_manager.get_local_path(mask_path)
mask = load_depth_mask(mask_path)
self.assertEqual(mask.dtype, np.float32)
self.assertEqual(len(mask.shape), 3)
45 changes: 20 additions & 25 deletions tests/implicitron/test_models_renderer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,45 @@ def test_implicitron_from_bins(self) -> None:
def test_implicitron_raise_value_error_bins_is_set_and_try_to_set_lengths(
self,
) -> None:
with self.assertRaises(ValueError) as context:
ray_bundle = ImplicitronRayBundle(
origins=torch.rand(2, 3, 4, 3),
directions=torch.rand(2, 3, 4, 3),
lengths=None,
xys=torch.rand(2, 3, 4, 2),
bins=torch.rand(2, 3, 4, 1),
)
ray_bundle = ImplicitronRayBundle(
origins=torch.rand(2, 3, 4, 3),
directions=torch.rand(2, 3, 4, 3),
lengths=None,
xys=torch.rand(2, 3, 4, 2),
bins=torch.rand(2, 3, 4, 14),
)
with self.assertRaisesRegex(
ValueError,
"If the bins attribute is not None you cannot set the lengths attribute.",
):
ray_bundle.lengths = torch.empty(2)
self.assertEqual(
str(context.exception),
"If the bins attribute is not None you cannot set the lengths attribute.",
)

def test_implicitron_raise_value_error_if_bins_dim_equal_1(self) -> None:
with self.assertRaises(ValueError) as context:
with self.assertRaisesRegex(
ValueError, "The last dim of bins must be at least superior or equal to 2."
):
ImplicitronRayBundle(
origins=torch.rand(2, 3, 4, 3),
directions=torch.rand(2, 3, 4, 3),
lengths=None,
xys=torch.rand(2, 3, 4, 2),
bins=torch.rand(2, 3, 4, 1),
)
self.assertEqual(
str(context.exception),
"The last dim of bins must be at least superior or equal to 2.",
)

def test_implicitron_raise_value_error_if_neither_bins_or_lengths_provided(
self,
) -> None:
with self.assertRaises(ValueError) as context:
with self.assertRaisesRegex(
ValueError,
"Please set either bins or lengths to initialize an ImplicitronRayBundle.",
):
ImplicitronRayBundle(
origins=torch.rand(2, 3, 4, 3),
directions=torch.rand(2, 3, 4, 3),
lengths=None,
xys=torch.rand(2, 3, 4, 2),
bins=None,
)
self.assertEqual(
str(context.exception),
"Please set either bins or lengths to initialize an ImplicitronRayBundle.",
)

def test_conical_frustum_to_gaussian(self) -> None:
origins = torch.zeros(3, 3, 3)
Expand Down Expand Up @@ -266,8 +262,6 @@ def test_conical_frustum_to_gaussian_raise_valueerror(self) -> None:
ray = ImplicitronRayBundle(
origins=origins, directions=directions, lengths=lengths, xys=None
)
with self.assertRaises(ValueError) as context:
_ = conical_frustum_to_gaussian(ray)

expected_error_message = (
"RayBundle pixel_radii_2d or bins have not been provided."
Expand All @@ -276,7 +270,8 @@ def test_conical_frustum_to_gaussian_raise_valueerror(self) -> None:
"`cast_ray_bundle_as_cone` to True?"
)

self.assertEqual(expected_error_message, str(context.exception))
with self.assertRaisesRegex(ValueError, expected_error_message):
_ = conical_frustum_to_gaussian(ray)

# Ensure message is coherent with AbstractMaskRaySampler
class FakeRaySampler(AbstractMaskRaySampler):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,8 @@ def test_getitem(self):
with self.assertRaisesRegex(IndexError, "out of bounds"):
cam[N_CAMERAS]

index = torch.tensor([1, 0, 1], dtype=torch.bool)
with self.assertRaisesRegex(ValueError, "does not match cameras"):
index = torch.tensor([1, 0, 1], dtype=torch.bool)
cam[index]

with self.assertRaisesRegex(ValueError, "Invalid index type"):
Expand All @@ -974,8 +974,8 @@ def test_getitem(self):
with self.assertRaisesRegex(ValueError, "Invalid index type"):
cam[[True, False]]

index = torch.tensor(SLICE, dtype=torch.float32)
with self.assertRaisesRegex(ValueError, "Invalid index type"):
index = torch.tensor(SLICE, dtype=torch.float32)
cam[index]

def test_get_full_transform(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_io_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ def test_load_obj_error_invalid_texture_indices(self):

def test_save_obj_invalid_shapes(self):
# Invalid vertices shape
verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]]) # (V, 4)
faces = torch.LongTensor([[0, 1, 2]])
with self.assertRaises(ValueError) as error:
verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]]) # (V, 4)
faces = torch.LongTensor([[0, 1, 2]])
with NamedTemporaryFile(mode="w", suffix=".obj") as f:
save_obj(Path(f.name), verts, faces)
expected_message = (
Expand All @@ -433,9 +433,9 @@ def test_save_obj_invalid_shapes(self):
self.assertTrue(expected_message, error.exception)

# Invalid faces shape
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
with self.assertRaises(ValueError) as error:
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
with NamedTemporaryFile(mode="w", suffix=".obj") as f:
save_obj(Path(f.name), verts, faces)
expected_message = (
Expand Down
8 changes: 4 additions & 4 deletions tests/test_io_ply.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,19 @@ def test_save_load_with_normals(self):

def test_save_ply_invalid_shapes(self):
# Invalid vertices shape
verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]]) # (V, 4)
faces = torch.LongTensor([[0, 1, 2]])
with self.assertRaises(ValueError) as error:
verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]]) # (V, 4)
faces = torch.LongTensor([[0, 1, 2]])
save_ply(BytesIO(), verts, faces)
expected_message = (
"Argument 'verts' should either be empty or of shape (num_verts, 3)."
)
self.assertTrue(expected_message, error.exception)

# Invalid faces shape
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
with self.assertRaises(ValueError) as error:
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
save_ply(BytesIO(), verts, faces)
expected_message = (
"Argument 'faces' should either be empty or of shape (num_faces, 3)."
Expand Down
6 changes: 2 additions & 4 deletions tests/test_meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,15 @@ def test_init_error(self):
]
faces_list = mesh.faces_list()

with self.assertRaises(ValueError) as cm:
with self.assertRaisesRegex(ValueError, "same device"):
Meshes(verts=verts_list, faces=faces_list)
self.assertTrue("same device" in cm.msg)

verts_padded = mesh.verts_padded() # on cpu
verts_padded = verts_padded.to("cuda:0")
faces_padded = mesh.faces_padded()

with self.assertRaises(ValueError) as cm:
with self.assertRaisesRegex(ValueError, "same device"):
Meshes(verts=verts_padded, faces=faces_padded)
self.assertTrue("same device" in cm.msg)

def test_simple_random_meshes(self):

Expand Down
9 changes: 3 additions & 6 deletions tests/test_pointclouds.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,28 @@ def test_init_error(self):
features_list = clouds.features_list()
normals_list = clouds.normals_list()

with self.assertRaises(ValueError) as cm:
with self.assertRaisesRegex(ValueError, "same device"):
Pointclouds(
points=points_list, features=features_list, normals=normals_list
)
self.assertTrue("same device" in cm.msg)

points_list = clouds.points_list()
features_list = [
f.to("cpu") if random.uniform(0, 1) > 0.2 else f for f in features_list
]
with self.assertRaises(ValueError) as cm:
with self.assertRaisesRegex(ValueError, "same device"):
Pointclouds(
points=points_list, features=features_list, normals=normals_list
)
self.assertTrue("same device" in cm.msg)

points_padded = clouds.points_padded() # on cuda:0
features_padded = clouds.features_padded().to("cpu")
normals_padded = clouds.normals_padded()

with self.assertRaises(ValueError) as cm:
with self.assertRaisesRegex(ValueError, "same device"):
Pointclouds(
points=points_padded, features=features_padded, normals=normals_padded
)
self.assertTrue("same device" in cm.msg)

def test_all_constructions(self):
public_getters = [
Expand Down
Loading

0 comments on commit 83bacda

Please sign in to comment.