Skip to content

Commit 83bacda

Browse files
bottlerfacebook-github-bot
authored andcommitted
lint
Summary: Fix recent flake complaints Reviewed By: MichaelRamamonjisoa Differential Revision: D51811912 fbshipit-source-id: 65183f5bc7058da910e4d5a63b2250ce8637f1cc
1 parent f74fc45 commit 83bacda

20 files changed

+73
-85
lines changed

.flake8

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[flake8]
2-
ignore = E203, E266, E501, W503, E221
2+
# B028 No explicit stacklevel argument found.
3+
# B907 'foo' is manually surrounded by quotes, consider using the `!r` conversion flag.
4+
# B905 `zip()` without an explicit `strict=` parameter.
5+
ignore = E203, E266, E501, W503, E221, B028, B905, B907
36
max-line-length = 88
47
max-complexity = 18
58
select = B,C,E,F,W,T4,B9

pytorch3d/implicitron/dataset/load_llff.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ def _minify(basedir, path_manager, factors=(), resolutions=()):
3434

3535
imgdir = os.path.join(basedir, "images")
3636
imgs = [os.path.join(imgdir, f) for f in sorted(_ls(path_manager, imgdir))]
37-
imgs = [
38-
f
39-
for f in imgs
40-
if any([f.endswith(ex) for ex in ["JPG", "jpg", "png", "jpeg", "PNG"]])
41-
]
37+
imgs = [f for f in imgs if f.endswith("JPG", "jpg", "png", "jpeg", "PNG")]
4238
imgdir_orig = imgdir
4339

4440
wd = os.getcwd()

pytorch3d/implicitron/dataset/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def resize_image(
200200
mode: str = "bilinear",
201201
) -> Tuple[torch.Tensor, float, torch.Tensor]:
202202

203-
if type(image) == np.ndarray:
203+
if isinstance(image, np.ndarray):
204204
image = torch.from_numpy(image)
205205

206206
if image_height is None or image_width is None:

