Repository: Project-MONAI/MONAI
File: tests/test_deepgrow_transforms.py (Python)

Code before:
        result = SpatialCropForegroundd(**arguments)(input_data)
        np.testing.assert_allclose(result["image"], expected_result)

    @parameterized.expand([CROP_TEST_CASE_2])
    def test_correct_shape(self, arguments, input_data, expected_shape):
        result = SpatialCropForegroundd(**arguments)(input_data)
        np.testing.assert_equal(result["image"].shape, expected_shape)

    @parameterized.expand([CROP_TEST_CASE_1])
    def test_foreground_position(self, arguments, input_data, _):
        result = SpatialCropForegroundd(**arguments)(input_data)
        np.testing.assert_allclose(result[PostFix.meta("image")]["foreground_start_coord"], np.array([0, 1, 1]))
        np.testing.assert_allclose(result[PostFix.meta("image")]["foreground_end_coord"], np.array([1, 4, 4]))

        arguments["start_coord_key"] = "test_start_coord"
        arguments["end_coord_key"] = "test_end_coord"
        result = SpatialCropForegroundd(**arguments)(input_data)
        np.testing.assert_allclose(result[PostFix.meta("image")]["test_start_coord"], np.array([0, 1, 1]))
        np.testing.assert_allclose(result[PostFix.meta("image")]["test_end_coord"], np.array([1, 4, 4]))


class TestAddInitialSeedPointd(unittest.TestCase):

    @parameterized.expand([ADD_INITIAL_POINT_TEST_CASE_1])
    def test_correct_results(self, arguments, input_data, expected_result):
        seed = 0
        add_fn = AddInitialSeedPointd(**arguments)
        add_fn.set_random_state(seed)
        result = add_fn(input_data)
        self.assertEqual(result[arguments["guidance"]], expected_result)


class TestAddGuidanceSignald(unittest.TestCase):

    @parameterized.expand([ADD_GUIDANCE_TEST_CASE_1])
    def test_correct_results(self, arguments, input_data, expected_result):
        result = AddGuidanceSignald(**arguments)(input_data)
        np.testing.assert_allclose(result["image"], expected_result, rtol=1e-5)


class TestFindDiscrepancyRegionsd(unittest.TestCase):

    @parameterized.expand([FIND_DISCREPANCY_TEST_CASE_1])
    def test_correct_results(self, arguments, input_data, expected_result):
        result = FindDiscrepancyRegionsd(**arguments)(input_data)
        np.testing.assert_allclose(result[arguments["discrepancy"]], expected_result)


class TestAddRandomGuidanced(unittest.TestCase):

    @parameterized.expand([ADD_RANDOM_GUIDANCE_TEST_CASE_1])

Review comment (suggestion):
We have this test for when all the flags are false and the previous one where only one flag is false, I think we need the other three tests for when one flag at a time is false. This just ensures any future changes doesn't impact behaviour, the tests can be simple like this one if that's easier.

Code after:
    RESTORE_LABEL_TEST_CASE_5_RESULT,
]

RESTORE_LABEL_TEST_CASE_6_RESULT = np.zeros((1, 4, 8, 8))
RESTORE_LABEL_TEST_CASE_6_RESULT[-1, 1, 2:6, 2:6] = np.array(
    [[10.0, 10.0, 20.0, 20.0], [10.0, 10.0, 20.0, 20.0], [30.0, 30.0, 40.0, 40.0], [30.0, 30.0, 40.0, 40.0]]
)
RESTORE_LABEL_TEST_CASE_6_RESULT[-1, 2, 2:6, 2:6] = np.array(
    [[50.0, 50.0, 60.0, 60.0], [50.0, 50.0, 60.0, 60.0], [70.0, 70.0, 80.0, 80.0], [70.0, 70.0, 80.0, 80.0]]
)

RESTORE_LABEL_TEST_CASE_6 = [
    {"keys": ["pred"], "ref_image": "image", "mode": "nearest", "restore_slicing": False},
    DATA_13,
    RESTORE_LABEL_TEST_CASE_6_RESULT,
]

RESTORE_LABEL_TEST_CASE_7 = [
    {
        "keys": ["pred"],
        "ref_image": "image",
        "mode": "nearest",
        "restore_resizing": False,
        "restore_cropping": False,
        "restore_spacing": False,
        "restore_slicing": False,
    },
    DATA_11,
    np.array([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]]),
]

FETCH_2D_SLICE_TEST_CASE_1 = [
    {"keys": ["image"], "guidance": "guidance"},
    DATA_12,
    np.array([[9, 10, 11], [12, 13, 14], [15, 16, 17]]),
]


class TestFindAllValidSlicesd(unittest.TestCase):

    @parameterized.expand([FIND_SLICE_TEST_CASE_1, FIND_SLICE_TEST_CASE_2])
    def test_correct_results(self, arguments, input_data, expected_result):
        result = FindAllValidSlicesd(**arguments)(input_data)
        np.testing.assert_allclose(result[arguments["sids"]], expected_result)


class TestSpatialCropForegroundd(unittest.TestCase):

    @parameterized.expand([CROP_TEST_CASE_1])
    def test_correct_results(self, arguments, input_data, expected_result):
        result = SpatialCropForegroundd(**arguments)(input_data)