Skip to Content
go-dicom(3)
Packages

anonymize

Removes identifying information from data sets by the Basic Application Level Confidentiality Profile of PS3.15 Annex E and its options, remaps UIDs consistently, and finds and fills text burned into the pixels.

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

When to use it

Before data leaves the place it was made: for research, teaching, a bug report or a vendor. The command line's anonymize and share are built on it.

How it fits together

The profile chooses the action for each attribute in Table E.1-1. A template holds actions and replacement values; custom actions override single tags. The UID map lives in the Anonymizer, so every file of a study gets the same new UIDs.

Key types and functions

NameWhat it is for
NewAnonymizer, AnonymizerOne per study: the profile, overrides and UID map.
Anonymizer.AnonymizeDe-identify a data set in place and record the method in (0012,0062)–(0012,0064).
Anonymizer.AnonymizeFileMetaGive the file meta the same remapped SOP Instance UID.
Profile, BasicProfileThe basic profile or one option on top of it.
Action, Anonymizer.SetCustomActionRemove, empty, replace, keep, clean, remap or dummy one tag.
Template, RetainDatesTemplate, Anonymizer.ApplyTemplateActions and replacement values as data.
Anonymizer.GetUIDMapping, Anonymizer.RemapUIDThe map from old UIDs to new; one UID remapped by hand.
BurnedInRiskWhether an image is of a kind that often carries burned-in text, and why.
NewTextDetector, TextDetectorFind lettering in a picture; supply your own detector through the interface.
Anonymizer.RedactDetectedText, Anonymizer.CleanPixelDataFill what was found, or the regions you name.
RemoveJPEGText, Anonymizer.SetRemoveJPEGTextRemove COM and APPn text from JPEG frames without re-coding them.
ProfileTable, LookupSafePrivatePS3.15 Tables E.1-1 and E.3.10-1 as data.
RecordDeidentificationMethodRecord the method for de-identification done by your own rules.

Example

// De-identify a file by the basic profile, keep one attribute by site policy,
// and give the file meta the same new UID.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/anonymize"
	"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()
	before := ds.GetStringByKeyword("SOPInstanceUID")

	a := anonymize.NewAnonymizer(anonymize.BasicProfile)             // one per study: it holds the UID map
	a.SetCustomAction(tag.New(0x0008, 0x0060), anonymize.ActionKeep) // Modality
	if err := a.Anonymize(ds); err != nil {
		log.Fatal(err)
	}
	a.AnonymizeFileMeta(df.FileMetaInfo)

	fmt.Printf("name %q, modality %q\n", ds.GetStringByKeyword("PatientName"), ds.GetStringByKeyword("Modality"))
	fmt.Println("identity removed:", ds.GetStringByKeyword("PatientIdentityRemoved"))
	fmt.Println("UID remapped:", a.GetUIDMapping()[before] == df.FileMetaInfo.MediaStorageSOPInstanceUID)

	if risk, reason := anonymize.BurnedInRisk(ds); risk {
		fmt.Println("check the pixels:", reason)
	}
}

On CT_small.dcm it prints an empty name, modality "CT" (kept by the custom action), identity removed: YES and UID remapped: true.

Notes

  • Anonymize changes the data set. Clone it first if you still need the original.
  • A Profile is one value: the basic profile, or the basic profile with one option. To combine options, start from a template and override tags.
  • Use one Anonymizer for every file of a study. A new one per file gives each file new, unrelated UIDs and breaks the study apart.
  • Pixel redaction works a frame at a time, so a data set read with filereader.ReadOptions{SkipBulkData: true} is redacted as filewriter copies it.
  • What validation checks of a de-identified file is in CONFORMANCE.md §8.7.

Important

GetUIDMapping is the link back to the original identities. Store it apart from the de-identified data, or throw it away.

Warning

Attribute rules do not reach text burned into the pixels. The detector can miss faint, small or handwritten text, and an automatic redaction is recorded as unverified, with Burned In Annotation not set to NO. Look at the images before they leave you. Private tags kept on purpose, and free text in reports, may still carry identity.

See also

dataset · filewriter · validate · De-identification · Examples · Coming from pydicom

On this page