From 6e4fde2b26701f077fb33b0e89d0766dbd6cd186 Mon Sep 17 00:00:00 2001 From: Sierra Taylor Moxon Date: Mon, 18 Nov 2024 14:06:59 -0800 Subject: [PATCH] adding denormalize example notebook --- docs/examples/denormalizing.ipynb | 115 ++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 docs/examples/denormalizing.ipynb diff --git a/docs/examples/denormalizing.ipynb b/docs/examples/denormalizing.ipynb new file mode 100644 index 0000000..c75b600 --- /dev/null +++ b/docs/examples/denormalizing.ipynb @@ -0,0 +1,115 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "cd2c8429-a0e7-4a95-a7ed-b1fd8a53b89d", + "metadata": {}, + "outputs": [], + "source": [ + "from pathlib import Path\n", + "\n", + "from linkml_runtime.dumpers import yaml_dumper\n", + "from linkml_runtime.utils.formatutils import camelcase, underscore\n", + "from linkml_runtime.utils.schemaview import SchemaView\n", + "from pytest import fixture\n", + "\n", + "from linkml_map.datamodel.transformer_model import (\n", + " ClassDerivation,\n", + " CopyDirective,\n", + " SlotDerivation,\n", + " TransformationSpecification,\n", + ")\n", + "from linkml_map.inference.schema_mapper import SchemaMapper\n", + "from linkml_map.session import Session\n", + "from linkml_map.utils.loaders import load_specification\n", + "\n", + "REPO_ROOT = Path.cwd().parent.parent" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "121e9e1c-28c2-4fec-bb76-e078836d1f3e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MappingSet\n", + "Mapping\n", + "\n", + "mappings\n", + "subject_id\n", + "subject_name\n", + "object_id\n", + "object_name\n", + "predicate_id\n" + ] + } + ], + "source": [ + "input_schema = REPO_ROOT / \"tests/input/examples/flattening/source/normalized.yaml\"\n", + "output_schema = REPO_ROOT / \"tests/input/examples/flattening/target/denormalized.yaml\"\n", + "transformation_specification_file = REPO_ROOT / \"tests/input/examples/flattening/transform/denormalize.transform.yaml\"\n", + "# Initialize Session and SchemaBuilder\n", + "session = Session()\n", + "\n", + "source_schema_view = SchemaView(input_schema)\n", + "target_schema_view = SchemaView(output_schema)\n", + "# Set the source schema in the session\n", + "session.set_source_schema(source_schema_view)\n", + "\n", + "tr_spec = load_specification(transformation_specification_file)\n", + "mapper = SchemaMapper()\n", + "mapper.source_schemaview = source_schema_view\n", + "\n", + "target_schema_obj = mapper.derive_schema(\n", + " specification=tr_spec,\n", + " target_schema_id=\"denormalized-view\",\n", + " target_schema_name=\"DenormalizedView\",\n", + ")\n", + "\n", + "yaml_dumper.dump(target_schema_obj, str(\"denormalized_view.yaml\"))\n", + "\n", + "transformed_sv = SchemaView(\"denormalized_view.yaml\")\n", + "\n", + "for class_name in transformed_sv.all_classes():\n", + " print(class_name)\n", + "print()\n", + "for slot_name in transformed_sv.all_slots():\n", + " print(slot_name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc48f85d-bf7b-44fe-90e1-cd256191c06a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}