From 695afd88063f7dd2d5b45a8c7f26af33decafd7e Mon Sep 17 00:00:00 2001 From: satra Date: Sun, 31 Oct 2021 00:02:52 +0000 Subject: [PATCH] add neuroglancer --- .../chunglab/demo/2021-09-27_dandi-demo.ipynb | 153 +++++++++++++++--- 000108/chunglab/demo/2021-09-27_dandi-demo.md | 58 ++++++- 2 files changed, 183 insertions(+), 28 deletions(-) diff --git a/000108/chunglab/demo/2021-09-27_dandi-demo.ipynb b/000108/chunglab/demo/2021-09-27_dandi-demo.ipynb index 721ae1f..da2d46c 100644 --- a/000108/chunglab/demo/2021-09-27_dandi-demo.ipynb +++ b/000108/chunglab/demo/2021-09-27_dandi-demo.ipynb @@ -369,7 +369,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 5, @@ -554,7 +554,8 @@ "# This is what our sidecar looks like - we're going to use\n", "# the pixel size and pieces of the chunk transform matrix.\n", "\n", - "get_json(get_asset_url(\"000108\", chunks_41[0][\"sidecar_asset_id\"]))" + "meta = get_json(get_asset_url(\"000108\", chunks_41[0][\"sidecar_asset_id\"]))\n", + "meta" ] }, { @@ -802,16 +803,49 @@ { "cell_type": "code", "execution_count": 10, - "id": "2313647b-8fac-47dd-8252-ec950f4c4dd0", + "id": "c0abccb2-174d-479b-9f08-1a6c03e56251", + "metadata": {}, + "outputs": [], + "source": [ + "level = 6\n", + "extents6 = find_extents(\"000108\", \"MITU01\", \"41\", \"YO\", level)\n", + "overview = read(\"000108\", \"MITU01\", \"41\", \"YO\", 0, extents6[\"x1\"], 0, extents6[\"y1\"], 0, extents6[\"z1\"], level)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "36a5fdfa-ab4d-4360-b0ae-d060d906e8a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(32, 261, 571)" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "overview.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "3de35868-c2e9-4286-8205-7181e1d30222", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 10, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" }, @@ -841,17 +875,88 @@ } ], "source": [ - "extents6 = find_extents(\"000108\", \"MITU01\", \"41\", \"YO\", 6)\n", - "overview = read(\"000108\", \"MITU01\", \"41\", \"YO\", 0, extents6[\"x1\"], 0, extents6[\"y1\"], 16, 17, 6)[0]\n", "pyplot.figure(figsize=(12, 12))\n", - "pyplot.imshow(overview, cmap='cubehelix')\n", + "pyplot.imshow(overview[16, :], cmap='cubehelix')\n", "pyplot.figure(figsize=(12, 12))\n", "pyplot.imshow(photo[::-1, ::-1])" ] }, + { + "cell_type": "markdown", + "id": "cfc4afdc-f01d-4bd5-a04b-9a8f692ebbf6", + "metadata": {}, + "source": [ + "### Use neuroglancer to display the stitched volume" + ] + }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, + "id": "e93d0508-ad0e-4fb1-b002-5e53d7fe3076", + "metadata": {}, + "outputs": [], + "source": [ + "import neuroglancer\n", + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "d3b6abed-2d8e-4b7c-80a6-c80a6e3a283d", + "metadata": {}, + "outputs": [], + "source": [ + "viewer = neuroglancer.Viewer()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "7a556758-c6b0-4286-8cc7-51c6b724f762", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Neuroglancer URL: https://hub.dandiarchive.org/user/satra/proxy/41511/v/19ce6d52b2adb874bb139eb855f64eda1c6f735e/\n" + ] + } + ], + "source": [ + "# This volume handle can be used to notify the viewer that the data has changed.\n", + "volume = neuroglancer.LocalVolume(\n", + " overview,\n", + " dimensions=neuroglancer.CoordinateSpace(\n", + " names=['x', 'y', 'z'],\n", + " units=['um', 'um', 'um'],\n", + " scales=[val*level for val in meta[\"PixelSize\"]],\n", + " ),\n", + " voxel_offset=[0,0,0]\n", + " )\n", + "with viewer.txn() as s:\n", + " s.layers['volume'] = neuroglancer.ImageLayer(\n", + " source=volume,\n", + " # Define a custom shader to display this mask array as red+alpha.\n", + " shader=\"\"\"\n", + "#uicontrol vec3 color color(default=\"green\")\n", + "#uicontrol float brightness slider(min=-1, max=1)\n", + "#uicontrol float contrast slider(min=-9, max=-5, step=0.1)\n", + "void main() {\n", + " emitRGB(color *\n", + " (toNormalized(getDataValue(0)) + brightness) *\n", + " exp(contrast));\n", + "}\n", + "\"\"\",\n", + " )\n", + " s.position = np.array(overview.shape)/2\n", + "print(\"Neuroglancer URL:\", viewer.get_viewer_url().replace(\"http://127.0.0.1:\", f\"https://hub.dandiarchive.org/user/{os.environ['GITHUB_USER']}/proxy/\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 16, "id": "hydraulic-terrain", "metadata": { "tags": [] @@ -865,17 +970,17 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "id": "9566618d-3025-42f0-ab6a-ad9044fc3686", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 12, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" }, @@ -910,17 +1015,17 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "id": "81670a03-690f-4ef2-84c9-7a5a83e0ee4d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 13, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" }, @@ -945,7 +1050,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 19, "id": "9a50a25c-7ffe-4a55-a15d-ea889e33117d", "metadata": {}, "outputs": [], @@ -956,7 +1061,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 20, "id": "1444c4ec-3083-46ff-ad91-1af41b15e132", "metadata": {}, "outputs": [], @@ -966,7 +1071,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 21, "id": "7cfc7a25-4f1c-424b-b54c-bedf4c0b092e", "metadata": {}, "outputs": [], @@ -989,17 +1094,17 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 22, "id": "13b5c5cd-21d6-43a0-a276-47991e016053", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 17, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" }, @@ -1023,17 +1128,17 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 23, "id": "38e58a07-938a-4aaf-9032-992e6183c25a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 18, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" }, diff --git a/000108/chunglab/demo/2021-09-27_dandi-demo.md b/000108/chunglab/demo/2021-09-27_dandi-demo.md index 59ccd4e..2aa34f0 100644 --- a/000108/chunglab/demo/2021-09-27_dandi-demo.md +++ b/000108/chunglab/demo/2021-09-27_dandi-demo.md @@ -230,7 +230,8 @@ chunks_41 # This is what our sidecar looks like - we're going to use # the pixel size and pieces of the chunk transform matrix. -get_json(get_asset_url("000108", chunks_41[0]["sidecar_asset_id"])) +meta = get_json(get_asset_url("000108", chunks_41[0]["sidecar_asset_id"])) +meta ``` ```python @@ -447,14 +448,63 @@ def find_extents(dandiset, subject, sample, stain, level=0): ``` ```python -extents6 = find_extents("000108", "MITU01", "41", "YO", 6) -overview = read("000108", "MITU01", "41", "YO", 0, extents6["x1"], 0, extents6["y1"], 16, 17, 6)[0] +level = 6 +extents6 = find_extents("000108", "MITU01", "41", "YO", level) +overview = read("000108", "MITU01", "41", "YO", 0, extents6["x1"], 0, extents6["y1"], 0, extents6["z1"], level) +``` + +```python +overview.shape +``` + +```python pyplot.figure(figsize=(12, 12)) -pyplot.imshow(overview, cmap='cubehelix') +pyplot.imshow(overview[16, :], cmap='cubehelix') pyplot.figure(figsize=(12, 12)) pyplot.imshow(photo[::-1, ::-1]) ``` +### Use neuroglancer to display the stitched volume + +```python +import neuroglancer +import os +``` + +```python +viewer = neuroglancer.Viewer() +``` + +```python +# This volume handle can be used to notify the viewer that the data has changed. +volume = neuroglancer.LocalVolume( + overview, + dimensions=neuroglancer.CoordinateSpace( + names=['x', 'y', 'z'], + units=['um', 'um', 'um'], + scales=[val*level for val in meta["PixelSize"]], + ), + voxel_offset=[0,0,0] + ) +with viewer.txn() as s: + s.layers['volume'] = neuroglancer.ImageLayer( + source=volume, + # Define a custom shader to display this mask array as red+alpha. + shader=""" +#uicontrol vec3 color color(default="green") +#uicontrol float brightness slider(min=-1, max=1) +#uicontrol float contrast slider(min=-9, max=-5, step=0.1) +void main() { + emitRGB(color * + (toNormalized(getDataValue(0)) + brightness) * + exp(contrast)); +} +""", + ) + s.position = np.array(overview.shape)/2 +print("Neuroglancer URL:", viewer.get_viewer_url().replace("http://127.0.0.1:", f"https://hub.dandiarchive.org/user/{os.environ['GITHUB_USER']}/proxy/")) +``` + ```python tags=[] # This is approximately at the stitching between chunks 1 and 2 x, y, z = 16080, 1957, 1024