Package: slicerio
slicerioPython utilities for3D Slicerinteroperability.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.InstallationUsingpip:pip install slicerioExamplesRead segmentation and show some information about segmentsimportslicerioimportjsonsegmentation_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 valuesimportslicerioimportnrrdinput_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 SlicerTheservermodule 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 using3D 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.importosimportslicerio.server# Load from remote URLslicerio.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 theSegmentationFilefile type:slicerio.server.file_load("path/to/Segmentation.seg.nrrd","SegmentationFile")Supported file typesimage files (nrrd, nii.gz, ...):VolumeFilesegmentation file (.seg.nrrd, nrrd, nii.gz, ...):SegmentationFilemodel file (.stl, .ply, .vtk, .vtp, .vtu, ...):ModelFilemarkup file (.mrj.json):MarkupsFiletransform file (.tfm, .h5, .txt):TransformFilespreadsheet file (.csv, .tsv):TableFiletext file (.txt, .json, ...):TextFilesequence file (.mrb, .seq.nrrd):SequenceFileSlicer scene file (.mrml, .mrb):SceneFileInspect data in 3D SlicerMetadata of data sets loaded into the server can be obtained usingnode_propertiesfunction: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"]forsegmentIdinsegments:print(f"Segment name:{segments[segmentId]['Name']}- color:{segments[segmentId]['Color']}")List of available nodes can be retrieved usingnode_namesandnode_idsfunctions:# Retreve node names of all imagesslicerio.server.node_names(class_name="vtkMRMLVolumeNode")# Retrieve all node IDsslicerio.server.node_ids(class_name="vtkMRMLVolumeNode")Nodes can be removed from the workspace:# Remove node by nameslicerio.server.node_remove(name="MRHead")# Clear the whole sceneslicerio.server.node_remove()Export files from 3D SlicerData 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})