|
| 1 | +.. _remote: |
| 2 | + |
| 3 | +Reading from Remote Filesystems |
| 4 | +=============================== |
| 5 | + |
| 6 | +Functions like `dcmread` from pydicom and :meth:`highdicom.imread`, |
| 7 | +:meth:`highdicom.seg.segread`, :meth:`highdicom.sr.srread`, and |
| 8 | +:meth:`highdicom.ann.annread` from highdicom can read from any object that |
| 9 | +exposes a "file-like" interface. Many alternative and remote filesystems have |
| 10 | +python clients that expose such an interface, and therefore can be read from |
| 11 | +directly. |
| 12 | + |
| 13 | +One such example is blobs on Google Cloud Storage buckets when accessed using |
| 14 | +the official Python SDK (installed through the `google-cloud-storage` PyPI |
| 15 | +package). This is particularly relevant since this is the storage mechanism |
| 16 | +underlying the `Imaging Data Commons <IDC>`_ (IDC), a large repository of |
| 17 | +public DICOM images. |
| 18 | + |
| 19 | +Coupling this with :ref:`"lazy" frame retrieval <lazy>` option is especially |
| 20 | +powerful, allowing frames to be retrieved from the remote filesystem only as |
| 21 | +and when they are needed. This is particularly useful for large multiframe |
| 22 | +files such as those found in slide microscopy or multi-segment binary |
| 23 | +or fractional segmentations. |
| 24 | + |
| 25 | +In this first example, we use lazy frame retrieval to load only a specific |
| 26 | +spatial patch from a large whole slide image from the IDC. |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + import numpy as np |
| 31 | + import highdicom as hd |
| 32 | +
|
| 33 | + # Additional libraries (install these separately) |
| 34 | + import matplotlib.pyplot as plt |
| 35 | + from google.cloud import storage |
| 36 | +
|
| 37 | +
|
| 38 | + # Create a storage client and use it to access the IDC's public data package |
| 39 | + client = storage.Client() |
| 40 | + bucket = client.bucket("idc-open-data") |
| 41 | +
|
| 42 | + # This is the path (within the above bucket) to a whole slide image from the |
| 43 | + # IDC collection called "CCDI MCI" |
| 44 | + blob = bucket.blob( |
| 45 | + "763fe058-7d25-4ba7-9b29-fd3d6c41dc4b/210f0529-c767-4795-9acf-bad2f4877427.dcm" |
| 46 | + ) |
| 47 | +
|
| 48 | + # Read directly from the blob object using lazy frame retrieval |
| 49 | + im = hd.imread( |
| 50 | + blob.open(mode="rb"), |
| 51 | + lazy_frame_retrieval=True |
| 52 | + ) |
| 53 | +
|
| 54 | + # Grab an arbitrary region of tile full pixel matrix |
| 55 | + region = im.get_total_pixel_matrix( |
| 56 | + row_start=15000, |
| 57 | + row_end=15512, |
| 58 | + column_start=17000, |
| 59 | + column_end=17512, |
| 60 | + dtype=np.uint8 |
| 61 | + ) |
| 62 | +
|
| 63 | + # Show the region |
| 64 | + plt.imshow(region) |
| 65 | + plt.show() |
| 66 | +
|
| 67 | +.. figure:: images/slide_screenshot.png |
| 68 | + :width: 512px |
| 69 | + :alt: Image of retrieved slide region |
| 70 | + :align: center |
| 71 | + |
| 72 | + Figure produced by the above code snippet showing an arbitrary spatial |
| 73 | + region of a slide loaded directly from a Google Cloud bucket |
| 74 | + |
| 75 | +As a further example, we use lazy frame retrieval to load only a specific set |
| 76 | +of segments from a large multi-organ segmentation of a CT image in the IDC |
| 77 | +stored in binary format (meaning each segment is stored using a separate set of |
| 78 | +frames). See :ref:`seg` for more information on working with DICOM |
| 79 | +segmentations. |
| 80 | + |
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + import highdicom as hd |
| 84 | +
|
| 85 | + # Additional libraries (install these separately) |
| 86 | + from google.cloud import storage |
| 87 | +
|
| 88 | +
|
| 89 | + # Create a storage client and use it to access the IDC's public data package |
| 90 | + client = storage.Client() |
| 91 | + bucket = client.bucket("idc-open-data") |
| 92 | +
|
| 93 | + # This is the path (within the above bucket) to a segmentation of a CT series |
| 94 | + # from IDC collection called "CCDI MCI", containing a large number of |
| 95 | + # different organs |
| 96 | + blob = bucket.blob( |
| 97 | + "3f38511f-fd09-4e2f-89ba-bc0845fe0005/c8ea3be0-15d7-4a04-842d-00b183f53b56.dcm" |
| 98 | + ) |
| 99 | +
|
| 100 | + # Open the blob with "segread" using the "lazy frame retrieval" option |
| 101 | + seg = hd.seg.segread( |
| 102 | + blob.open(mode="rb"), |
| 103 | + lazy_frame_retrieval=True |
| 104 | + ) |
| 105 | +
|
| 106 | + # Find the segment number corresponding to the liver segment |
| 107 | + selected_segment_numbers = seg.get_segment_numbers(segment_label="Liver") |
| 108 | +
|
| 109 | + # Read in the selected segments lazily |
| 110 | + volume = seg.get_volume( |
| 111 | + segment_numbers=selected_segment_numbers, |
| 112 | + combine_segments=True, |
| 113 | + ) |
| 114 | +
|
| 115 | +This works because running the ``.open("rb")`` method on a Blob object returns |
| 116 | +a `BlobReader <blob_reader>`_ object, which has a "file-like" interface |
| 117 | +(specifically the ``seek``, ``read``, and ``tell`` methods). If you can provide |
| 118 | +examples for reading from storage provided by other cloud providers, please |
| 119 | +consider contributing them to this documentation. |
| 120 | + |
| 121 | +.. _IDC: https://portal.imaging.datacommons.cancer.gov/ |
| 122 | +.. _blob_reader: https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.fileio.BlobReader |
0 commit comments