Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a parameterized example notebook to simplify the usage #833

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
185 changes: 185 additions & 0 deletions notebooks/run_cellpose_script.ipynb
Original file line number Diff line number Diff line change
@@ -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)"
]
},
{
"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
}