You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* added section on getting file URLs using idc-index
* updated all examples with the details on getting bucket file URLs using idc-index
* replaced direct use of tag numbers with keyword lookup
* updated importance of offset tables to clarify it is about SM
This notebook contains all of the code samples for convenient testing - I plan to
update this notebook and add it to IDC-Tutorials.
@@ -5,6 +5,25 @@ DICOM files in the IDC are stored as "blobs" on the cloud, with one copy housed
5
5
6
6
[Pydicom][2] is popular library for working with DICOM files in Python. Its [dcmread][3] function is able to accept any "file-like" object, meaning you can read a file straight from a cloud blob if you know its path. See [this page](../organization-of-data/files-and-metadata.md#storage-buckets) for information on finding the paths of the blobs for DICOM objects in IDC. The `dcmread` function also has some other options that allow you to control what is read. For example you can choose to read only the metadata and not the pixel data, or read only certain attributes. In the following two sections, we demonstrate these abilities using first Google Cloud Storage blobs and then AWS S3 blobs.
7
7
8
+
##### Mapping IDC DICOM series to bucket URLs
9
+
10
+
All of the image data available from IDC is replicated between public Google Cloud Storage (GCS) and AWS buckets. pip-installable [idc-index](https://github.com/imagingdatacommons/idc-index) package provides convenience functions to get URLs of the files corresponding to a given DICOM series.
11
+
12
+
```python
13
+
from idc_index import IDCClient
14
+
15
+
# create IDCClient() for looking up bucket URLs
16
+
idc_client = IDCClient()
17
+
18
+
# get the list of GCS file URLs in Google bucket from SeriesInstanceUID
The [official Python SDK for Google Cloud Storage][1] (installable from pip and PyPI as `google-cloud-storage`) provides a "file-like" interface allowing other Python libraries, such as Pydicom, to work with blobs as if they were "normal" files on the local filesystem.
@@ -15,15 +34,29 @@ To read from a GCS blob with Pydicom, first create a storage client and blob obj
15
34
from pydicom import dcmread
16
35
from google.cloud import storage
17
36
37
+
from pydicom.datadict import keyword_dict
38
+
39
+
from idc_index import IDCClient
40
+
41
+
# create IDCClient() for looking up bucket URLs
42
+
idc_client = IDCClient()
18
43
19
44
# Create a client and bucket object representing the IDC public data bucket
20
45
client = storage.Client.create_anonymous_client()
21
-
bucket = client.bucket("idc-open-data")
46
+
47
+
# get the list of file URLs in Google bucket from SeriesInstanceUID
Reading only metadata or only specific attributes will reduce the amount of data that needs to be pulled down under some circumstances and therefore make the loading process faster. This depends on the size of the attributes being retrieved, the `chunk_size` (a parameter of the `open()` method that controls how much data is pulled in each HTTP request to the server), and the position of the requested element within the file (since it is necessary to seek through the file until the requested attributes are found, but any data after the requested attributes need not be pulled).
@@ -48,17 +83,28 @@ This works because running the [open][4] method on a Blob object returns a [Blob
48
83
The `boto3` package provides a Python API for accessing S3 blobs. It can be installed with `pip install boto3`. In order to access open IDC data without providing AWS credentials, it is necessary to configure your own client object such that it does not require signing. This is demonstrated in the following example, which repeats the above example using the counterpart of the same blob on AWS S3. If you want to read an entire file, we recommend using a temporary buffer like this:
49
84
50
85
```python
51
-
52
86
from io import BytesIO
53
87
from pydicom import dcmread
54
88
55
89
import boto3
56
90
from botocore importUNSIGNED
57
91
from botocore.config import Config
58
92
93
+
from idc_index import IDCClient
94
+
95
+
# create IDCClient() for looking up bucket URLs
96
+
idc_client = IDCClient()
97
+
98
+
# get the list of file URLs in AWS bucket from SeriesInstanceUID
Unlike `google-cloud-storage`, `boto3` does not provide a file-like interface to access data in blobs. Instead, the `smart_open`[package][15] is a third-party package that wraps an S3 client to expose a "file-like" interface. It can be installed with `pip install 'smart_open[s3]'`. However, we have found that the buffering behavior of this package (which is intended for streaming) is not well matched to the use case of reading DICOM metadata, resulting in many unnecassary requests while reading the metadata of DICOM files (see [this](https://github.com/piskvorky/smart_open/issues/712) issue). Therefore while the following will work, we recommend using the approach in the above example (downloading the whole file) in most cases even if you only want to read the metadata as it will likely be much faster. The exception to this is when reading only the metadata of very large images where the total amount of pixel data dwarfs the amount of metadata (or using frame-level access to such images, see below).
@@ -83,12 +128,25 @@ from botocore import UNSIGNED
83
128
from botocore.config import Config
84
129
import smart_open
85
130
131
+
from idc_index import IDCClient
132
+
133
+
# create IDCClient() for looking up bucket URLs
134
+
idc_client = IDCClient()
135
+
136
+
# get the list of file URLs in AWS bucket from SeriesInstanceUID
# Open the blob with "segread" using the "lazy frame retrieval" option
@@ -182,13 +276,16 @@ with blob.open(mode="rb") as reader:
182
276
segment_numbers=selected_segment_numbers,
183
277
combine_segments=True,
184
278
)
279
+
280
+
# print dimensions of the liver segment volume
281
+
print(volume.shape)
185
282
```
186
283
187
284
See [this][11] page for more information on highdicom's `Image` class, and [this][12] page for the `Segmentation` class.
188
285
189
-
### The importance of offset tables
286
+
### The importance of offset tables for SM modality
190
287
191
-
Achieving good performance for these frame-level retrievals requires the presence of a "Basic Offset Table" or "Extended Offset Table" in the file. These tables specify the starting positions of each frame within the file's byte stream. Without an offset table being present, libraries such as highdicom have to parse through the pixel data to find markers that tell it where frame boundaries are, which involves pulling down significantly more data and is therefore very slow. This mostly eliminates the potential speed benefits of frame-level retrieval. Unfortunately there is no simple way to know whether a file has an offset table without downloading the pixel data and checking it. If you find that an image takes a long time to load initially, it is probably because highdicom is constucting the offset table itself because it wasn't included in the file.
288
+
Achieving good performance for the Slide Microscopy frame-level retrievals requires the presence of a "Basic Offset Table" or "Extended Offset Table" in the file. These tables specify the starting positions of each frame within the file's byte stream. Without an offset table being present, libraries such as highdicom have to parse through the pixel data to find markers that tell it where frame boundaries are, which involves pulling down significantly more data and is therefore very slow. This mostly eliminates the potential speed benefits of frame-level retrieval. Unfortunately there is no simple way to know whether a file has an offset table without downloading the pixel data and checking it. If you find that an image takes a long time to load initially, it is probably because highdicom is constucting the offset table itself because it wasn't included in the file.
192
289
193
290
Most IDC images do include an offset table, but some of the older pathology slide images do not. [This page][14] contains some notes about whether individual collections include offset tables.
0 commit comments