A machine learning pipeline designed to classify amplitude anomalies in bandpass calibration spectra (e.g., from ALMA calibration tables) using XGBoost. The system processes raw calibration tables, extracts features (including scan statistics based scores and ALMA receiver bands), maps flag templates to train labels, and outputs predictions on unseen calibration tables.
- Project Structure
- Core Architecture & Workflow
- Configuration (
config.toml) - Feature Extraction Engine
- Getting Started & Usage
bandpass_classifier/
├── bandpass_classifier/ # Source package
│ ├── __init__.py # Package initializer
│ ├── train.py # Training pipeline entry point
│ ├── predict.py # Inference pipeline entry point
│ ├── io_utils.py # CASA table and flagtemplate parsing
│ ├── utils.py # Helper utilities (caching, encoding)
│ └── features/ # Feature extraction sub-package
│ ├── __init__.py # Auto-boots the registry
│ ├── extractor.py # Topological feature extractor engine
│ ├── registry.py # Feature module coordinator
│ ├── basic.py # Base features (amplitude, NMAD)
│ └── scan_statistics/ # Advanced scan-based features
│ ├── features.py # Registered scan stats features
│ └── scan_statistics.py # Window searching and atmospheric line detection
├── inputs/ # Data inputs directory (full_spectrum.gzip, tables)
├── models/ # Model config and output artifacts
│ └── v1/
│ ├── config.toml # Configuration file
│ ├── model.json # Trained XGBoost model (generated after training)
│ └── column_categories.json # Categorical feature mapping (generated after training)
├── environment.yml # Conda environment configuration
└── README.md # Documentation (this file)
The pipeline consists of two main stages: Training and Prediction.
graph TD
A[Raw Calibration Tables .bcal.tbl] --> B[CASA Table I/O Reader]
B --> C[Topological Feature Extractor]
C --> D[Spectrum Pairer / Flattener]
%% Training Flow
D --> E[Train Pipeline]
F[Flag templates .flagtemplate.txt] --> G[Label Broadcasting]
G --> E
E --> H[Symmetric Data Augmentation]
H --> I[XGBoost Classifier Training]
I --> J[Save Model & Categories]
%% Prediction Flow
D --> K[Predict Pipeline]
J --> K
K --> L[Anomaly Prediction Output]
- CASA Tables: Uses
casatools.tableto read calibration parameters (CPARAM), flags (FLAG), and spectral properties from.bcal.tblstructures. - Flag Templates: Parses calibration flag files to determine ground truth reasons (e.g.,
QA2:bandpass_amplitude_frequency). - Label Broadcasting: Uses
broadcast_na()to map sparse flags to matching indexes across multi-index levels (Execution Block UID, Spectral Window, Antenna, Polarization).
- Loads and concatenates all matching bandpass tables.
- Aligns and maps flags to label each record as an anomaly (
True/False). - Splits training and testing partitions using an aggregated block structure (e.g., grouping by
eb_uidto prevent leakage). - Fits an XGBoost Classifier using
enable_categorical=Trueand handles class imbalance usingscale_pos_weight. - Saves the trained model to JSON along with JSON-encoded category mappings.
- Evaluates individual
.bcal.tbltables. - Runs the topological feature extractor, performs categorization using saved mappings, and executes inference with the trained XGBoost model.
Configurations control data inputs, features, hyperparameters, and symmetry strategies. Key sections include:
[model]: Paths to save or load themodel.jsonandcolumn_categories.json(supports relative paths resolved relative to the config file's parent directory).[features]: Declares shared features (e.g.,receiver_band) and individual spectrum features (e.g., NMAD-based scores).[training.data]: File glob patterns for input tables/flag templates and the specific anomaly labels/reasons to target.[training.hyperparameters]: Parameters passed directly to the XGBoost backend.[training.strategy]: Implements dataset partitioning schemes and training-time data permutation (symmetric = truefor order-invariant predictions).
Features are extracted dynamically using a custom topological dependency resolver:
- Topological Sorter (
features/extractor.py): Resolves feature dependencies (e.g.,amp_norm_nmad_diff4depends onamp_nmad_diff4andamp_nmad, which in turn depend onamplitude). - Parallel execution: Features are computed on chunked pandas DataFrames in parallel processes.
- Scan Statistics (
features/scan_statistics/): Searches for sliding window discrepancies in amplitude across three modes (fixed,masked,unmasked). It integrates atmospheric transmission tables to account for natural absorption lines. - Symmetric Pairing: Spectrum features are paired side-by-side (suffixed
_0and_1). Data augmentation is applied during training by swapping the order of the pairs.
Create and activate the Conda environment using the provided environment.yml file:
# Create the environment
conda env create -f environment.yml
# Activate the environment
conda activate bandpass_classifierThe pipeline supports the following environment variables for caching and performance optimization:
| Variable | Default | Description |
|---|---|---|
BANDPASS_ENABLE_CACHE |
0 (disabled) |
Set to 1, true, or yes to enable joblib disk caching for computationally intensive feature extractors (e.g. scan statistics). |
BANDPASS_CACHE_DIR |
.cache |
Directory path where joblib cache artifacts will be stored when caching is enabled. |
BANDPASS_MAX_WORKERS |
None (auto/all cores) |
Number of worker processes to use for parallel feature extraction in process_map. |
BANDPASS_DATA_DIR |
None (paths as configured) |
Base directory path for training data patterns (supports absolute and relative paths). |
# Set a base directory for training data patterns
BANDPASS_DATA_DIR=/path/to/data python -m bandpass_classifier.train --config models/v1/config.toml
# Enable joblib disk caching with default location (.cache/)
BANDPASS_ENABLE_CACHE=1 python -m bandpass_classifier.train --config models/v1/config.toml
# Restrict parallel feature extraction to 4 worker processes
BANDPASS_MAX_WORKERS=4 python -m bandpass_classifier.train --config models/v1/config.toml
# Enable joblib disk caching with a custom directory path
BANDPASS_ENABLE_CACHE=1 BANDPASS_CACHE_DIR=/path/to/custom_cache python -m bandpass_classifier.predict \
--config models/v1/config.toml \
--bandpass_table path/to/your/table.solintinf.bcal.tblTo train the classifier using a specific configuration file:
python -m bandpass_classifier.train --config models/v1/config.tomlTo overwrite existing model artifacts and intermediate outputs:
python -m bandpass_classifier.train --config models/v1/config.toml --overwriteTo run a fast test or train on a subset of the dataset:
python -m bandpass_classifier.train --config models/v1/config.toml --sample 1000To detect anomalies in an unseen calibration table:
python -m bandpass_classifier.predict \
--config models/v1/config.toml \
--bandpass_table path/to/your/table.solintinf.bcal.tbl