Lua script for automatic suffix on AccessionNumber

Hello All,

I want to make a Lua script that will automatically add a hardcoded prefix to the AccessionNumber of each incomming study.

As I hadn’t any expercience with Orthanc nor Lua before, I browsed this group to write my first script which is this one:

`
function OnStoredInstance(instanceId, tags, metadata, origin)

if origin[‘RequestOrigin’] ~= ‘Lua’ then

local replace = {}
replace[‘StudyInstanceUID’] = tags[‘StudyInstanceUID’]
replace[‘SeriesInstanceUID’] = tags[‘SeriesInstanceUID’]
replace[‘SOPInstanceUID’] = tags[‘SOPInstanceUID’]

replace[‘AccessionNumber’] = “HUB” … tags[‘AccessionNumber’]

local command = {}
command[‘Replace’] = replace
command[‘Force’] = true
local modifiedFile = RestApiPost(‘/instances/’ … instanceId … ‘/modify’, DumpJson(command, true))

end
end
`

At this point I do not have any error anymore in Orthanc logs (had problems on several lines before) but I have the impression this code doesn’t do anything as I do not see any modification in the AccessionNumbers of my stored examinations.

Can you help me figure what’s wrong?
Any help will be highly appreciated :wink:

Hello,

You can find Lua samples in the source distribution of Orthanc:
https://hg.orthanc-server.com/orthanc/file/Orthanc-1.7.2/OrthancServer/Resources/Samples/Lua

In your case, you can take the following sample as a starting point:
https://hg.orthanc-server.com/orthanc/file/Orthanc-1.7.2/OrthancServer/Resources/Samples/Lua/ModifyInstanceWithSequence.lua

Sébastien-

Bonjour Sébastien and thank you for your answer,

To be sure everything works before editing that script, I have created a new script, pasting in it the example you gave me and temporarly replaced mine by this one.

`
Saisissez le code ici.function OnStoredInstance(instanceId, tags, metadata, origin)

– Do not modify twice the same file

if origin[‘RequestOrigin’] ~= ‘Lua’ then

local replace = {}

replace[‘0010,1002’] = {}

replace[‘0010,1002’][1] = {}

replace[‘0010,1002’][1][‘PatientID’] = ‘Hello’

replace[‘0010,1002’][2] = {}

replace[‘0010,1002’][2][‘PatientID’] = ‘World’

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…
`

As I understand it, this script is supposed to rename the patient name for each new study.

After restarting Orthanc, I uploaded a CT study in Orthanc. The study is now in Orthanc but the patient name hasn’t been changed, as if the script wasn’t applied.

Sadly, logs doesn’t mentions anything.

`
W0811 10:18:32.333931 main.cpp:1625] Orthanc version: 1.7.2
W0811 10:18:32.333931 OrthancConfiguration.cpp:113] Scanning folder “Configuration” for configuration files
W0811 10:18:32.333931 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\dicomweb.json”
W0811 10:18:32.335797 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\mysql.json”
W0811 10:18:32.336736 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\orthanc.json”
W0811 10:18:32.337734 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\postgresql.json”
W0811 10:18:32.337734 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\serve-folders.json”
W0811 10:18:32.338785 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\transfers.json”
W0811 10:18:32.338785 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\webviewer.json”
W0811 10:18:32.338785 OrthancConfiguration.cpp:61] Reading the configuration from: “Configuration\worklists.json”
W0811 10:18:32.825878 main.cpp:802] Loading plugin(s) from: Configuration../Plugins/
W0811 10:18:32.839842 PluginsManager.cpp:269] Registering plugin ‘connectivity-checks’ (version 1.7.2)
W0811 10:18:32.844828 PluginsManager.cpp:269] Registering plugin ‘worklists’ (version 1.7.2)
W0811 10:18:32.844828 PluginsManager.cpp:168] Sample worklist plugin is initializing
W0811 10:18:32.844828 PluginsManager.cpp:168] Worklist server is disabled by the configuration file
W0811 10:18:32.850904 PluginsManager.cpp:269] Registering plugin ‘authorization’ (version 0.2.3)
W0811 10:18:32.850904 PluginsManager.cpp:168] Initializing the authorization plugin
W0811 10:18:32.850904 PluginsManager.cpp:168] No section “Authorization” in the configuration file, the authorization plugin is disabled
W0811 10:18:32.882727 PluginsManager.cpp:269] Registering plugin ‘dicom-web’ (version 1.2)
W0811 10:18:32.882727 PluginsManager.cpp:168] URI to the DICOMweb REST API: /dicom-web/
W0811 10:18:32.883757 PluginsManager.cpp:168] URI to the WADO-URI API: /wado
W0811 10:18:32.918632 PluginsManager.cpp:269] Registering plugin ‘gdcm’ (version 1.0)
W0811 10:18:32.918632 PluginsManager.cpp:168] Version of GDCM: 3.0.6
W0811 10:18:32.918632 PluginsManager.cpp:168] GDCM throttling is disabled
W0811 10:18:32.953537 PluginsManager.cpp:269] Registering plugin ‘mysql-index’ (version 2.0)
W0811 10:18:32.955532 PluginsManager.cpp:168] The MySQL index is currently disabled, set “EnableIndex” to “true” in the “MySQL” section of the configuration file of Orthanc
W0811 10:18:32.975479 PluginsManager.cpp:269] Registering plugin ‘mysql-storage’ (version 2.0)
W0811 10:18:32.977473 PluginsManager.cpp:168] The MySQL storage area is currently disabled, set “EnableStorage” to “true” in the “MySQL” section of the configuration file of Orthanc
W0811 10:18:33.092215 PluginsManager.cpp:269] Registering plugin ‘postgresql-index’ (version 3.2)
W0811 10:18:33.092215 PluginsManager.cpp:168] The PostgreSQL index is currently disabled, set “EnableIndex” to “true” in the “PostgreSQL” section of the configuration file of Orthanc
W0811 10:18:33.114167 PluginsManager.cpp:269] Registering plugin ‘postgresql-storage’ (version 3.2)
W0811 10:18:33.115164 PluginsManager.cpp:168] The PostgreSQL storage area is currently disabled, set “EnableStorage” to “true” in the “PostgreSQL” section of the configuration file of Orthanc
W0811 10:18:33.127148 PluginsManager.cpp:269] Registering plugin ‘transfers’ (version 1.0)
W0811 10:18:33.154002 PluginsManager.cpp:269] Registering plugin ‘web-viewer’ (version 2.6)
W0811 10:18:33.154002 PluginsManager.cpp:168] Initializing the Web viewer
W0811 10:18:33.155997 PluginsManager.cpp:168] Web viewer using 4 threads for the decoding of the DICOM images
W0811 10:18:33.155997 PluginsManager.cpp:168] Storing the cache of the Web viewer in folder: C:\Orthanc\WebViewerCache
W0811 10:18:33.161981 PluginsManager.cpp:168] Web viewer using a cache of 100 MB
W0811 10:18:33.179983 PluginsManager.cpp:269] Registering plugin ‘wsi’ (version 0.7)
W0811 10:18:33.179983 PluginsManager.cpp:168] The whole-slide imaging plugin will use at most 8 threads to transcode the tiles
W0811 10:18:33.368841 PluginsManager.cpp:269] Registering plugin ‘osimis-web-viewer’ (version 1.3.1.0-ea114768)
W0811 10:18:33.368841 PluginsManager.cpp:168] Initializing the Web viewer
W0811 10:18:33.369827 PluginsManager.cpp:168] Using GDCM instead of the DICOM decoder that is built in Orthanc
W0811 10:18:33.379800 PluginsManager.cpp:269] Registering plugin ‘serve-folders’ (version 1.7.2)
W0811 10:18:33.381799 PluginsManager.cpp:168] ServeFolders: Empty configuration file: No additional folder will be served!
W0811 10:18:33.381799 OrthancInitialization.cpp:293] SQLite index directory: “C:\Orthanc”
W0811 10:18:33.384841 OrthancInitialization.cpp:368] Storage directory: “C:\Orthanc”
W0811 10:18:33.388733 HttpClient.cpp:1055] HTTPS will use the CA certificates from this file: Configuration
W0811 10:18:33.389789 LuaContext.cpp:93] Lua says: Lua toolbox installed
W0811 10:18:33.391726 LuaContext.cpp:93] Lua says: Lua toolbox installed
W0811 10:18:33.393723 ServerContext.cpp:390] Disk compression is disabled
W0811 10:18:33.393723 ServerIndex.cpp:1636] No limit on the number of stored patients
W0811 10:18:33.393723 ServerIndex.cpp:1653] No limit on the size of the storage area
W0811 10:18:33.393723 ServerContext.cpp:191] Reloading the jobs from the last execution of Orthanc
W0811 10:18:33.396764 JobsEngine.cpp:272] The jobs engine has started with 2 threads
W0811 10:18:33.404745 main.cpp:1109] DICOM server listening with AET ORTHANC on port: 4242
W0811 10:18:33.405688 HttpServer.cpp:1298] HTTP compression is enabled
W0811 10:18:33.405688 main.cpp:1011] Remote LUA script execution is disabled
W0811 10:18:33.409677 HttpServer.cpp:1205] HTTP server listening on port: 8042 (HTTPS encryption is disabled, remote access is not allowed)
W0811 10:18:33.409677 main.cpp:814] Orthanc has started

`

Can you help me ?

The logs indicate that your Lua script is not loaded.

Make sure that the “LuaScripts” configuration option of Orthanc is properly set to point to your Lua script:
https://book.orthanc-server.com/users/lua.html#installing-a-lua-script

https://hg.orthanc-server.com/orthanc/file/Orthanc-1.7.2/OrthancServer/Resources/Configuration.json#l44

That’s weird, here’s my config file:

// List of paths to the custom Lua scripts that are to be loaded // into this instance of Orthanc "LuaScripts" : [ "C:\\Orthanc\\Script.lua" ],

Script.lua saved in C:\Orthanc.
It worked before as I was able to see errors in my previous test scripts. I don’t get it. Is there a other parameter I missed somewhere?

Triple check your configuration. That’s all I can tell you.

You must find a line similar to the following when running Orthanc in “–verbose” mode:

I0811 11:27:09.114632 LuaScripting.cpp:925] Installing the Lua scripts from: Script.lua

https://book.orthanc-server.com/faq/log.html