Skip to main content

A framework for evaluating Presidio's Named Entity Recognition performance

Project description

Presidio-research

This package provides evaluation and data-science capabilities for Presidio and PII detection models in general.

It also includes a fake data generator that creates synthetic sentences based on templates and fake PII.

Who should use it?

  • Anyone interested in developing or evaluating PII detection models, an existing Presidio instance or a Presidio PII recognizer.
  • Anyone interested in generating new data based on previous datasets or sentence templates (e.g., to increase the coverage of entity values) for Named Entity Recognition models.

Getting started

Using notebooks

The easiest way to get started is by reviewing the notebooks.

  • Notebook 1: Shows how to use the PII data generator.
  • Notebook 2: Shows a simple analysis of the PII dataset.
  • Notebook 3: Provides tools to split the dataset into train/test/validation sets while avoiding leakage due to the same pattern appearing in multiple folds (only applicable for synthetically generated data).
  • Notebook 4: Shows how to use the evaluation tools to evaluate how well Presidio detects PII. Note that this is using the vanilla Presidio, and the results aren't very accurate.
  • Notebook 5: Shows how one can configure Presidio to detect PII much more accurately, and boost the f score in ~30%.
  • Notebook 6: Explains the entity mapping process, which is crucial when evaluating multiple models each returning a different set of entities.

Installation

From PyPI

pip install presidio-evaluator
python -m spacy download en_core_web_sm # for tokenization
python -m spacy download en_core_web_lg # for NER

From source

To install the package:

  1. Clone the repo
  2. Install all dependencies:
# Install uv if not already installed
pip install uv

# Install package + dev dependencies
uv sync --extra dev

# Download the spaCy pipeline used for tokenization
uv run python -m spacy download en_core_web_sm

# To use the default Presidio configuration, a spaCy model is required:
uv run python -m spacy download en_core_web_lg

# Verify installation
uv run pytest

Note that some dependencies (such as Flair and Stanza) are no longer supported. Use Presidio Analyzer directly to add custom NER models.

What's in this package?

  1. Fake data generator for PII recognizers and NER models
  2. Data representation layer for data generation, modeling and analysis
  3. Model/Recognizer evaluation for Presidio Analyzer and custom Presidio recognizers
  4. Helper functions for results analysis

1. Data generation

See Data Generator README for more details.

The data generation process takes a file with templates, e.g. My name is {{name}}. Then, it creates new synthetic sentences by sampling templates and PII values. Furthermore, it tokenizes the data, creates tags (either IO/BIO/BILUO) and spans for the newly created samples.

Once data is generated, it could be split into train/test/validation sets while ensuring that each template only exists in one set. See this notebook for more details.

2. Data representation

In order to standardize the process, we use specific data objects that hold all the information needed for generating, analyzing, modeling and evaluating data and models. Specifically, see data_objects.py.

The standardized structure, List[InputSample], can be translated into different formats:

  • CoNLL

    • To CoNLL:

      from presidio_evaluator import InputSample
      dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
      conll = InputSample.create_conll_dataset(dataset)
      conll.to_csv("dataset.csv", sep="\t")
      
    • From CoNLL

      from pathlib import Path
      from presidio_evaluator.dataset_formatters import CONLL2003Formatter
      # Read from a folder containing ConLL2003 files
      conll_formatter = CONLL2003Formatter(files_path=Path("data/conll2003").resolve())
      train_samples = conll_formatter.to_input_samples(fold="train")
      
  • spaCy v3

    from presidio_evaluator import InputSample
    dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
    InputSample.create_spacy_dataset(dataset, output_path="dataset.spacy")
    
  • json

    from presidio_evaluator import InputSample
    dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
    InputSample.to_json(dataset, output_file="dataset_json")
    

3. PII models evaluation

The presidio-evaluator framework allows you to evaluate Presidio as a system, a NER model, or a specific PII recognizer for precision, recall, and error analysis. See Notebook 5 for an example.

For more information

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Copyright notice:

Fake Name Generator identities by the Fake Name Generator are licensed under a Creative Commons Attribution-Share Alike 3.0 United States License. Fake Name Generator and the Fake Name Generator logo are trademarks of Corban Works, LLC.

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

presidio_evaluator-0.3.tar.gz (2.5 MB view details)

Uploaded Source

Built Distribution

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

presidio_evaluator-0.3-py3-none-any.whl (693.6 kB view details)

Uploaded Python 3

File details

Details for the file presidio_evaluator-0.3.tar.gz.

File metadata

  • Download URL: presidio_evaluator-0.3.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for presidio_evaluator-0.3.tar.gz
Algorithm Hash digest
SHA256 f9e55e86ebc6f36c0f698e467761b3249b0006d10e586063a5c342ccef828a92
MD5 ebf816ab6e5912797723e0a684f41169
BLAKE2b-256 aafb321e2cdecd77d6662e2364fc4737496ed2879f3f4edbecba0b13eb3c1024

See more details on using hashes here.

Provenance

The following attestation bundles were made for presidio_evaluator-0.3.tar.gz:

Publisher: publish.yml on data-privacy-stack/presidio-research

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file presidio_evaluator-0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for presidio_evaluator-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ce774c5dd7567925b0913697471bae56fd6af73b45e39dd15d64a6db649eb51b
MD5 e01e2cacdcd3f9dd998e8214382d55de
BLAKE2b-256 187bf793dfa08ac21adb30599281dbc251280c6cb44328d7bf376eaf2b814d08

See more details on using hashes here.

Provenance

The following attestation bundles were made for presidio_evaluator-0.3-py3-none-any.whl:

Publisher: publish.yml on data-privacy-stack/presidio-research

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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