DICOM database initialization


I am starting Slicer in a Docker container. I need to initialize Slicer DICOM database prior to executing the module I am working on. How to initialize the db in a script rather than manually clicking “Create new database” in DICOM module GUI?

Thank you!

Hi @Nadya_Shusharina - See this thread with @Paula_Moreno for information about creating a settings file for server connectsions. Setting up the database should be similar. We also worked on this last project week and as I recall we ran into a language localization issue with the docker directory path, but otherwise I think it was working.

Thank you, Steve.

Looking at that thread, I made a workaround by manually initializing the database in the running container and added the empty ctkDICOM.sql and ctkDICOMTagCache.sql into the Dockerfile:

RUN mkdir -p /home/slicer/Documents/SlicerDICOMDatabase
COPY files/SlicerDICOMDatabase/ctkDICOM.sql /home/slicer/Documents/SlicerDICOMDatabase
COPY files/SlicerDICOMDatabase/ctkDICOMTagCache.sql /home/slicer/Documents/SlicerDICOMDatabase

It would be cleaner to have Slicer create the .sql files every time the container starts.

I haven’t looked at that initialization code in a while, but I thought it used to create a database when you initialized the browser, so it might be enough to run selectModule("DICOM") in python at startup. But you can probably also find a way to initialize a the database and set slicer.dicomDatabase on the fly.

In Slicer 5.0.2 without the database, running slicer.util.selectModule(‘DICOM’) in GUI python interactor presents two buttons, “Create new database” and “Select database folder”. The db is not created unless the button is pressed.

In command line,
Slicer --python-code "slicer.util.selectModule('DICOM');" --testing
prints a message but does not create the sql files:
Database folder does not contain ctkDICOM.sql file: /opt/slicer

The feature is implemented in CTK, so you should be able to call this method:

It “almost” worked: CTK initializes the database in $HOME/ctkDICOM-database while Slicer expects $HOME/Documents/SlicerDICOMDatabase. How to create it in the right place?

You can do something like this:

dicomBrowser = ctk.ctkDICOMBrowser()
dicomBrowser.databaseDirectory = "/tmp/x"

It works. The portable way to form the database directory is

import os
documentsLocation = qt.QStandardPaths.DocumentsLocation
documents = qt.QStandardPaths.writableLocation(documentsLocation)
databaseDirectory = os.path.join(documents, slicer.app.applicationName + "DICOMDatabase")
dicomBrowser = ctk.ctkDICOMBrowser()
dicomBrowser.databaseDirectory = databaseDirectory

Adapted from Slicer/DICOM.py at main · Slicer/Slicer · GitHub

Of note, this snippet does not work using Slicer --testing --python-script initDICOMdb.py because app.applicationName becomes Slicer-tmp rather than Slicer. It works wihtout --testing, and in .slicerrc.py