pedigree-graph
Sparse-matrix pedigree relationship extraction and kinship computation.
Builds parent→child CSR adjacency matrices and extracts relationship
categories using sparse matrix algebra (A @ A.T for siblings,
A² @ A²ᵀ for cousins, etc.). Each relationship type is parameterised
by (up, down, n_ancestors):
up: meioses from the first member (the junior role: offspring, descendant, niece_nephew, junior_cousin) up to the common ancestor(s); collateral categories are storedup ≥ downdown: meioses from common ancestor(s) down to individual Bn_ancestors: 1 (half/lineal) or 2 (full, i.e. mated pair)kinship = n_ancestors × (1/2)^(up + down + 1)
Install
pip install pedigree-graph
Binary wheels cover manylinux x86-64 and AArch64, macOS x86-64 and Apple
Silicon, and Windows x86-64 (one abi3 wheel per platform for CPython >= 3.13).
Elsewhere pip builds the sdist, which needs a Rust toolchain (rustc >= 1.85).
For development the locked pixi environment carries Python, Rust, and maturin
and installs the package editable (the extension module is rebuilt with
pixi run maturin develop --release after a Rust change):
git clone https://github.com/rwaples/pedigree-graph.git
cd pedigree-graph
pixi install --locked
pixi run pytest -m "not slow" # inner loop, about 2.5 minutes
pixi run pytest # before a commit, about 13 minutes
pixi run cargo test --release # the Rust core
Five of the nine slow tests are the random_30k integration gates. Each runs
the pair-kinship kernel over the whole 30,300-row pedigree, and a single kernel
call there costs 30 to 250 seconds regardless of how many pairs it is asked for,
so the full suite is dominated by a handful of tests. The other four are the
N=2000 effective-size scaling tests in tests/test_effective_size_scaling.py,
which touch neither random_30k nor the pair-kinship kernel.
Requires Python ≥ 3.13. Runtime deps: numpy, scipy, numba; the
construction, relationship, topology, and error kernels are compiled Rust
(pedigree_graph._native, sources under crates/).
Pandas is optional and only needed if you pass DataFrames to the
constructors.
Usage
import numpy as np
from pedigree_graph import RELATIONSHIPS, PedigreeGraph
# Construct from arrays (no pandas needed)
pg = PedigreeGraph.from_arrays(
ids=np.array([0, 1, 2, 3, 4]),
mother_ids=np.array([-1, -1, 0, 0, 0]),
father_ids=np.array([-1, -1, 1, 1, 1]),
)
# Or from a table: a dict of columns (pandas-free), or any pandas/polars frame
pg = PedigreeGraph.from_frame(
{
"id": np.array([0, 1, 2, 3]),
"mother": np.array([-1, -1, 0, 0]),
"father": np.array([-1, -1, 1, 1]),
"twin": np.array([-1, -1, -1, -1]),
"sex": np.array([0, 1, 0, 1], dtype=np.int8),
"generation": np.array([0, 0, 1, 1], dtype=np.int32),
}
)
# pg = PedigreeGraph.from_frame(df)
# Pairs by relationship category, up to a given degree; each block holds
# read-only int32 first_rows / second_rows in the category's role orientation
pairs = pg.relationship_pairs(max_degree=3)
first, second = pairs["FS"] # full sibs
print(len(pairs["1C"])) # 1st cousins (degree 3)
print(RELATIONSHIPS["FS"].nominal_kinship) # 0.25
# Pedigree-expected kinship for those pairs, or for any two row arrays
kin = pg.pair_kinship(pairs) # {code: float32 array}
kin_fs = pg.pair_kinship(first, second)
# Counts without materialising pairs, and the three kinship-matrix families
counts = pg.relationship_counts(max_degree=3)
K = pg.kinship_matrix() # complete, CSC float32
Absent optional columns read as absent: there is no sex default and no
generation fallback. Effective-size estimators live in
pedigree_graph.effective_size; the FrameLike protocol in
pedigree_graph.typing. Migrating from 0.7.1: see the old-to-new table in
CHANGELOG.md.
Relationship registry
Codes follow the convention up_down_n_anc:
| Code | Label | up | down | n_anc | Kinship | Degree |
|---|---|---|---|---|---|---|
MZ |
MZ twin | 0 | 0 | 0 | 0.5 | 0 |
MO |
Mother–offspring | 1 | 0 | 1 | 0.25 | 1 |
FO |
Father–offspring | 1 | 0 | 1 | 0.25 | 1 |
FS |
Full sib | 1 | 1 | 2 | 0.25 | 1 |
MHS |
Maternal half sib | 1 | 1 | 1 | 0.125 | 2 |
PHS |
Paternal half sib | 1 | 1 | 1 | 0.125 | 2 |
GP |
Grandparent | 2 | 0 | 1 | 0.125 | 2 |
Av |
Avuncular | 2 | 1 | 2 | 0.125 | 2 |
1C |
1st cousin | 2 | 2 | 2 | 0.0625 | 3 |
| ... | (full registry up to 2nd cousin / kinship 1/64) |
See RELATIONSHIPS for the complete list; each RelationshipCategory
carries code, label, degree, nominal_kinship, up, down,
ancestor_count, and the two positional roles.
Experimental engines
The package ships an alternate relationship-counting engine in
pedigree_graph.experimental for exploring large-pedigree scaling:
from pedigree_graph import PedigreeGraph
from pedigree_graph.experimental import count_pairs_bfs
pg = PedigreeGraph.from_frame(df)
counts = count_pairs_bfs(pg) # dict[str, int] over 23 codes
count_pairs_bfs uses boolean sparse matmul (set-union semantics) plus
a parallel numba kernel for cousin-style codes. It is counts-only;
there is no pair-array equivalent of relationship_pairs.
The submodule is not re-exported at the top level — callers must
import explicitly via pedigree_graph.experimental. First call emits
a FutureWarning.
Caveats — read before using
-
Experimental contract. API, signature, and semantics may change or the function may be removed in any minor release. No deprecation cycle is owed.
-
Counts are unfolded, and inbred-pedigree counting differs from the matrix engine. BFS counts a pair under every category it satisfies, where
PedigreeGraph.relationship_countskeeps only the closest. On non-inbred pedigrees the BFS counts equal the matrix engine's unfolded blocks exactly. On inbred pedigrees, BFS counts distinct shared ancestors at depth ≥ 2 while the matrix engine counts paths (multiplicity); the four cousin-style codes (1C1R,H1C1R,1C2R,2C) may diverge. Seetests/test_experimental.py::test_inbred_with_cousins_cousin_codes_divergefor a hand-built fixture pinning the exact divergence. -
max_degree=5only. Lower values raiseNotImplementedError— usePedigreeGraph.relationship_counts(max_degree=k)for partial extractions. -
No view support. Pass a full graph;
PedigreeViewhas no BFS counterpart. -
Threading. The numba kernel uses
prangefor cousin-style enumeration. Numba readsNUMBA_NUM_THREADSat first JIT compilation; the optionaln_threadskwarg only takes effect on the first call in a process. SetNUMBA_NUM_THREADS=Nin the environment to control threading on all calls. -
Performance. Scaling claims (BFS faster than matrix above ~5M individuals, where the matrix engine OOMs) are unverified at the time of v0.2.0. The matrix engine is faster at n=2M in the only head-to-head we have run. See open issues #2 (numba kernel parallelisation) and #3 (10M+ scaling). Treat this engine as an experimental scalability spike, not a tuned alternative.
Architecture
For contributors: docs/architecture.md maps the
module layout and the hidden contracts (coordinate space, exact vs
approximate counts, path-count vs distinct-ancestor semantics, sparse-ID
handling, default sex), each with its source of truth and regression test.
The relationship/coordinate vocabulary is in CONTEXT.md;
design decisions are in docs/adr/.
License
MIT — see LICENSE.
Metadata
Release files for pedigree-graph 0.8.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pedigree_graph-0.8.2.tar.gz | 608.3 kB | With provenance | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| pedigree_graph-0.8.2-cp313-abi3-win_amd64.whl | CPython 3.13 | abi3 | Windows x86-64 | Details |
| pedigree_graph-0.8.2-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.13 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| pedigree_graph-0.8.2-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.13 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| pedigree_graph-0.8.2-cp313-abi3-macosx_11_0_arm64.whl | CPython 3.13 | abi3 | macOS 11.0+ ARM64 | Details |
| pedigree_graph-0.8.2-cp313-abi3-macosx_10_12_x86_64.whl | CPython 3.13 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 2.6 MB
Release files / pedigree_graph-0.8.2.tar.gz
| Download URL | pedigree_graph-0.8.2.tar.gz |
|---|---|
| Size | 608.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1257734127090e3f1490ea716b73bac67ed66ee03524811b38b76431a4672537
|
|
BLAKE2b-256 checksum How to use checksums |
ea048eabc3a6c81716bce591175a8472785c0d4f99fca99336c9bd1474fc7ce5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / pedigree_graph-0.8.2-cp313-abi3-win_amd64.whl
| Download URL | pedigree_graph-0.8.2-cp313-abi3-win_amd64.whl |
|---|---|
| Size | 332.7 kB |
| Tags | CPython 3.13 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
b2a46cf617fa8874404d1b0d388e846d801f8e72b6c2d76a5f5318335fd97953
|
|
BLAKE2b-256 checksum How to use checksums |
0630552d55702f6024be46af5db8650277f4dcb9b9e6b51f3925c11777e1deaf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / pedigree_graph-0.8.2-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pedigree_graph-0.8.2-cp313-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 439.8 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
f3b915d3ec337d7f06d83ae637d0eaf51ee4870c41485781363516238def8679
|
|
BLAKE2b-256 checksum How to use checksums |
a6a5606504f39281746f65f2a91146a71ed20acc6fd84b565b92b48e241e31e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / pedigree_graph-0.8.2-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | pedigree_graph-0.8.2-cp313-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 419.7 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
59461fb5ef82ab693d198c3d1e0038561c87ff427454f967c01489d7e41205e2
|
|
BLAKE2b-256 checksum How to use checksums |
88d35460d74b0e3101a8405a52518c08b01d71938c9560870ff62f9f53fef343
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / pedigree_graph-0.8.2-cp313-abi3-macosx_11_0_arm64.whl
| Download URL | pedigree_graph-0.8.2-cp313-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 402.9 kB |
| Tags | CPython 3.13 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9ad9af18ca3bff6a3c16c52a58c9375255dda111afb137deefc59da10c8683cc
|
|
BLAKE2b-256 checksum How to use checksums |
1ca60dcfa99e57feb5ea18ce5408ad1825971a979773d84f39d462fe10bdc960
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / pedigree_graph-0.8.2-cp313-abi3-macosx_10_12_x86_64.whl
| Download URL | pedigree_graph-0.8.2-cp313-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 409.3 kB |
| Tags | CPython 3.13 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
cfd3976cbea6a76d9d50b37dc63e3f2f898185b54ce272ad5abe585185b45c78
|
|
BLAKE2b-256 checksum How to use checksums |
5646316872b56b56a30006bde500669a57435bb53cc93d9db096745686d5aee8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logSecurity for pedigree-graph 0.8.2
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.
Full provenance
All 6 files in this release have been uploaded with provenance metadata.
Attestation for pedigree_graph-0.8.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logA 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.