Beispiel #1
0
def _search_for_studies(args):
    '''Searches for *Studies* and writes metadata to standard output.'''
    params = _parse_search_parameters(args)
    client = DICOMwebClient(args.url,
                            username=args.username,
                            password=args.password,
                            ca_bundle=args.ca_bundle,
                            cert=args.cert)
    studies = client.search_for_studies(**params)
    _print_metadata(studies, args.prettify, args.dicomize)
Beispiel #2
0
    def _PubsubCallback(self, message):
        # type: pubsub_v1.Message -> None
        """Processes a Pubsub message.

    This function will retrieve the instance (specified in Pubsub message) from
    the Cloud Healthcare API. Then it will invoke CMLE to get the prediction
    results. Finally (and optionally), it will store the inference results back
    to the Cloud Healthcare API as a DICOM Structured Report. The instance URL
    to the Structured Report containing the prediction is then published to a
    pub/sub.

    Args:
      message: Incoming pubsub message.
    """
        image_instance_path = message.data.decode()
        _logger.debug('Received instance in pubsub feed: %s',
                      image_instance_path)
        try:
            parsed_message = pubsub_format.ParseMessage(
                message, dicom_path.Type.INSTANCE)
        except exception.CustomExceptionError:
            _logger.info('Invalid input path: %s', image_instance_path)
            message.ack()
            return
        input_path = parsed_message.input_path
        authed_session = session_utils.create_session_from_gcp_credentials()
        dicomweb_url = posixpath.join(_HEALTHCARE_API_URL_PREFIX,
                                      input_path.dicomweb_path_str)
        input_client = DICOMwebClient(dicomweb_url, authed_session)
        if not self._IsMammoInstance(input_client, input_path):
            _logger.info(
                'Instance is not of type MG modality, ignoring message: %s',
                image_instance_path)
            message.ack()
            return

        _logger.info('Processing instance: %s', image_instance_path)
        # Retrieve instance from DICOM API in JPEG format.
        image_jpeg_bytes = input_client.retrieve_instance_rendered(
            input_path.study_uid,
            input_path.series_uid,
            input_path.instance_uid,
            media_types=('image/jpeg', ))
        # Retrieve study level information
        study_json = input_client.search_for_studies(
            fields=['all'],
            search_filters={'StudyInstanceUID': input_path.study_uid})[0]
        # Get the predicted score and class from the inference model in Cloud ML or
        # AutoML.
        try:
            predicted_class, predicted_score = self._predictor.Predict(
                image_jpeg_bytes)
        except PermissionDenied as e:
            _logger.error('Permission error running prediction service: %s', e)
            message.nack()
            return
        except InvalidArgument as e:
            _logger.error(
                'Invalid arguments when running prediction service: %s', e)
            message.nack()
            return

        # Print the prediction.
        text = 'Base path: %s\nPredicted class: %s\nPredicted score: %s' % (
            image_instance_path, predicted_class, predicted_score)
        _logger.info(text)

        # If user requested destination DICOM store for inference, create a DICOM
        # structured report that stores the prediction.
        if self._dicom_store_path:
            # Create DICOMwebClient with output url.
            output_dicom_web_url = posixpath.join(
                _HEALTHCARE_API_URL_PREFIX,
                self._dicom_store_path.dicomweb_path_str)
            output_client = DICOMwebClient(output_dicom_web_url,
                                           authed_session)

            # Generate series uid and instance uid for the structured report.
            sr_instance_uid = pydicom.uid.generate_uid(prefix=None)
            sr_series_uid = pydicom.uid.generate_uid(prefix=None)
            sr_dataset = _BuildSR(study_json, text, sr_series_uid,
                                  sr_instance_uid)
            try:
                output_client.store_instances([sr_dataset])
            except RuntimeError as e:
                _logger.error('Error storing DICOM in API: %s', e)
                message.nack()
                return

            # If user requested that new structured reports be published to a channel,
            # publish the instance path of the Structured Report
            structured_report_path = dicom_path.FromPath(
                self._dicom_store_path,
                study_uid=input_path.study_uid,
                series_uid=sr_series_uid,
                instance_uid=sr_instance_uid)
            self._PublishInferenceResultsReady(str(structured_report_path))
            _logger.info('Published structured report with path: %s',
                         structured_report_path)
        # Ack the message (successful or invalid message).
        message.ack()
        self._success_count += 1
import json
from dicomweb_client.api import DICOMwebClient


studies_file = 'server-studies.json'


client = DICOMwebClient(
    url="https://cidia-pacs.ufrgs.dev",
    qido_url_prefix="/dicom-web",
    wado_url_prefix="/dicom-web",
    #     wado_url_prefix="pacs/wado",
)


studies = client.search_for_studies()
# studies = client.search_for_studies(search_filters={'PatientID': '*11'})

print(len(studies), "studies found")
print()


studies_dict = {}


for study in studies:
    try:
        patient_id = str(study["00100020"]["Value"][0])
        study_uid = str(study["0020000D"]["Value"][0])

        studies_dict[patient_id] = {