Skip to content

Commit

Permalink
more code style
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jul 1, 2024
1 parent 3c150e2 commit d614424
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/depiction/evaluate_signal_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def evaluate(self, mz_values: np.ndarray, int_values: np.ndarray) -> np.ndarray:
# Don't filter
return int_values
else:
# the problem with below's implementation was, that e.g. for input of size 1 it would create an output of size 5 (since it choses the bigger size of the two arrays)
# the problem with below's implementation was, that e.g. for input of size 1
# it would create an output of size 5 (since it choses the bigger size of the two arrays)
# return scipy.signal.convolve(int_array, gaussian_filter, mode='same')
values = np.convolve(int_values, gaussian_filter, mode="same")
values[0] = int_values[0]
Expand Down
5 changes: 3 additions & 2 deletions src/depiction/persistence/imzml_mode_enum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import enum


Expand All @@ -8,12 +9,12 @@ class ImzmlModeEnum(enum.Enum):
PROCESSED = enum.auto()

@classmethod
def as_pyimzml_str(cls, instance) -> str:
def as_pyimzml_str(cls, instance: ImzmlModeEnum) -> str:
"""Returns the string representation of the enum value as used by pyimzml."""
return instance.name.lower()

@classmethod
def from_pyimzml_str(cls, value: str) -> "ImzmlModeEnum":
def from_pyimzml_str(cls, value: str) -> ImzmlModeEnum:
if value == "continuous":
return cls.CONTINUOUS
elif value == "processed":
Expand Down
4 changes: 2 additions & 2 deletions src/depiction/persistence/imzml_read_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from contextlib import contextmanager
from functools import cached_property
from pathlib import Path
from typing import Any, Optional
from typing import Any, Optional, TextIO

import pyimzml.ImzMLParser
from numpy.typing import NDArray
Expand Down Expand Up @@ -164,7 +164,7 @@ def summary(self, checksums: bool = True) -> str:
f"{mz_range_line}"
)

def print_summary(self, checksums: bool = True, file=None) -> None:
def print_summary(self, checksums: bool = True, file: TextIO | None = None) -> None:
print(self.summary(checksums=checksums), file=file)

@cached_property
Expand Down
7 changes: 5 additions & 2 deletions src/depiction/persistence/imzml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from depiction.persistence.imzml_mode_enum import ImzmlModeEnum

if TYPE_CHECKING:
from types import TracebackType
from pathlib import Path
from numpy.typing import NDArray

Expand Down Expand Up @@ -60,7 +61,7 @@ def __getstate__(self) -> dict[str, Any]:
}

# TODO
def __setstate__(self, state: dict[str, Any]):
def __setstate__(self, state: dict[str, Any]) -> None:
# self._portable_reader = state["portable_reader"]
self._imzml_path = state["imzml_path"]
self._ibd_file = None
Expand Down Expand Up @@ -100,7 +101,9 @@ def ibd_mmap(self) -> mmap.mmap:
def __enter__(self) -> ImzmlReader:
return self

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> None:
self.close()

def close(self) -> None:
Expand Down
10 changes: 7 additions & 3 deletions src/depiction/persistence/imzml_write_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from contextlib import contextmanager
from __future__ import annotations
from contextlib import contextmanager, AbstractContextManager
from pathlib import Path
from typing import TYPE_CHECKING

from depiction.persistence.imzml_mode_enum import ImzmlModeEnum
from depiction.persistence.imzml_writer import ImzmlWriter

if TYPE_CHECKING:
from depiction.persistence.imzml_mode_enum import ImzmlModeEnum


