Metadata-Version: 2.4
Name: pelvitect
Version: 0.1.0
Summary: Pediatric hip geometry engine for developmental dysplasia of the hip (DDH) analysis from pelvic X-rays
Project-URL: Homepage, https://github.com/ammomahdavikia/pelvitect
Project-URL: Documentation, https://github.com/ammomahdavikia/pelvitect#readme
Project-URL: Repository, https://github.com/ammomahdavikia/pelvitect
Project-URL: Bug Tracker, https://github.com/ammomahdavikia/pelvitect/issues
Project-URL: Pelviset DOI, https://doi.org/10.5281/zenodo.20615290
Project-URL: HF Seg Model, https://huggingface.co/amirmmahdavikia/pelvitect-seg
Project-URL: HF KP Model, https://huggingface.co/amirmmahdavikia/pelvitect-kp
Author-email: "Amir M. Mahdavikia" <mahdavikiaamirmohammad@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Amir M. Mahdavikia
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: DDH,computer vision,geometry,hip dysplasia,keypoint detection,medical imaging,orthopedics,pediatric,segmentation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Healthcare Industry
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: huggingface-hub>=0.22
Requires-Dist: matplotlib>=3.6
Requires-Dist: numpy>=1.24
Requires-Dist: opencv-python-headless>=4.7
Requires-Dist: pillow>=9.0
Requires-Dist: safetensors>=0.4
Requires-Dist: scipy>=1.10
Requires-Dist: torch>=2.0
Requires-Dist: torchvision>=0.15
Requires-Dist: transformers>=4.40
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: mypy>=1.8; extra == 'dev'
Requires-Dist: pandas>=1.5; extra == 'dev'
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Provides-Extra: no-models
Requires-Dist: matplotlib>=3.6; extra == 'no-models'
Requires-Dist: numpy>=1.24; extra == 'no-models'
Requires-Dist: opencv-python-headless>=4.7; extra == 'no-models'
Requires-Dist: pillow>=9.0; extra == 'no-models'
Requires-Dist: scipy>=1.10; extra == 'no-models'
Description-Content-Type: text/markdown

# Pelvitect

**Pediatric Hip Geometry Engine for Developmental Dysplasia of the Hip (DDH)**

