Items in Bold are the short version:I sort of abandoned using a Lua script with OnStoredInstance as I was looking into earlier as it does not seem as flexible as using a Python script, plus no feedback about that. The issue is that some dicom tags are not populated by the Modality through the MWL (e.g. StudyDescription, ContentCreatorName, among others), so I want to add / edit those tags either as the instances are sent to Orthanc (preferably before they are even stored), or after they are stored (less desired).
I don't see how to modify tags before an instance is committed to storage using a Lua script, which is why I'm exploring the following approach. Python script that is called when a study becomes Stable.
def OnChange(changeType, level, resourceId):
if changeType == orthanc.ChangeType.STABLE_STUDY:
print('Stable study: %s' % resourceId)
study = json.loads(orthanc.RestApiGet('/studies/' + resourceId))
print (study)
updatetags = dict();
updatetags['ContentCreatorName'] = ""
updatetags['StudyDescription'] = ""
updatetags['PatientAddress'] = ""
updatetags['RequestedProcedureDescription'] = ""
updatetags['StudyInstanceUID'] = "1.3.6.1.4.1.56016.1.1.1.103.1620244246" # study['MainDicomTags']['StudyInstanceUID']
worklistmatches = json.loads(orthanc.RestApiPost('/modalities/PACS1/find-worklist', json.dumps(updatetags)))
if len(worklistmatches) == 1:
print("1 Match in the MWL folder")
worklistmatches = worklistmatches[0]
print(worklistmatches)
print("Insert Call To Function to Modify Tags from MWL response, then delete MWL")
elif len(worklistmatches) == 0:
print("StudyInstanceUID not in MWL")
else:
pinrt("More than one match for StudyInstanceUID in MWL")
orthanc.RegisterOnChangeCallback(OnChange)
This does get data from a MWL file if there is one matching the StudyInstanceUID for the study, e.g.:
{
"PatientAddress": "PatientAddress",
"RequestedProcedureDescription": "MRI INTERNAL AUDITORY CANAL - NO CONTRAST",
"SpecificCharacterSet": "ISO_IR 192",
"StudyDescription": "MRI INTERNAL AUDITORY CANAL - NO CONTRAST",
"StudyInstanceUID": "1.3.6.1.4.1.56016.1.1.1.103.1620244246"
}
and then what I'd like to do is modify / update the study with those new tags values. I know that the 'modify' feature can do that, but it looks like you have to make a copy and then delete the original. I would prefer to just edit or add some tags, not the PatientID or StudyInstanceUID. I would prefer to keep the original StudyInstanceUID instead of Orthanc creating a new Study with a newly generated StudyInstanceUID.
The is script actually does / will do what I'm looking for though since I already have MWL's somewhat setup and working with the modality with a few issues related to the StudyDescription and other tags. Also quite easy here to delete an MWL since I have a script to do that already. Not that I have a modality to actually work with, starting to explore using MPPS, but this method is actually is somewhat modality independent.
# BEGINNING OF /mwl/file/delete
# curl -k -X POST -d '["AccessionNumber"]'
http://localhost:8042/mwl/file/deletedef DeleteMWLByAccession(output, uri, **request):
if request['method'] != 'POST':
output.SendMethodNotAllowed('POST')
else:
response = dict();
try:
data = json.loads(request['body'])
# the accession_number to delete, for the filename
accession = data[0]
pathtoworklist = json.loads(orthanc.GetConfiguration())['Worklists']['Database'] + '/'
filenametxt = pathtoworklist + accession + '.txt'
filenamewl = pathtoworklist + accession + '.wl'
if os.path.exists(filenametxt):
os.remove(filenametxt)
response['filenametxt'] = "true"
else:
response['filenametxt'] = "false"
if os.path.exists(filenamewl):
os.remove(filenamewl)
response['filenamewl'] = "true"
else:
response['filenamewl'] = "false"
output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')
except Exception as e:
response['error'] = str(e)
output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')
orthanc.RegisterRestCallback('/mwl/file/delete(.*)', DeleteMWLByAccession)