From 66eeb9f9902e7c271b2cd40f6e7608d50ae6b4b0 Mon Sep 17 00:00:00 2001 From: TShapinsky Date: Thu, 9 May 2024 10:14:12 -0600 Subject: [PATCH] commit hashing notebook --- notebooks/hashing.ipynb | 157 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 notebooks/hashing.ipynb diff --git a/notebooks/hashing.ipynb b/notebooks/hashing.ipynb new file mode 100644 index 000000000..ddaadfb0f --- /dev/null +++ b/notebooks/hashing.ipynb @@ -0,0 +1,157 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace\n", + "import time\n", + "from buildingmotif.namespaces import BRICK, A\n", + "from buildingmotif.utils import approximate_graph_hash\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "g = Graph().parse('../tests/unit/fixtures/smallOffice_brick.ttl')\n", + "MODEL = Namespace(\"urn:model#\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ttl serialize time: 0.02885890007019043\n", + "n3 serialize time: 0.02724599838256836\n", + "xml serialize time: 0.009903907775878906\n", + "nt serialize time: 0.0031800270080566406\n", + "trig serialize time: 0.0222320556640625\n", + "json-ld serialize time: 0.01989889144897461\n", + "hext serialize time: 0.0049059391021728516\n" + ] + } + ], + "source": [ + "begin = time.time()\n", + "g.serialize(format=\"ttl\")\n", + "end = time.time()\n", + "print(f\"ttl serialize time: {end - begin}\")\n", + "\n", + "begin = time.time()\n", + "g.serialize(format=\"n3\")\n", + "end = time.time()\n", + "print(f\"n3 serialize time: {end - begin}\")\n", + "\n", + "begin = time.time()\n", + "g.serialize(format=\"xml\")\n", + "end = time.time()\n", + "print(f\"xml serialize time: {end - begin}\")\n", + "\n", + "begin = time.time()\n", + "g.serialize(format=\"nt\")\n", + "end = time.time()\n", + "print(f\"nt serialize time: {end - begin}\")\n", + "\n", + "begin = time.time()\n", + "g.serialize(format=\"trig\")\n", + "end = time.time()\n", + "print(f\"trig serialize time: {end - begin}\")\n", + "\n", + "begin = time.time()\n", + "g.serialize(format=\"json-ld\")\n", + "end = time.time()\n", + "print(f\"json-ld serialize time: {end - begin}\")\n", + "\n", + "begin = time.time()\n", + "g.serialize(format=\"hext\")\n", + "end = time.time()\n", + "print(f\"hext serialize time: {end - begin}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6727799026531355694\n", + "-4781114786451684549\n", + "5257158006352705709\n" + ] + } + ], + "source": [ + "g.add((MODEL[\"a\"], A, BRICK[\"AHU\"]))\n", + "g.serialize('before.nt', format='nt')\n", + "# print(hash(g.serialize(format='nt')))\n", + "print(approximate_graph_hash(g))\n", + "triple = (MODEL[\"b\"], A, BRICK[\"Sensor\"])\n", + "g.add(triple)\n", + "# g.serialize('after.nt', format='nt')\n", + "# print(hash(g.serialize(format='nt')))\n", + "print(approximate_graph_hash(g))\n", + "g.remove(triple)\n", + "g.serialize('before_after.nt', format='nt')\n", + "# print(hash(g.serialize(format='nt')))\n", + "print(approximate_graph_hash(g))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "triple = (MODEL[\"b\"], A, BRICK[\"Sensor\"])\n", + "g.add((MODEL[\"a\"], A, BRICK[\"AHU\"]))\n", + "before = approximate_graph_hash(g)\n", + "while True:\n", + " g.add(triple)\n", + " approximate_graph_hash(g)\n", + " g.remove(triple)\n", + " assert before == approximate_graph_hash(g)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}