Packages
fileset
Reads and writes DICOMDIR files, the directory of a PS3.10 file-set on media, and keeps an in-memory list of the DICOM files under a directory.
import "github.com/amrshadid/go-dicom/fileset" · pkg.go.dev
When to use it
- Read a DICOMDIR from a CD, DVD or USB file-set and walk its patients,
studies, series and images:
ReadDICOMDIR. - Write a DICOMDIR for files you are putting on media:
Builder. - Scan a folder and find files by patient, study, series or modality:
FileSet.
The command line's dicomdir is built on it.
Key types and functions
| Name | What it is for |
|---|---|
ReadDICOMDIR, DICOMDIR | The directory records of a read DICOMDIR, as a tree. |
ReadDICOMDIRWithOptions, ReadOptions | The same, with a bound on the number of records. |
Record | One directory record: type, in-use flag, Referenced File ID, its data set and children. |
DICOMDIR.RecordsOfType, DICOMDIR.Walk | Find records of one type; visit every record. |
RecordPatient and the other Record… constants | Directory Record Type values. |
NewBuilder, Builder, DICOMDIROptions | Collect each instance's File ID and header, then write the DICOMDIR. Safe for several goroutines. |
ValidFileID | Whether File ID components meet PS3.10 8.5: one to eight components of one to eight characters from A-Z, 0-9 and underscore. |
NewFileSet, FileSet | Files under a root directory: scan, find, sort, generate a DICOMDIR. |
FileSet.Scan, SkippedFile | Add every DICOM file found, and say which were skipped and why. |
ErrTooManyRecords, ErrInvalidFileID | Errors to test for. |
Example
// List what a DICOMDIR says is on the media, patient by patient.
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/amrshadid/go-dicom/filebase"
"github.com/amrshadid/go-dicom/filereader"
"github.com/amrshadid/go-dicom/fileset"
)
func main() {
f, err := os.Open(os.Args[1]) // the DICOMDIR file
if err != nil {
log.Fatal(err)
}
defer f.Close()
df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
if err != nil {
log.Fatal(err)
}
dir, err := fileset.ReadDICOMDIR(df)
if err != nil {
log.Fatal(err)
}
fmt.Println("records:", len(dir.Records), "images:", len(dir.RecordsOfType(fileset.RecordImage)))
var show func(r *fileset.Record, depth int)
show = func(r *fileset.Record, depth int) {
line := strings.Repeat(" ", depth) + r.Type
if r.Type == fileset.RecordPatient {
line += " " + r.Dataset.GetStringByKeyword("PatientName")
}
if r.HasValidFileID() {
line += " " + filepath.Join(r.ReferencedFileID...)
}
if depth < 3 { // patient, study, series
fmt.Println(line)
}
for _, c := range r.Children {
show(c, depth+1)
}
}
for _, root := range dir.Roots {
show(root, 0)
}
}On pydicom's dicomdirtests/DICOMDIR it prints records: 52 images: 31 and
the patient, study and series tree with each record's file.
Notes
- A record not in use (a deleted entry) is kept in the tree with
InUsefalse, so you see what the file claims. MaxRecordsbounds the tree, not the parse: bound the size of a DICOMDIR you do not trust before reading it.ConvertFileSetToRawFormat,ConvertRawFormatToFileSetandExecuteFileSetHookbelong to the hooks machinery and are not needed to read or write a DICOMDIR.- What is written and how it is checked is in CONFORMANCE.md §8.6.
Tip
Join Record.ReferencedFileID with filepath.Join under the directory the
DICOMDIR is in. The components are kept apart because the separator belongs
to the media, not the file-set.