Package: ppyorthanc
PPyOrthancPython library that "purely" wraps the Orthanc REST API and facilitates the manipulation
of data with several cool utilities.This is a fork of pyorthanc that avoids raising errors for failing requests.Installation$pipenvinstallgit+https://github.com/dnlcrl/pyorthanc-pure#egg=ppyorthancExample of usageBe sure that Orthanc is running. The default URL (if running locally) ishttp://localhost:8042.Getting access to patients, studies, series and instances information:fromppyorthancimportOrthancorthanc=Orthanc('http://localhost:8042',username='username',password='password')# To get patients identifier and main informationpatients_identifiers=orthanc.get_patients().json()forpatient_identifierinpatients_identifiers:# To get patient informationpatient_info=orthanc.get_patients_id(patient_identifier).json()patient_name=patient_info['MainDicomTags']['PatientName']...study_identifiers=patient_info['Studies']# To get patient's studies identifier and main informationforstudy_identifierinstudy_identifiers:# To get Study infostudy_info=orthanc.get_studies_id(study_identifier).json()study_date=study_info['MainDicomTags']['StudyDate']...series_identifiers=study_info['Series']# To get study's series identifier and main informationforseries_identifierinseries_identifiers:# Get series infoseries_info=orthanc.get_series_id(series_identifier).json()modality=series_info['MainDicomTags']['Modality']...instance_identifiers=series_info['Instances']# and so on ...forinstance_identifierininstance_identifiers:instance_info=orthanc.get_instances_id(instance_identifier).json()...Find patients with certain characteristics in an Orthanc instance:Each patient is a tree. Layers in each tree have the following structurePatient->Study->Series->Instancethat correspond to the provided filter functions.frompyorthancimportfindpatients=find(orthanc_url='http://localhost:8042/',auth=('username','password'),series_filter=lambdas:s.modality=='RTDOSE'# Optional: filter with pyorthanc.Series object)forpatientinpatients:patient_info=patient.get_main_information()patient.id_# Access PatientIDpatient.name# Access PatientNamepatient.get_zip()# DICOM files' content in bytesanonymized_patient_1_resp=patient.anonymize()# New patient that was anonymized by Orthancanonymized_patient_1=Patient(anonymized_patient_1_resp["PatientID"],client=patient.client)anonymized_patient_2_resp=patient.anonymize(keep=['PatientName'],# You can keep/remove/replace the DICOM tags you wantreplace={'PatientID':'TheNewPatientID'},remove=['ReferringPhysicianName'],force=True# Needed when changing PatientID/StudyInstanceUID/SeriesInstanceUID/SOPInstanceUID)anonymized_patient_2=Patient(anonymized_patient_2_resp["PatientID"],client=patient.client)...forstudyinpatient.studies:study.date# Date as a datetime objectstudy.referring_physician_name...forseriesinstudy.series:series.modality# Should be 'RTDOSE' because of the series_filter parameters...Upload DICOM files to Orthanc:frompyorthancimportOrthancorthanc=Orthanc('http://localhost:8042','username','password')withopen('A_DICOM_INSTANCE_PATH.dcm','rb')asfile:orthanc.post_instances(file.read())Getting list of connected remote modalities:frompyorthancimportOrthancorthanc=Orthanc('http://localhost:8042','username','password')orthanc.get_modalities()Query (C-Find) and Retrieve (C-Move) from remote modality:frompyorthancimportRemoteModality,Orthancorthanc=Orthanc('http://localhost:8042','username','password')modality=RemoteModality(orthanc,'modality')# Query (C-Find) on modalitydata={'Level':'Study','Query':{'PatientID':'*'}}query_response=modality.query(data=data)answer=modality.get_query_answers()[query_response['ID']]print(answer)# Retrieve (C-Move) results of query on a target modality (AET)modality.move(query_response['ID'],{'TargetAet':'target_modality'})Anonymize patient:frompyorthancimportOrthanc,Patientorthanc=Orthanc('http://localhost:8042','username','password')patient_identifier=orthanc.get_patients().json()[0]anonymized_patient=Patient(patient_identifier,orthanc).anonymize(keep=['PatientName'],# You can keep/remove/replace the DICOM tags you wantreplace={'PatientID':'TheNewPatientID'},remove=['ReferringPhysicianName'],force=True# Needed when changing PatientID/StudyInstanceUID/SeriesInstanceUID/SOPInstanceUID)# Or directly withorthanc.post_patients_id_anonymize(patient_identifier).json()# result is: (you can retrieve DICOM file from ID)# {'ID': 'dd41f2f1-24838e1e-f01746fc-9715072f-189eb0a2',#  'Path': '/patients/dd41f2f1-24838e1e-f01746fc-9715072f-189eb0a2',#  'PatientID': 'dd41f2f1-24838e1e-f01746fc-9715072f-189eb0a2',#  'Type': 'Patient'}CitationIf you publish using PyOrthanc, we kindly ask that you credit us. PyOrthanc can be found on Zenodo :https://zenodo.org/record/7086219.ContributingYou can contribute to this project with the following steps:First, fork the project on GithubClone the projectgitclonehttps://github.com/<your-github-username>/pyorthanccdpyorthancEnter the project and create a poetry environment
(this project use thepoetryfor dependency management)peotryinstallMake a new git branch where you will apply the changesgitcheckout-byour-branch-nameNow you can make your changesOnce done,git add,git commitandgit pushthe changes.Make a Pull Request from your branch to thehttps://github.com/ulaval-rs/pyorthanc.