{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Basic example on how to use OMERO Python language bindings\n", "See https://docs.openmicroscopy.org/latest/omero/developers/Python.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Packages required to connect to OMERO" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from omero.gateway import BlitzGateway\n", "from getpass import getpass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a connection to an OMERO server" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Username: trainer-1\n", "OMERO Password: ········\n", "Connected as trainer-1\n" ] } ], "source": [ "def connect(hostname, username, password):\n", " \"\"\"\n", " Connect to an OMERO server\n", " :param hostname: Host name\n", " :param username: User\n", " :param password: Password\n", " :return: Connected BlitzGateway\n", " \"\"\"\n", " conn = BlitzGateway(username, password,\n", " host=hostname, secure=True)\n", " conn.connect()\n", " conn.c.enableKeepAlive(60)\n", " return conn\n", "\n", "HOST = 'wss://workshop.openmicroscopy.org/omero-ws'\n", "conn = connect(HOST, input(\"Username: \"),\n", " getpass(\"OMERO Password: \"))\n", "print(\"Connected as {}\".format(conn.getUser().getName()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load images in a specified dataset method" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataset ID: 2391\n", "---- Processing image 28662\n", "---- Processing image 57226\n", "---- Processing image 28663\n", "---- Processing image 57227\n", "---- Processing image 28664\n", "---- Processing image 57228\n", "---- Processing image 28665\n", "---- Processing image 28666\n", "---- Processing image 28667\n", "---- Processing image 28668\n", "---- Processing image 57229\n", "---- Processing image 28669\n", "---- Processing image 57230\n", "---- Processing image 28670\n", "---- Processing image 57231\n", "---- Processing image 28671\n", "---- Processing image 28672\n", "---- Processing image 28673\n", "---- Processing image 28674\n", "---- Processing image 28675\n", "object #0 (::omero::RLong)\n", "{\n", " _val = 28662\n", "}\n" ] } ], "source": [ "def load_images(conn, dataset_id):\n", " \"\"\"\n", " Load the images in the specified dataset\n", " :param conn: The BlitzGateway\n", " :param dataset_id: The dataset's id\n", " :return: The Images or None\n", " \"\"\"\n", " dataset = conn.getObject(\"Dataset\", dataset_id)\n", " images = []\n", " for image in dataset.listChildren():\n", " images.append(image)\n", " if len(images) == 0:\n", " return None\n", "\n", " for image in images:\n", " print(\"---- Processing image\", image.id)\n", " return images\n", "\n", "images = load_images(conn, input(\"Dataset ID: \"))\n", "print(images[0]._obj.id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Close the connection to the OMERO server" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conn.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### License\n", "Copyright (C) 2019-2020 University of Dundee. All Rights Reserved.\n", "This program is free software; you can redistribute it and/or modify it\n", "under the terms of the GNU General Public License as published by the\n", "Free Software Foundation; either version 2 of the License, or\n", "(at your option) any later version.\n", "This program is distributed in the hope that it will be useful, but\n", "WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n", "or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\n", "more details. You should have received a copy of the GNU General\n", "Public License along with this program; if not, write to the\n", "Free Software Foundation,\n", "Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }