from jupyter_dash import JupyterDash
import dash
import dash_core_components as dcc
import dash_html_components as html
import nibabel as nib
import numpy as np
import plotly.express as px
from dash.dependencies import Input, Output, State
from scipy import ndimage
from skimage import draw
# Uncomment the next line to detect the proxy configuration on Binder
#JupyterDash.infer_jupyter_proxy_config()
def path_to_indices(path):
"""From SVG path to numpy array of coordinates, each row being a (row, col) point
"""
indices_str = [
el.replace('M', '').replace('Z', '').split(',') for el in path.split('L')
]
return np.floor(np.array(indices_str, dtype=float)).astype(np.int)
def path_to_mask(path, shape):
"""From SVG path to a boolean array where all pixels enclosed by the path
are True, and the other pixels are False.
"""
cols, rows = path_to_indices(path).T
rr, cc = draw.polygon(rows, cols)
mask = np.zeros(shape, dtype=np.bool)
mask[rr, cc] = True
mask = ndimage.binary_fill_holes(mask)
return mask
# Loading the data and creating the figures
img = nib.load('T1map_cropped_reoriented.nii.gz')
data = img.dataobj
default_view = 2
default_slice = int(data.shape[default_view]/2)
view = np.take(data, default_slice, axis=default_view)
view = view.T
fig = px.imshow(view, binary_string=True, origin='lower')
fig.update_layout(dragmode='drawopenpath',
newshape=dict(opacity=0.8, line=dict(color='red', width=2)))
config = {
'modeBarButtonsToAdd': [
'drawopenpath',
'drawclosedpath',
'eraseshape'
]
}
fig_hist = px.histogram(data.get_unscaled().ravel())
fig_hist.update_layout(showlegend=False)
app = JupyterDash(__name__)
server = app.server
# Defining the layout of the dashboard
app.layout = html.Div([html.Div([
html.Div(
[dcc.Dropdown(id="plane-dropdown", options=[
{'label': 'Axial', 'value': 2},
{'label': 'Coronal', 'value': 1},
{'label': 'Sagittal', 'value': 0}],
value=2),
dcc.Graph(id='graph-mri', figure=fig, config=config),
dcc.Slider(
id='slice-slider',
min=0,
max=data.shape[default_view] - 1,
value=int(data.shape[default_view]/2),
step=1)],
style={'width': '60%', 'display': 'inline-block', 'padding': '0 0'},
),
html.Div(
[dcc.Graph(id='graph-histogram', figure=fig_hist)],
style={'width': '40%', 'display': 'inline-block', 'padding': '0 0'},
), html.Div(id='test')
])
])