@@ -1522,28 +1522,33 @@ def get_segment_numbers(
1522
1522
generated by an automatic algorithm from a segmentation object ``seg``:
1523
1523
1524
1524
>>> from pydicom.sr.codedict import codes
1525
- >>> from highdicom.seg import SegmentAlgorithmTypeValues
1526
- >>>
1525
+ >>> from highdicom.seg import SegmentAlgorithmTypeValues, Segmentation
1526
+ >>> from pydicom import dcmread
1527
+ >>> ds = dcmread('data/test_files/seg_image_sm_control.dcm')
1528
+ >>> seg = Segmentation.from_dataset(ds)
1527
1529
>>> segment_numbers = seg.get_segment_numbers(
1528
- >>> segmented_property_type=codes.SCT.Tumor,
1529
- >>> algorithm_type=SegmentAlgorithmTypeValues.AUTOMATIC
1530
- >>> )
1531
- [1, 2, 3]
1530
+ ... segmented_property_type=codes.SCT.ConnectiveTissue,
1531
+ ... algorithm_type=SegmentAlgorithmTypeValues.AUTOMATIC
1532
+ ... )
1533
+ >>> segment_numbers
1534
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
1532
1535
1533
1536
Get segment numbers of all segments identified by a given
1534
1537
institution-specific tracking ID:
1535
1538
1536
1539
>>> segment_numbers = seg.get_segment_numbers(
1537
- >>> tracking_id='Tumor #1234567'
1538
- >>> )
1539
- [13]
1540
+ ... tracking_id='Segment #4'
1541
+ ... )
1542
+ >>> segment_numbers
1543
+ [4]
1540
1544
1541
1545
Get segment numbers of all segments identified a globally unique
1542
1546
tracking UID:
1543
1547
1544
- >>> uid = '1.2.826.0.1.3680043.10.511.3.73025483745501512180439199223117347 '
1548
+ >>> uid = '1.2.826.0.1.3680043.8.498.42540123542017542395135803252098380233 '
1545
1549
>>> segment_numbers = seg.get_segment_numbers(tracking_uid=uid)
1546
- [5]
1550
+ >>> segment_numbers
1551
+ [13]
1547
1552
1548
1553
""" # noqa: E501
1549
1554
filter_funcs = []
@@ -1627,13 +1632,12 @@ def get_tracking_ids(
1627
1632
1628
1633
List the tracking IDs and UIDs present in the segmentation image:
1629
1634
1630
- >>> seg.get_tracking_ids()
1631
- [('Spine', '1.2.826.0.1.3680043.10.511.3.10042414969629429693880339016394772'),
1632
- ('Bone', '1.2.826.0.1.3680043.10.511.3.83271046815894549094043330632275067')]
1635
+ >>> sorted(seg.get_tracking_ids(), reverse=True) # otherwise its a random order
1636
+ [('Spine', '1.2.826.0.1.3680043.10.511.3.10042414969629429693880339016394772'), ('Bone', '1.2.826.0.1.3680043.10.511.3.83271046815894549094043330632275067')]
1633
1637
1634
1638
>>> for seg_num in seg.segment_numbers:
1635
- >>> desc = seg.get_segment_description(seg_num)
1636
- >>> print(desc.segmented_property_type.meaning)
1639
+ ... desc = seg.get_segment_description(seg_num)
1640
+ ... print(desc.segmented_property_type.meaning)
1637
1641
Bone
1638
1642
Spine
1639
1643
@@ -2130,7 +2134,7 @@ def get_pixels_by_source_instance(
2130
2134
List the source images for this segmentation:
2131
2135
2132
2136
>>> for study_uid, series_uid, sop_uid in seg.get_source_image_uids():
2133
- >>> print(sop_uid)
2137
+ ... print(sop_uid)
2134
2138
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93
2135
2139
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94
2136
2140
1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.95
@@ -2139,11 +2143,11 @@ def get_pixels_by_source_instance(
2139
2143
Get the segmentation array for a subset of these images:
2140
2144
2141
2145
>>> pixels = seg.get_pixels_by_source_instance(
2142
- >>> source_sop_instance_uids=[
2143
- >>> '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93',
2144
- >>> '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94'
2145
- >>> ]
2146
- >>> )
2146
+ ... source_sop_instance_uids=[
2147
+ ... '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.93',
2148
+ ... '1.3.6.1.4.1.5962.1.1.0.0.0.1196530851.28319.0.94'
2149
+ ... ]
2150
+ ... )
2147
2151
>>> pixels.shape
2148
2152
(2, 16, 16, 1)
2149
2153
@@ -2359,6 +2363,7 @@ def get_pixels_by_source_frame(
2359
2363
List the source image SOP instance UID for this segmentation:
2360
2364
2361
2365
>>> sop_uid = seg.get_source_image_uids()[0][2]
2366
+ >>> sop_uid
2362
2367
'1.2.826.0.1.3680043.9.7433.3.12857516184849951143044513877282227'
2363
2368
2364
2369
Get the segmentation array for 3 of the frames in the multiframe source
@@ -2367,45 +2372,45 @@ def get_pixels_by_source_frame(
2367
2372
segments present in this segmentation.
2368
2373
2369
2374
>>> pixels = seg.get_pixels_by_source_frame(
2370
- >>> source_sop_instance_uid=sop_uid,
2371
- >>> source_frame_numbers=[4, 5, 6]
2372
- >>> )
2375
+ ... source_sop_instance_uid=sop_uid,
2376
+ ... source_frame_numbers=[4, 5, 6]
2377
+ ... )
2373
2378
>>> pixels.shape
2374
2379
(3, 10, 10, 20)
2375
2380
2376
2381
This time, select only 4 of the 20 segments:
2377
2382
2378
2383
>>> pixels = seg.get_pixels_by_source_frame(
2379
- >>> source_sop_instance_uid=sop_uid,
2380
- >>> source_frame_numbers=[4, 5, 6],
2381
- >>> segment_numbers=[10, 11, 12, 13]
2382
- >>> )
2384
+ ... source_sop_instance_uid=sop_uid,
2385
+ ... source_frame_numbers=[4, 5, 6],
2386
+ ... segment_numbers=[10, 11, 12, 13]
2387
+ ... )
2383
2388
>>> pixels.shape
2384
2389
(3, 10, 10, 4)
2385
2390
2386
2391
Instead create a multiclass label map for each source frame. Note
2387
2392
that segments 6, 8, and 10 are present in the three chosen frames.
2388
2393
2389
2394
>>> pixels = seg.get_pixels_by_source_frame(
2390
- >>> source_sop_instance_uid=sop_uid,
2391
- >>> source_frame_numbers=[4, 5, 6],
2392
- >>> combine_segments=True
2393
- >>> )
2395
+ ... source_sop_instance_uid=sop_uid,
2396
+ ... source_frame_numbers=[4, 5, 6],
2397
+ ... combine_segments=True
2398
+ ... )
2394
2399
>>> pixels.shape, np.unique(pixels)
2395
- (3, 10, 10), array([0, 6, 8, 10])
2400
+ (( 3, 10, 10), array([ 0, 6, 8, 10]) )
2396
2401
2397
2402
Now relabel the segments to give a pixel map with values between 0
2398
2403
and 3 (inclusive):
2399
2404
2400
2405
>>> pixels = seg.get_pixels_by_source_frame(
2401
- >>> source_sop_instance_uid=sop_uid,
2402
- >>> source_frame_numbers=[4, 5, 6],
2403
- >>> segment_numbers=[6, 8, 10]
2404
- >>> combine_segments=True,
2405
- >>> relabel=True
2406
- >>> )
2406
+ ... source_sop_instance_uid=sop_uid,
2407
+ ... source_frame_numbers=[4, 5, 6],
2408
+ ... segment_numbers=[6, 8, 10],
2409
+ ... combine_segments=True,
2410
+ ... relabel=True
2411
+ ... )
2407
2412
>>> pixels.shape, np.unique(pixels)
2408
- (3, 10, 10), array([0, 1, 2, 3])
2413
+ (( 3, 10, 10), array([0, 1, 2, 3]) )
2409
2414
2410
2415
"""
2411
2416
# Check that indexing in this way is possible
@@ -2619,7 +2624,7 @@ def get_pixels_by_dimension_index_values(
2619
2624
Get the default list of dimension index values
2620
2625
2621
2626
>>> for tag in seg.get_default_dimension_index_pointers():
2622
- >>> print(keyword_for_tag(tag))
2627
+ ... print(keyword_for_tag(tag))
2623
2628
ColumnPositionInTotalImagePixelMatrix
2624
2629
RowPositionInTotalImagePixelMatrix
2625
2630
XOffsetInSlideCoordinateSystem
@@ -2630,18 +2635,18 @@ def get_pixels_by_dimension_index_values(
2630
2635
Use a subset of these index pointers to index the image
2631
2636
2632
2637
>>> tags = [
2633
- >>> tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
2634
- >>> tag_for_keyword('RowPositionInTotalImagePixelMatrix')
2635
- >>> ]
2638
+ ... tag_for_keyword('ColumnPositionInTotalImagePixelMatrix'),
2639
+ ... tag_for_keyword('RowPositionInTotalImagePixelMatrix')
2640
+ ... ]
2636
2641
>>> assert seg.are_dimension_indices_unique(tags) # True
2637
2642
2638
2643
It is therefore possible to index using just this subset of
2639
2644
dimension indices
2640
2645
2641
2646
>>> pixels = seg.get_pixels_by_dimension_index_values(
2642
- >>> dimension_index_pointers=tags,
2643
- >>> dimension_index_values=[[1, 1], [1, 2]]
2644
- >>> )
2647
+ ... dimension_index_pointers=tags,
2648
+ ... dimension_index_values=[[1, 1], [1, 2]]
2649
+ ... )
2645
2650
>>> pixels.shape
2646
2651
(2, 10, 10, 20)
2647
2652
0 commit comments