[![PyPI](https://img.shields.io/pypi/v/pelvitect)](https://pypi.org/project/pelvitect/)
[![Python](https://img.shields.io/pypi/pyversions/pelvitect)](https://pypi.org/project/pelvitect/)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
[![PelviSet DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.20615290.svg)]

Pelvitect is an open-source Python library that measures and classifies developmental dysplasia of the hip (DDH) from anteroposterior pelvic radiographs. Given anatomical landmarks — provided manually or predicted by bundled SegFormer-B3 and ViTPose-Plus-Base models — it deterministically computes:

| Output | Description |
|--------|-------------|
| **Acetabular index (AI)** | Hilgenreiner line ∠ acetabular roof |
| **Center-edge angle (CE)** | Perkins line ∠ ASM → FHC |
| **Tönnis grade (I–IV)** | FHC quadrant position |
| **IHDI grade (I–IV)** | MOFM position relative to H, P, D-lines |
| **Hilgenreiner / Yamamuro / Smith distances** | Displacement metrics |
| **Shenton line continuity** | Arc gap in pixels |
| **Image quality control** | MTDDH symmetry standard (ORI, OAR, IRI, IAR) |

Pelvitect is the geometry engine behind **[PelviSet](https://doi.org/10.5281/zenodo.20615290)** — a unified dataset of 14,622 pediatric pelvic radiographs.

---

## Installation

```bash
pip install pelvitect
```

> **Python ≥ 3.9 required.**

---

## Quick start

### Full auto-inference from an image URL

```python
import numpy as np, requests
from PIL import Image
from io import BytesIO
from pelvitect import Pelvitect

url = "https://prod-images-static.radiopaedia.org/images/155111/24d626dbf763635f29e9f4efeacbb1_big_gallery.jpg"
img = np.array(Image.open(BytesIO(requests.get(url).content)).convert("RGB"))

# Downloads both models on first call (~380 MB total)
result = Pelvitect(image=img).analyze()
print(result.summary())
```

---

## Input modes

| Constructor call | What runs |
|-----------------|-----------|
| `Pelvitect(landmarks=lm)` | Pure geometry — no models |
| `Pelvitect(mask=mask)` | Mask-derived landmarks + Shenton + QC |
| `Pelvitect(mask=mask, landmarks=lm)` | Combined — KPs take priority over mask, mask adds Shenton + QC |
| `Pelvitect(image=img)` | Full auto: SegFormer-B3 → ViTPose → geometry |

---

## Output schema

`result.to_dict()` / `result.to_json()` return a structured dict:

```json
{
  "landmarks": {
    "tcc_r": {"x": 200.0, "y": 300.0, "source": "provided"},
    ...
  },
  "metrics": {
    "ai_l":     {"value": 28.4, "status": "computed", "unit": "°"},
    "h_dist_l": {"value": 45.0, "status": "computed", "unit": "px"},
    ...
  },
  "classifications": {
    "tonnis_l": {"grade": "I",  "status": "computed", "note": ""},
    "ihdi_l":   {"grade": "II", "status": "computed", "note": "MOFM below H-line …"},
    ...
  },
  "quality_control": {
    "status": "pass",
    "caution_msg": "",
    "criteria": [
      {"name": "ORI", "value": 1.02, "range": [0.78, 1.42], "status": "pass"},
      ...
    ]
  }
}
```

---

## Models

| Model | Architecture | Input | HuggingFace |
|-------|-------------|-------|------------|
| `pelvitect-seg` | SegFormer-B3 | 512×512 RGB | [`amirmmahdavikia/pelvitect-seg`](https://huggingface.co/amirmmahdavikia/pelvitect-seg) |
| `pelvitect-kp`  | ViTPose-Plus-Base (4-ch) | 256×192 ROI crop | [`amirmmahdavikia/pelvitect-kp`](https://huggingface.co/amirmmahdavikia/pelvitect-kp) |

Both models are trained on **[PelviSet](https://doi.org/10.5281/zenodo.20615290)** and download automatically from HuggingFace Hub on first use. Model weights are cached at `~/.cache/huggingface/hub/`.

---

## Coordinate conventions

- **Origin**: top-left of image; y increases **downward** (standard image convention).
- **Patient right** hip = image left (smaller x).
- **Landmark keys**: `{name}_{side}` where `l` = patient left, `r` = patient right.
- **Default coordinate space**: pixel. Pass `coord_space="normalized"` for [0, 1] inputs with `image_size=(w, h)`.

---

## Image quality control

Pelvitect automatically assesses radiograph quality when a segmentation mask is available, using the MTDDH symmetry standard (Qi et al., *Scientific Data* 2025):

| Metric | Definition | Accepted range |
|--------|-----------|----------------|
| ORI | Obturator rotation index | [0.78, 1.42] |
| OAR | Obturator area ratio | [0.77, 1.46] |
| IRI | Ilium rotation index | [0.71, 1.22] |
| IAR | Iliac area ratio | [0.71, 1.22] |

A violation produces a `WARNING` status and `caution_msg`:

```
"Image quality criterion failed (ORI). Measurements should be interpreted with caution."
```

The QC result is a non-blocking warning — all geometry measurements are still returned.

---

## Dataset

PelviSet is available at Zenodo and HuggingFace:

- **Zenodo**: [10.5281/zenodo.20615290](https://doi.org/10.5281/zenodo.20615290)
- **HuggingFace**: [`amirmmahdavikia/pelviset`](https://huggingface.co/datasets/amirmmahdavikia/pelviset)

14,622 cases · 4 sources · 3 institutions · 2 countries · 12-class segmentation · 8-point keypoints · geometry-derived clinical labels

---

## Citation

If you use Pelvitect in research, please cite:

```bibtex
@software{mahdavikia2026pelvitect,
  author    = {Mahdavikia, Amir M.},
  title     = {Pelvitect: Pediatric Hip Geometry Engine},
  year      = {2026},
  version = {0.1.0},
  doi     = {10.xxxx/zenodo.xxxxxxx},
  url     = {https://doi.org/10.xxxx/zenodo.xxxxxxx}
}
```

---

## License

MIT — see [LICENSE](LICENSE).
