|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Graph Algebra with `kglab`\n", |
| 8 | + "\n", |
| 9 | + "## intro\n", |
| 10 | + "`kglab` provides tools to access graph data from multiple source to build a `KnowledgeGraph` that can be easily used by data scientists. For a thorough explanation of how to use triples-stored data and how to load this data into `kglab` please see examples in the `examples/` directory. The examples in this directory (`examples/graph_algebra/`) will care to introduce graph algebra capabilities to be used on the graphs the user has loaded. \n", |
| 11 | + "\n", |
| 12 | + "## basic load and querying\n", |
| 13 | + "In particular, once your data is loaded in a `KnowledgeGraph` with something like:\n", |
| 14 | + "\n", |
| 15 | + "1. Instantiate a graph from a dataset:" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 1, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "data": { |
| 25 | + "text/plain": [ |
| 26 | + "<kglab.kglab.KnowledgeGraph at 0x7f283f3d3940>" |
| 27 | + ] |
| 28 | + }, |
| 29 | + "execution_count": 1, |
| 30 | + "metadata": {}, |
| 31 | + "output_type": "execute_result" |
| 32 | + } |
| 33 | + ], |
| 34 | + "source": [ |
| 35 | + "# for use in tutorial and development; do not include this `sys.path` change in production:\n", |
| 36 | + "import sys ; sys.path.insert(0, \"../../\")\n", |
| 37 | + "from os.path import dirname\n", |
| 38 | + "import kglab\n", |
| 39 | + "import os\n", |
| 40 | + "\n", |
| 41 | + "namespaces = {\n", |
| 42 | + " \"foaf\": \"http://xmlns.com/foaf/0.1/\",\n", |
| 43 | + " \"gorm\": \"http://example.org/sagas#\",\n", |
| 44 | + " \"rel\": \"http://purl.org/vocab/relationship/\",\n", |
| 45 | + " }\n", |
| 46 | + "\n", |
| 47 | + "kg = kglab.KnowledgeGraph(\n", |
| 48 | + " name = \"Happy Vikings KG example for SKOS/OWL inference\",\n", |
| 49 | + " namespaces=namespaces,\n", |
| 50 | + " )\n", |
| 51 | + "\n", |
| 52 | + "kg.load_rdf(dirname(dirname(os.getcwd())) + \"/dat/gorm.ttl\")" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "\n", |
| 60 | + "2. It is possible to create a subgraph by providing a SPARQL query, by defining a \"subject\" and \"object\":\n" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 2, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "query = \"\"\"SELECT ?subject ?object\n", |
| 70 | + "WHERE {\n", |
| 71 | + " ?subject rdf:type gorm:Viking .\n", |
| 72 | + " ?subject gorm:childOf ?object .\n", |
| 73 | + "}\n", |
| 74 | + "\"\"\"" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "\n", |
| 82 | + "## define a subgraph\n", |
| 83 | + "In this case we are looking for the network of parent-child relations among members of Vikings family.\n", |
| 84 | + "\n", |
| 85 | + "With this query we can define a **subgraph** so to have access to **graph algebra** capabilities: " |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 3, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "from kglab.subg import SubgraphMatrix\n", |
| 95 | + "\n", |
| 96 | + "subgraph = SubgraphMatrix(kg=kg, sparql=query)\n" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "## compute Adjacency matrices\n", |
| 104 | + "Let's compute the first basic adjacency matrix (usually noted with `A`):" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 4, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [ |
| 112 | + { |
| 113 | + "data": { |
| 114 | + "text/plain": [ |
| 115 | + "array([[0., 1., 1., 0., 0.],\n", |
| 116 | + " [0., 0., 0., 1., 0.],\n", |
| 117 | + " [0., 0., 0., 0., 0.],\n", |
| 118 | + " [0., 0., 0., 0., 1.],\n", |
| 119 | + " [0., 0., 0., 0., 0.]])" |
| 120 | + ] |
| 121 | + }, |
| 122 | + "execution_count": 4, |
| 123 | + "metadata": {}, |
| 124 | + "output_type": "execute_result" |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "adj_matrix = subgraph.to_adjacency()\n", |
| 129 | + "adj_matrix" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "what happened here is that all the subjects and objects have been turned into integer indices from 0 to number of nodes. So we can see that the entity with index 0 is adjancent (is connected, has a directed edge) to the entity with index 1. This is a directed graph because the relationship `gorm:childOf` goes from child to parent, let's turn this into an undirected graph so to see the relation in a more symmetric way (both the child-parent and parent-child)." |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 6, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [ |
| 144 | + { |
| 145 | + "data": { |
| 146 | + "text/plain": [ |
| 147 | + "array([[0., 1., 1., 0., 0.],\n", |
| 148 | + " [1., 0., 0., 1., 0.],\n", |
| 149 | + " [1., 0., 0., 0., 0.],\n", |
| 150 | + " [0., 1., 0., 0., 1.],\n", |
| 151 | + " [0., 0., 0., 1., 0.]])" |
| 152 | + ] |
| 153 | + }, |
| 154 | + "execution_count": 6, |
| 155 | + "metadata": {}, |
| 156 | + "output_type": "execute_result" |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "undirected_adj_mtx = subgraph.to_undirected()\n", |
| 161 | + "undirected_adj_mtx" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "We can see now the relationship is a generic symmetric \"parenthood\" relations, not just a child-parent directed relationship." |
| 169 | + ] |
| 170 | + } |
| 171 | + ], |
| 172 | + "metadata": { |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3.8.10 64-bit ('.venv': venv)", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.8.10" |
| 189 | + }, |
| 190 | + "orig_nbformat": 4, |
| 191 | + "vscode": { |
| 192 | + "interpreter": { |
| 193 | + "hash": "de68f9b565e1e230f4433adb1a318d8f3a0dfad0917fa0c696727472c8ddadbf" |
| 194 | + } |
| 195 | + } |
| 196 | + }, |
| 197 | + "nbformat": 4, |
| 198 | + "nbformat_minor": 2 |
| 199 | +} |
0 commit comments