lua script for automatic changing PatientID and PatientName

210 views
Skip to first unread message

Jaap Groen

unread,
Dec 3, 2019, 5:35:13 AM12/3/19
to Orthanc Users
Based on the documentation and different examples i wrote a lua script to change to PatientID and PatientName of studies (instances) to the stationname. Despite different attempts I could not get the script to work and keep getting a "bad parameters to RestApiPost()-error".
Any suggestions where I'm going wrong here?

Script:

function OnStoredInstance(instanceId, tags, metadata, origin)
   if origin['RequestOrigin'] ~= 'Lua' then
      local name = tags["StationName"]
 
      local replace = {}
      replace['PatientID'] = name
      replace['PatientName'] = name
 
      local request = {}
      request['Replace'] = replace
      request['Force'] = true
 
      local modified = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(request, true))
     RestApiPost('/instances/' .. modified)
      RestApiDelete('/instances/' .. instanceId)
   end
end

Alain Mazy

unread,
Dec 3, 2019, 6:19:41 AM12/3/19
to Jaap Groen, Orthanc Users
Hi Jaap,

Instead of 
RestApiPost('/instances/' .. modified)
, you should write
RestApiPost('/instances/', modified)

you were actually concatenating the content of the file into the url itself.

Best,

Alain

--
You received this message because you are subscribed to the Google Groups "Orthanc Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orthanc-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orthanc-users/01e5e21f-3bb3-4b9c-a1f4-315811434d73%40googlegroups.com.


--

Alain Mazy / Software Developer
a...@osimis.io / +32 494 31 67 27

Osimis

OSIMIS S.A. 
Quai Banning 6BE-4000 Liège 
www.osimis.io

Twitter LinkedIn


Jaap Groen

unread,
Dec 3, 2019, 6:57:31 AM12/3/19
to Orthanc Users
Nicely spotted, thanks!
Message has been deleted

Harkant Singh

unread,
Mar 15, 2020, 2:23:05 PM3/15/20
to Orthanc Users
Hi,

I wish to modify the 'StationName' with the username with which Orthanc server has been logged into. I modified the script to

function OnStoredInstance(instanceId, tags, metadata, origin)

 if origin['RequestOrigin'] ~= 'Lua' then
      local name = username
 
      local replace = {}
      replace['StationName'] = name

 
      local request = {}
      request['Replace'] = replace
      request['Force'] = true
 
      local modified = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(request, true))
      RestApiPost('/instances/', modified)

      RestApiDelete('/instances/' .. instanceId)
   end
end

it gives me "bad parameters to RestApiPost()" error
Any suggestions!!

Sébastien Jodogne

unread,
Mar 15, 2020, 4:52:12 PM3/15/20
to Orthanc Users
Hello,

Your script will obviously fail, as the variable "username" is not defined.

The "username" information is only available if the file was received through the REST API, and is available inside the "origin" argument:

Here is a working version of the script:

function OnStoredInstance(instanceId, tags, metadata, origin)
   if origin['RequestOrigin'] == 'RestApi' then
      local name = origin['Username']
      
      local replace = {}
      replace['StationName'] = name
      
      local request = {}
      request['Replace'] = replace

      local modified = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(request, true))
      RestApiPost('/instances/', modified)
      RestApiDelete('/instances/' .. instanceId)
   end
end

Harkant Singh

unread,
Mar 15, 2020, 9:47:18 PM3/15/20
to Orthanc Users
Hello,

Thanks.

Now there is one issue. As seen in the attached file - in the ‘Series’ display box the StationName is still the original one ("HOST-336121” in this case) and in the details of a particular ‘Instance’ it has changed to the username (“harry” in this particular instance).

Regards
Screenshot 2020-03-16 at 7.09.48 AM.png

Sébastien Jodogne

unread,
Mar 17, 2020, 3:49:15 AM3/17/20
to Orthanc Users
Hello,

The "StationName" is a series-level tag.

Your modification breaks the DICOM model by having 2 different values for the same series-level tag in the same series. In such a context, Orthanc keeps the first value it encountered (even after the old DICOM file is deleted).

You'll have to make a POST call to "/instances/.../reconstruct" to force Orthanc recreate its database of tags. This would give something like the following script (modifications in bold):

function OnStoredInstance(instanceId, tags, metadata, origin)
   if origin['RequestOrigin'] == 'RestApi' then
      local name = origin['Username']
      
      local replace = {}
      replace['StationName'] = name
      
      local request = {}
      request['Replace'] = replace

      local modified = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(request, true))
      local a = ParseJson(RestApiPost('/instances/', modified))
      RestApiPost('/instances/' .. a['ID'] .. '/reconstruct', '')
      RestApiDelete('/instances/' .. instanceId)
   end
end

HTH,
Sébastien-

Harkant Singh

unread,
Mar 17, 2020, 5:46:14 AM3/17/20
to Orthanc Users
Thanks. It works flawlessly now.
Reply all
Reply to author
Forward
0 new messages