Skip to main content

Interactive 3D volume slicer for Jupyter Notebooks

Project description

VolViz

License: MIT Python 3.7+ PyPI version

VolViz is a lightweight, interactive 3D volume slicer designed for Jupyter Notebooks and Google Colab. It allows researchers and developers to visualize 3D numpy arrays (such as MRI, CT scans, or scientific simulations) directly within their workflow without needing heavy external software. VolViz is designed to work inside Jupyter Notebook, JupyterLab, VS Code Notebooks and Google Colab.

Features

  • Orthogonal Slicing: View volumes in Sagittal (X), Coronal (Y), and Axial (Z) planes.
  • Anisotropic Spacing: Correctly renders volumes with non-cubic voxels (e.g., thick medical slices).
  • Interactive Contrast: Adjust Window/Level (brightness/contrast) in real-time.
  • Data Probe: Hover over images to see exact (x, y, z) coordinates and voxel intensity values.
  • Multi-Volume Support: Compare multiple volumes side-by-side (up to 3 per row).
  • Fluid Performance: Built on ipympl for smooth, GPU-accelerated 2D rendering.

Installation

pip install volviz

From Source (Development)

If you have cloned this repository, navigate to the root folder and run:

pip install .

Dependencies

VolViz requires the following packages (installed automatically):

  • numpy
  • matplotlib
  • ipywidgets
  • ipympl (Crucial for interactivity)

📓 Running on Jupyter Notebook

IMPORTANT: Use the %matplotlib widget magic command at the start of your notebook.

# 1. Enable interactive backend
%matplotlib widget

import numpy as np
from volviz import VolumeSlicer

# 2. Create some dummy 3D data (X, Y, Z)
# Let's create a 30x30x50 volume
vol = np.random.rand(30, 30, 50)

# 3. Visualize
# If your voxels are cubes (1mm x 1mm x 1mm), no extra config needed:
slicer = VolumeSlicer(vol)
slicer.show()

☁️ Running on Google Colab

Google Colab requires a specific setup to render interactive widgets correctly.

  1. Install the package:

    !pip install volviz
    
  2. Restart the Runtime: If you see a ValueError: Key backend: 'module://ipympl.backend_nbagg' is not a valid value..., go to Runtime > Restart Session.

  3. Enable Widgets & Run: You must enable the custom widget manager before importing the library:

    from google.colab import output
    output.enable_custom_widget_manager() # <--- REQUIRED for Colab
    
    %matplotlib widget
    import numpy as np
    from volviz import VolumeSlicer
    
    vol = np.random.rand(30, 30, 30)
    slicer = VolumeSlicer(vol)
    slicer.show()
    

Handling Medical Data with Anisotropy

If your data has non-cubic voxels (e.g., a CT scan with high resolution in X/Y but thick slices in Z), use the spacing parameter to ensure the aspect ratio is correct.

# Example: 1mm resolution in X/Y, but 3mm slice thickness in Z
slicer = VolumeSlicer(
    volumes=my_medical_scan, 
    spacing=(1.0, 1.0, 3.0)  # (x_mm, y_mm, z_mm)
)
slicer.show()

Controls

  • View Dropdown: Switch between Sagittal, Coronal, and Axial views.
  • Slice Slider: Navigate through the volume depth.
  • Contrast Slider: Drag the handles to change the black/white cut-off points (Window/Level).
  • Mouse Hover: Move your mouse over any image to see info at the bottom of the card.
  • Zoom/Pan: Use the toolbar buttons (left of the image) to zoom into specific regions.

Examples

Troubleshooting

The plot is blank or not interactive: Ensure you have ipympl installed and the magic command active:

  1. Run pip install ipympl
  2. Add %matplotlib widget as the first cell in your notebook.
  3. Restart your kernel and refresh the page.

I see "Error: Failed to display Jupyter Widget": If you are using JupyterLab, you may need to install nodejs or the widget extension:

jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib

(Note: In modern JupyterLab 3.0+, simply pip installing ipympl is usually enough).

License

Distributed under the MIT License. See LICENSE for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

volviz-0.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

volviz-0.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file volviz-0.1.1.tar.gz.

File metadata

  • Download URL: volviz-0.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for volviz-0.1.1.tar.gz
Algorithm Hash digest
SHA256 768f74269b00071eb6fdd884624f6d674ba7b0bcfcb8cf718efb1f4147cb59fb
MD5 bdd9113e91db5764ab39c588b5fd9bbb
BLAKE2b-256 49e1a15e939f42e3ccf8f2f27a07a3a4328bcfaf292883497efa60ecda8db6fc

See more details on using hashes here.

File details

Details for the file volviz-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: volviz-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for volviz-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b64bb6be22ae1ab3fe074aa07e4c06a2803247915dab2293a9dc0b8e22212e2
MD5 3354dcd5febc44ff421ba2e1da665f78
BLAKE2b-256 d27a8949cd20b54a3d6329e2a405e00e576ffffd68be8962ec603d5f27849418

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page