From 312a89c1fce7596171c55ceed0a931bfe3897bfd Mon Sep 17 00:00:00 2001 From: jamesyan-git Date: Mon, 30 May 2022 15:30:07 +1000 Subject: [PATCH 1/2] initial commit --- slice-interpolation-for-zarpaint.ipynb | 707 +++++++++++++++++++++---- 1 file changed, 603 insertions(+), 104 deletions(-) diff --git a/slice-interpolation-for-zarpaint.ipynb b/slice-interpolation-for-zarpaint.ipynb index ca7b070..3405570 100644 --- a/slice-interpolation-for-zarpaint.ipynb +++ b/slice-interpolation-for-zarpaint.ipynb @@ -36,7 +36,9 @@ "source": [ "import os\n", "import matplotlib.pyplot as plt\n", - "import numpy as np" + "import numpy as np\n", + "\n", + "%matplotlib inline" ] }, { @@ -54,40 +56,44 @@ "execution_count": 3, "id": "c88efc99", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Traceback (most recent call last):\n", - " File \"/Users/genevieb/mambaforge/envs/zarpaint/lib/python3.9/site-packages/magicgui/widgets/_bases/value_widget.py\", line 57, in _on_value_change\n", - " self.changed.emit(value)\n", - " File \"psygnal/_signal.py\", line 682, in psygnal._signal.SignalInstance.emit\n", - " File \"psygnal/_signal.py\", line 724, in psygnal._signal.SignalInstance._run_emit_loop\n", - " File \"psygnal/_signal.py\", line 725, in psygnal._signal.SignalInstance._run_emit_loop\n", - " File \"psygnal/_signal.py\", line 745, in psygnal._signal.SignalInstance._run_emit_loop\n", - " File \"/Users/genevieb/mambaforge/envs/zarpaint/lib/python3.9/site-packages/toolz/functoolz.py\", line 306, in __call__\n", - " return self._partial(*args, **kwargs)\n", - " File \"/Users/genevieb/mambaforge/envs/zarpaint/lib/python3.9/site-packages/zarpaint/_zarpaint.py\", line 20, in _set_default_labels_path\n", - " source_image = source_image_event.value\n", - "AttributeError: 'Image' object has no attribute 'value'\n" - ] - } - ], + "outputs": [], "source": [ "viewer = napari.Viewer()\n", - "viewer.open_sample('napari', 'cells3d')\n", - "membrane_layer = viewer.layers[0]\n", - "nuclei_layer = viewer.layers[1]" + "# viewer.open_sample('napari', 'cells3d')\n", + "# membrane_layer = viewer.layers[0]\n", + "# nuclei_layer = viewer.layers[1]" ] }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 4, "id": "378903ae", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "from skimage import data\n", + "blobs = np.stack(\n", + " [\n", + " data.binary_blobs(\n", + " length=128, blob_size_fraction=0.05, n_dim=3, volume_fraction=f, seed=0\n", + " )\n", + " for f in np.linspace(0.05, 0.5, 10)\n", + " ],\n", + " axis=0,\n", + ")\n", + "viewer.add_image(blobs)\n", + "\n", "# # numpy label data\n", "# single_nuclei_label = tifffile.imread('single-nuclei-label.tif')\n", "# viewer.add_labels(single_nuclei_label)\n", @@ -110,14 +116,15 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 47, "id": "a34808c0", "metadata": {}, "outputs": [], "source": [ "# Make note of which image slices you labelled\n", - "slice_index_1 = 30\n", - "slice_index_2 = 38" + "slice_index_1 = 60\n", + "slice_index_2 = 70\n", + "interp_dim = 1" ] }, { @@ -130,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 48, "id": "119114bf", "metadata": {}, "outputs": [ @@ -146,9 +153,9 @@ " },\n", " 'driver': 'zarr',\n", " 'dtype': 'uint32',\n", - " 'kvstore': {'driver': 'file', 'path': 'labels.zarr/'},\n", + " 'kvstore': {'driver': 'file', 'path': 'myblobs4d/'},\n", " 'metadata': {\n", - " 'chunks': [1, 128, 128],\n", + " 'chunks': [1, 1, 128, 128],\n", " 'compressor': {\n", " 'blocksize': 0,\n", " 'clevel': 5,\n", @@ -161,12 +168,12 @@ " 'fill_value': 0,\n", " 'filters': None,\n", " 'order': 'C',\n", - " 'shape': [60, 256, 256],\n", + " 'shape': [10, 128, 128, 128],\n", " 'zarr_format': 2,\n", " },\n", " 'transform': {\n", - " 'input_exclusive_max': [[60], [256], [256]],\n", - " 'input_inclusive_min': [0, 0, 0],\n", + " 'input_exclusive_max': [[10], [128], [128], [128]],\n", + " 'input_inclusive_min': [0, 0, 0, 0],\n", " },\n", "})\n" ] @@ -181,7 +188,84 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 31, + "id": "c31ef2c8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " ...\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]]\n", + "\n", + " [[0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " ...\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]]\n", + "\n", + " [[0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " ...\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]]\n", + "\n", + " ...\n", + "\n", + " [[0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " ...\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]]\n", + "\n", + " [[0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " ...\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]]\n", + "\n", + " [[0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " ...\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]\n", + " [0 0 0 ... 0 0 0]]]\n" + ] + } + ], + "source": [ + "print(np.take(label_layer.data, slice_index_1, axis=interp_dim))" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "c8950929", + "metadata": {}, + "outputs": [], + "source": [ + "image_1 = np.take(label_layer.data, slice_index_1, axis=interp_dim)\n", + "image_2 = np.take(label_layer.data, slice_index_2, axis=interp_dim)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, "id": "a6c7ab8d", "metadata": {}, "outputs": [ @@ -189,77 +273,156 @@ "name": "stdout", "output_type": "stream", "text": [ - "TensorStore({\n", - " 'context': {\n", - " 'cache_pool': {},\n", - " 'data_copy_concurrency': {},\n", - " 'file_io_concurrency': {},\n", - " },\n", - " 'driver': 'zarr',\n", - " 'dtype': 'uint32',\n", - " 'kvstore': {'driver': 'file', 'path': 'mylabels.zarr/'},\n", - " 'metadata': {\n", - " 'chunks': [1, 128, 128],\n", - " 'compressor': {\n", - " 'blocksize': 0,\n", - " 'clevel': 5,\n", - " 'cname': 'lz4',\n", - " 'id': 'blosc',\n", - " 'shuffle': 1,\n", - " },\n", - " 'dimension_separator': '.',\n", - " 'dtype': '" + "" ] }, - "execution_count": 6, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" }, { "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAlUAAADGCAYAAAD7a3V2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAWJElEQVR4nO3deZRU5Z3G8efXC7RtszQugICKptWBiaJ2EIzxOMe4hPEMGpXBOAEMR0yCM5gxC5o/zDJmDEbiMUYMjgpOFIckRlEZDOGYyWgEwQ1FgqKgNGloV9bY0t2/+aMuUsUA3dX1Vt2qut/POX266q1bdd9b3AceblXdMncXAAAAclMR9wQAAADKAaUKA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vLyac/ZSpI7k5xI8uDE2ln3m0Uf6X4fvpbk2ulNPh1mwkyYiT4zYSbmJRNTLVVJ1gB/DdwIXAO8Lck105zpAvq1qvrFie/duA04WFVbgYPd7dViL7D9jLWl9nsjsLX7txv46AWacSaYCTNhJvrMhJmYp0xM+0zVdcCRqnqsql4EPgnsmPJM07ID2Ndd3wfsnN4obVXVF4Bnzlhear87gI/XovuBy5JcdUEGnQ1m4jQzcXrdTJgJMBM7J9ZnMhPTLlWbgCcmbh/t1la7Aj6b5EtJdndrG6vqeHf9SWDjdEa7YJba71h/J1421v2bCTOxlLHu30zMYSbWTnuAkfqVqjqW5CeAA0m+PnlnVVWS0XzXxdj2q7MyExPGtl+dlZmYMC/7nfaZqmPA1RO3N3drq1pVHesuTwCfYfH09lMvn77sLk9Mb8ILYqn9jvJ3YsIo928mADOxlFHu30wAc5iJaZeqLwJbk2xJchFwM7B/yjOtqCSXJPnRl68Dvwk8yOK+d3WH7QLunc6EF8xS+90PvL37644bgOcmTv+OgZkwE2AmJpkJMwHzkomqmuo/4Cbgm8C3gPdOe54LsN+fBv69+/fQy3sGrmDxrxseBf4Z2DDtWRvu+S7gOHCKxfe+b1lqv0BY/EufbwEPAAvTnn8KPy8zUWbCTPR+XmaizMQ8ZML/TY0kSVID0377T5IkaVWwVEmSJDVgqZIkSWrAUiVJktSApUqSJKkBS5UkSVIDlipJkqQG/g9f7oj2bei0TgAAAABJRU5ErkJggg==", "text/plain": [ "
" ] @@ -271,14 +434,12 @@ } ], "source": [ - "image_1 = np.asarray(ts[slice_index_1])\n", - "image_2 = np.asarray(ts[slice_index_2])\n", "diff = image_1 - image_2\n", "\n", "fig, ax = plt.subplots(ncols=3, figsize=(10,3))\n", - "ax[0].imshow(image_1)\n", - "ax[1].imshow(image_2)\n", - "ax[2].imshow(diff)\n" + "ax[0].imshow(image_1[0]) # 4D original dataset\n", + "ax[1].imshow(image_2[0]) # 4D original dataset\n", + "ax[2].imshow(diff[0]) # 4D original dataset\n" ] }, { @@ -291,7 +452,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "id": "a3657b13", "metadata": {}, "outputs": [], @@ -325,7 +486,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 126, "id": "83658b31", "metadata": {}, "outputs": [], @@ -357,7 +518,7 @@ "def xi_coords(shape, percent=0.5):\n", " slices = [slice(0, i) for i in shape]\n", " xi = np.moveaxis(np.mgrid[slices], 0, -1).reshape(np.prod(shape), len(shape))\n", - " xi = xi = np.c_[np.full((np.prod(shape)), percent), xi]\n", + " xi = np.moveaxis(np.mgrid[slices], 0, -1).reshape(np.prod(shape), len(shape)).astype('float')\n", " return xi\n", "\n", "\n", @@ -383,11 +544,15 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 127, "id": "3be6323c", "metadata": {}, "outputs": [], "source": [ + "\n", + "\n", + "\n", + "\n", "# ## First draft, simply returns a numpy stack of the interpolated slices\n", "\n", "# def interpolate_between_slices(image_1, image_2, slice_index_1, slice_index_2, interp_dim=0, method='linear'):\n", @@ -419,19 +584,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 128, "id": "9b00f986", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Done!\n", - "Please scroll through napari to see the interpolated label slices\n" - ] - } - ], + "outputs": [], "source": [ "## Second draft, writes directly into tensorstore zarr array\n", "\n", @@ -449,14 +605,354 @@ "\n", " for slice_number, percentage in slice_iterator(slice_index_1, slice_index_2):\n", " interpolated_img = interpolated_slice(percentage, points, values, interp_dim=interp_dim, method='linear')\n", - " label_layer.data[slice_number, interpolated_img] = label_id\n", + " indices = np.argwhere(interpolated_img)\n", + " indices = np.insert(indices, interp_dim, slice_number, axis=1)\n", + " label_layer.data[indices] = label_id\n", " label_layer.refresh() # will update the current view\n", "\n", - "interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2)\n", + "# interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2)\n", + "# print(\"Done!\")\n", + "# print(\"Please scroll through napari to see the interpolated label slices\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "0744b5a8", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "The requested sample points xi have dimension 3, but this RegularGridInterpolator has dimension 4", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\User\\projects\\slice-interpolation\\slice-interpolation-for-zarpaint.ipynb Cell 23'\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m interp_dim \u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[1;32m----> 2\u001b[0m interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2, interp_dim\u001b[39m=\u001b[39;49minterp_dim)\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mDone!\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mPlease scroll through napari to see the interpolated label slices\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[1;32mc:\\Users\\User\\projects\\slice-interpolation\\slice-interpolation-for-zarpaint.ipynb Cell 22'\u001b[0m in \u001b[0;36minterpolate_between_slices\u001b[1;34m(label_layer, image_1, image_2, slice_index_1, slice_index_2, interp_dim, method)\u001b[0m\n\u001b[0;32m 13\u001b[0m points, values \u001b[39m=\u001b[39m point_and_values(image_1, image_2, interp_dim\u001b[39m=\u001b[39minterp_dim)\n\u001b[0;32m 15\u001b[0m \u001b[39mfor\u001b[39;00m slice_number, percentage \u001b[39min\u001b[39;00m slice_iterator(slice_index_1, slice_index_2):\n\u001b[1;32m---> 16\u001b[0m interpolated_img \u001b[39m=\u001b[39m interpolated_slice(percentage, points, values, interp_dim\u001b[39m=\u001b[39;49minterp_dim, method\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mlinear\u001b[39;49m\u001b[39m'\u001b[39;49m)\n\u001b[0;32m 17\u001b[0m indices \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39margwhere(interpolated_img)\n\u001b[0;32m 18\u001b[0m indices \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39minsert(indices, interp_dim, slice_number, axis\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m)\n", + "\u001b[1;32mc:\\Users\\User\\projects\\slice-interpolation\\slice-interpolation-for-zarpaint.ipynb Cell 20'\u001b[0m in \u001b[0;36minterpolated_slice\u001b[1;34m(percent, points, values, interp_dim, method)\u001b[0m\n\u001b[0;32m 44\u001b[0m \u001b[39m# Calculate the interpolated slice\u001b[39;00m\n\u001b[0;32m 45\u001b[0m xi \u001b[39m=\u001b[39m xi_coords(img_shape, percent\u001b[39m=\u001b[39mpercent)\n\u001b[1;32m---> 46\u001b[0m interpolated_img \u001b[39m=\u001b[39m interpn(points, values, xi, method\u001b[39m=\u001b[39;49mmethod)\n\u001b[0;32m 47\u001b[0m interpolated_img \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mreshape(interpolated_img, img_shape) \u001b[39m>\u001b[39m \u001b[39m0\u001b[39m\n\u001b[0;32m 48\u001b[0m \u001b[39mreturn\u001b[39;00m interpolated_img\n", + "File \u001b[1;32mc:\\Users\\User\\anaconda3\\envs\\napari\\lib\\site-packages\\scipy\\interpolate\\_interpolate.py:2701\u001b[0m, in \u001b[0;36minterpn\u001b[1;34m(points, values, xi, method, bounds_error, fill_value)\u001b[0m\n\u001b[0;32m 2699\u001b[0m xi \u001b[39m=\u001b[39m _ndim_coords_from_arrays(xi, ndim\u001b[39m=\u001b[39m\u001b[39mlen\u001b[39m(grid))\n\u001b[0;32m 2700\u001b[0m \u001b[39mif\u001b[39;00m xi\u001b[39m.\u001b[39mshape[\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m] \u001b[39m!=\u001b[39m \u001b[39mlen\u001b[39m(grid):\n\u001b[1;32m-> 2701\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe requested sample points xi have dimension \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 2702\u001b[0m \u001b[39m\"\u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m, but this RegularGridInterpolator has \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 2703\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mdimension \u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m\"\u001b[39m \u001b[39m%\u001b[39m (xi\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m], \u001b[39mlen\u001b[39m(grid)))\n\u001b[0;32m 2705\u001b[0m \u001b[39mif\u001b[39;00m bounds_error:\n\u001b[0;32m 2706\u001b[0m \u001b[39mfor\u001b[39;00m i, p \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(xi\u001b[39m.\u001b[39mT):\n", + "\u001b[1;31mValueError\u001b[0m: The requested sample points xi have dimension 3, but this RegularGridInterpolator has dimension 4" + ] + } + ], + "source": [ + "interp_dim = 1\n", + "interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2, interp_dim=interp_dim)\n", "print(\"Done!\")\n", - "print(\"Please scroll through napari to see the interpolated label slices\")\n" + "print(\"Please scroll through napari to see the interpolated label slices\")" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "bcaf0137", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "label_id = 1\n", + "image_1 = image_1.astype(bool)\n", + "image_2 = image_2.astype(bool)\n", + "\n", + "points, values = point_and_values(image_1, image_2, interp_dim=interp_dim)\n", + "\n", + "\n", + "#for slice_number, percentage in slice_iterator(slice_index_1, slice_index_2):\n", + "# interpolated_img = interpolated_slice(percentage, points, values, interp_dim=interp_dim, method='linear')\n", + "# label_layer.data[slice_number, interpolated_img] = label_id\n", + "#label_layer.refresh() # will update the current view" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "a911c2b9", + "metadata": {}, + "outputs": [], + "source": [ + "percentage = 0.5\n", + "percent = percentage\n", + "\n", + "img_shape = list(values.shape)\n", + "del img_shape[interp_dim]\n", + "# Calculate the interpolated slice\n", + "\n", + "# xi = xi_coords(img_shape, percent=percent)\n", + "method='linear'\n", + "shape = img_shape\n", + "slices = [slice(0, i) for i in shape]\n", + "xi = np.moveaxis(np.mgrid[slices], 0, -1).reshape(np.prod(shape), len(shape)).astype('float')\n", + "xi = np.insert(xi, interp_dim, percent, axis=1)\n", + "\n", + "interpolated_img = interpn(points, values, xi, method=method)\n", + "interpolated_img = np.reshape(interpolated_img, img_shape) > 0\n" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "ee19afac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10, 128, 128)" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "interpolated_img.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "9857f388", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 1, 128, 128)\n" + ] + } + ], + "source": [ + "slice_number = 50\n", + "temp = np.expand_dims(interpolated_img, interp_dim)\n", + "print(temp.shape)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "c6c9ad3d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[[False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " ...,\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False]]],\n", + "\n", + "\n", + " [[[False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " ...,\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False]]],\n", + "\n", + "\n", + " [[[False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " ...,\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False]]],\n", + "\n", + "\n", + " ...,\n", + "\n", + "\n", + " [[[False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " ...,\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False]]],\n", + "\n", + "\n", + " [[[False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " ...,\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False]]],\n", + "\n", + "\n", + " [[[False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " ...,\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False],\n", + " [False, False, False, ..., False, False, False]]]])" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "temp\n" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "b9f2a13e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 50, 54, 52],\n", + " [ 5, 50, 54, 53]], dtype=int64)" + ] + }, + "execution_count": 123, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "indices = np.argwhere(interpolated_img)\n", + "indices = np.insert(indices, interp_dim, slice_number, axis=1)\n" ] }, + { + "cell_type": "code", + "execution_count": 75, + "id": "df3e8414", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0.5, 0. , 0. , 0. ],\n", + " [ 0.5, 0. , 0. , 1. ],\n", + " [ 0.5, 0. , 0. , 2. ],\n", + " ...,\n", + " [ 0.5, 9. , 127. , 125. ],\n", + " [ 0.5, 9. , 127. , 126. ],\n", + " [ 0.5, 9. , 127. , 127. ]])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "xi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d0c69ad", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef63b9c3", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from scipy.interpolate import interpn\n", + "from scipy.ndimage import distance_transform_edt\n", + "\n", + "\n", + "def distance_transform(image):\n", + " \"\"\"Distance transform for a boolean image.\n", + " \n", + " Returns positive values inside the object,\n", + " and negative values outside.\n", + " \"\"\"\n", + " image = image.astype(bool)\n", + " edt = distance_transform_edt(image) - distance_transform_edt(~image)\n", + " return edt\n", + "\n", + "\n", + "def point_and_values(image_1, image_2, interp_dim=0):\n", + " edt_1 = distance_transform(image_1)\n", + " edt_2 = distance_transform(image_2)\n", + " values = np.stack([edt_1, edt_2], axis=interp_dim)\n", + " points = tuple([np.arange(i) for i in values.shape])\n", + " return points, values\n", + "\n", + "\n", + "def xi_coords(shape, percent=0.5):\n", + " slices = [slice(0, i) for i in shape]\n", + " xi = np.moveaxis(np.mgrid[slices], 0, -1).reshape(np.prod(shape), len(shape))\n", + " xi = xi = np.c_[np.full((np.prod(shape)), percent), xi]\n", + " return xi\n", + "\n", + "\n", + "def slice_iterator(slice_index_1, slice_index_2):\n", + " intermediate_slices = np.arange(slice_index_1 + 1, slice_index_2)\n", + " n_slices = slice_index_2 - slice_index_1 + 1 # inclusive\n", + " stepsize = 1 / n_slices\n", + " intermediate_percentages = np.arange(0 + stepsize, 1, stepsize)\n", + " return zip(intermediate_slices, intermediate_percentages)\n", + "\n", + "\n", + "def interpolated_slice(percent, points, values, interp_dim=0, method='linear'):\n", + " # Find the original image shape\n", + " img_shape = list(values.shape)\n", + " del img_shape[interp_dim]\n", + " # Calculate the interpolated slice\n", + " xi = xi_coords(img_shape, percent=percent)\n", + " interpolated_img = interpn(points, values, xi, method=method)\n", + " interpolated_img = np.reshape(interpolated_img, img_shape) > 0\n", + " return interpolated_img\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33f94119", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f23e2b74", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d9c2522c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1e8d31e7", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "449d7c17", @@ -583,8 +1079,11 @@ } ], "metadata": { + "interpreter": { + "hash": "e8f0b44cf16a59ebfa2ed0b00c97bf93787b28c44f2aafb01610def517855b78" + }, "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.12 ('napari')", "language": "python", "name": "python3" }, From 404f7f6dbf704e60c867cbe706f90c4f09ea0a14 Mon Sep 17 00:00:00 2001 From: jamesyan-git Date: Thu, 2 Jun 2022 14:59:45 +1000 Subject: [PATCH 2/2] Commit changes since last push --- slice-interpolation-for-zarpaint.ipynb | 167 ++++++++++++++++++++----- 1 file changed, 133 insertions(+), 34 deletions(-) diff --git a/slice-interpolation-for-zarpaint.ipynb b/slice-interpolation-for-zarpaint.ipynb index 3405570..2bb99f6 100644 --- a/slice-interpolation-for-zarpaint.ipynb +++ b/slice-interpolation-for-zarpaint.ipynb @@ -73,7 +73,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 4, @@ -116,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 6, "id": "a34808c0", "metadata": {}, "outputs": [], @@ -137,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 7, "id": "119114bf", "metadata": {}, "outputs": [ @@ -153,7 +153,7 @@ " },\n", " 'driver': 'zarr',\n", " 'dtype': 'uint32',\n", - " 'kvstore': {'driver': 'file', 'path': 'myblobs4d/'},\n", + " 'kvstore': {'driver': 'file', 'path': 'example4d448pm/'},\n", " 'metadata': {\n", " 'chunks': [1, 1, 128, 128],\n", " 'compressor': {\n", @@ -188,7 +188,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 148, "id": "c31ef2c8", "metadata": {}, "outputs": [ @@ -254,7 +254,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 8, "id": "c8950929", "metadata": {}, "outputs": [], @@ -265,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 9, "id": "a6c7ab8d", "metadata": {}, "outputs": [ @@ -386,7 +386,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 10, "id": "7fa99451", "metadata": {}, "outputs": [ @@ -395,7 +395,7 @@ "output_type": "stream", "text": [ "97\n", - "155\n" + "97\n" ] } ], @@ -406,17 +406,17 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 11, "id": "31b288bc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 53, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" }, @@ -452,7 +452,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 12, "id": "a3657b13", "metadata": {}, "outputs": [], @@ -469,7 +469,7 @@ " label_layer.data[slices] = 0\n", " label_layer.refresh()\n", "\n", - " \n", + "\n", "def erase_all_slices(label_layer):\n", " for i in label_layer.data:\n", " i[...] = 0\n", @@ -486,7 +486,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 13, "id": "83658b31", "metadata": {}, "outputs": [], @@ -517,8 +517,8 @@ "\n", "def xi_coords(shape, percent=0.5):\n", " slices = [slice(0, i) for i in shape]\n", - " xi = np.moveaxis(np.mgrid[slices], 0, -1).reshape(np.prod(shape), len(shape))\n", " xi = np.moveaxis(np.mgrid[slices], 0, -1).reshape(np.prod(shape), len(shape)).astype('float')\n", + " xi = np.insert(xi, interp_dim, percent, axis=1)\n", " return xi\n", "\n", "\n", @@ -584,7 +584,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 25, "id": "9b00f986", "metadata": {}, "outputs": [], @@ -605,34 +605,125 @@ "\n", " for slice_number, percentage in slice_iterator(slice_index_1, slice_index_2):\n", " interpolated_img = interpolated_slice(percentage, points, values, interp_dim=interp_dim, method='linear')\n", - " indices = np.argwhere(interpolated_img)\n", - " indices = np.insert(indices, interp_dim, slice_number, axis=1)\n", - " label_layer.data[indices] = label_id\n", + " indices = [slice(None) for _ in range(label_layer.data.ndim)]\n", + " indices[interp_dim] = slice_number\n", + " indices = tuple(indices)\n", + " label_layer.data[indices][interpolated_img] = label_id\n", " label_layer.refresh() # will update the current view\n", "\n", - "# interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2)\n", + "# interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2, interp_dim=interp_dim)\n", "# print(\"Done!\")\n", "# print(\"Please scroll through napari to see the interpolated label slices\")\n" ] }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 33, + "id": "51b5ee0e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 128, 128)\n" + ] + } + ], + "source": [ + "percentage = 0.5\n", + "points, values = point_and_values(image_1, image_2, interp_dim=interp_dim)\n", + "interpolated_img = interpolated_slice(percentage, points, values, interp_dim=interp_dim, method='linear')\n", + "print(interpolated_img.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "4f92243e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(slice(None, None, None), 50, slice(None, None, None), slice(None, None, None))\n" + ] + } + ], + "source": [ + "slice_number = 50\n", + "indices = [slice(None) for _ in range(label_layer.data.ndim)]\n", + "indices[interp_dim] = slice_number\n", + "indices = tuple(indices)\n", + "print(indices)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "3f61ef08", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(5, 50, 64, 64)" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "viewer.dims.current_step" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "702dc496", + "metadata": {}, + "outputs": [], + "source": [ + "label_layer.data[indices] = 2\n", + "label_layer.refresh() # will update the current view\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "2618bfc0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "label_layer.data[indices][interpolated_img].write(label_id)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, "id": "0744b5a8", "metadata": {}, "outputs": [ { - "ename": "ValueError", - "evalue": "The requested sample points xi have dimension 3, but this RegularGridInterpolator has dimension 4", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\User\\projects\\slice-interpolation\\slice-interpolation-for-zarpaint.ipynb Cell 23'\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m interp_dim \u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[1;32m----> 2\u001b[0m interpolate_between_slices(label_layer, image_1, image_2, slice_index_1, slice_index_2, interp_dim\u001b[39m=\u001b[39;49minterp_dim)\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mDone!\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mPlease scroll through napari to see the interpolated label slices\u001b[39m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;32mc:\\Users\\User\\projects\\slice-interpolation\\slice-interpolation-for-zarpaint.ipynb Cell 22'\u001b[0m in \u001b[0;36minterpolate_between_slices\u001b[1;34m(label_layer, image_1, image_2, slice_index_1, slice_index_2, interp_dim, method)\u001b[0m\n\u001b[0;32m 13\u001b[0m points, values \u001b[39m=\u001b[39m point_and_values(image_1, image_2, interp_dim\u001b[39m=\u001b[39minterp_dim)\n\u001b[0;32m 15\u001b[0m \u001b[39mfor\u001b[39;00m slice_number, percentage \u001b[39min\u001b[39;00m slice_iterator(slice_index_1, slice_index_2):\n\u001b[1;32m---> 16\u001b[0m interpolated_img \u001b[39m=\u001b[39m interpolated_slice(percentage, points, values, interp_dim\u001b[39m=\u001b[39;49minterp_dim, method\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mlinear\u001b[39;49m\u001b[39m'\u001b[39;49m)\n\u001b[0;32m 17\u001b[0m indices \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39margwhere(interpolated_img)\n\u001b[0;32m 18\u001b[0m indices \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39minsert(indices, interp_dim, slice_number, axis\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m)\n", - "\u001b[1;32mc:\\Users\\User\\projects\\slice-interpolation\\slice-interpolation-for-zarpaint.ipynb Cell 20'\u001b[0m in \u001b[0;36minterpolated_slice\u001b[1;34m(percent, points, values, interp_dim, method)\u001b[0m\n\u001b[0;32m 44\u001b[0m \u001b[39m# Calculate the interpolated slice\u001b[39;00m\n\u001b[0;32m 45\u001b[0m xi \u001b[39m=\u001b[39m xi_coords(img_shape, percent\u001b[39m=\u001b[39mpercent)\n\u001b[1;32m---> 46\u001b[0m interpolated_img \u001b[39m=\u001b[39m interpn(points, values, xi, method\u001b[39m=\u001b[39;49mmethod)\n\u001b[0;32m 47\u001b[0m interpolated_img \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mreshape(interpolated_img, img_shape) \u001b[39m>\u001b[39m \u001b[39m0\u001b[39m\n\u001b[0;32m 48\u001b[0m \u001b[39mreturn\u001b[39;00m interpolated_img\n", - "File \u001b[1;32mc:\\Users\\User\\anaconda3\\envs\\napari\\lib\\site-packages\\scipy\\interpolate\\_interpolate.py:2701\u001b[0m, in \u001b[0;36minterpn\u001b[1;34m(points, values, xi, method, bounds_error, fill_value)\u001b[0m\n\u001b[0;32m 2699\u001b[0m xi \u001b[39m=\u001b[39m _ndim_coords_from_arrays(xi, ndim\u001b[39m=\u001b[39m\u001b[39mlen\u001b[39m(grid))\n\u001b[0;32m 2700\u001b[0m \u001b[39mif\u001b[39;00m xi\u001b[39m.\u001b[39mshape[\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m] \u001b[39m!=\u001b[39m \u001b[39mlen\u001b[39m(grid):\n\u001b[1;32m-> 2701\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe requested sample points xi have dimension \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 2702\u001b[0m \u001b[39m\"\u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m, but this RegularGridInterpolator has \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 2703\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mdimension \u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m\"\u001b[39m \u001b[39m%\u001b[39m (xi\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m], \u001b[39mlen\u001b[39m(grid)))\n\u001b[0;32m 2705\u001b[0m \u001b[39mif\u001b[39;00m bounds_error:\n\u001b[0;32m 2706\u001b[0m \u001b[39mfor\u001b[39;00m i, p \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(xi\u001b[39m.\u001b[39mT):\n", - "\u001b[1;31mValueError\u001b[0m: The requested sample points xi have dimension 3, but this RegularGridInterpolator has dimension 4" + "name": "stdout", + "output_type": "stream", + "text": [ + "Done!\n", + "Please scroll through napari to see the interpolated label slices\n" ] } ], @@ -645,7 +736,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 18, "id": "bcaf0137", "metadata": {}, "outputs": [], @@ -921,6 +1012,14 @@ " " ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "864c8bda", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null,