Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update docs #281

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 59 additions & 150 deletions docs/notebooks/pipelines/seg_pipeline_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/alishakodibagkar/opt/anaconda3/envs/brainlit/lib/python3.8/site-packages/python_jsonschema_objects/__init__.py:50: UserWarning: Schema version http://json-schema.org/draft-04/schema not recognized. Some keywords and features may not be supported.\n",
" warnings.warn(\n"
]
}
],
"source": [
"import brainlit\n",
"from brainlit.utils.session import NeuroglancerSession\n",
"from brainlit.viz.swc import *\n",
"from brainlit.utils.Neuron_trace import NeuronTrace\n",
"from brainlit.viz.visualize import *\n",
"from brainlit.algorithms.generate_fragments import adaptive_thresh\n",
"import napari"
"import napari\n",
"from napari.utils import nbscreenshot\n",
"%gui qt5"
]
},
{
Expand Down Expand Up @@ -65,8 +76,8 @@
"source": [
"seg_id = 13\n",
"mip = 2\n",
"df = read_s3(url+\"_segments\", seg_id, mip)\n",
"#df.head()\n",
"s3_trace = NeuronTrace(url+\"_segments\", seg_id, mip)\n",
"df = s3_trace.get_df()\n",
"df['sample'].size # the number of vertex IDs [1, 2, ..., df['sample'].size]"
]
},
Expand Down Expand Up @@ -113,25 +124,15 @@
"outputs": [],
"source": [
"ngl = NeuroglancerSession(url, mip=mip)\n",
"buffer = [10, 10, 10]\n",
"buffer = 10\n",
"img, bounds, vox_in_img_list = ngl.pull_vertex_list(seg_id, vertex_list, buffer = buffer, expand = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare plotting functions\n",
"We add a useful plotting function to view the downloaded volume in Napari."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(vox_in_img_list)"
"## Plot"
]
},
{
Expand All @@ -141,96 +142,6 @@
"outputs": [],
"source": [
"# Reference: https://github.com/NeuroDataDesign/mouselit/blob/master/bijan/mouse_test/final%20notebook.ipynb\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import SimpleITK as sitk\n",
"import numpy as np\n",
"import napari\n",
"\n",
"\n",
"def plot_2d(img, title=None, margin=0.05, dpi=80):\n",
" nda = sitk.GetArrayFromImage(img)\n",
" spacing = img.GetSpacing()\n",
"\n",
" if nda.ndim == 3:\n",
" c = nda.shape[-1]\n",
"\n",
" if c not in (3, 4):\n",
" nda = nda[nda.shape[0] // 2, :, :]\n",
"\n",
" elif nda.ndim == 4:\n",
" c = nda.shape[-1]\n",
"\n",
" if c not in (3, 4):\n",
" raise RuntimeError(\"Unable to show 3D-vector Image\")\n",
"\n",
" nda = nda[nda.shape[0] // 2, :, :, :]\n",
"\n",
" xsize = nda.shape[1] * 2\n",
" ysize = nda.shape[0] * 2\n",
"\n",
" figsize = (1 + margin) * xsize / dpi, (1 + margin) * ysize / dpi\n",
"\n",
" figsize = (1 + margin) * xsize / dpi, (1 + margin) * ysize / dpi\n",
"\n",
" fig = plt.figure(figsize=figsize, dpi=dpi)\n",
" ax = plt.gca()\n",
"\n",
" extent = (0, xsize * spacing[0], ysize * spacing[1], 0)\n",
"\n",
" t = ax.imshow(nda, extent=extent, interpolation=None)\n",
"\n",
" if nda.ndim == 2:\n",
" t.set_cmap(\"gray\")\n",
"\n",
" if title:\n",
" plt.title(title)\n",
"\n",
" plt.show()\n",
"\n",
"\n",
"def plot_3d(img, xslices=[], yslices=[], zslices=[], title=None, margin=0.05, dpi=80):\n",
" \n",
" if not isinstance(img,sitk.SimpleITK.Image):\n",
" print(type(img))\n",
" raise Exception(\"Sorry, input must be an sitk image\")\n",
" else:\n",
" \n",
" img_xslices = [img[s, :, :] for s in xslices]\n",
" img_yslices = [img[:, s, :] for s in yslices]\n",
" img_zslices = [img[:, :, s] for s in zslices]\n",
"\n",
" maxlen = max(len(img_xslices), len(img_yslices), len(img_zslices))\n",
"\n",
" img_null = sitk.Image([0, 0], img.GetPixelID(), img.GetNumberOfComponentsPerPixel())\n",
"\n",
" img_slices = []\n",
" d = 0\n",
"\n",
" if len(img_xslices):\n",
" img_slices += img_xslices + [img_null] * (maxlen - len(img_xslices))\n",
" d += 1\n",
"\n",
" if len(img_yslices):\n",
" img_slices += img_yslices + [img_null] * (maxlen - len(img_yslices))\n",
" d += 1\n",
"\n",
" if len(img_zslices):\n",
" img_slices += img_zslices + [img_null] * (maxlen - len(img_zslices))\n",
" d += 1\n",
"\n",
" if maxlen != 0:\n",
" if img.GetNumberOfComponentsPerPixel() == 1:\n",
" img = sitk.Tile(img_slices, [maxlen, d])\n",
" else:\n",
" img_comps = []\n",
" for i in range(0, img.GetNumberOfComponentsPerPixel()):\n",
" img_slices_c = [sitk.VectorIndexSelectionCast(s, i) for s in img_slices]\n",
" img_comps.append(sitk.Tile(img_slices_c, [maxlen, d]))\n",
" img = sitk.Compose(img_comps)\n",
"\n",
" plot_2d(img, title, margin, dpi) \n",
"\n",
"def napari_viewer(img, labels=None, shapes=None, label_name=\"Segmentation\"):\n",
" viewer = napari.view_image(np.squeeze(np.array(img)))\n",
" if labels is not None:\n",
Expand All @@ -250,16 +161,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"metadata": {},
"outputs": [],
"source": [
"%gui qt\n",
"viewer = napari.Viewer(ndisplay=3)\n",
"viewer.add_image(img)\n",
"viewer.add_image(np.squeeze(np.array(img)))\n",
"nbscreenshot(viewer)"
]
},
Expand All @@ -269,11 +175,7 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import SimpleITK as sitk\n",
"arr = np.ones((5, 5, 5))\n",
"im = sitk.GetImageFromArray(arr)\n",
"plot_3d(im)"
"n=napari_viewer(img)"
]
},
{
Expand Down Expand Up @@ -313,7 +215,7 @@
},
"outputs": [],
"source": [
"We get a `corrected_subneuron_df` that contains `(x,y,z)` coordinates within the downloaded volume for the vertices in the SWC."
"# We get a `corrected_subneuron_df` that contains `(x,y,z)` coordinates within the downloaded volume for the vertices in the SWC."
]
},
{
Expand Down Expand Up @@ -344,7 +246,7 @@
},
"outputs": [],
"source": [
"We get a `corrected_subneuron_df` that contains `(x,y,z)` coordinates within the downloaded volume for the vertices in the SWC."
"# We get a `corrected_subneuron_df` that contains `(x,y,z)` coordinates within the downloaded volume for the vertices in the SWC."
]
},
{
Expand All @@ -371,7 +273,7 @@
"metadata": {},
"outputs": [],
"source": [
"We get a `corrected_subneuron_df` that contains `(x,y,z)` coordinates within the downloaded volume for the vertices in the SWC."
"# We get a `corrected_subneuron_df` that contains `(x,y,z)` coordinates within the downloaded volume for the vertices in the SWC."
]
},
{
Expand Down Expand Up @@ -401,7 +303,10 @@
"metadata": {},
"outputs": [],
"source": [
"corrected_subneuron_df = generate_df_subset(subneuron_df, vox_in_img_list)\n",
"transpose = vox_in_img_list.T\n",
"vox_in_img_list_t = transpose.tolist()\n",
"\n",
"corrected_subneuron_df = s3_trace.generate_df_subset(list(vox_in_img_list_t), subneuron_start = 0, subneuron_end = 5)\n",
"print(corrected_subneuron_df)"
]
},
Expand All @@ -418,11 +323,11 @@
"metadata": {},
"outputs": [],
"source": [
"G = df_to_graph(corrected_subneuron_df)\n",
"G = s3_trace._df_to_graph(df_voxel=corrected_subneuron_df)\n",
"print('Number of nodes:', len(G.nodes))\n",
"print('Number of edges:', len(G.edges))\n",
"print('Sample 1 coordinates (x,y,z):', G.nodes[1])\n",
"paths = graph_to_paths(G)\n",
"paths = s3_trace._graph_to_paths(G)\n",
"print('Number of paths:', len(paths))"
]
},
Expand All @@ -440,10 +345,7 @@
"outputs": [],
"source": [
"%gui qt\n",
"viewer = napari.Viewer(ndisplay=3)\n",
"viewer.add_image(img)\n",
"viewer.add_shapes(paths)\n",
"nbscreenshot(viewer)"
"napari_viewer(img, shapes=paths)"
]
},
{
Expand All @@ -455,20 +357,20 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"seed = [adaptive_thresh.get_seed(sample)[1] for sample in vox_in_img_list]\n",
"print(seed)"
"Next, we compute a confidence-connected threshold segmentation."
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Next, we compute a confidence-connected threshold segmentation."
"seed = [adaptive_thresh.get_seed(sample)[1] for sample in vox_in_img_list]\n",
"print(seed)"
]
},
{
Expand All @@ -477,7 +379,10 @@
"metadata": {},
"outputs": [],
"source": [
"labels = adaptive_thresh.confidence_connected_threshold(img, seed, num_iter=1, multiplier=0.5)"
"transpose = vox_in_img_list.T\n",
"vox_in_img_list = transpose.tolist()\n",
"\n",
"labels = adaptive_thresh.confidence_connected_threshold(img, seed, num_iter=1,multiplier=0.5)"
]
},
{
Expand All @@ -494,11 +399,7 @@
"outputs": [],
"source": [
"%gui qt\n",
"viewer = napari.Viewer(ndisplay=3)\n",
"viewer.add_image(img)\n",
"viewer.add_labels(labels, name=\"Confidence-Connected Threshold\")\n",
"viewer.add_shapes(paths)\n",
"nbscreenshot(viewer)"
"viewer = napari_viewer(img, labels=labels, shapes=paths, label_name=\"Confidence-Connected Threshold\")"
]
},
{
Expand Down Expand Up @@ -545,8 +446,16 @@
"outputs": [],
"source": [
"%%capture\n",
"ngl_upload = NeuroglancerSession(url+\"_seg\", mip=mip)\n",
"ngl_upload.push(manual_labels, bounds);"
"ngl_upload = NeuroglancerSession(url+\"_segments\", mip=mip)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#ngl_upload.push(manual_labels, bounds);"
]
},
{
Expand All @@ -562,7 +471,7 @@
"metadata": {},
"outputs": [],
"source": [
"downloaded_labels = ngl_upload.pull_bounds_seg(bounds)"
"#downloaded_labels = ngl_upload.pull_bounds_seg(bounds)"
]
},
{
Expand All @@ -571,7 +480,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(np.all(manual_labels == downloaded_labels))"
"#print(np.all(manual_labels == downloaded_labels))"
]
},
{
Expand All @@ -598,7 +507,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.8.5"
},
"pycharm": {
"stem_cell": {
Expand All @@ -612,4 +521,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
Loading