Skip to content

Commit cb34a0b

Browse files
committed
update pre- and postprocessing tests
1 parent ebf84b4 commit cb34a0b

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

tests/prediction_pipeline/test_postprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import xarray as xr
33
from bioimageio.core.resource_io.nodes import Postprocessing
4-
from bioimageio.core.prediction_pipeline._postprocessing import make_postprocessing
4+
from bioimageio.core.prediction_pipeline._combined_processing import make_postprocessing
55

66

77
def test_binarize_postprocessing():

tests/prediction_pipeline/test_preprocessing.py

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
import numpy as np
2-
import pytest
32
import xarray as xr
4-
from bioimageio.core.resource_io.nodes import Preprocessing
53

6-
from bioimageio.core.prediction_pipeline._preprocessing import make_preprocessing
4+
from bioimageio.core.prediction_pipeline._combined_processing import CombinedProcessing
75

86

97
def test_scale_linear():
10-
spec = Preprocessing(name="scale_linear", kwargs={"offset": [1, 2, 42], "gain": [1, 2, 3], "axes": "yx"})
11-
data = xr.DataArray(np.arange(6).reshape(1, 2, 3), dims=("x", "y", "c"))
8+
from bioimageio.core.prediction_pipeline._processing import ScaleLinear
9+
10+
preprocessing = ScaleLinear("data_name", offset=[1, 2, 42], gain=[1, 2, 3], axes="yx")
11+
data = xr.DataArray(np.arange(6).reshape((1, 2, 3)), dims=("x", "y", "c"))
1212
expected = xr.DataArray(np.array([[[1, 4, 48], [4, 10, 57]]]), dims=("x", "y", "c"))
13-
preprocessing = make_preprocessing([spec])
14-
result = preprocessing(data)
13+
result = preprocessing.apply(data)
1514
xr.testing.assert_allclose(expected, result)
1615

1716

1817
def test_scale_linear_no_channel():
19-
spec = Preprocessing(name="scale_linear", kwargs={"offset": 1, "gain": 2, "axes": "yx"})
18+
from bioimageio.core.prediction_pipeline._processing import ScaleLinear
19+
20+
preprocessing = ScaleLinear("data_name", offset=1, gain=2, axes="yx")
2021
data = xr.DataArray(np.arange(6).reshape(2, 3), dims=("x", "y"))
2122
expected = xr.DataArray(np.array([[1, 3, 5], [7, 9, 11]]), dims=("x", "y"))
22-
preprocessing = make_preprocessing([spec])
23-
result = preprocessing(data)
23+
result = preprocessing.apply(data)
2424
xr.testing.assert_allclose(expected, result)
2525

2626

2727
def test_zero_mean_unit_variance_preprocessing():
28-
zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs={})
28+
from bioimageio.core.prediction_pipeline._processing import ZeroMeanUnitVariance
29+
30+
preprocessing = ZeroMeanUnitVariance("data_name")
2931
data = xr.DataArray(np.arange(9).reshape(3, 3), dims=("x", "y"))
3032
expected = xr.DataArray(
3133
np.array(
@@ -37,14 +39,15 @@ def test_zero_mean_unit_variance_preprocessing():
3739
),
3840
dims=("x", "y"),
3941
)
40-
preprocessing = make_preprocessing([zero_mean_spec])
4142
result = preprocessing(data)
4243
xr.testing.assert_allclose(expected, result)
4344

4445

4546
def test_zero_mean_unit_across_axes():
46-
zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs={"axes": ("x", "y")})
47-
data = xr.DataArray(np.arange(18).reshape(2, 3, 3), dims=("c", "x", "y"))
47+
from bioimageio.core.prediction_pipeline._processing import ZeroMeanUnitVariance
48+
49+
preprocessing = ZeroMeanUnitVariance("data_name", axes=("x", "y"))
50+
data = xr.DataArray(np.arange(18).reshape((2, 3, 3)), dims=("c", "x", "y"))
4851
expected = xr.DataArray(
4952
np.array(
5053
[
@@ -55,54 +58,51 @@ def test_zero_mean_unit_across_axes():
5558
),
5659
dims=("x", "y"),
5760
)
58-
preprocessing = make_preprocessing([zero_mean_spec])
5961
result = preprocessing(data)
6062
xr.testing.assert_allclose(expected, result[dict(c=0)])
6163

6264

6365
def test_zero_mean_unit_variance_fixed():
66+
from bioimageio.core.prediction_pipeline._processing import ZeroMeanUnitVariance
67+
6468
np_data = np.arange(9).reshape(3, 3)
6569
mean = np_data.mean()
6670
std = np_data.mean()
6771
eps = 1.0e-7
68-
kwargs = {"mode": "fixed", "mean": mean, "std": std, "eps": eps}
69-
zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs=kwargs)
70-
data = xr.DataArray(np_data, dims=("x", "y"))
72+
preprocessing = ZeroMeanUnitVariance("data_name", mode="fixed", mean=mean, std=std, eps=eps)
7173

74+
data = xr.DataArray(np_data, dims=("x", "y"))
7275
expected = xr.DataArray((np_data - mean) / (std + eps), dims=("x", "y"))
73-
preprocessing = make_preprocessing([zero_mean_spec])
7476
result = preprocessing(data)
7577
xr.testing.assert_allclose(expected, result)
7678

7779

