Slicer often crashes when running with python-script in loading

Operating system: windows
Slicer version: 5.0.2 or 5.1.0
Expected behavior: stable
Actual behavior: crashes almost always.

import os
import sys

from DICOMLib import DICOMUtils
import DICOMScalarVolumePlugin

def importDicomDir(dicomDataDir):	
	loadedNodeIDs = []
	patientUIDs = []
	with DICOMUtils.TemporaryDICOMDatabase() as db:
		DICOMUtils.importDicom(dicomDataDir, db)
		patientUIDs = db.patients()
		for patientUID in patientUIDs:
			loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))
	patientUID = patientUIDs[0]			
	return loadedNodeIDs,patientUID

dicomDataDir = 'D:\\data\\dicoms\\CT_data\\CHENG_XIAO_MEI_F 48y 腰椎\\ABDOMEN_0ABDOMENIV3PHASE_CUSTOMIZED_(ADUL_20150921_142302_995000\\ABDOMNC_1_5_B30F_0006'
loadedNodeIDs,patientUID = importDicomDir(dicomDataDir)

If I run it line by line interactively, it mostly works. However, if I run it using command line
“C:\Users\zgan\AppData\Local\NA-MIC\Slicer 5.1.0-2022-06-01\Slicer.exe” --python-script “c:/zjsproj/slicer/t1.py”
, it almost always crashes.

sometimes it works, most times it will say ‘no responding’ on the title bar and then crashes when loading files.
Is there a better and more stable routine to load the files? Thanks!

checking the error logs I see these

Input port 0 of algorithm vtkImageMapToWindowLevelColors(0000022062EEB510) has 0 connections but is not optional.


Input port 0 of algorithm vtkImageThreshold(0000022062EE8BA0) has 0 connections but is not optional.


Input port 0 of algorithm vtkImageMapToWindowLevelColors(0000022062EEB510) has 0 connections but is not optional.


Input port 0 of algorithm vtkImageMapToWindowLevelColors(0000022062EE9B90) has 0 connections but is not optional.


Input port 0 of algorithm vtkImageThreshold(0000022062EE9750) has 0 connections but is not optional.


Input port 0 of algorithm vtkImageMapToWindowLevelColors(0000022062EE9B90) has 0 connections but is not optional.

Is that the reason for crashing? how to fix it then while loading?

Could someone help to look at what is wrong with this script? How do you load a DICOM directory into Slicer with Python script? The script sometimes works but most times fails. Also, a successful load may also show the error message above, so I am not sure that was the cause of the failure.

If possible, could someone provide a working script that loads a DICOM directory into Slicer? Thanks!!!

I guess it’s a temporal logix errror. Just add the line
Slicer.appp.ProcessEvents()
A few times along your code
Or add timers that execute that funxtion you want like
Timer.aingleshot(callback)

I’m sending this from my device sorry if it is not clear

Thanks Mauro.

slicer.app.processEvents() indeed helped. It does not crash as often and if I add enough of this line I can run my script to completion.

However, I still think this is an error/bug on the Slicer’s part. As every python statement ( or function call ) should be synchronized already. there is no reason to add this synchronization calls all over the places.

Hope someone can fix it!

@gzt036 does the crashing depend on which data is loaded?

1 Like