# neurotechnology-at-eth-zurich/implant architecture

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

- Interactive diagram: https://gitdiagram.com/neurotechnology-at-eth-zurich/implant
- Repository: https://github.com/neurotechnology-at-eth-zurich/implant

## Overview

#### Purpose and entry points
IMPLAnT is a Linux/Python 3.10 desktop GUI for rat intracranial electrode workflows: MRI-to-atlas planning, intraoperative coordinate correction, post-implant electrode localisation, and anatomy-linked ephys viewing. The application starts at `main_window.py`; Qt UI definitions are `form.ui`, generated bindings `ui_form.py`, and 3D planning UI `form_tp_3d.ui`.

#### Application shell and shared imaging state
`main_window.py` is the integration boundary for file/menu actions and launches the major workflows. MRI loading, layer state, cursor interaction, and measurements are concentrated in `core/load_MRI_file.py`, `core/image_layer.py`, and `core/interactor_style.py`. This shared imaging context is the handoff point between registration, planning, painting/localisation, and visualization.

#### MRI ingestion, rendering, and annotation
MRI files and metadata enter through `file_handling/loader.py`, `file_handling/mri_volume.py`, and `file_handling/metadata.py`. The GUI supports 3D/4D volume viewing and editing, including image resampling and manual segmentation/painting: `file_handling/resample_data.py`, `segmentation/segmentation_utils.py`, and `core/paintbrush.py`. These stages produce persistent NIfTI-derived images/masks and session-folder artifacts rather than a server-side datastore.

#### Atlas registration and coordinate transforms
Subject MRI is registered to the Waxholm Space atlas through the bundled SAMRI code and ANTs binaries. Registration-facing modules include `core/registration.py`, `samri/samri/optimization/registration.py`, and `samri/samri/pipelines/preprocess.py`. Atlas acquisition/caching is isolated in `mrid_utils/atlas_fetch.py` and registry/path logic in `mrid_utils/atlas_registry.py`. ANTs is an external executable dependency under `ants/bin`; the atlas is downloaded once or read from configured local storage.

#### Pre-surgical trajectory planning
Trajectory planning consumes a registered pre-surgical MRI and atlas-space geometry to position shanks and targets in multi-planar/3D views. Its domain model and UI are in `trajectory_planning/trajectory_planning.py`, `trajectory_planning/shank.py`, and `trajectory_planning/visualisation3D.py`. Coordinate conversion and registration interoperability sit in `trajectory_planning/coord_transform.py` and `trajectory_planning/registration.py`. Planning output includes shank data and a report PDF, intended as the contract for surgery and later localisation.

#### Intraoperative correction
The surgery workflow loads a prior plan/report, resolves the associated scan, and recalculates target positions from measured bregma/lambda coordinates. Control/UI is centered on `during_surgery/surgery_controller.py`, with plan loading in `during_surgery/load_surgery_plan.py` and spatial reprojection in `during_surgery/reprojection.py`. This is a distinct correction layer over the planned trajectory, not a replacement for atlas registration.

#### Post-implant localisation and region assignment
Post-implant data is resampled and registered to pre-surgical MRI before semi-supervised MRID tag localisation. Core pipeline modules are `core/electrode_localization.py`, `core/mrid_tags.py`, and `core/dfx_geometry_panel.py`; channel-to-atlas mapping utilities live in `mrid_utils/channel_mapper.py`, `mrid_utils/point_mapper.py`, and `mrid_utils/roi.py`. Inputs include painted anatomy/electrode traces, the registration transform, atlas registration, implanted-shank data, and the experiment-specific `mrid_library.pkl`; outputs are channel coordinates and atlas-region labels under the session’s analysis directories.

#### Ephys visualization and operational boundaries
Ephys loading and anatomy-linked viewing are implemented in `ephys/init_ephys.py`, `ephys/ephysrecording.py`, and `ephys/visualisationEphys.py`, with 3D electrode context in `ephys/visualisation3D.py`. The main supported flow is recording data plus localisation-derived channel metadata into curated, region-colored traces; advanced analysis utilities are present but README marks preprocessing/analysis as future scope. Runtime configuration is local `paths_config.json`, including raw-data root, atlas location, and optional MRID library path. Optional Bruker acquisition integration exists in the vendored SAMRI package (`samri/samri/fetch/local.py`); standalone packaging is defined by `MRID_GUI.spec`.

