Skip to content

Commit

Permalink
create copy_to method
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jul 4, 2024
1 parent 573e6fe commit d483f09
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/depiction/persistence/imzml_read_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
from collections import defaultdict
from collections.abc import Generator
from contextlib import contextmanager
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 2 additions & 10 deletions src/depiction/tools/pick_peaks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import shutil
from pathlib import Path
from typing import Literal, Any

from loguru import logger
Expand Down Expand Up @@ -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"))
11 changes: 11 additions & 0 deletions tests/unit/persistence/test_imzml_read_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d483f09

Please sign in to comment.