{ "cells": [ { "cell_type": "markdown", "id": "c74a70b1-7972-4cbe-9685-126ddb8e8cda", "metadata": {}, "source": [ "# Visualize HDF5 in a knowledge graph\n", "\n", "Using `kglab` (https://derwen.ai/docs/kgl/)" ] }, { "cell_type": "code", "execution_count": 1, "id": "a9b58411-88af-4972-b5c8-b01391df58fc", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# !pip install kglab" ] }, { "cell_type": "code", "execution_count": 2, "id": "feb70a18-b9e7-40db-b369-4e6c927301e5", "metadata": {}, "outputs": [], "source": [ "import kglab\n", "import rdflib" ] }, { "cell_type": "code", "execution_count": 3, "id": "690c0219-d7f5-4b0d-a897-59cf8f354149", "metadata": {}, "outputs": [], "source": [ "import h5rdmtoolbox as h5tbx" ] }, { "cell_type": "code", "execution_count": 4, "id": "d4d871e0-5bff-42f7-a27d-f1f0337dd964", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@context\": {\n", " \"foaf\": \"http://xmlns.com/foaf/0.1/\",\n", " \"hdf5\": \"http://purl.allotrope.org/ontologies/hdf5/1.8#\",\n", " \"prov\": \"http://www.w3.org/ns/prov#\",\n", " \"skos\": \"http://www.w3.org/2004/02/skos/core#\"\n", " },\n", " \"@graph\": [\n", " {\n", " \"@id\": \"_:N1\",\n", " \"@type\": \"hdf5:File\",\n", " \"hdf5:rootGroup\": {\n", " \"@id\": \"_:N0\",\n", " \"@type\": \"hdf5:Group\",\n", " \"hdf5:member\": [\n", " {\n", " \"@id\": \"https://orcid.org/0000-0001-8729-0482\",\n", " \"@type\": [\n", " \"hdf5:Group\",\n", " \"prov:Person\"\n", " ],\n", " \"foaf:firstName\": \"Matthias\",\n", " \"foaf:lastName\": \"Probst\",\n", " \"hdf5:attribute\": [\n", " {\n", " \"@id\": \"_:N2\",\n", " \"@type\": \"hdf5:Attribute\",\n", " \"hdf5:name\": \"fname\",\n", " \"hdf5:value\": \"Matthias\"\n", " },\n", " {\n", " \"@id\": \"_:N3\",\n", " \"@type\": \"hdf5:Attribute\",\n", " \"hdf5:name\": \"hint\",\n", " \"hdf5:value\": \"This group could be representing a person.\",\n", " \"skos:definition\": \"A hint gives helpful information on something.\"\n", " },\n", " {\n", " \"@id\": \"_:N4\",\n", " \"@type\": \"hdf5:Attribute\",\n", " \"hdf5:name\": \"lname\",\n", " \"hdf5:value\": \"Probst\"\n", " }\n", " ],\n", " \"hdf5:name\": \"/contact\"\n", " },\n", " {\n", " \"@id\": \"_:N5\",\n", " \"@type\": \"hdf5:Dataset\",\n", " \"hdf5:datatype\": \"H5T_FLOAT\",\n", " \"hdf5:dimension\": 0,\n", " \"hdf5:name\": \"/test\",\n", " \"hdf5:size\": 1\n", " }\n", " ],\n", " \"hdf5:name\": \"/\"\n", " }\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "with h5tbx.File() as h5:\n", " h5.create_dataset(name='test', data=4.3)\n", " grp = h5.create_group(name='contact')\n", " grp.attrs['fname', rdflib.FOAF.firstName] = 'Matthias'\n", " grp.attrs['lname', rdflib.FOAF.lastName] = 'Probst'\n", " grp.attrs['hint'] = 'This group could be representing a person.'\n", " grp.rdf['hint'].definition = 'A hint gives helpful information on something.'\n", " grp.rdf.type = rdflib.PROV.Person # --> rdf.type = Person\n", " grp.rdf.subject = 'https://orcid.org/0000-0001-8729-0482' # -> @id='https://orcid.org/0000-0001-8729-0482'\n", "\n", " print(h5.dump_jsonld(indent=2, structural=True, resolve_keys=True))\n", " \n", "g, ctx = h5tbx.jsonld.get_rdflib_graph(h5.hdf_filename)" ] }, { "cell_type": "code", "execution_count": 5, "id": "18754370-f273-4ce0-bedb-13cfab2fd962", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'hdf5': 'http://purl.allotrope.org/ontologies/hdf5/1.8#',\n", " 'prov': 'http://www.w3.org/ns/prov#',\n", " 'foaf': 'http://xmlns.com/foaf/0.1/',\n", " 'skos': 'http://www.w3.org/2004/02/skos/core#'}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ctx" ] }, { "cell_type": "code", "execution_count": 6, "id": "b0e139d7-94b8-48fe-b065-3efb03de9489", "metadata": {}, "outputs": [], "source": [ "# g = rdflib.Graph()\n", "# bn = rdflib.BNode()\n", "# g.add((bn, rdflib.URIRef('https://schema.org/name'), rdflib.Literal('Matthias')))" ] }, { "cell_type": "code", "execution_count": 7, "id": "064a9e31-1fd0-429e-9189-663e950bc52f", "metadata": {}, "outputs": [], "source": [ "kg = kglab.KnowledgeGraph(\n", " import_graph=g,\n", " namespaces=ctx)" ] }, { "cell_type": "code", "execution_count": 8, "id": "ce1a26f6-ad59-4014-b5b2-dcee38f11a26", "metadata": {}, "outputs": [], "source": [ "subgraph = kglab.SubgraphTensor(kg)" ] }, { "cell_type": "code", "execution_count": 9, "id": "78c9adf7-43f0-4aa1-8740-149bf3a5f74f", "metadata": {}, "outputs": [], "source": [ "VIS_STYLE= {\"hdf5\": {\"color\": \"red\", \"size\": 30},\n", " \"_\": {\"color\": \"black\", \"size\": 20}}" ] }, { "cell_type": "code", "execution_count": 10, "id": "e8d13d2e-d66e-4028-8909-7192cfef1bf6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Warning: When cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.\n" ] } ], "source": [ "pyvis_graph= subgraph.build_pyvis_graph(notebook=True, style=VIS_STYLE)\n", "#pyvis_graph.show('graph.html', notebook=True)\n", "# the above call fails in the readthedocs build process... we show an image of the result here:" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c93fe042-3c77-4c00-9738-324d465213da", "metadata": {}, "source": [ "![grafik.png](../_static/Screenshot_pyvis_graph.png)" ] }, { "cell_type": "code", "execution_count": 11, "id": "a44f0039-00b8-44c5-b237-ccba12993bdd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@context\": {\n", " \"fname\": \"http://xmlns.com/foaf/0.1/firstName\",\n", " \"lname\": \"http://xmlns.com/foaf/0.1/lastName\"\n", " },\n", " \"@graph\": [\n", " {\n", " \"@id\": \"http://www.w3.org/ns/prov#Person\",\n", " \"fname\": \"Matthias\",\n", " \"lname\": \"Probst\"\n", " },\n", " {\n", " \"@id\": \"_:N14\",\n", " \"http://www.w3.org/2004/02/skos/core#definition\": \"A hint\"\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "with h5tbx.File() as h5:\n", " h5.create_dataset(name='test', data=4.3)\n", " grp = h5.create_group(name='contact')\n", " grp.attrs['fname', rdflib.FOAF.firstName] = 'Matthias'\n", " grp.attrs['lname', rdflib.FOAF.lastName] = 'Probst'\n", " grp.attrs['hint'] = 'bah blah'\n", " grp.rdf['hint'].definition = 'A hint'\n", " grp.rdf.subject = rdflib.PROV.Person\n", " grp.attrs['@id'] = 'https://orcid.org/0000-0001-8729-0482'\n", " print(h5.dump_jsonld(indent=2, structural=False))\n", "\n", "g, ctx = h5tbx.jsonld.get_rdflib_graph(h5.hdf_filename)" ] }, { "cell_type": "code", "execution_count": 12, "id": "3e8834fc-6217-42c3-b442-2565b012f828", "metadata": {}, "outputs": [], "source": [ "jsonld = h5tbx.dump_jsonld(h5.hdf_filename, indent=2, semantic=True, structural=False)\n", "with open('test.jsonld', 'w') as f:\n", " f.write(jsonld)" ] }, { "cell_type": "code", "execution_count": 13, "id": "077911e8-8bc3-4b58-a480-df605bdceae4", "metadata": {}, "outputs": [], "source": [ "VIS_STYLE= {\"hdf5\": {\"color\": \"red\", \"size\": 30},\n", " \"_\": {\"color\": \"black\", \"size\": 20},\n", " \"prof\": {\"color\": \"green\", \"size\": 10},\n", " \"foaf\": {\"color\": \"green\", \"size\": 10}}" ] }, { "cell_type": "code", "execution_count": 14, "id": "67f99459-8351-4819-8a67-2b58d4b1e398", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Warning: When cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.\n" ] } ], "source": [ "kg_from_jsonld = kglab.KnowledgeGraph().load_jsonld('test.jsonld')\n", "\n", "subgraph = kglab.SubgraphTensor(kg_from_jsonld)\n", "pyvis_graph = subgraph.build_pyvis_graph(notebook=True, style=VIS_STYLE)\n", "\n", "pyvis_graph.force_atlas_2based(overlap=0)\n", "# pyvis_graph.show(\"tmp.grpah.html\", local=True, notebook=True)\n", "# the above call fails in the readthedocs build process... we show an image of the result here:" ] }, { "cell_type": "markdown", "id": "2d0d3238-7763-4734-9e34-29ae7caa5b4c", "metadata": {}, "source": [ "![grafik.png](../_static/Screenshot_pyvis_graph2.png)" ] }, { "cell_type": "code", "execution_count": null, "id": "d4b4a3df-6e4e-42de-ae39-7e51e5e6d465", "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.8.19" } }, "nbformat": 4, "nbformat_minor": 5 }