Data Science for Electron Microscopy
Week 3: Linear algebra, PCA & spectral unmixing

Prof. Dr. Philipp Pelz

FAU Erlangen-Nürnberg

Institute of Micro- and Nanostructure Research

FAU Logo IMN Logo CENEM Logo ERC Logo Eclipse Logo

Three numbers you will understand today

Week 2: the noise model decides the loss. Today: the same rule decides how we factorise a spectrum image, and every example is real data.

58

Ce map from 0.4 counts per pixel: raw and PCA

counts per pixel in a real EDS map: the Ce window gets only 0.4, yet PCA gives a clean map

→ Act 4 · PCA

0.96

ZLP shift map and PCA component 1

correlation of a PCA “component” with the energy drift of the spectrometer

→ Act 4 · PCA

∞

two triangles that enclose the same pixels

unmixings that fit the data exactly equally well: A and B are two of them

→ Act 6 · Trust

Today’s red line: from a datacube to a phase map you can trust

Act 1 · Vector

When are two spectra alike?

A spectrum is a vector. Similarity is an angle.

Cymes et al. 2023, Fig. 10: HAADF image of a lunar pyroxene rim and Fe-L EELS spectra next to FeO, magnetite and Fe3+ glass standards

Source study: Fe-L₂,₃ spectra of a lunar pyroxene and the Fe²⁺ / Fe³⁺ standards. Dashed lines: the two L₃ channels used here. Fig. 10 of Cymes, Brittany A. et al., (2023), doi:10.1111/maps.13941, CC BY 4.0.

Projection onto references: how much Fe³⁺?

Act 2 · Matrix

How does a datacube become a matrix?

A spectrum image is a matrix: rows are pixels, columns are channels

X = cube.reshape(ny * nx, ne) — every method today factorises this X ≈ (maps) × (spectra).

Least squares: make the squares small

Residual rᵢ = data − model. Least squares picks the parameters that make Σ rᵢ² as small as possible.

Least squares is a projection: the power-law background

Act 3 · SVD

What does any matrix do to space?

Every matrix rotates, stretches, rotates: A = UΣVᵀ

Application: strain mapping with 4D-STEM

Least squares finds the lattice in every pattern. The SVD splits its change into rotation and principal strains.

The best rank-k image: Eckart–Young

Act 4 · PCA

How many things vary?

PCA turns the direction v until the variance is largest

PCA = SVD of the centred data matrix

Centre

\(\tilde{\mathbf X}=\mathbf X-\mathbf 1\bar{\mathbf x}^{T}\)

remove the mean spectrum

Decompose

\(\tilde{\mathbf X}=\mathbf U\boldsymbol\Sigma\mathbf V^{T}\)

\(\lambda_k=\dfrac{\sigma_k^2}{N-1}\)

Read off

loadings \(\mathbf v_k\) (spectra)

scores \(\mathbf t_k=\tilde{\mathbf X}\mathbf v_k\) (maps)

\(\hat{\mathbf X}_K=\bar{\mathbf x}+\textstyle\sum_{k\le K}\mathbf t_k\mathbf v_k^{T}\)

mu = X.mean(0);  U, s, Vt = np.linalg.svd(X - mu, full_matrices=False)
T = (X - mu) @ Vt[:K].T;  X_hat = T @ Vt[:K] + mu;  maps = T.reshape(ny, nx, K)

Eigenspectra and score maps are signed

58 counts per pixel, yet a clean map: PCA pools the pixels

Weight the counts before PCA

A derivative-shaped component means drift, not chemistry

Act 5 · Unmix

What are the things?

Every pixel is a mixture: the simplex

\(\mathbf x_i \approx \sum_k a_{ik}\,\mathbf m_k\),   \(a_{ik}\ge 0\),   \(\sum_k a_{ik}=1\)   →   \(\mathbf X\approx\mathbf A\,\mathbf M\), both \(\ge 0\)

NMF: a factorisation with only non-negative parts

Model

\(\mathbf X\approx\mathbf W\mathbf H\)

\(\mathbf W\in\mathbb R_{\ge0}^{N\times K}\) maps, \(\mathbf H\in\mathbb R_{\ge0}^{K\times D}\) spectra

Loss = noise model

Gauss: \(\lVert\mathbf X-\mathbf{WH}\rVert_F^2\)

Poisson: \(\sum x\log\frac{x}{(\mathbf{WH})}-x+(\mathbf{WH})\)

Multiplicative update (KL)

\(\mathbf H\leftarrow\mathbf H\odot\dfrac{\mathbf W^{T}(\mathbf X/\mathbf{WH})}{\mathbf W^{T}\mathbf 1}\)

\(\mathbf W\leftarrow\mathbf W\odot\dfrac{(\mathbf X/\mathbf{WH})\,\mathbf H^{T}}{\mathbf 1\,\mathbf H^{T}}\)

No subtraction is possible, so the components can only add up: parts, peaks, phases Lee, Daniel D. et al., (1999), doi:10.1038/44565. KL-NMF is maximum-likelihood unmixing of counts.

Watch NMF learn the phases

Same factorisation, other data: 4D-STEM diffraction

