Skip to content

Commit bb85f53

Browse files
committed
Fix path issues
1 parent 58c461c commit bb85f53

File tree

9 files changed

+89
-114
lines changed

9 files changed

+89
-114
lines changed

tests/apps/pathology/test_sliding_window_hovernet_inference.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,21 @@ def compute(self, data):
121121
sigma_scale=1.0,
122122
)
123123

124-
expected = np.array([
124+
expected = np.array(
125125
[
126126
[
127-
[3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
128-
[3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
129-
[3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333],
130-
[3.6667, 3.6667, 3.6667, 3.6667, 3.6667, 3.6667, 3.6667],
131-
[4.3333, 4.3333, 4.3333, 4.3333, 4.3333, 4.3333, 4.3333],
132-
[4.5000, 4.5000, 4.5000, 4.5000, 4.5000, 4.5000, 4.5000],
133-
[5.0000, 5.0000, 5.0000, 5.0000, 5.0000, 5.0000, 5.0000],
127+
[
128+
[3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
129+
[3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
130+
[3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333],
131+
[3.6667, 3.6667, 3.6667, 3.6667, 3.6667, 3.6667, 3.6667],
132+
[4.3333, 4.3333, 4.3333, 4.3333, 4.3333, 4.3333, 4.3333],
133+
[4.5000, 4.5000, 4.5000, 4.5000, 4.5000, 4.5000, 4.5000],
134+
[5.0000, 5.0000, 5.0000, 5.0000, 5.0000, 5.0000, 5.0000],
135+
]
134136
]
135137
]
136-
])
138+
)
137139
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
138140
result = sliding_window_inference(
139141
inputs,
@@ -147,29 +149,31 @@ def compute(self, data):
147149
sigma_scale=1.0,
148150
progress=has_tqdm,
149151
)
150-
expected = np.array([
152+
expected = np.array(
151153
[
152154
[
153-
[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0],
154-
[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0],
155-
[3.3271625, 3.3271623, 3.3271623, 3.3271623, 3.3271623, 3.3271623, 3.3271625],
156-
[3.6728377, 3.6728377, 3.6728377, 3.6728377, 3.6728377, 3.6728377, 3.6728377],
157-
[4.3271623, 4.3271623, 4.3271627, 4.3271627, 4.3271627, 4.3271623, 4.3271623],
158-
[4.513757, 4.513757, 4.513757, 4.513757, 4.513757, 4.513757, 4.513757],
159-
[4.9999995, 5.0, 5.0, 5.0, 5.0, 5.0, 4.9999995],
155+
[
156+
[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0],
157+
[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0],
158+
[3.3271625, 3.3271623, 3.3271623, 3.3271623, 3.3271623, 3.3271623, 3.3271625],
159+
[3.6728377, 3.6728377, 3.6728377, 3.6728377, 3.6728377, 3.6728377, 3.6728377],
160+
[4.3271623, 4.3271623, 4.3271627, 4.3271627, 4.3271627, 4.3271623, 4.3271623],
161+
[4.513757, 4.513757, 4.513757, 4.513757, 4.513757, 4.513757, 4.513757],
162+
[4.9999995, 5.0, 5.0, 5.0, 5.0, 5.0, 4.9999995],
163+
]
160164
]
161165
]
162-
])
166+
)
163167
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
164168

165169
result = SlidingWindowHoVerNetInferer(roi_shape, sw_batch_size, overlap=0.5, mode="gaussian", sigma_scale=1.0)(
166170
inputs, _Pred().compute
167171
)
168172
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
169173

170-
result = SlidingWindowHoVerNetInferer(roi_shape, sw_batch_size, overlap=0.5, mode="gaussian", sigma_scale=[1.0, 1.0])(
171-
inputs, _Pred().compute
172-
)
174+
result = SlidingWindowHoVerNetInferer(
175+
roi_shape, sw_batch_size, overlap=0.5, mode="gaussian", sigma_scale=[1.0, 1.0]
176+
)(inputs, _Pred().compute)
173177
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
174178

175179
result = SlidingWindowHoVerNetInferer(
@@ -200,7 +204,9 @@ def compute(data):
200204
expected = np.ones((1, 1, 3, 3)) * -6.0
201205
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
202206

203-
result = SlidingWindowHoVerNetInferer(roi_shape, sw_batch_size, overlap=0.5, mode="constant", cval=-1)(inputs, compute)
207+
result = SlidingWindowHoVerNetInferer(roi_shape, sw_batch_size, overlap=0.5, mode="constant", cval=-1)(
208+
inputs, compute
209+
)
204210
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
205211

206212
def test_args_kwargs(self):
@@ -238,9 +244,9 @@ def compute(data, test1, test2):
238244
expected = np.ones((1, 1, 3, 3)) + 2.0
239245
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
240246

241-
result = SlidingWindowHoVerNetInferer(roi_shape, sw_batch_size, overlap=0.5, mode="constant", cval=-1, progress=has_tqdm)(
242-
inputs, compute, t1, test2=t2
243-
)
247+
result = SlidingWindowHoVerNetInferer(
248+
roi_shape, sw_batch_size, overlap=0.5, mode="constant", cval=-1, progress=has_tqdm
249+
)(inputs, compute, t1, test2=t2)
244250
np.testing.assert_allclose(result.cpu().numpy(), expected, rtol=1e-4)
245251

246252
@parameterized.expand(TEST_CASES_MULTIOUTPUT)
@@ -280,9 +286,9 @@ def compute_dict(data):
280286
for rr, _ in zip(result_dict, expected_dict):
281287
np.testing.assert_allclose(result_dict[rr].cpu().numpy(), expected_dict[rr], rtol=1e-4)
282288

283-
result = SlidingWindowHoVerNetInferer(roi_shape, sw_batch_size, overlap=0.5, mode="constant", cval=-1, progress=has_tqdm)(
284-
inputs, compute
285-
)
289+
result = SlidingWindowHoVerNetInferer(
290+
roi_shape, sw_batch_size, overlap=0.5, mode="constant", cval=-1, progress=has_tqdm
291+
)(inputs, compute)
286292
for rr, ee in zip(result, expected):
287293
np.testing.assert_allclose(rr.cpu().numpy(), ee, rtol=1e-4)
288294

tests/bundle/test_bundle_ckpt_export.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ def test_export(self, key_in_ckpt, use_trace):
7575
command_line_tests(full_cmd)
7676
self.assertTrue(os.path.exists(self.ts_file))
7777

78-
_, metadata, extra_files = load_net_with_metadata(self.ts_file, more_extra_files=["inference.json", "def_args.json"])
78+
_, metadata, extra_files = load_net_with_metadata(
79+
self.ts_file, more_extra_files=["inference.json", "def_args.json"]
80+
)
7981
self.assertIn("schema", metadata)
8082
self.assertIn("meta_file", json.loads(extra_files["def_args.json"]))
8183
self.assertIn("network_def", json.loads(extra_files["inference.json"]))

tests/bundle/test_bundle_workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
from __future__ import annotations
1313

14-
from pathlib import Path
1514
import os
1615
import shutil
1716
import sys
1817
import tempfile
1918
import unittest
2019
from copy import deepcopy
20+
from pathlib import Path
2121

2222
import nibabel as nib
2323
import numpy as np

tests/bundle/test_config_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ def test_parse_json_warn(self, config_string, extension, expected_unique_val, ex
373373

374374
@parameterized.expand([TEST_CASE_MERGE_JSON, TEST_CASE_MERGE_YAML])
375375
@skipUnless(has_yaml, "Requires pyyaml")
376-
def test_load_configs(self, config_string, config_string2, extension, expected_overridden_val, expected_merged_vals):
376+
def test_load_configs(
377+
self, config_string, config_string2, extension, expected_overridden_val, expected_merged_vals
378+
):
377379
with tempfile.TemporaryDirectory() as tempdir:
378380
config_path1 = Path(tempdir) / f"config1.{extension}"
379381
config_path2 = Path(tempdir) / f"config2.{extension}"

tests/data/test_dataset.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ def test_shape(self, expected_shape):
5252
"extra": os.path.join(tempdir, "test_extra2.nii.gz"),
5353
},
5454
]
55-
test_transform = Compose([
56-
LoadImaged(keys=["image", "label", "extra"]),
57-
SimulateDelayd(keys=["image", "label", "extra"], delay_time=[1e-7, 1e-6, 1e-5]),
58-
])
55+
test_transform = Compose(
56+
[
57+
LoadImaged(keys=["image", "label", "extra"]),
58+
SimulateDelayd(keys=["image", "label", "extra"], delay_time=[1e-7, 1e-6, 1e-5]),
59+
]
60+
)
5961
dataset = Dataset(data=test_data, transform=test_transform)
6062
data1 = dataset[0]
6163
data2 = dataset[1]

tests/data/test_threadcontainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from __future__ import annotations
1313

1414
import os
15-
from pathlib import Path
1615
import tempfile
1716
import time
1817
import unittest
18+
from pathlib import Path
1919

2020
import torch
2121

tests/handlers/test_handler_prob_map_producer.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131

3232

3333
class TestDataset(Dataset):
34-
__test__ = (
35-
False # indicate to pytest that this class is not intended for collection
36-
)
34+
__test__ = False # indicate to pytest that this class is not intended for collection
3735

3836
def __init__(self, name, size):
3937
super().__init__(
@@ -68,9 +66,7 @@ def __getitem__(self, index):
6866

6967

7068
class TestEvaluator(Evaluator):
71-
__test__ = (
72-
False # indicate to pytest that this class is not intended for collection
73-
)
69+
__test__ = False # indicate to pytest that this class is not intended for collection
7470

7571
def _iteration(self, engine, batchdata):
7672
return batchdata
@@ -96,10 +92,7 @@ def inference(engine, batch):
9692
prob_map_gen = ProbMapProducer(output_dir=output_dir)
9793

9894
evaluator = TestEvaluator(
99-
torch.device("cpu:0"),
100-
data_loader,
101-
np.ceil(size / batch_size),
102-
val_handlers=[prob_map_gen],
95+
torch.device("cpu:0"), data_loader, np.ceil(size / batch_size), val_handlers=[prob_map_gen]
10396
)
10497

10598
# set up validation handler
@@ -110,12 +103,8 @@ def inference(engine, batch):
110103
engine.run(data_loader)
111104

112105
prob_map = np.load(os.path.join(output_dir, name + ".npy"))
113-
self.assertListEqual(
114-
np.vstack(prob_map.nonzero()).T.tolist(), [[i, i + 1] for i in range(size)]
115-
)
116-
self.assertListEqual(
117-
prob_map[prob_map.nonzero()].tolist(), [i + 1 for i in range(size)]
118-
)
106+
self.assertListEqual(np.vstack(prob_map.nonzero()).T.tolist(), [[i, i + 1] for i in range(size)])
107+
self.assertListEqual(prob_map[prob_map.nonzero()].tolist(), [i + 1 for i in range(size)])
119108

120109

121110
if __name__ == "__main__":

tests/transforms/test_resize_with_pad_or_cropd.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ class TestResizeWithPadOrCropd(unittest.TestCase):
4949
@parameterized.expand(TEST_CASES)
5050
def test_pad_shape(self, input_param, input_data, expected_val):
5151
for p in TEST_NDARRAYS_ALL:
52-
if isinstance(p(0), torch.Tensor) and ("constant_values" in input_param or input_param["mode"] == "reflect"):
52+
if isinstance(p(0), torch.Tensor) and (
53+
"constant_values" in input_param or input_param["mode"] == "reflect"
54+
):
5355
continue
5456
padcropper = ResizeWithPadOrCropd(**input_param)
5557
input_data_ = deepcopy(input_data)

tests/visualize/utils/test_matshow3d.py

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from __future__ import annotations
1313

1414
import os
15-
from pathlib import Path
1615
import tempfile
1716
import unittest
17+
from pathlib import Path
1818

1919
import numpy as np
2020

@@ -41,95 +41,71 @@ def test_3d(self):
4141
testing_dir = os.path.join(test_root, "testing_data")
4242
print("test_root: ", testing_dir)
4343
keys = "image"
44-
xforms = Compose([
45-
LoadImaged(keys=keys),
46-
EnsureChannelFirstd(keys=keys, channel_dim="no_channel"),
47-
ScaleIntensityd(keys=keys),
48-
])
44+
xforms = Compose(
45+
[
46+
LoadImaged(keys=keys),
47+
EnsureChannelFirstd(keys=keys, channel_dim="no_channel"),
48+
ScaleIntensityd(keys=keys),
49+
]
50+
)
4951
image_path = os.path.join(testing_dir, "anatomical.nii")
5052
ims = xforms({keys: image_path})
5153

5254
fig = pyplot.figure() # external figure
53-
fig, _ = matshow3d(
54-
ims[keys],
55-
fig=fig,
56-
figsize=(2, 2),
57-
frames_per_row=5,
58-
every_n=2,
59-
frame_dim=-1,
60-
show=False,
61-
)
55+
fig, _ = matshow3d(ims[keys], fig=fig, figsize=(2, 2), frames_per_row=5, every_n=2, frame_dim=-1, show=False)
6256

6357
with tempfile.TemporaryDirectory() as tempdir:
6458
tempimg = f"{tempdir}/matshow3d_test.png"
6559
fig.savefig(tempimg)
6660
comp = compare_images(f"{testing_dir}/matshow3d_test.png", tempimg, 5e-2)
67-
self.assertIsNone(
68-
comp, f"value of comp={comp}"
69-
) # None indicates test passed
61+
self.assertIsNone(comp, f"value of comp={comp}") # None indicates test passed
7062

7163
_, axes = pyplot.subplots()
72-
matshow3d(
73-
ims[keys],
74-
fig=axes,
75-
figsize=(2, 2),
76-
frames_per_row=5,
77-
every_n=2,
78-
frame_dim=-1,
79-
show=False,
80-
)
64+
matshow3d(ims[keys], fig=axes, figsize=(2, 2), frames_per_row=5, every_n=2, frame_dim=-1, show=False)
8165

8266
def test_samples(self):
8367
test_root = Path(__file__).parents[2]
8468
testing_dir = os.path.join(test_root, "testing_data")
8569
keys = "image"
86-
xforms = Compose([
87-
LoadImaged(keys=keys),
88-
EnsureChannelFirstd(keys=keys),
89-
ScaleIntensityd(keys=keys),
90-
RandSpatialCropSamplesd(keys=keys, roi_size=(8, 8, 5), random_size=True, num_samples=10),
91-
])
70+
xforms = Compose(
71+
[
72+
LoadImaged(keys=keys),
73+
EnsureChannelFirstd(keys=keys),
74+
ScaleIntensityd(keys=keys),
75+
RandSpatialCropSamplesd(keys=keys, roi_size=(8, 8, 5), random_size=True, num_samples=10),
76+
]
77+
)
9278
image_path = os.path.join(testing_dir, "anatomical.nii")
9379
xforms.set_random_state(0)
9480
ims = xforms({keys: image_path})
9581
fig, mat = matshow3d(
96-
[im[keys] for im in ims],
97-
title=f"testing {keys}",
98-
figsize=(2, 2),
99-
frames_per_row=5,
100-
every_n=2,
101-
show=False,
82+
[im[keys] for im in ims], title=f"testing {keys}", figsize=(2, 2), frames_per_row=5, every_n=2, show=False
10283
)
10384
self.assertEqual(mat.dtype, np.float32)
10485

10586
with tempfile.TemporaryDirectory() as tempdir:
10687
tempimg = f"{tempdir}/matshow3d_patch_test.png"
10788
fig.savefig(tempimg)
108-
comp = compare_images(
109-
f"{testing_dir}/matshow3d_patch_test.png",
110-
tempimg,
111-
5e-2,
112-
in_decorator=True,
113-
)
89+
comp = compare_images(f"{testing_dir}/matshow3d_patch_test.png", tempimg, 5e-2, in_decorator=True)
11490
if comp:
11591
print("not none comp: ", comp) # matplotlib 3.2.2
11692
np.testing.assert_allclose(comp["rms"], 30.786983, atol=1e-3, rtol=1e-3)
11793
else:
118-
self.assertIsNone(
119-
comp, f"value of comp={comp}"
120-
) # None indicates test passed
94+
self.assertIsNone(comp, f"value of comp={comp}") # None indicates test passed
12195

12296
def test_3d_rgb(self):
12397
test_dir = Path(__file__).parents[2].as_posix()
12498
testing_dir = os.path.join(test_dir, "testing_data")
12599
keys = "image"
126-
xforms = Compose([
127-
LoadImaged(keys=keys),
128-
EnsureChannelFirstd(keys=keys, channel_dim="no_channel"),
129-
ScaleIntensityd(keys=keys),
130-
# change to RGB color image
131-
RepeatChanneld(keys=keys, repeats=3),
132-
])
100+
xforms = Compose(
101+
[
102+
LoadImaged(keys=keys),
103+
EnsureChannelFirstd(keys=keys, channel_dim="no_channel"),
104+
ScaleIntensityd(keys=keys),
105+
# change to RGB color image
106+
RepeatChanneld(keys=keys, repeats=3),
107+
]
108+
)
133109
image_path = os.path.join(testing_dir, "anatomical.nii")
134110
ims = xforms({keys: image_path})
135111

@@ -149,12 +125,8 @@ def test_3d_rgb(self):
149125
with tempfile.TemporaryDirectory() as tempdir:
150126
tempimg = f"{tempdir}/matshow3d_rgb_test.png"
151127
fig.savefig(tempimg)
152-
comp = compare_images(
153-
f"{testing_dir}/matshow3d_rgb_test.png", tempimg, 5e-2
154-
)
155-
self.assertIsNone(
156-
comp, f"value of comp={comp}"
157-
) # None indicates test passed
128+
comp = compare_images(f"{testing_dir}/matshow3d_rgb_test.png", tempimg, 5e-2)
129+
self.assertIsNone(comp, f"value of comp={comp}") # None indicates test passed
158130

159131

160132
if __name__ == "__main__":

0 commit comments

Comments
 (0)