Skip to content

Commit 6798485

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 6798485

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-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: 27 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,45 @@ 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 using the deprecated NormalizeLabelsInDatasetd raises a warning.
333+
334+
This test verifies the deprecation mechanism by using version_val parameter
335+
to simulate version 1.6 where the deprecation warning should be triggered.
336+
The actual NormalizeLabelsInDatasetd class uses the @deprecated decorator
337+
with since="1.6", so warnings will only appear when MONAI version >= 1.6.
338+
"""
336339
import warnings
337340

338341
from monai.utils import deprecated
339342

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):
343-
pass
344-
345343
data = {"label": np.array([[[0, 1]]])}
346344

345+
# Create a test wrapper that simulates version 1.6 behavior
346+
# This tests the same deprecation mechanism used by NormalizeLabelsInDatasetd
347+
@deprecated(
348+
since="1.6",
349+
removed="1.8",
350+
msg_suffix="Use `RemapLabelsToSequentiald` instead.",
351+
version_val="1.6", # Simulate version 1.6 to trigger warning
352+
)
353+
class DeprecatedNormalizeLabels(RemapLabelsToSequentiald):
354+
pass
355+
347356
with warnings.catch_warnings(record=True) as w:
348357
warnings.simplefilter("always")
349-
transform = TestDeprecatedClass(keys="label", label_names={"spleen": 1, "background": 0})
358+
transform = DeprecatedNormalizeLabels(keys="label", label_names={"spleen": 1, "background": 0})
350359
_ = transform(data) # Invoke the transform to confirm functionality
351360

352361
# Check that a deprecation warning was raised (deprecated decorator uses FutureWarning)
353362
self.assertEqual(len(w), 1)
354363
self.assertTrue(issubclass(w[0].category, FutureWarning))
355364
self.assertIn("RemapLabelsToSequentiald", str(w[0].message))
356365

366+
# Also verify the actual NormalizeLabelsInDatasetd class exists and works
367+
transform_actual = NormalizeLabelsInDatasetd(keys="label", label_names={"spleen": 1, "background": 0})
368+
result = transform_actual({"label": np.array([[[0, 1]]])})
369+
self.assertIn("label", result)
370+
357371

358372
class TestResizeGuidanceMultipleLabelCustomd(unittest.TestCase):
359373

0 commit comments

Comments
 (0)