{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:19.036357Z", "start_time": "2019-09-23T18:50:19.031896Z" } }, "source": [ "# Resources\n", "\n", "A [Resource](https://nexus-forge.readthedocs.io/en/latest/interaction.html#resource) is the base data exchange object in Nexus Forge. It is an identifiable data object with potentially a set of properties as metadata. This notebook shows how to create a resource from keyword arguments, JSON dictionary, or pandas dataframe." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.068658Z", "start_time": "2019-09-23T18:50:19.054054Z" } }, "outputs": [], "source": [ "from kgforge.core import KnowledgeGraphForge" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook [00-Initialization.ipynb](00%20-%20Initialization.ipynb)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/forge.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.127987Z", "start_time": "2019-09-23T18:50:20.119390Z" } }, "outputs": [], "source": [ "from kgforge.core import Resource" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creation\n", "\n", "It is possible to assign arbitrary properties to create a resource, and link them to other resources via properties." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From keyword arguments" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.143688Z", "start_time": "2019-09-23T18:50:20.134694Z" } }, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.154752Z", "start_time": "2019-09-23T18:50:20.146122Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " type: Association\n", " agent:\n", " {\n", " type: Person\n", " name: Jane Doe\n", " }\n", "}\n" ] } ], "source": [ "association = Resource(type=\"Association\", agent=jane)\n", "print(association)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### from a Json dictionnary" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " type: Association\n", " agent:\n", " {\n", " type: Person\n", " name: Jane Doe\n", " }\n", "}\n" ] } ], "source": [ "association_json = {\n", " \"type\" : \"Association\",\n", " \"agent\": jane\n", "}\n", "association = Resource.from_json(association_json)\n", "print(association)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### JSON keys with specific values can be excluded" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://id\n", " type: Association\n", " agent:\n", " {\n", " type: Person\n", " email: jane.doe@epfl.ch\n", " name: Jane Doe\n", " }\n", "}\n" ] } ], "source": [ "import numpy as np\n", "association_json = {\n", " \"id\": \"https://id\",\n", " \"type\" : \"Association\",\n", " \"nanValue\": np.nan,\n", " \"agent\": jane\n", "}\n", "association = Resource.from_json(association_json, na=np.nan)\n", "print(association)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### from pandas.DataFrame" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
typename
0PersonMarie Curie
1PersonAlbert Einstein
\n", "
" ], "text/plain": [ " type name\n", "0 Person Marie Curie\n", "1 Person Albert Einstein" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "{\n", " type: Person\n", " name: Marie Curie\n", "}\n", "{\n", " type: Person\n", " name: Albert Einstein\n", "}\n" ] } ], "source": [ "import pandas as pd\n", "dataframe = pd.read_csv(\"../../data/persons.csv\")\n", "display(dataframe)\n", "resources = forge.from_dataframe(dataframe)\n", "print(*resources, sep=\"\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### modification" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.261616Z", "start_time": "2019-09-23T18:50:20.255355Z" } }, "outputs": [], "source": [ "jane.email = \"jane.doe@epfl.ch\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### access" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.187035Z", "start_time": "2019-09-23T18:50:20.165715Z" } }, "outputs": [ { "data": { "text/plain": [ "'jane.doe@epfl.ch'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jane.email" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.207733Z", "start_time": "2019-09-23T18:50:20.199854Z" } }, "outputs": [ { "data": { "text/plain": [ "'jane.doe@epfl.ch'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "association.agent.email" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, 'id')" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "association.has_identifier(return_attribute=True)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://id'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "association.get_identifier()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, 'type')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "association.has_type(return_attribute=True)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Association'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "association.get_type()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " type: Person\n", " email: jane.doe@epfl.ch\n", " name: Jane Doe\n", "}\n" ] } ], "source": [ "print(jane)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.332705Z", "start_time": "2019-09-23T18:50:20.325401Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://id\n", " type: Association\n", " agent:\n", " {\n", " type: Person\n", " email: jane.doe@epfl.ch\n", " name: Jane Doe\n", " }\n", "}\n" ] } ], "source": [ "print(association)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7 (nexusforgelatest)", "language": "python", "name": "nexusforgelatest" }, "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.0" } }, "nbformat": 4, "nbformat_minor": 4 }