Skip to content

Commit d5cca7a

Browse files
committed
CI and linting fix
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent 2e8a7ab commit d5cca7a

18 files changed

Lines changed: 79 additions & 62 deletions

monai/data/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
load_decathlon_datalist,
4848
load_decathlon_properties,
4949
)
50+
from .export_utils import load_exported_program, save_exported_program
5051
from .folder_layout import FolderLayout, FolderLayoutBase
5152
from .grid_dataset import GridPatchDataset, PatchDataset, PatchIter, PatchIterd
5253
from .image_dataset import ImageDataset
@@ -77,7 +78,6 @@
7778
from .test_time_augmentation import TestTimeAugmentation
7879
from .thread_buffer import ThreadBuffer, ThreadDataLoader
7980
from .torchscript_utils import load_net_with_metadata, save_net_with_metadata
80-
from .export_utils import load_exported_program, save_exported_program
8181
from .utils import (
8282
affine_to_spacing,
8383
compute_importance_map,

monai/networks/nets/dints.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ def __init__(
376376
# Pre-compute node activation flags as Python booleans for torch.export compatibility.
377377
# NOTE: node_a must not be mutated after construction.
378378
self._node_flags: list[list[bool]] = [
379-
[bool(self.node_a[b, d]) for d in range(self.num_depths)]
380-
for b in range(self.node_a.shape[0])
379+
[bool(self.node_a[b, d]) for d in range(self.num_depths)] for b in range(self.node_a.shape[0])
381380
]
382381

383382
# define stem operations for every block
@@ -675,8 +674,7 @@ def __init__(
675674
# NOTE: arch_code_a must not be mutated after construction; this class
676675
# is only used at inference/re-training time, not during architecture search.
677676
self._active_flags: list[list[bool]] = [
678-
[bool(self.arch_code_a[b, r]) for r in range(self.arch_code_a.shape[1])]
679-
for b in range(self.num_blocks)
677+
[bool(self.arch_code_a[b, r]) for r in range(self.arch_code_a.shape[1])] for b in range(self.num_blocks)
680678
]
681679

682680
def forward(self, x: list[torch.Tensor]) -> list[torch.Tensor]:

tests/bundle/test_bundle_export_checkpoint.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
@skip_if_windows
3535
class TestExportCheckpoint(unittest.TestCase):
36-
3736
def setUp(self):
3837
self._orig_cuda_env = os.environ.get("CUDA_VISIBLE_DEVICES")
3938

@@ -61,14 +60,26 @@ def test_export(self, key_in_ckpt):
6160
save_state(src=net if key_in_ckpt == "" else {key_in_ckpt: net}, path=ckpt_file)
6261

6362
cmd = [
64-
"coverage", "run", "-m", "monai.bundle", "export_checkpoint",
65-
"network_def", "--filepath", pt2_file,
66-
"--meta_file", meta_file,
67-
"--config_file", f"['{config_file}','{def_args_file}']",
68-
"--ckpt_file", ckpt_file,
69-
"--key_in_ckpt", key_in_ckpt,
70-
"--args_file", def_args_file,
71-
"--input_shape", "[1, 1, 96, 96, 96]",
63+
"coverage",
64+
"run",
65+
"-m",
66+
"monai.bundle",
67+
"export_checkpoint",
68+
"network_def",
69+
"--filepath",
70+
pt2_file,
71+
"--meta_file",
72+
meta_file,
73+
"--config_file",
74+
f"['{config_file}','{def_args_file}']",
75+
"--ckpt_file",
76+
ckpt_file,
77+
"--key_in_ckpt",
78+
key_in_ckpt,
79+
"--args_file",
80+
def_args_file,
81+
"--input_shape",
82+
"[1, 1, 96, 96, 96]",
7283
]
7384
command_line_tests(cmd)
7485
self.assertTrue(os.path.exists(pt2_file))
@@ -96,11 +107,19 @@ def test_default_value(self, key_in_ckpt):
96107

97108
# check with default value
98109
cmd = [
99-
"coverage", "run", "-m", "monai.bundle", "export_checkpoint",
100-
"--key_in_ckpt", key_in_ckpt,
101-
"--config_file", config_file,
102-
"--bundle_root", tempdir,
103-
"--input_shape", "[1, 1, 96, 96, 96]",
110+
"coverage",
111+
"run",
112+
"-m",
113+
"monai.bundle",
114+
"export_checkpoint",
115+
"--key_in_ckpt",
116+
key_in_ckpt,
117+
"--config_file",
118+
config_file,
119+
"--bundle_root",
120+
tempdir,
121+
"--input_shape",
122+
"[1, 1, 96, 96, 96]",
104123
]
105124
command_line_tests(cmd)
106125
self.assertTrue(os.path.exists(pt2_file))

tests/data/test_export_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def forward(self, x):
3030

3131

3232
class TestExportUtils(unittest.TestCase):
33-
3433
def test_save_exported_program(self):
3534
"""Save an exported program without metadata to a file."""
3635
ep = torch.export.export(TestModule(), args=(torch.tensor(1.0),))

tests/losses/test_generalized_wasserstein_dice_loss.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import torch.optim as optim
2121

2222
from monai.losses import GeneralizedWassersteinDiceLoss
23-
from tests.test_utils import test_export_save
23+
from tests.test_utils import test_script_save
2424

2525

2626
class TestGeneralizedWassersteinDiceLoss(unittest.TestCase):
@@ -303,7 +303,7 @@ def test_batch_size_different_samples(self):
303303
float(loss_batch[1]), loss_b, places=5, msg=f"Batch loss[1] != loss_b for weighting_mode={w_mode}"
304304
)
305305

306-
def test_export(self):
306+
def test_script(self):
307307
target = torch.tensor([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]])
308308

309309
# add another dimension corresponding to the batch (batch size = 1 here)
@@ -312,7 +312,7 @@ def test_export(self):
312312

313313
loss = GeneralizedWassersteinDiceLoss(dist_matrix=np.array([[0.0, 1.0], [1.0, 0.0]]), weighting_mode="default")
314314

315-
test_export_save(loss, pred_very_good, target)
315+
test_script_save(loss, pred_very_good, target)
316316

317317

318318
if __name__ == "__main__":

tests/losses/test_masked_loss.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from monai.losses.dice import DiceFocalLoss, DiceLoss
2020
from monai.losses.spatial_mask import MaskedLoss
2121
from monai.utils import set_determinism
22-
from tests.test_utils import test_export_save
22+
from tests.test_utils import test_script_save
2323

2424
device = "cuda" if torch.cuda.is_available() else "cpu"
2525

@@ -73,14 +73,14 @@ def test_ill_opts(self):
7373
masked = MaskedLoss(loss=dice_loss)
7474
masked(input=torch.zeros((3, 3, 2, 2)), target=torch.zeros((3, 2, 2, 2)), mask=torch.zeros((3, 3, 2, 2)))
7575

76-
def test_export(self):
76+
def test_script(self):
7777
input_param, expected_val = TEST_CASES[0]
7878
size = [3, 3, 5, 5]
7979
label = torch.randint(low=0, high=2, size=size)
8080
label = torch.argmax(label, dim=1, keepdim=True)
8181
pred = torch.randn(size)
8282
loss = MaskedLoss(**input_param)
83-
test_export_save(loss, pred, label)
83+
test_script_save(loss, pred, label)
8484

8585

8686
if __name__ == "__main__":

tests/networks/blocks/test_fpn_block.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from monai.networks.blocks.feature_pyramid_network import FeaturePyramidNetwork
2222
from monai.networks.nets.resnet import resnet50
2323
from monai.utils import optional_import
24-
from tests.test_utils import test_export_save
24+
from tests.test_utils import test_script_save
2525

2626
_, has_torchvision = optional_import("torchvision")
2727

@@ -55,13 +55,13 @@ def test_fpn_block(self, input_param, input_shape, expected_shape):
5555
self.assertEqual(result["feat1"].shape, expected_shape[1])
5656

5757
@parameterized.expand(TEST_CASES)
58-
def test_export(self, input_param, input_shape, expected_shape):
59-
# test whether support torch.export
58+
def test_script(self, input_param, input_shape, expected_shape):
59+
# test whether support torchscript
6060
net = FeaturePyramidNetwork(**input_param)
6161
data = OrderedDict()
6262
data["feat0"] = torch.rand(input_shape[0])
6363
data["feat1"] = torch.rand(input_shape[1])
64-
test_export_save(net, data)
64+
test_script_save(net, data)
6565

6666

6767
@unittest.skipUnless(has_torchvision, "Requires torchvision")
@@ -75,11 +75,11 @@ def test_fpn(self, input_param, input_shape, expected_shape):
7575
self.assertEqual(result["pool"].shape, expected_shape[1])
7676

7777
@parameterized.expand(TEST_CASES2)
78-
def test_export(self, input_param, input_shape, expected_shape):
79-
# test whether support torch.export
78+
def test_script(self, input_param, input_shape, expected_shape):
79+
# test whether support torchscript
8080
net = _resnet_fpn_extractor(backbone=resnet50(), spatial_dims=input_param["spatial_dims"], returned_layers=[1])
8181
data = torch.rand(input_shape)
82-
test_export_save(net, data)
82+
test_script_save(net, data)
8383

8484

8585
if __name__ == "__main__":

tests/networks/nets/regunet/test_localnet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from monai.networks import eval_mode
2020
from monai.networks.nets.regunet import LocalNet
21-
from tests.test_utils import test_export_save
21+
from tests.test_utils import test_script_save
2222

2323
device = "cuda" if torch.cuda.is_available() else "cpu"
2424

@@ -75,11 +75,11 @@ def test_extract_levels(self, input_param, input_shape, expected_shape):
7575
self.assertEqual(len(net.decode_deconvs), len(input_param["extract_levels"]) - 1)
7676
self.assertEqual(len(net.decode_convs), len(input_param["extract_levels"]) - 1)
7777

78-
def test_export(self):
78+
def test_script(self):
7979
input_param, input_shape, _ = TEST_CASE_LOCALNET_2D[0]
8080
net = LocalNet(**input_param)
8181
test_data = torch.randn(input_shape)
82-
test_export_save(net, test_data)
82+
test_script_save(net, test_data)
8383

8484

8585
if __name__ == "__main__":

tests/networks/nets/regunet/test_regunet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from monai.networks import eval_mode
2020
from monai.networks.nets.regunet import RegUNet
21-
from tests.test_utils import test_export_save
21+
from tests.test_utils import test_script_save
2222

2323
device = "cuda" if torch.cuda.is_available() else "cpu"
2424

@@ -77,11 +77,11 @@ def test_ill_shape(self):
7777
net = RegUNet(**input_param).to(device)
7878
net.forward(torch.randn(input_shape).to(device))
7979

80-
def test_export(self):
80+
def test_script(self):
8181
input_param, input_shape, _ = TEST_CASE_REGUNET_2D[0]
8282
net = RegUNet(**input_param)
8383
test_data = torch.randn(input_shape)
84-
test_export_save(net, test_data)
84+
test_script_save(net, test_data)
8585

8686

8787
if __name__ == "__main__":

tests/networks/nets/test_ahnet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from monai.networks import eval_mode
2020
from monai.networks.blocks import FCN, MCFCN
2121
from monai.networks.nets import AHNet
22-
from tests.test_utils import skip_if_quick, test_pretrained_networks, test_export_save
22+
from tests.test_utils import skip_if_quick, test_export_save, test_pretrained_networks
2323

2424
device = "cuda" if torch.cuda.is_available() else "cpu"
2525

0 commit comments

Comments
 (0)