pytorch3d/io/obj_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ def save_obj(
750750
if path_manager is None:
751751
path_manager = PathManager()
752752

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

756756
# Save the .obj file

pytorch3d/renderer/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,6 @@ def parse_image_size(
453453
raise ValueError("Image size can only be a tuple/list of (H, W)")
454454
if not all(i > 0 for i in image_size):
455455
raise ValueError("Image sizes must be greater than 0; got %d, %d" % image_size)
456-
if not all(type(i) == int for i in image_size):
456+
if not all(isinstance(i, int) for i in image_size):
457457
raise ValueError("Image sizes must be integers; got %f, %f" % image_size)
458458
return tuple(image_size)

pytorch3d/structures/meshes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ def join_meshes_as_batch(meshes: List[Meshes], include_textures: bool = True) ->
16981698
# Now we know there are multiple meshes and they have textures to merge.
16991699
all_textures = [mesh.textures for mesh in meshes]
17001700
first = all_textures[0]
1701-
tex_types_same = all(type(tex) == type(first) for tex in all_textures)
1701+
tex_types_same = all(type(tex) == type(first) for tex in all_textures) # noqa: E721
17021702

17031703
if not tex_types_same:
17041704
raise ValueError("All meshes in the batch must have the same type of texture.")

pytorch3d/transforms/transform3d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,22 +440,22 @@ def transform_normals(self, normals) -> torch.Tensor:
440440

441441
def translate(self, *args, **kwargs) -> "Transform3d":
442442
return self.compose(
443-
Translate(device=self.device, dtype=self.dtype, *args, **kwargs)
443+
Translate(*args, device=self.device, dtype=self.dtype, **kwargs)
444444
)
445445

446446
def scale(self, *args, **kwargs) -> "Transform3d":
447447
return self.compose(
448-
Scale(device=self.device, dtype=self.dtype, *args, **kwargs)
448+
Scale(*args, device=self.device, dtype=self.dtype, **kwargs)
449449
)
450450

451451
def rotate(self, *args, **kwargs) -> "Transform3d":
452452
return self.compose(
453-
Rotate(device=self.device, dtype=self.dtype, *args, **kwargs)
453+
Rotate(*args, device=self.device, dtype=self.dtype, **kwargs)
454454
)
455455

456456
def rotate_axis_angle(self, *args, **kwargs) -> "Transform3d":
457457
return self.compose(
458-
RotateAxisAngle(device=self.device, dtype=self.dtype, *args, **kwargs)
458+
RotateAxisAngle(*args, device=self.device, dtype=self.dtype, **kwargs)
459459
)
460460

461461
def clone(self) -> "Transform3d":

tests/implicitron/models/test_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
class TestUtils(unittest.TestCase):
1616
def test_prepare_inputs_wrong_num_dim(self):
1717
img = torch.randn(3, 3, 3)
18-
with self.assertRaises(ValueError) as context:
18+
text = (
19+
"Model received unbatched inputs. "
20+
+ "Perhaps they came from a FrameData which had not been collated."
21+
)
22+
with self.assertRaisesRegex(ValueError, text):
1923
img, fg_prob, depth_map = preprocess_input(
2024
img, None, None, True, True, 0.5, (0.0, 0.0, 0.0)
2125
)
22-
self.assertEqual(
23-
"Model received unbatched inputs. "
24-
+ "Perhaps they came from a FrameData which had not been collated.",
25-
context.exception,
26-
)
2726

2827
def test_prepare_inputs_mask_image_true(self):
2928
batch, channels, height, width = 2, 3, 10, 10

tests/implicitron/test_frame_data_builder.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,22 @@ def test_load_image(self):
224224

225225
def test_load_mask(self):
226226
path = os.path.join(self.dataset_root, self.frame_annotation.mask.path)
227+
path = self.path_manager.get_local_path(path)
227228
mask = load_mask(path)
228229
self.assertEqual(mask.dtype, np.float32)
229230
self.assertLessEqual(np.max(mask), 1.0)
230231
self.assertGreaterEqual(np.min(mask), 0.0)
231232

232233
def test_load_depth(self):
233234
path = os.path.join(self.dataset_root, self.frame_annotation.depth.path)
235+
path = self.path_manager.get_local_path(path)
234236
depth_map = load_depth(path, self.frame_annotation.depth.scale_adjustment)
235237
self.assertEqual(depth_map.dtype, np.float32)
236238
self.assertEqual(len(depth_map.shape), 3)
237239

238240
def test_load_16big_png_depth(self):
239241
path = os.path.join(self.dataset_root, self.frame_annotation.depth.path)
242+
path = self.path_manager.get_local_path(path)
240243
depth_map = load_16big_png_depth(path)
241244
self.assertEqual(depth_map.dtype, np.float32)
242245
self.assertEqual(len(depth_map.shape), 2)
@@ -245,6 +248,7 @@ def test_load_1bit_png_mask(self):
245248
mask_path = os.path.join(
246249
self.dataset_root, self.frame_annotation.depth.mask_path
247250
)
251+
mask_path = self.path_manager.get_local_path(mask_path)
248252
mask = load_1bit_png_mask(mask_path)
249253
self.assertEqual(mask.dtype, np.float32)
250254
self.assertEqual(len(mask.shape), 2)
@@ -253,6 +257,7 @@ def test_load_depth_mask(self):
253257
mask_path = os.path.join(
254258
self.dataset_root, self.frame_annotation.depth.mask_path
255259
)
260+
mask_path = self.path_manager.get_local_path(mask_path)
256261
mask = load_depth_mask(mask_path)
257262
self.assertEqual(mask.dtype, np.float32)
258263
self.assertEqual(len(mask.shape), 3)

tests/implicitron/test_models_renderer_base.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,49 +38,45 @@ def test_implicitron_from_bins(self) -> None:
3838
def test_implicitron_raise_value_error_bins_is_set_and_try_to_set_lengths(
3939
self,
4040
) -> None:
41-
with self.assertRaises(ValueError) as context:
42-
ray_bundle = ImplicitronRayBundle(
43-
origins=torch.rand(2, 3, 4, 3),
44-
directions=torch.rand(2, 3, 4, 3),
45-
lengths=None,
46-
xys=torch.rand(2, 3, 4, 2),
47-
bins=torch.rand(2, 3, 4, 1),
48-
)
41+
ray_bundle = ImplicitronRayBundle(
42+
origins=torch.rand(2, 3, 4, 3),
43+
directions=torch.rand(2, 3, 4, 3),
44+
lengths=None,
45+
xys=torch.rand(2, 3, 4, 2),
46+
bins=torch.rand(2, 3, 4, 14),
47+
)
48+
with self.assertRaisesRegex(
49+
ValueError,
50+
"If the bins attribute is not None you cannot set the lengths attribute.",
51+
):
4952
ray_bundle.lengths = torch.empty(2)
50-
self.assertEqual(
51-
str(context.exception),
52-
"If the bins attribute is not None you cannot set the lengths attribute.",
53-
)
5453

5554
def test_implicitron_raise_value_error_if_bins_dim_equal_1(self) -> None:
56-
with self.assertRaises(ValueError) as context:
55+
with self.assertRaisesRegex(
56+
ValueError, "The last dim of bins must be at least superior or equal to 2."
57+
):
5758
ImplicitronRayBundle(
5859
origins=torch.rand(2, 3, 4, 3),
5960
directions=torch.rand(2, 3, 4, 3),
6061
lengths=None,
6162
xys=torch.rand(2, 3, 4, 2),
6263
bins=torch.rand(2, 3, 4, 1),
6364
)
64-
self.assertEqual(
65-
str(context.exception),
66-
"The last dim of bins must be at least superior or equal to 2.",
67-
)
6865

6966
def test_implicitron_raise_value_error_if_neither_bins_or_lengths_provided(
7067
self,
7168
) -> None:
72-
with self.assertRaises(ValueError) as context:
69+
with self.assertRaisesRegex(
70+
ValueError,
71+
"Please set either bins or lengths to initialize an ImplicitronRayBundle.",
72+
):
7373
ImplicitronRayBundle(
7474
origins=torch.rand(2, 3, 4, 3),
7575
directions=torch.rand(2, 3, 4, 3),
7676
lengths=None,
7777
xys=torch.rand(2, 3, 4, 2),
7878
bins=None,
7979
)
80-
self.assertEqual(
81-
str(context.exception),
82-
"Please set either bins or lengths to initialize an ImplicitronRayBundle.",
83-
)
8480

8581
def test_conical_frustum_to_gaussian(self) -> None:
8682
origins = torch.zeros(3, 3, 3)
@@ -266,8 +262,6 @@ def test_conical_frustum_to_gaussian_raise_valueerror(self) -> None:
266262
ray = ImplicitronRayBundle(
267263
origins=origins, directions=directions, lengths=lengths, xys=None
268264
)
269-
with self.assertRaises(ValueError) as context:
270-
_ = conical_frustum_to_gaussian(ray)
271265

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

279-
self.assertEqual(expected_error_message, str(context.exception))
273+
with self.assertRaisesRegex(ValueError, expected_error_message):
274+
_ = conical_frustum_to_gaussian(ray)
280275

281276
# Ensure message is coherent with AbstractMaskRaySampler
282277
class FakeRaySampler(AbstractMaskRaySampler):

tests/test_cameras.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,8 @@ def test_getitem(self):
964964
with self.assertRaisesRegex(IndexError, "out of bounds"):
965965
cam[N_CAMERAS]
966966

967+
index = torch.tensor([1, 0, 1], dtype=torch.bool)
967968
with self.assertRaisesRegex(ValueError, "does not match cameras"):
968-
index = torch.tensor([1, 0, 1], dtype=torch.bool)
969969
cam[index]
970970

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

977+
index = torch.tensor(SLICE, dtype=torch.float32)
977978
with self.assertRaisesRegex(ValueError, "Invalid index type"):
978-
index = torch.tensor(SLICE, dtype=torch.float32)
979979
cam[index]
980980

981981
def test_get_full_transform(self):

tests/test_io_obj.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ def test_load_obj_error_invalid_texture_indices(self):
422422

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

435435
# Invalid faces shape
436+
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
437+
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
436438
with self.assertRaises(ValueError) as error:
437-
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
438-
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
439439
with NamedTemporaryFile(mode="w", suffix=".obj") as f:
440440
save_obj(Path(f.name), verts, faces)
441441
expected_message = (

tests/test_io_ply.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,19 @@ def test_save_load_with_normals(self):
308308

309309
def test_save_ply_invalid_shapes(self):
310310
# Invalid vertices shape
311+
verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]]) # (V, 4)
312+
faces = torch.LongTensor([[0, 1, 2]])
311313
with self.assertRaises(ValueError) as error:
312-
verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]]) # (V, 4)
313-
faces = torch.LongTensor([[0, 1, 2]])
314314
save_ply(BytesIO(), verts, faces)
315315
expected_message = (
316316
"Argument 'verts' should either be empty or of shape (num_verts, 3)."
317317
)
318318
self.assertTrue(expected_message, error.exception)
319319

