Skip to Content
go-dicom(3)
Packages

matching

Decides whether a C-FIND query identifier matches a data set, and builds the answer a match is sent as, by the rules of PS3.4 C.2.2.2.

import "github.com/amrshadid/go-dicom/matching" · pkg.go.dev

When to use it

  • You write a C-FIND SCP (Query/Retrieve or Modality Worklist) over your own records and want them matched as the standard says.
  • You filter data sets in memory by DICOM query rules: wildcards, date ranges, UID lists, sequence keys.

The archive in dcmstore uses this package against its index, so a query is answered the same way by both.

Key types and functions

NameWhat it is for
Compile, IdentifierRead a query once; the result is safe for concurrent use.
Identifier.MatchDatasetMatch against a data set.
Identifier.Match, AttributesMatch against anything that can give values by tag, such as an index row.
ResponseThe answer for a match: the query's keys, filled from the candidate.
OptionsKeys to skip, keys matched literally, combined date and time, the answer's character set.
MatchValueMatch one value against one key value of a VR.
QueryLevel, LevelRead Query/Retrieve Level.
CheckHierarchy, Model, OperationCheck that an identifier fits the model's hierarchy for a C‑FIND, C‑MOVE or C‑GET.
Key, KeyKindThe keys of a compiled query and how each is matched.
WorklistDateTimeThe date and time pair Modality Worklist matches as one range.
StatusIdentifierDoesNotMatchSOPClass, StatusUnableToProcessStatuses to answer with when a query is refused.

Example

// Match a C-FIND identifier against a data set and build the answer.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/dataelem"
	"github.com/amrshadid/go-dicom/dataset"
	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
	"github.com/amrshadid/go-dicom/matching"
)

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)
	}
	candidate := df.GetDataset()

	query := dataset.NewDataset()
	keys := []struct {
		keyword string
		vr      dataelem.VR
		value   string
	}{
		{"PatientName", dataelem.PN, "compressed*"}, // wildcard; a PN matches in any case
		{"StudyDate", dataelem.DA, "2004-"},         // a range open at the end
		{"StudyInstanceUID", dataelem.UI, ""},       // universal: matches, and is returned
	}
	for _, k := range keys {
		if err := query.AddByKeyword(k.keyword, k.vr, k.value); err != nil {
			log.Fatal(err)
		}
	}

	id, err := matching.Compile(query, matching.Options{})
	if err != nil {
		log.Fatal(err)
	}
	if !id.MatchDataset(candidate) {
		fmt.Println("no match")
		return
	}
	answer, err := matching.Response(query, candidate, matching.Options{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(answer.GetStringByKeyword("PatientName"), answer.GetStringByKeyword("StudyDate"))
	fmt.Println(answer.GetStringByKeyword("StudyInstanceUID"))
}

On CT_small.dcm it prints CompressedSamples^CT1 20040119 and the study's UID.

Notes

  • Universal, single value, UID list, wildcard, range, sequence, empty value ("") and multiple value matching are all applied. A person name matches whatever its case; other VRs match as written.
  • The query and the candidate are each read in their own Specific Character Set. The answer is UTF-8 unless Options.AnswerCharacterSet names another.
  • A candidate without the attribute matches only universal, * and empty value keys. A range does not match a study with no date.
  • How the archive matches, and what it does not index, is in CONFORMANCE.md §2.7.

Tip

Build queries with Dataset.AddByKeyword and an explicit VR. SetStringByKeyword on an attribute the data set does not hold creates it with VR OB, and such a key matches nothing.

See also

dcmstore · network · dataset · Query and retrieve · Workflow services

On this page