diff --git a/setup.py b/setup.py index 9ccd732bb500e410e00976d062c218fc9eb473cf..0b679c3879cb818be91c24a9ee29352538ed4c13 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,8 @@ setup( ('share/' + package_name, ['package.xml']), ], install_requires=['setuptools', - 'Pillow'], + 'Pillow', + 'pyometiff'], zip_safe=True, maintainer='root', maintainer_email='simon.wittl@th-deg.de', diff --git a/test/test_rq_json_loader.py b/test/test_rq_json_loader.py new file mode 100644 index 0000000000000000000000000000000000000000..546e012eec759d70ba042684b173b94b3bc3c267 --- /dev/null +++ b/test/test_rq_json_loader.py @@ -0,0 +1,133 @@ +import pytest +import numpy as np +from pathlib import Path +from unittest.mock import MagicMock, patch + +# Importing symbols from loader module +from rq_controller.common import PyProjection, PyProjectionGeometry, PyRegionOfIntrest, PyVolume +import pyometiff + +from rq_controller.common.io.rq_json import RqJsonLoader + + +class MockOMETIFFReader: + def __init__(self, data): + self.data = data + + def read(self): + return self.data, None, None + + +@pytest.fixture +def rq_json_loader() -> RqJsonLoader: + return RqJsonLoader() + + +@pytest.fixture +def mock_load_json(): + # Mocking the load_json method to avoid actual file operations + with patch.object(RqJsonLoader, 'load_json') as mock: + mock.return_value = { + 'focal_spot_mm': np.array([1.0, 1.1, 1.2]), + 'detector_postion_mm': np.array([2.0, 2.1, 2.2]), + 'detector_orientation_quad': np.array([3.0, 3.1, 3.2]), + 'frame_id': 'frame1', + 'focal_spot_orientation_quad': np.array([4.0, 4.1, 4.2]), + 'detector_heigth_mm': 5.0, + 'detector_width_mm': 6.0, + 'voltage_kv': 7.0, + 'current_ua': 8.0, + 'exposure_time_ms': 9.0, + 'center_points_mm': [10.0, 11.0, 12.0], + 'dimensions_mm': [13.0, 14.0, 15.0], + 'resolution_mm': [16.0, 17.0, 18.0] + } + yield mock + + +def test_initialization(rq_json_loader): + assert rq_json_loader.porjection_geometry_suffix == '.geom-json' + assert rq_json_loader.projection_suffix == '.tif' + assert rq_json_loader.region_of_intrest_suffix == '.roi-json' + assert rq_json_loader.volume_suffix == '.ome.tiff' + + +def test_load_projection_geometry(rq_json_loader, mock_load_json): + load_path = Path('/fake/path.tif') + projection_geometry = rq_json_loader.load_projection_geometry(load_path) + assert isinstance(projection_geometry, PyProjectionGeometry) + # Add more assertions as needed based on your implementation + + +def test_load_projection(rq_json_loader, mock_load_json): + load_path = Path('/fake/path.tif') + projection = rq_json_loader.load_projection(load_path) + assert isinstance(projection, PyProjection) + # Add more assertions as needed based on your implementation + + +def test_load_region_of_intrest(rq_json_loader, mock_load_json): + load_path = Path('/fake/path.roi-json') + region_of_intrest = rq_json_loader.load_region_of_intrest(load_path) + assert isinstance(region_of_intrest, PyRegionOfIntrest) + # Add more assertions as needed based on your implementation + + +def test_load_volume(rq_json_loader, mock_load_json): + load_path = Path('/fake/path.ome.tiff') + with patch('pyometiff.OMETIFFReader', MockOMETIFFReader): + volume = rq_json_loader.load_volume(load_path) + assert isinstance(volume, PyVolume) + # Add more assertions as needed based on your implementation + + +def test_load_projection_geometry_raises_not_implemented(rq_json_loader): + load_path = Path('/fake/path.tif') + with pytest.raises(NotImplementedError): + rq_json_loader.load_projection_geometry(load_path) + + +def test_load_projection_raises_not_implemented(rq_json_loader): + load_path = Path('/fake/path.tif') + with pytest.raises(NotImplementedError): + rq_json_loader.load_projection(load_path) + + +def test_load_region_of_intrest_raises_not_implemented(rq_json_loader): + load_path = Path('/fake/path.roi-json') + with pytest.raises(NotImplementedError): + rq_json_loader.load_region_of_intrest(load_path) + + +def test_load_volume_raises_not_implemented(rq_json_loader): + load_path = Path('/fake/path.ome.tiff') + with pytest.raises(NotImplementedError): + rq_json_loader.load_volume(load_path) + + +def test_load_json(rq_json_loader): + load_path = Path('/fake/path.json') + data_dict = { + 'focal_spot_mm': np.array([1.0]), + 'detector_postion_mm': np.array([2.0]), + 'detector_orientation_quad': np.array([3.0]), + 'frame_id': 'frame1', + 'focal_spot_orientation_quad': np.array([4.0]) + } + with patch('builtins.open', MagicMock(return_value=MagicMock(spec=open))): + with patch('json.load', MagicMock(return_value=data_dict)): + result = rq_json_loader.load_json(load_path) + assert result == data_dict + + +def test_load_json_file_not_found(rq_json_loader): + load_path = Path('/fake/path.json') + with pytest.raises(FileNotFoundError): + rq_json_loader.load_json(load_path) + + +def test_load_volume_invalid_dtype(rq_json_loader, mock_load_json): + load_path = Path('/fake/path.ome.tiff') + with patch('pyometiff.OMETIFFReader', MockOMETIFFReader): + with pytest.raises(ValueError): + rq_json_loader.load_volume(load_path)