Skip to main content

anonix

CI Python 3.10+ License: Apache-2.0 Typed: PEP 561

anonix is a standalone, policy-driven Python library and CLI for deterministic and statistical de-identification of structured data (JSON, XML, YAML, and CSV) entirely in memory.

It separates what is masked (a declarative MaskingPolicy in YAML or Python) from how documents are parsed, indexed, conflict-resolved, and transformed in RAM.


Design Principles

  1. The Policy Is the Single Source of Truth anonix never applies a hidden default policy, never invents fallback redactions, and contains no hardcoded role names.
    • Fields not matched by an active rule remain untouched by default (unmatched_fields: "preserve").
    • Incompatible values raise an explicit TechniqueExecutionError by default (on_error: "raise") unless your policy explicitly configures "preserve", "redact", or "nullify".
  2. Fail-Fast Load-Time Validation Unknown policy keys, unregistered techniques, misspelled technique parameters, unknown generalization hierarchies, and invalid format-specific selectors fail immediately with PolicyValidationError when the policy is loaded — before any payload is processed.
  3. Zero-Disk In-Memory Execution Payloads are parsed into a mutable AST in memory, transformed in place across a deterministic multi-phase pipeline, and serialized back to UTF-8 bytes without writing temporary files.
  4. Open Registry Architecture Custom techniques (@register_technique), format adapters (@register_adapter), and generalization hierarchies (@register_hierarchy) register cleanly from Python code, CLI --plugin files, or third-party packages via entry_points — without modifying core pipeline code.

Installation

Requires Python 3.10+.

# Install directly from GitHub
pip install "git+https://github.com/lovepop1/Datamasking-Core.git"

# Or from a local source checkout
pip install .

# Editable install with development tools (pytest, hypothesis, ruff, mypy)
pip install -e ".[dev]"

Runtime dependencies are minimal: pydantic, pyyaml, lxml, and jsonpath-ng.


Quickstart

1. Author a Policy (policy.yaml)

# yaml-language-server: $schema=./schema/policy.schema.json
version: "1.0"
name: "patient-intake-export"
format: "json"
unmatched_fields: "preserve"
on_error: "raise"
record_root: "$.patients[*]"

rules:
  - id: "remove-ssn"
    selector: "$.patients[*].ssn"
    technique: "suppress"

  - id: "tokenize-mrn"
    selector: "$.patients[*].patient_id"
    technique: "pseudonymize"
    params:
      prefix: "PT_"
      token_length: 12

  - id: "redact-name"
    selector: "$.patients[*].name"
    technique: "redact"
    params:
      replacement: "[REDACTED]"

  - id: "generalize-dob"
    selector: "$.patients[*].dob"
    technique: "generalize"
    params:
      hierarchy: "date"
      level: 2

k_anonymity:
  k: 2
  strategy: "enforce"
  quasi_identifiers:
    - selector: "$.zipcode"
      hierarchy: "zipcode"
    - selector: "$.age"
      hierarchy: "age"

2. Python SDK

from anonix import MaskingEngine

engine = MaskingEngine.from_file("policy.yaml", salt="your-256-bit-secret-key")

payload = {
    "study_id": "CARDIO-04",
    "patients": [
        {
            "patient_id": "MRN-101",
            "name": "Alice Carter",
            "ssn": "123-45-6789",
            "dob": "1984-06-19",
            "zipcode": "94107",
            "age": 41,
            "diagnosis": "I10",
        },
        {
            "patient_id": "MRN-102",
            "name": "Brian Brooks",
            "ssn": "987-65-4321",
            "dob": "1981-11-03",
            "zipcode": "94109",
            "age": 44,
            "diagnosis": "J45.909",
        },
    ],
}

result = engine.mask(payload)
print(result.output_text)
assert result.k_achieved is True

Output (study_id and diagnosis are untouched because the policy did not target them):

{
  "study_id": "CARDIO-04",
  "patients": [
    {
      "patient_id": "PT_6e4a8052ab9d",
      "name": "[REDACTED]",
      "dob": "1984",
      "zipcode": "9410*",
      "age": "40-44",
      "diagnosis": "I10"
    },
    {
      "patient_id": "PT_9f70f0d157ec",
      "name": "[REDACTED]",
      "dob": "1981",
      "zipcode": "9410*",
      "age": "40-44",
      "diagnosis": "J45.909"
    }
  ]
}

3. Command-Line Interface

# Validate a policy strictly before deployment
anonix validate -p examples/quickstart/policy.yaml

