# csheaff/us-beamform-linarray architecture

> Architecture diagram of the GitHub repository csheaff/us-beamform-linarray, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/csheaff/us-beamform-linarray
- Repository: https://github.com/csheaff/us-beamform-linarray

## Overview

1. **Repository purpose**
   - This repo is a side-by-side ultrasound beamforming demo, not a reusable framework.
   - It contains two independent implementations of the same idea: a Python educational version and a Rust prototype version.

2. **Top-level structure**
   - `example_us_bmode_sensor_data.h5` is the shared input dataset.
   - `py/linear_array_beamforming.py` is the Python pipeline.
   - `rs/src/main.rs` is the Rust pipeline, with build metadata in `rs/Cargo.toml`.
   - The `result.png` images referenced in the README are generated outputs, showing the final reconstructed B-mode images.

3. **Data source boundary**
   - The only real input is simulated RF sensor data stored in HDF5 format.
   - The data was generated externally by the K-Wave MATLAB toolbox using a linear transducer phantom simulation.
   - That means the repo does not include simulation code; it consumes precomputed raw sensor data and reconstructs an image from it.

4. **Python architecture**
   - The Python script is the clearer “reference” implementation.
   - It loads HDF5 RF data with `h5py`, processes numeric arrays with `numpy` and `scipy`, and visualizes results with `matplotlib`.
   - The README says it demonstrates the conventional ultrasound processing chain and compares simple B-mode imaging, fixed receive-focus beamforming, and dynamic focusing.

5. **Python data flow**
   - Raw sensor data is read from the `.h5` file.
   - The signal is transformed through a sequence of ultrasound reconstruction steps.
   - Intermediate and final images are rendered to visualize the effect of different beamforming strategies.
   - This script is oriented toward explaining the algorithmic stages rather than optimizing runtime.

6. **Rust architecture**
   - The Rust side is a minimally functional prototype focused on the same reconstruction problem.
   - Core array handling is built around `ndarray`.
   - FFT-related work depends on external native libraries through `fftw`.
   - Image-handling/interop support comes from `opencv-rust`, while logging and plot generation use `simplelog` and `plotlib`.

7. **Rust data flow**
   - The Rust program reads the same kind of RF data and reconstructs a beamformed image, currently using dynamic focusing.
   - The README notes that upsampling is slow and signal filtering is not yet implemented.
   - This makes the Rust path more of a performance/portability experiment than a complete signal-processing reference.

8. **Processing boundaries**
   - The main architectural boundary is between input RF acquisition data and output reconstructed image.
   - Within that boundary, the pipeline is classic ultrasound DSP: load data, manipulate sample arrays, beamform, and display/save the result.
   - The repo does not show separate services, network calls, or distributed components.

9. **Dependency and runtime implications**
   - Python depends on the scientific stack: `numpy`, `scipy`, `matplotlib`, `h5py`.
   - Rust relies on native toolchain and system libraries, especially FFTW and OpenCV; the README calls out `gfortran` and `libopenblas-dev` as possible prerequisites.
   - This is important architecturally because the Rust implementation is not purely self-contained; it bridges into external C/C++ ecosystem libraries.

10. **How to draw the diagram**
   - Show one shared dataset feeding two independent pipelines: Python and Rust.
   - Each pipeline should be a single-process batch computation path, not a service.
   - Both terminate in an image output/plot, with Python emphasizing comparison of methods and Rust emphasizing dynamic-focused reconstruction and debugging output.

