Skip to content

Commit

Permalink
DOC: Add script to query and retrieve DICOM with ctkDICOMScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Punzo committed Sep 2, 2024
1 parent c8030b7 commit 3268e4c
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions Docs/developer_guide/script_repository/dicom.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,139 @@ for study, series in dicomQuery.studyAndSeriesInstanceUIDQueried:
slicer.dicomDatabase.updateDisplayedFields()
```

### Query and retrieve data from a PACS using classic DIMSE DICOM networking with the (experimental) ctkDICOMVisualBrowser

```python

# Get visual browser instance
visualBrowser = slicer.modules.dicom.widgetRepresentation().self().browserWidget.dicomVisualBrowser
dicomDatabase = visualBrowser.dicomDatabase()

# Disable query/retrieve for all existing servers
for index in range (0, visualBrowser.serversCount()):
server = visualBrowser.getNthServer(index)
server.queryRetrieveEnabled = False

# Add a new DICOM server
server = ctk.ctkDICOMServer()
server.connectionName = "test"
server.callingAETitle = "SLICER"
server.calledAETitle = "ANYAE"
server.host = "dicomserver.co.uk"
server.port = 104
server.retrieveProtocol = ctk.ctkDICOMServer.CGET

if visualBrowser.addServer(server) == -1:
raise RuntimeError("Failed to add server")

# Set the filters for the query
visualBrowser.filteringPatientID = "VTK3D"
visualBrowser.filteringPatientName = "VTK3D"
#visualBrowser.filteringStudyDescription = "Study description"
visualBrowser.filteringDate = ctk.ctkDICOMPatientItemWidget.LastYear
#Date options:
#Any,
#Today,
#Yesterday,
#LastWeek,
#LastMonth,
#LastYear
#visualBrowser.filteringSeriesDescription = "Series description"
visualBrowser.filteringModalities = ["CT", "MR"]

# Run patient query.
# NOTE: this will automatically also start query/retrieve jobs at study and series levels
visualBrowser.onQueryRetrieveOptionToggled(True)
visualBrowser.onQueryPatients()
```

### Query and retrieve data from a PACS using classic DIMSE DICOM networking with the (experimental) ctkDICOMScheduler (no UI needed)

```python


# Add a new DICOM server
server = ctk.ctkDICOMServer()
server.connectionName = "test"
server.callingAETitle = "SLICER"
server.calledAETitle = "ANYAE"
server.host = "dicomserver.co.uk"
server.port = 104
server.retrieveProtocol = ctk.ctkDICOMServer.CGET

scheduler = ctk.ctkDICOMScheduler()
scheduler.setDicomDatabase(slicer.dicomDatabase)
scheduler.addServer(server)

# Set the filters for the query
nDays = 325
endDate = qt.QDate().currentDate()
startDate = endDate.addDays(-nDays)
parameters = {
"ID": "VTK3D",
"Name": "VTK3D",
#"Study": "Study description",
#"Series": "Series description",
"Modalities": ["CT", "MR"],
"StartDate": startDate.toString("yyyyMMdd"),
"EndDate": endDate.toString("yyyyMMdd")
}

class Receiver(qt.QObject):
def __init__(self, scheduler, parameters):
super().__init__()
self.scheduler = scheduler
self.parameters = parameters
self.scheduler.setFilters(self.parameters)
self.scheduler.progressJobDetail.connect(self.onProgressDetails)
self.scheduler.jobFinished.connect(self.onJobFinished)
self.scheduler.jobFailed.connect(self.onJobFailed)

def startQueryRetrieve(self):
self.scheduler.queryPatients()

def onJobFinished(self, details):
for detail in details:
if detail.jobType() == ctk.ctkDICOMJobResponseSet.QueryPatients:
print ("Query patients success. Parameters used: ", self.parameters)
elif detail.jobType() == ctk.ctkDICOMJobResponseSet.QueryStudies:
patientID = detail.patientID()
print ("Query studies success for patientID: ", patientID)
elif detail.jobType() == ctk.ctkDICOMJobResponseSet.RetrieveStudy:
patientID = detail.patientID()
studyInstanceUID = detail.studyInstanceUID()
print ("Retrieve studies success for studyInstanceUID: ", studyInstanceUID, " (patientID: ",patientID, ")")

def onJobFailed(self, details):
for detail in details:
if detail.jobType() == ctk.ctkDICOMJobResponseSet.QueryPatients:
print ("Query patients failed. Parameters used: ", self.parameters)
elif detail.jobType() == ctk.ctkDICOMJobResponseSet.QueryStudies:
patientID = detail.patientID()
print ("Query studies failed for patientID: ", patientID)
elif detail.jobType() == ctk.ctkDICOMJobResponseSet.RetrieveStudy:
patientID = detail.patientID()
studyInstanceUID = detail.studyInstanceUID()
print ("Retrieve studies failed for studyInstanceUID: ", studyInstanceUID, " (patientID: ",patientID, ")")

def onProgressDetails(self, details):
for detail in details:
if detail.jobType() == ctk.ctkDICOMJobResponseSet.QueryPatients:
patientID = detail.patientID()
print ("Starting studies query for patient: ", patientID)
scheduler.queryStudies(patientID)
elif detail.jobType() == ctk.ctkDICOMJobResponseSet.QueryStudies:
patientID = detail.patientID()
studyInstanceUID = detail.studyInstanceUID()
print ("Starting studies retrieve for studyInstanceUID: ", studyInstanceUID, " (patientID: ",patientID, ")")
scheduler.retrieveStudy(patientID, studyInstanceUID)


receiver = Receiver(scheduler, parameters)
receiver.startQueryRetrieve()

```

### Send data to a PACS using classic DIMSE DICOM networking

```python
Expand Down

0 comments on commit 3268e4c

Please sign in to comment.