{ "cells": [ { "cell_type": "markdown", "id": "9e52d42c-06c9-491f-92f0-e95e186064ae", "metadata": {}, "source": [ "# Usage" ] }, { "cell_type": "code", "execution_count": null, "id": "a01eba6e-3002-4db6-ade2-48cd049b18cc", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import numpy as np\n", "import scipy\n", "import pandas as pd\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import skimage\n", "import skimage.io\n", "import seaborn as sb\n", "import tifffile\n", "\n", "from nima import nima\n", "\n", "cm = plt.cm.inferno_r\n", "\n", "fp = \"../../tests/data/1b_c16_15.tif\"\n", "\n", "channels = [\"G\", \"R\", \"C\"]\n", "# dark = io.imread('/home/dati/GBM_persson/analyses/15.02.05_cal-GBM5-pBJclop/dark/dark-25_500.tif')\n", "# flat = io.imread('/home/dati/GBM_persson/analyses/15.02.05_cal-GBM5-pBJclop/flat/flat-C-dark-37bis_500.tif')" ] }, { "cell_type": "code", "execution_count": null, "id": "876ebe8d-b553-4a0c-a58d-d8f5191d4342", "metadata": {}, "outputs": [], "source": [ "d_im, n_ch, times = nima.read_tiff(fp, channels)" ] }, { "cell_type": "code", "execution_count": null, "id": "bfe6f07d-2b11-4da0-8595-6b5f156fa0e4", "metadata": {}, "outputs": [], "source": [ "d_im_bg, bgs, ff, bgv = nima.d_bg(d_im, downscale=(2, 2), kind=\"li_adaptive\")\n", "bgs" ] }, { "cell_type": "code", "execution_count": null, "id": "ea82ebfb-a555-42a2-a4c4-bb266f490f8e", "metadata": {}, "outputs": [], "source": [ "d_im_bg[\"G\"].__sizeof__()" ] }, { "cell_type": "code", "execution_count": null, "id": "519bd702-c705-4d03-b7a0-8dae8a2b1d0c", "metadata": {}, "outputs": [], "source": [ "scipy.stats.distributions.norm.fit(bgv[\"C\"][1])" ] }, { "cell_type": "code", "execution_count": null, "id": "247d0a71-c762-447a-96a2-53fe4faeb67d", "metadata": {}, "outputs": [], "source": [ "sb.histplot(bgv[\"C\"][1], kde=True, stat=\"density\")\n", "plt.yscale(\"log\")" ] }, { "cell_type": "code", "execution_count": null, "id": "7fa10c3b-f6a9-4407-8c99-4399e5a771b4", "metadata": {}, "outputs": [], "source": [ "ff[\"C\"][1][0]" ] }, { "cell_type": "code", "execution_count": null, "id": "74de7076-c9b8-48ab-b7cc-10529ea9cffe", "metadata": {}, "outputs": [], "source": [ "for i in range(4):\n", " plt.hist(bgv[\"C\"][i], histtype=\"step\", bins=32, log=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "454bf86e-1fed-4351-9999-d9c0b992c73d", "metadata": {}, "outputs": [], "source": [ "nima.d_mask_label(\n", " d_im_bg, threshold_method=\"yen\", min_size=2000, channels=channels, watershed=0\n", ")\n", "d_im_bg.keys()" ] }, { "cell_type": "code", "execution_count": null, "id": "960a55fd-496c-432e-8b6e-286d787988f7", "metadata": {}, "outputs": [], "source": [ "f = nima.d_show(d_im_bg)" ] }, { "cell_type": "code", "execution_count": null, "id": "3037b4a8-3f07-49dc-8c99-e92b96ca81d6", "metadata": {}, "outputs": [], "source": [ "meas, pr = nima.d_meas_props(d_im_bg)\n", "f = nima.d_plot_meas(bgs, meas, channels=channels)" ] }, { "cell_type": "code", "execution_count": null, "id": "dfd6fcd7-cff1-4155-bc6c-7aa40dd22862", "metadata": {}, "outputs": [], "source": [ "plt.subplot(1, 2, 1)\n", "skimage.io.imshow(d_im_bg[\"r_cl\"][2], vmin=0.0, vmax=1.1)\n", "plt.subplot(1, 2, 2)\n", "skimage.io.imshow(d_im_bg[\"r_pH\"][2], vmin=7.3, vmax=10.7)" ] }, { "cell_type": "code", "execution_count": null, "id": "a6aeb70a-cea1-42ac-8235-62985a750ba6", "metadata": {}, "outputs": [], "source": [ "c0 = d_im[\"C\"][0]\n", "g0 = d_im[\"G\"][0]\n", "r0 = d_im[\"R\"][0]\n", "tifffile.imshow(1 / c0 / c0)" ] }, { "cell_type": "code", "execution_count": null, "id": "2c6423c9-1ef3-4d56-be19-6433c99254cc", "metadata": {}, "outputs": [], "source": [ "background_channel1 = skimage.restoration.rolling_ball(c0, radius=160)\n", "tifffile.imshow(background_channel1)" ] }, { "cell_type": "code", "execution_count": null, "id": "559a45e7-826d-4ee2-934a-78f72538dd94", "metadata": {}, "outputs": [], "source": [ "plt.plot(background_channel1[200])" ] }, { "cell_type": "markdown", "id": "fc8667b7-7bdf-4d8a-8c3c-123a81d5bb88", "metadata": {}, "source": [ "## background, AF, target cells" ] }, { "cell_type": "code", "execution_count": null, "id": "6c2fb71b-b91b-4e4e-8a83-a2aa911eb148", "metadata": {}, "outputs": [], "source": [ "import aicsimageio" ] }, { "cell_type": "code", "execution_count": null, "id": "994002e1-03ff-4543-aba4-384c48ecd80d", "metadata": {}, "outputs": [], "source": [ "aicsim = aicsimageio.AICSImage(\n", " \"/home/dati/GBM_persson/data/15.02.05_cal-GBM5-pBJclop/ph633/1_20_40.tif\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "120e3d11-2e9c-4b7f-8321-31866cc7d1ad", "metadata": {}, "outputs": [], "source": [ "aicsim.ome_metadata" ] }, { "cell_type": "code", "execution_count": null, "id": "9ff46d1f-f597-4b00-9200-7616a1af6a03", "metadata": {}, "outputs": [], "source": [ "import nima_io.read as ir" ] }, { "cell_type": "code", "execution_count": null, "id": "e51c93f2-ef02-4020-a0bc-0aaae602d1ff", "metadata": {}, "outputs": [], "source": [ "md, wr = ir.read(\n", " \"/home/dati/GBM_persson/data/15.02.05_cal-GBM5-pBJclop/ph633/1_20_40.tif\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "11777e2c-c0af-4944-bda9-1b4f85867533", "metadata": {}, "outputs": [], "source": [ "md.full" ] }, { "cell_type": "code", "execution_count": null, "id": "e7135bff-cc86-482c-89fe-6bb90f0c9454", "metadata": {}, "outputs": [], "source": [ "channels3 = [\"G\", \"R\", \"C\", \"G2\"]\n", "dim3, _, _ = nima.read_tiff(\n", " \"/home/dati/GBM_persson/data/15.02.05_cal-GBM5-pBJclop/ph633/1_20_40.tif\", channels3\n", ")\n", "dim3, bgm3, _, bgv3 = nima.d_bg(dim3, downscale=(2, 2), kind=\"li_adaptive\")\n", "c = dim3[\"C\"]\n", "g = dim3[\"G\"]\n", "r = dim3[\"R\"]\n", "\n", "bc = bgv3[\"C\"][3]\n", "bg = bgv3[\"G\"][3]\n", "br = bgv3[\"R\"][3]" ] }, { "cell_type": "markdown", "id": "617c484e-7ba8-401e-beb4-52f24f1f50d9", "metadata": {}, "source": [ "c = d_im_bg['C']\n", "g = d_im_bg['G']\n", "r = d_im_bg['R']\n", "\n", "bc = bgv['C'][2]\n", "bg = bgv['G'][3]\n", "br = bgv['R'][3]" ] }, { "cell_type": "code", "execution_count": null, "id": "02dbb82f-0a74-4f52-88f3-dad62515a58e", "metadata": {}, "outputs": [], "source": [ "skimage.io.imshow(dim3[\"R\"][1])" ] }, { "cell_type": "code", "execution_count": null, "id": "96b1e56e-b6ef-4e1e-9d1d-efaba9483d2a", "metadata": {}, "outputs": [], "source": [ "plt.plot(dim3[\"R\"][1][80, 30:])" ] }, { "cell_type": "code", "execution_count": null, "id": "af3ea1ad-71ad-418d-bab3-1312e934625d", "metadata": {}, "outputs": [], "source": [ "mmin = min(len(bc), len(bg), len(br))\n", "\n", "print(\"c =\", len(bc))\n", "print(\"g =\", len(bg))\n", "print(\"r =\", len(br))\n", "\n", "plt.figure(figsize=(7.5, 6))\n", "\n", "plt.subplot(2, 2, 1)\n", "mmin = min(len(bc), len(bg))\n", "plt.hexbin(bg[:mmin], bc[:mmin], bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"green\")\n", "plt.ylabel(\"cyan\")\n", "\n", "plt.subplot(2, 2, 2)\n", "mmin = min(len(bc), len(br))\n", "plt.hexbin(br[:mmin], bc[:mmin], bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"red\")\n", "plt.ylabel(\"cyan\")\n", "\n", "ax = plt.subplot(2, 2, 4)\n", "mmin = min(len(br), len(bg))\n", "plt.hexbin(br[:mmin], bg[:mmin], bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"red\")\n", "plt.ylabel(\"green\")" ] }, { "cell_type": "code", "execution_count": null, "id": "829b94c1-0bdb-42aa-a2f3-7759eb872ace", "metadata": {}, "outputs": [], "source": [ "from mpl_toolkits.axes_grid1.inset_locator import mark_inset\n", "\n", "plt.figure(figsize=(10, 8))\n", "plt.subplot(2, 2, 1)\n", "plt.hexbin(g.ravel(), c.ravel(), bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"green\")\n", "plt.ylabel(\"cyan\")\n", "\n", "plt.subplot(2, 2, 2)\n", "plt.hexbin(r.ravel(), c.ravel(), bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"red\")\n", "plt.ylabel(\"cyan\")\n", "\n", "ax = plt.subplot(2, 2, 4)\n", "plt.hexbin(r.ravel(), g.ravel(), bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"red\")\n", "plt.ylabel(\"green\")\n", "\n", "axins = plt.axes([0.2, 0.12, 0.28, 0.28])\n", "axins.hexbin(\n", " r.ravel(), g.ravel(), extent=(0, 80, 0, 150), bins=\"log\", cmap=plt.cm.viridis_r\n", ")\n", "\n", "mark_inset(ax, axins, loc1=1, loc2=4, fc=\"none\", ec=\"0.5\")" ] }, { "cell_type": "code", "execution_count": null, "id": "b0060b27-bfea-4cd5-99fa-8fde6c9eefde", "metadata": {}, "outputs": [], "source": [ "import statsmodels.api as sm\n", "\n", "\n", "# import pylab\n", "def qqplot(v1, v2):\n", " ml = min(len(v1), len(v2))\n", " sm.qqplot_2samples(v1[:ml], v2[:ml])\n", " # pylab.show()\n", "\n", "\n", "qqplot(bgv3[\"R\"][2], bgv3[\"G\"][2])\n", "qqplot(bgv3[\"C\"][3], bgv3[\"R\"][1])" ] }, { "cell_type": "markdown", "id": "48aa0fd4-e544-427d-9a26-fe5fcbc59e72", "metadata": {}, "source": [ "## flat image correction" ] }, { "cell_type": "code", "execution_count": null, "id": "d458cca7-0828-4817-ad1b-c39bf32362d7", "metadata": {}, "outputs": [], "source": [ "g1a = nima.read_tiff(\"/home/dati/dt-clop3/data/210917/3alpha/c1_2.tf8\", channels)[0][\n", " \"G\"\n", "][2]\n", "g2a = nima.read_tiff(\"/home/dati/dt-clop3/data/210917/3alpha/c2_2.tf8\", channels)[0][\n", " \"G\"\n", "][2]\n", "g3a = nima.read_tiff(\"/home/dati/dt-clop3/data/210917/3alpha/c3_2.tf8\", channels)[0][\n", " \"G\"\n", "][2]\n", "g1 = nima.read_tiff(\"/home/dati/dt-clop3/data/210917/3/c1_2.tf8\", channels)[0][\"G\"][2]\n", "g2 = nima.read_tiff(\"/home/dati/dt-clop3/data/210917/3/c2_2.tf8\", channels)[0][\"G\"][2]\n", "g3 = nima.read_tiff(\"/home/dati/dt-clop3/data/210917/3/c3_2.tf8\", channels)[0][\"G\"][2]" ] }, { "cell_type": "code", "execution_count": null, "id": "e5c8dcb8-2477-4d5f-ba08-5f2b5a104d63", "metadata": {}, "outputs": [], "source": [ "plt.subplot(1, 2, 1)\n", "skimage.io.imshow(\n", " skimage.filters.gaussian(np.max([g1, g2, g3, g1a, g2a, g3a], axis=0), sigma=500)\n", ")\n", "plt.subplot(1, 2, 2)\n", "skimage.io.imshow(skimage.filters.gaussian(np.max([g1, g2, g3], axis=0), sigma=500))" ] }, { "cell_type": "markdown", "id": "48b25e78-fbb6-43f0-b97e-5d93ba6cbdc7", "metadata": {}, "source": [ "Run simulazion to identify best algorithm for FLAT calculation.\n", "\n", "(Do we need to simulate a fluorophore solution sample?)\n", "\n", "Compare to external image simulator." ] }, { "cell_type": "code", "execution_count": null, "id": "7de61ad5-90ec-4bff-89cd-75ead98e9898", "metadata": {}, "outputs": [], "source": [ "phantom = skimage.io.imread(\n", " \"/home/dan/2024-02-05_15h-38m-58s_image_all_tif/final/final.tif\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "6eb16f72-8a22-4eef-808f-03056cb4def1", "metadata": {}, "outputs": [], "source": [ "skimage.io.imshow(phantom[14])" ] }, { "cell_type": "code", "execution_count": null, "id": "5b45f92c-dc55-41d3-9c9e-d6d2ec5057b8", "metadata": {}, "outputs": [], "source": [ "sb.histplot(phantom[1].flatten(), kde=True)\n", "plt.yscale(\"log\")" ] }, { "cell_type": "markdown", "id": "42435ad0-9d17-4122-a5d4-8f560b5f3d9d", "metadata": {}, "source": [ "## utils.bg" ] }, { "cell_type": "code", "execution_count": null, "id": "2b911cef-7b6f-4c84-a7e8-7da61d5bf746", "metadata": {}, "outputs": [], "source": [ "mmap24 = tifffile.memmap(\n", " \"/home/dati/dt-clop3/data/210917/3/c1_2.tf8\", mode=\"r\", page=24\n", ")\n", "mmap25 = tifffile.memmap(\n", " \"/home/dati/dt-clop3/data/210917/3/c1_2.tf8\", mode=\"r\", page=25\n", ")\n", "mmap26 = tifffile.memmap(\n", " \"/home/dati/dt-clop3/data/210917/3/c1_2.tf8\", mode=\"r\", page=26\n", ")\n", "skimage.io.imshow(mmap25)" ] }, { "cell_type": "code", "execution_count": null, "id": "0cc302c3-ce41-4dce-afa4-d16729c4cb68", "metadata": {}, "outputs": [], "source": [ "sb.histplot(mmap24.ravel())\n", "plt.yscale(\"log\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c74eb6ba-48dc-4053-b690-1ea4d7f2327b", "metadata": {}, "outputs": [], "source": [ "from nima import utils" ] }, { "cell_type": "code", "execution_count": null, "id": "f32c5d3d-f371-4052-a78f-ade18a7dd213", "metadata": {}, "outputs": [], "source": [ "bg24, sd24 = utils.bg(mmap24, bgmax=114950000000000000000000000000)\n", "bg25, sd25 = utils.bg(mmap25)\n", "bg26, sd26 = utils.bg(mmap26)\n", "bg24, sd24, bg25, sd25, bg26, sd26" ] }, { "cell_type": "code", "execution_count": null, "id": "77fd2a76-3126-47aa-96ab-f85bf249d7ef", "metadata": {}, "outputs": [], "source": [ "sb.histplot(mmap25[(utils.prob(mmap25, bg25, sd25) > 0.00033)])" ] }, { "cell_type": "code", "execution_count": null, "id": "42e30b2f-54d8-4753-84b6-d77927418900", "metadata": {}, "outputs": [], "source": [ "skimage.io.imshow(utils.prob(mmap25, bg25, sd25) > 0.033)" ] }, { "cell_type": "code", "execution_count": null, "id": "0ebbaf19-d5d7-4c3a-8bb6-6bc293f30eaa", "metadata": {}, "outputs": [], "source": [ "bg = mmap24[(utils.prob(mmap24, bg24, sd24) > 0.00033)]\n", "br = mmap25[(utils.prob(mmap25, bg25, sd25) > 0.00000033)]\n", "bc = mmap26[(utils.prob(mmap26, bg26, sd26) > 0.00000033)]\n", "\n", "mmin = min(len(bc), len(bg), len(br))\n", "\n", "print(\"c =\", len(bc))\n", "print(\"g =\", len(bg))\n", "print(\"r =\", len(br))\n", "\n", "plt.figure(figsize=(7.5, 6))\n", "\n", "plt.subplot(2, 2, 1)\n", "mmin = min(len(bc), len(bg))\n", "plt.hexbin(bg[:mmin], bc[:mmin], bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"green\")\n", "plt.ylabel(\"cyan\")\n", "\n", "plt.subplot(2, 2, 2)\n", "mmin = min(len(bc), len(br))\n", "plt.hexbin(br[:mmin], bc[:mmin], bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"red\")\n", "plt.ylabel(\"cyan\")\n", "\n", "ax = plt.subplot(2, 2, 4)\n", "mmin = min(len(br), len(bg))\n", "plt.hexbin(br[:mmin], bg[:mmin], bins=\"log\", cmap=plt.cm.viridis_r)\n", "cb = plt.colorbar()\n", "plt.xlabel(\"red\")\n", "plt.ylabel(\"green\")" ] }, { "cell_type": "code", "execution_count": null, "id": "911e29ae-91c6-4176-90cf-e640df6fb8dd", "metadata": {}, "outputs": [], "source": [ "with tifffile.TiffFile(\"/home/dati/dt-clop3/data/210917/3alpha/c1_2.tf8\") as tif:\n", " im = tif.asarray()\n", " axes = tif.series[0].axes\n", " print(axes)" ] }, { "cell_type": "markdown", "id": "e106c237-b203-4d36-a88b-c110aada7046", "metadata": {}, "source": [ "## BIAS and FLAT\n", "\n", "TODO:\n", "- pytest\n", "- build a function callable from both the library and the CLI" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }