Skip to content

Commit 87d4873

Browse files
committed
autofix formatting
1 parent d97bdd5 commit 87d4873

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

monai/transforms/compose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def set_group_recursive(obj, gid):
291291
# Check common attribute patterns for wrapped transforms
292292
for attr_name in dir(obj):
293293
# Skip magic methods and common non-transform attributes
294-
if attr_name.startswith('__') or attr_name in ('transforms', 'transform'):
294+
if attr_name.startswith("__") or attr_name in ("transforms", "transform"):
295295
continue
296296
try:
297297
attr = getattr(obj, attr_name, None)

tests/transforms/inverse/test_invertd.py

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,16 @@ def test_invertd_with_postprocessing_transforms(self):
152152
key = "image"
153153

154154
# Preprocessing pipeline
155-
preprocessing = Compose([
156-
EnsureChannelFirstd(key),
157-
Spacingd(key, pixdim=[2.0, 2.0]),
158-
])
155+
preprocessing = Compose([EnsureChannelFirstd(key), Spacingd(key, pixdim=[2.0, 2.0])])
159156

160157
# Postprocessing with Lambdad before Invertd
161158
# Previously this would raise RuntimeError about transform ID mismatch
162-
postprocessing = Compose([
163-
Lambdad(key, func=lambda x: x), # Should be ignored during inversion
164-
Invertd(key, transform=preprocessing, orig_keys=key)
165-
])
159+
postprocessing = Compose(
160+
[
161+
Lambdad(key, func=lambda x: x), # Should be ignored during inversion
162+
Invertd(key, transform=preprocessing, orig_keys=key),
163+
]
164+
)
166165

167166
# Apply transforms
168167
item = {key: img}
@@ -174,7 +173,7 @@ def test_invertd_with_postprocessing_transforms(self):
174173
# If we get here, the bug is fixed
175174
self.assertIsNotNone(post)
176175
self.assertIn(key, post)
177-
print(f"SUCCESS! Automatic group tracking fixed the bug.")
176+
print("SUCCESS! Automatic group tracking fixed the bug.")
178177
print(f" Preprocessing group ID: {id(preprocessing)}")
179178
print(f" Postprocessing group ID: {id(postprocessing)}")
180179
except RuntimeError as e:
@@ -192,22 +191,18 @@ def test_invertd_multiple_pipelines(self):
192191
img2 = MetaTensor(img2, meta={"original_channel_dim": float("nan"), "pixdim": [1.0, 1.0, 1.0]})
193192

194193
# Two different preprocessing pipelines
195-
preprocessing1 = Compose([
196-
EnsureChannelFirstd("image1"),
197-
Spacingd("image1", pixdim=[2.0, 2.0]),
198-
])
194+
preprocessing1 = Compose([EnsureChannelFirstd("image1"), Spacingd("image1", pixdim=[2.0, 2.0])])
199195

200-
preprocessing2 = Compose([
201-
EnsureChannelFirstd("image2"),
202-
Spacingd("image2", pixdim=[1.5, 1.5]),
203-
])
196+
preprocessing2 = Compose([EnsureChannelFirstd("image2"), Spacingd("image2", pixdim=[1.5, 1.5])])
204197

205198
# Postprocessing that inverts both
206-
postprocessing = Compose([
207-
Lambdad(["image1", "image2"], func=lambda x: x),
208-
Invertd("image1", transform=preprocessing1, orig_keys="image1"),
209-
Invertd("image2", transform=preprocessing2, orig_keys="image2"),
210-
])
199+
postprocessing = Compose(
200+
[
201+
Lambdad(["image1", "image2"], func=lambda x: x),
202+
Invertd("image1", transform=preprocessing1, orig_keys="image1"),
203+
Invertd("image2", transform=preprocessing2, orig_keys="image2"),
204+
]
205+
)
211206

212207
# Apply transforms
213208
item = {"image1": img1, "image2": img2}
@@ -228,18 +223,17 @@ def test_invertd_multiple_postprocessing_transforms(self):
228223
img = MetaTensor(img, meta={"original_channel_dim": float("nan"), "pixdim": [1.0, 1.0, 1.0]})
229224
key = "image"
230225

