|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on 17 Mar 2026 |
| 4 | +
|
| 5 | +@author: Tim Moerkerken |
| 6 | +
|
| 7 | +Copyright © 2014-2026 Tim Moerkerken, Delmic |
| 8 | +
|
| 9 | +This file is part of Odemis. |
| 10 | +
|
| 11 | +Odemis is free software: you can redistribute it and/or modify it under the terms |
| 12 | +of the GNU General Public License version 2 as published by the Free Software |
| 13 | +Foundation. |
| 14 | +
|
| 15 | +Odemis is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 16 | +without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 | +PURPOSE. See the GNU General Public License for more details. |
| 18 | +
|
| 19 | +You should have received a copy of the GNU General Public License along with |
| 20 | +Odemis. If not, see http://www.gnu.org/licenses/. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import tempfile |
| 25 | +import unittest |
| 26 | +import numpy |
| 27 | + |
| 28 | +from odemis import model |
| 29 | +from odemis.dataio import tiff |
| 30 | +from odemis.util.tiff_convert import ( |
| 31 | + get_conversion_output_path, |
| 32 | + ensure_pyramidal_tiff, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class TestTiffConversion(unittest.TestCase): |
| 37 | + |
| 38 | + def setUp(self) -> None: |
| 39 | + """Set up test fixtures.""" |
| 40 | + self.temp_dir = tempfile.mkdtemp() |
| 41 | + |
| 42 | + def tearDown(self) -> None: |
| 43 | + """Clean up temporary files.""" |
| 44 | + # Remove all temporary files |
| 45 | + try: |
| 46 | + for filename in os.listdir(self.temp_dir): |
| 47 | + file_path = os.path.join(self.temp_dir, filename) |
| 48 | + if os.path.isfile(file_path): |
| 49 | + os.remove(file_path) |
| 50 | + os.rmdir(self.temp_dir) |
| 51 | + except Exception: |
| 52 | + pass |
| 53 | + |
| 54 | + def _create_test_tiff(self, filename: str, pyramid: bool = False) -> str: |
| 55 | + """ |
| 56 | + Create a test TIFF file. |
| 57 | +
|
| 58 | + :param filename: Output filename |
| 59 | + :param pyramid: If True, create a pyramidal TIFF |
| 60 | + :return: Full path to created file |
| 61 | + """ |
| 62 | + # Create simple test image |
| 63 | + array = numpy.random.randint(0, 255, (256, 256), dtype=numpy.uint8) |
| 64 | + data = model.DataArray(array) |
| 65 | + |
| 66 | + full_path = os.path.join(self.temp_dir, filename) |
| 67 | + tiff.export(full_path, data, pyramid=pyramid, imagej=True) # Use ImageJ format to avoid OME issues |
| 68 | + return full_path |
| 69 | + |
| 70 | + def test_get_conversion_output_path_standalone(self) -> None: |
| 71 | + """Test output path generation in standalone mode.""" |
| 72 | + source_file = os.path.join(self.temp_dir, "original.tif") |
| 73 | + output = get_conversion_output_path(source_file, standalone_mode=True) |
| 74 | + |
| 75 | + # Should be in same directory with visible prefix |
| 76 | + self.assertEqual(os.path.dirname(output), self.temp_dir) |
| 77 | + self.assertTrue(os.path.basename(output).startswith("converted_")) |
| 78 | + self.assertTrue(os.path.basename(output).endswith(".tif")) |
| 79 | + |
| 80 | + def test_get_conversion_output_path_normal_mode(self) -> None: |
| 81 | + """Test output path generation in normal mode.""" |
| 82 | + source_file = "/some/path/original.tif" |
| 83 | + project_folder = "/project/folder" |
| 84 | + output = get_conversion_output_path(source_file, standalone_mode=False, project_folder=project_folder) |
| 85 | + |
| 86 | + # Should be in project folder |
| 87 | + self.assertEqual(os.path.dirname(output), project_folder) |
| 88 | + self.assertTrue(os.path.basename(output).startswith("converted_")) |
| 89 | + |
| 90 | + def test_get_conversion_output_path_normal_mode_no_project(self) -> None: |
| 91 | + """Test that ValueError is raised when project_folder is missing in normal mode.""" |
| 92 | + source_file = "/some/path/original.tif" |
| 93 | + with self.assertRaises(ValueError): |
| 94 | + get_conversion_output_path(source_file, standalone_mode=False, project_folder=None) |
| 95 | + |
| 96 | + def test_ensure_pyramidal_tiff_already_pyramidal(self) -> None: |
| 97 | + """Test ensure_pyramidal_tiff with already pyramidal file.""" |
| 98 | + pyramidal_file = self._create_test_tiff("already_pyramidal.tif", pyramid=True) |
| 99 | + |
| 100 | + # Call ensure_pyramidal_tiff |
| 101 | + result = ensure_pyramidal_tiff(pyramidal_file, standalone_mode=True) |
| 102 | + |
| 103 | + # Should return the same file without conversion |
| 104 | + self.assertEqual(result, pyramidal_file) |
| 105 | + |
| 106 | + def test_ensure_pyramidal_tiff_non_tiff_file(self) -> None: |
| 107 | + """Test ensure_pyramidal_tiff with non-TIFF file.""" |
| 108 | + # Create a non-TIFF file |
| 109 | + non_tiff_file = os.path.join(self.temp_dir, "test.txt") |
| 110 | + with open(non_tiff_file, 'w') as f: |
| 111 | + f.write("test content") |
| 112 | + |
| 113 | + # Non-TIFF should return as-is (in ensure_pyramidal_tiff before conversion attempt) |
| 114 | + result = ensure_pyramidal_tiff(non_tiff_file, standalone_mode=True) |
| 115 | + self.assertEqual(result, non_tiff_file) |
| 116 | + |
| 117 | + def test_ensure_pyramidal_tiff_nonexistent_file(self) -> None: |
| 118 | + """Test ensure_pyramidal_tiff with non-existent file.""" |
| 119 | + with self.assertRaises(IOError): |
| 120 | + ensure_pyramidal_tiff("/nonexistent/file.tif", standalone_mode=True) |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + unittest.main() |
0 commit comments