Skip to main content

MedRecords

MedRecords is an opinionated wrapper around GraphRecords for medical data. It puts a graph record to work in a medical context and makes it the input medical analyses and products run on.

Installation

pip install medrecords

Rendering a report to PDF needs the pdf extra:

pip install medrecords[pdf]

Building a Record

Every call returns a new record:

import medrecords as mr

medrecord = (
    mr.MedRecord()
    .add_nodes_in_group(
        [
            ("p1", {"gender": "F", "age": 71}),
            ("p2", {"gender": "M", "age": 64}),
            ("p3", {"gender": "F", "age": 38}),
        ],
        "Patient",
    )
    .add_nodes_in_group(
        [("I10", {"label": "Essential hypertension"})],
        "Diagnosis",
    )
    .add_edges_in_group(
        [("p1", "I10", {"year": 2019}), ("p2", "I10", {"year": 2021})],
        "Patient_Diagnosis",
    )
)

add_nodes and add_edges do the same without a group. Polars DataFrames work as a source, naming the index columns: add_nodes((frame, "patient_id")), add_edges((frame, "patient_id", "diagnosis_id")).

Reading a Record

medrecord.node_count()  # 4
medrecord.group_indices()  # ['Patient', 'Diagnosis', 'Patient_Diagnosis']
medrecord.group("Patient").nodes()  # ['p1', 'p2', 'p3']

patient = medrecord.node("p1")
patient.attributes()  # {'gender': 'F', 'age': 71}
patient.groups()  # ['Patient']

Querying

mr.nodes() and mr.edges() start an expression tied to no record. medrecord.nodes() binds one to a record, which makes it a series. A series runs when you call evaluate().

older = medrecord.nodes().filter(
    mr.nodes().in_group("Patient") & (mr.nodes().attribute("age") > 60)
)
list(older.evaluate())  # ['p1', 'p2']
diagnosed = (
    medrecord.edges().filter(mr.edges().in_group("Patient_Diagnosis")).source_node()
)
list(diagnosed.evaluate())  # ['p1', 'p2']
medrecord.nodes().filter(mr.nodes().in_group("Patient")).attribute("age").mean().evaluate()
# 57.666666666666664

medrecord.query(expression) binds a free expression the same way.

Evaluations

An analytic is a class with one compute method that returns one value. Analytics are placed in groups, and groups in an evaluation:

from medrecords.evaluation import Analytic, Evaluation, EvaluationGroup, read
from medrecords.evaluation.catalogue import AttributeCounts, AttributeMean, ElementCount

PATIENTS = mr.nodes().filter(mr.nodes().in_group("Patient"))


class OldestPatient(Analytic[mr.MedRecord, int]):
    """The age of the oldest patient."""

    def compute(self, medrecord: mr.MedRecord) -> int:
        return read(medrecord.query(PATIENTS).attribute("age").max().evaluate(), int)


evaluation = (
    Evaluation[mr.MedRecord]("Practice snapshot")
    .add_group(
        "Demographics",
        EvaluationGroup[mr.MedRecord]()
        .add_analytic("Patients", ElementCount(PATIENTS))
        .add_analytic("Mean age", AttributeMean("age", PATIENTS))
        .add_analytic("Sex", AttributeCounts("gender", PATIENTS)),
    )
    .add_analytic("Oldest", OldestPatient())
)

The second type argument of Analytic is the type of the value compute returns. The report is read by name:

report = evaluation.report(medrecord)

report.group("Demographics").analytic("Patients").result  # 3
report.group("Demographics").analytic("Mean age").result  # 57.666666666666664
report.analytic("Oldest").result  # 71

A result is a number or a string, or one of Table, Plot, Measurement, Assessment and Distribution. report.group("Demographics").analytic("Sex").result.to_polars() gives

┌───────┬───────┬──────────┐
│ value ┆ count ┆ share    │
╞═══════╪═══════╪══════════╡
│ F     ┆ 2     ┆ 0.666667 │
│ M     ┆ 1     ┆ 0.333333 │
└───────┴───────┴──────────┘

medrecords.evaluation.catalogue holds analytics for counts, attribute statistics, completeness, ranges and uniqueness. add_group_over and add_group_over_each run a group against something derived from the record, a cohort or one part per attribute value.

JSON and PDF are exports:

from medrecords.evaluation import Document, Report

report.to_json("report.json")
Report.from_json("report.json")

Document(report).to_pdf("report.pdf")  # needs the pdf extra

Documentation

Metadata

Release files for medrecords 0.2.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 medrecords 0.2.0
File Size Uploaded Provenance
medrecords-0.2.0.tar.gz 1.0 MB Without provenance Details

Built distribution (wheel)

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

Total release size: 2.1 MB

Release files / medrecords-0.2.0.tar.gz

Download URL medrecords-0.2.0.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
5b9106f8ccc57daf0cf9161f4869ce79f447420c303775b2c188e0aa2a309674
BLAKE2b-256 checksum
How to use checksums
dc4be086ace859867bb5ddd28ad69ed1c418b9ea2a41d97aa2e249c89c842144
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / medrecords-0.2.0-py3-none-any.whl

Download URL medrecords-0.2.0-py3-none-any.whl
Size 1.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
26baaff8e5bb8938376cfb5c1d2903bed52b9547d16a00960b627a651fde6e4f
BLAKE2b-256 checksum
How to use checksums
264a1c9c175c99b1b52039ec3819199755ed38c7723a407520ce220381faf001
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for medrecords 0.2.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.

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