# Issue using the dicomPatcher through code

**URL:** <https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687>\
**Category:** Development\
**Created:** [April 25, 2018, 4:30pm UTC](https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687 "2018-04-25T16:30:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricWilson](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ericwilson/32/1513_2.png) [@EricWilson](https://discourse.slicer.org/u/EricWilson)\
**Post date:** [April 25, 2018, 4:30pm UTC](https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687/1 "2018-04-25T16:30:15Z")

</div>

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:

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 25, 2018, 8:46pm UTC](https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687/2 "2018-04-25T20:46:30Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_can_I_use_a_visual_debugger_for_step-by-step_debugging) and inspect content of `ds`, the name and content of the file where the problem occurred, etc.

---

<div class="post-metadata">

**Author:** ![EricWilson](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ericwilson/32/1513_2.png) [@EricWilson](https://discourse.slicer.org/u/EricWilson)\
**Post date:** [April 25, 2018, 9:02pm UTC](https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687/3 "2018-04-25T21:02:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 26, 2018, 1:28pm UTC](https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687/4 "2018-04-26T13:28:06Z")

</div>

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](https://github.com/Slicer/Slicer/commit/937f5803431695549242eff624b23424c618a696)

---

<div class="post-metadata">

**Author:** ![EricWilson](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ericwilson/32/1513_2.png) [@EricWilson](https://discourse.slicer.org/u/EricWilson)\
**Post date:** [April 26, 2018, 2:15pm UTC](https://discourse.slicer.org/t/issue-using-the-dicompatcher-through-code/2687/5 "2018-04-26T14:15:45Z")

</div>

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