Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e263062
test
lutfia95 Apr 2, 2026
93a2482
logging
lutfia95 Apr 2, 2026
241946c
cpu
lutfia95 Apr 3, 2026
b28a678
cpu/gpu
lutfia95 Apr 3, 2026
971e85e
autog
lutfia95 Apr 5, 2026
8be7770
autog
lutfia95 Apr 5, 2026
d79f946
split
lutfia95 Apr 5, 2026
5e78883
training autog
lutfia95 Apr 5, 2026
f99f4c1
resume
lutfia95 Apr 6, 2026
ba03cbc
keep_config
lutfia95 Apr 6, 2026
f5a10f4
arch
lutfia95 Apr 6, 2026
0e2196e
use_fdr
lutfia95 Apr 6, 2026
0e0f132
Create README.md
lutfia95 Apr 6, 2026
3fa5ac1
weight_2
lutfia95 Apr 8, 2026
10105c5
path
lutfia95 Apr 8, 2026
1c78fd7
Update train_autogluon.py
lutfia95 Apr 8, 2026
1435da1
config_fix
lutfia95 Apr 8, 2026
119598f
outptus
lutfia95 Apr 9, 2026
85188ab
no_filter
lutfia95 Apr 9, 2026
fa6098c
impv
lutfia95 Apr 9, 2026
4c34d2c
imv_conf
lutfia95 Apr 10, 2026
366307e
instrument_type
lutfia95 Apr 12, 2026
81f3735
epoches
lutfia95 Apr 13, 2026
2b1078e
test
lutfia95 Apr 13, 2026
149e0db
fix_config
lutfia95 Apr 13, 2026
012e70b
cuda
lutfia95 Apr 13, 2026
ed93940
impv.changed.batch
lutfia95 Apr 20, 2026
d084ff6
Merge branch 'transformer_impv' of https://github.com/lutfia95/denois…
lutfia95 Apr 20, 2026
59cc01d
pytorch version error "weights_only"
lutfia95 Apr 23, 2026
0104100
ignore
lutfia95 Jun 29, 2026
efbcffd
Merge branch 'transformer_impv' of https://github.com/lutfia95/denois…
lutfia95 Jun 29, 2026
b9eba7c
edit
lutfia95 Jun 29, 2026
b5b16f7
split_aware_rec
lutfia95 Jun 29, 2026
35bbe8a
add_rec
lutfia95 Jun 29, 2026
2fe5d9d
recl_label
lutfia95 Jun 29, 2026
734f03a
check
lutfia95 Jun 29, 2026
7e3d42c
resume
lutfia95 Jul 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.csv
*.pyc
*.png
*.json
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# denoising
Spectra denoising
7 changes: 6 additions & 1 deletion configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ split:
train_fraction: 0.70
val_fraction: 0.15
test_fraction: 0.15
random_seed: 42
random_seed: 42
stratify_by_recali: true # preserve groups while matching the recali True/False ratio in every split
length_weight: true # if true, multiply the FDR weight by a clipped per-spectrum true/false ratio
length_weight_eps: 1.0 # smoothing so spectra with zero true or zero false labels stay finite
length_weight_min: 0.5 # lower clip for the true/false ratio factor
length_weight_max: 2.0 # upper clip for the true/false ratio factor
48 changes: 48 additions & 0 deletions configs/infer_transformer_imp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
input:
mgf_path: "/mnt/c/Users/yynk1/Downloads/denoising/B190717_12_HF_LS_IN_130_ECLP_DSSO_01_SCX23_hSAX05_rep1.mgf"
peak_list_file_name: "B190717_12_HF_LS_IN_130_ECLP_DSSO_01_SCX23_hSAX05_rep1.mgf" #PeakListFileName
recali: null # set true/false when the checkpoint was trained with use_recali_label: true

model:
checkpoint_path: "/mnt/c/Users/yynk1/Downloads/denoising/transformer_peak_classifier_imp_1pct_filtered_imp_03_threshold/best_model.pt"

