Passed
Pull Request — main (#662)
by Yunguan
03:31
created

TestBuildLoss.test_image_loss()   A

Complexity

Conditions 4

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 32
rs 9.304
c 0
b 0
f 0
cc 4
nop 4
1
# coding=utf-8
2
3
"""
4
Tests for deepreg/_model/network/ddf_dvf.py
5
"""
6
import itertools
7
from copy import deepcopy
8
from unittest.mock import MagicMock, patch
9
10
import pytest
11
12
from deepreg.model.network import RegistrationModel
13
from deepreg.registry import REGISTRY
14
15
moving_image_size = (1, 3, 5)
16
fixed_image_size = (2, 4, 6)
17
index_size = 2
18
batch_size = 3
19
backbone_args = {
20
    "local": {"extract_levels": [1, 2]},
21
    "global": {"extract_levels": [1, 2]},
22
    "unet": {"depth": 2},
23
}
24
config = {
25
    "backbone": {
26
        "num_channel_initial": 4,
27
    },
28
    "loss": {
29
        "image": {"name": "lncc", "weight": 0.1},
30
        "label": {
31
            "name": "dice",
32
            "weight": 1,
33
            "scales": [0, 1],
34
        },
35
        "regularization": {"weight": 0.1, "name": "bending"},
36
    },
37
}
38
39
config_multiple_losses = deepcopy(config)
40
config_multiple_losses["loss"]["image"] = [
41
    {"name": "lncc", "weight": 0.1},
42
    {"name": "ssd", "weight": 0.1},
43
    {"name": "gmi", "weight": 0.1},
44
]
45
46
47
@pytest.fixture
48
def model(method: str, labeled: bool, backbone: str) -> RegistrationModel:
49
    """
50
    A specific registration model object.
51
52
    :param method: name of method
53
    :param labeled: whether the data is labeled
54
    :param backbone: name of backbone
55
    :return: the built object
56
    """
57
    copied = deepcopy(config)
58
    copied["method"] = method
59
    copied["backbone"]["name"] = backbone
60
    copied["backbone"] = {**backbone_args[backbone], **copied["backbone"]}
61
    return REGISTRY.build_model(
62
        config=dict(
63
            name=method,  # TODO we store method twice
64
            moving_image_size=moving_image_size,
65
            fixed_image_size=fixed_image_size,
66
            index_size=index_size,
67
            labeled=labeled,
68
            batch_size=batch_size,
69
            config=copied,
70
        )
71
    )
72
73
74
def pytest_generate_tests(metafunc):
0 ignored issues
show
introduced by
"metafunc" missing in parameter type documentation
Loading history...
introduced by
Redundant returns documentation
Loading history...
75
    """
76
    Test parameter generator.
77
78
    This function is called once per each test function.
79
    It takes the attribute `params` from the test class,
80
    and then use the same `params` for all tests inside the class.
81
    This is specific for test of registration models only.
82
83
    This is modified from the pytest documentation,
84
    where their version defined the params for each test function separately.
85
86
    https://docs.pytest.org/en/stable/example/parametrize.html#parametrizing-test-methods-through-per-class-configuration
87
88
    :param metafunc:
89
    :return:
90
    """
91
    #
92
    funcarglist = metafunc.cls.params
93
    argnames = sorted(funcarglist[0])
94
    metafunc.parametrize(
95
        argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist]
96
    )
97
98
99
class TestRegistrationModel:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
100
    params = [dict(labeled=True), dict(labeled=False)]
101
102
    @pytest.fixture
103
    def empty_model(self, labeled: bool) -> RegistrationModel:
104
        """
105
        A RegistrationModel with build_model and build_loss mocked/overwritten.
106
107
        :param labeled: whether the data is labeled
108
        :return: the mocked object
109
        """
110
        with patch.multiple(
111
            RegistrationModel,
112
            build_model=MagicMock(return_value=None),
113
            build_loss=MagicMock(return_value=None),
114
        ):