class ImzmlWriteFile:
"""A handle for a .imzML file that is to be written.
Expand Down Expand Up @@ -38,7 +42,7 @@ def imzml_mode(self) -> ImzmlModeEnum:
return self._imzml_mode

@contextmanager
def writer(self):
def writer(self) -> AbstractContextManager[ImzmlWriter]:
"""Opens the .imzML file for writing and yields an `ImzmlWriter` instance."""
if self._write_mode == "x":
if self.imzml_file.exists():
Expand Down
31 changes: 16 additions & 15 deletions src/depiction/persistence/imzml_writer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations
from pathlib import Path
from typing import Optional, TYPE_CHECKING
from collections.abc import Sequence
from typing import TYPE_CHECKING

import numpy as np
import pyimzml
import pyimzml.ImzMLWriter
from tqdm import tqdm
Expand All @@ -11,6 +10,8 @@
from depiction.persistence.imzml_mode_enum import ImzmlModeEnum

if TYPE_CHECKING:
from collections.abc import Sequence
import numpy as np
from depiction.persistence.imzml_reader import ImzmlReader


Expand All @@ -19,13 +20,13 @@ def __init__(
self,
*,
wrapped_imzml_writer: pyimzml.ImzMLWriter.ImzMLWriter,
imzml_alignment_tracker: Optional[ImzmlAlignmentTracker],
imzml_alignment_tracker: ImzmlAlignmentTracker | None,
) -> None:
self._imzml_writer = wrapped_imzml_writer
self._imzml_alignment_tracker = imzml_alignment_tracker

@classmethod
def open(cls, path: str | Path, imzml_mode: ImzmlModeEnum, imzml_alignment_tracking: bool = True):
def open(cls, path: str | Path, imzml_mode: ImzmlModeEnum, imzml_alignment_tracking: bool = True) -> ImzmlWriter:
"""Opens an imzML file."""
imzml_alignment_tracker = ImzmlAlignmentTracker() if imzml_alignment_tracking else None
return cls(
Expand All @@ -37,7 +38,8 @@ def open(cls, path: str | Path, imzml_mode: ImzmlModeEnum, imzml_alignment_track
)

# TODO this currently has a bug, when closing a reader to which not a single spectrum was added.
# it should be handled gracefully, since there are reasonable cases where this could occur with the use of "with" clauses.
# it should be handled gracefully, since there are reasonable cases,
# where this could occur with the use of "with" clauses.
def close(self) -> None:
self._imzml_writer.close()

Expand Down Expand Up @@ -75,20 +77,19 @@ def add_spectrum(
if self._imzml_alignment_tracker:
self._imzml_alignment_tracker.track_mz_array(mz_arr)

if self.imzml_mode == ImzmlModeEnum.CONTINUOUS and self._imzml_alignment_tracker:
if not self.is_aligned:
raise ValueError(
"The m/z array of the first spectrum must be identical to the m/z array of all other spectra!"
)
if self.imzml_mode == ImzmlModeEnum.CONTINUOUS and self._imzml_alignment_tracker and not self.is_aligned:
raise ValueError(
"The m/z array of the first spectrum must be identical to the m/z array of all other spectra!"
)

# Write the spectrum.
self._imzml_writer.addSpectrum(mz_arr, int_arr, coordinates)

def copy_spectra(
self,
reader: "ImzmlReader",
reader: ImzmlReader,
spectra_indices: Sequence[int],
tqdm_position: Optional[int] = None,
tqdm_position: int | None = None,
) -> None:
"""
Copies spectra from an existing reader. Not optimized yet.
Expand All @@ -97,12 +98,12 @@ def copy_spectra(
"""
if tqdm_position is not None:

def progress_fn(x):
def progress_fn(x: Sequence[int]) -> tqdm:
return tqdm(x, desc=" spectrum", position=tqdm_position)

else:

def progress_fn(x):
def progress_fn(x: Sequence[int]) -> Sequence[int]:
return x

for spectrum_index in progress_fn(spectra_indices):
Expand Down
25 changes: 17 additions & 8 deletions src/depiction/persistence/ram_reader.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from typing import Any, NoReturn
from __future__ import annotations

from typing import Any, NoReturn, TYPE_CHECKING

import numpy as np
from numpy._typing import NDArray

from depiction.persistence import ImzmlModeEnum

if TYPE_CHECKING:
from types import TracebackType
from numpy.typing import NDArray


class RamReader:
def __init__(self, mz_arr_list, int_arr_list, coordinates) -> None:
def __init__(
self, mz_arr_list: list[NDArray[float]], int_arr_list: list[NDArray[float]], coordinates: NDArray[int]
) -> None:
self._mz_arr_list = mz_arr_list
self._int_arr_list = int_arr_list
self._coordinates = coordinates
Expand All @@ -27,10 +34,12 @@ def ibd_mmap(self) -> NoReturn:
# TODO a problem, right?
raise NotImplementedError

def __enter__(self):
def __enter__(self) -> RamReader:
return self

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> None:
self.close()

def close(self) -> None:
Expand All @@ -55,11 +64,11 @@ def n_spectra(self) -> int:
return len(self._mz_arr_list)

@property
def coordinates(self):
def coordinates(self) -> NDArray[int]:
return self._coordinates

@property
def coordinates_2d(self):
def coordinates_2d(self) -> NDArray[int]:
return self._coordinates[:, :2]

def get_spectrum(self, i_spectrum: int) -> tuple[np.ndarray, np.ndarray]:
Expand All @@ -76,7 +85,7 @@ def get_spectra(self, i_spectra: list[int]) -> tuple[np.ndarray | list[np.ndarra
def get_spectrum_mz(self, i_spectrum: int) -> NDArray[float]:
return self._mz_arr_list[i_spectrum]

def get_spectrum_int(self, i_spectrum) -> NDArray[float]:
def get_spectrum_int(self, i_spectrum: int) -> NDArray[float]:
return self._int_arr_list[i_spectrum]

def get_spectrum_n_points(self, i_spectrum: int) -> int:
Expand Down
1 change: 0 additions & 1 deletion src/depiction/tools/split_imzml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
import os
from pathlib import Path
from typing import Optional

import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion src/depiction/visualize/plot_mass_spectra_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, data_df: pl.DataFrame) -> None:
self._data_df = data_df
self._charts = []

def plot_spectrum(self, spectrum_name: str):
def plot_spectrum(self, spectrum_name: str) -> None:
data = self._data_df.filter(spectrum_name=spectrum_name)
chart = (
alt.Chart(data)
Expand Down

0 comments on commit d614424

Please sign in to comment.