MCR-ALS: the constraints are the science

\(\mathbf X=\mathbf C\mathbf S^{T}+\mathbf E\): alternate two constrained least-squares problems.

  1. Fix \(\mathbf S\), solve for \(\mathbf C\ge 0\) (+ closure).
  2. Fix \(\mathbf C\), solve for \(\mathbf S\ge 0\) (+ known spectra).
  3. Repeat until the residual stops falling.

NMF = MCR with non-negativity only. Each extra true constraint shrinks the set of valid answers.

MCR of a STEM-EDS spectrum image of a microelectronics interface: pure-component spectra and their maps. From Kotula, Paul G. et al. (2006), doi:10.1017/S1431927606060636.

Act 6 · Trust

When is the phase map lying?

The same fit, many answers: rotational ambiguity

The scree plot hides a rare phase; the residual map shows it

Overlapping peaks: κ amplifies the noise

Choosing the decomposition

Question Method Constraint Unique? Check
How many things vary? Denoise PCA / SVD, Poisson-weighted orthogonal, variance-ordered yes (± sign) scree after weighting, residual map
Which phases, references known NNLS / FCLS (projection) \(\mathbf a\ge 0\), \(\sum a=1\) yes structured residual → missing reference
Which phases, spectra unknown NMF (KL for counts) \(\mathbf W,\mathbf H\ge 0\) no restarts, pure pixels, feasible band
+ physical knowledge MCR-ALS, physics-guided NMF + closure, known spectra, selectivity ≈, if enough the constraints must be true
Overlapping signals any least-squares fit — yes, but unstable condition number κ
Discrete phases / curved data clustering, autoencoders (Week 9) labels / learned non-linear no mixed pixels, validation

The red line, complete

Must-know for the exam

1 Projection onto â: c = âᵀb. Least squares = projection: Xᵀ(y − Xŵ) = 0.

2 SVD: A = UΣVᵀ (rotate, stretch, rotate). ‖X − Xₖ‖²_F = Σⱼ>ₖ σⱼ² (Eckart–Young).

3 PCA = SVD of the centred data; λₖ = σₖ²/(N−1). Scores and loadings are signed.

4 PCA denoises: signal low-rank, noise full-rank. Poisson-weight counts before PCA.

5 A derivative-shaped loading signals an energy shift. Align before you decompose.

6 NMF: X ≈ WH, W, H ≥ 0. KL-NMF = Poisson maximum likelihood.

7 NMF / MCR are not unique: WH = (WT)(T⁻¹H). Constraints reduce the ambiguity.

8 Truncation removes low variance, not unimportant signal: read the residual map. κ = σ_max/σ_min.

Self-study this week

Notebook (molab): notebooks/unit03_pca_unmixing.py — “PCA and spectral unmixing on real EM data”

  1. Parts 1–3: cos θ and Fe³⁺ fractions from real Fe-L standards; power-law backgrounds for 1 600 pixels at once; strain and Eckart–Young with the SVD.
  2. Parts 4–5: write PCA and the Poisson weighting yourself on the real EDS map; find and remove a drift component.
  3. Parts 6–7: write KL-NMF; test the feasible band of an unmixing; find a hidden phase in the residual map.

Ten exercises with automatic checks; CPU only, NumPy and Matplotlib. Sign in to molab → Fork → your own copy.

Open in molab

Next week: regression, optimisation & honest validation

Least squares was a projection today. Next week it becomes learning: gradient descent, ridge for ill-conditioning, and validation that does not leak.

  • Linear regression as projection; gradient descent, SGD, Adam on the same loss.
  • Ridge and lasso: the cure for today’s large κ, and their geometry.
  • Honest validation: train / validation / test, GroupKFold by specimen; fit every preprocessing step (scaling, PCA) inside the training fold.

Continue

Backup slides

Material outside the 90-minute path, for questions and self-study.

Weighting and truncation in practice

STEM-XEDS element maps: raw → filtered → PCA unweighted → PCA weighted → PCA filtered & weighted. From Potapov, Pavel et al. (2019), doi:10.1186/s40679-019-0066-0.
  • EDS counts are sparse (often < 1 count per channel per pixel). Without weighting, PCA mainly models the bremsstrahlung and the bright lines.
  • Potapov & Lubk derive the optimal truncation rank from the noise statistics Potapov, Pavel et al., (2019), doi:10.1186/s40679-019-0066-0.
  • Recipe: weight → PCA → K where the eigenvalues leave the flat floor → reconstruct → un-weight.
  • Record weighting, K and software version (Week 2, FAIR).

PCA vs NMF with a known ground truth

Synthetic 3-phase spectrum image (TiO₂-like, FeO-like, Ni) with Poisson noise. Top: truth. Middle: PCA (K = 3), signed difference spectra and maps; PC3 is noise because sum-to-one removes one dimension after centring. Bottom: NMF (K = 3), phase-like spectra and non-negative maps (img/make_figures.py).

Case study: NMF-aided phase unmixing of STEM-EDXS

Recovered phase spectra (a–c) and abundance maps (d–f). From Chen, Hui et al. (2024), doi:10.1016/j.ultramic.2024.113981.

