Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions docs/shared/supportive_tooling/example-01.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,23 @@
"source": [
"# Install required libraries\n",
"!pip install uv\n",
"!uv pip install openminds\n",
"!uv pip install pandas"
"!uv pip install openminds"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24a7d367-fa86-4a80-b14a-5da4a0bd8ccd",
"metadata": {},
"outputs": [],
"source": [
"# Import dependencies\n",
"import csv\n",
"from itertools import islice\n",
"import json\n",
"\n",
"import openminds.latest.core as omcore\n",
"from openminds import Collection"
]
},
{
Expand All @@ -42,10 +57,25 @@
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"persons_path = '../../_static/test-data/persons.csv'\n",
"df = pd.read_csv(persons_path, sep=',')\n",
"df.head(4)"
"\n",
"# Read CSV file\n",
"with open(persons_path, newline='', encoding='utf-8') as csvfile:\n",
" reader = csv.reader(csvfile)\n",
" headers = next(reader)\n",
" rows = list(islice(reader, 4)) # read only first 4 rows\n",
"\n",
"# Compute columns width\n",
"col_widths = [max(len(str(x)) for x in col) for col in zip(headers, *rows)]\n",
"\n",
"# Print header\n",
"header_row = \" | \".join(f\"{h:<{w}}\" for h, w in zip(headers, col_widths))\n",
"print(header_row)\n",
"print(\"-+-\".join(\"-\" * w for w in col_widths))\n",
"\n",
"# Print data rows\n",
"for row in rows:\n",
" print(\" | \".join(f\"{c:<{w}}\" for c, w in zip(row, col_widths)))"
]
},
{
Expand All @@ -65,9 +95,7 @@
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"# read csv file to a list of dictionaries\n",
"# Read csv file to a list of dictionaries\n",
"with open(persons_path, 'r') as f:\n",
" csv_reader = csv.DictReader(f)\n",
" data = [row for row in csv_reader]\n",
Expand Down Expand Up @@ -103,9 +131,7 @@
"metadata": {},
"outputs": [],
"source": [
"import openminds.latest.core as omcore\n",
"\n",
"# extract data to create dictionary with unique \"Consortium\" instances\n",
"# Extract data to create dictionary with unique \"Consortium\" instances\n",
"consortia = {}\n",
"for d in data:\n",
" if d['memberOf'] not in consortia:\n",
Expand All @@ -114,7 +140,7 @@
" full_name = d['memberOf']\n",
" )\n",
"\n",
"# extract data to create dictionary with \"ContactInformation\" instances\n",
"# Extract data to create dictionary with \"ContactInformation\" instances\n",
"contacts = {}\n",
"for d in data:\n",
" if d['email'] not in contacts and d['email'] != '':\n",
Expand All @@ -123,7 +149,7 @@
" email = d['email']\n",
" )\n",
"\n",
"# extract data to create dictionary with \"Person\" instances where each \"Person\" instance\n",
"# Extract data to create dictionary with \"Person\" instances where each \"Person\" instance\n",
"# will link to their respective \"ContactInformation\" instance\n",
"# embed an \"Affiliation\" instance that links to the respective \"Consortium\" instance\n",
"persons = []\n",
Expand Down Expand Up @@ -154,10 +180,9 @@
"metadata": {},
"outputs": [],
"source": [
"# adding instances to collection\n",
"# Adding instances to collection\n",
"# we only need to add the \"Person\" instances, because ...\n",
"# linked instances are added to the collection automatically\n",
"from openminds import Collection\n",
"\n",
"# Create an empty metadata collection\n",
"collection = Collection()\n",
Expand Down Expand Up @@ -186,8 +211,6 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"# Open and read the JSON file\n",
"with open('my_collection.jsonld', 'r') as file:\n",
" my_collection = json.load(file)\n",
Expand Down