Issue using the dicomPatcher through code

I am trying to patch some problematic dicom files to make sure the patcher will be able to handle any issues when loading dicom files in. running the process through the GUI has no issues, and the patch works as intended. running the code through python generates some issues though. an error is thrown when the patcher is attempting to create an output path for the patched file by using the patient name and patient id attributes from the dicom file. this dicom file has no patient name attribute, so an error is thrown and the process is stopped.

why is this different from the GUI, where no error is thrown for the same file, and how do i fix it?

i’m calling it with the code below. i would also like to know how the logic instance could be accessed without the widget representation, I have only found the abstract class for logic accessible outside of the widget:

logic = slicer.modules.dicompatcher.widgetRepresentation().self().logic
logic.clearRules()
logic.addRule('NormalizeFileNames')
logic.addRule('GenerateMissingIDs')
outputPath = path + '_DicomPatcherOutput'
logic.patchDicomDir(path, outputPath)

here is the stack trace:
logic.patchDicomDir(path, outputPath)
File "C:/Program Files/Slicer 4.9.0-2018-04-04/lib/Slicer-4.9/qt-scripted-modules/DICOMPatcher.py", line 571, in patchDicomDir
patchedFilePath = rule.generateOutputFilePath(patchedFilePath, ds)
File "C:/Program Files/Slicer 4.9.0-2018-04-04/lib/Slicer-4.9/qt-scripted-modules/DICOMPatcher.py", line 458, in generateOutputFilePath
patientNameID = ds.PatientName+"*"+ds.PatientID
AttributeError: 'str' object has no attribute 'PatientName'

Create logic without GUI:

import DICOMPatcher
logic = DICOMPatcher.DICOMPatcherLogic()

The error is that ds is a string instead of a DICOM dataset. The best would be to attach a Python debugger and inspect content of ds, the name and content of the file where the problem occurred, etc.

the problem is I know it is missing the content piece it is looking for, and that causes the error. if it were just that though, it wouldn’t work in the GUI at all, which it does. so my question is why is it different through code? I just want to be able to handle dicom files that might be missing pieces of information with the patcher.

I was able to reproduce the problem. You’ve discovered a very interesting bug. generateOutputFilePath method was called with arguments in incorrect order, which flipped the order each time a rule was called. In the GUI, rules were added in a different order, that’s why it worked.

The fix will be included in tomorrow’s nightly build, but you can apply it on your computer right now - you just need to modify a single line: https://github.com/Slicer/Slicer/commit/937f5803431695549242eff624b23424c618a696

1 Like

that’s interesting, I’m glad you were able to figure it out. thanks for the fix!