Question about Lua Script to Edit / Add Tag OnStoredInstance, MWL Plug-in, misc.

857 views
Skip to first unread message

Stephen Douglas Scotti

unread,
May 6, 2021, 7:25:14 PM5/6/21
to Orthanc Users

Question and Comment:

1.  One of the acquisition devices we have does not read StudyDescription from the MWL and the operator normally has to enter that manually, although 'tags['RequestAttributesSequence'][1]['RequestedProcedureDescription']' is set via the MWL.  I wrote a first attempt at a Lua script (below) based on an example that seems to work, but there might be a better way and it needs some error checking etc.  Basically want to populate ['0008,1030'] with ['RequestAttributesSequence'][1]['RequestedProcedureDescription'] if it is present in an instance when it gets sent to a from a particular AET via DICOM.  Might want to add some other similar items down the road.  It seems to work, but I only want to do that if:

a.  ['0008,1030'] is not already set.
b.  ['RequestAttributesSequence'][1]['RequestedProcedureDescription'] is set.

Probably simple to check for those conditions, but I an a novice with Lua scripting, and not clear to me that this is the best way to do that.  Also, wondering about performance since it has to create a modified instance, store it, and then delete than old one, rather than just making the change before it is stored the first time.  Is it possible to just edit / add a tag before storing it the first time ?

2.  Also, discovered an MWL plug-in on Github after connecting with the author:

WorklistFilePurger

Not sure if that is indexed on the Orthanc site somewhere.  Since I am using Custom Docker images, I guess I'd have to compile that when I build the Docker Container, or rewrite it in Python with some modifiations, but could be useful because I'd like to implement MPPS also.  Might actually be easier to write a Python script that as suggested here:  https://groups.google.com/g/orthanc-users/c/Tec0xgD4s2c

Lua Script

function OnStoredInstance(instanceId, tags, metadata, origin)

  --  PrintRecursive(instanceId)
  --  PrintRecursive(tags)
  --  PrintRecursive(metadata)
  --  PrintRecursive(origin)
  -- Do not modify twice the same file
    if origin.RequestOrigin == 'DicomProtocol' and origin.RemoteAet == 'SOME_AET' then
    -- Need a check here for ['0008,1030'] not set and tags['RequestAttributesSequence'][1]['RequestedProcedureDescription'] is set.
      local replace = {}
      replace['0008,1030'] = tags['RequestAttributesSequence'][1]['RequestedProcedureDescription']
      local request = {}
      request['Replace'] = replace

      -- Create the modified instance
      local modified = RestApiPost('/instances/' .. instanceId .. '/modify',
      DumpJson(request, true))

      -- Upload the modified instance to the Orthanc store
      RestApiPost('/instances/', modified)

      -- Delete the original instance
      RestApiDelete('/instances/' .. instanceId)
    end    
  end
end 

Stephen Douglas Scotti

unread,
May 6, 2021, 10:35:03 PM5/6/21
to Orthanc Users

Did a little more testing.  This seems to work for the Instances, but not for the Study Level Study Description.  Must be that Orthanc creates a Study Objects upon receipt of a new StudyInstanceUID and populates the Study Level StudyDescription at that point, so it isn't setting that at the StudyLevel ?  This is the current version of the Lua Script.  Added a function  to check if a key in the table is set and not an empty string.  Not sure how to handle that ?  I basically want to add the StudyDescription to the Study, Series (if that applies) and to Instances.

Thanks.

function tableHasKey(table,key)
    return table[key] ~= nil and table[key] ~= ''
end



function OnStoredInstance(instanceId, tags, metadata, origin)

  -- PrintRecursive(instanceId)
  -- PrintRecursive(tags)
  -- PrintRecursive(metadata)
  -- PrintRecursive(origin)
  -- Do not modify twice the same file
  -- if origin.RequestOrigin == 'DicomProtocol' and origin.RemoteAet == 'IMACLATE2013' or NmrEsaote
  if origin['RequestOrigin'] ~= 'Lua' then
    if origin.RequestOrigin == 'DicomProtocol' and origin.RemoteAet == 'IMACLATE2013' and not tableHasKey(tags,'0008,1030') and tableHasKey(tags,'RequestAttributesSequence') then

Stephen Douglas Scotti

unread,
May 7, 2021, 10:58:55 PM5/7/21
to Orthanc Users
Seems to be somewhat related to this question:   Modification of DICOM tags without changing UUID, but not completely.

Stephen Douglas Scotti

unread,
May 12, 2021, 1:07:50 AM5/12/21
to Orthanc Users
Having a conversation with myself, but found what I was looking for, although now might modify that somewhat to integrate with RIS or MWL files so that tags not handled my the Modality can be updated / added by the RIS or python plug-in for Orthanc.

function OnStoredInstance(instanceId, tags, metadata)
   -- Ignore the instances that result from a modification to avoid
   -- infinite loops
   if (metadata['ModifiedFrom'] == nil and
       metadata['AnonymizedFrom'] == nil) then

     -- The tags to be replaced
      local replace = {}
      replace['0070,0084'] = 'Technologist/Operator'
      replace['StudyDescription'] = 'TEST STUDY'

     -- The tags to be removed
--      local remove = { 'MilitaryRank' }

     -- Modify the instance, send it, then delete the modified instance
      Delete(SendToModality(ModifyInstance(instanceId, replace, remove, true), 'sample'))


     -- Delete the original instance
      Delete(instanceId)
   end
end

Reply all
Reply to author
Forward
0 new messages