Trouble With LUA File for Renaming Folders

192 views
Skip to first unread message

Arthur

unread,
May 8, 2018, 4:56:38 PM5/8/18
to Orthanc Users
I'm trying to change how folder names are created with Orthanc. Currently it seems to make random folder names. We need it to create the name based on the patientid instead. I found references on here for a LUA script to customize the names. When I save the following script and have it loaded in the config, I constantly get a strange error. 

Here's the script:

TARGET = 'C:\\Shared\\Images'

function ToAscii(s)
   return s
end

function OnStableSeries(seriesId, tags, metadata)
   print('This series is now stable, writing its instances on the disk: ' .. seriesId)

   local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances']
   local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags']
   local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags']
   local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags']

   for i, instance in pairs(instances) do

      local path = ToAscii(TARGET)

      local dicom = RestApiGet('/instances/' .. instance .. '/file')

      local target = assert(io.open(path .. '\\' .. 'H' .. patient['PatientID'] .. '-' .. series['SeriesNumber'] .. '-' .. i .. '.dcm', 'wb'))

      target:write(dicom)
      target:close()
   end
end

Here's the error showing in the logs when trying to start the service:

0508 15:49:54.102709 main.cpp:651] Installing the Lua scripts from: C:\Program Files\Orthanc Server\namechange.txt

E0508 15:49:54.103709 LuaContext.cpp:580] Error while executing Lua script: [string "line"]:1: unexpected symbol near 'ÿ'

E0508 15:49:54.103709 ServerContext.cpp:146] INTERNAL ERROR: ServerContext::Stop() should be invoked manually to avoid mess in the destruction order!

Any help on what I'm doing wrong?

Alain Mazy

unread,
May 9, 2018, 3:33:43 AM5/9/18
to Arthur, Orthanc Users
[string "line"]:1: unexpected symbol near 'ÿ'

It looks your lua file is invalid.  Try changing the encoding to UTF-8 without BOM.  This can be done with Notepad++.
--

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

Osimis

OSIMIS S.A. 
Rue bois Saint-Jean 15/1BE-4102 Seraing 
www.osimis.io

Twitter LinkedIn


Arthur

unread,
May 9, 2018, 10:29:23 AM5/9/18
to Orthanc Users
That was the issue! It now loads, worked for one processing of files, then never again. Would someone be able to look at the following and see what I'm doing wrong? I need to have it write the files to a folder with the patient id as the folder name. Currently, it always created seemingly random two character folder names which we can't easily search.

TARGET = 'C:\\Shared\\Images'

function ToAscii(s)
   return s
end

function OnStableSeries(seriesId, tags, metadata)
   print('This series is now stable, writing its instances on the disk: ' .. seriesId)

   local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances']
   local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags']
   local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags']
   local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags']

   for i, instance in pairs(instances) do

      local path = ToAscii(TARGET)

      local dicom = RestApiGet('/instances/' .. instance .. '/file')

      local target = assert(io.open(path .. '\\' .. patient['PatientID'] '\\' .. series['SeriesNumber'] .. '-' .. i .. '.dcm', 'wb'))

      target:write(dicom)
      target:close()
   end
end

Sébastien Jodogne

unread,
May 9, 2018, 11:15:36 AM5/9/18
to Orthanc Users
A working sample to achieve your goal is available in the source distribution of Orthanc:

Sébastien-

Marcelo Duarte

unread,
May 9, 2018, 7:01:47 PM5/9/18
to Orthanc Users
WritetoDisk.lua is great.

I've been working some issues, that I believe may be usefull to share.

My first line is:
TARGET = 'c:/Users/mgmdu/Pictures/orthanc'

and what I got in return was the creation of folders in C:/Orthanc like this - c:/orthanc/c_/Users/mgmdu...... and so on

The problem was that this function  - gsub('[^a-zA-Z0-9-/ ]', '_') - changed the ":" to "_" and this way, the mkdir couldn´t understand the arguments in - path.

So I added "-:" to gsub arguments. Like this: gsub('[^a-zA-Z0-9-/-: ]', '_')

Now its working fine.

I´m not used to the correct terms in coding, but I hope this may be usefull

Congrats Sébastien. Great job with Orthanc.

Marcelo

Sébastien Jodogne

unread,
May 11, 2018, 2:39:32 AM5/11/18
to Orthanc Users
Dear Marcelo,

Fine, glad to read that your issue is now solved!

I have updated the "WriteToDisk.lua" sample accordingly:

Thanks for sharing your insights. 

Regards,
Sébastien-
Reply all
Reply to author
Forward
0 new messages