{ "cells": [ { "cell_type": "markdown", "id": "14a338b0", "metadata": {}, "source": [ "# Exploring the Ontology with EMMOntoPy\n", "\n", "This notebook shows how to use **EMMOntoPy**, a Python interface for OWL ontologies based on EMMO, to retrieve structured information from the `domain-battery` ontology.\n", "\n", "EMMOntoPy makes it simple to:\n", "\n", "- Retrieve ontology classes and properties by label\n", "- Access rich metadata (like `elucidation`, `IEVReference`)\n", "- Integrate semantic battery data into other tools or pipelines\n", "\n", "Let's explore the `BatteryCell` class from the battery ontology.\n" ] }, { "cell_type": "code", "execution_count": 82, "id": "dbeb9508", "metadata": {}, "outputs": [], "source": [ "from ontopy import get_ontology\n", "\n", "# Loading from web\n", "battinfo = get_ontology('https://w3id.org/emmo/domain/battery/inferred').load()" ] }, { "cell_type": "markdown", "id": "bcd538ce", "metadata": {}, "source": [ "## Access a Class from the Ontology\n", "\n", "We start by retrieving the `BatteryCell` class from the ontology. Each class has a globally unique identifier (IRI) and carries metadata that helps us understand its role in the ontology.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "984ff91e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IRI: https://w3id.org/emmo/domain/battery#battery_68ed592a_7924_45d0_a108_94d6275d57f0\n" ] } ], "source": [ "battery = battinfo.BatteryCell\n", "\n", "# Print its IRI (unique identifier in the ontology)\n", "print(\"IRI:\", battery.iri)" ] }, { "cell_type": "markdown", "id": "b7af25ac", "metadata": {}, "source": [ "## View Properties Associated with the Class\n", "\n", "We can retrieve the object and data properties that are defined for this class using `.get_class_properties()`. These properties describe the relationships and attributes a `BatteryCell` can have." ] }, { "cell_type": "code", "execution_count": 84, "id": "bf28dd9e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Class Properties:\n", " - core.altLabel\n", " - emmo.IEVReference\n", " - emmo.elucidation\n", " - core.prefLabel\n" ] } ], "source": [ "# Retrieve all properties (object & data) where BatteryCell is in the domain\n", "print(\"Class Properties:\")\n", "for p in battery.get_class_properties():\n", " print(f\" - {p}\")\n" ] }, { "cell_type": "markdown", "id": "6772ae1e", "metadata": {}, "source": [ "## View Annotations Like Elucidation and Standards References\n", "\n", "Ontology classes often carry rich semantic annotations such as `elucidation` (a human-readable explanation) or `IEVReference` (a standards reference, e.g. from IEC). These are useful for interoperability and traceability." ] }, { "cell_type": "code", "execution_count": 85, "id": "21e2187b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elucidation: [locstr('basic functional unit, consisting of an assembly of electrodes, electrolyte, container, terminals and usually separators, that is a source of electric energy obtained by direct conversion of chemical energy.', 'en')]\n", "IEV Reference: ['https://www.electropedia.org/iev/iev.nsf/display?openform&ievref=482-01-01']\n" ] } ], "source": [ "# Print annotation properties\n", "print(\"Elucidation:\", battery.elucidation)\n", "print(\"IEV Reference:\", battery.IEVReference)\n" ] }, { "cell_type": "markdown", "id": "0d31d4eb", "metadata": {}, "source": [ "## Explore the Class Hierarchy\n", "\n", "The `.is_a` attribute shows the direct superclass(es) of the class, while `.mro()` (method resolution order) lists the full inheritance chain from the most specific to the most general.\n" ] }, { "cell_type": "code", "execution_count": 86, "id": "8a968869", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Immediate superclasses (is_a): [battery.Battery, electrochemistry.ElectrochemicalDevice]\n", "Full inheritance chain (MRO):\n", " - battery.BatteryCell\n", " - battery.Battery\n", " - electrochemistry.ElectrochemicalDevice\n", " - emmo.Device\n", " - emmo.ManufacturedProduct\n", " - emmo.Product\n", " - emmo.Maximal\n", " - emmo.Fundamental\n", " - emmo.Structural\n", " - electrochemistry.ElectrochemicalCell\n", " - emmo.HolisticArrangement\n", " - emmo.HolisticSystem\n", " - emmo.Whole\n", " - emmo.QualifiedWhole\n", " - emmo.Holistic\n", " - emmo.Object\n", " - emmo.Persistence\n", " - emmo.Perspective\n", " - emmo.CausalStructure\n", " - emmo.Fusion\n", " - emmo.Item\n", " - emmo.EMMO\n", " - owl.Thing\n", " - \n" ] } ], "source": [ "# Show the direct superclass(es)\n", "print(\"Immediate superclasses (is_a):\", battery.is_a)\n", "\n", "# Show the method resolution order (full class inheritance chain)\n", "print(\"Full inheritance chain (MRO):\")\n", "for cls in battery.mro():\n", " print(f\" - {cls}\")\n" ] }, { "cell_type": "markdown", "id": "4c1dd26e", "metadata": {}, "source": [ "## Summary\n", "\n", "EMMOntoPy allows you to access structured ontology knowledge in Python with minimal effort:\n", "\n", "- Use `.iri`, `.is_a`, and `.elucidation` for metadata\n", "- Use `.get_class_properties()` to integrate ontology structure with simulation tools, ELNs, or data pipelines\n", "\n", "Next steps:\n", "- Build instances of `BatteryCell` using JSON-LD or RDF\n", "- Annotate test data using ontology terms\n", "- Query the ontology with SPARQL or integrate into a digital twin\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }