Skip to main content

semantikon

Push-Pull Coverage

Logo

Motivation

Let's take a look at the following function:

def get_speed(distance: float, time: float) -> float:
    speed = distance / time
    return speed

For you as a human, it is clear that this is a function to calculate the speed for a given distance and a time. But for a computer, it is just a function that takes two floats and returns a float. The computer does not know what the inputs and outputs mean. This is where semantikon comes in. It provides a way to give scientific context to the inputs and outputs, as well as to the function itself.

Installation

You can install semantikon via pip:

pip install semantikon

You can also install semantikon via conda:

conda install -c conda-forge semantikon

Quick insight

In the realm of the workflow management systems, there are well defined inputs and outputs for each node. semantikon is a Python package to give scientific context to python functions by type annotations and decorators, which can then be translated into an rdflib knowledge graph. semantikon utilizes PMD core ontology (PMDco), which is based on the Basic Formal Ontology (BFO) and Ontology for Biomedical Investigations (OBI).

semantikon provides a way to define types for input and output parameters for function via type hinting. Type hinting is done with the function u, which requires the type, and optionally you can define ontological metadata. The type hinting is done in the following way:

>>> from rdflib import Namespace
>>> from semantikon import u
>>>
>>> EX = Namespace("http://example.org/")
>>>
>>> def get_speed(
...     distance: u(float, uri=EX.distance, units="meter"),
...     time: u(float, uri=EX.time, units="second"),
... ) -> u(float, uri=EX.speed, units="meter/second", label="speed"):
...     """some random docstring"""
...     speed = distance / time
...     return speed
>>> 
>>> 
>>> def get_kinetic_energy(
...     mass: u(float, units="kilogram", uri=EX.Mass),
...     velocity: u(float, units="meter/second", uri=EX.Velocity),
... ) -> u(float, units="joule", uri=EX.KineticEnergy):
...     return 0.5 * mass * velocity**2
>>> 
>>> 
>>> def my_kinetic_energy_workflow(
...     distance, time, mass
... ):
...     speed = get_speed(distance, time)
...     kinetic_energy = get_kinetic_energy(mass, speed)
...     return kinetic_energy

The workflow decorator from flowrep.workflow allows you to define a workflow that uses the above functions in semantikon. You can use any workflow management system that can export the workflow in the flowrep-format. Via semantikon.get_knowledge_graph you can extract a knowledge graph from the workflow. The knowledge graph schematically has the following structure:

graph TD
    get_speed["get_speed (obi:planned_process)"] -- bfo:has_part --> speed["speed (pmdco:output_assignment)"]
    speed -- pmdco:has_specified_output --> speed_data["speed_data (obi:value_specification)"]
    speed_data -- obi:specifies_value_of --> EX.speed
    speed_data -- qudt:hasUnit --> meter["meter (qudt:unit)"]
    velocity -- pmdco:has_specified_input --> speed_data
    get_kinetic_energy["get_kinetic_energy (obi:planned_process)"] -- bfo:has_part --> velocity["velocity (pmdco:input_assignment)"]

This is only the first insight into the knowledge graph. You can find the details in the notebook in the notebooks folder.

Side tour: unit conversion with pint

This part has nothing to do with the knowledge graph, but semantikon provides a way to interpret the types of inputs and outputs of a function via a decorator, in order to check consistency of the types and to convert them if necessary. Currently, semantikon provides an interpreter for pint.UnitRegistry objects. The interpreter is applied in the following way:

>>> from semantikon.metadata import u
>>> from semantikon.converter import units
>>> from pint import UnitRegistry
>>>
>>> @units
... def get_speed(
...     distance: u(float, units="meter"),
...     time: u(float, units="second")
... ) -> u(float, units="meter/second", label="speed"):
...     speed = distance / time
...     return speed
>>>
>>> ureg = UnitRegistry()
>>>
>>> print(get_speed(1 * ureg.meter, 1 * ureg.second))
1.0 meter / second

The interpreters check all types and, if necessary, convert them to the expected types before the function is executed, in order for all possible errors would be raised before the function execution. The interpreters convert the types in the way that the underlying function would receive the raw values.

In case there are multiple outputs, the type hints are to be passed as a tuple (e.g. tuple[u(float, units="meter"), u(float, units="second"))).

It is not fully guaranteed as a feature, but relative units as given on this page can be also used.

Interpreters can distinguish between annotated arguments and non-annotated arguments. If the argument is annotated, the interpreter will try to convert the argument to the expected type. If the argument is not annotated, the interpreter will pass the argument as is.

Regardless of whether type hints are provided, the interpreter acts only when the input values contain units and ontological types. If the input values do not contain units and ontological types, the interpreter will pass the input values to the function as is.

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

Copyright (c) 2026, Max-Planck-Institut für Nachhaltige Materialien GmbH - Computational Materials Design (CM) Department

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semantikon-1.4.2.tar.gz (182.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

semantikon-1.4.2-py3-none-any.whl (103.4 kB view details)

Uploaded Python 3

File details

Details for the file semantikon-1.4.2.tar.gz.

File metadata

  • Download URL: semantikon-1.4.2.tar.gz
  • Upload date:
  • Size: 182.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for semantikon-1.4.2.tar.gz
Algorithm Hash digest
SHA256 d88e29e8aef552d6df4492a73200ee8c2ed65ccabec82dc37995212b01faea1a
MD5 235923a09e5d8a6d5cce3c233847bfc3
BLAKE2b-256 4e687fe51439b029e2fc2adfaab13a3ec9ec8d456f6663d7a84c0f87f0cba146

See more details on using hashes here.

File details

Details for the file semantikon-1.4.2-py3-none-any.whl.

File metadata

  • Download URL: semantikon-1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 103.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for semantikon-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ca86fda769c390ee913b9131212c101898466f450b10e4a86f78ea4e00029836
MD5 5f6cd4077ec1f39dbfeafdd27fd4dc16
BLAKE2b-256 79482a23990a469b018d83869e58a74401946841b55200989398285fb8aad416

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.4.2 This release

2 files

1.4.1

2 files

1.4.0

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

0.0.27

2 files

0.0.26

2 files

0.0.25

2 files

0.0.24

2 files

0.0.23

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

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