From 08798947e9df7106c1e6ba99ae74e6ca9ce2ec85 Mon Sep 17 00:00:00 2001 From: rraadd88 Date: Tue, 28 Nov 2023 21:12:42 -0500 Subject: [PATCH 1/2] added: scripted notebook --- notebooks/run_cellpose_script.ipynb | 185 ++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 notebooks/run_cellpose_script.ipynb diff --git a/notebooks/run_cellpose_script.ipynb b/notebooks/run_cellpose_script.ipynb new file mode 100644 index 00000000..9c84a3de --- /dev/null +++ b/notebooks/run_cellpose_script.ipynb @@ -0,0 +1,185 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Script\n", + "\n", + "Compatible with [`papermill`](https://github.com/nteract/papermill).\n", + "Based on [`run_cellpose.ipynb`](./run_cellpose.ipynb)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "#!pip install matplotlib\n", + "import numpy as np\n", + "from cellpose import models" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "parameters" + ] + }, + "outputs": [], + "source": [ + "## parameters\n", + "input_path=None\n", + "output_path=None\n", + "\n", + "gpu=False\n", + "model_type='cyto' # model_type='cyto' or model_type='nuclei'\n", + "kws_eval={}\n", + "plot=True" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "if not input_path.endswith('.npy'):\n", + " img = io.imread(input_path)\n", + "else:\n", + " img = np.load(input_path)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Prediction" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# DEFINE CELLPOSE MODEL\n", + "model = models.Cellpose(gpu=gpu, model_type=model_type)\n", + "masks, flows, styles, diams = model.eval(img, diameter=None, **kws_eval)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if output_path is None:\n", + " from cellpose import io\n", + " # save results so you can load in gui\n", + " io.masks_flows_to_seg(img, masks, flows, diams, input_path)\n", + " # save results as png\n", + " io.save_to_png(img, masks, flows, input_path)\n", + "elif output_path.endswith('.npy'):\n", + " np.save(\n", + " output_path,\n", + " masks,\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Visualization" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "if plot:\n", + " import matplotlib.pyplot as plt\n", + " from cellpose import plot \n", + " fig = plt.figure(figsize=(12,5))\n", + " plot.show_segmentation(fig, img, masks, flows[0])\n", + " plt.tight_layout()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "cellpose", + "language": "python", + "name": "cellpose" + }, + "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.8.18" + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From c2643af9cd954dc3dfd69501249b48ca3ae8b514 Mon Sep 17 00:00:00 2001 From: rraadd88 Date: Sun, 26 May 2024 14:06:43 -0400 Subject: [PATCH 2/2] updates --- notebooks/run_cellpose_script.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebooks/run_cellpose_script.ipynb b/notebooks/run_cellpose_script.ipynb index 9c84a3de..d5674063 100644 --- a/notebooks/run_cellpose_script.ipynb +++ b/notebooks/run_cellpose_script.ipynb @@ -61,7 +61,7 @@ "if not input_path.endswith('.npy'):\n", " img = io.imread(input_path)\n", "else:\n", - " img = np.load(input_path)\n" + " img = np.load(input_path)" ] }, {