De-identification
How go-dicom removes identifying information from DICOM files: the PS3.15 Annex E profiles and what each one changes, per-attribute overrides and templates, UID remapping, recording the method, and text burned into the pixels. It is for anyone preparing data to leave the place it was made, and it says plainly what is not removed.
Overview
anonymize.Anonymizer applies the Basic Application Level Confidentiality
Profile of PS3.15 Table E.1-1, with at most one option on top. The pixels are a
separate step: an attribute profile does nothing for a name drawn into the
image.
Anonymize changes the data set in place; clone it first if you still need
the original.
De-identify a data set
a := anonymize.NewAnonymizer(anonymize.BasicProfile)
for _, df := range study { // one Anonymizer for the whole study
ds := df.GetDataset()
if err := a.Anonymize(ds); err != nil {
return err
}
a.AnonymizeFileMeta(df.FileMetaInfo) // the meta's SOP Instance UID gets the same new UID
// ... write ds with df.FileMetaInfo, as in Reading and writing
}What Anonymize does, in order:
- With
SetRemoveJPEGText(true), removes text segments from JPEG frames (below). - Walks every element of the data set, into every sequence item to a depth of 64, and applies the action Table E.1-1 gives it. A sequence marked for removal is removed; one marked to be emptied loses its items.
- Curve and overlay data, which the table names by a range of groups, are removed: every element of a curve group (50xx), and Overlay Data and Overlay Comments (60xx,3000 and 60xx,4000).
- Removes every private element, except under the Retain Safe Private option.
- Records the method: Patient Identity Removed (0012,0062) YES, De-identification Method (0012,0063) and its code sequence (0012,0064).
An attribute the table does not name is kept. The profile is a floor; it does not discard data it says nothing about.
The actions, with the PS3.15 code each corresponds to:
| Action | Code | What it does here |
|---|---|---|
ActionRemove | X | Removes the element |
ActionEmpty | Z | Keeps the element with an empty value |
ActionDummy | D | A fixed value by VR: 19000101 for DA, 000000 for TM, 19000101000000 for DT, ANONYMOUS for PN, empty otherwise |
ActionReplace | ANONYMOUS for Patient's Name, a random 12-character ID for Patient ID, ANONYMIZED otherwise | |
ActionClean | C | Replaces the value with CLEANED |
ActionUID | U | A new 2.25. UID, the same for the same original within one Anonymizer |
ActionKeep | K | Leaves it |
On CT_small.dcm, for instance, Patient's Name, Patient ID, Patient's Sex,
Study Date and Institution Name are emptied, Series and Content Date become
19000101, Study Description and every private element are removed, and every
UID is replaced. Where the table gives a choice (X/Z/D and the like), the
library resolves it; BasicProfileTemplate() shows the action chosen for each
attribute.
UIDs
UIDs are remapped consistently within one Anonymizer, so a study stays one
study and references inside it still resolve. Use one Anonymizer for every
file of a study; a new one per file breaks the links. The file meta group is
not part of the data set: AnonymizeFileMeta gives it the same new SOP
Instance UID, and RemapUID does the same for a meta built by hand.
Caution
GetUIDMapping() returns every original UID with its replacement. It links
the output back to the patient. Keep it apart from the de-identified data,
or do not keep it. ResetUIDMapping() clears it between unrelated batches.
Profiles and options
NewAnonymizer takes one Profile: the Basic Profile alone, or the Basic
Profile with one option. Options are not combined. What each changes in this
version:
| Profile | CLI -profile | Changes from the Basic Profile |
|---|---|---|
BasicProfile | basic | none |
CleanDescriptorsProfile | clean-descriptors | Study Description and Series Description become CLEANED instead of being removed |
CleanGraphicsProfile | clean-graphics | records the Clean Graphics option (113103); the attribute actions are the Basic Profile's |
CleanStructuredContentProfile | clean-structured-content | empties Text Value, Person Name, DateTime, Date, Time and UID content items, and walks Content Sequence instead of emptying it |
RetainLongFullDatesProfile | retain-dates | keeps Study Date and Study Time; other dates and times are handled as in the Basic Profile |
RetainPatientCharsProfile | retain-patient-characteristics | keeps Patient's Sex, Age, Size and Weight |
RetainDeviceIdentProfile | retain-device-identity | keeps Manufacturer, Manufacturer's Model Name, Device Serial Number, Software Versions and Station Name |
RetainUIDsProfile | retain-uids | keeps every UID the Basic Profile would remap |
RetainSafePrivateProfile | retain-safe-private | keeps the private attributes PS3.15 Table E.3.10-1 lists as safe, looked up by creator; removes the rest |
Warning
The options do less than their PS3.15 columns. retain-dates keeps Study
Date and Time only, not every date the Retain Longitudinal Temporal
Information with Full Dates column keeps, and clean-graphics does not
remove graphic annotation; each still records its option code. Where a
column matters, read it with LookupProfileEntry and set the actions
yourself with SetCustomAction or a template.
Override attributes
SetCustomAction sets the action for one tag, ahead of the profile and its
option. SetRetainPatientName and SetRetainPatientID are shorthands:
a := anonymize.NewAnonymizer(anonymize.BasicProfile)
a.SetCustomAction(tag.New(0x0008, 0x0050), anonymize.ActionKeep) // Accession Number
a.SetCustomAction(tag.New(0x0008, 0x0021), anonymize.ActionKeep) // Series Date
a.SetCustomAction(tag.New(0x0029, 0x1010), anonymize.ActionRemove)
if err := a.Anonymize(ds); err != nil {
return err
}A custom action applies to the tag wherever it is, in sequence items too.
Templates
A Template is a named set of actions by tag, to keep and reuse a policy:
tmpl := anonymize.NewTemplate("research", "basic profile, series dates kept")
tmpl.SetAction(tag.New(0x0008, 0x0021), anonymize.ActionKeep) // Series Date
tmpl.SetAction(tag.New(0x0008, 0x0031), anonymize.ActionKeep) // Series Time
a := anonymize.NewAnonymizer(anonymize.BasicProfile)
a.ApplyTemplate(tmpl) // replaces any custom actions; SetTemplate adds to them
if err := a.Anonymize(ds); err != nil {
return err
}A template's actions become custom actions, so they win over the profile and
its option. BasicProfileTemplate() holds the action for every attribute of
Table E.1-1, and CleanDescriptorsTemplate(), RetainDatesTemplate() and
RetainDeviceTemplate() start from it. Merge combines two templates, the
other's actions winning; Clone copies one.
The standard's tables as data
Table E.1-1 and the safe private table are in the package, for a program that builds its own policy:
e, ok := anonymize.LookupProfileEntry(tag.New(0x0010, 0x0040)) // Patient's Sex
if ok {
fmt.Println(e.Name, e.Basic(), e.Code(anonymize.ColumnRetainPatientCharacteristics)) // Patient's Sex Z K
}
vr, safe := anonymize.LookupSafePrivate(0x0019, "GEMS_ACQU_01", 0x23)
fmt.Println(vr, safe, anonymize.ProfileTableEdition) // DS true 2026dProfileTable() returns every row and ProfileEntries() iterates them; codes
are as the standard writes them, X/Z/D unresolved. The Column… constants
name the option columns. SafePrivateEntries() iterates Table E.3.10-1, and
SafePrivateEdition names its edition.
Record the method
A program that de-identifies by its own rules records what it did as
Anonymize does:
err := anonymize.RecordDeidentificationMethod(ds,
[]anonymize.MethodCode{anonymize.MethodBasicProfile.Code(), anonymize.MethodCleanPixelData.Code()},
anonymize.MethodRecord{IdentityRemoved: true, BurnedInAnnotation: "NO"})
if err != nil {
return err
}The codes are PS3.16 CID 7050 (MethodBasicProfile 113100 to
MethodRetainInstitutionIdentity 113112). MethodRecord also sets
Longitudinal Temporal Information Modified when asked. A method recorded by an
earlier pass is replaced, so pass its codes again to keep them. (Anonymize
itself keeps the Clean Pixel Data code and the automatic-redaction note when
they are there.)
Burned-in text
Ultrasound, endoscopy, secondary captures and screen captures often have the patient's name drawn into the image. No attribute holds it, and no attribute profile removes it.
Is there a risk?
risk, reason := anonymize.BurnedInRisk(ds)
_, code := anonymize.BurnedInRiskReason(ds)
fmt.Println(risk, code, reason)
// true modality its modality is US (ultrasound), and Burned In Annotation (0028,0301) does not say there is noneBurned In Annotation (0028,0301) decides when present. Otherwise the SOP
class, Image Type and modality do. The answer is about the kind of image, not
this image: false means nothing suggests text, not that the pixels were
examined. The codes are BurnedInDeclared, BurnedInSecondaryCapture,
BurnedInSOPClass, BurnedInSlideLabel and BurnedInModality.
Fill regions you know
a := anonymize.NewAnonymizer(anonymize.BasicProfile)
if err := a.CleanPixelData(ds, dataset.PixelRegion{X: 0, Y: 0, Width: 320, Height: 56}); err != nil {
return err
}CleanPixelData fills each rectangle with black in every frame, sets Burned
In Annotation to NO and records the Clean Pixel Data option. Setting NO is a
claim that the image now holds no text, so the regions have to cover all of
it. A compressed image is decoded and stays native: nothing is coded again, so
no loss is added, and the file grows. A data set read with its pixels left in
the file (GetDatasetWithValuesInFile) is redacted frame by frame as it is
written.
Let a detector find it
regions, err := a.RedactDetectedText(ds, nil) // nil: anonymize.NewTextDetector()
if errors.Is(err, anonymize.ErrImplausibleRedaction) {
return err // what was found covers more of the picture than text does
}
if err != nil {
return err
}
fmt.Println(len(regions), "regions filled") // 10 for examples_rgb_color.dcmDetectBurnedInText finds the regions without filling them. The detector is
an interface (TextDetector), so a better one can be put in its place.
Warning
A detector misses text that is faint, very small, hand-written or drawn
over a busy part of the picture. RedactDetectedText therefore leaves Burned
In Annotation as it was and records "Burned-in text redacted where detected,
unverified" in De-identification Method. An image is clean when a person has
looked at it.
When the regions found cover more than a quarter of the picture
(MaxDetectedShare), nothing is filled and the error wraps
ErrImplausibleRedaction.
Text inside the JPEG stream
A JPEG frame can carry comment and application segments (Exif, XMP, notes) that
are neither attributes nor pixels. JPEGSegmentText(ds) lists them for JPEG
Baseline, Extended, Lossless and JPEG-LS data; RemoveJPEGText(ds) removes
them without decoding the image, so the samples are unchanged.
Anonymizer.SetRemoveJPEGText(true) does it inside Anonymize:
segments, err := anonymize.JPEGSegmentText(ds)
if err != nil {
return err // ErrJPEGSegmentsNotSearched for JPEG 2000 and other syntaxes
}
for _, s := range segments {
fmt.Printf("frame %d %s: %q\n", s.Frame, s.Name(), s.Text) // frame 0 APP0: "JFIF"
}The JFIF and Adobe headers and the JPEG-LS mrfx segment are kept, because
they decide how the samples are read; a JFIF thumbnail is dropped. JPEG 2000 is
not searched.
On the command line
$ go-dicom anonymize ct.dcm ct-anon.dcm
De-identified 1 file(s)
$ go-dicom anonymize -profile retain-dates -uid-map map.json study/ study-anon/
De-identified 2 file(s)
$ go-dicom show study-anon/ct.dcm::StudyDate
0008,0020 DA 8 20040119
$ go-dicom show study-anon/ct.dcm::SeriesDate
0008,0021 DA 8 19000101A directory is walked and written under the output at the same relative
paths; one run shares one UID mapping, so a study stays one study.
-uid-map writes the mapping as JSON, with the same caution as above. Unless
-redact is given, the pixels are copied from input to output as it is written
and never held in memory.
An image that may hold burned-in text is refused until you say where the text is, or that there is none:
$ go-dicom anonymize examples_rgb_color.dcm us-anon.dcm
Error: anonymize: examples_rgb_color.dcm: it may hold text burned into the pixels: its modality is US (ultrasound), and Burned In Annotation (0028,0301) does not say there is none. Give -redact x,y,width,height for where the text is, -redact auto to fill what a detector finds, or -allow-burned-in if there is none
$ go-dicom anonymize -detect examples_rgb_color.dcm
248,10,63,43
8,11,78,42
…
241,227,70,10
$ go-dicom anonymize -redact auto examples_rgb_color.dcm us-anon.dcm
examples_rgb_color.dcm: 10 region(s) detected and filled
De-identified 1 file(s)
$ go-dicom show us-anon.dcm::DeidentificationMethod
0012,0063 LO 92 Basic Application Confidentiality Profile\Burned-in text redacted where detected, unverified
$ go-dicom anonymize -redact 0,0,320,56 examples_rgb_color.dcm us-anon2.dcm
De-identified 1 file(s)
$ go-dicom show us-anon2.dcm::BurnedInAnnotation
0028,0301 CS 2 NOshare writes a PNG or JPEG instead of a DICOM file, with the same
-redact, -redact auto, -detect and -allow-burned-in, and -preview
to see what the detector would fill:
$ go-dicom share -preview examples_rgb_color.dcm check.png
Wrote check.png
The picture holds pixels only: no name, date or identifier from the file is in it.
This is a preview: 10 region(s) a detector found are outlined and nothing is redacted. Do not hand it on.
$ go-dicom share -redact 0,0,320,56 examples_rgb_color.dcm us.png
Wrote us.png
The picture holds pixels only: no name, date or identifier from the file is in it.
Nothing looked at the picture itself. Text drawn into it is still there unless you redacted it.Limits and gotchas
What is not removed:
- Text burned into the pixels, unless you redact it. The attribute profile never reads the pixels.
- Attributes Table E.1-1 does not name, which are kept.
- File names.
anonymizekeeps them, so a name that identifies the patient still does. - Overlay Data is removed, not inspected, and the other attributes of an overlay group are kept. An encapsulated image's JPEG segments stay unless you remove them.
- The SOP Instance UID in a file meta group you build yourself: call
AnonymizeFileMetaorRemapUID.
Also:
- One profile per
Anonymizer. Options are not combined, and some do less than their column in PS3.15 (see the warning above). - Custom actions and templates win over the option, so a template built
from
BasicProfileTemplate()given to a Retain UIDs anonymizer removes the UIDs again. - Non-DICOM files and DICOMDIRs are left out of an
anonymizedirectory run; the DICOMDIR is reported, not written.
See also
- Rendering:
shareand the picture it writes - Pixel data: what redacting a compressed image does to its syntax
- Examples: de-identify
- anonymize on pkg.go.dev
Rendering
How go-dicom turns a frame into a picture a screen can show (windowing, lookup tables, MONOCHROME1, color and ICC profiles), how to write it as a PNG or JPEG, and how the volume package puts the slices of a series in order. It is for anyone who needs to look at images, or hand them to something that is not DICOM.
Validation
How go-dicom checks an instance against what its SOP class requires: the IOD tables it uses, what it checks and what it reports as not checked, in Go and with go-dicom validate. It is for anyone who makes DICOM files, or receives them and wants to know whether they are complete.