# gihankaushyal/hit_finder architecture

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

- Interactive diagram: https://gitdiagram.com/gihankaushyal/hit_finder
- Repository: https://github.com/gihankaushyal/hit_finder

## Overview

#### Purpose and Entry Points
This is a Python/PyTorch research pipeline for detector-agnostic SFX hit classification across AGIPD, JUNGFRAU, ePix10k, and Eiger4M. Primary executable training entry points are `src/training/train_supervised.py`, `src/training/train_ssl_pretrain.py`, and `src/training/train_ssl_finetune.py`; experiment selection is YAML-driven under `configs/`.

#### Input and Detector Boundary
Raw HDF5/CXI data and pre-assembled `.img` files enter through detector-aware I/O. Detector identity is read from metadata, not image appearance. `src/preprocessing/io.py` loads formats; `src/preprocessing/geometry.py` applies detector-specific geometry assets such as `src/preprocessing/data/agipd.geom` and `src/preprocessing/data/epix10k.geom`. Reborn is the material external geometry/assembly dependency; Fabio supports `.img`.

#### Shared Preprocessing Contract
`src/preprocessing/pipeline.py` orchestrates the invariant preprocessing sequence: assemble detector panels, compute global contrast normalization on the full assembled frame, then crop/tile patches and apply local normalization. `src/preprocessing/normalize.py` implements GCN/LCN; `src/preprocessing/augment.py` applies training augmentation and peak-aware cutout. The critical boundary is that 224×224 patches are cropped, never resized, and GCN precedes all crops.

#### Hitfinding and Patch Label Generation
The hitfinder subsystem detects Bragg-peak centroids used to create reliable patch labels rather than assigning a frame label to arbitrary crops. It exposes interchangeable implementations through `src/hitfinders/base.py`, with CPU PF8 paths in `src/hitfinders/pf8.py` and `src/hitfinders/numpy_pf8.py`, plus GPU paths in `src/hitfinders/gpu.py` and `src/hitfinders/gpu_pf8.py`. A compiled PF8 wrapper is available in `src/hitfinders/_pf8_wrap.c`.

#### Dataset and Training Data Flow
`src/data/dataset.py` and `src/data/dataloader.py` bridge preprocessed frames into PyTorch batches. The asymmetric supervised path samples positive crops centered on hitfinder centroids and hard-negative crops kept away from peaks; both are augmented after cropping. `src/data/synthetic.py` supplies synthetic-data support. Data is expected under `data/raw`, `data/processed`, `data/splits`, and `data/synthetic`.

#### Model Tracks
The supervised baseline is defined in `src/models/supervised.py` and trained by `src/training/train_supervised.py`, using configurable ResNet variants including `configs/supervised/resnet18_asymmetric.yaml` and `configs/supervised/resnet50.yaml`. The SSL track is defined in `src/models/ssl.py`: MAE/ViT pretraining on unlabeled frames is run by `src/training/train_ssl_pretrain.py`, followed by labeled classification fine-tuning in `src/training/train_ssl_finetune.py`. PyTorch 2.x and timm are core runtime dependencies.

#### Evaluation and Compute
`src/evaluation/benchmark.py` performs cross-detector, leave-one-detector-out evaluation; `src/evaluation/metrics.py` calculates classification metrics. Evaluation tiles full frames into 224×224 patches, scores patches, and aggregates patch votes into frame predictions. The intended execution platform is ASU Sol HPC with CUDA GPUs and SLURM; submission workflows are represented by `scripts/submit_all_lodo_folds.sh`, `scripts/submit_ssl_pretrain.sh`, and `scripts/templates/slurm_gpu_job.sh`.

