# softmatterdb/barcode architecture

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

- Interactive diagram: https://gitdiagram.com/softmatterdb/barcode
- Repository: https://github.com/softmatterdb/barcode

## Overview

1. BARCODE is a Python 3.12 desktop analysis app, not a service. The architecture is centered on a GUI that collects run settings, an orchestration layer that executes analysis, and branch-specific computation modules that produce metrics and artifacts.

2. `main.py` is the entrypoint. It starts the GUI and routes the user into either:
   - a fresh analysis run over files/folders, or
   - the barcode/CSV aggregation workflow that reuses previously generated summaries.

3. The GUI layer in `gui/` is split by task, with separate tabs for execution settings, binarization, optical flow, intensity distribution, and barcode generation. This is a strong architectural boundary: each tab maps to one subsystem and its parameters.

4. `analysis.py` is the central coordinator for analysis runs. It likely receives the user’s selected inputs/settings from the GUI, dispatches to the enabled branches, and aggregates their outputs into the summary CSV and optional visualizations.

5. Input handling is isolated in `utils/reader.py`. It is responsible for loading TIFF and ND2 data, iterating over files or directories, and extracting channel-level data. ND2 handling matters because the optical-flow settings can be filled from file metadata instead of manual input.

6. The analysis pipeline is organized into three independent branches under `analysis_branches/`:
   - `image_binarization.py`
   - `optical_flow.py`
   - `intensity_distribution.py`
   Each branch consumes selected frames/channels from the same source video data but computes different metrics, so they can be enabled or disabled independently.

7. The binarization branch converts grayscale frames to binary frames, segments connected components, and measures island/void growth and connectivity over time. It also drives live preview and saved comparison images, with lower-level helpers in `utils/analysis/image_binarization.py`.

8. The optical-flow branch computes frame-to-frame motion fields using Farneback optical flow. Its architecture includes settings that affect temporal sampling, spatial downsampling, and physical-unit conversion. The reduced artifact is the flow field itself, which is also used for visualization and metric computation.

9. The intensity-distribution branch builds per-frame intensity histograms, then derives kurtosis and skewness-based metrics. Its supporting helpers live in `utils/analysis/intensity_distribution.py` and `utils/intensity_distribution.py`, which suggests a split between general histogram utilities and analysis-specific reduction logic.

10. Cross-cutting output concerns are handled in `utils/writer.py`, `utils/visualization.py`, and `utils/preview.py`. These modules are the boundary between computation and artifacts: they write summary CSVs, reduced data structures, PNG summaries, and live/diagnostic previews.

11. BARCODE has two output modes. The normal analysis mode produces per-video, per-channel CSV summaries plus optional visualizations and reduced CSV data. The barcode-generator mode consumes existing CSV summaries, aggregates them, optionally sorts by a metric, and renders a colorized dataset barcode without rerunning analysis.

12. Output is organized around file/channel boundaries. Each processed source file gets its own output folder with per-channel subfolders for images and reduced structures, while the root run location receives the summary CSV and any barcode visualization. Flags in the summary mark low-quality data such as dim or saturated channels.

13. For an architecture diagram, the key data flow is: user settings in GUI → analysis orchestration → reader → selected analysis branches → metrics/artifacts → writer/visualization. The separate CSV aggregation path bypasses frame processing and operates only on previously generated summaries.

