-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
474 lines (441 loc) · 15.6 KB
/
cli.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import argparse
import logging
from importlib.metadata import version
from pathlib import Path
from typing import Any
import defusedxml.ElementTree as DET
import numpy as np
import tifffile
from numpy.typing import NDArray
logger = logging.getLogger(__name__)
def reshape_volume(
volume: NDArray[Any],
frames_per_data_group: int,
total_data_groups: int,
oct_window_height: int,
xy_scan_length: int,
) -> NDArray[Any]:
"""Reshape a 1-dimensional array to a 3-dimensional array.
Parameters
----------
volume : NDArray[Any]
A 1-dimensional array.
frames_per_data_group : int
The number of frames per data group.
total_data_groups : int
The total number of data groups.
oct_window_height : int
The OCT window height.
xy_scan_length : int
The XY scan length.
Returns
-------
volume : NDArray[Any]
A 3-dimensional array.
"""
volume = np.reshape(
volume,
(
frames_per_data_group * total_data_groups,
xy_scan_length,
oct_window_height,
),
)
return volume
def rotate_volume(
volume: NDArray[Any],
) -> NDArray[Any]:
"""Rotate a 3-dimensional array 90 degrees left (anti-clockwise) about the z-axis.
Parameters
----------
volume : NDArray[Any]
A 3-dimensional array.
Returns
-------
volume : NDArray[Any]
A rotated version of the input volume.
"""
volume = np.rot90(volume, k=1, axes=(1, 2))
return volume
def write_volume(
output_path: Path,
volume: NDArray[Any],
pixel_size_x: float,
pixel_size_y: float,
pixel_size_z: float,
) -> None:
"""Write a 3-dimensional array to the output path as an OME-TIFF file, including voxel size in the metadata.
Parameters
----------
output_path : Path
The specified output path.
volume : NDArray[Any]
A 3-dimensional array.
pixel_size_x : float
The pixel (voxel) width in mm.
pixel_size_y : float
The pixel (voxel) height in mm.
pixel_size_z : float
The pixel (voxel) depth in mm.
"""
tifffile.imwrite(
output_path,
volume,
photometric="minisblack",
metadata={
"axes": "ZYX",
"PhysicalSizeX": pixel_size_x,
"PhysicalSizeXUnit": "mm",
"PhysicalSizeY": pixel_size_y,
"PhysicalSizeYUnit": "mm",
"PhysicalSizeZ": pixel_size_z,
"PhysicalSizeZUnit": "mm",
},
)
def extract_boundaries(input_path: str | Path) -> None:
"""Extract segmentation lines.
Parameters
----------
input_path : str | Path
The specified input path.
"""
input_path = Path(input_path)
tree = DET.parse(input_path)
root = tree.getroot()
array_size = int(root.findtext("./Curve_Set/Image/Curve/ARRAY", 0))
data_points = [
int(point.text) if point.text else 0
for point in root.findall("./Curve_Set/Image/Curve/D")
]
scan_length = np.arange(len(data_points))
num_files = len(data_points) // array_size
for i in range(num_files):
start = i * array_size
end = start + array_size
table = np.column_stack([scan_length[start:end], data_points[start:end]])
table_path = f"{input_path.parent}/{input_path.stem}_{i+1}.txt"
np.savetxt(table_path, table, delimiter="\t", fmt="%d")
def main() -> None:
parser = argparse.ArgumentParser(
description="Convert optical coherence tomography angiography (OCTA) data."
)
parser.add_argument("input", type=Path, help="OCT file to convert")
parser.add_argument("--output", type=Path, help="specify a custom output directory")
parser.add_argument(
"--overwrite",
default=False,
action="store_true",
help="overwrite output file if it exists",
)
parser.add_argument("--size", type=float, help="scan size in mm")
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--angio",
default=False,
action="store_true",
help="convert extracted OCTA data",
)
group.add_argument(
"--en-face",
default=False,
action="store_true",
help="convert extracted en face image",
)
group.add_argument(
"--seg-curve",
default=False,
action="store_true",
help="convert extracted segmentation data",
)
group.add_argument(
"--boundaries",
default=False,
action="store_true",
help="extract segmentation lines",
)
parser.add_argument(
"--log-level",
default="WARNING",
metavar="LEVEL",
help="sets the logging level (default: %(default)s)",
)
parser.add_argument(
"--version", action="version", version="%(prog)s " + version("oct_to_tiff")
)
args = parser.parse_args()
numeric_level = getattr(logging, args.log_level.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError(f"Invalid log level: {args.log_level}")
logging.basicConfig(
level=numeric_level,
format="%(asctime)s %(name)s:%(funcName)s %(levelname)s - %(message)s",
)
input_path = args.input
if args.output:
dir_name = args.output
dir_name.mkdir(parents=True, exist_ok=True)
else:
dir_name = input_path.parent
file_name = input_path.stem
file_extension = ".ome.tif"
output_path = dir_name / (file_name + file_extension)
if Path.is_file(output_path):
if args.overwrite:
pass
else:
logger.error(f"{output_path} already exists.")
return
if args.boundaries:
extract_boundaries(input_path)
return
with open(input_path, "rb") as f:
if args.angio and args.size:
volume = np.frombuffer(f.read(), dtype=np.uint16)
oct_window_height = 160
frames_per_data_group = int((len(volume) // oct_window_height) ** 0.5)
total_data_groups = 1
xy_scan_length = int((len(volume) // oct_window_height) ** 0.5)
pixel_size_x = args.size / xy_scan_length
pixel_size_y = 0.012283
pixel_size_z = args.size / frames_per_data_group
elif args.en_face and args.size:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 1
total_data_groups = 1
oct_window_height = int(len(volume) ** 0.5)
xy_scan_length = int(len(volume) ** 0.5)
pixel_size_x = args.size / oct_window_height
pixel_size_y = args.size / xy_scan_length
pixel_size_z = 1
elif args.seg_curve:
volume = np.frombuffer(f.read(), dtype=np.single)
if len(volume) == 1280000 or len(volume) == 1120000:
frames_per_data_group = 400
oct_window_height = 400
elif len(volume) == 739328 or len(volume) == 646912:
frames_per_data_group = 304
oct_window_height = 304
total_data_groups = 1
xy_scan_length = len(volume) // (frames_per_data_group * oct_window_height)
pixel_size_x = 1
pixel_size_y = 1
pixel_size_z = 1
elif "3D Cornea" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 106
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 513
pixel_size_x = 0.007797
pixel_size_y = 0.003071
pixel_size_z = 0.040000
elif "3D Disc" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 106
total_data_groups = 1
oct_window_height = 768
xy_scan_length = 513
pixel_size_x = 0.011696
pixel_size_y = 0.003071
pixel_size_z = 0.060000
elif "3D Retina" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 144
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 385
pixel_size_x = 0.018182
pixel_size_y = 0.003071
pixel_size_z = 0.050000
elif "3D Widefield MCT" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 320
total_data_groups = 1
oct_window_height = 768
xy_scan_length = 320
pixel_size_x = 0.003075
pixel_size_y = 0.003071
pixel_size_z = 0.028125
elif "3D Widefield" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 323
total_data_groups = 1
oct_window_height = 768
xy_scan_length = 320
pixel_size_x = 0.003075
pixel_size_y = 0.003071
pixel_size_z = 0.028125
elif "Angle" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 1
total_data_groups = 2
oct_window_height = 768
xy_scan_length = 1020
pixel_size_x = 0.002941
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Cornea Cross Line" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 2
total_data_groups = 2
oct_window_height = 640
xy_scan_length = 941
pixel_size_x = 0.008502
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Cornea Line" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 1
total_data_groups = 2
oct_window_height = 640
xy_scan_length = 1020
pixel_size_x = 0.007843
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Cross Line" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 2
total_data_groups = 2
oct_window_height = 768
xy_scan_length = 1020
pixel_size_x = 0.009804
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Enhanced HD Line" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 1
total_data_groups = 2
oct_window_height = 960
xy_scan_length = 998
pixel_size_x = 0.012024
pixel_size_y = 0.003071
pixel_size_z = 1
elif "GCC" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 16
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 933
pixel_size_x = 0.007503
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Grid" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 10
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 1020
pixel_size_x = 0.005882
pixel_size_y = 0.003071
pixel_size_z = 1
elif "HD Angio Disc" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 400
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 400
pixel_size_x = 0.011250
pixel_size_y = 0.003071
pixel_size_z = 0.011250
if args.size:
pixel_size_x = args.size / xy_scan_length
pixel_size_z = args.size / frames_per_data_group
elif "Angio Disc" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 304
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 304
pixel_size_x = 0.009868
pixel_size_y = 0.003071
pixel_size_z = 0.009868
if args.size:
pixel_size_x = args.size / xy_scan_length
pixel_size_z = args.size / frames_per_data_group
elif "HD Angio Retina" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 400
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 400
pixel_size_x = 0.015000
pixel_size_y = 0.003071
pixel_size_z = 0.015000
elif "Angio Retina" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 304
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 304
pixel_size_x = 0.019737
pixel_size_y = 0.003071
pixel_size_z = 0.019737
if args.size:
pixel_size_x = args.size / xy_scan_length
pixel_size_z = args.size / frames_per_data_group
elif "Radial Lines" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 18
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 1024
pixel_size_x = 0.009766
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Line" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 1
total_data_groups = 2
oct_window_height = 960
xy_scan_length = 1020
pixel_size_x = 0.008824
pixel_size_y = 0.003071
pixel_size_z = 1
elif "ONH" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single, count=2223360)
frames_per_data_group = 3
total_data_groups = 1
oct_window_height = 768
xy_scan_length = 965
pixel_size_x = 0.015952
pixel_size_y = 0.003071
pixel_size_z = 1
elif "PachymetryWide" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 16
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 1536
pixel_size_x = 0.005859
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Raster" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single)
frames_per_data_group = 21
total_data_groups = 1
oct_window_height = 768
xy_scan_length = 1020
pixel_size_x = 0.011765
pixel_size_y = 0.003071
pixel_size_z = 1
elif "Retina Map" in file_name:
volume = np.frombuffer(f.read(), dtype=np.single, count=6680960)
frames_per_data_group = 13
total_data_groups = 1
oct_window_height = 640
xy_scan_length = 803
pixel_size_x = 0.007472
pixel_size_y = 0.003071
pixel_size_z = 1
volume = reshape_volume(
volume,
frames_per_data_group,
total_data_groups,
oct_window_height,
xy_scan_length,
)
if not args.en_face and not args.seg_curve:
volume = rotate_volume(volume)
write_volume(output_path, volume, pixel_size_x, pixel_size_y, pixel_size_z)
if __name__ == "__main__":
main()