{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from glob import glob\n", "import pandas as pd\n", "import os\n", "from skimage.io import imread\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleframeframe_idpath
0MI50MI5_0_00[../data/MI5_0_00_1500.rec.8bit.tif, ../data/M...
1MI51MI5_1_00[../data/MI5_1_00_1500.rec.8bit.tif, ../data/M...
2MI51MI5_1_01[../data/MI5_1_01_1500.rec.8bit.tif, ../data/M...
3MI51MI5_1_02[../data/MI5_1_02_1500.rec.8bit.tif, ../data/M...
4MI70MI7_0_00[../data/MI7_0_00_1300.rec.8bit.tif, ../data/M...
5MI71MI7_1_00[../data/MI7_1_00_1300.rec.8bit.tif, ../data/M...
6MI71MI7_1_01[../data/MI7_1_01_1300.rec.8bit.tif, ../data/M...
7MI71MI7_1_02[../data/MI7_1_02_1300.rec.8bit.tif, ../data/M...
\n", "
" ], "text/plain": [ " sample frame frame_id path\n", "0 MI5 0 MI5_0_00 [../data/MI5_0_00_1500.rec.8bit.tif, ../data/M...\n", "1 MI5 1 MI5_1_00 [../data/MI5_1_00_1500.rec.8bit.tif, ../data/M...\n", "2 MI5 1 MI5_1_01 [../data/MI5_1_01_1500.rec.8bit.tif, ../data/M...\n", "3 MI5 1 MI5_1_02 [../data/MI5_1_02_1500.rec.8bit.tif, ../data/M...\n", "4 MI7 0 MI7_0_00 [../data/MI7_0_00_1300.rec.8bit.tif, ../data/M...\n", "5 MI7 1 MI7_1_00 [../data/MI7_1_00_1300.rec.8bit.tif, ../data/M...\n", "6 MI7 1 MI7_1_01 [../data/MI7_1_01_1300.rec.8bit.tif, ../data/M...\n", "7 MI7 1 MI7_1_02 [../data/MI7_1_02_1300.rec.8bit.tif, ../data/M..." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_images = glob(os.path.join('..', 'data', '*.tif'))\n", "img_df = pd.DataFrame([dict(path = c_file) for c_file in all_images])\n", "img_df['basename'] = img_df['path'].map(lambda x: os.path.basename(x).split('.')[0])\n", "img_df['frame_id'] = img_df['basename'].map(lambda x: '_'.join(x.split('_')[0:-1]))\n", "img_df['sample'] = img_df['basename'].map(lambda x: x.split('_')[0])\n", "img_df['slice'] = img_df['basename'].map(lambda x: int(x.split('_')[3]))\n", "img_df['frame'] = img_df['basename'].map(lambda x: int(x.split('_')[1]))\n", "# group by prefix\n", "img_3d_df = img_df.sort_values(['sample', 'frame_id', 'slice']).groupby(['sample', 'frame', 'frame_id'])['path'].apply(list).reset_index()\n", "img_3d_df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from skimage.filters import threshold_minimum\n", "from skimage.morphology.convex_hull import convex_hull_image\n", "from skimage.morphology import binary_closing, binary_opening, disk, label\n", "from skimage.measure import regionprops\n", "\n", "from skimage.color import label2rgb\n", "from scipy import ndimage as ndi\n", "\n", "def threshold_slice_2d(raw_slice):\n", " in_slice = ndi.median_filter(raw_slice, [7, 7])\n", " # basic threshold\n", " thresh_min = threshold_minimum(in_slice)\n", " # make solid component\n", " solid_part = binary_closing(in_slice>thresh_min, disk(4))\n", " solid_part = ndi.binary_fill_holes(solid_part)\n", " # calculate porous component inside solid\n", " porous_part = (in_slice < thresh_min)*solid_part\n", " #porous_part = binary_opening(porous_part, disk(2))\n", " porous_part = binary_closing(porous_part, disk(2))\n", " # create label by combining\n", " label_slice = solid_part.astype(np.uint8).clip(0,1)\n", " label_slice += (porous_part).astype(np.uint8).clip(0,1)\n", " return label_slice, thresh_min\n", "\n", "def paths_to_vol(in_paths):\n", " return np.stack([threshold_slice_2d(imread(x))[0] for x in in_paths],0)\n", "def paths_to_raw_vol(in_paths):\n", " return np.stack([imread(x) for x in in_paths], 0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as ipw\n", "import ipyvolume as p3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# MI5" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/mader/anaconda/envs/qbi2018/lib/python3.6/site-packages/ipyvolume/serialize.py:66: RuntimeWarning: invalid value encountered in true_divide\n", " gradient = gradient / np.sqrt(gradient[0]**2 + gradient[1]**2 + gradient[2]**2)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b565107fb7e3481c9aa367a5398e830e", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type VBox.

\n", "

\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "VBox(children=(Figure(camera_center=[0.0, 0.0, 0.0], data_max=255.0, height=500, matrix_projection=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], matrix_world=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], style={'axes': {'visible': True, 'label': {'color': 'black'}, 'ticklabel': {'color': 'black'}, 'color': 'black'}, 'box': {'visible': True}, 'background-color': 'white'}, tf=TransferFunction(rgba=array([[0. , 0. , 0. , 0. ],\n", " [0.00392157, 0.00392157, 0.00392157, 0. ],\n", " [0.00784314, 0.00784314, 0.00784314, 0. ],\n", " ...,\n", " [0.99215686, 0.99215686, 0.99215686, 0.49372549],\n", " [0.99607843, 0.99607843, 0.99607843, 0.49686275],\n", " [1. , 1. , 1. , 0.5 ]])), volume_data=array([[[ 76., 84., 88., ..., 81., 83., 85.],\n", " [ 72., 78., 85., ..., 76., 78., 80.],\n", " [ 74., 79., 86., ..., 73., 79., 83.],\n", " ...,\n", " [ 73., 82., 93., ..., 74., 74., 74.],\n", " [ 93., 103., 110., ..., 74., 73., 72.],\n", " [ 97., 104., 104., ..., 82., 76., 72.]],\n", "\n", " [[ 87., 80., 69., ..., 81., 79., 75.],\n", " [ 87., 87., 79., ..., 71., 70., 70.],\n", " [ 91., 93., 90., ..., 65., 69., 76.],\n", " ...,\n", " [ 77., 79., 86., ..., 71., 87., 99.],\n", " [ 77., 81., 86., ..., 61., 79., 95.],\n", " [ 81., 88., 87., ..., 59., 70., 88.]],\n", "\n", " [[ 82., 87., 85., ..., 109., 109., 89.],\n", " [ 73., 81., 84., ..., 118., 101., 71.],\n", " [ 69., 69., 76., ..., 105., 72., 46.],\n", " ...,\n", " [ 95., 87., 77., ..., 83., 87., 92.],\n", " [ 85., 76., 71., ..., 78., 82., 87.],\n", " [ 81., 78., 78., ..., 76., 79., 90.]],\n", "\n", " ...,\n", "\n", " [[ 81., 81., 80., ..., 90., 91., 95.],\n", " [ 72., 75., 72., ..., 93., 96., 99.],\n", " [ 74., 77., 74., ..., 100., 100., 98.],\n", " ...,\n", " [ 75., 81., 88., ..., 84., 81., 74.],\n", " [ 79., 85., 88., ..., 84., 84., 77.],\n", " [ 88., 89., 87., ..., 85., 86., 84.]],\n", "\n", " [[ 84., 88., 89., ..., 93., 94., 96.],\n", " [ 82., 85., 87., ..., 93., 95., 93.],\n", " [ 88., 87., 86., ..., 93., 94., 89.],\n", " ...,\n", " [ 73., 70., 75., ..., 90., 87., 88.],\n", " [ 69., 71., 80., ..., 89., 85., 85.],\n", " [ 67., 75., 85., ..., 90., 87., 87.]],\n", "\n", " [[ 76., 79., 83., ..., 89., 96., 99.],\n", " [ 71., 72., 78., ..., 94., 98., 94.],\n", " [ 73., 64., 63., ..., 101., 94., 84.],\n", " ...,\n", " [ 88., 84., 80., ..., 85., 93., 102.],\n", " [ 90., 86., 80., ..., 81., 85., 97.],\n", " [ 83., 78., 69., ..., 85., 83., 91.]]], dtype=float32), width=400, xlim=[0.0, 1.0], ylim=[0.0, 1.0], zlim=[0.0, 1.0]), HBox(children=(FloatSlider(value=0.5, description='ambient', max=1.0, step=0.001), FloatSlider(value=0.8, description='diffuse', max=1.0, step=0.001))), HBox(children=(FloatSlider(value=0.5, description='specular', max=1.0, step=0.001), FloatSlider(value=5.0, description='specular exp', max=10.0, step=0.001)))))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/mader/anaconda/envs/qbi2018/lib/python3.6/site-packages/ipyvolume/serialize.py:66: RuntimeWarning: invalid value encountered in true_divide\n", " gradient = gradient / np.sqrt(gradient[0]**2 + gradient[1]**2 + gradient[2]**2)\n" ] } ], "source": [ "_, c_row = next(img_3d_df.query('sample == \"MI5\"').iterrows())\n", "n_vol = paths_to_raw_vol(c_row['path'])\n", "fig = p3.figure()\n", "# create a custom LUT\n", "temp_tf = plt.cm.gray(np.linspace(0, 1, 256))\n", "temp_tf[:,3] = np.linspace(-.3, 0.5, 256).clip(0, 1) # make transparency more aggressive\n", "tf = p3.transferfunction.TransferFunction(rgba = temp_tf)\n", "p3.volshow(n_vol.astype(np.float32), \n", " lighting = True, \n", " max_opacity=0.5, \n", " tf = tf, \n", " controls = True)\n", "\n", "p3.show()\n", "p3.save('mi5_raw.html')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/mader/anaconda/envs/qbi2018/lib/python3.6/site-packages/ipyvolume/serialize.py:66: RuntimeWarning: invalid value encountered in true_divide\n", " gradient = gradient / np.sqrt(gradient[0]**2 + gradient[1]**2 + gradient[2]**2)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "08e091159d424cb389442832f1afdbc3", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type VBox.

\n", "

\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "VBox(children=(VBox(children=(HBox(children=(Label(value='levels:'), FloatSlider(value=0.1, max=1.0, step=0.001), FloatSlider(value=0.5, max=1.0, step=0.001), FloatSlider(value=0.9, max=1.0, step=0.001))), HBox(children=(Label(value='opacities:'), FloatSlider(value=0.01, max=0.2, step=0.001), FloatSlider(value=0.05, max=0.2, step=0.001), FloatSlider(value=0.05, max=0.2, step=0.001))))), Figure(ambient_coefficient=1.0, camera_center=[0.0, 0.0, 0.0], data_max=2.0, diffuse_coefficient=0.0, height=500, matrix_projection=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], matrix_world=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], specular_coefficient=0.0, style={'axes': {'visible': True, 'label': {'color': 'black'}, 'ticklabel': {'color': 'black'}, 'color': 'black'}, 'box': {'visible': True}, 'background-color': 'white'}, tf=TransferFunctionWidgetJs3(level3=0.9), volume_data=array([[[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]]], dtype=uint8), width=400, xlim=[0.0, 1.0], ylim=[0.0, 1.0], zlim=[0.0, 1.0])))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "n_vol = paths_to_vol(c_row['path'])\n", "p3.quickvolshow(n_vol)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# MI7" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/mader/anaconda/envs/qbi2018/lib/python3.6/site-packages/ipyvolume/serialize.py:66: RuntimeWarning: invalid value encountered in true_divide\n", " gradient = gradient / np.sqrt(gradient[0]**2 + gradient[1]**2 + gradient[2]**2)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "17ad3b41a47d4d15b5b7669c24e9971b", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type VBox.

\n", "

\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "VBox(children=(VBox(children=(HBox(children=(Label(value='levels:'), FloatSlider(value=0.1, max=1.0, step=0.001), FloatSlider(value=0.5, max=1.0, step=0.001), FloatSlider(value=0.9, max=1.0, step=0.001))), HBox(children=(Label(value='opacities:'), FloatSlider(value=0.01, max=0.2, step=0.001), FloatSlider(value=0.05, max=0.2, step=0.001), FloatSlider(value=0.05, max=0.2, step=0.001))))), Figure(camera_center=[0.0, 0.0, 0.0], data_max=2.0, height=500, matrix_projection=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], matrix_world=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], style={'axes': {'visible': True, 'label': {'color': 'black'}, 'ticklabel': {'color': 'black'}, 'color': 'black'}, 'box': {'visible': True}, 'background-color': 'white'}, tf=TransferFunctionWidgetJs3(level3=0.9), volume_data=array([[[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]]], dtype=uint8), width=400, xlim=[0.0, 1.0], ylim=[0.0, 1.0], zlim=[0.0, 1.0]), HBox(children=(FloatSlider(value=0.5, description='ambient', max=1.0, step=0.001), FloatSlider(value=0.8, description='diffuse', max=1.0, step=0.001))), HBox(children=(FloatSlider(value=0.5, description='specular', max=1.0, step=0.001), FloatSlider(value=5.0, description='specular exp', max=10.0, step=0.001)))))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "_, c_row = next(img_3d_df.query('sample == \"MI7\"').iterrows())\n", "n_vol = paths_to_vol(c_row['path'])\n", "p3.quickvolshow(n_vol, lighting=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }