Skip to content

Commit

Permalink
move correct_baseline code
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Oct 15, 2024
1 parent f223cb2 commit 641fecb
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/depiction/tools/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from cyclopts import App

from depiction.tools.calibrate.__main__ import app as calibrate
from depiction.tools.cli.cli_correct_baseline import app as correct_baseline
from depiction.tools.cli.cli_filter_peaks import app as filter_peaks
from depiction.tools.cli.cli_generate_ion_images import app as generate_ion_images
from depiction.tools.cli.cli_pick_peaks import app as pick_peaks
from depiction.tools.correct_baseline.__main__ import app as correct_baseline

app = App()
app.command(calibrate, name="calibrate")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from loguru import logger

from depiction.persistence import ImzmlReadFile
from depiction.tools.correct_baseline import BaselineVariants, BaselineCorrectionConfig, correct_baseline
from depiction.tools.correct_baseline.correct_baseline import correct_baseline
from depiction.tools.correct_baseline.config import BaselineVariants, BaselineCorrectionConfig

app = cyclopts.App()

Expand Down
18 changes: 18 additions & 0 deletions src/depiction/tools/correct_baseline/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

import enum
from typing import Literal

from pydantic import BaseModel, PositiveInt, PositiveFloat


class BaselineVariants(str, enum.Enum):
TopHat = "TopHat"
LocMedians = "LocMedians"


class BaselineCorrectionConfig(BaseModel, use_enum_values=True, validate_default=True):
n_jobs: PositiveInt | None = None
baseline_variant: BaselineVariants = BaselineVariants.TopHat
window_size: PositiveInt | PositiveFloat = 5000.0
window_unit: Literal["ppm", "index"] = "ppm"
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
from __future__ import annotations

import enum
from pathlib import Path
from typing import TYPE_CHECKING, Literal

import numpy as np
from pydantic import BaseModel, PositiveInt, PositiveFloat

from depiction.parallel_ops.parallel_config import ParallelConfig
from depiction.parallel_ops.write_spectra_parallel import WriteSpectraParallel
from depiction.persistence import ImzmlWriteFile, ImzmlWriter, ImzmlReader, ImzmlReadFile
from depiction.spectrum.baseline.local_medians_baseline import LocalMediansBaseline
from depiction.spectrum.baseline.tophat_baseline import TophatBaseline
from depiction.tools.correct_baseline.config import BaselineVariants, BaselineCorrectionConfig

if TYPE_CHECKING:
from numpy.typing import NDArray
from depiction.spectrum.baseline.baseline import Baseline


class BaselineVariants(str, enum.Enum):
TopHat = "TopHat"
LocMedians = "LocMedians"


class BaselineCorrectionConfig(BaseModel, use_enum_values=True, validate_default=True):
n_jobs: PositiveInt | None = None
baseline_variant: BaselineVariants = BaselineVariants.TopHat
window_size: PositiveInt | PositiveFloat = 5000.0
window_unit: Literal["ppm", "index"] = "ppm"


class CorrectBaseline:
"""Implements baseline correction for imzml files."""

Expand Down
2 changes: 1 addition & 1 deletion src/depiction_targeted_preproc/pipeline_config/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pydantic import BaseModel, ConfigDict

from depiction.tools.calibrate.config import CalibrationConfig
from depiction.tools.correct_baseline import BaselineCorrectionConfig
from depiction.tools.correct_baseline.config import BaselineCorrectionConfig
from depiction.tools.filter_peaks import FilterPeaksConfig
from depiction.tools.pick_peaks import PickPeaksConfig

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rule proc_correct_baseline_run:
output:
imzml=temp(multiext("{sample}/corrected.original", ".imzML", ".ibd")),
shell:
"python -m depiction.tools.cli.cli_correct_baseline run-config"
"python -m depiction.tools.correct_baseline run-config"
" --config {input.config}"
" --input-imzml {input.imzml[0]} --output-imzml {output.imzml[0]}"

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import pytest
from pytest_mock import MockerFixture

from depiction.tools.cli.cli_correct_baseline import run
from depiction.tools.correct_baseline import BaselineVariants
from depiction.tools.correct_baseline.__main__ import run
from depiction.tools.correct_baseline.config import BaselineVariants


def test_run_when_other_variant(mocker: MockerFixture) -> None:
mock_imzml_mode = mocker.MagicMock(name="mock_imzml_mode", spec=[])
construct_imzml_read_file = mocker.patch("depiction.tools.correct_baseline.ImzmlReadFile")
construct_imzml_read_file = mocker.patch("depiction.tools.correct_baseline.correct_baseline.ImzmlReadFile")
construct_imzml_read_file.return_value.imzml_mode = mock_imzml_mode
construct_imzml_write_file = mocker.patch("depiction.tools.correct_baseline.ImzmlWriteFile")
construct_correct_baseline = mocker.patch("depiction.tools.correct_baseline.CorrectBaseline.from_variant")
construct_imzml_write_file = mocker.patch("depiction.tools.correct_baseline.correct_baseline.ImzmlWriteFile")
construct_correct_baseline = mocker.patch(
"depiction.tools.correct_baseline.correct_baseline.CorrectBaseline.from_variant"
)
mock_input_imzml = Path("/dev/null/hello.imzML")
mock_output_imzml = mocker.MagicMock(name="mock_output_imzml")

Expand Down

0 comments on commit 641fecb

Please sign in to comment.