Skip to Content
go-dicom(3)

Structured reports

How go-dicom reads the content tree of a DICOM Structured Report, writes one back, and reads a Key Object Selection document, in Go and with go-dicom sr. It is for anyone who needs what a report says, not only that it is there.

Overview

A Structured Report (PS3.3 C.17) is a tree of content items. Each item has a value type (TEXT, NUM, CODE, CONTAINER and the rest), a concept name saying what it means, and a relationship to its parent (CONTAINS, HAS OBS CONTEXT, INFERRED FROM, …). The root item's attributes sit directly in the data set, and its children are in the Content Sequence (0040,A730).

Read the content tree

root, err := sr.ReadContentTree(ds)
if err != nil {
	return err
}
fmt.Println(root.Count(), "content items")
printTree(root, 0)

with a printer that picks the value field by value type:

func printTree(item *sr.ContentItem, depth int) {
	name := ""
	if item.ConceptName != nil {
		name = item.ConceptName.Meaning
	}
	value := ""
	switch item.ValueType {
	case sr.ValueTypeText:
		value = fmt.Sprintf("%q", item.Text)
	case sr.ValueTypeCode:
		value = item.Code.Meaning
	case sr.ValueTypeNum:
		value = item.Measurement.Value
		if item.Measurement.Units != nil {
			value += " " + item.Measurement.Units.Value
		}
	}
	if item.ReferencedContentItem != nil {
		value = fmt.Sprint("-> ", item.ReferencedContentItem)
	}
	fmt.Printf("%s%s %s %s %s\n", strings.Repeat("  ", depth), item.RelationshipType, item.ValueType, name, value)
	for _, child := range item.Children {
		printTree(child, depth+1)
	}
}

On pydicom's test-SR.dcm (trimmed):

29 content items
 CONTAINER Diagnosis
  HAS OBS CONTEXT UIDREF Some UID
  CONTAINS CONTAINER
    CONTAINS TEXT Text Code "A mass of"
      HAS CONCEPT MOD CODE Code Sample Code 1
      HAS CONCEPT MOD CODE Code Sample Code 2
    CONTAINS NUM Diameter 3 cm
…
    HAS PROPERTIES TCOORD TCoord Code
      SELECTED FROM   -> [1 3 2]
…

A ContentItem carries its value in the field for its value type, and only one is set:

Value typeField
TEXTText
CODECode (*sr.Code: Value, SchemeDesignator, SchemeVersion, Meaning)
NUMMeasurement (Value as written, Units)
DATE, TIME, DATETIMEDateTime, as written
UIDREFUID
PNAMEPersonName
IMAGE, WAVEFORM, COMPOSITEReference (SOPClassUID, SOPInstanceUID)
CONTAINERContinuityOfContent (SEPARATE or CONTINUOUS)
SCOORD, SCOORD3D, TCOORD and anything elseDataset, the item as stored

ConceptName and RelationshipType are on every item but the root, which has no relationship. Dataset holds every item as stored, for what the fields do not model: graphic data, observation date and time, frame numbers.

An item that points at another item instead of holding a value (a by-reference relationship, PS3.3 C.17.3.1) has ReferencedContentItem set: the target's position as one-based ordinals starting at the root, so [1 3 2] is the second child of the root's third child. Such an item has a relationship and nothing else.

Walk visits an item and everything below it, depth first; returning false skips what is below that item:

numbers := 0
root.Walk(func(item *sr.ContentItem) bool {
	if item.ValueType == sr.ValueTypeNum {
		numbers++
	}
	return true
})
fmt.Println(numbers, "measurements") // 2

Write a content tree

sr.WriteContentTree(ds, root) puts a tree into a data set: the root's attributes into the data set itself and its children into the Content Sequence. An item read from a document keeps what it was read with: what the fields do not model is written from its Dataset, and a value left unchanged is written as the element it was read from, so a code keeps its Coding Scheme UID and a reference its frame numbers. Only a changed value is written from the fields.

root.Children[1].Children[0].Text = "A lesion of" // the first TEXT under the second child
if err := sr.WriteContentTree(ds, root); err != nil {
	return err
}

Write the data set as any other (Reading and writing). Reports written this way pass dcmtk's dsrdump, which checks the relationship constraints of Basic Text, Enhanced and Comprehensive SR (CONFORMANCE.md §8.3). The tree is not checked against a template here; Validation checks the SR document modules, into the content tree to twelve levels.

Read a Key Object Selection

A Key Object Selection document (TID 2010) says why a set of instances was selected: for a conference, for teaching, or rejected for quality or patient safety reasons. ReadKeyObjectSelection reads what it says:

kos, err := sr.ReadKeyObjectSelection(ds, sr.KeyObjectSelectionOptions{MaxReferences: 10000})
if errors.Is(err, sr.ErrNotKeyObjectSelection) {
	return err // another SOP class
}
if err != nil {
	return err // ErrTooManyReferences, ErrMalformedKeyObjectSelection
}
fmt.Println(kos.Title.Meaning, sr.IsRejection(kos.Title), kos.Description)
for _, m := range kos.TitleModifiers {
	fmt.Println("modifier:", m.Meaning)
}
for _, ref := range kos.References {
	fmt.Println(ref.ValueType, ref.SOPInstanceUID, ref.FrameNumbers, ref.SegmentNumbers)
}
fmt.Println(len(kos.Evidence), "evidence instances")

