{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Registry ingest and search\n", "\n", "This notebook demonstrates how to ingest metadata sources into the \n", "BDF registry and query them with `bdf.search()` and `bdf.sparql()`.\n" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## Setup\n", "\n", "Install BDF first: `pip install batterydf`.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import bdf\n" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Crawl a trusted source\n", "\n", "This builds `registry.ttl` and `registry.db` under the default \n", "registry directory (or `BDF_REGISTRY_DIR` if set). Here the source is a \n", "small metadata snapshot committed under `examples/in/registry`; swap in the \n", "upstream repo URL (see the commented line) to crawl the full dataset.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# Build from the metadata snapshot committed under examples/in/registry\n", "# (keeps this notebook offline / CI-friendly).\n", "source = \"in/registry\"\n", "# To crawl the full upstream dataset repo instead, run locally with refresh=True:\n", "# source = \"https://github.com/DigiBatt/battery-data/tree/main\"\n", "registry_dir = Path(\"out/registry\")\n", "summary = bdf.build_registry(source, registry_dir=registry_dir, refresh=False)\n", "summary\n" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "By default, `bdf.search()` and `bdf.sparql()` will query the official BDF registry.\n", "Until that exists, pass `registry_dir` to query a local registry build.\n" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## Fast search\n", "\n", "`bdf.search()` queries the local `registry.db` index.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "bdf.search(\"graphite\", registry_dir=registry_dir)[:5]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "bdf.search(\"hppc\", registry_dir=registry_dir)[:5]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "bdf.search(\">=3 Ah\", registry_dir=registry_dir)[:5]\n" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "## SPARQL queries\n", "\n", "Use `bdf.sparql()` to run linked-data queries against `registry.ttl`.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "query = \"\"\"\n", "SELECT ?dataset ?name\n", "WHERE {\n", " ?dataset a ;\n", " ?name .\n", "}\n", "LIMIT 5\n", "\"\"\"\n", "bdf.sparql(query, registry_dir=registry_dir)\n" ] } ], "metadata": { "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 }