7880
def test_binarize():
79-
binarize_spec = Preprocessing(name="binarize", kwargs={"threshold": 14})
80-
data = xr.DataArray(np.arange(30).reshape(2, 3, 5), dims=("x", "y", "c"))
81+
from bioimageio.core.prediction_pipeline._processing import Binarize
82+
83+
preprocessing = Binarize("data_name", threshold=14)
84+
data = xr.DataArray(np.arange(30).reshape((2, 3, 5)), dims=("x", "y", "c"))
8185
expected = xr.zeros_like(data)
8286
expected[{"x": slice(1, None)}] = 1
83-
preprocessing = make_preprocessing([binarize_spec])
8487
result = preprocessing(data)
8588
xr.testing.assert_allclose(expected, result)
8689

8790

8891
def test_clip_preprocessing():
89-
clip_spec = Preprocessing(name="clip", kwargs={"min": 3, "max": 5})
92+
from bioimageio.core.prediction_pipeline._processing import Clip
93+
94+
preprocessing = Clip("data_name", min=3, max=5)
9095
data = xr.DataArray(np.arange(9).reshape(3, 3), dims=("x", "y"))
9196
expected = xr.DataArray(np.array([[3, 3, 3], [3, 4, 5], [5, 5, 5]]), dims=("x", "y"))
92-
preprocessing = make_preprocessing([clip_spec])
9397
result = preprocessing(data)
9498
xr.testing.assert_equal(expected, result)
9599

96100

97-
def test_unknown_preprocessing_should_raise():
98-
mypreprocessing = Preprocessing(name="mycoolpreprocessing", kwargs={"axes": ("x", "y")})
99-
with pytest.raises(NotImplementedError):
100-
make_preprocessing([mypreprocessing])
101-
102-
103101
def test_combination_of_preprocessing_steps_with_dims_specified():
104-
zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs={"axes": ("x", "y")})
105-
data = xr.DataArray(np.arange(18).reshape(2, 3, 3), dims=("c", "x", "y"))
102+
from bioimageio.core.prediction_pipeline._processing import ZeroMeanUnitVariance
103+
104+
preprocessing = ZeroMeanUnitVariance("data_name", axes=("x", "y"))
105+
data = xr.DataArray(np.arange(18).reshape((2, 3, 3)), dims=("c", "x", "y"))
106106

107107
expected = xr.DataArray(
108108
np.array(
@@ -115,51 +115,65 @@ def test_combination_of_preprocessing_steps_with_dims_specified():
115115
dims=("x", "y"),
116116
)
117117

118-
preprocessing = make_preprocessing([zero_mean_spec])
119118
result = preprocessing(data)
120119
xr.testing.assert_allclose(expected, result[dict(c=0)])
121120

122121

123122
def test_scale_range():
124-
scale_range_spec = Preprocessing(name="scale_range", kwargs={})
123+
from bioimageio.core.prediction_pipeline._processing import ScaleRange
125124

125+
preprocessing = ScaleRange("data_name")
126126
np_data = np.arange(9).reshape(3, 3).astype("float32")
127127
data = xr.DataArray(np_data, dims=("x", "y"))
128+
preprocessing.set_computed_sample_statistics(
129+
CombinedProcessing.compute_sample_statistics(
130+
{"data_name": data}, preprocessing.get_required_sample_statistics()
131+
)
132+
)
128133

129134
exp_data = (np_data - np_data.min()) / np_data.max()
130135
expected = xr.DataArray(exp_data, dims=("x", "y"))
131136

132-
preprocessing = make_preprocessing([scale_range_spec])
133137
result = preprocessing(data)
134138
xr.testing.assert_allclose(expected, result)
135139

136140

137141
def test_scale_range_axes():
142+
from bioimageio.core.prediction_pipeline._processing import ScaleRange
143+
138144
min_percentile = 1.0
139145
max_percentile = 99.0
140-
kwargs = {"axes": ("x", "y"), "min_percentile": min_percentile, "max_percentile": max_percentile}
141-
scale_range_spec = Preprocessing(name="scale_range", kwargs=kwargs)
146+
preprocessing = ScaleRange(
147+
"data_name", axes=("x", "y"), min_percentile=min_percentile, max_percentile=max_percentile
148+
)
142149

143-
np_data = np.arange(18).reshape(2, 3, 3).astype("float32")
150+
np_data = np.arange(18).reshape((2, 3, 3)).astype("float32")
144151
data = xr.DataArray(np_data, dims=("c", "x", "y"))
145152

153+
preprocessing.set_computed_sample_statistics(
154+
CombinedProcessing.compute_sample_statistics(
155+
{"data_name": data}, preprocessing.get_required_sample_statistics()
156+
)
157+
)
158+
146159
p_low = np.percentile(np_data, min_percentile, axis=(1, 2), keepdims=True)
147160
p_up = np.percentile(np_data, max_percentile, axis=(1, 2), keepdims=True)
148161
exp_data = (np_data - p_low) / p_up
149162
expected = xr.DataArray(exp_data, dims=("c", "x", "y"))
150163

151-
preprocessing = make_preprocessing([scale_range_spec])
152164
result = preprocessing(data)
153165
xr.testing.assert_allclose(expected, result)
154166

155167

156168
def test_sigmoid():
169+
from bioimageio.core.prediction_pipeline._processing import Sigmoid
170+
157171
shape = (3, 32, 32)
158172
axes = ("c", "y", "x")
159173
np_data = np.random.rand(*shape)
160174
data = xr.DataArray(np_data, dims=axes)
161175

162-
sigmoid = make_preprocessing([Preprocessing("sigmoid", kwargs={})])
176+
sigmoid = Sigmoid("data_name")
163177
res = sigmoid(data)
164178

165179
exp = xr.DataArray(1.0 / (1 + np.exp(-np_data)), dims=axes)

0 commit comments

Comments
 (0)