On the test document in sr/testdata/kos_pydicom.dcm:

Rejected for Quality Reasons true Patient moved during the acquisition
modifier: Motion blur
IMAGE 1.2.826.0.1.3680043.10.511.77.10.1 [] []
IMAGE 1.2.826.0.1.3680043.10.511.77.10.2 [2 5] []
IMAGE 1.2.826.0.1.3680043.10.511.77.20.1 [] [1 3]
COMPOSITE 1.2.826.0.1.3680043.10.511.77.30.1 [] []
4 evidence instances
FieldWhat it holds
TitleThe root's concept name, a CID 7010 code; the KOSTitle… constants name them
TitleModifiersDocument Title Modifier items (113011): a rejection reason, a Best In Set scope
ProcedureCodesProcedure Code items (121023)
DescriptionKey Object Description (113012), or empty
ReferencesThe IMAGE, WAVEFORM and COMPOSITE items in document order, with frame and segment numbers
EvidenceCurrent Requested Procedure Evidence Sequence, one entry per instance
IdenticalDocumentsIdentical Documents Sequence, flattened the same way

sr.IsRejection(code) is true for the rejection titles (113001, 113037, 113038, 113039), and sr.IsRejectionWithdrawal(code) for Rejection Withdrawn (131360). Each of the three lists is bounded by MaxReferences (DefaultMaxKeyObjectReferences, 131072, when zero). Only the root's own items are read; the Image Library of a Manifest with Description is left to ReadContentTree.

On the command line

go-dicom sr prints the header and the tree, one item to a line: the relationship, the value type, the concept name with its code, and the value.

$ go-dicom sr test-SR.dcm
Comprehensive SR
Patient: Test^S R
Completion: COMPLETE  Verification: VERIFIED
Verifying observer: Riesmeier^Jörg, OFFIS e.V., 2001-02-13 18:47:46 "JR" [1705, 99_OFFIS_DCMTK]
Verifying observer: Observer^Verifying, Organisation, 2001-02-13 18:47:46
Predecessor document: Comprehensive SR [1.2.840.10008.5.1.4.1.1.88.33] 1.2.276.0.7230010.3.1.4.2139363186.7819.982086466.1 (series 1.2.276.0.7230010.3.1.4.2139363186.7819.982086466.3, study 1.2.276.0.7230010.3.1.4.2139363186.7819.982086466.2)
Content date/time: 2001-02-13 18:47:46
29 content item(s)

CONTAINER  "Diagnosis" [1111, TEST] = SEPARATE  @ 2001-02-13 18:47:46
  has obs context UIDREF  "Some UID" [1234.0, 99_OFFIS_DCMTK] = 1.2.3.4.5
  contains CONTAINER = CONTINUOUS
    contains TEXT  "Text Code" [1234, 99_OFFIS_DCMTK] = "A mass of"
…
    has properties SCOORD  "SCoord Code" [1234, 99_OFFIS_DCMTK] = CIRCLE 0/0 255/255
    has properties TCOORD  "TCoord Code" [1234, 99_OFFIS_DCMTK] = SEGMENT offsets 1.000000,2.500000
      selected from -> 1.3.2
…
  contains IMAGE = CT Image [1.2.840.10008.5.1.4.1.1.2] 1.2.3.4.5.0 frames 5,2 shown with Grayscale Softcopy Presentation State [1.2.840.10008.5.1.4.1.1.11.1] 1.2.3.5.6.7  @ 2001-02-13 18:47:46
…
$ go-dicom sr kos.dcm
Key Object Selection Document
Patient: Test^KOS
Content date/time: 2026-12-03 12:00:00
Template: TID 2010 (DCMR)
8 content item(s)

CONTAINER  "Rejected for Quality Reasons" [113001, DCM] = SEPARATE
  has concept mod CODE  "Document Title Modifier" [113011, DCM] = "Motion blur" [111210, DCM]
  has obs context CODE  "Observer Type" [121005, DCM] = "Person" [121006, DCM]
  contains TEXT  "Key Object Description" [113012, DCM] = "Patient moved during the acquisition"
  contains IMAGE = CT Image [1.2.840.10008.5.1.4.1.1.2] 1.2.826.0.1.3680043.10.511.77.10.1
  contains IMAGE = CT Image [1.2.840.10008.5.1.4.1.1.2] 1.2.826.0.1.3680043.10.511.77.10.2 frames 2,5
…

-> and dotted positions mark by-reference items, @ an observation time, # TID an item built from another template than the root's. --json prints the same as JSON with "schemaVersion": 1, each item with its position.

Limits and gotchas

  • Coordinates are not modeled. SCOORD, SCOORD3D and TCOORD values are in the item's Dataset; go-dicom sr prints them.
  • Templates are not enforced. Reading and writing do not check a tree against a TID; ReadKeyObjectSelection checks only the shape TID 2010 needs.
  • Identical documents are not printed by go-dicom sr, though KeyObjectSelection.IdenticalDocuments has them.
  • ReferencedContentItem is a position, not a pointer: change the tree's shape and the positions it names may no longer be right.
  • The StructuredReport type (sr.FromDataset, NewSRBuilder) is a separate, simpler model of findings and observations, not the content tree described here.

See also

On this page