115
            return RegistrationModel(
116
                moving_image_size=moving_image_size,
117
                fixed_image_size=fixed_image_size,
118
                index_size=index_size,
119
                labeled=labeled,
120
                batch_size=batch_size,
121
                config=dict(),
122
            )
123
124
    def test_get_config(self, empty_model, labeled):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
125
        got = empty_model.get_config()
126
        assert got == dict(
127
            moving_image_size=moving_image_size,
128
            fixed_image_size=fixed_image_size,
129
            index_size=index_size,
130
            labeled=labeled,
131
            batch_size=batch_size,
132
            config=dict(),
133
            num_devices=1,
134
            name="RegistrationModel",
135
        )
136
137
    def test_build_inputs(self, empty_model, labeled):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
138
        inputs = empty_model.build_inputs()
139
        expected_inputs_len = 5 if labeled else 3
140
        assert len(inputs) == expected_inputs_len
141
142
        moving_image = inputs["moving_image"]
143
        fixed_image = inputs["fixed_image"]
144
        indices = inputs["indices"]
145
        assert moving_image.shape == (batch_size, *moving_image_size)
146
        assert fixed_image.shape == (batch_size, *fixed_image_size)
147
        assert indices.shape == (batch_size, index_size)
148
149
        if labeled:
150
            moving_label = inputs["moving_label"]
151
            fixed_label = inputs["fixed_label"]
152
            assert moving_label.shape == (batch_size, *moving_image_size)
153
            assert fixed_label.shape == (batch_size, *fixed_image_size)
154
155
    def test_concat_images(self, empty_model, labeled):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
156
        inputs = empty_model.build_inputs()
157
        moving_image = inputs["moving_image"]
158
        fixed_image = inputs["fixed_image"]
159
        if labeled:
160
            moving_label = inputs["moving_label"]
161
            images = empty_model.concat_images(moving_image, fixed_image, moving_label)
162
            assert images.shape == (batch_size, *fixed_image_size, 3)
163
        else:
164
            images = empty_model.concat_images(moving_image, fixed_image)
165
            assert images.shape == (batch_size, *fixed_image_size, 2)
166
167
168
class TestBuildLoss:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
169
    params = [
170
        dict(config=config, option=0, expected=2),
171
        dict(config=config, option=1, expected=2),
172
        dict(config=config, option=2, expected=3),
173
        dict(config=config_multiple_losses, option=3, expected=5),
174
    ]
175
176
    def test_image_loss(self, config: dict, option: int, expected: int):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
config is re-defining a name which is already available in the outer-scope (previously defined on line 24).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
177
        method = "ddf"
178
        backbone = "local"
179
        labeled = True
180
        copied = deepcopy(config)
181
        copied["method"] = method
182
        copied["backbone"]["name"] = backbone
183
        copied["backbone"] = {**backbone_args[backbone], **copied["backbone"]}
184
185
        if option == 0:
186
            # remove image loss config, so loss is not used
187
            copied["loss"].pop("image")
188
        elif option == 1:
189
            # set image loss weight to zero, so loss is not used
190
            copied["loss"]["image"]["weight"] = 0.0
191
        elif option == 2:
192
            # remove image loss weight, so loss is used with default weight 1
193
            copied["loss"]["image"].pop("weight")
194
195
        ddf_model = REGISTRY.build_model(
196
            config=dict(
197
                name=method,  # TODO we store method twice
198
                moving_image_size=moving_image_size,
199
                fixed_image_size=fixed_image_size,
200
                index_size=index_size,
201
                labeled=labeled,
202
                batch_size=batch_size,
203
                config=copied,
204
            )
205
        )
206
207
        assert len(ddf_model._model.losses) == expected
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _model was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
208
209
210
class TestDDFModel:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
211
    params = [
212
        dict(method=method, labeled=labeled, backbone=backbone)
213
        for method, labeled, backbone in itertools.product(
214
            ["ddf"], [True, False], ["local", "global", "unet"]
215
        )
216
    ]
217
218 View Code Duplication
    def test_build_model(self, model, labeled, backbone):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
