MaldiAMRKit 0.15.0__tar.gz → 0.17.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0/MaldiAMRKit.egg-info}/PKG-INFO +3 -1
  2. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/MaldiAMRKit.egg-info/SOURCES.txt +4 -1
  3. {maldiamrkit-0.15.0/MaldiAMRKit.egg-info → maldiamrkit-0.17.0}/PKG-INFO +3 -1
  4. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/README.md +2 -0
  5. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/__init__.py +3 -3
  6. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/alignment/__init__.py +2 -0
  7. maldiamrkit-0.17.0/maldiamrkit/alignment/peak_align.py +123 -0
  8. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/__init__.py +7 -1
  9. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/dataset_layouts.py +41 -0
  10. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/input_layouts.py +59 -6
  11. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/loader.py +2 -0
  12. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/dataset.py +96 -0
  13. maldiamrkit-0.17.0/maldiamrkit/detection/__init__.py +12 -0
  14. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/detection/peak_detector.py +271 -1
  15. maldiamrkit-0.17.0/maldiamrkit/detection/peaklist.py +469 -0
  16. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/differential/stats.py +8 -1
  17. maldiamrkit-0.17.0/tests/test_peaklist.py +334 -0
  18. maldiamrkit-0.15.0/maldiamrkit/detection/__init__.py +0 -5
  19. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/LICENSE +0 -0
  20. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/MaldiAMRKit.egg-info/dependency_links.txt +0 -0
  21. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/MaldiAMRKit.egg-info/entry_points.txt +0 -0
  22. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/MaldiAMRKit.egg-info/requires.txt +0 -0
  23. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/MaldiAMRKit.egg-info/top_level.txt +0 -0
  24. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/_compat.py +0 -0
  25. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/alignment/raw_warping.py +0 -0
  26. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/alignment/strategies.py +0 -0
  27. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/alignment/warping.py +0 -0
  28. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/cli.py +0 -0
  29. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/README.md +0 -0
  30. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v1.0.yaml +0 -0
  31. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v1.1.yaml +0 -0
  32. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v1.2.yaml +0 -0
  33. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v1.3.yaml +0 -0
  34. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v10.0.yaml +0 -0
  35. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v11.0.yaml +0 -0
  36. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v12.0.yaml +0 -0
  37. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v13.1.yaml +0 -0
  38. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v14.0.yaml +0 -0
  39. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v15.0.yaml +0 -0
  40. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v16.0.yaml +0 -0
  41. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v2.0.yaml +0 -0
  42. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v4.0.yaml +0 -0
  43. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v5.0.yaml +0 -0
  44. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v6.0.yaml +0 -0
  45. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v7.1.yaml +0 -0
  46. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v8.0.yaml +0 -0
  47. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v8.1.yaml +0 -0
  48. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/eucast/eucast_v9.0.yaml +0 -0
  49. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/breakpoints/example.yaml +0 -0
  50. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/builder.py +0 -0
  51. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/duplicates.py +0 -0
  52. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/data/site_info.py +0 -0
  53. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/differential/__init__.py +0 -0
  54. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/differential/analysis.py +0 -0
  55. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/differential/plots.py +0 -0
  56. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/drift/__init__.py +0 -0
  57. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/drift/monitor.py +0 -0
  58. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/drift/pca_drift.py +0 -0
  59. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/drift/peak_drift.py +0 -0
  60. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/drift/plots.py +0 -0
  61. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/drift/reference.py +0 -0
  62. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/evaluation/__init__.py +0 -0
  63. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/evaluation/label_encoder.py +0 -0
  64. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/evaluation/metrics.py +0 -0
  65. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/evaluation/mic_regression.py +0 -0
  66. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/evaluation/splitting.py +0 -0
  67. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/filters.py +0 -0
  68. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/io/__init__.py +0 -0
  69. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/io/mic.py +0 -0
  70. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/io/readers.py +0 -0
  71. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/__init__.py +0 -0
  72. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/binning.py +0 -0
  73. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/merging.py +0 -0
  74. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/pipeline.py +0 -0
  75. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/preprocessing_pipeline.py +0 -0
  76. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/quality.py +0 -0
  77. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/preprocessing/transformers.py +0 -0
  78. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/py.typed +0 -0
  79. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/similarity/__init__.py +0 -0
  80. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/similarity/clustering.py +0 -0
  81. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/similarity/metrics.py +0 -0
  82. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/similarity/pairwise.py +0 -0
  83. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/similarity/plots.py +0 -0
  84. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/spectrum.py +0 -0
  85. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/susceptibility/__init__.py +0 -0
  86. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/susceptibility/breakpoint.py +0 -0
  87. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/susceptibility/label_encoder.py +0 -0
  88. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/susceptibility/mic_encoder.py +0 -0
  89. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/visualization/__init__.py +0 -0
  90. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/visualization/_common.py +0 -0
  91. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/visualization/alignment_plots.py +0 -0
  92. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/visualization/exploratory_plots.py +0 -0
  93. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/visualization/peak_plots.py +0 -0
  94. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/maldiamrkit/visualization/spectrum_plots.py +0 -0
  95. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/pyproject.toml +0 -0
  96. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/requirements-batch.txt +0 -0
  97. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/requirements-dev.txt +0 -0
  98. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/requirements-docs.txt +0 -0
  99. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/requirements.txt +0 -0
  100. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/setup.cfg +0 -0
  101. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/tests/test_compat.py +0 -0
  102. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/tests/test_numerical_edge_cases.py +0 -0
  103. {maldiamrkit-0.15.0 → maldiamrkit-0.17.0}/tests/test_peak_detector.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MaldiAMRKit
