Auto-Routing with Anonimization

Right now the docs give this as an example of performing DICOM routing:

function OnStoredInstance(instanceId, tags, metadata)
   Delete(SendToModality(instanceId, 'sample'))
end

Is there some way to anonymize the study before sending. Something like this perhaps:

function OnStoredInstance(instanceId, tags, metadata)
   Delete(SendToModality(Anonymize(instanceId), 'sample'))
end

Hi!

This has been addressed (somewhat) in another thread:
https://groups.google.com/forum/#!topic/orthanc-users/AmiCxrnib0I

If not mistaken, you can modify most tags this way except PatientID. Because Lua scripts handle each instance separately, I believe that it cannot safely generate a random PatientID. You’d probably need some other method if you want to anonymize PatientID (after the study received has "stable"lized).

Regards,
Em

If I am reading that other thread correctly, it suggest anonymizing field by field. This seems far less ideal than the built in anonymizer described here: https://code.google.com/p/orthanc/wiki/Anonymization

I’m merely a user, not the developer, so I’ll provide the following answer “as far as I know”: (Somebody smarter than I will probably need to confirm or refute this answer.)

[begin answer]
For https://code.google.com/p/orthanc/wiki/Anonymization, when all the instances have been stored into Orthanc, Orthanc knows which instances belong to which series / studies / patient. You can thus anonymize all those instances at once by calling the anonymization via REST and Orthanc will know to assign the same anonymized PatientID to all those instances.

However, the Lua script handles each instance individually as they are coming in. Meaning that it does not know whether the instances that are coming in are coming from the same patient or not. Therefore, it cannot assign a single anonymized PatientID to all those instances because they may actually come from different patients.
[end answer]

Again, I reiterate that this is what (little) I understand of Lua scripting in Orthanc.

If I had to solve this problem, I might possibly loop a php script to detect /changes, call the anonymization and do a STORE to the destination modality. But then again, it’s obviously not as efficient as on-the-fly anonymization.

The issue is that means the non-anonymized data is potentially persisted in the database / file system until the PHP script gets around to calling the anonymize / store operation.

Yes, this is ultimately what held us back from using this. In the end, we decided to directly modify the dicom files before routing them to other places, e.g. with storescu.

Jon

Hello,

Since Orthanc 0.9.1, the Lua scripts have full access to the REST API of Orthanc and can be triggered when all the instances of a study have been received:
https://code.google.com/p/orthanc/source/browse/Resources/Samples/Lua/OnStableStudy.lua

This allows to carry on an anonymization of all the instances of a study in a single step, which was not possible previously.

The sample script above can be complemented by a second call to “RestApiPost()” to trigger a C-Store SCU to another modality.

HTH,
Sébastien-

How would I write a Lua script to save the anonymized study locally? i.e. the non anonymized study never gets saved to any volatile storage?

That should say *non-*volatile storage.

The Orthanc distribution now contains a sample Lua script showing how to save DICOM files to the filesystem:
https://code.google.com/p/orthanc/source/browse/Resources/Samples/Lua/WriteToDisk.lua

Pay attention that this sample will only work on the mainline code and for versions of Orthanc above 0.9.2 (to be released).

Regards,
Sébastien-

How does this worth though: https://code.google.com/p/orthanc/source/browse/Resources/Samples/Lua/WriteToDisk.lua#23 ?
Does the DICOM already have to be saved?

What do you mean by “saved to the filesystem”?

Whenever Orthanc receives a DICOM instance, it first writes this file in its private database before calling any Lua script or any plugin. By default, this private database lies in the filesystem (inside the “OrthancStorage” directory).

If you do not want this file to be present in the filesystem, you could use the official PostgreSQL plugin (so that the file is stored in a secured PostgreSQL database). You could also write a plugin that replaces the filesystem storage with a fully volatile, memory-based storage, provided you have enough RAM [1].

[1] https://groups.google.com/d/msg/orthanc-users/WetkJVJB-w8/TOo3N0tHfxYJ

Okay. The example here: https://code.google.com/p/orthanc/source/browse/Resources/Samples/Lua/OnStableStudy.lua#29
anonymizes on a field by field basis rather than using the anonymization endpoint. Is it possible to re-use the anonymization endpoint rather than having to code the individual fields in the Lua script?

Sure, you just have to replace the line:

local m = RestApiPost(‘/studies/’ … studyId … ‘/modify’, DumpJson(command))

by:

local m = RestApiPost(‘/studies/’ … studyId … ‘/anonymize’, DumpJson(command))

Have a look at the following wiki page to know the allowed arguments for “command”:
https://code.google.com/p/orthanc/wiki/Anonymization