219
        expected_outputs_len = 3 if labeled else 2
220
        if backbone == "global":
221
            expected_outputs_len += 1
222
            theta = model._outputs["theta"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
223
            assert theta.shape == (batch_size, 4, 3)
224
        assert len(model._outputs) == expected_outputs_len
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
225
226
        ddf = model._outputs["ddf"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
227
        pred_fixed_image = model._outputs["pred_fixed_image"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
228
        assert ddf.shape == (batch_size, *fixed_image_size, 3)
229
        assert pred_fixed_image.shape == (batch_size, *fixed_image_size)
230
231
        if labeled:
232
            pred_fixed_label = model._outputs["pred_fixed_label"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
233
            assert pred_fixed_label.shape == (batch_size, *fixed_image_size)
234
235
    def test_build_loss(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
236
        expected = 3 if labeled else 2
237
        assert len(model._model.losses) == expected
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _model was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
238
239
    def test_postprocess(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
240
        indices, processed = model.postprocess(
241
            inputs=model._inputs, outputs=model._outputs
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _inputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
242
        )
243
        assert indices.shape == (batch_size, index_size)
244
        expected = 7 if labeled else 4
245
        if backbone == "global":
246
            expected += 1
247
        assert len(processed) == expected
248
249
250
class TestDVFModel:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
251
    params = [
252
        dict(method=method, labeled=labeled, backbone=backbone)
253
        for method, labeled, backbone in itertools.product(
254
            ["dvf"], [True, False], ["local", "unet"]
255
        )
256
    ]
257
258 View Code Duplication
    def test_build_model(self, model, labeled, backbone):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
259
        expected_outputs_len = 4 if labeled else 3
260
        assert len(model._outputs) == expected_outputs_len
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
261
262
        dvf = model._outputs["dvf"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
263
        ddf = model._outputs["ddf"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
264
        pred_fixed_image = model._outputs["pred_fixed_image"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
265
        assert dvf.shape == (batch_size, *fixed_image_size, 3)
266
        assert ddf.shape == (batch_size, *fixed_image_size, 3)
267
        assert pred_fixed_image.shape == (batch_size, *fixed_image_size)
268
269
        if labeled:
270
            pred_fixed_label = model._outputs["pred_fixed_label"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
271
            assert pred_fixed_label.shape == (batch_size, *fixed_image_size)
272
273
    def test_build_loss(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
274
        expected = 3 if labeled else 2
275
        assert len(model._model.losses) == expected
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _model was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
276
277
    def test_postprocess(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
278
        indices, processed = model.postprocess(
279
            inputs=model._inputs, outputs=model._outputs
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _inputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
280
        )
281
        assert indices.shape == (batch_size, index_size)
282
        expected = 8 if labeled else 5
283
        assert len(processed) == expected
284
285
286
class TestConditionalModel:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
287
    params = [
288
        dict(method=method, labeled=labeled, backbone=backbone)
289
        for method, labeled, backbone in itertools.product(
290
            ["conditional"], [True], ["local", "unet"]
291
        )
292
    ]
293
294
    def test_build_model(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument labeled seems to be unused.
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
295
        assert len(model._outputs) == 1
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
296
        pred_fixed_label = model._outputs["pred_fixed_label"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
297
        assert pred_fixed_label.shape == (batch_size, *fixed_image_size)
298
299
    def test_build_loss(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument labeled seems to be unused.
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
300
        assert len(model._model.losses) == 1
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _model was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
301
302
    def test_postprocess(self, model, labeled, backbone):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Comprehensibility Bug introduced by
model is re-defining a name which is already available in the outer-scope (previously defined on line 48).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The argument labeled seems to be unused.
Loading history...
Unused Code introduced by
The argument backbone seems to be unused.
Loading history...
303
        indices, processed = model.postprocess(
304
            inputs=model._inputs, outputs=model._outputs
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _inputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
Coding Style Best Practice introduced by
It seems like _outputs was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
305
        )
306
        assert indices.shape == (batch_size, index_size)
307
        assert len(processed) == 5
308