Skip to main content

logo

Data anonymization package, supporting different anonymization strategies

Test Package version Supported Python versions Monthly package downloads Total package downloads


Documentation: https://eriknovak.github.io/anonipy

Source code: https://github.com/eriknovak/anonipy


The anonipy package is a python package for data anonymization. It is designed to be simple to use and highly customizable, supporting different anonymization strategies. Powered by LLMs.

Install

pip install anonipy

Upgrade

pip install anonipy --upgrade

Example

original_text = """\
Medical Record

Patient Name: John Doe
Date of Birth: 15-01-1985
Date of Examination: 20-05-2024
Social Security Number: 123-45-6789

Examination Procedure:
John Doe underwent a routine physical examination. The procedure included measuring vital signs (blood pressure, heart rate, temperature), a comprehensive blood panel, and a cardiovascular stress test. The patient also reported occasional headaches and dizziness, prompting a neurological assessment and an MRI scan to rule out any underlying issues.

Medication Prescribed:

Ibuprofen 200 mg: Take one tablet every 6-8 hours as needed for headache and pain relief.
Lisinopril 10 mg: Take one tablet daily to manage high blood pressure.
Next Examination Date:
15-11-2024
"""

Use the language detector to detect the language of the text:

from anonipy.utils.language_detector import LanguageDetector

language_detector = LanguageDetector()
language = language_detector(original_text)

Prepare the entity extractor and extract the personal infomation from the original text:

from anonipy.anonymize.extractors import NERExtractor

# define the labels to be extracted and anonymized
labels = [
    {"label": "name", "type": "string"},
    {"label": "social security number", "type": "custom"},
    {"label": "date of birth", "type": "date"},
    {"label": "date", "type": "date"},
]

# initialize the NER extractor for the language and labels
extractor = NERExtractor(labels, lang=language, score_th=0.5)

# extract the entities from the original text
doc, entities = extractor(original_text)

# display the entities in the original text
extractor.display(doc)

Use generators to create substitutes for the entities:

from anonipy.anonymize.generators import (
    LLMLabelGenerator,
    DateGenerator,
    NumberGenerator,
)

# initialize the generators
llm_generator = LLMLabelGenerator()
date_generator = DateGenerator()
number_generator = NumberGenerator()

# prepare the anonymization mapping
def anonymization_mapping(text, entity):
    if entity.type == "string":
        return llm_generator.generate(entity, temperature=0.7)
    if entity.label == "date":
        return date_generator.generate(entity, output_gen="MIDDLE_OF_THE_MONTH")
    if entity.label == "date of birth":
        return date_generator.generate(entity, output_gen="MIDDLE_OF_THE_YEAR")
    if entity.label == "social security number":
        return number_generator.generate(entity)
    return "[REDACTED]"

Anonymize the text using the anonymization mapping:

from anonipy.anonymize.strategies import PseudonymizationStrategy

# initialize the pseudonymization strategy
pseudo_strategy = PseudonymizationStrategy(mapping=anonymization_mapping)

# anonymize the original text
anonymized_text, replacements = pseudo_strategy.anonymize(original_text, entities)

Acknowledgements

Anonipy is developed by the Department for Artificial Intelligence at the Jozef Stefan Institute, and other contributors.

The project has received funding from the European Union's Horizon Europe research and innovation programme under Grant Agreement No 101080288 (PREPARE).

European

Metadata

Release files for anonipy 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for anonipy 0.6.1
File Size Uploaded Provenance
anonipy-0.6.1.tar.gz 44.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for anonipy 0.6.1
File Interpreter ABI Platform
anonipy-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 86.5 kB

Release files / anonipy-0.6.1.tar.gz

Download URL anonipy-0.6.1.tar.gz
Size 44.4 kB
Tags Source
SHA-256 checksum
How to use checksums
022bec07ddd190aad3f071de5188db184365fa5deff94d4bc5949d93a73adfec
BLAKE2b-256 checksum
How to use checksums
5fea76929a24925aabf8e2b6144a8f29be80ce15d6cc8c8f58fdf595af0641b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / anonipy-0.6.1-py3-none-any.whl

Download URL anonipy-0.6.1-py3-none-any.whl
Size 42.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cef784d9d23b9878f7bc7b6ef5976d9ce57669ce626975a5d57731ce7ccae744
BLAKE2b-256 checksum
How to use checksums
fe2740f313c729e8269f18caf279737c1806911aa3ba247a40637bafb4c748d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for anonipy 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page