features:
peak_feature_columns:
- "peak_feature_log_intensity"
- "peak_feature_relative_intensity"
- "peak_feature_mz_over_precursor"
- "peak_feature_delta_to_precursor"
- "peak_feature_delta_prev"
- "peak_feature_delta_next"
spectrum_feature_columns:
- "Charge"
- "exp m/z"
- "num_peaks"
- "tic"
use_instrument_label: false
use_recali_label: false
recali_label_source_column: "recali"
use_raw_peak_mz: true
raw_peak_mz_column: "mz_arr"
use_raw_peak_intensity: false
raw_peak_intensity_column: "int_arr"
sort_raw_peak_inputs_by_mz: true
broadcast_spectrum_features_to_peaks: false
normalize_peak_features: true
normalize_spectrum_features: true

evaluation:
use_checkpoint_threshold: true
threshold_for_binary_metrics: 0.3

runtime:
device: "cuda"
batch_size: 2

output:
output_dir: "/mnt/c/Users/yynk1/Downloads/denoising/inference_transformer_peak_classifier_imp"
write_filtered_mgf: true
write_dropped_peaks_report: true
dropped_peaks_report_name: "dropped_peaks.csv"
#default: drop low-probability peaks instead of writing zero-intensity peaks.
filtered_mgf_mode: "drop_below_threshold"
85 changes: 85 additions & 0 deletions configs/train_autogluon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
data:
train_path: "../data/splits_5pct_filtered_new/train.parquet"
val_path: "../data/splits_5pct_filtered_new/val.parquet"
test_path: "../data/splits_5pct_filtered_new/test.parquet"

target_column: "annotation_mask" # per-peak binary labels after flattening each spectrum into peak rows
use_training_weights: true # optionally repeat a spectrum weight onto each peak row for AutoGluon
weight_column: "weight"

output:
#output_dir: "../outputs/splits_6pct_filtered"
output_dir: "../outputs/autogluon_imp_5pct_recali_instrument"
predictor_subdir: "autogluon_predictor"
unique_predictor_subdir: true # create a new predictor directory each run to avoid "Learner is already fit"
save_metrics_summary: true
save_confusion_matrices: true
save_predictions: true
save_flattened_tables: false # can become very large; enable only when you need to inspect the flat peak table
save_fit_summary: true
enable_file_logging: true
log_file_name: "training.log"

features:
peak_feature_columns:
- "peak_feature_mz"
- "peak_feature_log_intensity"
- "peak_feature_relative_intensity"
- "peak_feature_mz_over_precursor"
- "peak_feature_delta_to_precursor"
- "peak_feature_delta_prev"
- "peak_feature_delta_next"

spectrum_feature_columns:
- "Charge"
- "exp m/z"
- "num_peaks"
- "tic"

use_raw_peak_mz: true
raw_peak_mz_column: "mz_arr"
use_raw_peak_intensity: true
raw_peak_intensity_column: "int_arr"
sort_raw_peak_inputs_by_mz: true
broadcast_spectrum_features_to_peaks: true
normalize_peak_features: true
normalize_spectrum_features: true
normalization_fit_split: "train"

autogluon:
label: "annotation_mask"
problem_type: "binary"
eval_metric: "average_precision" # good fit for imbalanced signal-vs-noise prediction
presets: "high_quality" # strong baseline without the very large cost of best_quality
time_limit: 14400 # 4h default budget for a serious baseline on a large table
verbosity: 2
positive_class: 1
sample_weight_column: null
weight_evaluation: false
dynamic_stacking: false # avoid expensive DyStack / Ray side work unless you really need it
num_stack_levels: 0
use_bag_holdout: false # valid when tuning_data is passed alongside bagging
fit_weighted_ensemble: true
save_bag_folds: true
save_leaderboard: true
save_feature_importance: true
feature_importance_subsample_size: 50000
feature_importance_num_shuffle_sets: 5
feature_importance_time_limit: 600

evaluation:
primary_metric: "pr_auc"
threshold_for_binary_metrics: 0.3

report_metrics:
- "pr_auc"
- "roc_auc"
- "f1"
- "mcc"
- "precision"
- "recall"

retained_peak_fractions:
- 0.3
- 0.5
- 0.7
109 changes: 109 additions & 0 deletions configs/train_mlp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
data:
train_path: "../data/splits/train.parquet"
val_path: "../data/splits/val.parquet"
test_path: "../data/splits/test.parquet"

