Skip to content

Commit 4916f3a

Browse files
committed
Fix code formatting and improve deprecation test
- Apply black formatting to transforms.py and test file - Improve test_deprecated_name_warning docstring to explain the testing approach - Add verification that actual NormalizeLabelsInDatasetd class works - Rename test class to DeprecatedNormalizeLabels for clarity Signed-off-by: Soumya Snigdha Kundu <[email protected]>
1 parent 664028c commit 4916f3a

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

monai/apps/deepedit/transforms.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ def __call__(self, data: Mapping[Hashable, np.ndarray]) -> dict[Hashable, np.nda
134134
label = np.zeros(d[key].shape)
135135

136136
# Sort label names to ensure deterministic ordering (exclude background)
137-
sorted_labels = sorted(
138-
[(k, v) for k, v in self.label_names.items() if k != "background"]
139-
)
137+
sorted_labels = sorted([(k, v) for k, v in self.label_names.items() if k != "background"])
140138

141139
# Always set background to 0 first
142140
if "background" in self.label_names:

tests/apps/deepedit/test_deepedit_transforms.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,14 @@ def test_ordering_determinism(self):
291291
# Test case 1: liver first, then kidney, then spleen
292292
data1 = {"label": label.copy()}
293293
transform1 = RemapLabelsToSequentiald(
294-
keys="label",
295-
label_names={"liver": 6, "kidney": 3, "spleen": 1, "background": 0}
294+
keys="label", label_names={"liver": 6, "kidney": 3, "spleen": 1, "background": 0}
296295
)
297296
result1 = transform1(data1)
298297

299298
# Test case 2: spleen first, then kidney, then liver (different order)
300299
data2 = {"label": label.copy()}
301300
transform2 = RemapLabelsToSequentiald(
302-
keys="label",
303-
label_names={"spleen": 1, "kidney": 3, "liver": 6, "background": 0}
301+
keys="label", label_names={"spleen": 1, "kidney": 3, "liver": 6, "background": 0}
304302
)
305303
result2 = transform2(data2)
306304

@@ -321,8 +319,7 @@ def test_multiple_labels(self):
321319
label = np.array([[[0, 1, 2, 5]]]) # background, spleen, kidney, liver
322320
data = {"label": label.copy()}
323321
transform = RemapLabelsToSequentiald(
324-
keys="label",
325-
label_names={"spleen": 1, "kidney": 2, "liver": 5, "background": 0}
322+
keys="label", label_names={"spleen": 1, "kidney": 2, "liver": 5, "background": 0}
326323
)
327324
result = transform(data)
328325

@@ -332,28 +329,54 @@ def test_multiple_labels(self):
332329
self.assertEqual(result["label_names"], {"background": 0, "kidney": 1, "liver": 2, "spleen": 3})
333330

334331
def test_deprecated_name_warning(self):
335-
"""Test that using the deprecated name raises a warning when version >= 1.6"""
332+
"""Test that NormalizeLabelsInDatasetd is properly deprecated.
333+
334+
The deprecation warning only triggers when MONAI version >= 1.6 (since="1.6").
335+
This test verifies:
336+
1. The actual NormalizeLabelsInDatasetd class is marked as deprecated in docstring
337+
2. The class is a subclass of RemapLabelsToSequentiald
338+
3. The deprecation mechanism works correctly (tested via version_val simulation)
339+
4. The actual class functions correctly
340+
"""
336341
import warnings
337342

338343
from monai.utils import deprecated
339344

340-
# Create a test class with version_val to simulate version 1.6
341-
@deprecated(since="1.6", removed="1.8", msg_suffix="Use `RemapLabelsToSequentiald` instead.", version_val="1.6")
342-
class TestDeprecatedClass(RemapLabelsToSequentiald):
345+
# Verify NormalizeLabelsInDatasetd docstring indicates deprecation
346+
self.assertIn("deprecated", NormalizeLabelsInDatasetd.__doc__.lower())
347+
self.assertIn("RemapLabelsToSequentiald", NormalizeLabelsInDatasetd.__doc__)
348+
349+
# Verify NormalizeLabelsInDatasetd is a subclass of RemapLabelsToSequentiald
350+
self.assertTrue(issubclass(NormalizeLabelsInDatasetd, RemapLabelsToSequentiald))
351+
352+
# Test the deprecation mechanism using version_val to simulate version 1.6
353+
# This verifies the @deprecated decorator behavior that NormalizeLabelsInDatasetd uses
354+
@deprecated(
355+
since="1.6",
356+
removed="1.8",
357+
msg_suffix="Use `RemapLabelsToSequentiald` instead.",
358+
version_val="1.6", # Simulate version 1.6 to trigger warning
359+
)
360+
class DeprecatedNormalizeLabels(RemapLabelsToSequentiald):
343361
pass
344362

345363
data = {"label": np.array([[[0, 1]]])}
346364

347365
with warnings.catch_warnings(record=True) as w:
348366
warnings.simplefilter("always")
349-
transform = TestDeprecatedClass(keys="label", label_names={"spleen": 1, "background": 0})
350-
_ = transform(data) # Invoke the transform to confirm functionality
367+
transform = DeprecatedNormalizeLabels(keys="label", label_names={"spleen": 1, "background": 0})
368+
_ = transform(data)
351369

352-
# Check that a deprecation warning was raised (deprecated decorator uses FutureWarning)
370+
# Check that a deprecation warning was raised
353371
self.assertEqual(len(w), 1)
354372
self.assertTrue(issubclass(w[0].category, FutureWarning))
355373
self.assertIn("RemapLabelsToSequentiald", str(w[0].message))
356374

375+
# Verify the actual NormalizeLabelsInDatasetd class works correctly
376+
transform_actual = NormalizeLabelsInDatasetd(keys="label", label_names={"spleen": 1, "background": 0})
377+
result = transform_actual({"label": np.array([[[0, 1]]])})
378+
self.assertIn("label", result)
379+
357380

358381
class TestResizeGuidanceMultipleLabelCustomd(unittest.TestCase):
359382

0 commit comments

Comments
 (0)