320320
# Invalid faces shape
321+
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
322+
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
321323
with self.assertRaises(ValueError) as error:
322-
verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
323-
faces = torch.LongTensor([[0, 1, 2, 3]]) # (F, 4)
324324
save_ply(BytesIO(), verts, faces)
325325
expected_message = (
326326
"Argument 'faces' should either be empty or of shape (num_faces, 3)."

tests/test_meshes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,15 @@ def test_init_error(self):
324324
]
325325
faces_list = mesh.faces_list()
326326

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

331330
verts_padded = mesh.verts_padded() # on cpu
332331
verts_padded = verts_padded.to("cuda:0")
333332
faces_padded = mesh.faces_padded()
334333

335-
with self.assertRaises(ValueError) as cm:
334+
with self.assertRaisesRegex(ValueError, "same device"):
336335
Meshes(verts=verts_padded, faces=faces_padded)
337-
self.assertTrue("same device" in cm.msg)
338336

339337
def test_simple_random_meshes(self):
340338

tests/test_pointclouds.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,28 @@ def test_init_error(self):
148148
features_list = clouds.features_list()
149149
normals_list = clouds.normals_list()
150150

151-
with self.assertRaises(ValueError) as cm:
151+
with self.assertRaisesRegex(ValueError, "same device"):
152152
Pointclouds(
153153
points=points_list, features=features_list, normals=normals_list
154154
)
155-
self.assertTrue("same device" in cm.msg)
156155

157156
points_list = clouds.points_list()
158157
features_list = [
159158
f.to("cpu") if random.uniform(0, 1) > 0.2 else f for f in features_list
160159
]
161-
with self.assertRaises(ValueError) as cm:
160+
with self.assertRaisesRegex(ValueError, "same device"):
162161
Pointclouds(
163162
points=points_list, features=features_list, normals=normals_list
164163
)
165-
self.assertTrue("same device" in cm.msg)
166164

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

171-
with self.assertRaises(ValueError) as cm:
169+
with self.assertRaisesRegex(ValueError, "same device"):
172170
Pointclouds(
173171
points=points_padded, features=features_padded, normals=normals_padded
174172
)
175-
self.assertTrue("same device" in cm.msg)
176173

177174
def test_all_constructions(self):
178175
public_getters = [

0 commit comments

Comments
 (0)