231-
preprocessing = Compose([
232-
EnsureChannelFirstd(key),
233-
Spacingd(key, pixdim=[2.0, 2.0]),
234-
])
226+
preprocessing = Compose([EnsureChannelFirstd(key), Spacingd(key, pixdim=[2.0, 2.0])])
235227

236228
# Multiple transforms in postprocessing before Invertd
237-
postprocessing = Compose([
238-
Lambdad(key, func=lambda x: x * 2),
239-
Lambdad(key, func=lambda x: x + 1),
240-
Lambdad(key, func=lambda x: x - 1),
241-
Invertd(key, transform=preprocessing, orig_keys=key)
242-
])
229+
postprocessing = Compose(
230+
[
231+
Lambdad(key, func=lambda x: x * 2),
232+
Lambdad(key, func=lambda x: x + 1),
233+
Lambdad(key, func=lambda x: x - 1),
234+
Invertd(key, transform=preprocessing, orig_keys=key),
235+
]
236+
)
243237

244238
item = {key: img}
245239
pre = preprocessing(item)
@@ -257,15 +251,10 @@ def test_invertd_group_isolation(self):
257251
key = "image"
258252

259253
# First preprocessing
260-
preprocessing1 = Compose([
261-
EnsureChannelFirstd(key),
262-
Spacingd(key, pixdim=[2.0, 2.0]),
263-
])
254+
preprocessing1 = Compose([EnsureChannelFirstd(key), Spacingd(key, pixdim=[2.0, 2.0])])
264255

265256
# Second preprocessing (different pipeline)
266-
preprocessing2 = Compose([
267-
Spacingd(key, pixdim=[1.5, 1.5]),
268-
])
257+
preprocessing2 = Compose([Spacingd(key, pixdim=[1.5, 1.5])])
269258

270259
item = {key: img}
271260
pre1 = preprocessing1(item)
@@ -298,10 +287,7 @@ def test_compose_inverse_with_groups(self):
298287
key = "image"
299288

300289
# Create a preprocessing pipeline
301-
preprocessing = Compose([
302-
EnsureChannelFirstd(key),
303-
Spacingd(key, pixdim=[2.0, 2.0]),
304-
])
290+
preprocessing = Compose([EnsureChannelFirstd(key), Spacingd(key, pixdim=[2.0, 2.0])])
305291

306292
# Apply preprocessing
307293
item = {key: img}
@@ -326,15 +312,10 @@ def test_compose_inverse_with_postprocessing_groups(self):
326312
key = "image"
327313

328314
# Preprocessing pipeline
329-
preprocessing = Compose([
330-
EnsureChannelFirstd(key),
331-
Spacingd(key, pixdim=[2.0, 2.0]),
332-
])
315+
preprocessing = Compose([EnsureChannelFirstd(key), Spacingd(key, pixdim=[2.0, 2.0])])
333316

334317
# Postprocessing pipeline (different group)
335-
postprocessing = Compose([
336-
Lambdad(key, func=lambda x: x * 2),
337-
])
318+
postprocessing = Compose([Lambdad(key, func=lambda x: x * 2)])
338319

339320
# Apply both pipelines
340321
item = {key: img}
@@ -346,7 +327,7 @@ def test_compose_inverse_with_postprocessing_groups(self):
346327
# This WILL fail because applied_operations contains postprocessing transforms
347328
# and inverse() doesn't do group filtering (only Invertd does)
348329
with self.assertRaises(RuntimeError):
349-
inverted = preprocessing.inverse(post)
330+
preprocessing.inverse(post)
350331

351332
def test_mixed_invertd_and_compose_inverse(self):
352333
"""Test mixing Invertd (with group filtering) and Compose.inverse() (without filtering)."""
@@ -357,10 +338,7 @@ def test_mixed_invertd_and_compose_inverse(self):
357338
key = "image"
358339

359340
# First pipeline
360-
pipeline1 = Compose([
361-
EnsureChannelFirstd(key),
362-
Spacingd(key, pixdim=[2.0, 2.0]),
363-
])
341+
pipeline1 = Compose([EnsureChannelFirstd(key), Spacingd(key, pixdim=[2.0, 2.0])])
364342

365343
# Apply first pipeline
366344
item = {key: img}

0 commit comments

Comments
 (0)