Skip to content

Commit

Permalink
deploy: b966a34
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-mueller committed Aug 30, 2024
1 parent 904f946 commit f364a6c
Show file tree
Hide file tree
Showing 15 changed files with 1,362 additions and 9 deletions.
Binary file added _images/kv_pairs.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/kv_pairs_edited.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions _sources/notebooks/metadata/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Metadata

Metadata is a central concept for the entirety of working with OMERo. This section will introduce some ways to interact with metadata in OMERO.
257 changes: 257 additions & 0 deletions _sources/notebooks/metadata/key_value_pairs.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reading, writing and editing key-value pairs\n",
"\n",
"Key value pairs are a key (pun intended) part of annotating data in OMERO in a very flexible way. In essence, they provide a way of adding Python dictionaries to an image, a project or a dataset - and a Python dictionary is fit for data of virtually any form. Here are a few ways of interacting with data in this form through Python."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"import ezomero\n",
"from omero.rtypes import rstring, rlong"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"host = 'omero-int.biotec.tu-dresden.de'\n",
"user = 'johamuel' # replace this with your username\n",
"secure = True\n",
"port = 4064\n",
"group = 'default'\n",
"\n",
"conn = ezomero.connect(host=host, user=user, secure=secure, port=port, group=group)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Adding key value pairs\n",
"\n",
"Let's add some kv pairs from a Python dictionary. In the OMERO terminology, key value pairs are referred to as \"map annotations\", so that's the function we'll need to use:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"kv_pairs = {'Age': 35, 'Patient name': 'John Doe'}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object group 3\n",
"8257\n"
]
}
],
"source": [
"map_annotation_id = ezomero.post_map_annotation(\n",
" conn,\n",
" object_type='Image',\n",
" object_id=330,\n",
" kv_dict=kv_pairs,\n",
" ns='openmicroscopy.org/omero/client/mapAnnotation')\n",
"print(map_annotation_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you check out your image on OMERo, you see the posted key-value pairs:\n",
"\n",
"![kv-pairs](./imgs/kv_pairs.PNG)\n",
"\n",
"Let's quickly talk about the elephant the room: What exactly is the `ns` argument of the `post_map_annotation` function? It stands for \"namespace\" and is a way of grouping key-value pairs. If you have a lot of key-value pairs, you might want to group them into different namespaces to keep things organized. If you don't care about namespaces, you can just use an empty string. The default namespace is `'openmicroscopy.org/omero/client/mapAnnotation'`. \n",
"\n",
"```{note}\n",
"If you set the namespace to a value different from 'openmicroscopy.org/omero/client/mapAnnotation', you will not be able to edit the key-value pairs in the OMERO web interface.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reading a key-value pair from OMERO\n",
"\n",
"Let's now do it the other way round: read a key-value pair from OMERO. For this, we need a two-step process: We first need to retrieve all map annotations of a single image (there can be more than one) and then we can access the key-value pairs of the individual annotations."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8257]\n"
]
}
],
"source": [
"map_annotation_ids = ezomero.get_map_annotation_ids(conn, object_type='Image', object_id=330)\n",
"print(map_annotation_ids)a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at the first (and only) map annotation we just retrieved:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Age': '35', 'Patient name': 'John Doe'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kv_pairs = ezomero.get_map_annotation(conn, map_ann_id=map_annotation_ids[0])\n",
"kv_pairs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Edit key-value pairs\n",
"\n",
"Lastly, let's say we don't want to read or write a kv pairs, but we really just want to edit a particular entry of a key-value dictionary. We start just as before by getting the map_annotation_id of the particular map annotation we are interested in."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8257]\n"
]
}
],
"source": [
"map_annotation_ids = ezomero.get_map_annotation_ids(conn, object_type='Image', object_id=330)\n",
"print(map_annotation_ids)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we retrieve the map annotation as an omero object:"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"mapAnnotation = conn.getObject(obj_type='MapAnnotation', oid=map_annotation_ids[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the new dictoinary we want to save.\n",
"\n",
"```{warning}\n",
"Setting a key-value pair like this will overwrite the entire map annotation. If you want to keep the existing key-value pairs, you will need to read the existing key-value pairs first and then update or append the dictionary.\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"kv_pairs = [\n",
" ('Age', '36'),\n",
" ('Patient name', 'John Doe2')\n",
" ] # Note: the value should be a string - no integers allowed here"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"# Save the updated annotation\n",
"mapAnnotation.setValue(kv_pairs)\n",
"conn.getUpdateService().saveObject(mapAnnotation._obj)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's your result:\n",
"\n",
"![kv-pairs](./imgs/kv_pairs_edited.PNG)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "stress",
"language": "python",
"name": "python3"
},
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 0 additions & 1 deletion _sources/notebooks/read_and_write/read_and_write.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"outputs": [],
"source": [
"import ezomero\n",
"from omero.plugins.tag import TagControl\n",
"import numpy as np\n",
"from skimage import io\n",
"import matplotlib.pyplot as plt"
Expand Down
4 changes: 4 additions & 0 deletions genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="notebooks/read_and_write/read_and_write.html">Read and write data to OMERO from Python</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="notebooks/metadata/Readme.html">Metadata</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="notebooks/metadata/key_value_pairs.html">Reading, writing and editing key-value pairs</a></li>
</ul>
</details></li>
<li class="toctree-l1"><a class="reference internal" href="instructions/transfer_dataset.html">How to transfer a dataset between two omero instances</a></li>
<li class="toctree-l1"><a class="reference internal" href="notebooks/synchronize_tags.html">Synchronize tags across groups</a></li>

Expand Down
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="notebooks/read_and_write/read_and_write.html">Read and write data to OMERO from Python</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="notebooks/metadata/Readme.html">Metadata</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="notebooks/metadata/key_value_pairs.html">Reading, writing and editing key-value pairs</a></li>
</ul>
</details></li>
<li class="toctree-l1"><a class="reference internal" href="instructions/transfer_dataset.html">How to transfer a dataset between two omero instances</a></li>
<li class="toctree-l1"><a class="reference internal" href="notebooks/synchronize_tags.html">Synchronize tags across groups</a></li>

Expand Down
10 changes: 7 additions & 3 deletions instructions/transfer_dataset.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Synchronize tags across groups" href="../notebooks/synchronize_tags.html" />
<link rel="prev" title="Read and write data to OMERO from Python" href="../notebooks/read_and_write/read_and_write.html" />
<link rel="prev" title="Reading, writing and editing key-value pairs" href="../notebooks/metadata/key_value_pairs.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
Expand Down Expand Up @@ -181,6 +181,10 @@
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../notebooks/read_and_write/read_and_write.html">Read and write data to OMERO from Python</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../notebooks/metadata/Readme.html">Metadata</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../notebooks/metadata/key_value_pairs.html">Reading, writing and editing key-value pairs</a></li>
</ul>
</details></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">How to transfer a dataset between two omero instances</a></li>
<li class="toctree-l1"><a class="reference internal" href="../notebooks/synchronize_tags.html">Synchronize tags across groups</a></li>

Expand Down Expand Up @@ -413,12 +417,12 @@ <h2>Transfer the data<a class="headerlink" href="#transfer-the-data" title="Link

<div class="prev-next-area">
<a class="left-prev"
href="../notebooks/read_and_write/read_and_write.html"
href="../notebooks/metadata/key_value_pairs.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Read and write data to OMERO from Python</p>
<p class="prev-next-title">Reading, writing and editing key-value pairs</p>
</div>
</a>
<a class="right-next"
Expand Down
Loading

0 comments on commit f364a6c

Please sign in to comment.