{ "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", " | type | \n", "name | \n", "
---|---|---|
0 | \n", "Person | \n", "Marie Curie | \n", "
1 | \n", "Person | \n", "Albert Einstein | \n", "