Main entry points: [`rs/src/main.rs`](https://github.com/csheaff/us-beamform-linarray/blob/master/rs/src/main.rs) (Beamforming), [`example_us_bmode_sensor_data.h5`](https://github.com/csheaff/us-beamform-linarray/blob/master/example_us_bmode_sensor_data.h5) (RF dataset), [`rs/Cargo.toml`](https://github.com/csheaff/us-beamform-linarray/blob/master/rs/Cargo.toml) (Cargo manifest)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_shared["Shared data"]
  node_dataset[("RF dataset<br/>HDF5 input")]
end

subgraph group_python["Python pipeline"]
  node_py_script["Beamforming<br/>Python script"]
  node_py_load["Load data<br/>h5py I/O"]
  node_py_chain["DSP chain<br/>numpy/scipy"]
  node_py_modes["Method compare<br/>B-mode variants"]
  node_py_plot["Render plots<br/>matplotlib output"]
  node_py_artifact["Image output<br/>generated image"]
end

subgraph group_rust["Rust pipeline"]
  node_rs_manifest["Cargo manifest<br/>build config<br/>[Cargo.toml]"]
  node_rs_main["Beamforming<br/>Rust binary<br/>[main.rs]"]
  node_rs_load["Read RF<br/>input stage"]
  node_rs_arrays["Array core<br/>ndarray"]
  node_rs_focus["Dynamic focus<br/>beamforming"]
  node_rs_fft["FFT bridge<br/>fftw"]
  node_rs_img["Image bridge<br/>opencv-rust"]
  node_rs_log["Debug output<br/>simplelog + plotlib"]
  node_rs_artifact["Image output<br/>generated image"]
end

node_dataset -->|"read"| node_py_load
node_py_load -->|"samples"| node_py_chain
node_py_chain -->|"variants"| node_py_modes
node_py_modes -->|"render"| node_py_plot
node_py_plot -->|"save"| node_py_artifact
node_dataset -->|"read"| node_rs_load
node_rs_manifest -->|"build"| node_rs_main
node_rs_main -->|"buffers"| node_rs_arrays
node_rs_main -->|"beamform"| node_rs_focus
node_rs_main -->|"fft"| node_rs_fft
node_rs_main -->|"image"| node_rs_img
node_rs_main -->|"debug"| node_rs_log
node_rs_focus -->|"save"| node_rs_artifact
node_rs_load -->|"populate"| node_rs_arrays
node_rs_focus -->|"trace"| node_rs_log

click node_dataset "https://github.com/csheaff/us-beamform-linarray/blob/master/example_us_bmode_sensor_data.h5"
click node_py_script "https://github.com/csheaff/us-beamform-linarray/blob/master/py/linear_array_beamforming.py"
click node_rs_manifest "https://github.com/csheaff/us-beamform-linarray/blob/master/rs/Cargo.toml"
click node_rs_main "https://github.com/csheaff/us-beamform-linarray/blob/master/rs/src/main.rs"

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_dataset toneBlue
class node_py_script,node_py_load,node_py_chain,node_py_modes,node_py_plot,node_py_artifact toneAmber
class node_rs_manifest,node_rs_main,node_rs_load,node_rs_arrays,node_rs_focus,node_rs_fft,node_rs_img,node_rs_log,node_rs_artifact toneMint
```

## Components

### Shared data

Common ultrasound input dataset consumed by both pipelines

- [RF dataset](https://github.com/csheaff/us-beamform-linarray/blob/master/example_us_bmode_sensor_data.h5): HDF5 input · Simulated ultrasound sensor data consumed by both pipelines · `example_us_bmode_sensor_data.h5`

### Python pipeline

Reference beamforming implementation

- [Beamforming](https://github.com/csheaff/us-beamform-linarray/blob/master/py/linear_array_beamforming.py): Python script · Educational reconstruction pipeline for conventional ultrasound processing · `py/linear_array_beamforming.py`
- **Load data**: h5py I/O · Reads RF samples and metadata from HDF5
- **DSP chain**: numpy/scipy · Array transforms and signal processing stages
- **Method compare**: B-mode variants · Compares basic imaging, fixed focus, and dynamic focusing
- **Render plots**: matplotlib output · Visualizes intermediate and final reconstructed images
- **Image output**: generated image · Saved reconstructed B-mode figure produced by the Python demo

### Rust pipeline

Prototype beamforming implementation

- [Cargo manifest](https://github.com/csheaff/us-beamform-linarray/blob/master/rs/Cargo.toml): build config · Rust dependency and build metadata · `rs/Cargo.toml`
- [Beamforming](https://github.com/csheaff/us-beamform-linarray/blob/master/rs/src/main.rs): Rust binary · Prototype dynamic-focused reconstruction program · `rs/src/main.rs`
- **Read RF**: input stage · Loads the same ultrasound sample data into Rust buffers
- **Array core**: ndarray · Numerical buffer and intermediate array handling
- **Dynamic focus**: beamforming · Reconstructs the image with dynamic receive focusing
- **FFT bridge**: fftw · Native FFT and signal-processing dependency
- **Image bridge**: opencv-rust · Interop for image handling and display support
- **Debug output**: simplelog + plotlib · Logging and lightweight plot generation for inspection
- **Image output**: generated image · Reconstructed beamformed image produced by the Rust demo

## Connections

- RF dataset → Load data: read · Python ingests the shared HDF5 RF dataset
- Load data → DSP chain: samples · Loaded RF samples enter the reconstruction chain
- DSP chain → Method compare: variants · The pipeline produces multiple beamforming modes for comparison
- Method compare → Render plots: render · Comparison results are visualized as plots and images
- Render plots → Image output: save · Final figures are written as generated image output
- RF dataset → Read RF: read · Rust consumes the same kind of RF input data
- Cargo manifest → Beamforming: build · Cargo metadata defines the binary and external dependencies
- Beamforming → Array core: buffers · Main reconstruction logic operates on ndarray-backed sample buffers
- Beamforming → Dynamic focus: beamform · Dynamic receive focusing reconstructs the image
- Beamforming → FFT bridge: fft · Signal processing depends on native FFTW support
- Beamforming → Image bridge: image · Image interop supports display or export of results
- Beamforming → Debug output: debug · Logging and plotting support runtime inspection
- Dynamic focus → Image output: save · Reconstruction terminates in a generated image artifact
- Read RF → Array core: populate · Loaded RF data becomes numerical arrays for processing
- Dynamic focus → Debug output: trace · Beamforming stages can be inspected through logs and plots
