diff --git a/src/depiction/persistence/imzml_read_file.py b/src/depiction/persistence/imzml_read_file.py index 42eb4b4..650cf7f 100644 --- a/src/depiction/persistence/imzml_read_file.py +++ b/src/depiction/persistence/imzml_read_file.py @@ -1,3 +1,4 @@ +import shutil from collections import defaultdict from collections.abc import Generator from contextlib import contextmanager @@ -185,6 +186,11 @@ def pixel_size(self) -> PixelSize | None: # TODO actual check of the unit return PixelSize(size_x=pixel_size_x, size_y=pixel_size_y, unit="micrometer") + def copy_to(self, path: Path) -> None: + """Copies the file of this instance to the given path. Needs to end with .imzML.""" + shutil.copy(self.imzml_file, path) + shutil.copy(self.ibd_file, path.with_suffix(".ibd")) + @cached_property def _cached_properties(self) -> dict[str, Any]: with self.reader() as reader: diff --git a/src/depiction/tools/pick_peaks.py b/src/depiction/tools/pick_peaks.py index c96e364..1b6e20a 100644 --- a/src/depiction/tools/pick_peaks.py +++ b/src/depiction/tools/pick_peaks.py @@ -1,7 +1,5 @@ from __future__ import annotations -import shutil -from pathlib import Path from typing import Literal, Any from loguru import logger @@ -145,14 +143,8 @@ def pick_peaks( if config.peak_picker is None or ( not config.force_peak_picker and input_file.imzml_mode == ImzmlModeEnum.PROCESSED ): - copy_without_picking(input_imzml_path=input_file.imzml_file, output_imzml_path=output_file.imzml_file) + logger.info("Peak picking is deactivated") + input_file.copy_to(output_file.imzml_file) else: pick_peaks = PickPeaks(peak_picker=peak_picker, parallel_config=parallel_config) pick_peaks.evaluate_file(read_file=input_file, write_file=output_file) - - -def copy_without_picking(input_imzml_path: Path, output_imzml_path: Path) -> None: - # TODO this is duplicated in several places and should be unified, in fact it could be a method of ImzmlReadFile - logger.info("Peak picking is deactivated") - shutil.copy(input_imzml_path, output_imzml_path) - shutil.copy(input_imzml_path.with_suffix(".ibd"), output_imzml_path.with_suffix(".ibd")) diff --git a/tests/unit/persistence/test_imzml_read_file.py b/tests/unit/persistence/test_imzml_read_file.py index 6644c53..3476c45 100644 --- a/tests/unit/persistence/test_imzml_read_file.py +++ b/tests/unit/persistence/test_imzml_read_file.py @@ -265,6 +265,17 @@ def test_print_summary(mocker: MockerFixture, mock_read_file: ImzmlReadFile) -> mock_print.assert_called_once_with(mock_summary.return_value, file=None) +def test_copy_to(mocker: MockerFixture, mock_read_file: ImzmlReadFile) -> None: + mock_output_file = mocker.MagicMock(name="mock_output_file") + mock_copy = mocker.patch("shutil.copy") + mock_read_file.copy_to(mock_output_file) + assert mock_copy.mock_calls == [ + mocker.call(mock_read_file.imzml_file, mock_output_file), + mocker.call(mock_read_file.ibd_file, mock_output_file.with_suffix.return_value), + ] + mock_output_file.with_suffix.assert_called_once_with(".ibd") + + def test_cached_properties(mocker: MockerFixture, mock_read_file: ImzmlReadFile) -> None: mock_reader = MagicMock(name="mock_reader") mocker.patch.object(ImzmlReadFile, "reader").return_value.__enter__.return_value = mock_reader