target_column: "annotation_mask" # peak-level labels where True means signal and False means noise
use_training_weights: false # stage 1 MLP ignores FDR-derived weights (this will be used only in the Loss)
weight_column: "weight" # column name for training weights used in later stages

output:
output_dir: "outputs/mlp_baseline" # directory where training artifacts will be saved
save_best_model: true # save the best checkpoint for later inference
save_epoch_history: true # save epoch-by-epoch metrics as a table
save_metrics_summary: true # save final validation and test metrics
save_plots: true # save training curves such as loss and PR-AUC
save_confusion_matrices: true # save confusion matrices for validation and test
device_verbose: true # print device infos! important for GPU/used-CPUs :)
enable_file_logging: true # mirror all console training output into a log file
log_file_name: "training.log" # written inside the model output directory

features:
peak_feature_columns:
- "peak_feature_mz"
- "peak_feature_log_intensity"
- "peak_feature_relative_intensity"
- "peak_feature_mz_over_precursor"
- "peak_feature_delta_to_precursor" # precursor m/z minus peak m/z
- "peak_feature_delta_prev" # m/z difference to the previous peak after sorting
- "peak_feature_delta_next" # m/z difference to the next peak after sorting

spectrum_feature_columns:
- "Charge" # precursor charge state
- "exp m/z" # precursor experimental m/z
- "num_peaks" # number of peaks in the processed spectrum
- "tic" # total ion current computed as the sum of intensities

use_raw_peak_mz: false # optional raw per-peak m/z input from mz_arr
raw_peak_mz_column: "mz_arr"
use_raw_peak_intensity: false # optional raw per-peak intensity input from int_arr
raw_peak_intensity_column: "int_arr"
sort_raw_peak_inputs_by_mz: true # keep raw arrays aligned with sorted engineered peak features
broadcast_spectrum_features_to_peaks: true # repeat spectrum-level features for every peak before concatenation
normalize_peak_features: true # apply feature normalization to peak-level inputs
normalize_spectrum_features: true # apply feature normalization to spectrum-level inputs
normalization_fit_split: "train" # fit normalization statistics only on the training split

model:
name: "mlp_peak_classifier"

mlp:
hidden_dims: [128, 64] # hidden layer sizes of the MLP
dropout: 0.1 # dropout probability used between hidden layers
activation: "gelu" # non-linear activation function used in the MLP
use_layer_norm: true # apply layer normalization in hidden layers
output_dim: 1 # one output logit per peak

training:
seed: 42 # random seed for reproducible training behavior
batch_size: 256 # GPU-oriented default; reduce only if you hit CUDA OOM
num_workers: 16 # enough workers to keep the GPU fed without excessive loader overhead
max_epochs: 50 # maximum number of training epochs
cpu_num_threads: 8 # leave most CPU capacity available for dataloader workers and system overhead
cpu_num_interop_threads: 2 # low interop setting usually works better for single-GPU training
dataloader_prefetch_factor: 4 # let each worker prepare several batches ahead
dataloader_persistent_workers: true # keep workers alive across epochs
dataloader_pin_memory: true # force pinned host memory for faster CUDA transfers
enable_amp: true # use mixed precision on CUDA
compile_model: true # use torch.compile when supported by the local PyTorch build
cache_dataset_in_memory: true # preprocess arrays once instead of rebuilding them per sample

optimizer:
name: "adamw" # optimizer used for parameter updates
learning_rate: 0.001 # optimizer learning rate
weight_decay: 0.0001 # L2-style regularization strength

scheduler:
enabled: false # no learning-rate scheduler is used in the first baseline

gradient_clip_norm: 1.0 # clip gradients to avoid unstable large updates

loss:
name: "bce_with_logits" # binary cross-entropy loss applied to raw logits
use_pos_weight: true # compensate for class imbalance using positive-class weighting
pos_weight: null # if null, estimate the positive-class weight from the training split
reduction: "none" # keep unreduced loss values so masking and weighting stay flexible

early_stopping:
enabled: true # stop training early when validation performance stops improving
monitor: "val_pr_auc" # validation metric used to decide early stopping
mode: "max" # higher values of the monitored metric are better
patience: 7 # number of bad epochs allowed before stopping
min_delta: 0.0001 # minimum improvement required to count as progress

