Skip to content

Commit

Permalink
examples updated to pass doctests, might need change ImagingDataCommo…
Browse files Browse the repository at this point in the history
  • Loading branch information
shishirpy committed Dec 12, 2021
1 parent 6ba2bbf commit ac675e8
Showing 1 changed file with 54 additions and 49 deletions.
103 changes: 54 additions & 49 deletions src/highdicom/seg/sop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,28 +1478,33 @@ def get_segment_numbers(
generated by an automatic algorithm from a segmentation object ``seg``:
>>> from pydicom.sr.codedict import codes
>>> from highdicom.seg import SegmentAlgorithmTypeValues
>>>
>>> from highdicom.seg import SegmentAlgorithmTypeValues, Segmentation
>>> from pydicom import dcmread
>>> ds = dcmread('data/test_files/seg_image_ct_true_fractional.dcm')
>>> seg = Segmentation.from_dataset(ds)
>>> segment_numbers = seg.get_segment_numbers(
>>> segmented_property_type=codes.SCT.Tumor,
>>> algorithm_type=SegmentAlgorithmTypeValues.AUTOMATIC
>>> )
[1, 2, 3]
... segmented_property_type=codes.SCT.Tumor,
... algorithm_type=SegmentAlgorithmTypeValues.AUTOMATIC
... )
>>> segment_numbers
[]
Get segment numbers of all segments identified by a given
institution-specific tracking ID:
>>> segment_numbers = seg.get_segment_numbers(
>>> tracking_id='Tumor #1234567'
>>> )
[13]
... tracking_id='Tumor #1234567'
... )
>>> segment_numbers
[]
Get segment numbers of all segments identified a globally unique
tracking UID:
>>> uid = '1.2.826.0.1.3680043.10.511.3.73025483745501512180439199223117347'
>>> segment_numbers = seg.get_segment_numbers(tracking_uid=uid)
[5]
>>> segment_numbers
[]
""" # noqa: E501
filter_funcs = []
Expand Down Expand Up @@ -1583,13 +1588,12 @@ def get_tracking_ids(
List the tracking IDs and UIDs present in the segmentation image:
>>> seg.get_tracking_ids()
[('Spine', '1.2.826.0.1.3680043.10.511.3.10042414969629429693880339016394772'),
('Bone', '1.2.826.0.1.3680043.10.511.3.83271046815894549094043330632275067')]
>>> sorted(seg.get_tracking_ids(), reverse=True) # otherwise its a random order
[('Spine', '1.2.826.0.1.3680043.10.511.3.10042414969629429693880339016394772'), ('Bone', '1.2.826.0.1.3680043.10.511.3.83271046815894549094043330632275067')]
>>> for seg_num in seg.segment_numbers:
>>> desc = seg.get_segment_description(seg_num)
>>> print(desc.segmented_property_type.meaning)
... desc = seg.get_segment_description(seg_num)
... print(desc.segmented_property_type.meaning)
Bone
Spine
Expand Down Expand Up @@ -2086,7 +2090,7 @@ def get_pixels_by_source_instance(
List the source images for this segmentation:
>>> for study_uid, series_uid, sop_uid in seg.get_source_image_uids():
>>> print(sop_uid)
... print(sop_uid)
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.95
Expand All @@ -2095,11 +2099,11 @@ def get_pixels_by_source_instance(
Get the segmentation array for a subset of these images:
>>> pixels = seg.get_pixels_by_source_instance(
>>> source_sop_instance_uids=[
>>> '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93',
>>> '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94'
>>> ]
>>> )
... source_sop_instance_uids=[
... '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93',
... '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94'
... ]
... )
>>> pixels.shape
(2, 16, 16, 1)
Expand Down Expand Up @@ -2315,53 +2319,54 @@ def get_pixels_by_source_frame(
List the source image SOP instance UID for this segmentation:
>>> sop_uid = seg.get_source_image_uids()[0][2]
'1.2.826.0.1.3680043.9.7433.3.12857516184849951143044513877282227'
# '1.2.826.0.1.3680043.9.7433.3.12857516184849951143044513877282227'
Get the segmentation array for 3 of the frames in the multiframe source
image. The resulting segmentation array has 3 10 x 10 frames, one for
each source frame. The final dimension contains the 20 different
segments present in this segmentation.
>>> pixels = seg.get_pixels_by_source_frame(
>>> source_sop_instance_uid=sop_uid,
>>> source_frame_numbers=[4, 5, 6]
>>> )
... source_sop_instance_uid=sop_uid,
... source_frame_numbers=[4, 5, 6]
... )
>>> pixels.shape
(3, 10, 10, 20)
This time, select only 4 of the 20 segments:
>>> pixels = seg.get_pixels_by_source_frame(
>>> source_sop_instance_uid=sop_uid,
>>> source_frame_numbers=[4, 5, 6],
>>> segment_numbers=[10, 11, 12, 13]
>>> )
... source_sop_instance_uid=sop_uid,
... source_frame_numbers=[4, 5, 6],
... segment_numbers=[10, 11, 12, 13]
... )
>>> pixels.shape
(3, 10, 10, 4)
Instead create a multiclass label map for each source frame. Note
that segments 6, 8, and 10 are present in the three chosen frames.
>>> pixels = seg.get_pixels_by_source_frame(
>>> source_sop_instance_uid=sop_uid,
>>> source_frame_numbers=[4, 5, 6],
>>> combine_segments=True
>>> )
... source_sop_instance_uid=sop_uid,
... source_frame_numbers=[4, 5, 6],
... combine_segments=True
... )
>>> pixels.shape, np.unique(pixels)
(3, 10, 10), array([0, 6, 8, 10])
((3, 10, 10), array([ 0, 6, 8, 10]))
Now relabel the segments to give a pixel map with values between 0
and 3 (inclusive):
>>> pixels = seg.get_pixels_by_source_frame(
>>> source_sop_instance_uid=sop_uid,
>>> source_frame_numbers=[4, 5, 6],
>>> segment_numbers=[6, 8, 10]
>>> combine_segments=True,
>>> relabel=True
>>> )
... source_sop_instance_uid=sop_uid,
... source_frame_numbers=[4, 5, 6],
... segment_numbers=[6, 8, 10],
... combine_segments=True,
... relabel=True
... )
>>> pixels.shape, np.unique(pixels)
(3, 10, 10), array([0, 1, 2, 3])
((3, 10, 10), array([0, 1, 2, 3]))
"""
# Check that indexing in this way is possible
Expand Down Expand Up @@ -2568,14 +2573,14 @@ def get_pixels_by_dimension_index_values(
>>> import highdicom as hd
>>> from pydicom.datadict import keyword_for_tag, tag_for_keyword
>>> from pydicom import dcmread
>>>
...
>>> ds = dcmread('data/test_files/seg_image_sm_control.dcm')
>>> seg = hd.seg.Segmentation.from_dataset(ds)
Get the default list of dimension index values
>>> for tag in seg.get_default_dimension_index_pointers():
>>> print(keyword_for_tag(tag))
... print(keyword_for_tag(tag))
ColumnPositionInTotalImagePixelMatrix
RowPositionInTotalImagePixelMatrix
XOffsetInSlideCoordinateSystem
Expand All @@ -2586,18 +2591,18 @@ def get_pixels_by_dimension_index_values(
Use a subset of these index pointers to index the image
>>> tags = [
>>> tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
>>> tag_for_keyword('RowPositionInTotalImagePixelMatrix')
>>> ]
... tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
... tag_for_keyword('RowPositionInTotalImagePixelMatrix')
... ]
>>> assert seg.are_dimension_indices_unique(tags) # True
It is therefore possible to index using just this subset of
dimension indices
>>> pixels = seg.get_pixels_by_dimension_index_values(
>>> dimension_index_pointers=tags,
>>> dimension_index_values=[[1, 1], [1, 2]]
>>> )
... dimension_index_pointers=tags,
... dimension_index_values=[[1, 1], [1, 2]]
... )
>>> pixels.shape
(2, 10, 10, 20)
Expand Down

0 comments on commit ac675e8

Please sign in to comment.