3
- Version: 0.15.0
3
+ Version: 0.17.0
4
4
  Summary: A comprehensive toolkit for MALDI-TOF mass spectrometry data preprocessing for antimicrobial resistance (AMR) prediction purposes
5
5
  Author-email: Ettore Rocchi <ettore.rocchi3@unibo.it>
6
6
  License-Expression: MIT
@@ -135,6 +135,7 @@ pip install -e .[dev]
135
135
  - **AMR Metrics**: VME, ME, sensitivity, specificity, categorical agreement, and `amr_classification_report` following EUCAST conventions
136
136
  - **`mic_regression_report`**: RMSE in log2 dilutions, essential agreement (±1 dilution), and categorical agreement after re-binning to S/I/R - the regression counterpart to `amr_classification_report`
137
137
  - **Stratified Splitting**: Species-drug stratified and case-based (patient-grouped) splitting to prevent data leakage
138
+ - **Group-Aware CV**: `MaldiSet.groups` / `isolate_ids()` derive per-isolate labels (DRIAMS `_MALDI<N>` replicates stripped by default, or read from a metadata column) to pass as `groups=` to splitters like `StratifiedGroupKFold`, keeping replicates of one isolate on the same side of a split
138
139
 
139
140
  ### Susceptibility (MIC encoding & breakpoints)
140
141
  - **`MICEncoder`**: Convert raw MIC strings into `log2(MIC)` regression targets plus S/I/R categories and ATU flags in a single pass
@@ -152,6 +153,7 @@ pip install -e .[dev]
152
153
 
153
154
  ### Data Management
154
155
  - **Dataset Building & Loading**: `DatasetBuilder` and `DatasetLoader` with pluggable layout adapters (`FlatLayout`, `BrukerTreeLayout`, `DRIAMSLayout`, `MARISMaLayout`)
156
+ - **Replicate Collapsing**: `DRIAMSLayout(collapse_replicates=True)` merges technical replicates (`_MALDI<N>`) to one row per isolate at load time via the active `duplicate_strategy`
155
157
  - **Bruker Format Support**: Read Bruker flexAnalysis binary data (fid/1r + acqus) natively via `read_spectrum()` on directories
156
158
  - **MIC Parsing**: `parse_mic_column()` for parsing MIC strings with qualifiers and European decimals
157
159
  - **Composable Filters**: `SpeciesFilter`, `DrugFilter`, `QualityFilter`, `MetadataFilter` combinable with `&`/`|`/`~` operators
@@ -19,6 +19,7 @@ maldiamrkit/filters.py
19
19
  maldiamrkit/py.typed
20
20
  maldiamrkit/spectrum.py
21
21
  maldiamrkit/alignment/__init__.py
22
+ maldiamrkit/alignment/peak_align.py
22
23
  maldiamrkit/alignment/raw_warping.py
23
24
  maldiamrkit/alignment/strategies.py
24
25
  maldiamrkit/alignment/warping.py
@@ -52,6 +53,7 @@ maldiamrkit/data/breakpoints/eucast/eucast_v8.1.yaml
52
53
  maldiamrkit/data/breakpoints/eucast/eucast_v9.0.yaml
53
54
  maldiamrkit/detection/__init__.py
54
55
  maldiamrkit/detection/peak_detector.py
56
+ maldiamrkit/detection/peaklist.py
55
57
  maldiamrkit/differential/__init__.py
56
58
  maldiamrkit/differential/analysis.py
57
59
  maldiamrkit/differential/plots.py
@@ -94,4 +96,5 @@ maldiamrkit/visualization/peak_plots.py
94
96
  maldiamrkit/visualization/spectrum_plots.py
95
97
  tests/test_compat.py
96
98
  tests/test_numerical_edge_cases.py
97
- tests/test_peak_detector.py
99
+ tests/test_peak_detector.py
100
+ tests/test_peaklist.py
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MaldiAMRKit
3
- Version: 0.15.0
3
+ Version: 0.17.0
4
4
  Summary: A comprehensive toolkit for MALDI-TOF mass spectrometry data preprocessing for antimicrobial resistance (AMR) prediction purposes
5
5
  Author-email: Ettore Rocchi <ettore.rocchi3@unibo.it>
6
6
  License-Expression: MIT