Main entry points: [`analysis.py`](https://github.com/softmatterdb/barcode/blob/main/analysis.py) (analysis.py), [`utils/reader.py`](https://github.com/softmatterdb/barcode/blob/main/utils/reader.py) (reader), [`analysis_branches/image_binarization.py`](https://github.com/softmatterdb/barcode/blob/main/analysis_branches/image_binarization.py) (Binarization), [`utils/writer.py`](https://github.com/softmatterdb/barcode/blob/main/utils/writer.py) (writer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_entry["Entry"]
  node_main_py["main.py<br/>entrypoint<br/>[main.py]"]
end

subgraph group_grp_gui["GUI"]
  node_gui_exec["Run settings<br/>gui tab"]
  node_gui_bin["Binarization<br/>gui tab"]
  node_gui_flow["Optical flow<br/>gui tab"]
  node_gui_int["Intensity dist.<br/>gui tab"]
  node_gui_barcode["Barcode gen<br/>gui tab"]
end

subgraph group_grp_core["Core analysis"]
  node_analysis_py["analysis.py<br/>orchestrator<br/>[analysis.py]"]
  node_reader_py["reader<br/>input loader<br/>[reader.py]"]
  node_bin_branch["Binarization<br/>analysis branch"]
  node_flow_branch["Optical flow<br/>analysis branch<br/>[optical_flow.py]"]
  node_int_branch["Intensity dist.<br/>analysis branch"]
end

subgraph group_grp_utils["Helpers"]
  node_bin_helpers["Bin helpers<br/>analysis helper"]
  node_int_helpers["Hist helpers<br/>analysis helper"]
  node_int_utils["Dist utils"]
end

subgraph group_grp_outputs["Outputs"]
  node_writer_py["writer<br/>[writer.py]"]
  node_viz_py["visualization<br/>artifact renderer<br/>[visualization.py]"]
  node_preview_py["preview<br/>[preview.py]"]
end

node_main_py -->|"launches"| node_gui_exec
node_main_py -->|"routes"| node_gui_barcode
node_gui_exec -->|"run config"| node_analysis_py
node_gui_bin -->|"parameters"| node_bin_branch
node_gui_flow -->|"parameters"| node_flow_branch
node_gui_int -->|"parameters"| node_int_branch
node_gui_barcode -->|"csv inputs"| node_writer_py
node_analysis_py -->|"loads data"| node_reader_py
node_reader_py -->|"frames"| node_bin_branch
node_reader_py -->|"frames"| node_flow_branch
node_reader_py -->|"frames"| node_int_branch
node_bin_branch -->|"uses"| node_bin_helpers
node_flow_branch -->|"metrics"| node_writer_py
node_int_branch -->|"uses"| node_int_helpers
node_int_helpers -->|"uses"| node_int_utils
node_bin_branch -->|"preview"| node_preview_py
node_analysis_py -->|"writes"| node_writer_py
node_analysis_py -->|"renders"| node_viz_py
node_writer_py -->|"feeds"| node_viz_py
node_gui_barcode -->|"renders"| node_viz_py

click node_main_py "https://github.com/softmatterdb/barcode/blob/main/main.py"
click node_analysis_py "https://github.com/softmatterdb/barcode/blob/main/analysis.py"
click node_reader_py "https://github.com/softmatterdb/barcode/blob/main/utils/reader.py"
click node_gui_exec "https://github.com/softmatterdb/barcode/blob/main/gui/execution_settings_tab.py"
click node_gui_bin "https://github.com/softmatterdb/barcode/blob/main/gui/image_binarization_tab.py"
click node_gui_flow "https://github.com/softmatterdb/barcode/blob/main/gui/optical_flow_tab.py"
click node_gui_int "https://github.com/softmatterdb/barcode/blob/main/gui/intensity_distribution_tab.py"
click node_gui_barcode "https://github.com/softmatterdb/barcode/blob/main/gui/barcode_generation_tab.py"
click node_bin_branch "https://github.com/softmatterdb/barcode/blob/main/analysis_branches/image_binarization.py"
click node_flow_branch "https://github.com/softmatterdb/barcode/blob/main/analysis_branches/optical_flow.py"
click node_int_branch "https://github.com/softmatterdb/barcode/blob/main/analysis_branches/intensity_distribution.py"
click node_bin_helpers "https://github.com/softmatterdb/barcode/blob/main/utils/analysis/image_binarization.py"
click node_int_helpers "https://github.com/softmatterdb/barcode/blob/main/utils/analysis/intensity_distribution.py"
click node_int_utils "https://github.com/softmatterdb/barcode/blob/main/utils/intensity_distribution.py"
click node_writer_py "https://github.com/softmatterdb/barcode/blob/main/utils/writer.py"
click node_viz_py "https://github.com/softmatterdb/barcode/blob/main/utils/visualization.py"
click node_preview_py "https://github.com/softmatterdb/barcode/blob/main/utils/preview.py"

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_main_py toneBlue
class node_gui_exec,node_gui_bin,node_gui_flow,node_gui_int,node_gui_barcode toneAmber
class node_analysis_py,node_reader_py,node_bin_branch,node_flow_branch,node_int_branch toneMint
class node_bin_helpers,node_int_helpers,node_int_utils toneRose
class node_writer_py,node_viz_py,node_preview_py toneIndigo
```

## Components

### Entry

Application startup and mode routing

- [main.py](https://github.com/softmatterdb/barcode/blob/main/main.py): entrypoint · Starts the desktop app and routes into analysis or barcode generation · `main.py`

### GUI

Task-specific desktop tabs for settings and workflows

- [Run settings](https://github.com/softmatterdb/barcode/blob/main/gui/execution_settings_tab.py): gui tab · Collects analysis inputs, execution options, and run parameters · `gui/execution_settings_tab.py`
- [Binarization](https://github.com/softmatterdb/barcode/blob/main/gui/image_binarization_tab.py): gui tab · Configures binary segmentation and related preview settings · `gui/image_binarization_tab.py`
- [Optical flow](https://github.com/softmatterdb/barcode/blob/main/gui/optical_flow_tab.py): gui tab · Configures motion analysis settings and metadata-derived defaults · `gui/optical_flow_tab.py`
- [Intensity dist.](https://github.com/softmatterdb/barcode/blob/main/gui/intensity_distribution_tab.py): gui tab · Configures histogram and distribution analysis parameters · `gui/intensity_distribution_tab.py`
- [Barcode gen](https://github.com/softmatterdb/barcode/blob/main/gui/barcode_generation_tab.py): gui tab · Configures CSV aggregation and barcode rendering workflow · `gui/barcode_generation_tab.py`

### Core analysis

Run orchestration, data loading, and branch execution

- [analysis.py](https://github.com/softmatterdb/barcode/blob/main/analysis.py): orchestrator · Coordinates analysis runs, dispatches branches, and aggregates results · `analysis.py`
- [reader](https://github.com/softmatterdb/barcode/blob/main/utils/reader.py): input loader · Loads TIFF/ND2 inputs and iterates files, folders, and channels · `utils/reader.py`
- [Binarization](https://github.com/softmatterdb/barcode/blob/main/analysis_branches/image_binarization.py): analysis branch · Segments frames into binary masks and measures connectivity over time · `analysis_branches/image_binarization.py`
- [Optical flow](https://github.com/softmatterdb/barcode/blob/main/analysis_branches/optical_flow.py): analysis branch · Computes Farneback motion fields and flow-based metrics · `analysis_branches/optical_flow.py`
- [Intensity dist.](https://github.com/softmatterdb/barcode/blob/main/analysis_branches/intensity_distribution.py): analysis branch · Builds histograms and derives skewness and kurtosis metrics · `analysis_branches/intensity_distribution.py`

### Helpers

Shared readers, writers, previews, and analysis helpers

- [Bin helpers](https://github.com/softmatterdb/barcode/blob/main/utils/analysis/image_binarization.py): analysis helper · Lower-level binarization logic for segmentation, previews, and comparison images · `utils/analysis/image_binarization.py`
- [Hist helpers](https://github.com/softmatterdb/barcode/blob/main/utils/analysis/intensity_distribution.py): analysis helper · Histogram and reduction helpers for intensity distribution analysis · `utils/analysis/intensity_distribution.py`
- [Dist utils](https://github.com/softmatterdb/barcode/blob/main/utils/intensity_distribution.py): utility · Shared intensity-distribution utilities used by the analysis helpers · `utils/intensity_distribution.py`

### Outputs

Summary CSVs, reduced data, and visual artifacts

- [writer](https://github.com/softmatterdb/barcode/blob/main/utils/writer.py): output writer · Writes summary CSVs, reduced structures, and per-channel artifacts · `utils/writer.py`
- [visualization](https://github.com/softmatterdb/barcode/blob/main/utils/visualization.py): artifact renderer · Renders PNG summaries and barcode-style visual outputs · `utils/visualization.py`
- [preview](https://github.com/softmatterdb/barcode/blob/main/utils/preview.py): live preview · Generates diagnostic and live previews during analysis · `utils/preview.py`

## Connections

- main.py → Run settings: launches · Startup opens the GUI and run settings workflow
- main.py → Barcode gen: routes · Startup can also route into barcode aggregation mode
- Run settings → analysis.py: run config · User-selected inputs and settings are passed into analysis runs
- Binarization → Binarization: parameters · Binarization tab feeds branch-specific settings
- Optical flow → Optical flow: parameters · Optical flow tab feeds branch-specific settings
- Intensity dist. → Intensity dist.: parameters · Intensity tab feeds branch-specific settings
- Barcode gen → writer: csv inputs · Barcode mode consumes existing summaries and aggregation options
- analysis.py → reader: loads data · Coordinator requests TIFF/ND2 inputs and channel data
- reader → Binarization: frames · Loaded frames feed the binarization branch
- reader → Optical flow: frames · Loaded frames feed the optical-flow branch
- reader → Intensity dist.: frames · Loaded frames feed the intensity branch
- Binarization → Bin helpers: uses · Branch delegates segmentation and helper operations to lower-level bin utilities
- Optical flow → writer: metrics · Optical-flow results are reduced into summary outputs
- Intensity dist. → Hist helpers: uses · Branch uses histogram reduction helpers
- Hist helpers → Dist utils: uses · Helper layer relies on shared distribution utilities
- Binarization → preview: preview · Binarization drives live previews and comparison views
- analysis.py → writer: writes · Coordinator emits CSV summaries and reduced data
- analysis.py → visualization: renders · Coordinator triggers optional visual artifact generation
- writer → visualization: feeds · Written summaries and reduced data can be rendered into barcode or report visuals
- Barcode gen → visualization: renders · Barcode workflow renders the aggregated dataset barcode
