Save patients ID in a list from Database with Python

Hi everyone,
I’m quite a new entry in the Slicer world. I’m trying to use a python script for batch conversion of structures to .vtk but I face some problems. The first issue I would like to solve is this: I loaded two test patients files and it seems they are correcly imported in the Database, but then when I use slicer.dicomDatabase.patients() I get a list of integers instead of patients IDs and subsequently I get some errors, trying to call the patients in other parts of the code. Here is a simple example:

from DICOMLib import DICOMUtils

input_folder = '/Users/lisaalborghetti/Desktop/Patients/dicom'

DICOMUtils.openTemporaryDatabase()

DICOMUtils.importDicom(input_folder)

all_patients = slicer.dicomDatabase.patients()

print("Patient list: ", all_patients)

patient_names = {}

# Loop through the patient IDs and extract patient names

for patient_id in all_patients:
  patient_name = slicer.dicomDatabase.fileValue(patient_id, '0010,0010')
  patient_names[patient_id] = patient_name

for patient_id, patient_name in patient_names.items():
  print(f"Patient ID: {patient_id}, Patient Name: {patient_name}")

This is what I see in 3Dslicer:

And this the terminal output:
Patient list: (‘1’, ‘2’)
Could not load “1”
DCMTK says: No such file or directory
File 1 could not be initialized.
Could not load “2”
DCMTK says: No such file or directory
File 2 could not be initialized.
Patient ID: 1, Patient Name:
Patient ID: 2, Patient Name:

Am I doing something wrong?
Thank you!

Lisa

Hi -

Since PatientID is assigned by the institution and they are not globally unique, Slicer assigns these integer local ids that are unique within the database.

The slicer.dicomDatabase.fileValue takes a path to a dicom instance file as an argument, hence the error message and the result of None.

To get from a Patient, who may have multiple studies, each of which with multiple series, and each of those with multiple instances, you can drill down with logic like in this example:

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#iterate-through-dicom-series

1 Like

Thanks a lot Steve, that was helpful!