> For the complete documentation index, see [llms.txt](https://docs.rhinofcp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rhinofcp.com/rhino-sdk/calculating-sdk-metrics-on-dicom-studies-directly-via-a-dicomweb-connection.md).

# Calculating SDK metrics on DICOM studies directly via a DICOMWeb connection

You can use the `DicomwebQueryCreateInput` and `DicomwebQueryEndpoint` methods in the Rhino SDK to extract metrics directly from a Dicomweb server without extracting any image data or importing it into the Rhino client.

Below is some example code for how this workflow works:

### Import relevant libraries

```python
from rhino_health import RhinoSession
from rhino_health.lib.endpoints.dicomweb_query.dicomweb_query_endpoints import DicomwebQueryEndpoint
from rhino_health.lib.endpoints.dicomweb_query.dicomweb_query_dataclass import DicomwebQueryCreateInput
```

### Define Dicomweb connection parameters

```python
dicomweb_server_url="http://orthanc-external:4321/dicom-web/"
dicomweb_auth_credentials=dict(
    auth_type="basic_http",
    username="orthanc-user-external",
    password=getpass("DICOMweb Server Password:"),
)
```

### Define which studies to include in your query with `DicomwebQueryCreateInput`, then run your query with `DicomwebQueryEndpoint`

```python
create_query_input = DicomwebQueryCreateInput(
    project=project_uid,
    workgroup=workgroup_uid,
    dicomweb_server_url=dicomweb_server_url,
    dicomweb_auth_credentials=dicomweb_auth_credentials,
    dicomweb_query={"Study Instance UID": "1.2.156.14702.1.1000.16.0.19480514*"},
    dicom_object_level="Series",
    metric_definitions=[dict(\
        metric_name="count",\
        metric_params={},\
        request_arguments=dict(\
            variable="Modality",\
        ),\
    )],
)

query = DicomwebQueryEndpoint(session).run_query(create_query_input, timeout_seconds=30)

if query.results:
    for metric in query.results:
        print("Calculated Metric:", metric.calculated_metric)
        if metric.errors:
            print("Metric Calculation Errors:", metric.errors)
else:
    print("Errors:", query.errors)
```

**Example output:**

```
DICOMweb query status: Started
DICOMweb query status: Completed
Calculated Metric: {
    '': {'count': 250}
}
```

```python
create_query_input = DicomwebQueryCreateInput(
    project=project_uid,
    workgroup=workgroup_uid,
    dicomweb_server_url=dicomweb_server_url,
    dicomweb_auth_credentials=dicomweb_auth_credentials,
    dicomweb_query={},
    dicom_object_level="Instance",
    metric_definitions=[dict(\
        metric_name="count",\
        metric_params={},\
        request_arguments=dict(\
            variable="Modality",\
            group_by={"groupings": ["Modality", "PatientSex"]}\
        ),\
    )],
)

query = DicomwebQueryEndpoint(session).run_query(create_query_input, timeout_seconds=30)

if query.results:
    for metric in query.results:
        print("Calculated Metric:", metric.calculated_metric)
        if metric.errors:
            print("Metric Calculation Errors:", metric.errors)
else:
    print("Errors:", query.errors)
```

**Example output:**

```
DICOMweb query status: Started
DICOMweb query status: Completed
Calculated Metric: {
    "('Ultrasound', 'Male')": { 'count': 59 },
    "('Ultrasound', 'Female')": { 'count': 58 },
    "('Magnetic Resonance', 'Male')": { 'count': 57 },
    "('Computed Tomography', 'Male')": { 'count': 70 },
    "('Magnetic Resonance', 'Female')": { 'count': 61 },
    "('Computed Tomography', 'Female')": { 'count': 78 }
}
```

```python
create_query_input = DicomwebQueryCreateInput(
    project=project_uid,
    workgroup=workgroup_uid,
    dicomweb_server_url=dicomweb_server_url,
    dicomweb_auth_credentials=dicomweb_auth_credentials,
    dicomweb_query={},
    dicom_object_level="Series",
    metric_definitions=[\
        dict(\
            metric_name="mean",\
            metric_params={},\
            request_arguments=dict(\
                variable="PatientAge",\
                group_by={"groupings": ["PatientSex"]}\
            ),\
        ),\
        dict(\
            metric_name="std",\
            metric_params={},\
            request_arguments=dict(\
                variable="PatientAge",\
                group_by={"groupings": ["PatientSex"]}\
            ),\
        ),\
    ],
)

query = DicomwebQueryEndpoint(session).run_query(create_query_input, timeout_seconds=30)

if query.results:
    for metric in query.results:
        print("Calculated Metric:", metric.calculated_metric)
        if metric.errors:
            print("Metric Calculation Errors:", metric.errors)
else:
    print("Errors:", query.errors)
```

**Example output:**

```
DICOMweb query status: Started
DICOMweb query status: Completed
Calculated Metric: {
    'Male': {
        'mean': 46.65,
        'variable_count': 120
    },
    'Female': {
        'mean': 47.03076923076923,
        'variable_count': 130
    }
}
Calculated Metric: {
    'Male': {
        'std': 25.11130223624414,
        'variable_count': 120
    },
    'Female': {
        'std': 25.49447555890626,
        'variable_count': 130
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rhinofcp.com/rhino-sdk/calculating-sdk-metrics-on-dicom-studies-directly-via-a-dicomweb-connection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
