-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from simphony/dev
Merge sprint #3.
- Loading branch information
Showing
12 changed files
with
359 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "boxed-professional", | ||
"metadata": {}, | ||
"source": [ | ||
"# Tutorial: Import and export" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "operational-honey", | ||
"metadata": {}, | ||
"source": [ | ||
"<figure style=\"display: table; text-align:center; margin-left: auto; margin-right:auto\">\n", | ||
" \n", | ||
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/simphony/docs/master?filepath=docs%2Fsource%2Fjupyter%2Fimport_export.ipynb \"Click to run the tutorial yourself!\")\n", | ||
" \n", | ||
"</figure>\n", | ||
"\n", | ||
"In this tutorial we will be covering the import and export capabilities of OSP-core. The utility functions that provide these functionalities are `import_cuds` and `export_cuds`, respectively." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "qualified-works", | ||
"metadata": {}, | ||
"source": [ | ||
"<div class=\"admonition important\">\n", | ||
"<div class=\"admonition-title\" style=\"font-weight: bold\"><div style=\"display: inline-block\">Tip</div></div>\n", | ||
" \n", | ||
"The full API specifictions of the import and export functions can be found in the\n", | ||
"[utilities API reference page](../api_ref.html#osp.core.utils.export_cuds).\n", | ||
" \n", | ||
"</div>\n", | ||
"</div>\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "driving-injury", | ||
"metadata": {}, | ||
"source": [ | ||
"For our running example, we'll be using the *city ontology* that was already introduces in the [cuds API tutorial](./cuds_api.html). First, make sure the city ontology is installed. If not, run the following command:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "dying-accreditation", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"INFO 2021-05-31 10:55:51,839 [osp.core.ontology.installation]: Will install the following namespaces: ['city']\n", | ||
"INFO 2021-05-31 10:55:51,879 [osp.core.ontology.yml.yml_parser]: Parsing YAML ontology file c:\\users\\yoav\\anaconda3\\envs\\osp\\lib\\site-packages\\osp\\core\\ontology\\docs\\city.ontology.yml\n", | ||
"INFO 2021-05-31 10:55:51,995 [osp.core.ontology.yml.yml_parser]: You can now use `from osp.core.namespaces import city`.\n", | ||
"INFO 2021-05-31 10:55:51,996 [osp.core.ontology.parser]: Loaded 7396 ontology triples in total\n", | ||
"INFO 2021-05-31 10:55:52,437 [osp.core.ontology.installation]: Installation successful\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pico install city" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "lonely-listening", | ||
"metadata": {}, | ||
"source": [ | ||
"Next we create a few CUDS objects:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "considered-leonard", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<city.Citizen: 98f8ac8c-713d-4406-bc36-e0152f9e2ea3, CoreSession: @0x221b0a05250>" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from osp.core.namespaces import city\n", | ||
"\n", | ||
"c = city.City(name=\"Freiburg\", coordinates=[47, 7])\n", | ||
"p1 = city.Citizen(name=\"Peter\")\n", | ||
"p2 = city.Citizen(name=\"Anne\")\n", | ||
"c.add(p1, rel=city.hasInhabitant)\n", | ||
"c.add(p2, rel=city.hasInhabitant)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "worth-province", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can use the `export_cuds` methods to export the data into a file:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "monthly-anxiety", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from osp.core.utils import export_cuds\n", | ||
"\n", | ||
"export_cuds(c, file='./data.ttl', format='turtle')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "determined-nursing", | ||
"metadata": {}, | ||
"source": [ | ||
"This will create the file `data.ttl` with the following content:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "outstanding-wound", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"@prefix city: <http://www.osp-core.com/city#> .\n", | ||
"@prefix cuba: <http://www.osp-core.com/cuba#> .\n", | ||
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n", | ||
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n", | ||
"\n", | ||
"cuba:_serialization rdf:first \"47398674-720b-4765-9047-b5351ed175c0\" .\n", | ||
"\n", | ||
"<http://www.osp-core.com/cuds#8a90e2b3-7cca-4103-9eba-aab55e5903b1> a city:Citizen ;\n", | ||
" city:INVERSE_OF_hasInhabitant <http://www.osp-core.com/cuds#47398674-720b-4765-9047-b5351ed175c0> ;\n", | ||
" city:age 25 ;\n", | ||
" city:name \"Peter\" .\n", | ||
"\n", | ||
"<http://www.osp-core.com/cuds#98f8ac8c-713d-4406-bc36-e0152f9e2ea3> a city:Citizen ;\n", | ||
" city:INVERSE_OF_hasInhabitant <http://www.osp-core.com/cuds#47398674-720b-4765-9047-b5351ed175c0> ;\n", | ||
" city:age 25 ;\n", | ||
" city:name \"Anne\" .\n", | ||
"\n", | ||
"<http://www.osp-core.com/cuds#47398674-720b-4765-9047-b5351ed175c0> a city:City ;\n", | ||
" city:coordinates \"[47, 7]\"^^<http://www.osp-core.com/cuba#_datatypes/VECTOR-INT-2> ;\n", | ||
" city:hasInhabitant <http://www.osp-core.com/cuds#8a90e2b3-7cca-4103-9eba-aab55e5903b1>,\n", | ||
" <http://www.osp-core.com/cuds#98f8ac8c-713d-4406-bc36-e0152f9e2ea3> ;\n", | ||
" city:name \"Freiburg\" .\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from sys import platform\n", | ||
"\n", | ||
"if platform == 'win32':\n", | ||
" !more data.ttl\n", | ||
"else:\n", | ||
" !cat data.ttl" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "offshore-cotton", | ||
"metadata": {}, | ||
"source": [ | ||
"You can change the format by entering a different value for the parameter `format`. The supported formats are “xml”, “n3”, “turtle”, “nt”, “pretty-xml”, “trix”, “trig” and “nquads”." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "derived-advancement", | ||
"metadata": {}, | ||
"source": [ | ||
"To import data, we can use the `import` method. Let's assume we wish to import data into an SQLite session. The following code will help us to achieve our aim:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "stable-session", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from osp.wrappers.sqlite import SqliteSession\n", | ||
"from osp.core.utils import import_cuds\n", | ||
"\n", | ||
"with SqliteSession(\"test.db\") as session:\n", | ||
" wrapper = city.CityWrapper(session=session)\n", | ||
" c = import_cuds('./data.ttl')\n", | ||
" wrapper.add(c)\n", | ||
" session.commit()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "higher-short", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can verify the data was indeed imported:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "suspended-albert", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"- Cuds object:\n", | ||
" uid: 03015cb9-f88c-4ab1-9df9-bb52743b99de\n", | ||
" type: city.CityWrapper\n", | ||
" superclasses: city.CityWrapper, cuba.Entity, cuba.Wrapper\n", | ||
" description: \n", | ||
" To Be Determined\n", | ||
"\n", | ||
" |_Relationship city.hasPart:\n", | ||
" - city.City cuds object named <Freiburg>:\n", | ||
" . uid: 72595bc4-1b68-46a3-97e9-8f3de2650f2c\n", | ||
" . coordinates: [47 7]\n", | ||
" . |_Relationship city.hasInhabitant:\n", | ||
" . - city.Citizen cuds object named <Anne>:\n", | ||
" . . uid: 92a00459-0927-438c-a305-a26512ac7f03\n", | ||
" . . age: 25\n", | ||
" . - city.Citizen cuds object named <Peter>:\n", | ||
" . uid: 27d1e83b-4ee9-4f4f-adb5-0b01a3cc2c1b\n", | ||
" . age: 25\n", | ||
" - city.City cuds object named <Freiburg>:\n", | ||
" . uid: 47398674-720b-4765-9047-b5351ed175c0\n", | ||
" . coordinates: [47 7]\n", | ||
" . |_Relationship city.hasInhabitant:\n", | ||
" . - city.Citizen cuds object named <Anne>:\n", | ||
" . . uid: 98f8ac8c-713d-4406-bc36-e0152f9e2ea3\n", | ||
" . . age: 25\n", | ||
" . - city.Citizen cuds object named <Peter>:\n", | ||
" . uid: 8a90e2b3-7cca-4103-9eba-aab55e5903b1\n", | ||
" . age: 25\n", | ||
" - city.City cuds object named <Freiburg>:\n", | ||
" uid: d886f8ce-1326-40f5-a98b-c4c893b8c085\n", | ||
" coordinates: [47 7]\n", | ||
" |_Relationship city.hasInhabitant:\n", | ||
" - city.Citizen cuds object named <Anne>:\n", | ||
" . uid: 2b5d0a3f-81a5-4746-aab9-40adcb65e71f\n", | ||
" . age: 25\n", | ||
" - city.Citizen cuds object named <Peter>:\n", | ||
" uid: 766b320a-7e9a-43ec-a696-96b4f9ee494d\n", | ||
" age: 25\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from osp.core.utils import pretty_print\n", | ||
"\n", | ||
"with SqliteSession(\"test.db\") as session:\n", | ||
" wrapper = city.CityWrapper(session=session)\n", | ||
" pretty_print(wrapper) " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "virgin-river", | ||
"metadata": {}, | ||
"source": [ | ||
"<div class=\"admonition important\">\n", | ||
"<div class=\"admonition-title\" style=\"font-weight: bold\"><div style=\"display: inline-block\">Notes</div></div>\n", | ||
" \n", | ||
"1. The format is automatically inferred from the file extension. To specify it explicitly, you can add the `format` parameter, like so: `import_cuds('./data.ttl', format='turtle')`.\n", | ||
"1. The `session` parameter is optional and inferred automatically from the context that created by the `with` statement (see the [tutorial on multiple wrappers](./multiple_wrappers.html) for more information). You can specify the session explicitly like so: `import_cuds('./data.ttl', session=session)`.\n", | ||
" \n", | ||
"</div>" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3.7.4 64-bit", | ||
"language": "python", | ||
"name": "python37464bit7e5bfc198a4544d1be12f13215aed90d" | ||
}, | ||
"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.5" | ||
}, | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "301cd6007de04cbbf15bca26f0bc1cb48004d089278091d760363de622bdd0c8" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.