Skip to content

Commit 08670f9

Browse files
committed
ENH: DSA example notebook
1 parent e67c9da commit 08670f9

File tree

2 files changed

+322
-0
lines changed

2 files changed

+322
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*~
22
build/
3+
.ipynb_checkpoints
4+
examples/.ipynb_checkpoints
Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Install dependencies for this example\n",
10+
"# Note: This does not include itkwidgets, itself\n",
11+
"import sys\n",
12+
"!{sys.executable} -m pip install --upgrade itk-minimalpathextraction"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from pathlib import Path\n",
22+
"\n",
23+
"import itk\n",
24+
"import numpy as np\n",
25+
"\n",
26+
"from itkwidgets import view"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"DATA_DIR = Path('../test/Input')"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"data": {
45+
"application/vnd.jupyter.widget-view+json": {
46+
"model_id": "0072c3bbb27a4bd4b35a965458611254",
47+
"version_major": 2,
48+
"version_minor": 0
49+
},
50+
"text/plain": [
51+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageUC2; proxy …"
52+
]
53+
},
54+
"metadata": {},
55+
"output_type": "display_data"
56+
}
57+
],
58+
"source": [
59+
"dsa = itk.imread(str(DATA_DIR / 'Real-DSA-01.jpg'))\n",
60+
"view(dsa, ui_collapsed=True)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 4,
66+
"metadata": {},
67+
"outputs": [
68+
{
69+
"data": {
70+
"application/vnd.jupyter.widget-view+json": {
71+
"model_id": "6e9289c05fec4f19b77ebab14a5267e4",
72+
"version_major": 2,
73+
"version_minor": 0
74+
},
75+
"text/plain": [
76+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
77+
]
78+
},
79+
"metadata": {},
80+
"output_type": "display_data"
81+
}
82+
],
83+
"source": [
84+
"dsa_speed = itk.imread(str(DATA_DIR / 'Real-DSA-01-Speed-02.mhd'))\n",
85+
"view(dsa_speed, ui_collapsed=True)"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 5,
91+
"metadata": {},
92+
"outputs": [
93+
{
94+
"name": "stdout",
95+
"output_type": "stream",
96+
"text": [
97+
"Path: [394.00, 203.00] [388.00, 229.00] [356.00, 217.00] [289.00, 252.00] [194.00, 309.00] [162.00, 318.00] [232.00, 490.00]\r\n"
98+
]
99+
}
100+
],
101+
"source": [
102+
"path_file = str(DATA_DIR / 'Real-DSA-01.path')\n",
103+
"!cat {path_file}"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 6,
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"PointType = itk.Point[itk.D,2]\n",
113+
"PathInformationType = itk.SpeedFunctionPathInformation[PointType]"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 7,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"path_information = PathInformationType.New()\n",
123+
"with open(path_file, 'r') as fp:\n",
124+
" for line in fp:\n",
125+
" line = line.replace(\"Path: \", \"\")\n",
126+
" line = line.replace(\"[\", \"\").strip()\n",
127+
" points = line.split(']')[:-1]\n",
128+
" way_points = np.empty((0, 2))\n",
129+
" for index, point in enumerate(points):\n",
130+
" point_float = PointType()\n",
131+
" point_float[0] = float(point.split(',')[0])\n",
132+
" point_float[1] = float(point.split(',')[1])\n",
133+
" if index == 0:\n",
134+
" start_point = np.array(point_float).reshape(1,2)\n",
135+
" path_information.SetStartPoint(point_float)\n",
136+
" elif index == len(points)-1:\n",
137+
" end_point = np.array(point_float).reshape(1,2)\n",
138+
" path_information.SetEndPoint(point_float)\n",
139+
" else:\n",
140+
" way_points = np.vstack((way_points, np.array(point_float).reshape(1,2)))\n",
141+
" path_information.AddWayPoint(point_float)"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 8,
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"data": {
151+
"application/vnd.jupyter.widget-view+json": {
152+
"model_id": "d907dae42c9243cf862bbb75e3f2ba77",
153+
"version_major": 2,
154+
"version_minor": 0
155+
},
156+
"text/plain": [
157+
"Viewer(geometries=[], gradient_opacity=0.22, point_set_colors=array([[1. , 0. , 0. ],\n",
158+
" [1. , 0. , 0.5],\n",
159+
""
160+
]
161+
},
162+
"metadata": {},
163+
"output_type": "display_data"
164+
}
165+
],
166+
"source": [
167+
"view(dsa_speed,\n",
168+
" point_sets=[start_point, way_points, end_point],\n",
169+
" point_set_colors=[(1.0,0.0,0.0), (1.0,0.0,0.5), (1.0,0.0,0.8)],\n",
170+
" ui_collapsed=True)"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 9,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"interpolator = itk.LinearInterpolateImageFunction.New(dsa_speed)\n",
180+
"interpolator.SetInputImage(dsa_speed)"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 10,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"cost_function = itk.SingleImageCostFunction[type(dsa_speed)].New(interpolator=interpolator)\n",
190+
"cost_function.SetInterpolator(interpolator)"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": 11,
196+
"metadata": {},
197+
"outputs": [],
198+
"source": [
199+
"spacing = list(dsa_speed.GetSpacing())\n",
200+
"min_spacing = min(spacing)\n",
201+
"step_length_factor = 0.25\n",
202+
"step_length_relax = 0.5\n",
203+
"number_of_iterations = 4000\n",
204+
"\n",
205+
"optimizer = itk.RegularStepGradientDescentOptimizer.New(\n",
206+
" number_of_iterations=number_of_iterations,\n",
207+
" maximum_step_length=1.0*step_length_factor*min_spacing,\n",
208+
" minimum_step_length=0.5*step_length_factor*min_spacing,\n",
209+
" relaxation_factor=step_length_relax)"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 12,
215+
"metadata": {},
216+
"outputs": [],
217+
"source": [
218+
"termination_value = 3.0\n",
219+
"path_filter = itk.SpeedFunctionToPathFilter.New(dsa_speed,\n",
220+
" cost_function=cost_function,\n",
221+
" optimizer=optimizer,\n",
222+
" termination_value=termination_value)\n",
223+
"path_filter.SetInput(dsa_speed)"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 13,
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"path_filter.AddPathInformation(path_information)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 14,
238+
"metadata": {},
239+
"outputs": [
240+
{
241+
"name": "stdout",
242+
"output_type": "stream",
243+
"text": [
244+
"CPU times: user 64.8 ms, sys: 0 ns, total: 64.8 ms\n",
245+
"Wall time: 64.2 ms\n"
246+
]
247+
}
248+
],
249+
"source": [
250+
"%time path_filter.Update()"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": 15,
256+
"metadata": {},
257+
"outputs": [
258+
{
259+
"data": {
260+
"text/plain": [
261+
"2232"
262+
]
263+
},
264+
"execution_count": 15,
265+
"metadata": {},
266+
"output_type": "execute_result"
267+
}
268+
],
269+
"source": [
270+
"path = path_filter.GetOutput(0)\n",
271+
"path.GetVertexList().Size()"
272+
]
273+
},
274+
{
275+
"cell_type": "code",
276+
"execution_count": 16,
277+
"metadata": {},
278+
"outputs": [
279+
{
280+
"data": {
281+
"application/vnd.jupyter.widget-view+json": {
282+
"model_id": "6ed292f79b4647408ba500afa897af1a",
283+
"version_major": 2,
284+
"version_minor": 0
285+
},
286+
"text/plain": [
287+
"Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…"
288+
]
289+
},
290+
"metadata": {},
291+
"output_type": "display_data"
292+
}
293+
],
294+
"source": [
295+
"view(dsa, geometries=path, ui_collapsed=True)"
296+
]
297+
}
298+
],
299+
"metadata": {
300+
"kernelspec": {
301+
"display_name": "Python 3",
302+
"language": "python",
303+
"name": "python3"
304+
},
305+
"language_info": {
306+
"codemirror_mode": {
307+
"name": "ipython",
308+
"version": 3
309+
},
310+
"file_extension": ".py",
311+
"mimetype": "text/x-python",
312+
"name": "python",
313+
"nbconvert_exporter": "python",
314+
"pygments_lexer": "ipython3",
315+
"version": "3.6.8"
316+
}
317+
},
318+
"nbformat": 4,
319+
"nbformat_minor": 2
320+
}

0 commit comments

Comments
 (0)