From aa15114680ac5f602940675658125c28cf239f50 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Mon, 29 Dec 2025 21:21:11 +0100 Subject: [PATCH 1/4] Add integration test for `ToolBaseplate` with an optional input image (failing) --- tests/integration/test__cli.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/tests/integration/test__cli.py b/tests/integration/test__cli.py index fe887c2..25e76eb 100644 --- a/tests/integration/test__cli.py +++ b/tests/integration/test__cli.py @@ -10,12 +10,16 @@ import giatools.cli import giatools.image +import giatools.typing as _T from ..tools import minimum_python_version -def _threshold(image1: np.ndarray, image2: np.ndarray) -> np.ndarray: - return image1 > image2 +def _threshold(image1: np.ndarray, image2: _T.Optional[np.ndarray]) -> np.ndarray: + if image2 is None: + return image1 > image1.mean() + else: + return image1 > image2 if __name__ == '__main__': @@ -27,7 +31,7 @@ def _threshold(image1: np.ndarray, image2: np.ndarray) -> np.ndarray: tool = giatools.cli.ToolBaseplate('ToolBaseplate Test', params_required=False) tool.add_input_image('input1') - tool.add_input_image('input2') + tool.add_input_image('input2', required=False) tool.add_output_image('output') tool.parse_args() @@ -35,7 +39,10 @@ def _threshold(image1: np.ndarray, image2: np.ndarray) -> np.ndarray: print(tool.args.params) for proc in tool.run('YX', output_dtype_hint='binary'): - proc['output'] = _threshold(proc['input1'].data, proc['input2'].data) + if proc['input2'] is None: + proc['output'] = _threshold(proc['input1'].data) + else: + proc['output'] = _threshold(proc['input1'].data, proc['input2'].data) class ToolBaseplate(unittest.TestCase): @@ -77,7 +84,7 @@ def test__help(self): self.assertIn(token, result.stdout) @minimum_python_version(3, 11) - def test(self): + def test__two_inputs(self): with tempfile.TemporaryDirectory() as temp_path: output_filepath = str(pathlib.Path(temp_path) / 'output.png') result = self._run_cli( @@ -94,6 +101,22 @@ def test(self): self.assertEqual(output_image.axes, 'YXC') self.assertEqual(result.stdout, '') + @minimum_python_version(3, 11) + def test__one_input(self): + with tempfile.TemporaryDirectory() as temp_path: + output_filepath = str(pathlib.Path(temp_path) / 'output.png') + result = self._run_cli( + '--input1', 'tests/data/input4_uint8.png', + '--output', output_filepath, + ) + output_image = giatools.image.Image.read(output_filepath, normalize_axes=None) + expected_image_data = _threshold( + giatools.image.Image.read('tests/data/input4_uint8.png', normalize_axes=None).data, + ).astype(np.uint8) * 255 + np.testing.assert_array_equal(output_image.data, expected_image_data) + self.assertEqual(output_image.axes, 'YXC') + self.assertEqual(result.stdout, '') + @minimum_python_version(3, 11) def test__verbose(self): with tempfile.TemporaryDirectory() as temp_path: From 4ec8c518c318b8460e2963ef37fb0d141d1e69c1 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Mon, 29 Dec 2025 21:21:25 +0100 Subject: [PATCH 2/4] Bump version to 0.7.2 --- giatools/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/giatools/version.py b/giatools/version.py index ee0eff8..0f78390 100644 --- a/giatools/version.py +++ b/giatools/version.py @@ -1,5 +1,5 @@ VERSION_MAJOR = 0 VERSION_MINOR = 7 -VERSION_PATCH = 1 +VERSION_PATCH = 2 __version__ = '%d.%d.%d' % (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH) From 569366c040a43cec28b32ae5f79c22a86f242df2 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Mon, 29 Dec 2025 21:50:27 +0100 Subject: [PATCH 3/4] Fix, but still failing --- giatools/cli.py | 6 +++++- tests/integration/test__cli.py | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/giatools/cli.py b/giatools/cli.py index ffcf7bf..04f76f0 100644 --- a/giatools/cli.py +++ b/giatools/cli.py @@ -109,7 +109,11 @@ def parse_args(self) -> types.SimpleNamespace: """ args = self.parser.parse_args() input_filepaths = {key: getattr(args, key) for key in self.input_keys} - input_images = {key: self._read_image(args, key, filepath) for key, filepath in input_filepaths.items()} + input_images = { + key: self._read_image(args, key, filepath) + for key, filepath in input_filepaths.items() + if filepath is not None + } output_filepaths = {key: getattr(args, key) for key in self.output_keys} if args.params is None: params = None diff --git a/tests/integration/test__cli.py b/tests/integration/test__cli.py index 25e76eb..a44ff65 100644 --- a/tests/integration/test__cli.py +++ b/tests/integration/test__cli.py @@ -39,10 +39,10 @@ def _threshold(image1: np.ndarray, image2: _T.Optional[np.ndarray]) -> np.ndarra print(tool.args.params) for proc in tool.run('YX', output_dtype_hint='binary'): - if proc['input2'] is None: - proc['output'] = _threshold(proc['input1'].data) - else: + if 'input2' in tool.args.input_images: proc['output'] = _threshold(proc['input1'].data, proc['input2'].data) + else: + proc['output'] = _threshold(proc['input1'].data, None) class ToolBaseplate(unittest.TestCase): @@ -111,7 +111,7 @@ def test__one_input(self): ) output_image = giatools.image.Image.read(output_filepath, normalize_axes=None) expected_image_data = _threshold( - giatools.image.Image.read('tests/data/input4_uint8.png', normalize_axes=None).data, + giatools.image.Image.read('tests/data/input4_uint8.png', normalize_axes=None).data, None, ).astype(np.uint8) * 255 np.testing.assert_array_equal(output_image.data, expected_image_data) self.assertEqual(output_image.axes, 'YXC') From 7589e53b418d6631e070254fd98585375a1ccddc Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Mon, 29 Dec 2025 23:58:49 +0100 Subject: [PATCH 4/4] Fix test --- tests/integration/test__cli.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/integration/test__cli.py b/tests/integration/test__cli.py index a44ff65..b375656 100644 --- a/tests/integration/test__cli.py +++ b/tests/integration/test__cli.py @@ -109,9 +109,13 @@ def test__one_input(self): '--input1', 'tests/data/input4_uint8.png', '--output', output_filepath, ) + input_image = giatools.image.Image.read('tests/data/input4_uint8.png', normalize_axes=None) output_image = giatools.image.Image.read(output_filepath, normalize_axes=None) - expected_image_data = _threshold( - giatools.image.Image.read('tests/data/input4_uint8.png', normalize_axes=None).data, None, + expected_image_data = np.stack( + [ + _threshold(input_image.data[:, :, ch], None) for ch in range(input_image.shape[2]) + ], + axis=2, ).astype(np.uint8) * 255 np.testing.assert_array_equal(output_image.data, expected_image_data) self.assertEqual(output_image.axes, 'YXC')