@@ -135,6 +135,7 @@ pip install -e .[dev]
135
135
  - **AMR Metrics**: VME, ME, sensitivity, specificity, categorical agreement, and `amr_classification_report` following EUCAST conventions
136
136
  - **`mic_regression_report`**: RMSE in log2 dilutions, essential agreement (±1 dilution), and categorical agreement after re-binning to S/I/R - the regression counterpart to `amr_classification_report`
137
137
  - **Stratified Splitting**: Species-drug stratified and case-based (patient-grouped) splitting to prevent data leakage
138
+ - **Group-Aware CV**: `MaldiSet.groups` / `isolate_ids()` derive per-isolate labels (DRIAMS `_MALDI<N>` replicates stripped by default, or read from a metadata column) to pass as `groups=` to splitters like `StratifiedGroupKFold`, keeping replicates of one isolate on the same side of a split
138
139
 
139
140
  ### Susceptibility (MIC encoding & breakpoints)
140
141
  - **`MICEncoder`**: Convert raw MIC strings into `log2(MIC)` regression targets plus S/I/R categories and ATU flags in a single pass
@@ -152,6 +153,7 @@ pip install -e .[dev]
152
153
 
153
154
  ### Data Management
154
155
  - **Dataset Building & Loading**: `DatasetBuilder` and `DatasetLoader` with pluggable layout adapters (`FlatLayout`, `BrukerTreeLayout`, `DRIAMSLayout`, `MARISMaLayout`)
156
+ - **Replicate Collapsing**: `DRIAMSLayout(collapse_replicates=True)` merges technical replicates (`_MALDI<N>`) to one row per isolate at load time via the active `duplicate_strategy`
155
157
  - **Bruker Format Support**: Read Bruker flexAnalysis binary data (fid/1r + acqus) natively via `read_spectrum()` on directories
156
158
  - **MIC Parsing**: `parse_mic_column()` for parsing MIC strings with qualifiers and European decimals
157
159
  - **Composable Filters**: `SpeciesFilter`, `DrugFilter`, `QualityFilter`, `MetadataFilter` combinable with `&`/`|`/`~` operators
@@ -78,6 +78,7 @@ pip install -e .[dev]
78
78
  - **AMR Metrics**: VME, ME, sensitivity, specificity, categorical agreement, and `amr_classification_report` following EUCAST conventions
79
79
  - **`mic_regression_report`**: RMSE in log2 dilutions, essential agreement (±1 dilution), and categorical agreement after re-binning to S/I/R - the regression counterpart to `amr_classification_report`
80
80
  - **Stratified Splitting**: Species-drug stratified and case-based (patient-grouped) splitting to prevent data leakage
81
+ - **Group-Aware CV**: `MaldiSet.groups` / `isolate_ids()` derive per-isolate labels (DRIAMS `_MALDI<N>` replicates stripped by default, or read from a metadata column) to pass as `groups=` to splitters like `StratifiedGroupKFold`, keeping replicates of one isolate on the same side of a split
81
82
 
82
83
  ### Susceptibility (MIC encoding & breakpoints)
83
84
  - **`MICEncoder`**: Convert raw MIC strings into `log2(MIC)` regression targets plus S/I/R categories and ATU flags in a single pass
@@ -95,6 +96,7 @@ pip install -e .[dev]
95
96
 
96
97
  ### Data Management
97
98
  - **Dataset Building & Loading**: `DatasetBuilder` and `DatasetLoader` with pluggable layout adapters (`FlatLayout`, `BrukerTreeLayout`, `DRIAMSLayout`, `MARISMaLayout`)
99
+ - **Replicate Collapsing**: `DRIAMSLayout(collapse_replicates=True)` merges technical replicates (`_MALDI<N>`) to one row per isolate at load time via the active `duplicate_strategy`
98
100
  - **Bruker Format Support**: Read Bruker flexAnalysis binary data (fid/1r + acqus) natively via `read_spectrum()` on directories
99
101
  - **MIC Parsing**: `parse_mic_column()` for parsing MIC strings with qualifiers and European decimals
100
102
  - **Composable Filters**: `SpeciesFilter`, `DrugFilter`, `QualityFilter`, `MetadataFilter` combinable with `&`/`|`/`~` operators
@@ -5,8 +5,8 @@ Subpackage guide
5
5
  - ``maldiamrkit.data`` -- DatasetBuilder, DatasetLoader, InputLayout, DatasetLayout
6
6
  - ``maldiamrkit.filters`` -- SpeciesFilter, DrugFilter, QualityFilter, MetadataFilter
7
7
  - ``maldiamrkit.preprocessing`` -- PreprocessingPipeline, transformers, binning, quality, merging
8
- - ``maldiamrkit.alignment`` -- Warping, RawWarping, create_raw_input, AlignmentStrategy, AlignmentMethod
9
- - ``maldiamrkit.detection`` -- MaldiPeakDetector
8
+ - ``maldiamrkit.alignment`` -- Warping, RawWarping, create_raw_input, align_peaks, AlignmentStrategy, AlignmentMethod
9
+ - ``maldiamrkit.detection`` -- MaldiPeakDetector, PeakSet, PeakList, create_peakset_input
10
10
  - ``maldiamrkit.evaluation`` -- AMR metrics (binary + mic_regression_report), splitting (LabelEncoder moved to susceptibility)
