Python Auto-route and called AET

Hi All,

I am experimenting with using Orthanc and the associated Python plugin to auto-route studies based on called AET.

In the sample code auto-route code below from the documentation:

import orthanc

def OnChange(changeType, level, resourceId):
    if changeType == orthanc.ChangeType.STABLE_STUDY:
        print('Stable study: %s' % resourceId)
        orthanc.RestApiPost('/modalities/sample/store', resourceId)

orthanc.RegisterOnChangeCallback(OnChange)

The resourceId seems to be at the study level. However the called AET is only found in the metadata at the instance level. I can’t find an easy way to use the study resourceId to find any of the stored instance Ids. If i could, then I could easily use the REST API to get the calledAET.

What is the easiest way to do so? I thought of reimplementing the SHA1 hash function
for Instance Ids as listed here (but this seems like a little unelegant).

Instances are identified as the SHA-1 hash of the concatenation of 
their PatientID tag (0010,0020),
their StudyInstanceUID tag (0020,000d), 
their SeriesInstanceUID tag (0020,000e), and 
their SOPInstanceUID tag (0008,0018).

Any help on best way to approach this would be much appreciated. Thank you.

Hi Mex,

You may retrieve the metadata from the first instance of the study with this code:

import orthanc
import json
import pprint

def OnChange(changeType, level, resourceId):
    if changeType == orthanc.ChangeType.STABLE_STUDY:
        print('Stable study: %s' % resourceId)

        instances_ids = json.loads(orthanc.RestApiGet(f"/studies/{resourceId}/instances"))
        instance_metadata = json.loads(orthanc.RestApiGet(f"/instances/{instances_ids[0]['ID']}/metadata?expand"))
        
        pprint.pprint(instance_metadata)
        if "CalledAET" in instance_metadata and "RemoteAET" in instance_metadata:
            print(f"This study orginates from {instance_metadata['RemoteAET']} and was addressed to {instance_metadata['CalledAET']}")

        # orthanc.RestApiPost('/modalities/sample/store', resourceId)

orthanc.RegisterOnChangeCallback(OnChange)

HTH,

Alain.

1 Like

Just want to take a moment to thank you for your work on Orthanc, it’s really a great piece of software, and thanks for your assistance here.

Dear Mex,

Thanks for your positive feedback! It is always nice to read such kind words.

Kind Regards,
Sébastien-