# andreasleitherer/ai4stem architecture

> Architecture diagram of the GitHub repository andreasleitherer/ai4stem, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andreasleitherer/ai4stem
- Repository: https://github.com/andreasleitherer/ai4stem

## Overview

AI-STEM characterizes atomic-resolution STEM images. Its primary workflow loads an image, divides it into local windows, computes FFT-HAADF descriptors, and applies a pretrained CNN to produce crystal-class predictions and uncertainty; a separate analysis workflow embeds neural-network representations for unsupervised exploration. The repository also includes CNN training and lattice reconstruction utilities. Edges are limited to relationships visible in the sampled source or SOURCE INDEX; reconstruction is shown as a capability, but no caller was sampled.

Main entry points: [`ai4stem/utils/utils_data.py`](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_data.py) (Data loaders), [`ai4stem/utils/utils_prediction.py`](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_prediction.py) (Image prediction), [`scripts/ai4stem_prediction.py`](https://github.com/andreasleitherer/ai4stem/blob/main/scripts/ai4stem_prediction.py) (Prediction example), [`ai4stem/utils/utils_unsupervised.py`](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_unsupervised.py) (Representation analysis)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_supervised["Supervised analysis"]
  node_prediction_example["Prediction example"]
  node_prediction["Image prediction"]
  node_windowing["Local windows"]
  node_fft["FFT descriptors<br/>[utils_fft.py]"]
end

subgraph group_models["Models and data"]
  node_inference["CNN inference<br/>[utils_nn.py]"]
  node_training["CNN training<br/>[utils_nn.py]"]
  node_data_access["Data loaders<br/>[utils_data.py]"]
  node_example_image[("Example image<br/>[Fe_bcc_100.npy]")]
  node_pretrained_model[("Pretrained CNN<br/>[optimal_model.h5]")]
  node_class_labels[("Class definitions")]
end

subgraph group_unsupervised["Unsupervised analysis"]
  node_unsupervised_example["Embedding example"]
  node_unsupervised_analysis["Representation analysis"]
  node_embedding["UMAP embedding"]
end

subgraph group_lattice["Lattice analysis"]
  node_reconstruction["Atomic-column reconstruction"]
  node_reference_lattices[("Reference lattices")]
end

node_analyst(("Microscopy analyst"))

node_analyst -->|"runs"| node_prediction_example
node_prediction_example -->|"loads image"| node_data_access
node_prediction_example -->|"loads model"| node_data_access
node_prediction_example -->|"calls"| node_prediction
node_prediction_example -->|"loads labels"| node_data_access
node_data_access -->|"reads"| node_example_image
node_data_access -->|"loads"| node_pretrained_model
node_data_access -->|"reads"| node_class_labels
node_prediction -->|"extracts windows"| node_windowing
node_prediction -->|"computes descriptors"| node_fft
node_prediction -->|"loads model"| node_data_access
node_prediction -->|"predicts with uncertainty"| node_inference
node_inference -.->|"loads model"| node_pretrained_model
node_analyst -->|"runs"| node_unsupervised_example
node_unsupervised_example -->|"loads image"| node_data_access
node_unsupervised_example -->|"calls"| node_unsupervised_analysis
node_unsupervised_analysis -->|"gets descriptors"| node_prediction
node_unsupervised_analysis -->|"loads model"| node_data_access
node_unsupervised_analysis -->|"gets representations"| node_inference
node_unsupervised_analysis -->|"fits and transforms"| node_embedding
node_embedding -->|"returns embedding"| node_analyst
node_training -.->|"saves model"| node_pretrained_model
node_reconstruction -.->|"uses reference lattice"| node_reference_lattices

click node_prediction_example "https://github.com/andreasleitherer/ai4stem/blob/main/scripts/ai4stem_prediction.py"
click node_prediction "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_prediction.py"
click node_windowing "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_prediction.py"
click node_fft "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_fft.py"
click node_inference "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_nn.py"
click node_training "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_nn.py"
click node_data_access "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_data.py"
click node_example_image "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/data/experimental_images/Fe_bcc_100.npy"
click node_pretrained_model "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/data/pretrained_models/optimal_model.h5"
click node_class_labels "https://github.com/andreasleitherer/ai4stem/tree/main/ai4stem/data/class_definitions"
click node_unsupervised_example "https://github.com/andreasleitherer/ai4stem/blob/main/scripts/ai4stem_unsupervised_learning.py"
click node_unsupervised_analysis "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_unsupervised.py"
click node_embedding "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_unsupervised.py"
click node_reconstruction "https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_reconstruction.py"
click node_reference_lattices "https://github.com/andreasleitherer/ai4stem/tree/main/ai4stem/data/reference_images"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_prediction_example,node_prediction,node_windowing,node_fft toneBlue
class node_inference,node_training,node_data_access,node_example_image,node_pretrained_model,node_class_labels toneAmber
class node_unsupervised_example,node_unsupervised_analysis,node_embedding toneMint
class node_reconstruction,node_reference_lattices toneRose
class node_analyst toneIndigo
```

## Components

### Supervised analysis

- [Prediction example](https://github.com/andreasleitherer/ai4stem/blob/main/scripts/ai4stem_prediction.py): `scripts/ai4stem_prediction.py`
- [Image prediction](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_prediction.py): `ai4stem/utils/utils_prediction.py`
- [Local windows](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_prediction.py): `ai4stem/utils/utils_prediction.py`
- [FFT descriptors](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_fft.py): `ai4stem/utils/utils_fft.py`

### Models and data

- [CNN inference](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_nn.py): `ai4stem/utils/utils_nn.py`
- [CNN training](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_nn.py): `ai4stem/utils/utils_nn.py`
- [Data loaders](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_data.py): `ai4stem/utils/utils_data.py`
- [Example image](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/data/experimental_images/Fe_bcc_100.npy): `ai4stem/data/experimental_images/Fe_bcc_100.npy`
- [Pretrained CNN](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/data/pretrained_models/optimal_model.h5): `ai4stem/data/pretrained_models/optimal_model.h5`
- [Class definitions](https://github.com/andreasleitherer/ai4stem/tree/main/ai4stem/data/class_definitions): `ai4stem/data/class_definitions`

### Unsupervised analysis

- [Embedding example](https://github.com/andreasleitherer/ai4stem/blob/main/scripts/ai4stem_unsupervised_learning.py): `scripts/ai4stem_unsupervised_learning.py`
- [Representation analysis](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_unsupervised.py): `ai4stem/utils/utils_unsupervised.py`
- [UMAP embedding](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_unsupervised.py): `ai4stem/utils/utils_unsupervised.py`

### Lattice analysis

- [Atomic-column reconstruction](https://github.com/andreasleitherer/ai4stem/blob/main/ai4stem/utils/utils_reconstruction.py): `ai4stem/utils/utils_reconstruction.py`
- [Reference lattices](https://github.com/andreasleitherer/ai4stem/tree/main/ai4stem/data/reference_images): `ai4stem/data/reference_images`

### Other components

- **Microscopy analyst**

## Connections

- Microscopy analyst → Prediction example: runs (evidence: `scripts/ai4stem_prediction.py`)
- Prediction example → Data loaders: loads image (evidence: `scripts/ai4stem_prediction.py`)
- Prediction example → Data loaders: loads model (evidence: `scripts/ai4stem_prediction.py`)
- Prediction example → Image prediction: calls (evidence: `scripts/ai4stem_prediction.py`)
- Prediction example → Data loaders: loads labels (evidence: `scripts/ai4stem_prediction.py`)
- Data loaders → Example image: reads (evidence: `ai4stem/utils/utils_data.py`)
- Data loaders → Pretrained CNN: loads (evidence: `ai4stem/utils/utils_data.py`)
- Data loaders → Class definitions: reads (evidence: `ai4stem/utils/utils_data.py`)
- Image prediction → Local windows: extracts windows (evidence: `ai4stem/utils/utils_prediction.py`)
- Image prediction → FFT descriptors: computes descriptors (evidence: `ai4stem/utils/utils_prediction.py`)
- Image prediction → Data loaders: loads model (evidence: `ai4stem/utils/utils_prediction.py`)
- Image prediction → CNN inference: predicts with uncertainty (evidence: `ai4stem/utils/utils_prediction.py`)
- CNN inference → Pretrained CNN: loads model
- Microscopy analyst → Embedding example: runs (evidence: `scripts/ai4stem_unsupervised_learning.py`)
- Embedding example → Data loaders: loads image (evidence: `scripts/ai4stem_unsupervised_learning.py`)
- Embedding example → Representation analysis: calls (evidence: `scripts/ai4stem_unsupervised_learning.py`)
- Representation analysis → Image prediction: gets descriptors (evidence: `ai4stem/utils/utils_unsupervised.py`)
- Representation analysis → Data loaders: loads model (evidence: `ai4stem/utils/utils_unsupervised.py`)
- Representation analysis → CNN inference: gets representations (evidence: `ai4stem/utils/utils_unsupervised.py`)
- Representation analysis → UMAP embedding: fits and transforms (evidence: `ai4stem/utils/utils_unsupervised.py`)
- UMAP embedding → Microscopy analyst: returns embedding
- CNN training → Pretrained CNN: saves model
- Atomic-column reconstruction → Reference lattices: uses reference lattice