# Preview which rules would apply without masking values (--dry-run)
anonix mask -i examples/quickstart/patients.json -p examples/quickstart/policy.yaml --dry-run

# Mask a file (or pass '-i - -f json' to stream from stdin to stdout)
anonix mask \
  -i examples/quickstart/patients.json \
  -p examples/quickstart/policy.yaml \
  -s "$ANONIX_SALT" \
  -o masked_patients.json \
  --report audit_report.json

# Export Draft 2020-12 JSON Schema for IDE YAML validation
anonix schema -o schema/policy.schema.json

Built-in Capabilities

Category Built-in Implementations
Formats json (extended JSONPath + filter predicates), xml (XXE-hardened XPath for elements & @attributes), yaml (safe_load), csv (header-isolated row AST)
Techniques redact, suppress, nullify, pseudonymize (HMAC-SHA256), generalize, format_preserve, noise, mask_pattern, synthesize, deep_redact
Hierarchies date (day -> month -> year -> decade -> *), zipcode / zip (10001 -> 1000* -> ... -> *****), age (exact -> 5-yr -> 10-yr -> 20-yr -> *), icd10 (J45.909 -> J45 -> J** -> ***)
Privacy Controls Subtree scopes & reusable profiles, user-defined roles, deterministic conflict resolution, record-level k-anonymity (verify, enforce, suppress)

Adding a Custom Technique

from typing import Any
from pydantic import Field
from anonix import (
    BaseTechnique,
    FormatAdapter,
    MaskingContext,
    TechniqueParams,
    register_technique,
)


class DomainRedactParams(TechniqueParams):
    keep_tld: bool = Field(default=True)


@register_technique("redact_email_user")
class RedactEmailUserTechnique(BaseTechnique):
    params_model = DomainRedactParams

    def apply(
        self,
        adapter: FormatAdapter,
        node: Any,
        params: DomainRedactParams,
        context: MaskingContext,
    ) -> None:
        raw = adapter.get_value(node)
        if raw is None:
            return
        text = str(raw)
        if "@" not in text:
            context.handle_value_error(
                adapter, node, technique=self.name, reason="Value is not an email address."
            )
            return
        _, domain = text.rsplit("@", 1)
        tld = domain.rsplit(".", 1)[-1] if "." in domain else domain
        adapter.set_value(node, f"***@***.{tld}" if params.keep_tld else "***@***")

Save to a .py file and pass --plugin ./my_plugin.py on the CLI, import it in Python, or publish an independent plugin package via [project.entry-points."anonix.techniques"].


Documentation

  • Policy Specification & Execution Semantics — Complete field-by-field reference for policy.yaml, how phases 0–4 transform a document, conflict resolution scoring, scopes, roles, and k-anonymity.
  • Built-in Techniques & Hierarchies — Exact parameter schemas, types, defaults, null handling, error behavior, and examples for all 10 techniques and 4 hierarchies.
  • Selectors & Format Adapters — JSONPath, XPath, YAML, and CSV addressing rules, filter predicates, XML attributes/namespaces, and edge cases.
  • Extending anonix — Authoring custom techniques, format adapters, hierarchies, and installable entry_points plugins.
  • Architecture & Memory Model — Multi-phase AST compiler design, complexity, concurrency, and gateway integration boundaries.
  • Security & Privacy Model — Threat boundaries, HMAC key management, k-anonymity caveats, parser hardening, and Python memory lifecycle notes.

License

Apache License 2.0. See LICENSE.

Metadata

Release files for anonix 0.1.0

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

Source distribution (sdist)

Source distribution for anonix 0.1.0
File Size Uploaded Provenance Actions
anonix-0.1.0.tar.gz 88.5 kB Without provenance Details for anonix-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 160.9 kB

Release files / anonix-0.1.0.tar.gz

Download URL anonix-0.1.0.tar.gz
Size 88.5 kB
Tags Source
SHA-256 checksum
How to use checksums
49f7d654869bf96859c3f9f8998cbb1f7dd13c894706affa6116fa23c5c19618
BLAKE2b-256 checksum
How to use checksums
03b2fee6adf845b099a9f80db341c928cbfbb4b6b2c642a2f94ed639181530e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / anonix-0.1.0-py3-none-any.whl

Download URL anonix-0.1.0-py3-none-any.whl
Size 72.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f95ce43929756bf094433e1a77116b552eae142ac9822f435b7ad0a48000f125
BLAKE2b-256 checksum
How to use checksums
d07aaa820ee324be4f5353a2e7155dbcfdd8116051584bf2a44b442b34fc3871
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Security for anonix 0.1.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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