Main entry points: [`src/data/dataset.py`](https://github.com/gihankaushyal/hit_finder/blob/main/src/data/dataset.py) (Asymmetric patch dataset), [`src/preprocessing/pipeline.py`](https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/pipeline.py) (Preprocessing pipeline), [`src/hitfinders/base.py`](https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/base.py) (Hitfinder interface), [`src/data/dataloader.py`](https://github.com/gihankaushyal/hit_finder/blob/main/src/data/dataloader.py) (PyTorch dataloader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Input &amp; preprocessing"]
  node_raw_data[("Raw frames<br/>HDF5/CXI/.img")]
  node_detector_io["Detector I/O<br/>loader<br/>[io.py]"]
  node_geometry["Geometry assembly<br/>preprocessing<br/>[geometry.py]"]
  node_preprocessing["Preprocessing pipeline<br/>[pipeline.py]"]
  node_augmentation["Augmentation<br/>preprocessing<br/>[augment.py]"]
end

subgraph group_hitfinding["Hitfinding"]
  node_hitfinder_api["Hitfinder interface<br/>abstraction<br/>[base.py]"]
  node_pf8_cpu["PF8 CPU<br/>hitfinder<br/>[pf8.py]"]
  node_pf8_gpu["PF8 GPU<br/>hitfinder<br/>[gpu_pf8.py]"]
end

subgraph group_data["Training data"]
  node_patch_dataset["Asymmetric patch dataset<br/>[dataset.py]"]
  node_dataloader["PyTorch dataloader<br/>batching<br/>[dataloader.py]"]
  node_synthetic_data["Synthetic data<br/>data support<br/>[synthetic.py]"]
end

subgraph group_models["Model tracks"]
  node_supervised_model["ResNet classifier<br/>PyTorch model<br/>[supervised.py]"]
  node_supervised_train["Supervised training<br/>entry point"]
  node_ssl_model["MAE/ViT SSL<br/>PyTorch model<br/>[ssl.py]"]
  node_ssl_pretrain["SSL pretraining<br/>entry point"]
  node_ssl_finetune["SSL fine-tuning<br/>entry point"]
end

subgraph group_evaluation["Evaluation &amp; compute"]
  node_benchmark["LODO benchmark<br/>evaluation<br/>[benchmark.py]"]
  node_metrics["Classification metrics<br/>evaluation<br/>[metrics.py]"]
  node_slurm["SLURM GPU jobs<br/>HPC execution"]
end

node_raw_data -->|"loads"| node_detector_io
node_detector_io -->|"detector metadata"| node_geometry
node_geometry -->|"assembled frame"| node_preprocessing
node_preprocessing -->|"full frame"| node_hitfinder_api
node_hitfinder_api -->|"CPU implementation"| node_pf8_cpu
node_hitfinder_api -->|"GPU implementation"| node_pf8_gpu
node_preprocessing -->|"cropped patches"| node_patch_dataset
node_hitfinder_api -->|"peak centroids"| node_patch_dataset
node_augmentation -->|"post-crop augmentation"| node_patch_dataset
node_synthetic_data -.->|"optional samples"| node_patch_dataset
node_patch_dataset -->|"labeled patches"| node_dataloader
node_dataloader -->|"batches"| node_supervised_train
node_supervised_train -->|"trains"| node_supervised_model
node_preprocessing -->|"unlabeled frames"| node_ssl_pretrain
node_ssl_pretrain -->|"pretrains"| node_ssl_model
node_ssl_model -->|"initializes"| node_ssl_finetune
node_dataloader -->|"labeled batches"| node_ssl_finetune
node_supervised_model -->|"patch scores"| node_benchmark
node_ssl_model -->|"patch scores"| node_benchmark
node_benchmark -->|"frame predictions"| node_metrics
node_slurm -.->|"submits jobs"| node_supervised_train
node_slurm -.->|"submits jobs"| node_ssl_pretrain

click node_raw_data "https://github.com/gihankaushyal/hit_finder/tree/main/data/raw"
click node_detector_io "https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/io.py"
click node_geometry "https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/geometry.py"
click node_preprocessing "https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/pipeline.py"
click node_augmentation "https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/augment.py"
click node_hitfinder_api "https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/base.py"
click node_pf8_cpu "https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/pf8.py"
click node_pf8_gpu "https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/gpu_pf8.py"
click node_patch_dataset "https://github.com/gihankaushyal/hit_finder/blob/main/src/data/dataset.py"
click node_dataloader "https://github.com/gihankaushyal/hit_finder/blob/main/src/data/dataloader.py"
click node_synthetic_data "https://github.com/gihankaushyal/hit_finder/blob/main/src/data/synthetic.py"
click node_supervised_model "https://github.com/gihankaushyal/hit_finder/blob/main/src/models/supervised.py"
click node_supervised_train "https://github.com/gihankaushyal/hit_finder/blob/main/src/training/train_supervised.py"
click node_ssl_model "https://github.com/gihankaushyal/hit_finder/blob/main/src/models/ssl.py"
click node_ssl_pretrain "https://github.com/gihankaushyal/hit_finder/blob/main/src/training/train_ssl_pretrain.py"
click node_ssl_finetune "https://github.com/gihankaushyal/hit_finder/blob/main/src/training/train_ssl_finetune.py"
click node_benchmark "https://github.com/gihankaushyal/hit_finder/blob/main/src/evaluation/benchmark.py"
click node_metrics "https://github.com/gihankaushyal/hit_finder/blob/main/src/evaluation/metrics.py"
click node_slurm "https://github.com/gihankaushyal/hit_finder/blob/main/scripts/submit_all_lodo_folds.sh"

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_raw_data,node_detector_io,node_geometry,node_preprocessing,node_augmentation toneBlue
class node_hitfinder_api,node_pf8_cpu,node_pf8_gpu toneAmber
class node_patch_dataset,node_dataloader,node_synthetic_data toneMint
class node_supervised_model,node_supervised_train,node_ssl_model,node_ssl_pretrain,node_ssl_finetune toneRose
class node_benchmark,node_metrics,node_slurm toneIndigo
```

## Components

### Input & preprocessing

Detector-aware frame assembly and patch preparation.

- [Raw frames](https://github.com/gihankaushyal/hit_finder/tree/main/data/raw): HDF5/CXI/.img · Raw, processed, split, and synthetic datasets. · `data/raw`
- [Detector I/O](https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/io.py): loader · Reads detector metadata and supported frame formats. · `src/preprocessing/io.py`
- [Geometry assembly](https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/geometry.py): preprocessing · Applies detector-specific panel geometry via Reborn. · `src/preprocessing/geometry.py`
- [Preprocessing pipeline](https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/pipeline.py): pipeline · Assembles frames, applies GCN, then crops 224×224 patches and LCN. · `src/preprocessing/pipeline.py`
- [Augmentation](https://github.com/gihankaushyal/hit_finder/blob/main/src/preprocessing/augment.py): preprocessing · Applies training augmentation and peak-aware cutout after cropping. · `src/preprocessing/augment.py`

### Hitfinding

Peak centroids drive reliable crop labels.

- [Hitfinder interface](https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/base.py): abstraction · Interchangeable peak-centroid detection implementations. · `src/hitfinders/base.py`
- [PF8 CPU](https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/pf8.py): hitfinder · Python, NumPy, and compiled PF8 paths. · `src/hitfinders/pf8.py`
- [PF8 GPU](https://github.com/gihankaushyal/hit_finder/blob/main/src/hitfinders/gpu_pf8.py): hitfinder · GPU peak detection path. · `src/hitfinders/gpu_pf8.py`

### Training data

Patch sampling and PyTorch batching.

- [Asymmetric patch dataset](https://github.com/gihankaushyal/hit_finder/blob/main/src/data/dataset.py): dataset · Centers positives on peaks and keeps hard negatives away from peaks. · `src/data/dataset.py`
- [PyTorch dataloader](https://github.com/gihankaushyal/hit_finder/blob/main/src/data/dataloader.py): batching · Builds training and evaluation batches. · `src/data/dataloader.py`
- [Synthetic data](https://github.com/gihankaushyal/hit_finder/blob/main/src/data/synthetic.py): data support · `src/data/synthetic.py`

### Model tracks

Supervised and self-supervised classifiers.

- [ResNet classifier](https://github.com/gihankaushyal/hit_finder/blob/main/src/models/supervised.py): PyTorch model · Configurable supervised ResNet variants. · `src/models/supervised.py`
- [Supervised training](https://github.com/gihankaushyal/hit_finder/blob/main/src/training/train_supervised.py): entry point · YAML-selected baseline training. · `src/training/train_supervised.py`
- [MAE/ViT SSL](https://github.com/gihankaushyal/hit_finder/blob/main/src/models/ssl.py): PyTorch model · MAE/ViT pretraining and classification fine-tuning model. · `src/models/ssl.py`
- [SSL pretraining](https://github.com/gihankaushyal/hit_finder/blob/main/src/training/train_ssl_pretrain.py): entry point · Trains on unlabeled frames. · `src/training/train_ssl_pretrain.py`
- [SSL fine-tuning](https://github.com/gihankaushyal/hit_finder/blob/main/src/training/train_ssl_finetune.py): entry point · Fine-tunes SSL representations with labels. · `src/training/train_ssl_finetune.py`

### Evaluation & compute

Cross-detector benchmarking and cluster execution.

- [LODO benchmark](https://github.com/gihankaushyal/hit_finder/blob/main/src/evaluation/benchmark.py): evaluation · Tiles frames, scores patches, and aggregates patch votes. · `src/evaluation/benchmark.py`
- [Classification metrics](https://github.com/gihankaushyal/hit_finder/blob/main/src/evaluation/metrics.py): evaluation · `src/evaluation/metrics.py`
- [SLURM GPU jobs](https://github.com/gihankaushyal/hit_finder/blob/main/scripts/submit_all_lodo_folds.sh): HPC execution · ASU Sol CUDA submission workflows. · `scripts/submit_all_lodo_folds.sh`

## Connections

- Raw frames → Detector I/O: loads
- Detector I/O → Geometry assembly: detector metadata
- Geometry assembly → Preprocessing pipeline: assembled frame
- Preprocessing pipeline → Hitfinder interface: full frame
- Hitfinder interface → PF8 CPU: CPU implementation
- Hitfinder interface → PF8 GPU: GPU implementation
- Preprocessing pipeline → Asymmetric patch dataset: cropped patches
- Hitfinder interface → Asymmetric patch dataset: peak centroids
- Augmentation → Asymmetric patch dataset: post-crop augmentation
- Synthetic data → Asymmetric patch dataset: optional samples
- Asymmetric patch dataset → PyTorch dataloader: labeled patches
- PyTorch dataloader → Supervised training: batches
- Supervised training → ResNet classifier: trains
- Preprocessing pipeline → SSL pretraining: unlabeled frames
- SSL pretraining → MAE/ViT SSL: pretrains
- MAE/ViT SSL → SSL fine-tuning: initializes
- PyTorch dataloader → SSL fine-tuning: labeled batches
- ResNet classifier → LODO benchmark: patch scores
- MAE/ViT SSL → LODO benchmark: patch scores
- LODO benchmark → Classification metrics: frame predictions
- SLURM GPU jobs → Supervised training: submits jobs
- SLURM GPU jobs → SSL pretraining: submits jobs
