Skip to content

Instantly share code, notes, and snippets.

@mikebind
mikebind / Failed_C-ECHO.txt
Created March 27, 2024 00:05
Verbose log from pynetdicom trying to communicate with Slicer DICOM storage listener and aborting on C-ECHO request
I: Requesting Association
I: Association Accepted
Association successful
I: Sending Echo Request: MsgID 1
I: Releasing Association
I: Association Aborted
D: Request Parameters:
D: ======================= OUTGOING A-ASSOCIATE-RQ PDU ========================
D: Our Implementation Class UID: 1.2.826.0.1.3680043.9.3811.2.0.2
D: Our Implementation Version Name: PYNETDICOM_202
@mikebind
mikebind / Slicer_FSL-FLIRT_Transforms_Conversion.py
Created August 3, 2023 05:03
Contains python functions which should work in the 3D Slicer envrionment to convert between Slicer image registration transformation matrices and FSL/FLIRT image registration transform matrices
import numpy as np
import slicer, vtk
def getVolumeCoordinateTransformMatrixDict(volNode):
import numpy as np
def translationMatrix(point):
trMat = np.eye(4)
trMat[0:3, 3] = (*point,)
@mikebind
mikebind / exportRedSliceToDICOM.py
Last active April 8, 2022 22:24
Excerpt of 3D Slicer module logic code to essentially export the "Red" slice view image to a single slice DICOM file
class SomeModuleLogic(ScriptedLoadableModuleLogic):
def exportRedSliceAsDicom(self, patientNameSH, xRangeMm=150, yRangeMm=150, zRangeMm=0.5, outputFolderPath=None, dicomTagDict=None):
""" This function creates an ROI centered on the center of the Red slice view, and
with axes aligned with that slice view's slice, and with thickness specified by zRangeMm.
This ROI is then used to crop the red slice's current background image volume to slices
in this orientation (I choose the zRange such that this results in a single slice by
setting it to close to the minimum voxel side length), and export the result to DICOM.
In order to export to DICOM, a patient and study need to be created in the subject hierarchy,
these are done the subject name given in patientNameSH and with study name "SingleSliceExport".
@mikebind
mikebind / StitchVolumes.py
Created March 1, 2022 19:48
This is code for a simple 3D Slicer module which allows stitching together of multiple image volumes into one larger image volume. This is intended for cases where imaging was acquired at multiple stations along the same scanner axis.
import os
import unittest
import logging
import vtk, qt, ctk, slicer
import numpy as np
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin
#
# StitchVolumes
import logging
import os
import unittest
import vtk, qt, ctk, slicer
import SegmentStatistics
from slicer.ScriptedLoadableModule import *
from slicer.util import TESTING_DATA_URL
from slicer.util import VTKObservationMixin
@mikebind
mikebind / TestDebug1.py
Last active January 28, 2022 22:41
Mostly working 3D Slicer module which allows switching between volumes for volume rendering and allows interactive thresholding via slider.
import logging
import os
import unittest
import vtk, qt, ctk, slicer
import SegmentStatistics
from slicer.ScriptedLoadableModule import *
from slicer.util import TESTING_DATA_URL
from slicer.util import VTKObservationMixin
@mikebind
mikebind / shiftVolumeRenderingGist.py
Last active December 3, 2021 07:56
Function which allows shifting 3D Slicer volume rendering scalar opacity transfer function similar to "Shift" slider in Volume Rendering module
def shiftVolumeRendering(volumePropertyNode, xOffset=0):
""" Shift the scalar opacity control point values by an
offset value (similar to moving the "Shift" slider in the volume rendering module)
"""
volProp = volumePropertyNode.GetVolumeProperty()
scalarOpacity = volProp.GetScalarOpacity() # this is a vtkPiecewiseFunction
pointIdx = 0
opacityPointValues = [] # list to hold values
# Gather Existing Values
for pointIdx in range( scalarOpacity.GetSize() ):
@mikebind
mikebind / AutoCompleteCrash.py
Created May 26, 2020 15:45
Code which can be used to demonstrate Slicer crash on tab-autocomplete on my machine. All of the code is auto-generated by the extension wizard except for lines 82-86, which I added.
import os
import unittest
import logging
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin
#
# AutoCompleteCrash
#