Skip to main content

Collection of tools to check uploaded scans and records for identifiable data.

Project description

PHI-finder

CI/CD Codecov

Local testing (docker required)

conda create -n phi-finder python==3.11
conda activate phi-finder
pip install -e .[dev,test] --no-cache-dir
pytest .

Building

python -m pip install --upgrade build

python -m build

pip install dist/phi_finder-0.1.14-py3-none-any.whl

Basic usage (headers only)

import pydicom as dicom
from phi_finder.dicom_tools import anonymise_dicom

path = "/path/to/some/dicom.dcm"
dcm = dicom.dcmread(path)
anonymised_dcm = anonymise_dicom.anonymise_image(dcm)
anonymised_dcm.save_as('/path/to/some/dicom_anon.dcm')

More advanced usage

import pydicom as dicom
from presidio_image_redactor import (
    DicomImageRedactorEngine, ImageAnalyzerEngine, ContrastSegmentedImageEnhancer)
from phi_finder.dicom_tools import anonymise_dicom

path = "/path/to/some/dicom.dcm"
dcm = dicom.dcmread(path)
score_threshold=.15
analyser = anonymise_dicom._build_presidio_analyser(score_threshold, "en_core_web_lg")
image_redactor = DicomImageRedactorEngine(
    image_analyzer_engine=ImageAnalyzerEngine(
        analyzer_engine=analyser, 
        image_preprocessor=ContrastSegmentedImageEnhancer(),
        ))
anonymised_dcm = anonymise_dicom.anonymise_image(dcm,score_threshold=score_threshold,
                                                 analyser=analyser,
                                                 image_redactor=image_redactor,
                                                 )
anonymised_dcm.save_as('/path/to/some/dicom_anon.dcm')

De-identifying headers with the DICOM PS3.15 profile

By default anonymise_image scans the header values with the Presidio NER pipeline (and GLiNER, when supplied). Passing use_case="PS3.15" instead de-identifies the headers with the DICOM PS3.15 Annex E Basic Application Level Confidentiality Profile. This applies the standard's per-attribute actions (empty, dummy, remove, or remap UIDs), records the de-identification in DeidentificationMethod / DeidentificationMethodCodeSequence, and sets PatientIdentityRemoved to YES. In this mode the NER engines are not run on the headers, so you do not need to build an analyser.

import pydicom as dicom
from phi_finder.dicom_tools import anonymise_dicom

path = "/path/to/some/dicom.dcm"
dcm = dicom.dcmread(path)
anonymised_dcm = anonymise_dicom.anonymise_image(dcm, use_case="PS3.15")
anonymised_dcm.save_as('/path/to/some/dicom_anon.dcm')

Retain Patient Characteristics

Use use_case="PS3.15_Rtn. Pat." to apply the basic profile together with the PS3.15 Retain Patient Characteristics Option. Direct identifiers (patient name, birth date, etc.) are still removed, but patient characteristics such as age, sex, size, weight, ethnic group and smoking status are kept. The retain option is recorded in DeidentificationMethodCodeSequence (code 113108).

import pydicom as dicom
from phi_finder.dicom_tools import anonymise_dicom

path = "/path/to/some/dicom.dcm"
dcm = dicom.dcmread(path)
anonymised_dcm = anonymise_dicom.anonymise_image(dcm, use_case="PS3.15_Rtn. Pat.")
anonymised_dcm.save_as('/path/to/some/dicom_anon.dcm')

The use_case match is case-insensitive and tolerant of separator spelling, so "PS3.15", "ps3.15", "PS3_15" and "PS3-15" all select the plain profile, and "PS3.15_Rtn. Pat." or "PS3.15 Retain Patient Characteristics" select the retain variant. Any other value (e.g. "Standard", the default, or "Aggressive") falls back to the Presidio/GLiNER pipeline described above.

Note: use_case only controls how the headers are handled. Burned-in pixel PHI is still redacted only when an image_redactor is passed, exactly as in the examples above.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

phi_finder-0.1.14.tar.gz (66.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

phi_finder-0.1.14-py3-none-any.whl (66.7 kB view details)

Uploaded Python 3

File details

Details for the file phi_finder-0.1.14.tar.gz.

File metadata

  • Download URL: phi_finder-0.1.14.tar.gz
  • Upload date:
  • Size: 66.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for phi_finder-0.1.14.tar.gz
Algorithm Hash digest
SHA256 7e7a4d469604f40f246e26e7c4828dd0282fc531a8ab7d413a54f893a84d7ddc
MD5 451dd2c6665ed52a52a0edfa8e0528f6
BLAKE2b-256 9769d03f19d1ecbbf3858bda131dd4e870103b589db8099d58986347485c8f02

See more details on using hashes here.

File details

Details for the file phi_finder-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: phi_finder-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 66.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for phi_finder-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 de66f53feb270979207035b8860a42e750dde8c20d96909bfe55c97203e5223b
MD5 68e578df2f6ca55c6f4fa63b77e4b13e
BLAKE2b-256 c77cbe08d96639c35648c78a69cbf45801383f479b3733ed1beb64317057ef89

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page