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
| Name | What it is for |
|---|---|
NewAnonymizer, Anonymizer | One per study: the profile, overrides and UID map. |
Anonymizer.Anonymize | De-identify a data set in place and record the method in (0012,0062)–(0012,0064). |
Anonymizer.AnonymizeFileMeta | Give the file meta the same remapped SOP Instance UID. |
Profile, BasicProfile | The basic profile or one option on top of it. |
Action, Anonymizer.SetCustomAction | Remove, empty, replace, keep, clean, remap or dummy one tag. |
Template, RetainDatesTemplate, Anonymizer.ApplyTemplate | Actions and replacement values as data. |
Anonymizer.GetUIDMapping, Anonymizer.RemapUID | The map from old UIDs to new; one UID remapped by hand. |
BurnedInRisk | Whether an image is of a kind that often carries burned-in text, and why. |
NewTextDetector, TextDetector | Find lettering in a picture; supply your own detector through the interface. |
Anonymizer.RedactDetectedText, Anonymizer.CleanPixelData | Fill what was found, or the regions you name. |
RemoveJPEGText, Anonymizer.SetRemoveJPEGText | Remove COM and APPn text from JPEG frames without re-coding them. |
ProfileTable, LookupSafePrivate | PS3.15 Tables E.1-1 and E.3.10-1 as data. |
RecordDeidentificationMethod | Record 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
Anonymizechanges the data set. Clone it first if you still need the original.- A
Profileis one value: the basic profile, or the basic profile with one option. To combine options, start from a template and override tags. - Use one
Anonymizerfor 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 asfilewritercopies 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
Packages
The library's public packages. Each links its overview here and its full API on pkg.go.dev.
charset
Text in the character sets of DICOM (PS3.5 6.1, PS3.3 C.12.1.1.2): turning Specific Character Set (0008,0005) into encodings, decoding bytes to UTF-8 and encoding UTF-8 back, with ISO 2022 escape sequences, and person names with their three component groups.