@@ -1478,28 +1478,33 @@ def get_segment_numbers(
1478
1478
generated by an automatic algorithm from a segmentation object ``seg``:
1479
1479
1480
1480
>>> 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)
1483
1485
>>> 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
+ []
1488
1491
1489
1492
Get segment numbers of all segments identified by a given
1490
1493
institution-specific tracking ID:
1491
1494
1492
1495
>>> segment_numbers = seg.get_segment_numbers(
1493
- >>> tracking_id='Tumor #1234567'
1494
- >>> )
1495
- [13]
1496
+ ... tracking_id='Tumor #1234567'
1497
+ ... )
1498
+ >>> segment_numbers
1499
+ []
1496
1500
1497
1501
Get segment numbers of all segments identified a globally unique
1498
1502
tracking UID:
1499
1503
1500
1504
>>> uid = '1.2.826.0.1.3680043.10.511.3.73025483745501512180439199223117347'
1501
1505
>>> segment_numbers = seg.get_segment_numbers(tracking_uid=uid)
1502
- [5]
1506
+ >>> segment_numbers
1507
+ []
1503
1508
1504
1509
""" # noqa: E501
1505
1510
filter_funcs = []
@@ -1583,13 +1588,12 @@ def get_tracking_ids(
1583
1588
1584
1589
List the tracking IDs and UIDs present in the segmentation image:
1585
1590
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')]
1589
1593
1590
1594
>>> 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)
1593
1597
Bone
1594
1598
Spine
1595
1599
@@ -2086,7 +2090,7 @@ def get_pixels_by_source_instance(
2086
2090
List the source images for this segmentation:
2087
2091
2088
2092
>>> for study_uid, series_uid, sop_uid in seg.get_source_image_uids():
2089
- >>> print(sop_uid)
2093
+ ... print(sop_uid)
2090
2094
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93
2091
2095
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94
2092
2096
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(
2095
2099
Get the segmentation array for a subset of these images:
2096
2100
2097
2101
>>> 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
+ ... )
2103
2107
>>> pixels.shape
2104
2108
(2, 16, 16, 1)
2105
2109
@@ -2315,53 +2319,54 @@ def get_pixels_by_source_frame(
2315
2319
List the source image SOP instance UID for this segmentation:
2316
2320
2317
2321
>>> 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'
2319
2324
2320
2325
Get the segmentation array for 3 of the frames in the multiframe source
2321
2326
image. The resulting segmentation array has 3 10 x 10 frames, one for
2322
2327
each source frame. The final dimension contains the 20 different
2323
2328
segments present in this segmentation.
2324
2329
2325
2330
>>> 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
+ ... )
2329
2334
>>> pixels.shape
2330
2335
(3, 10, 10, 20)
2331
2336
2332
2337
This time, select only 4 of the 20 segments:
2333
2338
2334
2339
>>> 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
+ ... )
2339
2344
>>> pixels.shape
2340
2345
(3, 10, 10, 4)
2341
2346
2342
2347
Instead create a multiclass label map for each source frame. Note
2343
2348
that segments 6, 8, and 10 are present in the three chosen frames.
2344
2349
2345
2350
>>> 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
+ ... )
2350
2355
>>> pixels.shape, np.unique(pixels)
2351
- (3, 10, 10), array([0, 6, 8, 10])
2356
+ (( 3, 10, 10), array([ 0, 6, 8, 10]) )
2352
2357
2353
2358
Now relabel the segments to give a pixel map with values between 0
2354
2359
and 3 (inclusive):
2355
2360
2356
2361
>>> 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
+ ... )
2363
2368
>>> pixels.shape, np.unique(pixels)
2364
- (3, 10, 10), array([0, 1, 2, 3])
2369
+ (( 3, 10, 10), array([0, 1, 2, 3]) )
2365
2370
2366
2371
"""
2367
2372
# Check that indexing in this way is possible
@@ -2568,14 +2573,14 @@ def get_pixels_by_dimension_index_values(
2568
2573
>>> import highdicom as hd
2569
2574
>>> from pydicom.datadict import keyword_for_tag, tag_for_keyword
2570
2575
>>> from pydicom import dcmread
2571
- >>>
2576
+ ...
2572
2577
>>> ds = dcmread('data/test_files/seg_image_sm_control.dcm')
2573
2578
>>> seg = hd.seg.Segmentation.from_dataset(ds)
2574
2579
2575
2580
Get the default list of dimension index values
2576
2581
2577
2582
>>> for tag in seg.get_default_dimension_index_pointers():
2578
- >>> print(keyword_for_tag(tag))
2583
+ ... print(keyword_for_tag(tag))
2579
2584
ColumnPositionInTotalImagePixelMatrix
2580
2585
RowPositionInTotalImagePixelMatrix
2581
2586
XOffsetInSlideCoordinateSystem
@@ -2586,18 +2591,18 @@ def get_pixels_by_dimension_index_values(
2586
2591
Use a subset of these index pointers to index the image
2587
2592
2588
2593
>>> tags = [
2589
- >>> tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
2590
- >>> tag_for_keyword('RowPositionInTotalImagePixelMatrix')
2591
- >>> ]
2594
+ ... tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
2595
+ ... tag_for_keyword('RowPositionInTotalImagePixelMatrix')
2596
+ ... ]
2592
2597
>>> assert seg.are_dimension_indices_unique(tags) # True
2593
2598
2594
2599
It is therefore possible to index using just this subset of
2595
2600
dimension indices
2596
2601
2597
2602
>>> 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
+ ... )
2601
2606
>>> pixels.shape
2602
2607
(2, 10, 10, 20)
2603
2608
0 commit comments