Metadata-Version: 2.5
Name: neuroviewer
Version: 0.2.0
Summary: Freeview-like neuroimaging viewer in pure Python (PySide6 + vedo/VTK + nibabel)
Project-URL: Homepage, https://github.com/rdneuro/neuroviewer
Project-URL: Repository, https://github.com/rdneuro/neuroviewer
Project-URL: Issues, https://github.com/rdneuro/neuroviewer/issues
Project-URL: Changelog, https://github.com/rdneuro/neuroviewer/blob/main/CHANGELOG.md
Author-email: Rodrigo Debona <r.debona@ufrj.br>
License-Expression: MIT
License-File: LICENSE
Keywords: brain,freesurfer,medical-imaging,mri,neuroimaging,nifti,tractography,vedo,viewer,vtk
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: X11 Applications :: Qt
Classifier: Intended Audience :: Healthcare Industry
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Topic :: Scientific/Engineering :: Visualization
Requires-Python: >=3.9
Requires-Dist: matplotlib>=3.7
Requires-Dist: nibabel>=5.0
Requires-Dist: numpy>=1.24
Requires-Dist: pyside6-essentials>=6.5
Requires-Dist: vedo>=2024.5
Requires-Dist: vtk>=9.2
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Requires-Dist: twine>=5; extra == 'dev'
Provides-Extra: open3d
Requires-Dist: open3d>=0.17; extra == 'open3d'
Provides-Extra: test
Requires-Dist: pytest>=7; extra == 'test'
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/rdneuro/neuroviewer/main/src/neuroviewer/resources/neuroviewer.png" width="128" alt="Neuroviewer icon">
</p>

<h1 align="center">Neuroviewer</h1>

<p align="center">
A freeview-like neuroimaging viewer written in pure Python.<br>
PySide6 interface, vedo/VTK rendering, nibabel reading. Native desktop app, no browser, no server.
</p>

<p align="center">
  <a href="https://pypi.org/project/neuroviewer/"><img src="https://img.shields.io/pypi/v/neuroviewer.svg" alt="PyPI"></a>
  <a href="https://pypi.org/project/neuroviewer/"><img src="https://img.shields.io/pypi/pyversions/neuroviewer.svg" alt="Python versions"></a>
  <a href="https://github.com/rdneuro/neuroviewer/actions/workflows/tests.yml"><img src="https://github.com/rdneuro/neuroviewer/actions/workflows/tests.yml/badge.svg" alt="Tests"></a>
  <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT license"></a>
</p>

<p align="center"><img src="docs/screenshot.png" width="900" alt="Neuroviewer"></p>
-->

## Install

```bash
pip install neuroviewer              # core viewer
pip install "neuroviewer[open3d]"    # + Open3D point-cloud tools
```

Then:

```bash
neuroviewer                          # empty window
neuroviewer T1.mgz aseg.mgz lh.pial  # open files at start
python -m neuroviewer                # same thing
neuroviewer --install-desktop        # Linux: add a menu entry with icon
```

Requirements: Python 3.9+, an OpenGL-capable display. On Linux, Qt 6.5+
needs `libxcb-cursor0` (`sudo apt install libxcb-cursor0`). A self-contained
Linux AppImage is attached to each GitHub release.

## What it does

- **Files** (left): folder tree filtered to neuroimaging files, plus a
  recursive search under the root folder (name filter such as `aseg`,
  `*T1*.nii.gz`; blank = everything). Double click opens a file.
- **Views** (centre): one main view and three side views (layout `1+3`,
  `2x2` or `1x1`). Click a panel title, a toolbar button or `Ctrl+1..4` to put
  3D / sagittal / coronal / axial in the main position.
- **Layers** (right): one entry per volume, top of the list drawn on top,
  with two tabs:
  - **Display**: colormap, LUT (label maps), window min/max, opacity,
    "transparent below min", "outline only" (label borders), frame (4D);
  - **ROIs**: every label present in the volume with its LUT colour, name and
    volume (see below).