11
11
  - ``maldiamrkit.susceptibility`` -- MICEncoder, BreakpointTable, BreakpointResult, LabelEncoder
12
12
  - ``maldiamrkit.similarity`` -- spectral_distance, pairwise_distances, clustering, plots
@@ -27,7 +27,7 @@ Examples
27
27
  from .dataset import MaldiSet
28
28
  from .spectrum import MaldiSpectrum
29
29
 
30
- __version__ = "0.15.0"
30
+ __version__ = "0.17.0"
31
31
  __author__ = "Ettore Rocchi"
32
32
 
33
33
  __all__ = [
@@ -1,5 +1,6 @@
1
1
  """Spectral alignment and warping transformers."""
2
2
 
3
+ from .peak_align import align_peaks
3
4
  from .raw_warping import RawWarping, create_raw_input
4
5
  from .strategies import AlignmentMethod, AlignmentStrategy
5
6
  from .warping import Warping
@@ -9,5 +10,6 @@ __all__ = [
9
10
  "AlignmentStrategy",
10
11
  "RawWarping",
11
12
  "Warping",
13
+ "align_peaks",
12
14
  "create_raw_input",
13
15
  ]
@@ -0,0 +1,123 @@
1
+ """Fit-free peak-m/z alignment for peak sets.
2
+
3
+ :func:`align_peaks` warps the m/z positions of a :class:`~maldiamrkit.detection.PeakSet`
4
+ onto a reference peak list using the shared alignment strategies. It is
5
+ **fit-free**: the reference peaks are supplied by the caller, which is expected
6
+ to estimate them on the training split only (in the consumer's fold). The
7
+ intensities are carried through unchanged.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import numpy as np
13
+
14
+ from ..detection.peaklist import PeakSet
15
+ from .strategies import ALIGNMENT_REGISTRY, AlignmentMethod, AlignmentStrategy
16
+
17
+
18
+ def _build_strategy(
19
+ method: AlignmentMethod,
20
+ max_shift_da: float,
21
+ n_segments: int,
22
+ smooth_sigma: float,
23
+ lowess_frac: float,
24
+ lowess_it: int,
25
+ ) -> AlignmentStrategy:
26
+ """Instantiate the requested alignment strategy with its parameters."""
27
+ cls = ALIGNMENT_REGISTRY[method]
28
+ if method in (AlignmentMethod.shift, AlignmentMethod.linear):
29
+ return cls(max_shift=max_shift_da)
30
+ if method == AlignmentMethod.piecewise:
31
+ return cls(
32
+ n_segments=n_segments, smooth_sigma=smooth_sigma, max_shift=max_shift_da
33
+ )
34
+ if method == AlignmentMethod.quadratic:
35
+ return cls(max_shift=max_shift_da, degree=2)
36
+ if method == AlignmentMethod.cubic:
37
+ return cls(max_shift=max_shift_da, degree=3)
38
+ if method == AlignmentMethod.lowess:
39
+ return cls(max_shift=max_shift_da, frac=lowess_frac, it=lowess_it)
40
+ return cls()
41
+
42
+
43
+ def align_peaks(
44
+ peaks: PeakSet,
45
+ ref_peaks_mz: np.ndarray,
46
+ *,
47
+ method: str | AlignmentMethod = "shift",
48
+ max_shift_da: float = 50.0,
49
+ n_segments: int = 5,
50
+ smooth_sigma: float = 2.0,
51
+ lowess_frac: float = 0.3,
52
+ lowess_it: int = 3,
53
+ ) -> PeakSet:
54
+ """Align a peak set's m/z positions to a reference peak list.
55
+
56
+ Fit-free: ``ref_peaks_mz`` is supplied by the caller. Builds the requested
57
+ warping transform from the matched (sample, reference) peak pairs and applies
58
+ it to the peak m/z; intensities are unchanged.
59
+
60
+ Parameters
61
+ ----------
62
+ peaks : PeakSet
63
+ Peak set to align.
64
+ ref_peaks_mz : array-like
65
+ Reference peak m/z positions to align to.
66
+ method : {"shift", "linear", "piecewise", "quadratic", "cubic", "lowess"}, default="shift"
67
+ Warping strategy. ``"dtw"`` is unsupported for peak sets because it
68
+ resamples onto a dense grid and would destroy the set structure.
69
+ max_shift_da : float, default=50.0
70
+ Maximum allowed shift in Daltons (used by the shift fallback and the
71
+ rigid/linear strategies).
72
+ n_segments : int, default=5
73
+ Number of segments for ``method="piecewise"``.
74
+ smooth_sigma : float, default=2.0
75
+ Gaussian smoothing (Da) for piecewise transitions.
76
+ lowess_frac : float, default=0.3
77
+ LOWESS bandwidth for ``method="lowess"``.
78
+ lowess_it : int, default=3
79
+ LOWESS robustness iterations for ``method="lowess"``.
80
+
81
+ Returns
82
+ -------
83
+ PeakSet
84
+ A new peak set with warped m/z and unchanged intensities.
85
+
86
+ Raises
87
+ ------
88
+ ValueError
89
+ If ``method="dtw"``.
90
+
91
+ Notes
92
+ -----
93
+ ``"shift"``, ``"linear"``, ``"quadratic"``, ``"cubic"`` and ``"lowess"``
94
+ operate directly on the matched peak m/z and are the well-behaved choices
95
+ for sparse peak sets. ``"piecewise"`` derives its Gaussian smoothing width
96
+ from the median spacing of the input points; on a sparse peak set that
97
+ spacing is large, so the smoothing is effectively inactive and the warp
98
+ reduces to an unsmoothed piecewise-linear transform.
99
+ """
100
+ method = AlignmentMethod(method)
101
+ if method == AlignmentMethod.dtw:
102
+ raise ValueError(
103
+ "method='dtw' is not supported for peak sets (it resamples onto a "
104
+ "dense grid). Use 'shift', 'linear', 'piecewise', 'quadratic', "
105
+ "'cubic', or 'lowess'."
106
+ )
107
+
108
+ ref_peaks_mz = np.asarray(ref_peaks_mz, dtype=float).ravel()
109
+ if peaks.n_peaks == 0 or ref_peaks_mz.size == 0:
110
+ return PeakSet(peaks.mz.copy(), peaks.intensity.copy())
111
+
112
+ strategy = _build_strategy(
113
+ method, max_shift_da, n_segments, smooth_sigma, lowess_frac, lowess_it
114
+ )
115
+ new_mz, _ = strategy.align_raw(
116
+ peaks.mz,
117
+ peaks.intensity,
118
+ peaks.mz,
119
+ ref_peaks_mz,
120
+ ref_peaks_mz,
121
+ np.ones_like(ref_peaks_mz),
122
+ )
123
+ return PeakSet(np.asarray(new_mz, dtype=float), peaks.intensity.copy())
@@ -12,7 +12,12 @@ Submodules
12
12
  """
13
13
 
14
14
  from .builder import BuildReport, DatasetBuilder, ProcessingHandler
15
- from .dataset_layouts import DatasetLayout, DRIAMSLayout, MARISMaLayout
15
+ from .dataset_layouts import (
16
+ DatasetLayout,
17
+ DRIAMSLayout,
18
+ MARISMaLayout,
19
+ strip_driams_replicate,
20
+ )
16
21
  from .duplicates import DuplicateStrategy
17
22
  from .input_layouts import BrukerTreeLayout, FlatLayout, InputLayout
18
23
  from .loader import DatasetLoader
@@ -36,6 +41,7 @@ __all__ = [
36
41
  "DatasetLayout",
37
42
  "DRIAMSLayout",
38
43
  "MARISMaLayout",
44
+ "strip_driams_replicate",
39
45
  # Builder
40
46
  "DatasetBuilder",
41
47
  "ProcessingHandler",
@@ -50,6 +50,21 @@ _AUTO: _Sentinel = _Sentinel()
50
50
  # itself is user-supplied and doesn't rely on this pattern.
51
51
  _DRIAMS_MALDI_SUFFIX_RE = re.compile(r"^(?P<stem>.+)_MALDI\d+$")
52
52
 
53
+ # Suffix-only variant for stripping the replicate tag to recover the
54
+ # underlying biological-sample ID. Used by the ``collapse_replicates``
55
+ # shortcut and as the canonical isolate key for group-aware CV.
56
+ _DRIAMS_REPLICATE_SUFFIX_RE = re.compile(r"_MALDI\d+$")
57
+
58
+
59
+ def strip_driams_replicate(sample_id: str) -> str:
60
+ """Return the underlying DRIAMS sample ID by removing the ``_MALDI<N>`` tag.
61
+
62
+ ``"abc123_MALDI2" -> "abc123"``. IDs without the suffix are returned
63
+ unchanged. Use as a group key (one value per biological isolate) for
64
+ group-aware cross-validation.
65
+ """
66
+ return _DRIAMS_REPLICATE_SUFFIX_RE.sub("", str(sample_id))
67
+
53
68
 
54
69
  def _warn_on_likely_replicates(ids: pd.Series) -> None:
55
70
  """Emit a one-shot warning when DRIAMS IDs look like shared-sample replicates.
@@ -93,6 +108,15 @@ _STAGE_PRIORITY = re.compile(r"^binned_\d+$")
93
108
  class DatasetLayout(ABC):
94
109
  """Abstract adapter for navigating and loading from a dataset."""
95
110
 
111
+ # How to recover the per-isolate group key from a spectrum ID, consumed by
112
+ # :meth:`~maldiamrkit.dataset.MaldiSet.isolate_ids` / ``.groups`` for
113
+ # group-aware CV. A layout declares at most one: ``replicate_pattern`` (a
114
+ # regex suffix to strip off each ID) or ``isolate_column`` (a metadata
115
+ # column already holding the isolate ID). Both ``None`` means the layout
116
+ # has no replicate structure to collapse.
117
+ replicate_pattern: re.Pattern[str] | None = None
118
+ isolate_column: str | None = None
119
+
96
120
  @abstractmethod
97
121
  def discover_metadata(self) -> pd.DataFrame:
98
122
  """Load metadata, return DataFrame with ``'ID'`` column."""
@@ -183,6 +207,11 @@ class DRIAMSLayout(DatasetLayout):
183
207
  ``id_transform`` is ``None``, pointing at this kwarg; the
184
208
  warning can be silenced by passing ``id_transform=str`` if
185
209
  the per-replicate semantics are intentional.
210
+ collapse_replicates : bool, default=False
211
+ Convenience shortcut for ``id_transform=strip_driams_replicate``:
212
+ collapse DRIAMS technical replicates (``_MALDI<N>``) to one row per
213
+ underlying isolate via the active ``duplicate_strategy``. Ignored when
214
+ an explicit ``id_transform`` is given (that always takes precedence).
186
215
  mz_min : float, default=2000.0
187
216
  Lower m/z edge to assign to bin index 0 when a ``binned_N/`` stage
188
217
  is loaded. Only consulted by :meth:`postprocess_spectrum`.
@@ -203,6 +232,10 @@ class DRIAMSLayout(DatasetLayout):
203
232
  already produces TIC=1.
204
233
  """
205
234
 
235
+ # DRIAMS encodes technical replicates as a ``_MALDI<N>`` suffix; stripping
236
+ # it yields the per-isolate group key for group-aware CV.
237
+ replicate_pattern: re.Pattern[str] | None = _DRIAMS_REPLICATE_SUFFIX_RE
238
+
206
239
  def __init__(
207
240
  self,
208
241
  dataset_dir: str | Path,
@@ -215,6 +248,7 @@ class DRIAMSLayout(DatasetLayout):
215
248
  spectrum_ext: str | _Sentinel = _AUTO,
216
249
  duplicate_strategy: str | DuplicateStrategy = DuplicateStrategy.first,
217
250
  id_transform: Callable[[str], str] | None = None,
251
+ collapse_replicates: bool = False,
218
252
  mz_min: float | _Sentinel = _AUTO,
219
253
  mz_max: float | _Sentinel = _AUTO,
220
254
  normalize_tic: bool = False,
@@ -273,6 +307,13 @@ class DRIAMSLayout(DatasetLayout):
273
307
  self.species_column = species_column
274
308
  self.year = str(year) if year is not None else None
275
309
  self.duplicate_strategy = DuplicateStrategy(duplicate_strategy)
310
+ # ``collapse_replicates=True`` is a friendly shortcut for
311
+ # ``id_transform=strip_driams_replicate``: DRIAMS technical replicates
312
+ # (``_MALDI<N>``) collapse to one row per underlying sample via the
313
+ # active duplicate_strategy. An explicit id_transform always wins.
314
+ if id_transform is None and collapse_replicates:
315
+ id_transform = strip_driams_replicate
316
+ self.collapse_replicates = bool(collapse_replicates)
276
317
  self.id_transform = id_transform
277
318
  self.normalize_tic = bool(normalize_tic)
278
319
 
@@ -71,7 +71,15 @@ class FlatLayout(InputLayout):
71
71
  id_column : str, default="ID"
72
72
  Column name for the spectrum identifier in the metadata.
73
73
  year_column : str or None
74
- Column to extract year from, or ``None`` for flat layout.
74
+ Column to extract year from. When ``None``, the year is instead
75
+ inferred from a four-digit input subfolder name (if any), so a
76
+ year-organised input directory still produces year subfolders in
77
+ the output. See :meth:`get_year`.
78
+ year_overrides : dict[str, str] or None
79
+ Optional explicit ``{spectrum_id: year}`` mapping. Useful when the
80
+ ``spectra_dir`` is itself flat but the years are known from another
81
+ source. Takes precedence over the inferred subfolder year,
82
+ but not over ``year_column``.
75
83
  """
76
84
 
77
85
  def __init__(
@@ -81,22 +89,55 @@ class FlatLayout(InputLayout):
81
89
  *,
82
90
  id_column: str = "ID",
83
91
  year_column: str | None = None,
92
+ year_overrides: dict[str, str] | None = None,
84
93
  ) -> None:
85
94
  self.spectra_dir = Path(spectra_dir)
86
95
  self.metadata_csv = Path(metadata_csv)
87
96
  self.id_column = id_column
88
97
  self.year_column = year_column
98
+ self.year_overrides = year_overrides
89
99
  self._year_map: dict[str, str] | None = None
100
+ self._folder_year_map: dict[str, str] = {}
90
101
 
91
102
  def discover_spectra(self) -> list[Path]:
92
- """Glob for ``.txt`` files, flat or with year subfolders."""
103
+ """Find ``.txt`` files, whether flat or in (possibly nested) subfolders.
104
+
105
+ Files are searched flat first, then one level down, then
106
+ recursively. Whenever a spectrum sits under a four-digit-year
107
+ folder, that year is recorded so it can serve as a fallback when
108
+ no ``year_column`` is supplied (see :meth:`get_year`).
109
+ """
93
110
  files = sorted(self.spectra_dir.glob("*.txt"))
94
111
  if not files:
95
112
  files = sorted(self.spectra_dir.glob("*/*.txt"))
113
+ if not files:
114
+ files = sorted(self.spectra_dir.rglob("*.txt"))
96
115
  if not files:
97
116
  raise ValueError(f"No .txt spectrum files found in {self.spectra_dir}")
117
+
118
+ self._folder_year_map = {}
119
+ for f in files:
120
+ year = self._infer_year_from_path(f)
121
+ if year is not None:
122
+ self._folder_year_map[self.get_id(f)] = year
98
123
  return files
99
124
 
125
+ def _infer_year_from_path(self, path: Path) -> str | None:
126
+ """Return a four-digit year taken from an input subfolder, if any.
127
+
128
+ Inspects the directory components between ``spectra_dir`` and the
129
+ file and returns the first that is a four-digit number. Returns
130
+ ``None`` for flat inputs or non-year folder names.
131
+ """
132
+ try:
133
+ relative = path.relative_to(self.spectra_dir)
134
+ except ValueError:
135
+ relative = path
136
+ for part in relative.parts[:-1]:
137
+ if len(part) == 4 and part.isdigit():
138
+ return part
139
+ return None
140
+
100
141
  def discover_metadata(self) -> pd.DataFrame:
101
142
  """Read metadata CSV and normalise the ID column."""
102
143
  meta = pd.read_csv(self.metadata_csv)
@@ -128,10 +169,22 @@ class FlatLayout(InputLayout):
128
169
  return spectrum_path.stem
129
170
 
130
171
  def get_year(self, spectrum_id: str) -> str | None:
131
- """Year from the metadata column, or ``None``."""
132
- if self._year_map is None:
133
- return None
134
- return self._year_map.get(spectrum_id)
172
+ """Resolve a spectrum's year.
173
+
174
+ Resolution order: the metadata ``year_column`` (authoritative when
175
+ set), then an explicit ``year_overrides`` entry, then the year
176
+ inferred from a four-digit input subfolder (see
177
+ :meth:`discover_spectra`); failing all three, ``None`` (flat layout).
178
+ """
179
+ if self._year_map is not None:
180
+ year = self._year_map.get(spectrum_id)
181
+ if year is not None:
182
+ return year
183
+ if self.year_overrides is not None:
184
+ year = self.year_overrides.get(spectrum_id)
185
+ if year is not None:
186
+ return year
187
+ return self._folder_year_map.get(spectrum_id)
135
188
 
136
189
 
137
190
  class BrukerTreeLayout(InputLayout):
@@ -157,6 +157,8 @@ class DatasetLoader:
157
157
  meta,
158
158
  aggregate_by=aggregate_by,
159
159
  verbose=self.verbose,
160
+ isolate_pattern=self.layout.replicate_pattern,
161
+ isolate_column=self.layout.isolate_column,
160
162
  )
161
163
 
162
164
  @staticmethod
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
+ import re
6
7
  import warnings
7
8
  from pathlib import Path
8
9
 
@@ -18,6 +19,14 @@ from .spectrum import MaldiSpectrum
18
19
 
19
20
  logger = logging.getLogger(__name__)
20
21
 
22
+ # DRIAMS encodes technical replicates of one isolate with a ``_MALDI<N>``
23
+ # suffix on the spectrum ID; stripping it recovers the per-isolate group key.
24
+ _DRIAMS_REPLICATE_RE = re.compile(r"_MALDI\d+$")
25
+
26
+ # Sentinel for isolate_ids() args: distinguishes "caller passed nothing"
27
+ # (inherit from the layout) from an explicit pattern/column.
28
+ _INHERIT = object()
29
+
21
30
 
22
31
  def _load_single_spectrum(
23
32
  path: Path,
@@ -96,6 +105,8 @@ class MaldiSet:
96
105
  bin_method: str | BinningMethod = BinningMethod.uniform,
97
106
  bin_kwargs: dict | None = None,
98
107
  verbose: bool = False,
108
+ isolate_pattern: str | re.Pattern | None = None,
109
+ isolate_column: str | None = None,
99
110
  ) -> None:
100
111
  self.spectra = spectra
101
112
 
@@ -121,6 +132,9 @@ class MaldiSet:
121
132
  self._bin_metadata: pd.DataFrame | None = None
122
133
  self._X_cache: pd.DataFrame | None = None
123
134
 
135
+ self._isolate_pattern = isolate_pattern
136
+ self._isolate_column = isolate_column
137
+
124
138
  self.verbose = verbose
125
139
  if verbose:
126
140
  logger.info("Dataset created: %d spectra", len(self.spectra))
@@ -481,6 +495,88 @@ class MaldiSet:
481
495
 
482
496
  return self.meta.loc[self.X.index, antibiotic]
483
497
 
498
+ def isolate_ids(
499
+ self,
500
+ *,
501
+ pattern: str | re.Pattern = _INHERIT,
502
+ column: str | None = _INHERIT,
503
+ ) -> pd.Series:
504
+ """Per-isolate group IDs aligned to the feature-matrix rows.
505
+
506
+ Technical replicates of one biological isolate share an underlying
507
+ sample. Recovering a per-isolate label lets you pass ``groups=`` to a
508
+ group-aware cross-validator so replicates of the same isolate never
509
+ span a train/test split (replicate leakage).
510
+
511
+ How the label is derived is resolved in this order:
512
+
513
+ 1. an explicit ``column=`` argument (read from metadata), else
514
+ 2. an explicit ``pattern=`` argument (regex suffix stripped off the ID),
515
+ else
516
+ 3. the ``isolate_column`` / ``replicate_pattern`` stamped by the source
517
+ layout when the set was loaded (e.g. ``_MALDI<N>`` for DRIAMS), else
518
+ 4. the DRIAMS ``_MALDI<N>`` suffix as a last-resort fallback.
519
+
520
+ If the resolved pattern strips nothing from any ID -- every spectrum
521
+ becomes its own group, giving no leakage protection -- a warning is
522
+ emitted pointing at ``pattern=`` / ``column=``.
523
+
524
+ Parameters
525
+ ----------
526
+ pattern : str or compiled regex, optional
527
+ Replicate suffix to strip from each spectrum ID. Overrides the
528
+ layout-stamped pattern; ignored if ``column`` is given.
529
+ column : str or None, optional
530
+ Read isolate IDs from this metadata column instead of deriving
531
+ them from the spectrum-ID index.
532
+
533
+ Returns
534
+ -------
535
+ pd.Series
536
+ Isolate IDs indexed by spectrum ID, aligned to ``self.X`` rows.
537
+ """
538
+ idx = self.X.index # ensures features are built and meta is aligned
539
+ if column is not _INHERIT and column is not None:
540
+ return self._isolate_from_column(idx, column)
541
+ if pattern is not _INHERIT:
542
+ return self._isolate_from_pattern(idx, pattern)
543
+ if self._isolate_column is not None:
544
+ return self._isolate_from_column(idx, self._isolate_column)
545
+ if self._isolate_pattern is not None:
546
+ return self._isolate_from_pattern(idx, self._isolate_pattern)
547
+ return self._isolate_from_pattern(idx, _DRIAMS_REPLICATE_RE)
548
+
549
+ def _isolate_from_column(self, idx: pd.Index, column: str) -> pd.Series:
550
+ if column not in self.meta.columns:
551
+ raise ValueError(f"column {column!r} not found in metadata")
552
+ return self.meta.loc[idx, column].astype(str).rename("isolate")
553
+
554
+ def _isolate_from_pattern(
555
+ self, idx: pd.Index, pattern: str | re.Pattern
556
+ ) -> pd.Series:
557
+ rx = re.compile(pattern) if isinstance(pattern, str) else pattern
558
+ out = pd.Series([rx.sub("", str(i)) for i in idx], index=idx, name="isolate")
559
+ if len(idx) and (out.to_numpy() == idx.astype(str).to_numpy()).all():
560
+ warnings.warn(
561
+ f"isolate group pattern {rx.pattern!r} stripped no replicate "
562
+ "suffix from any spectrum ID: every spectrum becomes its own "
563
+ "group, so group-aware CV gives no leakage protection. Pass an "
564
+ "explicit pattern= or column= matching this dataset's replicate "
565
+ "encoding.",
566
+ stacklevel=3,
567
+ )
568
+ return out
569
+
570
+ @property
571
+ def groups(self) -> np.ndarray:
572
+ """Isolate group labels (ndarray) aligned to ``X`` rows.
573
+
574
+ Convenience over :meth:`isolate_ids` using the replicate pattern stamped
575
+ by the source layout (or the DRIAMS fallback); pass directly as
576
+ ``groups=`` to a group-aware splitter (e.g. ``StratifiedGroupKFold``).
577
+ """
578
+ return self.isolate_ids().to_numpy()
579
+
484
580
  def to_csv(self, path: str | Path) -> None:
485
581
  """Export the feature matrix to CSV.
486
582
 
@@ -0,0 +1,12 @@
1
+ """Peak detection algorithms and transformers."""
2
+
3
+ from .peak_detector import MaldiPeakDetector, PeakMethod
4
+ from .peaklist import PeakList, PeakSet, create_peakset_input
5
+
6
+ __all__ = [
7
+ "MaldiPeakDetector",
8
+ "PeakMethod",
9
+ "PeakList",
10
+ "PeakSet",
11
+ "create_peakset_input",
12
+ ]