Skip to content

Commit

Permalink
Merge pull request #159 from simphony/dev
Browse files Browse the repository at this point in the history
Merge sprint #3.
  • Loading branch information
kysrpex authored May 31, 2021
2 parents 7482140 + d163205 commit df8526e
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 24 deletions.
17 changes: 3 additions & 14 deletions docs/source/api_ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,7 @@ This document is for developers and/or advanced users of OSP-core, it contains a

## Utilities
```eval_rst
.. autoclass:: osp.core.utils.cuds2dot.Cuds2dot
:members:
.. automodule:: osp.core.utils.general
:members:
.. automodule:: osp.core.utils.pretty_print
:members:
.. automodule:: osp.core.utils.simple_search
:members:
.. automodule:: osp.core.utils.wrapper_development
:members:
.. automodule:: osp.core.utils
:imported-members:
:members:
```
2 changes: 1 addition & 1 deletion docs/source/fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ CUDS, or Common Universal Data Structure, is the ontology compliant data format
- **CUDS is an ontology individual**: each CUDS object is an instantiation of a class in the ontology.
If we assume a food ontology that describes classes like pizza or pasta, a CUDS object could represent one specific pizza or pasta dish, that exists in the real world.
Similar to ontology individuals, CUDS objects can be related with other individuals/CUDS by relations defined in the ontology. Like a _pizza_ that 'hasPart' _tomato sauce_
- **CUDS is API**: To allow users to interact with the ontology individuals and their data, CUDS provide a CRUD API.
- **CUDS is API**: To allow users to interact with the ontology individuals and their data, CUDS provides a CRUD API.
- **CUDS is a container**: Depending on the relationship connecting two CUDS objects, a certain instance can be seen as a container of other instances.
We call a relationship that express containment an 'active relationship'.
In the pizza example, 'hasPart' would be an 'active relationship'. If one would like to share the pizza CUDS object with others, one would like to share also the tomato sauce.
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ SimPhoNy is an ontology-based [open-source](./license.md) Python framework that
jupyter/sessions_and_vars.ipynb
utils.md
jupyter/multiple_wrappers.ipynb
jupyter/import_export.ipynb
jupyter/simlammps.ipynb
jupyter/quantum_espresso.ipynb
Expand Down
319 changes: 319 additions & 0 deletions docs/source/jupyter/import_export.ipynb
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
}
22 changes: 22 additions & 0 deletions docs/source/jupyter/ontology_interface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@
"- When the keyword `reference_by_label` is set to `False` (disabled) or not set, the dot notation is a shorthand for fetching by suffix instead. This keyword is **disabled** in the `city` namespace."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get a list of all the entities available within a namespace, run `list(namespace)`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div style=\"background: #D3D3D3\">\n",
"<div class=\"admonition important\" style=\"padding: 6px 12px\">\n",
"<div class=\"admonition-title\" style=\"font-weight: bold\"><div style=\"display: inline-block\">Tip</div></div>\n",
" \n",
"The dot notation supports IPython autocompletion. For example, when working on a Jupyter notebook, once the namespace has been imported, it is possible to get suggestions for the entity names by writing <code class=\"pre\">`namespace.`</code> and pressing TAB.\n",
"\n",
"</div>\n",
"</div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
3 changes: 2 additions & 1 deletion docs/source/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ The following table describes the compatibilities between of SimPhoNy docs and O
============= ==========
SimPhoNy docs OSP-core
============= ==========
2.4.x 3.5.2-beta
2.4.1 3.5.3.1-beta
2.4.0 3.5.2-beta
2.3.x 3.4.0-beta
2.2.x 3.3.5-beta
2.1.x 3.3.0-beta
Expand Down
Loading

0 comments on commit df8526e

Please sign in to comment.