- **3D scene** (right, second tab): volume rendering of the active layer and
  the list of 3D objects (surfaces, streamlines, ROI surfaces, isosurfaces,
  point clouds) with visibility, colour and opacity.
- **Time course** (bottom): signal of the active 4D layer at the cursor; the
  panel opens by itself when a 4D layer is selected.
- **Status bar**: cursor in RAS mm, voxel index and value of every visible
  layer (with the LUT name for label maps).

### Menus

| menu | contents |
|---|---|
| File | Open, load surfaces / tracts, **Load LUT**, save screenshot, quit |
| View | layout, main view (`Ctrl+1..4`), reset views, go to RAS, **Clean 3D view** (`Ctrl+K`: objects only, no slices / box / cursor), panels |
| Preferences | **Appearance** (dark / light), **Viewer options** (2D crosshair, orientation letters, slice number; 3D slices, bounding box, cursor), **Slice interpolation** (nearest = voxels, linear / cubic = de-voxelised), **3D surfaces** (smoothing / inflate), reset |
| Tools | isosurface, label surfaces as separate objects, point clouds, Open3D tools |
| Help | mouse and keys, about |

Preferences are remembered per user (QSettings) and survive upgrades;
`config.py` only provides the first-run defaults.

### ROIs: choose which labels are shown

The **ROIs** tab lists the labels present in the active label map. Whatever
is unchecked disappears from the 2D views, the 3D slices, the volume
rendering and the ROI surfaces.

- Click the check boxes, or select rows (drag over them, Shift/Ctrl click)
  and press **Space** (or *Show selected* / *Hide selected*).
- The filter box narrows the list by name or ID ("hippo", "17"); **All**,
  **None** and **Invert** act on the rows currently listed. A typical
  sequence: *None*, then type `hippocampus` in the filter and press *All*.
- **By ID**: `17 53` or ranges such as `1000-1035`, then *Show only*, *Add*
  or *Hide*. *Copy set* / *Paste set* carry the shown IDs to another volume
  (e.g. the same hippocampal labels in several `hippoAmygLabels` files); the
  set also goes to the clipboard as text.
- Double click a ROI to move the cursor to its centre.
- **Render shown ROIs as 3D surfaces** builds organic surfaces of the shown
  labels in the 3D view. Combine with *Clean 3D view* to see only them.

### Organic surfaces (de-voxelise and inflate)

Label and threshold surfaces are extracted from the voxels and then
smoothed with a windowed-sinc filter, which removes the voxel staircase
while keeping volume and position (*Smoothing*: 0 = raw voxel faces,
1 = very smooth). *Inflate* rounds folds and corners with Laplacian
iterations and rescales about the centroid, so the shape balloons instead of
shrinking. Both live in Preferences > 3D surfaces and apply to the ROI
surfaces and to the surface tools. In 2D, Preferences > Slice interpolation
> Linear / Cubic gives the de-voxelised look.

### LUTs

Label maps start with `FreeSurferColorLUT.txt` (from `$FREESURFER_HOME` or
`FREESURFER_LUT_PATH`). Any number of other tables can be loaded with
File > Load LUT (or *Load...* next to the LUT box) and chosen per layer;
loaded LUTs are reloaded at the next start. Supported formats:

- FreeSurfer / 3D Slicer colour tables (`idx name R G B A`);
- ITK-SNAP label descriptions (`idx R G B A VIS MSH "name"`);
- CSV / TSV with a header (BIDS `*_dseg.tsv`, nilearn/Schaefer style):
  index, name and either `r g b` columns or a hex `color` column.

A LUT next to a volume with the same stem is used automatically
(`atlas.nii.gz` + `atlas.txt`, `sub-01_dseg.nii.gz` + `sub-01_dseg.tsv`).

### Mouse and keys

