Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update integration tests metatensor #4262

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions tests/test_cross_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from monai.apps import CrossValidation, DecathlonDataset
from monai.data import MetaTensor
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd, ToTensord
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd
from tests.utils import skip_if_downloading_fails, skip_if_quick


Expand All @@ -23,12 +23,7 @@ class TestCrossValidation(unittest.TestCase):
def test_values(self):
testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data")
train_transform = Compose(
[
LoadImaged(keys=["image", "label"]),
AddChanneld(keys=["image", "label"]),
ScaleIntensityd(keys="image"),
ToTensord(keys=["image", "label"]),
]
[LoadImaged(keys=["image", "label"]), AddChanneld(keys=["image", "label"]), ScaleIntensityd(keys="image")]
)
val_transform = LoadImaged(keys=["image", "label"])

Expand Down
9 changes: 2 additions & 7 deletions tests/test_decathlondataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from monai.apps import DecathlonDataset
from monai.data import MetaTensor
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd, ToTensord
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd
from tests.utils import skip_if_downloading_fails, skip_if_quick


Expand All @@ -25,12 +25,7 @@ class TestDecathlonDataset(unittest.TestCase):
def test_values(self):
testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data")
transform = Compose(
[
LoadImaged(keys=["image", "label"]),
AddChanneld(keys=["image", "label"]),
ScaleIntensityd(keys="image"),
ToTensord(keys=["image", "label"]),
]
[LoadImaged(keys=["image", "label"]), AddChanneld(keys=["image", "label"]), ScaleIntensityd(keys="image")]
)

def _test_dataset(dataset):
Expand Down
13 changes: 3 additions & 10 deletions tests/test_mednistdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from monai.apps import MedNISTDataset
from monai.data import MetaTensor
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd, ToTensord
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd
from tests.utils import skip_if_downloading_fails, skip_if_quick

MEDNIST_FULL_DATASET_LENGTH = 58954
Expand All @@ -26,14 +26,7 @@ class TestMedNISTDataset(unittest.TestCase):
@skip_if_quick
def test_values(self):
testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data")
transform = Compose(
[
LoadImaged(keys="image"),
AddChanneld(keys="image"),
ScaleIntensityd(keys="image"),
ToTensord(keys=["image", "label"]),
]
)
transform = Compose([LoadImaged(keys="image"), AddChanneld(keys="image"), ScaleIntensityd(keys="image")])

def _test_dataset(dataset):
self.assertEqual(len(dataset), int(MEDNIST_FULL_DATASET_LENGTH * dataset.test_frac))
Expand All @@ -59,7 +52,7 @@ def _test_dataset(dataset):
data = MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False, seed=42)
_test_dataset(data)
self.assertEqual(data[0]["class_name"], "AbdomenCT")
self.assertEqual(data[0]["label"].cpu().item(), 0)
self.assertEqual(data[0]["label"], 0)
shutil.rmtree(os.path.join(testing_dir, "MedNIST"))
try:
MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_rotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import torch
from parameterized import parameterized

from monai.data import MetaTensor
from monai.transforms import Rotated
from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, NumpyImageTestCase3D

Expand Down Expand Up @@ -65,6 +66,8 @@ def test_correct_results(self, im_type, angle, keep_size, mode, padding_mode, al
self.segn[0, 0], -np.rad2deg(angle), (0, 1), not keep_size, order=0, mode=_mode, prefilter=False
)
expected = np.stack(expected).astype(int)
if isinstance(rotated["seg"], MetaTensor):
rotated["seg"] = rotated["seg"].as_tensor() # pytorch 1.7 compatible
self.assertLessEqual(np.count_nonzero(expected != rotated["seg"][0]), 30)


Expand Down Expand Up @@ -96,6 +99,8 @@ def test_correct_results(self, im_type, angle, keep_size, mode, padding_mode, al
self.segn[0, 0], np.rad2deg(angle), (0, 2), not keep_size, order=0, mode=_mode, prefilter=False
)
expected = np.stack(expected).astype(int)
if isinstance(rotated["seg"], MetaTensor):
rotated["seg"] = rotated["seg"].as_tensor() # pytorch 1.7 compatible
self.assertLessEqual(np.count_nonzero(expected != rotated["seg"][0]), 160)


Expand Down Expand Up @@ -127,6 +132,8 @@ def test_correct_results(self, im_type, angle, keep_size, mode, padding_mode, al
self.segn[0, 0], -np.rad2deg(angle), (0, 1), not keep_size, order=0, mode=_mode, prefilter=False
)
expected = np.stack(expected).astype(int)
if isinstance(rotated["seg"], MetaTensor):
rotated["seg"] = rotated["seg"].as_tensor() # pytorch 1.7 compatible
self.assertLessEqual(np.count_nonzero(expected != rotated["seg"][0]), 160)


Expand Down