Skip to content

Instantly share code, notes, and snippets.

@manthey
Created December 1, 2022 16:22
Show Gist options
  • Save manthey/75d18e6c7373b5a23480a54d1c61d0ab to your computer and use it in GitHub Desktop.
Save manthey/75d18e6c7373b5a23480a54d1c61d0ab to your computer and use it in GitHub Desktop.
This tests reading dicom wsi using the dicomslide library. For files generated with wsi2dcm, this augments and modifies some data to conform to what dicomslide expects. If invalid UIDs are present, it will still fail.
import logging
import sys
import dicomslide
import dicomweb_client
import pydicom
dslog = logging.getLogger('dicomslide')
# dslog.addHandler(logging.StreamHandler(sys.stderr))
# dslog.setLevel(logging.WARN)
root = '/mnt/data2/dicom/sample3' if len(sys.argv) < 2 else sys.argv[1]
c = dicomweb_client.DICOMfileClient(
url='file://' + root,
in_memory=True, recreate_db=True)
c_retrieve_instance_metadata = c.retrieve_instance_metadata
def ditag(key):
return '%08X' % pydicom.datadict.tag_for_keyword(key)
defaultTags = {
ditag('ContainerIdentifier'): {'vr': 'LO', 'Value': ['1']},
ditag('OpticalPathSequence'): {
'vr': 'SQ', 'Value': [{
'00220016': {'vr': 'SQ', 'Value': [{
'00080100': {'vr': 'SH', 'Value': ['111744']},
'00080102': {'vr': 'SH', 'Value': ['DCM']},
'00080104': {'vr': 'LO', 'Value': ['Brightfield illumination']}}]},
'00480106': {'vr': 'SH', 'Value': ['1']},
'00480108': {'vr': 'SQ', 'Value': [{
'00080100': {'vr': 'SH', 'Value': ['414298005']},
'00080102': {'vr': 'SH', 'Value': ['SCT']},
'00080104': {'vr': 'LO', 'Value': ['Full spectrum']}}]},
}]},
ditag('TotalPixelMatrixOriginSequence'): {
'vr': 'SQ', 'Value': [{
'0040072A': {'vr': 'DS', 'Value': [0.0]},
'0040073A': {'vr': 'DS', 'Value': [0.0]}
}]},
ditag('TotalPixelMatrixFocalPlanes'): {'vr': 'UL', 'Value': [1]},
}
def retrieve_instance_metadata(*args, **kwargs):
metadata = c_retrieve_instance_metadata(*args, **kwargs)
# print('>', metadata)
anySet = False
for tag, value in defaultTags.items():
if tag not in metadata:
metadata[tag] = value
anySet = True
if anySet:
metadata[ditag('FrameOfReferenceUID')]['Value'][0] = metadata[
ditag('FrameOfReferenceUID')]['Value'][0].rsplit('.', 1)[0]
return metadata
c.retrieve_instance_metadata = retrieve_instance_metadata
try:
for idx, slide in enumerate(dicomslide.find_slides(c)):
print('-- Slide %d (%s):' % (idx, root))
print(slide)
print('Channels:', slide.num_channels)
print('Focal planes:', slide.num_focal_planes)
print('Levels:', slide.num_levels)
print('Dimensions:', slide.total_pixel_matrix_dimensions)
print('Downsampling:', slide.downsampling_factors)
print('Label images:', slide.label_images)
print('Volume image 0,0:', slide.get_volume_images(channel_index=0, focal_plane_index=0))
except Exception:
print('XX Exception of %s' % root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment