mioXpektron.plotting.plot_peaks

Functions

plot_overlapping_peaks(data_dir, ...[, ...])

Overlay spectra with two colors (Cancer vs Control) inferred from file names.

Classes

PlotPeaks([config])

Class for plotting overlapping peaks from multiple spectra files.

PlotPeaksConfig(data_dir[, file_pattern, ...])

Configuration for PlotPeaks class.

class mioXpektron.plotting.plot_peaks.PlotPeaksConfig(data_dir, file_pattern='*.txt', mz_min=0.0, mz_max=1000.0, norm_tic=False, bin_width=0.001, alpha=0.18, show_median=True, show_group_cumulative=True, figsize=(10, 6), cumulative_figsize=(10, 4), color_map=None, save_fig=False, save_path='output_files/plots')[source]

Bases: object

Configuration for PlotPeaks class.

Parameters:
  • data_dir (str) – Directory containing spectra files.

  • file_pattern (str, default “*.txt”) – Glob pattern for matching spectrum files.

  • mz_min (float, default 0.0) – Minimum m/z value for the plotting window.

  • mz_max (float, default 1000.0) – Maximum m/z value for the plotting window.

  • norm_tic (bool, default False) – If True, normalize intensities by total ion count.

  • bin_width (float, default 0.001) – Bin width for interpolation grid.

  • alpha (float, default 0.18) – Transparency for individual spectra lines.

  • show_median (bool, default True) – If True, overlay median curves on the plot.

  • show_group_cumulative (bool, default True) – If True, create cumulative intensity plot.

  • figsize (tuple, default (10, 6)) – Figure size for overlay plot.

  • cumulative_figsize (tuple, default (10, 4)) – Figure size for cumulative plot.

  • color_map (dict, optional) – Dictionary mapping group names to colors.

  • save_fig (bool, default False) – If True, save figures as PDF files.

  • save_path (str, default "../output_files/plots") – Directory path where PDF files will be saved.

data_dir: str
file_pattern: str = '*.txt'
mz_min: float = 0.0
mz_max: float = 1000.0
norm_tic: bool = False
bin_width: float = 0.001
alpha: float = 0.18
show_median: bool = True
show_group_cumulative: bool = True
figsize: Tuple[int, int] = (10, 6)
cumulative_figsize: Tuple[int, int] = (10, 4)
color_map: Dict[str, str] | None = None
save_fig: bool = False
save_path: str = 'output_files/plots'
class mioXpektron.plotting.plot_peaks.PlotPeaks(config=None)[source]

Bases: object

Class for plotting overlapping peaks from multiple spectra files.

Features: - Load and group spectra by inferred labels (Cancer/Control/Unknown) - Overlay individual spectra with customizable transparency - Plot per-group median curves - Plot cumulative intensity by group - Flexible configuration through PlotPeaksConfig

Example:

>>> config = PlotPeaksConfig(
...     data_dir="data/spectra",
...     mz_min=100.0,
...     mz_max=200.0,
...     norm_tic=True,
...     save_fig=True,
...     save_path="../output_files/plots"
... )
>>> plotter = PlotPeaks(config)
>>> plotter.load_data()
>>> plotter.plot_overlay()
>>> plotter.plot_cumulative()
__init__(config=None)[source]

Initialize PlotPeaks.

Parameters:

config (PlotPeaksConfig, optional) – Configuration object. If None, must set attributes manually.

files: List[str]
grouped_curves: Dict[str, List[Tuple[ndarray, ndarray]]]
set_group_inference(func)[source]

Set custom group inference function.

Parameters:

func (callable) – Function that takes a file path and returns group label.

static load_window(file_path, mz_min, mz_max, norm_tic=False)[source]

Read one spectrum and return (m/z, intensity) in the requested window.

Parameters:
  • file_path (str) – Path to a tab or comma-separated spectrum with columns for m/z and intensity. Column names are case-insensitive and support variations: - m/z: “mz”, “m/z”, “M/Z”, “MZ”, “Mz” - intensity: “intensity”, “Intensity”, “INTENSITY”, “int”, “Int”

  • mz_min (float) – Inclusive m/z window to extract.

  • mz_max (float) – Inclusive m/z window to extract.

  • norm_tic (bool, default False) – If True, normalize intensities by total ion count (sum to 1).

Returns:

  • mz (np.ndarray)

  • inten (np.ndarray) – Intensities scaled by 1e6 (to keep values readable on plots).

Return type:

Tuple[ndarray, ndarray]

load_data()[source]

Load all files matching the pattern and group them by inferred labels.

Raises:

RuntimeError – If no files match the pattern.

Return type:

None

get_group_counts()[source]

Get counts of spectra per group.

Returns:

Dictionary with group names as keys and counts as values.

Return type:

dict

plot_overlay(ax=None, show=True)[source]

Plot overlapping spectra with optional median curves.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure.

  • show (bool, default True) – If True, call plt.show() at the end.

Returns:

The figure object.

Return type:

matplotlib.figure.Figure

plot_cumulative(ax=None, show=True)[source]

Plot cumulative intensity curves by group.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure.

  • show (bool, default True) – If True, call plt.show() at the end.

Returns:

The figure object.

Return type:

matplotlib.figure.Figure

plot_all()[source]

Convenience method to plot both overlay and cumulative plots.

Returns:

  • fig_overlay (matplotlib.figure.Figure) – The overlay plot figure.

  • fig_cumulative (matplotlib.figure.Figure) – The cumulative plot figure (or None if disabled).

Return type:

Tuple[Figure, Figure]

Parameters:

config (PlotPeaksConfig | None)

mioXpektron.plotting.plot_peaks.plot_overlapping_peaks(data_dir, file_pattern, mz_min, mz_max, norm_tic=False, alpha=0.18, bin_width=0.001, show_median=True, show_group_cumulative=True)[source]

Overlay spectra with two colors (Cancer vs Control) inferred from file names.

DEPRECATED: This function is maintained for backwards compatibility. Use PlotPeaks class for new code.

Parameters:
  • data_dir (str) – Directory containing spectra.

  • file_pattern (str) – Glob pattern (e.g., “*.txt”).

  • mz_min (float) – m/z window to visualize.

  • mz_max (float) – m/z window to visualize.

  • norm_tic (bool, default False) – Normalize each spectrum by its TIC prior to plotting.

  • alpha (float, default 0.18) – Line transparency for individual spectra.

  • bin_width (float, default 0.001) – Common grid step for interpolation (used for medians/cumulative plots).

  • show_median (bool, default True) – If True, overlay per-group median curves (thicker lines).

  • show_group_cumulative (bool, default True) – If True, plot per-group cumulative intensity curves on a separate figure.

Notes

  • Group detection is based on substrings in filenames: “_CC” (Cancer), “_CT” (Control).

  • Files without these markers are labeled “Unknown” and plotted in grey.

Examples

>>> # New recommended approach
>>> config = PlotPeaksConfig(
...     data_dir="data/spectra",
...     mz_min=100.0,
...     mz_max=200.0
... )
>>> plotter = PlotPeaks(config)
>>> plotter.load_data()
>>> plotter.plot_all()