Пример #1
0
 def load_config(self, fname: str):
     with open(fname, 'r') as fin:
         try:
             self.cvpipeline = CompVizPipeline.load(fin)
             self.operators_changed.emit()
         except ConfigParsingError as e:
             self.loading_failed.emit(e)
Пример #2
0
 def test_operators_params(self, config_stream):
     pipeline = CompVizPipeline.load(config_stream)
     ops = pipeline.operators
     op1 = ops['op1']
     op2 = ops['op2']
     assert op1.param1 == OP1_PARAM1
     assert op1.param2 == OP1_PARAM2
     assert op2.param1 == OP2_PARAM1
     assert op2.param2 == OP2_PARAM2
Пример #3
0
 def test_operators_types(self, config_stream):
     pipeline = CompVizPipeline.load(config_stream)
     assert all(
         isinstance(op, TestOperator) for op in pipeline.operators.values())
Пример #4
0
 def test_operators_names(self, config_stream):
     pipeline = CompVizPipeline.load(config_stream)
     assert list(pipeline.operators.keys()) == ["op1", "op2"]
Пример #5
0
 def test_return_type(self, config_stream):
     pipeline = CompVizPipeline.load(config_stream)
     assert isinstance(pipeline, CompVizPipeline)
Пример #6
0
def pipeline(config_stream):
    return CompVizPipeline.load(config_stream)
Пример #7
0
class TestRun:
    @parametrize_img
    @pytest.mark.parametrize(
        'pipeline_',
        [CompVizPipeline(),
         CompVizPipeline.load(get_config_stream())])
    def test_return_type(self, img, pipeline_):
        r = pipeline_.run(img)
        assert isinstance(r, tuple)
        assert len(r) == 2
        out, ctx = r
        assert is_image(out)
        assert isinstance(ctx, PipelineContext)

    @parametrize_img(include_valid=False, include_invalid=True)
    def test_invalid_img(self, img):
        pipeline = CompVizPipeline()
        with pytest.raises(BadImageError) as e:
            pipeline.run(img)

        assert_terms_in_exception(e, ['invalid', 'image'])

    @parametrize_img(kind='black')
    def test_result(self, img, pipeline):
        out, ctx = pipeline.run(img)
        assert np.all(out == 2)

    @parametrize_img
    def test_run_all_ops(self, img, pipeline):
        with patch(__name__ + '.TestOperator.run') as mock:
            mock.side_effect = lambda i, _: i
            _ = pipeline.run(img)
            assert mock.call_count == 2

    @pytest.mark.parametrize('return_value', [
        np.random.randint(0, 256, size=(16, 16), dtype='int64'),
        np.random.randint(0, 256, size=(0, 16), dtype='uint8'),
        np.random.randint(0, 256, size=(16, 0), dtype='uint8'),
        np.random.randint(0, 256, size=(0, 0), dtype='uint8'),
        np.random.randint(0, 256, size=(16, 16, 2), dtype='uint8'), 10, None,
        object(),
        np.random.randint(0, 256, size=(10, ), dtype='uint8')
    ])
    def test_check_op_return(self, return_value):
        class TestWrongReturnOperator(Operator):
            def run(self, img: np.ndarray, ctx: PipelineContext) -> Any:
                return return_value

        pipeline = CompVizPipeline()
        pipeline.add_operator('test_op', TestWrongReturnOperator())

        with pytest.raises(BadImageError) as e:
            pipeline.run(build_img((16, 16)))

        assert_terms_in_exception(e, ['return', 'invalid'])

    @parametrize_img
    def test_set_ctx_original_img(self, img):
        class TestCtxOriginalImgOperator(Operator):
            def run(self, img_: Image, ctx: PipelineContext) -> Image:
                assert np.all(ctx.original_img == img_)
                return img_

        pipeline = CompVizPipeline()
        pipeline.add_operator('test_op', TestCtxOriginalImgOperator())
        pipeline.run(img)

    def test_operator_cant_alter_original_img(self):
        class TestCtxOriginalImg(Operator):
            def run(self, img: Image, ctx: PipelineContext) -> Image:
                original_img = ctx.original_img
                original_img[10, ...] = 255
                return img

        pipeline = CompVizPipeline()
        pipeline.add_operator('test_op', TestCtxOriginalImg())

        test_img = build_img((128, 128), kind='black')
        with pytest.raises(OperatorFailedError) as e:
            pipeline.run(test_img)

        assert_terms_in_exception(e, ['read-only'])

    def test_add_info_works(self):
        info_name = 'info_name'
        info_value = object()

        class TestCtxAddInfo(Operator):
            def run(self, img: Image, ctx: PipelineContext) -> Image:
                ctx.add_info(info_name, info_value)
                return img

        pipeline = CompVizPipeline()
        op_name1 = 'test_op1'
        pipeline.add_operator(op_name1, TestCtxAddInfo())
        op_name2 = 'test_op2'
        pipeline.add_operator(op_name2, TestCtxAddInfo())

        _, returned_ctx = pipeline.run(build_img((16, 16)))

        expected_info = {
            op_name1: {
                info_name: info_value
            },
            op_name2: {
                info_name: info_value
            }
        }
        actual_info = returned_ctx.info

        assert actual_info == expected_info

    def test_pipeline_run_empty_info(self, pipeline):
        img = build_img((16, 16))
        out, ctx = pipeline.run(img)
        assert ctx.info == {'op1': {}, 'op2': {}}

    def test_gray_only_flag(self):
        @settings.GRAY_ONLY(True)
        class TestOnlyGrayFlagOperator(Operator):
            def run(self, img: Image, ctx: PipelineContext) -> Image:
                return img

        pipeline = CompVizPipeline()
        pipeline.add_operator('test_op', TestOnlyGrayFlagOperator())

        with pytest.raises(OperatorFailedError) as e:
            pipeline.run(build_img((16, 16), rgb=True))

        assert_terms_in_exception(e, ["expect", "gray"])