dataset
A data set in memory: the elements of one DICOM object, keyed by tag, with sequences of nested data sets, and the operations that work on the whole object: reading and changing attributes, decoding, rendering and compressing its pixels, transcoding, and the DICOM JSON and XML forms.
import "github.com/amrshadid/go-dicom/dataset" · pkg.go.dev
When to use it
- You have read a file with filereader and want its attributes
or its pixels:
df.GetDataset()returns a*dataset.Dataset. - You build an object in code to write with filewriter or send
with network or dicomweb. Every one of those
takes and returns a
*Dataset. - You want pixels as numbers (
PixelArrayInterpreted), as a picture (RenderFrame), decompressed (DecompressInPlace), compressed (Compress…InPlace,PixelEncoderFor) or in another transfer syntax (Transcode).
Use filereader/attributes instead when a program only lists attributes and should not link the pixel codecs. The element type itself is in dataelem; a sequence's container is sequence.
Key types and functions
| Name | What it is for |
|---|---|
| Attributes | |
Dataset | The elements of one object (or one sequence item), in insertion order, indexed by tag. |
NewDataset | An empty data set to fill in code. |
Dataset.GetStringByKeyword | A text value by keyword, padding trimmed; "" when absent. |
Dataset.Get | The element for a tag, and whether it is there. |
Dataset.Add, AddByKeyword | Add or replace an element with the VR you give. |
Dataset.SetStringByKeyword | Change the value of an element that is already there. |
Dataset.RemoveByKeyword, Remove | Delete an element. |
Dataset.GetSequence, AddSequence | Read or add a sequence; its items are *Dataset. |
Dataset.ForEach, IterAll | Visit the top-level elements, or every element at every depth. |
Dataset.Clone | A deep copy, sequences included. |
| Pixels | |
Dataset.GetPixelDataInfo | Rows, Columns, bits, samples, frames, rescale, as one struct. |
Dataset.PixelArrayInterpreted | Samples as [frame][row][column], signed when Pixel Representation says so. |
Dataset.RenderFrame, RenderOptions | One frame as an 8-bit image.Image, through the Modality, VOI and Presentation LUTs. |
Dataset.DecompressInPlace | Replace encapsulated Pixel Data with native samples, and fix the attributes that describe them. |
PixelEncoderFor, PixelEncoders | The coder for a target transfer syntax, and the list of syntaxes written here. |
Dataset.Transcode, TranscodeOptions | A copy in another transfer syntax; lossy only when the options allow it. |
FrameReader, Dataset.RenderFramesFrom | Render frames read one at a time from the file, without the Pixel Data in memory. |
| Other forms | |
Dataset.ToDICOMJSON, FromDICOMJSON | The DICOM JSON Model of PS3.18 Annex F. |
Dataset.ToDICOMXML, FromDICOMXML | The Native DICOM Model of PS3.19. |
Example
// Read attributes, walk a sequence, change a value and look at the pixels.
package main
import (
"fmt"
"log"
"os"
"github.com/amrshadid/go-dicom/dataset"
"github.com/amrshadid/go-dicom/filebase"
"github.com/amrshadid/go-dicom/filereader"
"github.com/amrshadid/go-dicom/tag"
)
func main() {
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer f.Close()
df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
if err != nil {
log.Fatal(err)
}
ds := df.GetDataset()
// Text by keyword; a missing attribute reads as "".
fmt.Println("Patient:", ds.GetStringByKeyword("PatientName"))
// By tag: the element, its VR and its value as bytes.
if elem, ok := ds.Get(tag.New(0x0028, 0x0010)); ok {
b, _ := elem.Bytes()
fmt.Println("Rows element:", elem.GetVR(), b)
}
// A sequence holds *dataset.Dataset items.
if seq, err := ds.GetSequence(ds.KeywordToTag("ProcedureCodeSequence")); err == nil && seq != nil {
for i := 0; i < seq.Length(); i++ {
item, _ := seq.Get(i)
fmt.Println("Procedure:", item.(*dataset.Dataset).GetStringByKeyword("CodeMeaning"))
}
}
if err := ds.SetStringByKeyword("PatientName", "Roe^Richard"); err != nil {
log.Fatal(err)
}
ds.RemoveByKeyword("PatientBirthDate")
info, err := ds.GetPixelDataInfo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%dx%d, %d bits, rescale %g/%g\n", info.Columns, info.Rows,
info.BitsAllocated, info.RescaleSlope, info.RescaleIntercept)
arr, err := ds.PixelArrayInterpreted() // [frame][row][column], decoded first if compressed
if err != nil {
log.Fatal(err)
}
if samples, ok := arr.([][][]int16); ok {
fmt.Println("First sample:", samples[0][0][0])
}
}Run on pydicom's CT_small.dcm it prints the patient name, Rows element: US [128 0], 128x128, 16 bits, rescale 1/-1024 and the first sample.
Notes
- Text values are held as the file's bytes with their padding; read them with
GetStringByKeyword, which trims it. Numbers stored in binary (US, SS, UL, FL, FD) are read withelem.Bytes()or through a type that knows them, such asGetPixelDataInfo. GetDatasetdecodes text to UTF-8, and the data set it builds then declaresISO_IR 192; see charset.- Methods take a lock, so concurrent calls do not corrupt the data set. A read followed by a write is two calls, not one transaction.
- Pixel decoding refuses rather than guesses: a syntax with no decoder is
ErrUndecodedSyntax, a frame whose header disagrees with Rows, Columns or Samples per Pixel is refused before it is decoded, and decoded sizes are bounded (compress.MaxDecompressedSize). What rendering does and does not apply is in CONFORMANCE.md §8.1. Transcodechanges no UID. A lossy copy that will be kept as an instance of its own needs a new SOP Instance UID from the caller.
Warning
SetValue, and so SetStringByKeyword and SetValueByKeyword, create an
element that is not yet in the data set with VR OB, not the dictionary's VR.
Use them to change an attribute that is there; add a new one with
AddByKeyword(keyword, vr, value) or Add(dataelem.NewDataElement(...)).
Note
The long "API Reference" section in the package's own documentation lists
some signatures that differ from the code (GetSequence returns an error, not
a bool; GetStringByKeyword returns only a string; there is no
GetStringValue). The function index on pkg.go.dev is the one to trust.
See also
dataelem · sequence · filereader · filewriter · compress · validate · Reading and writing · Pixel data · Rendering · Coming from pydicom