Skip to main content

Monocle - a Python interface to the Monocle viewer

monocle is a Python interface to Monocle, a browser-based radiology viewer for interacting with 3D medical data. It targets clinical, multimodal imaging as well as research-related viewing requirements. Load a study from Python (or the command line), and monocle emits a self-contained HTML page and opens it in the browser.

pip install monocle-viewer

Command line

Pip installs a monocle console command that opens the viewer on files from the shell:

monocle t1.nii.gz t2.nii.gz          # positional image arguments
monocle t1.nii.gz -s tumor.nii.gz    # image + segmentation
monocle t1.nii.gz -m pial.surf       # image + mesh
monocle -i t1.nii.gz -i t2.nii.gz    # -i is the explicit image flag
monocle t1.nii.gz --html scan.html   # write a shareable file (no browser)

Positionals are images; -i/--image adds more; -s/--seg adds segmentation label masks; -m/--mesh adds triangular meshes. Without --html it opens the browser via the one-shot local server.

The Monocle python builder

For per-volume display options and session-wide viewing config within python, use the builder class. Monocle(...) sets session config, each .image(...) adds a scan:

import monocle

m = monocle.Monocle(title='Patient 123')
t1 = m.image(t1, name='T1')
m.image(flair, name='FLAIR')
m.segmentation(label, name='Tumor', apply=t1)
m.mesh(surface, name='Cortex', apply=t1)

m.show()                # open in browser (no file, see below)
m.write('scan.html')    # write a persistent file
html = m.html()         # or get the HTML string

.image(...) returns the added image, and .segmentation(apply=...) takes one (or a list) to open with the mask already overlaid on those images' panels — or apply=True to overlay every populated panel.

The return values are session references rather than replacements for the objects supplied by nibabel, voxel, or surfa:

image: monocle.ImageHandle = m.image(t1)
annotation: monocle.AnnotationHandle = m.segmentation(mask, apply=image)

.image(...) and .segmentation(...) accept a range of sources and handle the conversion:

  • voxel volumes
  • nibabel images
  • torch tensors
  • numpy arrays
  • File paths

Segmentation label metadata is passed through to the viewer automatically when an in-memory volumes have a populated .labels lookup. You can override it, or provide labels for an array or file source, with labels=:

m.segmentation(
    mask,
    labels={
        0: 'background',              # omitted from the viewer label table
        1: 'tumor',
        2: ('edema', (255, 128, 0)),  # RGB can be 0..255
        3: ('necrosis', (0.8, 0.1, 0.2)),  # or 0..1
    },
)

.mesh(...) accepts voxel/trimesh objects, (vertices, faces) pairs, OBJ/STL/PLY paths, and FreeSurfer meshes. Mesh vertices must use world / scanner-RAS coordinates.

Convenience methods

monocle.show(sources, ...) is a one-liner for a quick look. It builds a Monocle instance, adds each source (with optional names= and affine=), and serves the page in the browser. Session config kwargs (title=, mode=, …) pass straight through.

monocle.show(t1, flair, names=['T1', 'FLAIR'], title='Patient 123')

Payload interoperability

The payload adapters are available when a backend needs Monocle's normalized transfer representation directly:

payload = monocle.as_voxel_payload(nib_image)
mesh_payload = monocle.as_mesh_payload((vertices, faces))

monocle.decode_voxel_payload(entry, data) performs the inverse operation for a voxel manifest entry and its raw byte block. Payloads contain geometry and binary data only; display settings and annotation relationships live on the session handles.

If you work with voxel Volumes, they carry a built-in volume.show() that visualizes the scan through monocle directly — monocle ships with voxel by default, so there's nothing extra to install.

The embedded viewer bundle

Offline mode (the default, inline=True) pastes the viewer's UMD bundle into the HTML so the file works from file:// with no network. That bundle ships as package data at monocle/static/monocle.umd.js.

License

Monocle is free for noncommercial use and distribution, and for internal use by commercial organizations. Commercial distribution or use in a customer-facing product or service requires a separate commercial license. See LICENSE for the complete terms.

Metadata

Release files for monocle-viewer 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monocle-viewer 0.2.0
File Size Uploaded
monocle_viewer-0.2.0.tar.gz 621.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for monocle-viewer 0.2.0
File Interpreter ABI Platform
monocle_viewer-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.2 MB

Release files / monocle_viewer-0.2.0.tar.gz

Download URL monocle_viewer-0.2.0.tar.gz
Size 621.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a253379bbe83b5dd7bd3f24addfba9180dc6a07638501e570dd5acda65cec9d0
BLAKE2b-256 checksum
How to use checksums
fbc911f8c8dfb03ba2130d1c3f41645f4d740ae5ae5796462d66d5895938d9ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / monocle_viewer-0.2.0-py3-none-any.whl

Download URL monocle_viewer-0.2.0-py3-none-any.whl
Size 622.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9447baf5818d4491af3b62f1a47d39a443c711580b4374fc576d0a412e7f2bb6
BLAKE2b-256 checksum
How to use checksums
85ea796c336916c006b9e3f52ba806c6217b488b06a1eb3c1d2b8895b1a71852
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page