|
Download docs/METHODOLOGY.md from Laibaff7/cda-hmoc-classifier: direct link, hf CLI and curl.
- Browser
- Download file 7.12 kB
-
https://huggingface.co/Laibaff7/cda-hmoc-classifier/resolve/main/docs/METHODOLOGY.md
- Command line
-
hf download hf://Laibaff7/cda-hmoc-classifier/docs/METHODOLOGY.md
-
curl -L -o METHODOLOGY.md https://huggingface.co/Laibaff7/cda-hmoc-classifier/resolve/main/docs/METHODOLOGY.md
7.12 kB
| # HMOC Binary Classification: Algorithm & Methodology | |
| This document outlines the complete end-to-end methodology for the HMOC (High Mass Organic Cation) binary classification model. It details the data preparation, model architecture, training strategy, and evaluation metrics. | |
| ## 1. Problem Formulation & Constraints | |
| **Goal:** Identify HMOC spectra within a pool of 70,000 unlabelled samples. | |
| **Constraints:** | |
| - **Extreme Class Imbalance:** Only 83 labeled HMOC rows available vs. 15,000+ non-HMOC rows. | |
| - **No Data Augmentation / SMOTE:** Generative techniques for minority classes were not permitted. | |
| To resolve this, we formulated the task as an **Anomaly/Retrieval Binary Classification** problem (HMOC [Class 1] vs. All Clean Non-HMOC [Class 0]). | |
| ## 2. Data Preparation & Curation | |
| Because we cannot artificially increase the positive class, we must strictly curate the negative class to ensure the model learns a high-quality decision boundary. | |
| - **Class Filtering:** We explicitly dropped the `?` (553 rows) and `Noise` (12,077 rows) classes. These contain ambiguous or uninformative physical signatures that would corrupt the network's understanding of what constitutes a "true" clean background. | |
| - **Resulting Ratio:** Reducing the negatives to only high-quality labeled classes (1, 2, 3, etc.) left us with ~2,600 negatives. This brought the imbalance from an impossible ~160:1 down to a tractable ~31:1 ratio. | |
| - **Sequence Processing:** All raw spectral arrays were standardized to exactly **1000 bins (sequence length `TARGET_LEN = 1000`)** via truncation (if longer) or zero-padding (if shorter). | |
| - **Normalization:** Applied per-spectrum Min-Max Normalization to scale all intensity values between `0` and `1`. This standardizes peak magnitudes, helping the neural network converge smoothly. | |
| ## 3. Model Architecture: 1D CNN | |
| We selected a custom, intentionally shallow **1D Convolutional Neural Network (1D CNN)**. | |
| **Why?** Mass spectra are 1-dimensional sequential signals where local features (peaks, clusters of mass/charge ratios) define the class. A deep network (like ResNet) would instantly overfit the 83 positive samples. | |
| ### Layer-by-Layer Breakdown: | |
| 1. **Conv1D Layer 1:** 32 filters, `kernel_size=7`, `padding=3` + BatchNorm1d + ReLU. | |
| - *Purpose:* Large kernel to capture wide, low-frequency patterns and broad spectrum trends. | |
| 2. **Conv1D Layer 2:** 64 filters, `kernel_size=5`, `padding=2` + BatchNorm1d + ReLU. | |
| - *Purpose:* Medium kernel to extract composite relationships between structural peaks. | |
| 3. **Conv1D Layer 3:** 128 filters, `kernel_size=3`, `padding=1` + BatchNorm1d + ReLU. | |
| - *Purpose:* Small kernel for fine, high-frequency local peak detection. | |
| 4. **Global Average Pooling 1D (GAP):** | |
| - *Purpose:* Replaces a traditional `Flatten()` layer. GAP computes the mean of each of the 128 feature maps. This drastically reduces the parameter count to prevent overfitting and provides robustness to minor shifts in peak positions. | |
| 5. **Fully Connected Classifier (Dense Layers):** | |
| - Linear(128 -> 64) + ReLU | |
| - **Dropout (Rate = 0.4):** Randomly zeros out 40% of neurons during training to enforce redundancy and prevent the network from memorizing the 83 samples. | |
| - Linear(64 -> 1) mapping to a binary probability via a Sigmoid activation. | |
| ## 4. Addressing Imbalance: Loss Function & Batching | |
| Since augmentation wasn't allowed, the mathematical approach during training had to combat the 31:1 imbalance. | |
| ### Focal Loss | |
| Standard Binary Cross Entropy (BCE) would fail because the easy negative samples would overwhelm the total loss, causing the model to just predict "0" for everything. We replaced it with **Focal Loss**: | |
| - **Alpha (`α = 0.85`):** Explicitly weights the positive (HMOC) class heavier than the negative class. | |
| - **Gamma (`γ = 2.0`):** The "focusing parameter." As an example's prediction probability approaches 1 (meaning the model is confident and correct), its contribution to the loss drops to near zero. This forces the model to ignore easy negatives and strictly focus its gradient updates on difficult samples and the rare HMOCs. | |
| ### Weighted Random Sampler | |
| In DataLoader, we used `WeightedRandomSampler` which constructs mini-batches based on inverse class frequencies. Every `BATCH_SIZE=64` was mathematically forced to include at least a few HMOC samples, ensuring stable gradient descent directions. | |
| ## 5. Training Strategy & Regularization | |
| - **Optimizer:** `AdamW` (Adam with explicit Weight Decay). | |
| - **Learning Rate:** `1e-3`. | |
| - **Weight Decay:** `1e-4` (L2 regularization to penalize large weights and constrain model complexity). | |
| - **Cross-Validation:** **Stratified 5-Fold Cross Validation** ensuring that the 83 HMOC samples were evenly distributed across training and validation sets. | |
| - **Early Stopping:** Training halted if the Validation AUPRC didn't improve for `PATIENCE = 15` epochs, preventing late-stage overfitting. | |
| ## 6. The Ensemble Approach | |
| A model trained on just 67 positive samples (4/5ths of 83) is highly susceptible to the initial random state of its weights. To counter this "seed lottery", we trained **5 separate models** using different random seeds (42, 100, 2026, 777, 888) corresponding to the 5 folds. | |
| During inference, all 5 models process an unknown spectrum independently, and their 5 output probabilities are **averaged**. This yields a highly robust, calibrated score with much lower variance than a single model could provide. | |
| ## 7. Metrics Explained | |
| When dealing with a 31:1 imbalance, raw "Accuracy" is misleading (a model guessing 100% negative achieves ~97% accuracy). Instead, we optimized and evaluated on the following: | |
| - **AUPRC (Area Under the Precision-Recall Curve):** *The primary metric.* While AUROC looks at False Positive Rates, AUPRC strictly measures how well the model handles the minority class. A baseline random guess yields an AUPRC of ~0.03. Our models achieved AUPRCs between **0.358 and 0.567**, indicating robust learned features far beyond random chance. | |
| - **Precision:** The proportion of predicted HMOCs that were actually HMOCs. (Our models scored between 40% - 70%). | |
| - **Recall (Sensitivity):** The proportion of total actual HMOCs that the model successfully found. (Our models scored between 43% - 70%). | |
| - **F1 Score:** The harmonic mean of Precision and Recall. Our models consistently hit an F1 of **~0.51 to 0.53**. | |
| - **Best Threshold Calibration:** By default, models classify `probability > 0.5` as positive. We programmatically scanned thresholds to maximize the F1 score. Thresholds varied from `0.44` to `0.82` depending on the fold. | |
| ## 8. Summary for 70k Inference | |
| With an F1 score of ~0.52 and varying precision/recall trade-offs across the ensemble, the model is not a perfect oracle. Instead, it is an **expert triage system**. When applied to the 70,000 unlabelled spectra, averaging the ensemble limits false positives and guarantees that the **top-ranked candidates** have an incredibly high density of true HMOCs for researchers to manually review, condensing years of manual search into a single high-probability CSV list. | |