|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import os\n", |
| 10 | + "import urllib.request\n", |
| 11 | + "from os import path\n", |
| 12 | + "from pathlib import Path\n", |
| 13 | + "\n", |
| 14 | + "import numpy as np\n", |
| 15 | + "from PIL import Image\n", |
| 16 | + "\n", |
| 17 | + "from myoquant.common_func import (\n", |
| 18 | + " load_cellpose,\n", |
| 19 | + " load_sdh_model,\n", |
| 20 | + " load_stardist,\n", |
| 21 | + " run_cellpose,\n", |
| 22 | + " run_stardist,\n", |
| 23 | + ")\n", |
| 24 | + "from myoquant.HE_analysis import run_he_analysis\n", |
| 25 | + "from myoquant.SDH_analysis import run_sdh_analysis\n", |
| 26 | + "\n", |
| 27 | + "try:\n", |
| 28 | + " from imageio.v2 import imread\n", |
| 29 | + "except:\n", |
| 30 | + " from imageio import imread" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "# ANALYSE SDH\n", |
| 40 | + "\n", |
| 41 | + "# Importer l'image et dossier de sauvegarde\n", |
| 42 | + "image_path = \"LE CHEMIN VERS L'IMAGE SDH A ANALYSER\"\n", |
| 43 | + "output_path = image_path.parents[0]\n", |
| 44 | + "Path(output_path).mkdir(parents=True, exist_ok=True)\n", |
| 45 | + "\n", |
| 46 | + "# Télécharger le modèle SDH\n", |
| 47 | + "model_path_abs = Path(os.path.abspath(__file__)).parents[0] / \"model.h5\"\n", |
| 48 | + "if not path.exists(model_path_abs):\n", |
| 49 | + " urllib.request.urlretrieve(\n", |
| 50 | + " \"https://lbgi.fr/~meyer/SDH_models/model.h5\",\n", |
| 51 | + " model_path_abs,\n", |
| 52 | + " )\n", |
| 53 | + "model_path = model_path_abs\n", |
| 54 | + "\n", |
| 55 | + "# Charger Cellpose\n", |
| 56 | + "model_cellpose = load_cellpose()\n", |
| 57 | + "# Charger l'image\n", |
| 58 | + "image_ndarray_sdh = imread(image_path)\n", |
| 59 | + "\n", |
| 60 | + "# Faire tourner CellPose sur l'image & sauvegarder\n", |
| 61 | + "mask_cellpose = run_cellpose(image_ndarray_sdh, model_cellpose)\n", |
| 62 | + "mask_cellpose = mask_cellpose.astype(np.uint16)\n", |
| 63 | + "cellpose_mask_filename = image_path.stem + \"_cellpose_mask.tiff\"\n", |
| 64 | + "Image.fromarray(mask_cellpose).save(output_path / cellpose_mask_filename)\n", |
| 65 | + "\n", |
| 66 | + "# Faire tourner le modèle SDH sur l'image et récupérer le tableau de sommaire (result_df)\n", |
| 67 | + "model_SDH = load_sdh_model(model_path)\n", |
| 68 | + "result_df, full_label_map = run_sdh_analysis(\n", |
| 69 | + " image_ndarray_sdh, model_SDH, mask_cellpose\n", |
| 70 | + ")\n", |
| 71 | + "csv_name = image_path.stem + \"_results.csv\"\n", |
| 72 | + "result_df.to_csv(\n", |
| 73 | + " output_path / csv_name,\n", |
| 74 | + " index=False,\n", |
| 75 | + ")\n", |
| 76 | + "label_map_name = image_path.stem + \"_label_map.tiff\"\n", |
| 77 | + "Image.fromarray(full_label_map).save(output_path / label_map_name)" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "# ANALYSE HE\n", |
| 87 | + "\n", |
| 88 | + "# Importer l'image et dossier de sauvegarde\n", |
| 89 | + "image_path = \"LE CHEMIN VERS L'IMAGE HE A ANALYSER\"\n", |
| 90 | + "output_path = image_path.parents[0]\n", |
| 91 | + "Path(output_path).mkdir(parents=True, exist_ok=True)\n", |
| 92 | + "\n", |
| 93 | + "# Charger les modèles CellPose et Stardist\n", |
| 94 | + "model_cellpose = load_cellpose()\n", |
| 95 | + "model_stardist = load_stardist()\n", |
| 96 | + "# Charger l'image\n", |
| 97 | + "image_ndarray = imread(image_path)\n", |
| 98 | + "# Faire tourner Cellpose (fibres) puis sauvegarder l'image\n", |
| 99 | + "mask_cellpose = run_cellpose(image_ndarray, model_cellpose)\n", |
| 100 | + "mask_cellpose = mask_cellpose.astype(np.uint16)\n", |
| 101 | + "cellpose_mask_filename = image_path.stem + \"_cellpose_mask.tiff\"\n", |
| 102 | + "Image.fromarray(mask_cellpose).save(output_path / cellpose_mask_filename)\n", |
| 103 | + "# Faire tourner Stardist (noyaux) puis sauvegarder l'image\n", |
| 104 | + "mask_stardist = run_stardist(image_ndarray, model_stardist)\n", |
| 105 | + "mask_stardist = mask_stardist.astype(np.uint16)\n", |
| 106 | + "stardist_mask_filename = image_path.stem + \"_stardist_mask.tiff\"\n", |
| 107 | + "Image.fromarray(mask_stardist).save(output_path / stardist_mask_filename)\n", |
| 108 | + "# Analyser la position des noyaux et fibres et sauvegarder le tableau de sommaire (result_df)\n", |
| 109 | + "result_df, full_label_map = run_he_analysis(image_ndarray, mask_cellpose, mask_stardist)\n", |
| 110 | + "csv_name = image_path.stem + \"_results.csv\"\n", |
| 111 | + "result_df.to_csv(\n", |
| 112 | + " output_path / csv_name,\n", |
| 113 | + " index=False,\n", |
| 114 | + ")\n", |
| 115 | + "label_map_name = image_path.stem + \"_label_map.tiff\"\n", |
| 116 | + "Image.fromarray(full_label_map).save(output_path / label_map_name)" |
| 117 | + ] |
| 118 | + } |
| 119 | + ], |
| 120 | + "metadata": { |
| 121 | + "kernelspec": { |
| 122 | + "display_name": "Python 3.9.12 ('myoquant-WpXbYFOG-py3.9')", |
| 123 | + "language": "python", |
| 124 | + "name": "python3" |
| 125 | + }, |
| 126 | + "language_info": { |
| 127 | + "name": "python", |
| 128 | + "version": "3.9.12" |
| 129 | + }, |
| 130 | + "orig_nbformat": 4, |
| 131 | + "vscode": { |
| 132 | + "interpreter": { |
| 133 | + "hash": "d376be9f186256918b4977b6bd310794ac5a9b1babe0c9318e787478ad5da552" |
| 134 | + } |
| 135 | + } |
| 136 | + }, |
| 137 | + "nbformat": 4, |
| 138 | + "nbformat_minor": 2 |
| 139 | +} |
0 commit comments