Importing file names to Horos database

859 views
Skip to first unread message

Ben Liotta

unread,
Jan 26, 2021, 3:08:25 PM1/26/21
to Horos Project
I have a database of 1000 blinded images, so each filename is a long number. I need to measure each image, but when I import them into Horos each image comes up as "unnamed" and the database info on Horos doesn't include the file name-- no way to identify the files!

Does anyone know how to get the file name to show up in the Horos database view (ie in the study name, study description, or some other column) ? 

thanks!

Todd Jensen

unread,
Jan 27, 2021, 8:36:02 AM1/27/21
to Horos Project
I don't believe what you are asking for is possible with Horos as is.

You could write a script to update the DICOM files so that the patient name or other element is set to the filename before importing the files. I've done similar things using Python/pydicom. The following will change the series description for each file to the filename and save it as FIXED_<original_filename> (you'll have to update it to handle files not using .dcm suffix):

#!/usr/bin/env python3
#
# USAGE: <script_name> <directory_with_dcm_files>
#
import pydicom as dicom
import os
import sys

directory = os.fsencode(sys.argv[1])
for file in os.listdir(directory):
    filename = os.fsdecode(file)

    try:
        if filename.endswith(".dcm"):
            print("Reading file %s" % (filename))
            filepath = os.path.join(sys.argv[1], filename)
            ds = dicom.read_file(filepath)

            # Change value for element
            # ds[0x0010, 0x0040].value = 'F'

            # Delete element
            # del ds[0x0010, 0x0040]  # patient sex

            # New instance UID (uncomment if you want a new series and/or instance)
            #ds[0x0020, 0x000e].value = dicom.uid.generate_uid()
            #ds[0x0008, 0x0018].value = dicom.uid.generate_uid()
            new_series_desc = filename
            ds[0x0008, 0x103e].value = new_series_desc

            new_filename = "FIXED_" + filename
            print("Saving file %s" % (new_filename))
            ds.save_as(os.path.join(sys.argv[1], new_filename))

    except Exception as e:
        print("Exception caught %s" % (e))



Todd Jensen, PhD
Jensen Informatics LLC

Soren Christensen

unread,
Jan 27, 2021, 8:52:10 AM1/27/21
to Todd Jensen, Horos Project
The file names we never meant to carry any information - that is the core of the issue. A proper de-identification scheme writes new IDs to the files.
I guess one could import the files one by one and edit the meta header in Horos. Would probably take most of 1 day - or more that way.
In addition to Todds solution which opens doors to more flexibility you can also use the dcmodify command line utility (https://dicom.offis.de/dcmtk.php.en)
Say your files are in FOLDER/ and called A.dcm B.dcm C.dcm
Then you could do this in the terminal - make a backup of your data first!
for f in FOLDER/*dcm; do 
fname=$(basename $f)
dcmodify -nb -ma PatientID=${fname%.dcm} -ma PatientName=${fname%.dcm} $fname
done


 

--
You received this message because you are subscribed to the Google Groups "Horos Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to horos-projec...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/horos-project/327ab78e-87d6-4eae-83b0-837c1dbc1172n%40googlegroups.com.

Ben Liotta

unread,
Jan 27, 2021, 12:56:15 PM1/27/21
to Horos Project
Thanks Soren and Todd!
I'm a novice at mac scripts. 
For Todd's solution sounds like I download the Pydicom package then use the supplied script--- where in that script would I put in the folder name containing the files I want to edit? 

For Soren's solution, sounds like I download the DCMTK toolkit then just run the supplied script in the terminal which copies the file name into the Patient ID and Patient name fields. 

Let me know if that sounds right. thanks!

Soren Christensen

unread,
Jan 27, 2021, 1:14:05 PM1/27/21
to Ben Liotta, Horos Project
No worries!
Todds script will require you to have/install python (python3 is used in the script). You also need to install the pydicom python package. It will then be called as ./[script_name.py] foldername
For the dcmtk stuff I think you can use homebrew: https://formulae.brew.sh/formula-linux/dcmtk - or check the web page. 
Once installed and your path is set up correctly, you should be able to run dcmodify. At that point you can run the script. IF you have a different folder structure you would need to modify things accordingly of course.
Good luck!
Soren


Ben Liotta

unread,
Jan 29, 2021, 12:38:37 AM1/29/21
to Soren Christensen, Horos Project
Thanks Soren! Homebrew/DCMTK worked fine. 

Still getting an error though. I run the script: 
for f in /Users/***/***/***/*dcm; do

fname=$(basename $f) 
dcmodify -nb -ma PatientID=${fname%.dcm} -ma PatientName=${fname%.dcm} $fname 
done

But get the error: 
E: unable to load file image_111111111111.dcm: No such file or directory
The path is right and it gives the error for all the images I want to edit, maybe a problem with the file naming? 

thanks!
Ben

Soren Christensen

unread,
Jan 29, 2021, 1:03:05 AM1/29/21
to Ben Liotta, Horos Project
Hi Ben,
 My mistake. The last item in the line with dcmodify is currently $fname, it should be $f which is the full path. Make sure you work on a copy of the data because the files will be modified in-place.
Soren

Ben Liotta

unread,
Jan 29, 2021, 11:48:01 AM1/29/21
to Soren Christensen, Horos Project
Success! Thanks again Soren--- off to image measuring! 

Soren Christensen

unread,
Jan 29, 2021, 11:51:41 AM1/29/21
to Ben Liotta, Horos Project
Good to hear!
Reply all
Reply to author
Forward
0 new messages