Metadata-Version: 2.4
Name: meddeid-core
Version: 0.2.4
Summary: Language-neutral schema, taxonomy, and span contracts for MedDeID
Author: Stig Hellemans
License-Expression: AGPL-3.0-only
Project-URL: Homepage, https://stighellemans.github.io/meddeid/
Project-URL: Documentation, https://stighellemans.github.io/meddeid/concepts/data-contract/
Project-URL: Source, https://github.com/stighellemans/meddeid-core
Project-URL: Issues, https://github.com/stighellemans/meddeid-core/issues
Project-URL: MedDeID on Hugging Face, https://huggingface.co/collections/stighellemans/meddeid
Keywords: de-identification,clinical-nlp,privacy,schema
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: rfc8785<0.2,>=0.1.4
Provides-Extra: models
Requires-Dist: pydantic>=2; extra == "models"
Provides-Extra: dev
Requires-Dist: pydantic>=2; extra == "dev"
Requires-Dist: pytest>=8; extra == "dev"
Dynamic: license-file

# meddeid-core

`meddeid-core` provides the language-neutral contracts shared by the MedDeID
suite: the document schema, 15-label data taxonomy, ordered 14-label model head,
record normalization, and character-offset validation.

The [suite documentation](https://stighellemans.github.io/meddeid/concepts/data-contract/)
explains how this contract moves through MedDeID workflows. This repository is
the authority for the schema, taxonomy, label order, and offset rules.

## Installation

```bash
pip install meddeid-core
```

Install the optional Pydantic models with:

```bash
pip install 'meddeid-core[models]'
```

## Python API

```python
from meddeid_core import BERT_ENTITY_LABELS, normalize_record, validate_record

record = normalize_record(record)
validate_record(record)
```

MedDeID uses half-open `[begin, end)` offsets measured in Unicode code points.
The only canonical top-level span container is `spans`.

The core package also owns the language-neutral
`meddeid.age-granularity.v1` JSON contract. `load_age_granularity_policy()`
loads the packaged default, a custom JSON path, or a mapping and returns its
validated calendar generalizer and deterministic identity. Language packages
remain responsible only for parsing and localized rendering.

Benchmark subannotations are nested under their parent primary span. A non-empty
subannotation list must be a complete, contiguous partition of the parent, and
all offsets remain absolute document offsets:

```json
{
  "begin": 0,
  "end": 11,
  "text": "Jan Peeters",
  "label": "Name:Patient",
  "category": "Name",
  "subtype": "Patient",
  "subannotations": [
    {"begin": 0, "end": 3, "text": "Jan", "category": "given"},
    {"begin": 3, "end": 4, "text": " ", "category": "formatting"},
    {"begin": 4, "end": 11, "text": "Peeters", "category": "family"}
  ]
}
```

## Name replacement contract

For HTTP inference, use `render.name_replacement` with a patient scope.
The gateway supplies the mapping key. A CPU job can pass the same scope and
key to `meddeid_core.rendering.apply_spans`. The canonical JSONL metadata
normalizer also accepts a strict Boolean `name_replacement` field.
[Rendering guide](https://stighellemans.github.io/meddeid/workflows/rendering/)

Core provides `meddeid.name-components.v2` in `meddeid_core.name_components`. A language pack supplies lookup lists and recognized titles. Each normalized name component has its own keyed alias, with a common keyed initial mapping. This keeps full names, middle names, initials and individual components consistent. The original v1 digest remains available for reproducing older exports.

`configuration()` requires a secret of at least 32 bytes and a nonempty study scope. `scoped_settings(settings, "patient", patient_id)` derives a patient-specific key within that study. Use `"study"` for deliberate cross-patient linkage. Keys stay in deployment configuration. Shared components remain linkable within a scope and finite aliases can collide.

## Normalize canonical JSONL

The normalizer standardizes fields within records that use the canonical
`document_id`, `text`, and `spans` containers. For example, it can convert
`start` to `begin`:

```bash
meddeid-normalize-jsonl input.jsonl normalized.jsonl
```

Other top-level span containers, including `annotations`, `entities`,
`deid_spans`, and `items`, are not part of the MedDeID schema and are rejected.

## Taxonomy contract

The Python taxonomy is authoritative. The generated language-neutral contract
is committed at `contracts/taxonomy.json` for JavaScript and non-Python
consumers. After an intentional taxonomy change, regenerate it with:

```bash
python scripts/export_contracts.py
```

Language-specific behavior belongs in packages such as
`meddeid-language-nl`; this package has no language rules or model runtime.

## Development

```bash
pip install -e '.[dev]'
pytest
```

## Licence

AGPL-3.0-only. See `NOTICE` for attribution information.

### Render previously detected spans on CPU

`meddeid_core.rendering.apply_spans(text, spans, ...)` renders offsets in the
original Unicode text. The default replaces detected identifiers with labels.
Names, clinical ages and event dates can be enabled independently. Install the
matching Dutch or English language package for locale-specific rendering.
`PseudonymizationScope`, `pseudonymize_name` and `derive_date_offset` use the
same mapping for free text and structured values. Treat the mapping key and the
derived offset as sensitive. Keep the key, language resources and algorithm
version fixed to reproduce an export.

## HTTP clients and data pipelines

Use `meddeid_core.client.GatewayClient` to call the inference service and `meddeid_core.rendering.apply_spans` to render further variants on CPU. For stored detections, `meddeid_core.pipeline.create_annotation_record` binds the response to your JSON source reference. On a later run pass the independently selected row reference and exact text to `meddeid_core.pipeline.read_spans`.