| where | action |
|---|---|
| 2D | left click / drag: move cursor; shift+left or middle drag: pan; right drag or wheel: zoom |
| 2D | Up / PageUp, Down / PageDown: next / previous slice; `r`: fit slice |
| 3D | left drag: rotate; middle: pan; right / wheel: zoom; **double click** on a slice or surface: move cursor; `r`: reset camera |

### Formats

- Volumes: `.nii`, `.nii.gz`, `.mgz`, `.mgh`, Analyze `.img/.hdr`, MINC `.mnc`, PAR/REC.
- Surfaces: FreeSurfer `lh.pial`, `rh.white`, ... (shifted to scanner RAS with
  `c_ras`) and GIfTI `.surf.gii`.
- Streamlines: `.trk`, `.tck` (direction-coloured, subsampled to
  `TRACT_MAX_STREAMLINES`).

### Coordinates

Everything is kept in world coordinates (scanner RAS, mm). Volumes are
reoriented to RAS+ voxel order on load (FreeSurfer conformed `.mgz` is LIA on
disk) and keep their full affine. The first loaded volume defines the display
grid; every other layer is sampled onto it through its own affine, so layers
with different resolutions or fields of view overlay correctly. Oblique
volumes are resampled for display.

Label maps are detected by file name (`aseg`, `aparc`, `ribbon`, ...) or by
a sidecar LUT, plus integer values. Any layer can be shown as labels by
setting its colormap to `lut`.

## Configuration

The packaged defaults live in `neuroviewer/config.py`. Do not edit that copy;
create your own once and edit it:

```bash
neuroviewer --write-config   # writes ~/.config/neuroviewer/config.py (Linux/macOS)
neuroviewer --config-path    # shows where it is (%APPDATA%\neuroviewer on Windows)
```

It is plain Python with the same names (radiological convention, colours,
layout, label hints, LUT sidecar suffixes, surface smoothing, point-cloud
size, Open3D parameters, screenshot scale, ...). Choices made in the
Preferences menu override these defaults. Keep only the lines you change. Set `NEUROVIEWER_CONFIG` to use another
file. An existing file is never overwritten unless you pass `--force`.

## Tools (extension point)

`neuroviewer/tools.py` holds a registry. A tool is one function that receives
a context (layers, active layer, cursor, grid, dialogs) and returns 3D objects:

```python
from neuroviewer.tools import register_tool

@register_tool("My tool", "What it does.")
def my_tool(ctx):
    layer = ctx.require_active()
    arr, origin, spacing = ctx.aligned(layer)
    ...
    return [("result name", vedo_object, "mesh")]
```

Built-in tools: isosurface, label surfaces as separate objects (e.g. `17 53`), cortex point cloud
from FreeSurfer labels, threshold point cloud, point cloud from the last
surface, Open3D voxel downsampling and Open3D Poisson reconstruction. Open3D
is optional; it is used as a compute engine and the result is shown in vedo.

## Troubleshooting

- **`Could not find the Qt platform plugin "xcb"`**: another Qt install is
  exporting its own plugin path (Spyder/PyQt5, opencv-python, conda qt).
  Neuroviewer resets it to the PySide6 plugins and prints the folder it used
  (`[qt] ...`). Start it from a terminal, not from the Spyder/IPython console.
  If it still fails, run `QT_DEBUG_PLUGINS=1 neuroviewer`.
- **Black views on Wayland**: `QT_QPA_PLATFORM=xcb neuroviewer`.
- **Missing `libxcb-cursor0`** (Qt 6.5+ on Linux): install it with your
  package manager.

## Development

```bash
git clone https://github.com/rdneuro/neuroviewer.git
cd neuroviewer
pip install -e ".[dev]"
pytest
```

See [RELEASING.md](RELEASING.md) for the PyPI and AppImage release steps.

## License

MIT. See [LICENSE](LICENSE). Neuroviewer depends on PySide6 (LGPLv3), VTK
(BSD), vedo (MIT), nibabel (MIT) and matplotlib (PSF-based); the AppImage
redistributes them under their own licenses.
