-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
277 lines (228 loc) · 8.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkIOImage import vtkDICOMImageReader
import os
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import dash
from dash import dcc
from dash import html
import dash_bootstrap_components as dbc
from dash_slicer import VolumeSlicer
from dash.dependencies import Input, Output, State
def npArrAppend(np_arr, target):
if np_arr is None:
np_arr = target[np.newaxis, :]
else:
np_arr = np.append(np_arr, target[np.newaxis, :], axis=0)
return np_arr
app = dash.Dash(__name__, update_title=None)
server = app.server
# ------------- I/O and data massaging ---------------------------------------------------
# ------------- dicom Image ---------------------------------------------------
def DicomImage(inDirname="./assets/RockCT") -> np.array:
reader = vtkDICOMImageReader()
reader.SetDirectoryName(inDirname)
reader.Update()
files = os.listdir(inDirname)
dcmImage_CT = np.array(
reader.GetOutput().GetPointData().GetScalars()).reshape(
len(files), reader.GetHeight(), reader.GetWidth())
return dcmImage_CT
Hu = DicomImage()
slicer = VolumeSlicer(app, Hu, scene_id="rock")
slicer.graph.figure.update_layout(dragmode="drawrect",
newshape_line_color="cyan",
plot_bgcolor="rgb(0, 0, 0)")
slicer.graph.config.update(modeBarButtonsToAdd=["drawrect", "eraseshape"])
slider = dcc.Slider(id="slider0", max=slicer.nslices)
# Create a store with a specific ID so we can set the slicer position.
setpos_store = dcc.Store(id={
"context": "app",
"scene": slicer.scene_id,
"name": "setpos"
})
# # ------------- Percent Image ---------------------------------------------------
def PercentImage(
inDirname_image_np='./assets/image_np/',
inDirname_percent_np='./assets/percent_np/'
) -> (np.array, np.array, np.array, np.array):
AIR = -1024
imgs_np = None
solids_np = None
CTs_np = None
customdatas = None
files = os.listdir(inDirname_image_np)
# read img
for i in range(len(files)):
img = np.load(inDirname_image_np + f'img_{i}.npy')
img = img.reshape(img.shape[-2], img.shape[-1])
ct = ((img + 1) / 2.0) * (3000 - AIR) + AIR
percent = np.load(inDirname_percent_np + f'percent_{i}.npy').reshape(
3, img.shape[-2], img.shape[-1])
imgs_np = npArrAppend(imgs_np, img)
solids_np = npArrAppend(solids_np, percent[0])
CTs_np = npArrAppend(CTs_np, ct)
# customdata
customdata = np.array([ct, percent[0], percent[1], percent[2]])
customdatas = npArrAppend(customdatas, customdata)
return imgs_np, solids_np, CTs_np, customdata
imgs_np, solids_np, CTs_np, customdata = PercentImage()
slicer_percent = VolumeSlicer(app, solids_np * 1000, scene_id="rock")
slicer_percent.graph.figure.update_layout(dragmode="drawrect",
newshape_line_color="cyan",
plot_bgcolor="rgb(0, 0, 0)")
hovertemplate = "x: %{x} <br> y: %{y} <br> z: %{z} <br> ct: %{customdata[0]:.4f} <br> percent: %{customdata[1]:.4f}, %{customdata[2]:.4f}, %{customdata[3]:.4f}"
slicer_percent.graph.figure.update_traces(hoverinfo='text',
customdata=customdata,
hovertemplate=hovertemplate)
slicer.graph.figure.update_traces(overwrite=True,hoverinfo="text",
customdata=customdata,
hovertemplate=hovertemplate)
slicer_percent.graph.config.update(
modeBarButtonsToAdd=["drawrect", "eraseshape"])
slider_percent = dcc.Slider(id="slider1", max=slicer_percent.nslices)
# Create a store with a specific ID so we can set the slicer position.
# setpos_store_percent = dcc.Store(id={
# "context": "app",
# "scene": slicer_percent.scene_id,
# "name": "setpos_percent"
# })
# ------------- Porosity ---------------------------------------------------
targetCol = [
'Depth (cm)', 'Fractional porosity', 'CTG=1095 by Computer with weight',
'pix2pix unet 512 train 1095 test 1095',
'512_unet512_lsgan_1095_isResetValAboveSoildCt',
'pix2pix unet 512 train 970 test 970'
]
df = pd.read_excel('./assets/porosity.xlsx',
'MSCL_BH-3_15m',
usecols=targetCol).iloc[:495]
axial_card = dbc.Card([
dbc.CardHeader("Image feeded AI"),
dbc.CardBody([
html.Big("BVH3_15"),
html.Br(), slicer.graph, slicer.slider, setpos_store, *slicer.stores
]),
dbc.CardFooter([
html.H6([
"Use upper right toolbox to manipulate these images. Also, move the slider at the bottom to browse the image. ",
]),
dbc.Tooltip(
"Use the slider to scroll vertically through the image and look for the ground glass occlusions.",
target="tooltip-target-1",
),
]),
])
percent_info_card = dbc.Card([
dbc.CardHeader("Percentage AI give"),
dbc.CardBody([
html.Big("BVH3_15"),
html.Br(), slicer_percent.graph, slicer_percent.slider,
*slicer_percent.stores
]),
dbc.CardFooter([
html.H6([
"Use upper right toolbox to manipulate these images. Also, move the slider at the bottom to browse the image. ",
]),
dbc.Tooltip(
"Use the slider to scroll vertically through the image and look for the ground glass occlusions.",
target="tooltip-target-1",
),
]),
])
line_card = dbc.Card([
dbc.CardHeader("Slices of Porosity"),
dbc.CardBody([
dcc.Dropdown([*targetCol[1:], 'All'],
'Fractional porosity',
id='line-dropdown'),
dcc.Graph(id="graph-line",
figure=px.line(
df,
x=targetCol[0],
y=targetCol[1],
labels={
"x": "Depth (cm)",
"y": "Porosity"
},
template="plotly_white",
),
config={
"modeBarButtonsToAdd": [
"drawline",
"drawclosedpath",
"drawrect",
"eraseshape",
]
}),
]),
dbc.CardFooter([
dbc.Toast(
[
html.P(
"Click the point on the line. You can get the correspond"
" image.",
className="mb-0",
)
],
id="roi-warning",
header="You can check the point on the line",
icon="danger",
is_open=True,
dismissable=False,
),
"Click the point on the line",
]),
])
app.layout = html.Div([
dbc.Container(
[
dbc.Row([dbc.Col(axial_card),
dbc.Col(percent_info_card)]),
dbc.Row([html.Hr()]),
dbc.Row([dbc.Col(line_card)]),
],
fluid=True,
),
dcc.Store(id="annotations", data={}),
dcc.Store(id="occlusion-surface", data={}),
], )
@app.callback(Output('graph-line', 'figure'), Input('line-dropdown', 'value'))
def update_output(value):
if value != 'All':
fig = px.line(
df,
x=targetCol[0],
y=value,
labels={
"x": "Depth (cm)",
"y": "Porosity"
},
template="plotly_white",
)
else:
fig = go.Figure()
fig.update_layout(xaxis_title="Depth (cm)",
yaxis_title="Porosity",
template="plotly_white")
for target in targetCol[1:]:
fig.add_trace(
go.Scatter(x=df[targetCol[0]],
y=df[target],
mode='lines',
name=target))
return fig
@app.callback(Output(setpos_store.id, 'data'), Input('graph-line',
'clickData'))
def Click_changeImage(clickData):
if clickData != None:
print(clickData["points"][0]['pointIndex'])
return None, None, clickData["points"][0]['pointIndex']
return None, int(len(Hu) / 2), int(len(Hu) / 2)
if __name__ == "__main__":
app.run_server(debug=True, dev_tools_props_check=False)