← Back to case study

PROJECT 057 · PYTHON SOURCE

Carbon monoxide sensor calibration

Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.

Download Python file ↓
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute

META = {'id': 57, 'dataset': 'air', 'title': 'Carbon monoxide sensor calibration', 'question': 'Can contemporaneous sensor responses estimate an available reference CO concentration?', 'method': 'Chronological regression benchmark using sensors and environmental measurements, excluding other gas references.', 'action': 'Validate on a later instrument period before filling reference gaps.', 'limitations': 'The -200 sentinel is treated as missing. Reference instruments have incomplete coverage; comparisons use paired observations. Sensor calibration is retrospective, not a health or regulatory compliance assessment. '}

def analyze():
    df = load('air').copy()
    t,p,e=regression(df,AIR_SENSORS,'co_gt',split='ordered')
    out=result(t,'model','mae','CO reference-unit MAE','This estimates a simultaneous reference reading, not a future concentration. Test dates follow training among observed target rows.',extra={'predictions':p},evaluation=e)
    return out

if __name__ == "__main__":
    execute(Path(__file__).parent, META, analyze)