evaluation:
primary_metric: "pr_auc" # main metric used to compare models
threshold_for_binary_metrics: 0.5 # probability threshold for binary predictions

report_metrics:
- "pr_auc" # precision-recall area under the curve
- "roc_auc" # ROC area under the curve
- "f1" # harmonic mean of precision and recall
- "mcc" # Matthews correlation coefficient for imbalanced binary classification
- "precision" # fraction of predicted positives that are correct
- "recall" # fraction of true positives that are recovered

retained_peak_fractions:
- 0.3 # evaluate how well the model keeps signal when retaining the top 30% of peaks
- 0.5 # evaluate how well the model keeps signal when retaining the top 50% of peaks
- 0.7 # evaluate how well the model keeps signal when retaining the top 70% of peaks
129 changes: 129 additions & 0 deletions configs/train_transformer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
data:
train_path: "../data/splits_1pct_filtered/train.parquet"
val_path: "../data/splits_1pct_filtered/val.parquet"
test_path: "../data/splits_1pct_filtered/test.parquet"
# train_path: "../data/splits_6pct_filtered/train.parquet"
# val_path: "../data/splits_6pct_filtered/val.parquet"
# test_path: "../data/splits_6pct_filtered/test.parquet"
#train_path: "../data/demo/train_ten_rows.parquet"
#val_path: "../data/demo/val_ten_rows.parquet"
#test_path: "../data/demo/test_ten_rows.parquet"

target_column: "annotation_mask" # Column containing the per-peak binary targets.
#loss_matrix = loss_matrix * weights otherwise:
#FDR weights are already constrained to a safe range in src/features/fdr_weights.py
#the caution about “very large weights” does not really apply to the used-setup, we can keep it! ;)
use_training_weights: true # Enables per-example weighting in the loss if available.
weight_column: "weight" # Column name to read sample weights from when weighting is enabled.

output:
output_dir: "../outputs/transformer_peak_classifier_1pct_filtered" # Directory where all training artifacts are written.
#output_dir: "../outputs/transformer_peak_classifier_6pct_filtered"
save_best_model: true # Writes the best validation checkpoint to disk.
save_epoch_history: true # Saves per-epoch metrics to a CSV file.
save_metrics_summary: true # Saves the final validation and test metrics summary as JSON.
save_plots: true # Generates training curve plots after training finishes.
save_confusion_matrices: true # Saves confusion-matrix artifacts for validation and test sets.
device_verbose: true # Prints detailed device and accelerator information at startup.
enable_file_logging: true # Mirrors console logs into a file in the output directory.
log_file_name: "training.log" # File name used for the persisted training log.

features:
peak_feature_columns:
- "peak_feature_mz" # Peak m/z-based engineered feature.
- "peak_feature_log_intensity" # Log-scaled peak intensity feature.
- "peak_feature_relative_intensity" # Intensity normalized relative to the spectrum.
- "peak_feature_mz_over_precursor" # Ratio between peak m/z and precursor m/z.
- "peak_feature_delta_to_precursor" # Difference between peak m/z and precursor m/z.
- "peak_feature_delta_prev" # Distance from the previous peak in the ordered spectrum.
- "peak_feature_delta_next" # Distance to the next peak in the ordered spectrum.

spectrum_feature_columns:
- "Charge" # Precursor charge state feature.
- "exp m/z" # Experimental precursor m/z feature.
- "num_peaks" # Total number of peaks in the spectrum.
- "tic" # Total ion current summary feature.

use_raw_peak_mz: true # Appends the raw m/z sequence as an additional peak input column.
raw_peak_mz_column: "mz_arr" # Source column holding the raw m/z array.
use_raw_peak_intensity: true # Appends the raw intensity sequence as an additional peak input column.
raw_peak_intensity_column: "int_arr" # Source column holding the raw intensity array.
sort_raw_peak_inputs_by_mz: true # Sorts raw peak arrays by m/z before building tensors.
broadcast_spectrum_features_to_peaks: false # Kept for config compatibility but unused by the transformer path.
normalize_peak_features: true # Standardizes peak-level input features using train-set statistics.
normalize_spectrum_features: true # Standardizes spectrum-level input features using train-set statistics.
normalization_fit_split: "train" # Split used to fit feature normalization statistics.