Main entry points: [`main_window.py`](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/main_window.py) (Main window), [`core/load_MRI_file.py`](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/load_MRI_file.py) (Shared imaging context), [`core/registration.py`](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/registration.py) (MRI-to-atlas registration), [`core/electrode_localization.py`](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/electrode_localization.py) (Electrode localisation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_shell["Desktop shell"]
  node_main["Main window<br/>Qt application<br/>[main_window.py]"]
  node_ui["Qt UI definitions<br/>Qt forms<br/>[form.ui]"]
end

subgraph group_imaging["Imaging workspace"]
  node_mri_loader["MRI ingestion<br/>volume loader<br/>[loader.py]"]
  node_mri_volume["MRI volume &amp; metadata<br/>imaging model<br/>[mri_volume.py]"]
  node_imaging_context["Shared imaging context<br/>MRI workspace<br/>[load_MRI_file.py]"]
  node_layers["Image layers<br/>layer state<br/>[image_layer.py]"]
  node_painting["Painting &amp; segmentation<br/>annotation tools<br/>[paintbrush.py]"]
  node_artifacts[("Session images &amp; masks<br/>local artifacts")]
end

subgraph group_atlas["Atlas &amp; registration"]
  node_atlas_fetch[("Atlas cache<br/>atlas acquisition<br/>[atlas_fetch.py]")]
  node_registration["MRI-to-atlas registration<br/>registration pipeline<br/>[registration.py]"]
  node_samri["SAMRI preprocessing<br/>vendored pipeline<br/>[preprocess.py]"]
  node_ants{{"ANTs binaries<br/>external executable"}}
end

subgraph group_workflows["Clinical workflows"]
  node_planning["Trajectory planning<br/>planning workflow"]
  node_plan_output["Plan &amp; surgery report<br/>planning artifact"]
  node_surgery["Intraoperative correction<br/>surgery workflow"]
  node_localisation["Electrode localisation<br/>post-implant pipeline"]
  node_channel_mapping["Channel-to-atlas mapping<br/>region assignment<br/>[channel_mapper.py]"]
end

subgraph group_ephys["Ephys viewing"]
  node_ephys_loader["Ephys recording loader<br/>recording model<br/>[ephysrecording.py]"]
  node_ephys_viewer["Anatomy-linked ephys viewer<br/>trace visualization"]
end

node_main -->|"renders"| node_ui
node_main -->|"loads MRI into"| node_imaging_context
node_main -->|"launches"| node_planning
node_main -->|"launches"| node_surgery
node_main -->|"launches"| node_localisation
node_main -->|"launches"| node_ephys_viewer
node_mri_loader -->|"creates"| node_mri_volume
node_mri_volume -->|"supplies volumes"| node_imaging_context
node_imaging_context -->|"manages"| node_layers
node_imaging_context -->|"provides editing context"| node_painting
node_painting -->|"writes masks"| node_artifacts
node_imaging_context -->|"supplies subject MRI"| node_registration
node_atlas_fetch -->|"supplies atlas"| node_registration
node_registration -->|"uses"| node_samri
node_samri -->|"executes"| node_ants
node_registration -->|"provides atlas transform"| node_planning
node_planning -->|"writes"| node_plan_output
node_plan_output -->|"loads prior plan"| node_surgery
node_plan_output -->|"supplies implanted shanks"| node_localisation
node_artifacts -->|"supplies traces and anatomy"| node_localisation
node_registration -->|"supplies transforms"| node_localisation
node_localisation -->|"localises tags"| node_channel_mapping
node_channel_mapping -->|"writes channel metadata"| node_artifacts
node_ephys_loader -->|"supplies recordings"| node_ephys_viewer
node_channel_mapping -->|"supplies region labels"| node_ephys_viewer

click node_main "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/main_window.py"
click node_ui "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/form.ui"
click node_mri_loader "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/file_handling/loader.py"
click node_mri_volume "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/file_handling/mri_volume.py"
click node_imaging_context "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/load_MRI_file.py"
click node_layers "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/image_layer.py"
click node_painting "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/paintbrush.py"
click node_atlas_fetch "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/mrid_utils/atlas_fetch.py"
click node_registration "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/registration.py"
click node_samri "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/samri/samri/pipelines/preprocess.py"
click node_planning "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/trajectory_planning/trajectory_planning.py"
click node_surgery "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/during_surgery/surgery_controller.py"
click node_localisation "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/electrode_localization.py"
click node_channel_mapping "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/mrid_utils/channel_mapper.py"
click node_ephys_loader "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/ephys/ephysrecording.py"
click node_ephys_viewer "https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/ephys/visualisationEphys.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,node_ui toneBlue
class node_mri_loader,node_mri_volume,node_imaging_context,node_layers,node_painting,node_artifacts toneAmber
class node_atlas_fetch,node_registration,node_samri,node_ants toneMint
class node_planning,node_plan_output,node_surgery,node_localisation,node_channel_mapping toneRose
class node_ephys_loader,node_ephys_viewer toneIndigo
```

## Components

### Desktop shell

- [Main window](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/main_window.py): Qt application · Integration boundary for menus and workflow launch. · `main_window.py`
- [Qt UI definitions](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/form.ui): Qt forms · `form.ui`

### Imaging workspace

- [MRI ingestion](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/file_handling/loader.py): volume loader · `file_handling/loader.py`
- [MRI volume & metadata](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/file_handling/mri_volume.py): imaging model · `file_handling/mri_volume.py`
- [Shared imaging context](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/load_MRI_file.py): MRI workspace · Owns loaded layers, cursor interaction, and measurements. · `core/load_MRI_file.py`
- [Image layers](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/image_layer.py): layer state · `core/image_layer.py`
- [Painting & segmentation](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/paintbrush.py): annotation tools · `core/paintbrush.py`
- **Session images & masks**: local artifacts · NIfTI-derived images, masks, and session outputs.

### Atlas & registration

- [Atlas cache](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/mrid_utils/atlas_fetch.py): atlas acquisition · Downloads once or reads configured local atlas storage. · `mrid_utils/atlas_fetch.py`
- [MRI-to-atlas registration](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/registration.py): registration pipeline · Registers subject MRI to Waxholm Space. · `core/registration.py`
- [SAMRI preprocessing](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/samri/samri/pipelines/preprocess.py): vendored pipeline · `samri/samri/pipelines/preprocess.py`
- **ANTs binaries**: external executable

### Clinical workflows

- [Trajectory planning](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/trajectory_planning/trajectory_planning.py): planning workflow · Positions shanks and targets in multi-planar and 3D views. · `trajectory_planning/trajectory_planning.py`
- **Plan & surgery report**: planning artifact · Shank data and PDF contract for surgery and localisation.
- [Intraoperative correction](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/during_surgery/surgery_controller.py): surgery workflow · Reprojects planned targets using measured bregma and lambda. · `during_surgery/surgery_controller.py`
- [Electrode localisation](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/core/electrode_localization.py): post-implant pipeline · Semi-supervised MRID tag localisation after post-implant registration. · `core/electrode_localization.py`
- [Channel-to-atlas mapping](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/mrid_utils/channel_mapper.py): region assignment · Produces channel coordinates and atlas-region labels. · `mrid_utils/channel_mapper.py`

### Ephys viewing

- [Ephys recording loader](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/ephys/ephysrecording.py): recording model · `ephys/ephysrecording.py`
- [Anatomy-linked ephys viewer](https://github.com/neurotechnology-at-eth-zurich/implant/blob/main/ephys/visualisationEphys.py): trace visualization · Displays curated region-colored traces with electrode context. · `ephys/visualisationEphys.py`

## Connections

- Main window → Qt UI definitions: renders
- Main window → Shared imaging context: loads MRI into
- Main window → Trajectory planning: launches
- Main window → Intraoperative correction: launches
- Main window → Electrode localisation: launches
- Main window → Anatomy-linked ephys viewer: launches
- MRI ingestion → MRI volume & metadata: creates
- MRI volume & metadata → Shared imaging context: supplies volumes
- Shared imaging context → Image layers: manages
- Shared imaging context → Painting & segmentation: provides editing context
- Painting & segmentation → Session images & masks: writes masks
- Shared imaging context → MRI-to-atlas registration: supplies subject MRI
- Atlas cache → MRI-to-atlas registration: supplies atlas
- MRI-to-atlas registration → SAMRI preprocessing: uses
- SAMRI preprocessing → ANTs binaries: executes
- MRI-to-atlas registration → Trajectory planning: provides atlas transform
- Trajectory planning → Plan & surgery report: writes
- Plan & surgery report → Intraoperative correction: loads prior plan
- Plan & surgery report → Electrode localisation: supplies implanted shanks
- Session images & masks → Electrode localisation: supplies traces and anatomy
- MRI-to-atlas registration → Electrode localisation: supplies transforms
- Electrode localisation → Channel-to-atlas mapping: localises tags
- Channel-to-atlas mapping → Session images & masks: writes channel metadata
- Ephys recording loader → Anatomy-linked ephys viewer: supplies recordings
- Channel-to-atlas mapping → Anatomy-linked ephys viewer: supplies region labels
