Greetings,
I have absolutely no experience in using Python so I was hoping some kind soul might be able to help me with a Python script I need that logs the StudyId, StudyUID and time of arrival of all incoming studies to a Postgresql db table.
I know there is a OnStoredInstance event that is fired (hope that’s the correct Python terminology) on the storage of each instance. Is there one for studies as well ie something like OnStoredStudy?
There is a StableAge setting in orthanc.json or elsewhere (i.e. Docker)
// Number of seconds without receiving any instance before a
// patient, a study or a series is considered as stable.
“StableAge” : 60,
You can adjust that and maybe use something like from the example here: https://book.orthanc-server.com/plugins/python.html?highlight=python#auto-routing-studies:
def OnChange(changeType, level, resourceId):
if changeType == orthanc.ChangeType.STABLE_STUDY:
study = json.loads(orthanc.RestApiGet(‘/studies/%s’ % resource))
orthanc.LogWarning(‘A Study is Stable, ID: %s’ % resource)
orthanc.RegisterOnChangeCallback(OnChange)
That doesn’t necessarily mean that the study is complete, just that there have not been any new instances since that period of time. If there are subsequent instances sent later it’ll trigger the OnChange again.
You would need to add a MySQL or PostGres connector to save it in a DB from the Python script::
study[‘ID’] will be the orthanc uuid, and study[‘MainDicomTags’][‘StudyInstanceUID’] will be the UID.
It isn’t that hard to add the MySQL connector and code. I’ve never done that for a Postgres DB, but probably very similar with a different connector.
There are PIP modules for both:
import mysql.connector # sudo python3 -m pip install mysql-connector-python https://pypi.org/project/mysql-connector-python/
import the psycopg2 database adapter for PostgreSQL
import psycopg2 # RUN pip3 install psycopg2, https://www.psycopg.org/docs/install.html#quick-install
Stephen D. Scotti, M.D.
Oh wow thank you for the info. I’ll look into it when I get time and report back here.
Hello,
Depending on your scenario, note that there is also the “orthanc.ChangeType.NEW_STUDY” event:
https://book.orthanc-server.com/plugins/python.html#listening-to-changes
Sébastien-
Here is what I came up with…serves my purpose perfectly
from io import BytesIO
import json
from pydicom import dcmread, dcmwrite
from pydicom.filebase import DicomFileLike
import psycopg2
import orthanc
from datetime import datetime
def OnChange(changeType, level, resourceId):
if changeType == orthanc.ChangeType.NEW_STUDY:
study = json.loads(orthanc.RestApiGet(‘/studies/%s’ % resourceId))
orthanc.LogWarning(‘A NEW STUDY HAS ARRIVED, UID: %s’ % study[‘MainDicomTags’][‘StudyInstanceUID’])
connection = psycopg2.connect(user = “postgres”, password = “alnasarpacs577”, host = “127.0.0.1”, port = “5432”, database = “PACS”)
cursor = connection.cursor()
postgres_insert_query = “”“INSERT INTO tblstudylog (studyid, studyuid, arrivaldatetime) VALUES (%s,%s,%s)”“”
record_to_insert = (study[‘ID’], study[‘MainDicomTags’][‘StudyInstanceUID’], datetime.datetime.now().strftime(“%Y%m%d%H%M%S”))
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
cursor.close()
connection.close()
orthanc.RegisterOnChangeCallback(OnChange)
Thanks for all the help Stephen and Sebastian