Skip to main content

Utilities for 3D Slicer

Project description

Python package Upload Python Package PyPI version

slicerio

Python utilities for 3D Slicer interoperability.

The package contains utility functions for reading and writing segmentation files and convenience functions for using 3D Slicer via its web API. More functions will be added in the future.

Installation

Using pip:

pip install slicerio

Examples

Read segmentation and show some information about segments

import slicerio
import json

segmentation_info = slicerio.read_segmentation_info("Segmentation.seg.nrrd")

number_of_segments = len(segmentation_info["segments"])
print(f"Number of segments: {number_of_segments}")

segment_names = slicerio.segment_names(segmentation_info)
print(f"Segment names: {', '.join(segment_names)}")

segment0 = slicerio.segment_from_name(segmentation_info, segment_names[0])
print("First segment info:\n" + json.dumps(segment0, sort_keys=False, indent=4))

Extract selected segments with chosen label values

import slicerio
import nrrd

input_filename = "path/to/Segmentation.seg.nrrd"
output_filename = "path/to/SegmentationExtracted.seg.nrrd"
segment_names_to_labels = [("ribs", 10), ("right lung", 12), ("left lung", 6)]

voxels, header = nrrd.read(input_filename)
extracted_voxels, extracted_header = slicerio.extract_segments(voxels, header, segmentation_info, segment_names_to_labels)
nrrd.write(output_filename, extracted_voxels, extracted_header)

View files in 3D Slicer

The server module allows using Slicer as a data viewer in any Python environment. All files are loaded into a single Slicer instance, which eliminates the wait time for application startup and also allows analyzing, comparing multiple data sets in one workspace. The feature is implemented by using 3D Slicer's built-in Web Server module, which offers data access via a REST API.

For example, an image file can be loaded with the command below. The command starts a new Slicer application instance with the web API enabled.

import os
import slicerio.server

# Load from remote URL
slicerio.server.file_load("https://github.com/rbumm/SlicerLungCTAnalyzer/releases/download/SampleData/LungCTAnalyzerChestCT.nrrd")

# Load from local file
# A Slicer application instance (with Web Server enabled) is automatically started, if it is not running already.
slicerio.server.file_load("path/to/SomeImage.nrrd", slicer_executable=f"{os.environ["LOCALAPPDATA"]}/NA-MIC/Slicer 5.2.0/Slicer.exe")

A segmentation file can be loaded by specifying the SegmentationFile file type:

slicerio.server.file_load("path/to/Segmentation.seg.nrrd", "SegmentationFile")

Supported file types

  • image files (nrrd, nii.gz, ...): VolumeFile
  • segmentation file (.seg.nrrd, nrrd, nii.gz, ...): SegmentationFile
  • model file (.stl, .ply, .vtk, .vtp, .vtu, ...): ModelFile
  • markup file (.mrj.json): MarkupsFile
  • transform file (.tfm, .h5, .txt): TransformFile
  • spreadsheet file (.csv, .tsv): TableFile
  • text file (.txt, .json, ...): TextFile
  • sequence file (.mrb, .seq.nrrd): SequenceFile
  • Slicer scene file (.mrml, .mrb): SceneFile

Inspect data in 3D Slicer

Metadata of data sets loaded into the server can be obtained using node_properties function:

properties= slicerio.server.node_properties(name="MRHead")[0]
print(properties["ClassName"])
print(properties["ImageData"]["Extent"])

properties = slicerio.server.node_properties(id=segmentationId)[0]
segments = properties["Segmentation"]["Segments"]
for segmentId in segments:
   print(f"Segment name: {segments[segmentId]['Name']} - color: {segments[segmentId]['Color']}")

List of available nodes can be retrieved using node_names and node_idsfunctions:

# Retreve node names of all images
slicerio.server.node_names(class_name="vtkMRMLVolumeNode")

# Retrieve all node IDs
slicerio.server.node_ids(class_name="vtkMRMLVolumeNode")

Nodes can be removed from the workspace:

# Remove node by name
slicerio.server.node_remove(name="MRHead")

# Clear the whole scene
slicerio.server.node_remove()

Export files from 3D Slicer

Data sets created in Slicer (e.g., segmentations, landmark point sets), which can be retrieved by writing into file.

# Save the node identified by `MRHead` node name, uncompressed, into the specified file.
slicerio.server.file_save("c:/tmp/MRHeadSaved.nrrd", name="MRHead", properties={'useCompression': False})

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

slicerio-0.1.8.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

slicerio-0.1.8-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file slicerio-0.1.8.tar.gz.

File metadata

  • Download URL: slicerio-0.1.8.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for slicerio-0.1.8.tar.gz
Algorithm Hash digest
SHA256 51f1fc91a2be2a6bdd06685583be37cd49cb1f361b11ef1e450e4f7f64385217
MD5 6a617d822ee465b3c68f895e5b0de52c
BLAKE2b-256 1ffa24eb998d38d908cb884aabe9608fb632bfadf501dac2541061f83dba6134

See more details on using hashes here.

File details

Details for the file slicerio-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: slicerio-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for slicerio-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 da8cfe3e27317847f62696e778fd93774f16b98bad703c026450c0eeaf1d8f95
MD5 a327cd741aef09f3a335b3837ac168a9
BLAKE2b-256 1b59db8f1aaafd801950b0e6baea2a61a8b24e9e94971d3a3c2d99c9dce356c8

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