|
25 | 25 | FindAllValidSlicesMissingLabelsd, |
26 | 26 | FindDiscrepancyRegionsDeepEditd, |
27 | 27 | NormalizeLabelsInDatasetd, |
| 28 | + RemapLabelsToSequentiald, |
28 | 29 | ResizeGuidanceMultipleLabelDeepEditd, |
29 | 30 | SingleLabelSelectiond, |
30 | 31 | SplitPredsLabeld, |
@@ -282,6 +283,100 @@ def test_correct_results(self, arguments, input_data, expected_result): |
282 | 283 | result = add_fn(input_data) |
283 | 284 | self.assertEqual(len(np.unique(result["label"])), expected_result) |
284 | 285 |
|
| 286 | + def test_ordering_determinism(self): |
| 287 | + """Test that different input ordering produces the same output (alphabetical)""" |
| 288 | + # Create a label array with different label values |
| 289 | + label = np.array([[[0, 1, 6, 3]]]) # background=0, spleen=1, liver=6, kidney=3 |
| 290 | + |
| 291 | + # Test case 1: liver first, then kidney, then spleen |
| 292 | + data1 = {"label": label.copy()} |
| 293 | + transform1 = RemapLabelsToSequentiald( |
| 294 | + keys="label", label_names={"liver": 6, "kidney": 3, "spleen": 1, "background": 0} |
| 295 | + ) |
| 296 | + result1 = transform1(data1) |
| 297 | + |
| 298 | + # Test case 2: spleen first, then kidney, then liver (different order) |
| 299 | + data2 = {"label": label.copy()} |
| 300 | + transform2 = RemapLabelsToSequentiald( |
| 301 | + keys="label", label_names={"spleen": 1, "kidney": 3, "liver": 6, "background": 0} |
| 302 | + ) |
| 303 | + result2 = transform2(data2) |
| 304 | + |
| 305 | + # Both should produce the same output (alphabetically sorted) |
| 306 | + # Expected mapping: background=0, kidney=1, liver=2, spleen=3 |
| 307 | + np.testing.assert_array_equal(result1["label"], result2["label"]) |
| 308 | + |
| 309 | + # Verify the actual mapping is alphabetical |
| 310 | + expected_output = np.array([[[0, 3, 2, 1]]]) # kidney=1, liver=2, spleen=3, background=0 |
| 311 | + np.testing.assert_array_equal(result1["label"], expected_output) |
| 312 | + |
| 313 | + # Verify label_names is correct |
| 314 | + self.assertEqual(result1["label_names"], {"background": 0, "kidney": 1, "liver": 2, "spleen": 3}) |
| 315 | + self.assertEqual(result2["label_names"], {"background": 0, "kidney": 1, "liver": 2, "spleen": 3}) |
| 316 | + |
| 317 | + def test_multiple_labels(self): |
| 318 | + """Test with multiple non-background labels""" |
| 319 | + label = np.array([[[0, 1, 2, 5]]]) # background, spleen, kidney, liver |
| 320 | + data = {"label": label.copy()} |
| 321 | + transform = RemapLabelsToSequentiald( |
| 322 | + keys="label", label_names={"spleen": 1, "kidney": 2, "liver": 5, "background": 0} |
| 323 | + ) |
| 324 | + result = transform(data) |
| 325 | + |
| 326 | + # Expected: background=0, kidney=1, liver=2, spleen=3 (alphabetical) |
| 327 | + expected = np.array([[[0, 3, 1, 2]]]) |
| 328 | + np.testing.assert_array_equal(result["label"], expected) |
| 329 | + self.assertEqual(result["label_names"], {"background": 0, "kidney": 1, "liver": 2, "spleen": 3}) |
| 330 | + |
| 331 | + def test_deprecated_name_warning(self): |
| 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 | + """ |
| 341 | + import warnings |
| 342 | + |
| 343 | + from monai.utils import deprecated |
| 344 | + |
| 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): |
| 361 | + pass |
| 362 | + |
| 363 | + data = {"label": np.array([[[0, 1]]])} |
| 364 | + |
| 365 | + with warnings.catch_warnings(record=True) as w: |
| 366 | + warnings.simplefilter("always") |
| 367 | + transform = DeprecatedNormalizeLabels(keys="label", label_names={"spleen": 1, "background": 0}) |
| 368 | + _ = transform(data) |
| 369 | + |
| 370 | + # Check that a deprecation warning was raised |
| 371 | + self.assertEqual(len(w), 1) |
| 372 | + self.assertTrue(issubclass(w[0].category, FutureWarning)) |
| 373 | + self.assertIn("RemapLabelsToSequentiald", str(w[0].message)) |
| 374 | + |
| 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 | + |
285 | 380 |
|
286 | 381 | class TestResizeGuidanceMultipleLabelCustomd(unittest.TestCase): |
287 | 382 |
|
|
0 commit comments