TestMain.test_mode1_no_slidce_inds()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 20
rs 9.55
c 0
b 0
f 0
cc 1
nop 1
1
import os
2
3
import pytest
4
5
from deepreg.vis import main, string_to_list
6
7
8
def test_string_to_list():
9
    string = "abc, 123, def, 456"
10
    got = string_to_list(string)
11
    expected = ["abc", "123", "def", "456"]
12
    assert got == expected
13
14
15
class TestMain:
16
    img_paths = "./data/test/nifti/unit_test/moving_image.nii.gz"
17
    ddf_path = "./data/test/nifti/unit_test/ddf.nii.gz"
18
19
    def test_mode0(self):
20
        # test mode 0 and check output
21
        out_path = "logs/moving_image.gif"
22
        main(
23
            args=[
24
                "--mode",
25
                0,
26
                "--image-paths",
27
                self.img_paths,
28
                "--save-path",
29
                "logs",
30
                "--interval",
31
                "50",
32
            ]
33
        )
34
        assert os.path.exists(out_path)
35
        os.remove(out_path)
36
37
    def test_mode1_output(self):
38
        # test mode 1 and check output
39
        out_path_1 = "logs/moving_image_slice_1.gif"
40
        out_path_2 = "logs/moving_image_slice_2.gif"
41
        out_path_3 = "logs/moving_image_slice_3.gif"
42
        main(
43
            args=[
44
                "--mode",
45
                "1",
46
                "--image-paths",
47
                self.img_paths,
48
                "--save-path",
49
                "logs",
50
                "--interval",
51
                "50",
52
                "--num-interval",
53
                "100",
54
                "--slice-inds",
55
                "1,2,3",
56
                "--ddf-path",
57
                self.ddf_path,
58
            ]
59
        )
60
        assert os.path.exists(out_path_1)
61
        assert os.path.exists(out_path_2)
62
        assert os.path.exists(out_path_3)
63
        os.remove(out_path_1)
64
        os.remove(out_path_2)
65
        os.remove(out_path_3)
66
67
    def test_mode1_no_slidce_inds(self):
68
        # test mode 1 and check output when no slice_inds
69
        out_path_partial = "moving_image_slice"
70
        main(
71
            args=[
72
                "--mode",
73
                "1",
74
                "--image-paths",
75
                self.img_paths,
76
                "--save-path",
77
                "logs",
78
                "--interval",
79
                "50",
80
                "--num-interval",
81
                "100",
82
                "--ddf-path",
83
                self.ddf_path,
84
            ]
85
        )
86
        assert any([out_path_partial in file for file in os.listdir("logs")])
87
88
    def test_mode1_err(self):
89
        # test mode 1 and check if exception is caught
90
        with pytest.raises(Exception) as err_info:
91
            main(
92
                args=[
93
                    "--mode",
94
                    "1",
95
                    "--image-paths",
96
                    self.img_paths,
97
                    "--save-path",
98
                    "logs",
99
                    "--interval",
100
                    "50",
101
                    "--num-interval",
102
                    "100",
103
                    "--slice-inds",
104
                    "1,2,3",
105
                ]
106
            )
107
        assert "--ddf-path is required when using --mode 1" in str(err_info.value)
108
109
    @pytest.mark.parametrize(
110
        "extra_args",
111
        [
112
            [
113
                "--slice-inds",
114
                "1,2,3",
115
                "--fname",
116
                "visualisation.png",
117
                "--col-titles",
118
                "abc",
119
            ],
120
            [
121
                "--slice-inds",
122
                "1,2,3",
123
                "--fname",
124
                "visualisation.png",
125
            ],  # no col_titles
126
            [
127
                "--col-titles",
128
                "abc",
129
            ],  # no slice_inds and no fname
130
        ],
131
    )
132
    def test_mode2_output(self, extra_args):
133
        # test mode 2 and check output
134
        common_args = [
135
            "--mode",
136
            "2",
137
            "--image-paths",
138
            self.img_paths,
139
            "--save-path",
140
            "logs",
141
        ]
142
        out_path = "logs/visualisation.png"
143
        main(args=common_args + extra_args)
144
        assert os.path.exists(out_path)
145
        os.remove(out_path)
146
147
    @pytest.mark.parametrize(
148
        "extra_args",
149
        [
150
            [
151
                "--fname",
152
                "visualisation.gif",
153
            ],
154
            [],  # no fname
155
        ],
156
    )
157
    def test_case3(self, extra_args):
158
        # test mode 3 and check output
159
        out_path = "logs/visualisation.gif"
160
        img_paths_moded = ",".join([self.img_paths] * 6)
161
        common_args = [
162
            "--mode",
163
            "3",
164
            "--image-paths",
165
            img_paths_moded,
166
            "--save-path",
167
            "logs",
168
            "--interval",
169
            "50",
170
            "--size",
171
            "2,3",
172
        ]
173
        main(
174
            args=common_args + extra_args,
175
        )
176
        assert os.path.exists(out_path)
177
        os.remove(out_path)
178
179
    def test_mode3_num_image_err(self):
180
        # test mode 3 and check if exception is caught
181
        img_paths = ",".join([self.img_paths] * 6)
182
        with pytest.raises(Exception) as err_info:
183
            main(
184
                args=[
185
                    "--mode",
186
                    "3",
187
                    "--image-paths",
188
                    img_paths,
189
                    "--save-path",
190
                    "logs",
191
                    "--interval",
192
                    "50",
193
                    "--size",
194
                    "2,1",
195
                    "--fname",
196
                    "visualisation.gif",
197
                ]
198
            )
199
        assert "The number of images supplied is " in str(err_info.value)
200
201
    def test_mode3_shape_err(self):
202
        # test mode 3 and check if exception is caught
203
        new_img_path = "data/test/nifti/paired/test/fixed_images/case000025.nii.gz"
204
        img_paths_moded = self.img_paths + "," + new_img_path
205
        with pytest.raises(Exception) as err_info:
206
            main(
207
                args=[
208
                    "--mode",
209
                    "3",
210
                    "--image-paths",
211
                    img_paths_moded,
212
                    "--save-path",
213
                    "logs",
214
                    "--interval",
215
                    "50",
216
                    "--size",
217
                    "2,1",
218
                    "--fname",
219
                    "visualisation.gif",
220
                ]
221
            )
222
        assert "all images do not have equal shapes" in str(err_info.value)
223