{ "cells": [ { "cell_type": "markdown", "id": "8c3882b9", "metadata": {}, "source": [ "# Solutions of exercises\n", "\n", "Exercises are available in a few notebooks. Below are solutions to the exercises.\n", "How to use the notebook:\n", "\n", "* **DO NOT RUN THE NOTEBOOK**\n", "* **Copy the code snippet(s) and run the snippet(s) in the relevant notebook**\n" ] }, { "cell_type": "markdown", "id": "38dce9f9", "metadata": {}, "source": [ "## Exercises from notebook idr0062_prediction" ] }, { "cell_type": "markdown", "id": "75db4ae2", "metadata": {}, "source": [ "### Exercise 1 " ] }, { "cell_type": "code", "execution_count": null, "id": "47d19053", "metadata": {}, "outputs": [], "source": [ "# Question 1: Read the geojson file:\n", "import json\n", "with open(geojson_file) as f:\n", " d = json.load(f)\n", " values = d['features']" ] }, { "cell_type": "code", "execution_count": null, "id": "e8fa7860", "metadata": {}, "outputs": [], "source": [ "# Question 2: Overlay the shapes from a specific plane on the image\n", "z = 5\n", "ax = plt.subplot()\n", "plt.imshow(data[0, 0, z, :, :], cmap='gray')\n", "for v in values:\n", " if v[\"properties\"][\"z\"] == z:\n", " label_geojson = numpy.array(v[\"geometry\"][\"coordinates\"])\n", " p = label_geojson.squeeze()\n", " ax.plot(p[:, 0], p[:, 1], linewidth=v[\"properties\"][\"stroke-width\"], color=\"red\")\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "dddaad7f", "metadata": {}, "source": [ "### Exercise 2 \n", "\n", "In order to save the StarDist labels to an OMERO.server with write access, you will first need to log in and change the image ID." ] }, { "cell_type": "code", "execution_count": 1, "id": "e0d0c98b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "from omero.gateway import BlitzGateway\n", "from getpass import getpass\n", "# To be replaced by the desired host\n", "HOST = 'wss://workshop.openmicroscopy.org/omero-ws'\n", "conn = BlitzGateway(input(\"Username: \"),\n", " getpass(\"OMERO Password: \"),\n", " host=HOST, secure=True)\n", "conn.connect()\n", "print(conn.connect())" ] }, { "cell_type": "code", "execution_count": null, "id": "3f3f0360", "metadata": {}, "outputs": [], "source": [ "# Question 1: Convert the StarDist labels into OMERO polygons\n", "import omero\n", "from omero.rtypes import rint, rstring\n", "\n", "image_id = REPLACE_WITH_IMAGE_ID\n", "\n", "image = omero.model.ImageI(image_id, False)\n", "rois = []\n", "for i in range(len(img)):\n", " new_labels, details = model_versatile.predict_instances(img[i])\n", " # Convert into Polygon\n", " for obj_id, region in enumerate(details['coord']):\n", " roi = omero.model.RoiI()\n", " roi.setName(rstring(\"Object %s\" % obj_id))\n", " polygon = omero.model.PolygonI()\n", " x = region[1]\n", " y = region[0]\n", " points = \" \".join(\n", " [\"{},{}\".format(x[j], y[j]) for j in range(len(x))])\n", " polygon.setPoints(rstring(points))\n", " polygon.theZ = rint(i)\n", " polygon.theC = rint(0)\n", " roi.addShape(polygon)\n", " # Link the ROI and the image\n", " roi.setImage(image)\n", " rois.append(roi) " ] }, { "cell_type": "code", "execution_count": null, "id": "43eef9bb", "metadata": {}, "outputs": [], "source": [ "# Question 2: Save the ROI\n", "rois = conn.getUpdateService().saveAndReturnArray(rois)" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:stardist]", "language": "python", "name": "conda-env-stardist-py" }, "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }