How to create a Python Module in slicer from a python script?

I have a python script that i paste in the Python Interactive console to achieve whatever task that i wish to. How do i convert this code into a simple module where the user just clicks on the module and the code runs?

Edit: I’m adding the script i wish to convert to a module below:

import SampleData
import numpy as np
import random

def segmentation():
	sampleDataLogic = SampleData.SampleDataLogic()
	masterVolumeNode = sampleDataLogic.downloadSample('MRHead')
	volumeNode = arrayFromVolume(getNode('MRHead'))
	p = np.shape(volumeNode)[0]
	q = np.shape(volumeNode)[1]
	r = np.shape(volumeNode)[2]
	volumeNode = getNode('MRHead')
	point_Ijk = [r,q,p]
	volumeIjkToRas = vtk.vtkMatrix4x4()
	volumeNode.GetIJKToRASMatrix(volumeIjkToRas)
	point_VolumeRas = [0, 0, 0, 1]
	volumeIjkToRas.MultiplyPoint(np.append(point_Ijk,1.0), point_VolumeRas)
	transformVolumeRasToRas = vtk.vtkGeneralTransform()
	slicer.vtkMRMLTransformNode.GetTransformBetweenNodes(volumeNode.GetParentTransformNode(), None, transformVolumeRasToRas)
	point_Ras = transformVolumeRasToRas.TransformPoint(point_VolumeRas[0:3])
	pqr_ras = [ ]
	for i in point_Ras:
		if i<0:
			pqr_ras.append(-1*i)
		else:
			pqr_ras.append(i)
	roiNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLAnnotationROINode())
	roiNode.SetRadiusXYZ(pqr_ras[0], pqr_ras[1], pqr_ras[2]/2)
	roiNode.SetXYZ(0,0,pqr_ras[2]/4)
	cropVolumeLogic = slicer.modules.cropvolume.logic()
	cropVolumeParameterNode = slicer.vtkMRMLCropVolumeParametersNode()
	cropVolumeParameterNode.SetROINodeID(roiNode.GetID())
	cropVolumeParameterNode.SetInputVolumeNodeID(volumeNode.GetID())
	cropVolumeParameterNode.SetVoxelBased(True)
	cropVolumeLogic.Apply(cropVolumeParameterNode)
	croppedVolume = slicer.mrmlScene.GetNodeByID(cropVolumeParameterNode.GetOutputVolumeNodeID())
	masterVolumeNode = getNode('MRHead cropped')
	segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
	segmentationNode.CreateDefaultDisplayNodes()
	segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
	segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
	segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
	segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
	segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
	segmentEditorWidget.setSegmentationNode(segmentationNode)
	segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
	segmentName = "Skull"
	thresholdMin = 20
	thresholdMax = 279
	addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(segmentName)
	segmentEditorNode.SetSelectedSegmentID(addedSegmentID)
	segmentEditorWidget.setActiveEffectByName("Threshold")
	effect = segmentEditorWidget.activeEffect()
	effect.setParameter("MinimumThreshold",str(thresholdMin))
	effect.setParameter("MaximumThreshold",str(thresholdMax))
	effect.self().onApply()
	segmentEditorWidget.setActiveEffectByName("Islands")
	effect = segmentEditorWidget.activeEffect()
	effect.setParameter("MinimumSize","1000")
	effect.setParameter("Operation","KEEP_LARGEST_ISLAND")
	segmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
	effect.self().onApply()
	segmentEditorWidget = None
	slicer.mrmlScene.RemoveNode(segmentEditorNode)
	getNode('Segmentation').CreateClosedSurfaceRepresentation()
	segmentationNode = getNode("Segmentation")
	labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
	slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, slicer.vtkSegmentation.EXTENT_REFERENCE_GEOMETRY)
	segmentationNode = getNode("Segmentation")
	# Export segments to models
	shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
	exportFolderItemId = shNode.CreateFolderItem(shNode.GetSceneItemID(), "Skull")
	slicer.modules.segmentations.logic().ExportAllSegmentsToModels(segmentationNode, exportFolderItemId)
	segmentModels = vtk.vtkCollection()
	shNode.GetDataNodesInBranch(exportFolderItemId, segmentModels)
	# Get exported model of first segment
	modelNode = segmentModels.GetItemAsObject(0)

segmentation()
1 Like

Using the Extension Wizard module will allow you to create a skeleton where you could integrate the logic you implemented.

Since your module is completely written in python, you can create the skeleton using the Slicer downloaded from https://download.slicer.org without having to “build” the program.

Looking at the following resources will also be helpful:

1 Like