# google/neuroglancer architecture

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

- Interactive diagram: https://gitdiagram.com/google/neuroglancer
- Repository: https://github.com/google/neuroglancer

## Overview

##### Main project type and purpose
- The README tells you that Neuroglancer is a WebGL-based viewer for volumetric data. It displays complex 3D and cross-sectional views along with 3-D meshes and skeletons.
- The project supports multiple data source formats (precomputed format, N5, Zarr, Deep Zoom, etc.) and even includes integrations with Python for mesh generation and additional processing.
- A major point is that the viewer is designed to be fast and responsive using a split architecture for the UI (frontend) and asynchronous background processing (backend web worker).

##### Break down the directory structure into major components
- Frontend/UI – Code in directories like “src/ui”, “src/viewer.ts”, “src/layer”, “src/widget”, and others provide all the user interface elements, WebGL rendering logic, event handling, and interaction.
- Backend/Worker – The asynchronous tasks, such as data decoding and computation, are handled in directories such as “src/async_computation”, “src/chunk_manager”, and files related to worker RPC (“src/worker_rpc.ts”, “src/worker_rpc_context.ts”). This represents the separate WebWorker thread that handles data fetching and pre-processing.
- Python Integration – The “python” directory (and its associated build tools) indicate a separate layer for bridging Neuroglancer with Python tools (including auto mesh-generation and integration components).
- Build and Tools – There are build scripts in “build_tools”, examples in the “examples” directory (for different bundlers like Parcel, Rspack, Vite, Webpack), as well as configuration and test directories. These are essential for building and testing the project but less central to the runtime architecture.

##### Relationships and interactions between components
- The Backend Worker is tasked with queuing, downloading, and preprocessing data (e.g., decoding image formats like JPEG, PNG, Zstandard, etc.). It uses modules like “async_computation” and “chunk_manager” to process data requests.
- The Data Source Modules abstract how data is loaded from various external endpoints. They reside at a lower level in the worker and are responsible for fetching data over HTTP (or interfacing with cloud storage) and applying the required transformations.
- The Python integration can be depicted as an external optional component that interfaces (e.g., via wrapper libraries or command‐line tools) with Neuroglancer for specific tasks like mesh generation.
- The build/test tools (e.g., using Node.js, Vitest, TypeScript configurations) support development, so you may choose to show them as the “development toolchain” besides the runtime flow.

##### Architectural patterns and design principles
- Modularity and Extensibility: The “datasource” folder is organized into multiple modules. This shows that Neuroglancer uses a plug‐in–style architecture where new data formats can be added with minimal changes to the overall system.
- Separation of Concerns: The codebase separates concerns: UI rendering, asynchronous processing, data fetching/decoding, and integration with cloud/data storage systems.

##### Special attention to details
- That the system is multi-threaded (frontend vs. backend) and note that all DOM and WebGL operations are performed solely on the main UI thread.
- Architectural patterns:
– “Asynchronous Message Passing” in the communication layer.
– “Modular Data Source Abstraction” in the datasource modules.
- Although the file-tree shows many test files and build configurations, the primary focus for the runtime design is the interaction between the viewer, its background worker, and the various data source modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend UI Subgraph
    subgraph "Frontend UI"
        ui1["UI Components"]:::ui
        ui2["Viewer Core"]:::ui
        ui3["Layer Components"]:::ui
        ui4["Widget Components"]:::ui
    end

    %% Worker RPC Subgraph
    subgraph "Worker RPC"
        rpc1["Worker RPC"]:::rpc
        rpc2["RPC Context"]:::rpc
    end

    %% Backend Worker Subgraph
    subgraph "Backend Worker"
        bw1["Async Computation"]:::worker
        bw2["Chunk Manager"]:::worker
    end

    %% Data Source Abstraction Node
    ds["Data Source Abstraction"]:::datasource

    %% Python Integration Subgraph
    subgraph "Python Integration"
        py1["Python Integration"]:::python
        py2["Neuroglancer Bridge"]:::python
    end

    %% Development Toolchain Subgraph
    subgraph "Development Toolchain"
        dt1["Build Tools"]:::dev
        dt2["Examples"]:::dev
        dt3["Tests"]:::dev
    end

    %% Communication Flow between components
    ui2 -->|"sendCommand"| rpc1
    rpc1 -->|"bridge"| rpc2
    rpc1 <-->|"messagePassing"| bw1
    bw2 -->|"managesChunks"| bw1
    bw1 -->|"fetchData"| ds
    ds -->|"rawData"| bw1
    bw1 -->|"processedData"| rpc1
    rpc1 -->|"updateUI"| ui2
    py1 -->|"meshGeneration"| bw1

    %% Click Events for Frontend/UI
    click ui1 "https://github.com/google/neuroglancer/tree/master/src/ui/"
    click ui2 "https://github.com/google/neuroglancer/blob/master/src/viewer.ts"
    click ui3 "https://github.com/google/neuroglancer/tree/master/src/layer/"
    click ui4 "https://github.com/google/neuroglancer/tree/master/src/widget/"

    %% Click Events for Worker RPC
    click rpc1 "https://github.com/google/neuroglancer/blob/master/src/worker_rpc.ts"
    click rpc2 "https://github.com/google/neuroglancer/blob/master/src/worker_rpc_context.ts"

    %% Click Events for Backend Worker / Asynchronous Computation
    click bw1 "https://github.com/google/neuroglancer/tree/master/src/async_computation/"
    click bw2 "https://github.com/google/neuroglancer/tree/master/src/chunk_manager/"

    %% Click Event for Data Source Abstraction
    click ds "https://github.com/google/neuroglancer/tree/master/src/datasource/"

    %% Click Events for Python Integration
    click py1 "https://github.com/google/neuroglancer/tree/master/python/"
    click py2 "https://github.com/google/neuroglancer/tree/master/python/neuroglancer/"

    %% Click Events for Development Toolchain
    click dt1 "https://github.com/google/neuroglancer/tree/master/build_tools/"
    click dt2 "https://github.com/google/neuroglancer/tree/master/examples/"
    click dt3 "https://github.com/google/neuroglancer/tree/master/tests/"

    %% Styles
    classDef ui fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef rpc fill:#ffcccb,stroke:#000,stroke-width:2px;
    classDef worker fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef datasource fill:#ffff99,stroke:#000,stroke-width:2px;
    classDef python fill:#ffdead,stroke:#000,stroke-width:2px;
    classDef dev fill:#d3d3d3,stroke:#000,stroke-width:2px;
```
