|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Using FISSA with SIMA\n", |
| 8 | + "\n", |
| 9 | + "[SIMA](http://www.losonczylab.org/sima/) is a toolbox for motion correction and cell detection.\n", |
| 10 | + "Here we illustrate how to create a workflow which uses SIMA to detect cells and FISSA to extract decontaminated signals from those cells.\n", |
| 11 | + "\n", |
| 12 | + "**Reference:**\n", |
| 13 | + "Kaifosh, P., Zaremba, J. D., Danielson, N. B., Losonczy, A. SIMA: Python software for analysis of dynamic fluorescence imaging data. *Frontiers in neuroinformatics*, **8**(80), 2014. doi: [10.3389/fninf.2014.00080](https://doi.org/10.3389/fninf.2014.00080).\n", |
| 14 | + "\n", |
| 15 | + "Please note that SIMA only supports Python 3.6 and below." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "### Import packages" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": null, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "# FISSA toolbox\n", |
| 32 | + "import fissa\n", |
| 33 | + "\n", |
| 34 | + "# SIMA toolbox\n", |
| 35 | + "import sima\n", |
| 36 | + "import sima.segment\n", |
| 37 | + "\n", |
| 38 | + "# File operations\n", |
| 39 | + "import glob\n", |
| 40 | + "\n", |
| 41 | + "# For plotting our results, use numpy and matplotlib\n", |
| 42 | + "import matplotlib.pyplot as plt\n", |
| 43 | + "import numpy as np" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "## Detecting cells with SIMA" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "markdown", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "### Setup data" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "# Define folder where tiffs are present\n", |
| 67 | + "tiff_folder = \"exampleData/20150529/\"\n", |
| 68 | + "\n", |
| 69 | + "# Find tiffs in folder\n", |
| 70 | + "tiffs = sorted(glob.glob(tiff_folder + \"/*.tif*\"))\n", |
| 71 | + "\n", |
| 72 | + "# define motion correction method\n", |
| 73 | + "mc_approach = sima.motion.DiscreteFourier2D()\n", |
| 74 | + "\n", |
| 75 | + "# Define SIMA dataset\n", |
| 76 | + "sequences = [sima.Sequence.create(\"TIFF\", tiff) for tiff in tiffs[:1]]\n", |
| 77 | + "try:\n", |
| 78 | + " dataset = sima.ImagingDataset(sequences, \"example.sima\")\n", |
| 79 | + "except Exception:\n", |
| 80 | + " dataset = sima.ImagingDataset.load(\"example.sima\")" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "metadata": {}, |
| 86 | + "source": [ |
| 87 | + "### Run SIMA segmentation algorithm" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "stica_approach = sima.segment.STICA(components=2)\n", |
| 97 | + "stica_approach.append(sima.segment.SparseROIsFromMasks())\n", |
| 98 | + "stica_approach.append(sima.segment.SmoothROIBoundaries())\n", |
| 99 | + "stica_approach.append(sima.segment.MergeOverlapping(threshold=0.5))\n", |
| 100 | + "rois = dataset.segment(stica_approach, \"auto_ROIs\")" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "### Plot detected cells" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "# Plotting lines surrounding each of the ROIs\n", |
| 117 | + "plt.figure(figsize=(7, 6))\n", |
| 118 | + "\n", |
| 119 | + "for roi in rois:\n", |
| 120 | + " # Plot border around cell\n", |
| 121 | + " plt.plot(roi.coords[0][:, 0], roi.coords[0][:, 1])\n", |
| 122 | + "\n", |
| 123 | + "# Invert the y-axis because image co-ordinates are labelled from top-left\n", |
| 124 | + "plt.gca().invert_yaxis()\n", |
| 125 | + "plt.show()" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "markdown", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "## Extract decontaminated signals with FISSA\n", |
| 133 | + "\n", |
| 134 | + "FISSA needs either ImageJ ROIs or numpy arrays as inputs for the ROIs. \n", |
| 135 | + "\n", |
| 136 | + "SIMA outputs ROIs as numpy arrays, and can be directly read into FISSA.\n", |
| 137 | + "\n", |
| 138 | + "A given roi is given as\n", |
| 139 | + "```python\n", |
| 140 | + "rois[i].coords[0][:, :2]\n", |
| 141 | + "```\n", |
| 142 | + "\n", |
| 143 | + "FISSA expects rois to be provided as a list of lists\n", |
| 144 | + "```python\n", |
| 145 | + "[[roiA1, roiA2, roiA3, ...]]\n", |
| 146 | + "```\n", |
| 147 | + "So some formatting will need to be done first." |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [ |
| 156 | + "rois_fissa = [roi.coords[0][:, :2] for roi in rois]" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [], |
| 164 | + "source": [ |
| 165 | + "rois[0].coords[0][:, :2].shape" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "markdown", |
| 170 | + "metadata": {}, |
| 171 | + "source": [ |
| 172 | + "We can then run FISSA on the data using the ROIs supplied by SIMA having converted them to a FISSA-compatible format, `rois_fissa`." |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "output_folder = \"fissa_sima_example\"\n", |
| 182 | + "experiment = fissa.Experiment(tiff_folder, [rois_fissa], output_folder)\n", |
| 183 | + "experiment.separate()" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "metadata": {}, |
| 189 | + "source": [ |
| 190 | + "### Plotting the results" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "# Fetch the colormap object for Cynthia Brewer's Paired color scheme\n", |
| 200 | + "cmap = plt.get_cmap(\"Paired\")" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": null, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "# Select which trial (TIFF index) to plot\n", |
| 210 | + "trial = 0\n", |
| 211 | + "\n", |
| 212 | + "# Plot the mean image and ROIs from the FISSA experiment\n", |
| 213 | + "plt.figure(figsize=(7, 7))\n", |
| 214 | + "plt.imshow(experiment.means[trial], cmap=\"gray\")\n", |
| 215 | + "\n", |
| 216 | + "for i_roi in range(len(experiment.roi_polys)):\n", |
| 217 | + " # Plot border around ROI\n", |
| 218 | + " for contour in experiment.roi_polys[i_roi, trial][0]:\n", |
| 219 | + " plt.plot(\n", |
| 220 | + " contour[:, 1],\n", |
| 221 | + " contour[:, 0],\n", |
| 222 | + " color=cmap((i_roi * 2 + 1) % cmap.N),\n", |
| 223 | + " )\n", |
| 224 | + "\n", |
| 225 | + "plt.show()" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": null, |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [], |
| 233 | + "source": [ |
| 234 | + "# Plot all ROIs and trials\n", |
| 235 | + "\n", |
| 236 | + "# Get the number of ROIs and trials\n", |
| 237 | + "n_roi = experiment.result.shape[0]\n", |
| 238 | + "n_trial = experiment.result.shape[1]\n", |
| 239 | + "\n", |
| 240 | + "# Find the maximum signal intensities for each ROI\n", |
| 241 | + "roi_max_raw = [\n", |
| 242 | + " np.max([np.max(experiment.raw[i_roi, i_trial][0]) for i_trial in range(n_trial)])\n", |
| 243 | + " for i_roi in range(n_roi)\n", |
| 244 | + "]\n", |
| 245 | + "roi_max_result = [\n", |
| 246 | + " np.max([np.max(experiment.result[i_roi, i_trial][0]) for i_trial in range(n_trial)])\n", |
| 247 | + " for i_roi in range(n_roi)\n", |
| 248 | + "]\n", |
| 249 | + "roi_max = np.maximum(roi_max_raw, roi_max_result)\n", |
| 250 | + "\n", |
| 251 | + "# Plot our figure using subplot panels\n", |
| 252 | + "plt.figure(figsize=(16, 10))\n", |
| 253 | + "for i_roi in range(n_roi):\n", |
| 254 | + " for i_trial in range(n_trial):\n", |
| 255 | + " # Make subplot axes\n", |
| 256 | + " i_subplot = 1 + i_trial * n_roi + i_roi\n", |
| 257 | + " plt.subplot(n_trial, n_roi, i_subplot)\n", |
| 258 | + " # Plot the data\n", |
| 259 | + " plt.plot(\n", |
| 260 | + " experiment.raw[i_roi][i_trial][0, :],\n", |
| 261 | + " label=\"Raw (SIMA)\",\n", |
| 262 | + " color=cmap((i_roi * 2) % cmap.N),\n", |
| 263 | + " )\n", |
| 264 | + " plt.plot(\n", |
| 265 | + " experiment.result[i_roi][i_trial][0, :],\n", |
| 266 | + " label=\"FISSA\",\n", |
| 267 | + " color=cmap((i_roi * 2 + 1) % cmap.N),\n", |
| 268 | + " )\n", |
| 269 | + " # Labels and boiler plate\n", |
| 270 | + " plt.ylim([-0.05 * roi_max[i_roi], roi_max[i_roi] * 1.05])\n", |
| 271 | + " if i_roi == 0:\n", |
| 272 | + " plt.ylabel(\n", |
| 273 | + " \"Trial {}\\n\\nSignal intensity\\n(candela per unit area)\".format(\n", |
| 274 | + " i_trial + 1\n", |
| 275 | + " )\n", |
| 276 | + " )\n", |
| 277 | + " if i_trial == 0:\n", |
| 278 | + " plt.legend()\n", |
| 279 | + " plt.title(\"ROI {}\".format(i_roi))\n", |
| 280 | + " if i_trial == n_trial - 1:\n", |
| 281 | + " plt.xlabel(\"Time (frame number)\")\n", |
| 282 | + "\n", |
| 283 | + "plt.show()" |
| 284 | + ] |
| 285 | + }, |
| 286 | + { |
| 287 | + "cell_type": "markdown", |
| 288 | + "metadata": {}, |
| 289 | + "source": [ |
| 290 | + "The figure shows the raw signal from the ROI identified by SIMA (pale), and after decontaminating with FISSA (dark).\n", |
| 291 | + "The hues match the ROI locations drawn above.\n", |
| 292 | + "Each column shows the results from one of the ROIs detected by SIMA.\n", |
| 293 | + "Each row shows the results from one of the three trials." |
| 294 | + ] |
| 295 | + } |
| 296 | + ], |
| 297 | + "metadata": { |
| 298 | + "kernelspec": { |
| 299 | + "display_name": "Python", |
| 300 | + "language": "python", |
| 301 | + "name": "python" |
| 302 | + }, |
| 303 | + "language_info": { |
| 304 | + "codemirror_mode": { |
| 305 | + "name": "ipython", |
| 306 | + "version": 3 |
| 307 | + }, |
| 308 | + "file_extension": ".py", |
| 309 | + "mimetype": "text/x-python", |
| 310 | + "name": "python", |
| 311 | + "nbconvert_exporter": "python", |
| 312 | + "pygments_lexer": "ipython", |
| 313 | + "version": "3.8.2" |
| 314 | + } |
| 315 | + }, |
| 316 | + "nbformat": 4, |
| 317 | + "nbformat_minor": 1 |
| 318 | +} |
0 commit comments