Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Ontolutils: Python models for scientific metadata and RDF

Tests Documentation

Ontolutils helps you describe things and concepts in Python and express how they relate to one another. For example, you can describe a dataset, the researcher who created it, and the instrument used to collect the data. Define your own models or generate them from an ontology. Pydantic validates the fields, and you can share the resulting RDF graph as JSON-LD or Turtle.

Install

Ontolutils supports Python 3.10–3.14, Pydantic 2.x, and RDFLib 7.x. Python 3.14 requires Pydantic 2.12 or newer.

Install the current checkout in a virtual environment:

python -m pip install .

Create, save, and reload metadata

from pathlib import Path
from pydantic import Field
from ontolutils import Thing, namespaces, urirefs


@namespaces(prov="http://www.w3.org/ns/prov#", foaf="http://xmlns.com/foaf/0.1/")
@urirefs(Person="prov:Person", firstName="foaf:firstName", lastName="foaf:lastName")
class Person(Thing):
    firstName: str = Field(alias="first_name")
    lastName: str | None = Field(default=None, alias="last_name")

researcher = Person(
    id="https://example.org/people/ada",
    first_name="Ada",
    last_name="Lovelace",
)
Path("researcher.jsonld").write_text(researcher.model_dump_jsonld(), encoding="utf-8")
restored = Person.from_jsonld(source="researcher.jsonld", limit=1)
assert restored.firstName == "Ada"
print(researcher.serialize("ttl"))

first_name is an input alias for firstName, mapped to foaf:firstName. model_dump_jsonld() returns a string; get_jsonld_dict() returns a dictionary. Loading normally returns a list; limit=1 returns one matching object, or [] if none match. Assign stable id values when sharing metadata.

Pydantic checks the model’s declared field types and constraints. Use SHACL to check an RDF graph against a set of shapes. Scientific validity still depends on the data and the rules you apply.

Models contain selected ontology terms. Loading projects RDF onto their mapped fields, so unknown predicates and additional source types may be omitted on export. Keep the source graph when all triples must survive. Numerical arrays export as unordered RDF values; order, duplicates, shape and dtype are not preserved. See the 1.0 migration guide.

Load an ontology and create Python models

onto.load() accepts a Turtle file or an HTTP(S) URL and generates Thing subclasses. Use rdf_fields() to discover the Python field names and their RDF predicates.

from ontolutils import onto

m4i = onto.load("https://w3id.org/nfdi4ing/metadata4ing/ontology.ttl")
print(sorted(vars(m4i.models)))

Tool = m4i.models.Tool
print(Tool.rdf_fields())
tool = Tool(
    id="https://example.org/instruments/laser",
    hasParameter="https://example.org/parameters/power",
)
print(tool.serialize("ttl"))

The loader discovers hasParameter from the M4I property domain, which includes Tool and Method. The Turtle output links the laser to the power parameter with m4i:hasParameter. See the ontology loader guide to pin a snapshot or add stricter application rules with a profile.

Generate Python vocabulary terms

Use render_namespace to generate an importable RDFLib namespace for your project. This example uses two class declarations from Metadata4Ing (M4I) so you can run it offline.

from pathlib import Path

from rdflib import Graph

from ontolutils.namespace_generation import render_namespace

namespace = "http://w3id.org/nfdi4ing/metadata4ing#"
graph = Graph().parse(
    data=f'''
        @prefix m4i: <{namespace}> .
        @prefix owl: <http://www.w3.org/2002/07/owl#> .

        m4i:NumericalVariable a owl:Class .
        m4i:ProcessingStep a owl:Class .
    ''',
    format="turtle",
)
module = render_namespace(graph, namespace, "M4I")
Path("m4i_terms.py").write_text(module, encoding="utf-8", newline="\n")

from m4i_terms import M4I

assert str(M4I.NumericalVariable) == namespace + "NumericalVariable"

For the full vocabulary, download the M4I Turtle snapshot as m4i.ttl and verify it against the SHA-256 recorded for your chosen revision. The publisher verifier records the tested M4I 1.5.0 digest. Replace the example's graph with Graph().parse("m4i.ttl", format="turtle") and keep the exact M4I namespace above. Ship m4i_terms.py with your package for offline imports. Define Pydantic models and application-specific aliases in your own code.

See the namespace guide for file-based generation and the publisher snapshot checks to reproduce verification against M4I, Allotrope HDF5, SSNO, QUDT, and other vocabularies.

Learn by task

The introductory examples run with the base installation. Install Pint for Pint-based unit arithmetic and xarray to create DataArrays. Install pySHACL to validate RDF graphs against shapes. See the installation guide for the commands.

Development

See test instructions and documentation build instructions.

Metadata

Release files for ontolutils 1.0.0b1

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

Source distribution (sdist)

Source distribution for ontolutils 1.0.0b1
File Size Uploaded Provenance Actions
ontolutils-1.0.0b1.tar.gz 161.3 kB Without provenance Details for ontolutils-1.0.0b1.tar.gz

Built distribution (wheel)

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

Total release size: 274.5 kB

Release files / ontolutils-1.0.0b1.tar.gz

Download URL ontolutils-1.0.0b1.tar.gz
Size 161.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6a6ab378782e421c451b727fe48778cba64dfba378137848da76afd5afbc4dfe
BLAKE2b-256 checksum
How to use checksums
612997dd45e0d2e0e8459ede50b3378be52cd3dba6f70e25763ff3bbac476153
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.16

Release files / ontolutils-1.0.0b1-py3-none-any.whl

Download URL ontolutils-1.0.0b1-py3-none-any.whl
Size 113.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b743cb1c40518894f42c0f55d63bb8c9516a9be7fc44f00fce78a5b011e78322
BLAKE2b-256 checksum
How to use checksums
7b00186a490959cac093b97a47f056b393bcfa5b64d6ede0931a51428d9fd5ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.16

Security for ontolutils 1.0.0b1

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
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