-
Notifications
You must be signed in to change notification settings - Fork 24
/
ImportOCT.py
171 lines (138 loc) · 5.74 KB
/
ImportOCT.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
import os
import unittest
import logging
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
#
# ImportOCT
#
class ImportOCT(ScriptedLoadableModule):
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = "ImportOCT"
self.parent.categories = ["Informatics"]
self.parent.dependencies = []
self.parent.contributors = ["Andras Lasso (PerkLab)"]
self.parent.helpText = """Load OCT image volumes"""
self.parent.acknowledgementText = """This file was originally developed by Andras Lasso, PerkLab."""
# don't show this module - it is only for registering a reader
parent.hidden = True
#
# Reader plugin
# (identified by its special name <moduleName>FileReader)
#
class ImportOCTFileReader(object):
def __init__(self, parent):
self.parent = parent
def description(self):
return 'OCT image'
def fileType(self):
return 'OCTImageFile'
def extensions(self):
# TODO: we could expose all file formats that oct-converter Python package supports,
# but we would need sample data sets to test with
return ['Topcon OCT image file (*.fda)']
def checkRequiredPythonPackages(self):
try:
import oct_converter
# Successfully imported
return True
except ModuleNotFoundError as e:
if slicer.util.confirmOkCancelDisplay("Importing of OCT files require 'oct-converter' Python package. Click OK to install it now (it may take several minutes)."):
# Install converter
slicer.util.pip_install("oct-converter")
else:
# User chose not to install converter
return False
# Failed once, but may have been installed successfully since then, test it now
try:
import oct_converter
except ModuleNotFoundError as e:
slicer.util.errorDisplay("Required 'oct-converter' Python package has not been installed. Cannot import OCT image.")
return False
return True
def canLoadFile(self, filePath):
if not self.checkRequiredPythonPackages():
return False
try:
from oct_converter.readers import FDA
fda = FDA(filePath)
if fda is None:
return False
except Exception as e:
return False
return True
def load(self, properties):
if not self.checkRequiredPythonPackages():
return False
try:
filePath = properties['fileName']
# TODO: we could expose all file formats that oct-converter Python package supports,
# but we would need sample data sets to test with
from oct_converter.readers import FDA
fda = FDA(filePath)
octVolume = fda.read_oct_volume_2()
import numpy as np
octVolumeArray = np.zeros([octVolume.volume[0].shape[0], octVolume.volume[0].shape[1], len(octVolume.volume)])
for sliceIndex in range(len(octVolume.volume)):
octVolumeArray[:, :, sliceIndex] = octVolume.volume[sliceIndex]
# Get node base name from filename
if 'name' in properties.keys():
baseName = properties['name']
else:
baseName = os.path.splitext(os.path.basename(filePath))[0]
baseName = slicer.mrmlScene.GenerateUniqueName(baseName)
volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", baseName)
volumeNode.SetAttribute("oct.laterality", octVolume.laterality)
volumeNode.SetAttribute("oct.patient_id", octVolume.patient_id)
volumeNode.SetAttribute("oct.patient_dob", octVolume.DOB)
# Set up IJK to RAS so that the axis slice shows the usual orientation of the image and the matrix is right-handed.
# The converter does not provide voxel spacing information, so we just use 1.0 and log a warning message.
spacingNotSetMessage = "File importer for .fda OCT images does not set voxel size. Length measurement in physical unit will not be accurate."
try:
self.parent.userMessages().AddMessage(vtk.vtkCommand.WarningEvent, spacingNotSetMessage)
except:
# In Slicer-4.11 and earlier versions userMessages() method was not exposed.
logging.warning(f"Note for {filePath}: {spacingNotSetMessage}")
ijkToRasArray = np.array([
[ 0.0, 1.0, 0.0, 0.0],
[ 0.0, 0.0,-1.0, 0.0],
[-1.0, 0.0, 0.0, 0.0],
[ 0.0, 0.0, 0.0, 1.0]
])
volumeNode.SetIJKToRASMatrix(slicer.util.vtkMatrixFromArray(ijkToRasArray))
slicer.util.updateVolumeFromArray(volumeNode, octVolumeArray)
except Exception as e:
logging.error('Failed to load file: '+str(e))
import traceback
traceback.print_exc()
return False
# Show volume
selectionNode = slicer.app.applicationLogic().GetSelectionNode()
selectionNode.SetActiveVolumeID(volumeNode.GetID())
slicer.app.applicationLogic().PropagateVolumeSelection()
self.parent.loadedNodes = [volumeNode.GetID()]
return True
#
# ImportOCTTest
#
class ImportOCTTest(ScriptedLoadableModuleTest):
def setUp(self):
slicer.mrmlScene.Clear()
def runTest(self):
self.setUp()
self.test_ImportOCT1()
def test_ImportOCT1(self):
self.delayDisplay("Loading test image as segmentation")
import SampleData
testFdaFilePath = SampleData.downloadFromURL(
fileNames='OS_260397.fda',
uris='https://github.com/PerkLab/SlicerSandbox/releases/download/TestingData/OS_260397.fda',
checksums='SHA256:5536006a2bb4d117f5804e49d393ecc1cb7e98c3e5f7924b7b83b8f0e0567e2c')[0]
volumeNode = slicer.util.loadNodeFromFile(testFdaFilePath, 'OCTImageFile')
self.assertIsNotNone(volumeNode)
self.delayDisplay('Checking loaded image')
self.assertEqual(volumeNode.GetImageData().GetDimensions()[0], 9)
self.assertEqual(volumeNode.GetImageData().GetDimensions()[1], 320)
self.assertEqual(volumeNode.GetImageData().GetDimensions()[2], 992)
self.delayDisplay('Test passed')