Skip to content

Commit ac675e8

Browse files
committed
examples updated to pass doctests, might need change ImagingDataCommons#126
1 parent 6ba2bbf commit ac675e8

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

src/highdicom/seg/sop.py

+54-49
Original file line numberDiff line numberDiff line change
@@ -1478,28 +1478,33 @@ def get_segment_numbers(
14781478
generated by an automatic algorithm from a segmentation object ``seg``:
14791479
14801480
>>> from pydicom.sr.codedict import codes
1481-
>>> from highdicom.seg import SegmentAlgorithmTypeValues
1482-
>>>
1481+
>>> from highdicom.seg import SegmentAlgorithmTypeValues, Segmentation
1482+
>>> from pydicom import dcmread
1483+
>>> ds = dcmread('data/test_files/seg_image_ct_true_fractional.dcm')
1484+
>>> seg = Segmentation.from_dataset(ds)
14831485
>>> segment_numbers = seg.get_segment_numbers(
1484-
>>> segmented_property_type=codes.SCT.Tumor,
1485-
>>> algorithm_type=SegmentAlgorithmTypeValues.AUTOMATIC
1486-
>>> )
1487-
[1, 2, 3]
1486+
... segmented_property_type=codes.SCT.Tumor,
1487+
... algorithm_type=SegmentAlgorithmTypeValues.AUTOMATIC
1488+
... )
1489+
>>> segment_numbers
1490+
[]
14881491
14891492
Get segment numbers of all segments identified by a given
14901493
institution-specific tracking ID:
14911494
14921495
>>> segment_numbers = seg.get_segment_numbers(
1493-
>>> tracking_id='Tumor #1234567'
1494-
>>> )
1495-
[13]
1496+
... tracking_id='Tumor #1234567'
1497+
... )
1498+
>>> segment_numbers
1499+
[]
14961500
14971501
Get segment numbers of all segments identified a globally unique
14981502
tracking UID:
14991503
15001504
>>> uid = '1.2.826.0.1.3680043.10.511.3.73025483745501512180439199223117347'
15011505
>>> segment_numbers = seg.get_segment_numbers(tracking_uid=uid)
1502-
[5]
1506+
>>> segment_numbers
1507+
[]
15031508
15041509
""" # noqa: E501
15051510
filter_funcs = []
@@ -1583,13 +1588,12 @@ def get_tracking_ids(
15831588
15841589
List the tracking IDs and UIDs present in the segmentation image:
15851590
1586-
>>> seg.get_tracking_ids()
1587-
[('Spine', '1.2.826.0.1.3680043.10.511.3.10042414969629429693880339016394772'),
1588-
('Bone', '1.2.826.0.1.3680043.10.511.3.83271046815894549094043330632275067')]
1591+
>>> sorted(seg.get_tracking_ids(), reverse=True) # otherwise its a random order
1592+
[('Spine', '1.2.826.0.1.3680043.10.511.3.10042414969629429693880339016394772'), ('Bone', '1.2.826.0.1.3680043.10.511.3.83271046815894549094043330632275067')]
15891593
15901594
>>> for seg_num in seg.segment_numbers:
1591-
>>> desc = seg.get_segment_description(seg_num)
1592-
>>> print(desc.segmented_property_type.meaning)
1595+
... desc = seg.get_segment_description(seg_num)
1596+
... print(desc.segmented_property_type.meaning)
15931597
Bone
15941598
Spine
15951599
@@ -2086,7 +2090,7 @@ def get_pixels_by_source_instance(
20862090
List the source images for this segmentation:
20872091
20882092
>>> for study_uid, series_uid, sop_uid in seg.get_source_image_uids():
2089-
>>> print(sop_uid)
2093+
... print(sop_uid)
20902094
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93
20912095
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94
20922096
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.95
@@ -2095,11 +2099,11 @@ def get_pixels_by_source_instance(
20952099
Get the segmentation array for a subset of these images:
20962100
20972101
>>> pixels = seg.get_pixels_by_source_instance(
2098-
>>> source_sop_instance_uids=[
2099-
>>> '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93',
2100-
>>> '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94'
2101-
>>> ]
2102-
>>> )
2102+
... source_sop_instance_uids=[
2103+
... '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93',
2104+
... '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94'
2105+
... ]
2106+
... )
21032107
>>> pixels.shape
21042108
(2, 16, 16, 1)
21052109
@@ -2315,53 +2319,54 @@ def get_pixels_by_source_frame(
23152319
List the source image SOP instance UID for this segmentation:
23162320
23172321
>>> sop_uid = seg.get_source_image_uids()[0][2]
2318-
'1.2.826.0.1.3680043.9.7433.3.12857516184849951143044513877282227'
2322+
2323+
# '1.2.826.0.1.3680043.9.7433.3.12857516184849951143044513877282227'
23192324
23202325
Get the segmentation array for 3 of the frames in the multiframe source
23212326
image. The resulting segmentation array has 3 10 x 10 frames, one for
23222327
each source frame. The final dimension contains the 20 different
23232328
segments present in this segmentation.
23242329
23252330
>>> pixels = seg.get_pixels_by_source_frame(
2326-
>>> source_sop_instance_uid=sop_uid,
2327-
>>> source_frame_numbers=[4, 5, 6]
2328-
>>> )
2331+
... source_sop_instance_uid=sop_uid,
2332+
... source_frame_numbers=[4, 5, 6]
2333+
... )
23292334
>>> pixels.shape
23302335
(3, 10, 10, 20)
23312336
23322337
This time, select only 4 of the 20 segments:
23332338
23342339
>>> pixels = seg.get_pixels_by_source_frame(
2335-
>>> source_sop_instance_uid=sop_uid,
2336-
>>> source_frame_numbers=[4, 5, 6],
2337-
>>> segment_numbers=[10, 11, 12, 13]
2338-
>>> )
2340+
... source_sop_instance_uid=sop_uid,
2341+
... source_frame_numbers=[4, 5, 6],
2342+
... segment_numbers=[10, 11, 12, 13]
2343+
... )
23392344
>>> pixels.shape
23402345
(3, 10, 10, 4)
23412346
23422347
Instead create a multiclass label map for each source frame. Note
23432348
that segments 6, 8, and 10 are present in the three chosen frames.
23442349
23452350
>>> pixels = seg.get_pixels_by_source_frame(
2346-
>>> source_sop_instance_uid=sop_uid,
2347-
>>> source_frame_numbers=[4, 5, 6],
2348-
>>> combine_segments=True
2349-
>>> )
2351+
... source_sop_instance_uid=sop_uid,
2352+
... source_frame_numbers=[4, 5, 6],
2353+
... combine_segments=True
2354+
... )
23502355
>>> pixels.shape, np.unique(pixels)
2351-
(3, 10, 10), array([0, 6, 8, 10])
2356+
((3, 10, 10), array([ 0, 6, 8, 10]))
23522357
23532358
Now relabel the segments to give a pixel map with values between 0
23542359
and 3 (inclusive):
23552360
23562361
>>> pixels = seg.get_pixels_by_source_frame(
2357-
>>> source_sop_instance_uid=sop_uid,
2358-
>>> source_frame_numbers=[4, 5, 6],
2359-
>>> segment_numbers=[6, 8, 10]
2360-
>>> combine_segments=True,
2361-
>>> relabel=True
2362-
>>> )
2362+
... source_sop_instance_uid=sop_uid,
2363+
... source_frame_numbers=[4, 5, 6],
2364+
... segment_numbers=[6, 8, 10],
2365+
... combine_segments=True,
2366+
... relabel=True
2367+
... )
23632368
>>> pixels.shape, np.unique(pixels)
2364-
(3, 10, 10), array([0, 1, 2, 3])
2369+
((3, 10, 10), array([0, 1, 2, 3]))
23652370
23662371
"""
23672372
# Check that indexing in this way is possible
@@ -2568,14 +2573,14 @@ def get_pixels_by_dimension_index_values(
25682573
>>> import highdicom as hd
25692574
>>> from pydicom.datadict import keyword_for_tag, tag_for_keyword
25702575
>>> from pydicom import dcmread
2571-
>>>
2576+
...
25722577
>>> ds = dcmread('data/test_files/seg_image_sm_control.dcm')
25732578
>>> seg = hd.seg.Segmentation.from_dataset(ds)
25742579
25752580
Get the default list of dimension index values
25762581
25772582
>>> for tag in seg.get_default_dimension_index_pointers():
2578-
>>> print(keyword_for_tag(tag))
2583+
... print(keyword_for_tag(tag))
25792584
ColumnPositionInTotalImagePixelMatrix
25802585
RowPositionInTotalImagePixelMatrix
25812586
XOffsetInSlideCoordinateSystem
@@ -2586,18 +2591,18 @@ def get_pixels_by_dimension_index_values(
25862591
Use a subset of these index pointers to index the image
25872592
25882593
>>> tags = [
2589-
>>> tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
2590-
>>> tag_for_keyword('RowPositionInTotalImagePixelMatrix')
2591-
>>> ]
2594+
... tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
2595+
... tag_for_keyword('RowPositionInTotalImagePixelMatrix')
2596+
... ]
25922597
>>> assert seg.are_dimension_indices_unique(tags) # True
25932598
25942599
It is therefore possible to index using just this subset of
25952600
dimension indices
25962601
25972602
>>> pixels = seg.get_pixels_by_dimension_index_values(
2598-
>>> dimension_index_pointers=tags,
2599-
>>> dimension_index_values=[[1, 1], [1, 2]]
2600-
>>> )
2603+
... dimension_index_pointers=tags,
2604+
... dimension_index_values=[[1, 1], [1, 2]]
2605+
... )
26012606
>>> pixels.shape
26022607
(2, 10, 10, 20)
26032608

0 commit comments

Comments
 (0)