---
title: "Reading rock chemistry from a Mars rover’s laser"
author: "Christian Bager Bach Houmann"
description: "Our master’s thesis on ChemCam’s laser spectra: a leak-free evaluation, twelve models, a 16,000-trial search, and stacking ensembles that beat a replica of ChemCam’s calibration on every oxide."
url: "https://bagerbach.com/projects/chemcam-libs/"
---

# Reading rock chemistry from a Mars rover’s laser

- Master’s thesis: Aalborg University
- Period: Sep 2023 to Jun 2024
- Built with: Python, scikit-learn, Optuna
- Source: [GitHub](https://github.com/chhoumann/thesis-chemcam), [Thesis PDF](https://bagerbach.com/thesis.pdf)

Curiosity carries a laser. ChemCam fires it at a rock, the spot turns to plasma, and three spectrometers record the light. For our master’s thesis, two classmates and I worked on the step after that: turning one of those spectra into the rock’s chemistry.

## The problem

A spectrum has 6,144 channels, 2,048 from each spectrometer. From it, a model has to predict eight major oxides in weight percent, from SiO₂ to K₂O. We trained on ChemCam’s calibration data from NASA’s Planetary Data System: 408 rock samples the ChemCam team shot in a lab under Mars-like conditions, at five spots per sample and fifty shots per spot, all from 1.6 meters away. For LIBS, that is a big dataset. For machine learning, it is small.

Neighbouring channels move together. Matrix effects make an element’s line brighter or dimmer for reasons that have nothing to do with how much of it is there. And the samples with unusually high or low amounts of an oxide are rare, which makes them both the most important and the easiest to lose in a careless split.

Perseverance carries ChemCam’s successor, SuperCam, which faces the same problem. Our data and models are ChemCam’s.

## Where we started

ChemCam’s calibration model, MOC, runs partial least squares sub-models and an ICA-based model side by side, then blends them with fixed weights per oxide. We had rebuilt it from the papers the semester before. For the thesis we changed how the replica uses the data and automated its outlier removal, and one of MOC’s original authors confirmed the changes were reasonable. That replica is our baseline.

## Testing without fooling ourselves

A random split leaks: one rock’s five spectra end up on both sides. So we keep every sample in one fold, sort the samples by the oxide we are predicting, and deal them into five folds like cards, which spreads the range evenly. The top and bottom 5% only ever go into training folds. I built a small app to choose that 5% by watching where the extremes landed.

The cost is that the test set is easier than the full range, so test error (RMSEP) runs lower than cross-validation error (RMSECV). We report both.

## Twelve models

We ran twelve models on the same input and the same folds with near-default settings. XGBoost had the lowest cross-validation error, with SVR and gradient boosting close behind. The two neural networks came last. With 408 samples they needed far more tuning than we could afford, so we dropped them. The more useful finding was that the best model changed from oxide to oxide.

Mean cross-validation error of the first benchmark, relative to XGBoost at 100 (thesis Tables 7 and 8):

| Model | Relative RMSECV | Mean RMSECV | Mean RMSEP |
| --- | ---: | ---: | ---: |
| XGBoost | 100.00 | 1.941 | 1.456 |
| SVR | 100.85 | 1.958 | 1.436 |
| Gradient boosting | 103.07 | 2.001 | 1.435 |
| NGBoost | 103.94 | 2.017 | 1.445 |
| Random forest | 104.45 | 2.027 | 1.479 |
| Extra trees | 104.84 | 2.035 | 1.507 |
| Ridge | 105.04 | 2.039 | 1.649 |
| PLS | 111.66 | 2.167 | 1.781 |
| ElasticNet | 114.12 | 2.215 | 1.731 |
| LASSO | 114.30 | 2.219 | 1.736 |
| ANN (dropped) | 127.82 | 2.481 | 1.821 |
| CNN (dropped) | 143.18 | 2.779 | 1.880 |

## 16,000 trials

So we stopped picking by hand. I wrote an Optuna search that builds a whole pipeline per trial (a scaler, an optional transform, optional PCA, then the model) and minimizes cross-validation error and its spread together. We set 200 trials for each of the ten remaining models on each of the eight oxides, 16,000 in all, and a server failure cut NGBoost on MgO short at 143. Every trial went to MLflow, and every new best went to a Discord channel.

ChemCam’s own normalization, which scales each spectrometer separately (Norm 3 in the thesis), was the most used scaler, in 5,090 trials. Neither PCA nor kernel PCA seemed to help, and 9,419 trials used neither.

## Stacking

MOC blends its models with weights set by hand. A stacking regressor learns the blend instead. For each oxide we took the best two or three pipelines from different model families and trained a small meta-model on their out-of-fold predictions. SVR made it into all eight.

Each oxide’s stack: two or three base pipelines (level 0), whose out-of-fold predictions on the four cross-validation folds train a meta-learner (level 1). Base pipelines from the thesis code, the meta-learner with the lowest test error from Tables 16 to 19:

| Oxide | Base pipelines | Meta-learner |
| --- | --- | --- |
| SiO₂ | PLS (min-max scaling, cosine kernel PCA, 1 component); SVR (min-max scaling, a polynomial kernel); Gradient boosting (Norm 3) | SVR |
| TiO₂ | SVR (Norm 3, a power transform, a polynomial kernel); Random forest (Norm 3, a quantile transform) | ElasticNet α = 0.1 |
| Al₂O₃ | XGBoost (Norm 3, a power transform); SVR (min-max scaling, a quantile transform, a linear kernel); Ridge (Norm 3, a quantile transform) | ElasticNet α = 1 |
| FeOT | SVR (Norm 3, a quantile transform, an RBF kernel); PLS (standard scaling, a power transform, 30 components); Ridge (Norm 3, a quantile transform) | SVR |
| MgO | SVR (robust scaling, a power transform, a polynomial kernel); PLS (Norm 3, RBF kernel PCA, 3 components); Ridge (robust scaling, a power transform) | ElasticNet α = 1 |
| CaO | SVR (min-max scaling, a quantile transform, a linear kernel); PLS (max-abs scaling, a quantile transform, 22 components); Gradient boosting (Norm 3, a quantile transform) | SVR |
| Na₂O | SVR (Norm 3, a power transform, a polynomial kernel); PLS (Norm 3, a power transform, 30 components); Gradient boosting (Norm 3, a quantile transform) | SVR |
| K₂O | PLS (Norm 3, 30 components); Gradient boosting (min-max scaling, a quantile transform); SVR (Norm 3, a quantile transform, an RBF kernel) | ElasticNet α = 1 |

The meta-model matters more than it looks. With a strongly regularized ElasticNet on top, the TiO₂ predictions went flat, almost the same number for every rock. Most TiO₂ values sit between 0 and 2.5 wt%, and the penalty swamped them. Turning the regularization down took TiO₂’s test error from 0.571 to 0.319.

## Results

On the held-out test set, every oxide has a stack that beats our MOC replica. Summed over the eight oxides, test error is 37.3% lower with ElasticNet (α = 1), 38.1% with ElasticNet (α = 0.1), and 39.5% with SVR. Per oxide, the ElasticNet (α = 1) stack ranges from 1.4% better on FeOT to 75.1% better on K₂O, and with α = 0.1, FeOT gets 1.2% worse.

RMSEP on the held-out test set, in wt% (thesis Table 19):

| Oxide | MOC replica | ElasticNet α = 1 | ElasticNet α = 0.1 | SVR |
| --- | ---: | ---: | ---: | ---: |
| SiO₂ | 5.61 | 3.59 | 3.60 | 3.47 |
| TiO₂ | 0.61 | 0.57 | 0.32 | 0.34 |
| Al₂O₃ | 2.47 | 1.66 | 1.66 | 1.73 |
| FeOT | 1.82 | 1.79 | 1.84 | 1.69 |
| MgO | 1.56 | 0.71 | 0.77 | 0.82 |
| CaO | 2.09 | 1.64 | 1.65 | 1.59 |
| Na₂O | 1.33 | 0.47 | 0.44 | 0.37 |
| K₂O | 1.91 | 0.48 | 0.49 | 0.51 |

These are test-set numbers on lab samples. The two test sets were built differently but are similar in composition, which is what makes the comparison fair, and some single pipelines still beat the stacks on some metrics.

## PyHAT

PyHAT is the USGS’s Python toolkit for this kind of spectral analysis. Dr. Ryan B. Anderson merged three of my merge requests: [a faster ChemCam data loader](https://code.usgs.gov/astrogeology/pyhat/-/merge_requests/9) that took 2,068 files from 136.35 s to 39.28 s, [a switch for a file-version check](https://code.usgs.gov/astrogeology/pyhat/-/merge_requests/8) that rejected real data, and [an automated version of an outlier method](https://code.usgs.gov/astrogeology/pyhat/-/merge_requests/10) that used to be done by hand. Patrick contributed [a function for reading ICA loadings](https://code.usgs.gov/astrogeology/pyhat/-/merge_requests/11), which is still open.

After we presented our results, the USGS team wrote us a letter:

> These contributions stand to make a sizeable impact on the field of chemometrics as applied to the ChemCam and SuperCam emission spectroscopy instruments on two active missions as part of NASA’s Mars Exploration Program.
>
> First, the team suggested the use of a Stacking Regressor to combine the results of several models into a final “meta model”. This is an alternative to the relatively crude and labor-intensive “submodel blending” approach that was used for the current ChemCam calibration. The team also suggested the use of a power transform as a preprocessing step. Both suggestions have been implemented and are showing promising results.
>
> — Dr. Ryan B. Anderson and Dr. Travis S.J. Gabriel, USGS Astrogeology Science Center

## What’s left

The test set leaves out the extremes, so we never measured how the models do on the rarest compositions, and we only had data from one distance. We ran a grid search over stack combinations for TiO₂ alone, left the meta-learners at their defaults, and left the neural networks out of the search. Synthetic data, a separate test on extreme values, and a wider search over stacks are where I would go next.

## History

- **September 2023.** First commit. Rebuilding ChemCam’s calibration model from the papers.
- **January 2024.** Our 9th-semester report on where that model falls short.
- **May 2024.** Opened PyHAT merge requests 8, 9, and 10.
- **May 17 to June 12, 2024.** The 16,000-trial search, then the stacking results.
- **June 13, 2024.** The letter from the USGS team.
- **June 26, 2024.** The automated outlier removal merged into PyHAT.
- **March 3, 2025.** The faster loader and the file-check switch merged into PyHAT.

## Credits

The thesis is joint work with Patrick Frostholm Østergaard and Ivik Lau Dalgas Hostrup. Our supervisors were Daniele Dell’Aglio and Juan Manuel Rodriguez, and Jens Frydenvang of the ChemCam team was our external supervisor.

[Read the thesis](https://bagerbach.com/thesis.pdf), browse [the code on GitHub](https://github.com/chhoumann/thesis-chemcam), or see [our PyHAT merge requests](https://code.usgs.gov/astrogeology/pyhat/-/merge_requests?scope=all&state=all).