The covariance matrix: where geometry meets statistics

  • \(\mathbf S=\frac{1}{N-1}\tilde{\mathbf X}^{T}\tilde{\mathbf X}\in\mathbb R^{D\times D}\): \(S_{jj}\) = variance of channel \(j\), \(S_{jk}\) = covariance of channels \(j\) and \(k\).
  • The level set \(\mathbf x^{T}\mathbf S^{-1}\mathbf x=1\) is an ellipsoid; its axes are the eigenvectors of \(\mathbf S\), its semi-axes \(\sqrt{\lambda_k}\).
  • \(\tilde{\mathbf X}=\mathbf U\boldsymbol\Sigma\mathbf V^{T}\Rightarrow\mathbf S=\mathbf V\frac{\boldsymbol\Sigma^2}{N-1}\mathbf V^{T}\): PCA directions = right singular vectors.
  • \(\mathbf S\) is \(D\times D\) (512 × 512 for today’s EELS), not \(N\times N\); SVD never forms it.

Ill-conditioning: a quick diagnostic

  1. Compute np.linalg.cond(X) on the standardised design matrix. Above ~10³, coefficients are unstable.
  2. Standardise features with different units first; otherwise κ is artificial.
  3. Add independent information: more lines, better resolution, a known component.
  4. Regularise (ridge, Week 4) or project onto the top singular vectors.

References

STEM-Bank-26: An open dataset of analytical scanning transmission electron microscopy images and spectra, George Hollyer & Eric Stach https://doi.org/10.48550/arXiv.2610.36086.
Detection of ferric iron in an exsolved lunar pyroxene using electron energy loss spectroscopy (EELS): Implications for space weathering and redox conditions on the Moon, Meteoritics & Planetary Science, Brittany A. Cymes, Katherine D. Burgess, & Rhonda M. Stroud https://doi.org/10.1111/maps.13941.
Unsupervised machine learning applied to scanning precession electron diffraction data, Advanced Structural and Chemical Imaging, Ben H. Martineau, Duncan N. Johnstone, Antonius T. J. van Helvoort, Paul A. Midgley, & Alexander S. Eggeman https://doi.org/10.1186/s40679-019-0063-3.
Electron energy-loss spectroscopy in the electron microscope, R. F. Egerton.
Pattern recognition and machine learning, Christopher M. Bishop.
Data processing for atomic resolution electron energy loss spectroscopy, Microscopy and Microanalysis, Paul Cueva, Robert Hovden, Julia A. Mundy, Huolin L. Xin, & David A. Muller https://doi.org/10.1017/S1431927612000244.
Nanoscale mapping of point defect concentrations with 4D-STEM, Acta Materialia, Sean H. Mills, Steven E. Zeltmann, Peter Ercius, Aaron A. Kohnert, Blas P. Uberuaga, & Andrew M. Minor https://doi.org/10.1016/j.actamat.2023.118721.
The approximation of one matrix by another of lower rank, Psychometrika, Carl Eckart & Gale Young https://doi.org/10.1007/BF02288367.
Statistical consequences of applying a PCA noise filter on EELS spectrum images, Ultramicroscopy, Stijn Lichtert & Jo Verbeeck https://doi.org/10.1016/j.ultramic.2012.10.001.
Accounting for Poisson noise in the multivariate analysis of ToF-SIMS spectrum images, Surface and Interface Analysis, Michael R. Keenan & Paul G. Kotula https://doi.org/10.1002/sia.1657.
Optimal principal component analysis of STEM XEDS spectrum images, Advanced Structural and Chemical Imaging, Pavel Potapov & Axel Lubk https://doi.org/10.1186/s40679-019-0066-0.
Spectral mixture analysis of EELS spectrum-images, Ultramicroscopy, Nicolas Dobigeon & Nathalie Brun https://doi.org/10.1016/j.ultramic.2012.05.006.
Learning the parts of objects by non-negative matrix factorization, Nature, Daniel D. Lee & H. Sebastian Seung https://doi.org/10.1038/44565.
Algorithms for non-negative matrix factorization, Advances in Neural Information Processing Systems, Daniel D. Lee & H. Sebastian Seung.
Application of multivariate statistical analysis to STEM x-ray spectral images: Interfacial analysis in microelectronics, Microscopy and Microanalysis, Paul G. Kotula & Michael R. Keenan https://doi.org/10.1017/S1431927606060636.
Multivariate curve resolution (MCR). Solving the mixture analysis problem, Analytical Methods, Anna de Juan, Joaquim Jaumot, & Romà Tauler.
Non-negative matrix factorization-aided phase unmixing and trace element quantification of STEM-EDXS data, Ultramicroscopy, Hui Chen, Farhang Nabiei, James Badro, Duncan T. L. Alexander, & Cécile Hébert https://doi.org/10.1016/j.ultramic.2024.113981.
From STEM-EDXS data to phase separation and quantification using physics-guided NMF, arXiv preprint arXiv:2404.17496, Adrien Teurtrie, Nathanaël Perraudin, Thomas Holvoet, Hui Chen, Duncan T. L. Alexander, Guillaume Obozinski, & Cécile Hébert.