annotate OrthancServer/Resources/Samples/Lua/ClassifyUsingAet.lua @ 5582:17da828f9443 find-refactoring

removed FindResponse::Item::level_
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 27 Apr 2024 21:42:32 +0200
parents cbf9afa17415
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4248
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 -- Write the received DICOM instances to an external directory, and
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 -- group the patients by the source AET that has sent the DICOM
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 -- files. This sample is similar to "WriteToDisk.lua".
4250
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4249
diff changeset
4 -- https://groups.google.com/g/orthanc-users/c/7RU1EVi9VYY/m/tBdvkeI1AAAJ
4248
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 TARGET = '/tmp/lua'
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 function ToAscii(s)
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 -- https://groups.google.com/d/msg/orthanc-users/qMLgkEmwwPI/6jRpCrlgBwAJ
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 return s:gsub('[^a-zA-Z0-9-/-: ]', '_')
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 end
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 function GetFromTable(t, key, defaultValue)
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 local result = t[key]
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 if result == nil or result == '' then
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 return defaultValue
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 else
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 return result
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 end
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 end
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 function OnStoredInstance(instanceId, tags, metadata, origin)
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 local tags = ParseJson(RestApiGet('/instances/' .. instanceId .. '/tags?simplify'))
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 local metadata = ParseJson(RestApiGet('/instances/' .. instanceId .. '/metadata?expand'))
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
4249
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
32 local path = ToAscii(TARGET .. '/' ..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
33 GetFromTable(metadata, 'RemoteAET', 'None') .. '/'..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
34 GetFromTable(tags, 'StudyDate', '') .. '/' ..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
35 GetFromTable(tags, 'PatientID', '') .. ' - ' ..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
36 GetFromTable(tags, 'PatientName', '') .. ' - ' ..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
37 GetFromTable(tags, 'StudyDescription', '') .. '/' ..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
38 GetFromTable(tags, 'Modality', '') .. ' - ' ..
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
39 GetFromTable(tags, 'SeriesDescription', ''))
4248
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 -- http://stackoverflow.com/a/16029744/881731
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 os.execute('mkdir -p "' .. path .. '"')
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 -- Write to the file
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 local target = assert(io.open(path .. '/' .. GetFromTable(tags, 'SOPInstanceUID', 'none') .. '.dcm', 'wb'))
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 target:write(dicom)
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 target:close()
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
4249
485f9be9ba92 improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4248
diff changeset
50 -- Optional step: Remove the DICOM instance from Orthanc
4248
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 RestApiDelete('/instances/' .. instanceId)
38b24aa98ed3 new lua sample: ClassifyUsingAet.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 end