model:
name: "transformer_peak_classifier" # Identifier for this model family.

transformer:
d_model: 96 # Hidden width used for transformer token embeddings.
num_heads: 4 # Number of attention heads in each encoder block.
num_layers: 3 # Number of stacked transformer encoder blocks.
ff_multiplier: 4.0 # Expansion factor for the feed-forward hidden layer.
#It prevents the model from relying too much on a few specific peak-to-peak relationships.
dropout: 0.1 # Dropout probability applied inside the transformer.
activation: "gelu" # Nonlinearity used in the feed-forward network and head.
use_layer_norm: true # Enables layer normalization inside the encoder blocks.
output_dim: 1 # Number of logits produced per peak.
use_spectrum_context_gating: true # Gates peak tokens with spectrum-level context.
use_peak_positional_projection: true # Projects the first two peak features as a lightweight positional cue.

training:
seed: 42 # Global random seed for reproducible training behavior.
batch_size: 8 # Number of spectra per batch before peak padding is applied.
num_workers: 10 # Number of dataloader worker processes.
max_epochs: 50 # Maximum number of training epochs to run.
cpu_num_threads: 8 # CPU compute threads used by PyTorch kernels where applicable.
cpu_num_interop_threads: 2 # CPU inter-op threads used to schedule parallel work.
dataloader_prefetch_factor: 4 # Number of batches each worker preloads ahead of time.
dataloader_persistent_workers: true # Keeps dataloader workers alive across epochs.
dataloader_pin_memory: true # Pins host memory to speed CPU-to-GPU transfers.
enable_amp: true # Enables automatic mixed precision on CUDA.
#Mixed precision tries to match each op to its appropriate datatype. see https://docs.pytorch.org/docs/stable/amp.html
compile_model: false # Disables torch.compile to reduce runtime memory risk.
cache_dataset_in_memory: true # Precomputes dataset samples and keeps them in RAM.
save_latest_checkpoint: true # Saves a resumable checkpoint after each epoch.
resume_from_checkpoint: null # Set to "auto" or a checkpoint path to continue after interruption.

optimizer:
name: "adamw" # Optimizer used to update model parameters.
learning_rate: 0.0003 # Base learning rate passed to the optimizer.
weight_decay: 0.0001 # L2-style parameter decay applied by the optimizer.

scheduler:
# sofar not important, can be added afterwards
enabled: false # Placeholder switch for learning-rate scheduling support.

gradient_clip_norm: 1.0 # Max gradient norm used for clipping before optimizer step.

loss: #pos_weight is important, because we have class-imbalanced data!
name: "bce_with_logits" # Per-peak binary classification loss function.
use_pos_weight: true # Uses a positive-class weight estimated from the training split.
pos_weight: null # Manual positive-class weight override; null means auto-compute.
reduction: "none" # Keeps unreduced loss values so padding can be masked out manually.

early_stopping:
enabled: true # Stops training early when the monitored metric stops improving.
monitor: "val_pr_auc" # Validation metric used to select the best epoch.
mode: "max" # Improvement direction for the monitored metric.
patience: 7 # Number of unimproved epochs tolerated before stopping.
min_delta: 0.0001 # Minimum metric change required to count as an improvement.

evaluation:
primary_metric: "pr_auc" # Main evaluation metric emphasized in reports.
threshold_for_binary_metrics: 0.3 # Probability cutoff used for discrete binary metrics.

report_metrics:
- "pr_auc" # Precision-recall area under the curve.
- "roc_auc" # Receiver operating characteristic area under the curve.
- "f1" # Harmonic mean of precision and recall.
- "mcc" # Matthews correlation coefficient.
- "precision" # Positive predictive value at the chosen threshold.
- "recall" # True positive rate at the chosen threshold.

retained_peak_fractions:
- 0.3 # Fraction used for retained-peak style analysis.
- 0.5 # Fraction used for retained-peak style analysis.
- 0.7 # Fraction used for retained-peak style analysis.
Loading