-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdopplertext.py
295 lines (232 loc) · 8.55 KB
/
dopplertext.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
# -*- coding: utf-8 -*-
"""
dopplertext is a program to convert Doppler parameters stored on DCM images of PW Doppler into a readable, useable format.
Copyright (c) 2018 Gordon Stevenson.
This file is part of dopplertext.
dopplertext is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dopplertext is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with dopplertext. If not, see <http://www.gnu.org/licenses/>.
dopplertext Created on Thu July 12 14:41:18 2018
@author: gordon
"""
import skimage.io
import skimage.color
import skimage.util
from skimage.feature import match_template
import pandas as pd
import imghdr
try:
import cPickle as pickle
except ImportError:
import pickle
try:
import pydicom as dicom
except:
import dicom
import numpy as np
import os, glob, sys
from gooey import Gooey, GooeyParser
@Gooey(default_size=(800,600))
def main():
parser = GooeyParser(description="GE DICOM Text Parser")
parser.add_argument('outputfile', help='Select Output Spreadsheet Filename',widget="FileChooser")
area_threshold_group = parser.add_mutually_exclusive_group(required=True)
area_threshold_group.add_argument('--inputfile',
help='Input DCM File', widget='FileChooser')
area_threshold_group.add_argument('--inputdir',
help='Input DCM Directory', widget='DirChooser')
args = parser.parse_args()
runFile(args)
def loadImgDict():
if not os.path.isfile('image_dictionary.pickle'):
raise NotImplementedError
try:
with open('image_dictionary.pickle', 'rb') as handle:
image_dictionary = pickle.load(handle)
except UnicodeDecodeError as e:
with open('image_dictionary.pickle', 'rb') as f:
image_dictionary = pickle.load(f, encoding='latin1')
return image_dictionary
def runFile(args):
"""
:param args: dictionary
:return:
"""
inputfile = args.inputfile
inputdir = args.inputdir
image_dictionary = loadImgDict()
output_df = []
final_df = pd.DataFrame()
if inputfile is not None:
print('Analysing Single File....')
inputfile = inputfile.replace('\\', '/')
output_df, y_order = getTextFromDCMFile(inputfile, image_dictionary)
output_df = pd.DataFrame(output_df)
output_df['FileName'] = [inputfile[inputfile.rfind('/') + 1:] for i in range(len(output_df))]
output_df = output_df.iloc[np.argsort(y_order)].reset_index(drop=True)
write_df = pd.DataFrame(output_df)
print('done!')
if inputdir is not None:
file_list = glob.glob(args.inputdir + '/*')
print('Analysing Each DCM File....')
final_df = pd.DataFrame([])
for i,f in enumerate(file_list):
f= f.replace('\\', '/')
if isDICOM(f):
print('Processing....{} of {}'.format(i+1, len(file_list)))
output_df, y_order = getTextFromDCMFile(f, image_dictionary)
output_df = pd.DataFrame(output_df)
output_df['FileName'] = [f[f.rfind('/') + 1:] for i in range(len(output_df))]
output_df = output_df.iloc[np.argsort(y_order)].reset_index(drop=True)
final_df = final_df.append(output_df)
print('...done!')
final_df = final_df.reindex()
write_df = final_df
save_df(write_df, args.outputfile)
def save_df(write_df, outputfile):
"""
save out a dataframe write_df to a file with path outputfile depending on if excel or csv based on suffix
:param write_df dataframe
:param outputfile path to the dataframe to write to
"""
print('Saving File....')
if outputfile.split('.')[-1] == 'csv':
write_df.to_csv(outputfile)
elif outputfile.split('.')[-1] == 'xls' or outputfile.split('.')[-1] == 'xlsx':
writer = pd.ExcelWriter(outputfile)
write_df.to_excel(writer,'Result')
writer.save()
else:
print('csv or excel needs to be selected for file {}!'.format(outputfile))
raise NotImplementedError
write_df.to_clipboard()
print('...done!')
def isDICOM(file_name):
#return a bool depending on if this is a DICOM file
with open(file_name, 'rb') as f:
hdr = f.read()[128:132]
return (hdr == b'DICM')
def isJPEG(file_name):
#return a bool depending on if this is a jpeg file
if imghdr.what(file_name) == 'jpeg' or imghdr.what(file_name) == 'jpg':
return True
return False
def getTextFromDCMFile(file_name, image_dictionary):
"""
returns a List for Dataframe and the ordering in row order of which statistics are
provided
using x-corr against an image_dictionary, text is extracted from the particular image and then reordered and
returned as a DataFrame which can then saved as a spreadsheet
:param file_name
:param image_dictionary - a dict indexed by the characters within the np.array that contains the particular text.
:return output_df
:return y_list
"""
file_name = file_name.replace('\\', '/')
output_df = []
if isDICOM(file_name):
ds = dicom.read_file(file_name)
image = skimage.color.rgb2gray(ds.pixel_array)
else:
raise NotImplementedError
image = image[50:300, 700:]
positions = {}
image = skimage.util.pad(image, [3, 3], 'constant', constant_values=[0])
for i, d in image_dictionary.items():
result = match_template(image, d)
ij = np.unravel_index(np.argmax(result), result.shape)
x, y = ij[::-1]
positions[i] = np.argwhere(result > 0.95)
for p in positions[i]:
p[0], p[1] = p[0] - 3, p[1] - 3
y_pos = set()
for i, d in positions.items():
for ele in d:
y_pos.add(ele[0])
output_df = []
y_list = list(y_pos)
for y in y_pos:
txt = list()
x_pos = list()
for i, d in positions.items():
for ele in d:
if ele[0] == y:
txt.append(i)
x_pos.append(ele[1])
for i, d in positions.items():
for ele in d:
if ele[0] == y:
if isNumber(i):
if distanceFromNum(i, txt, x_pos):
txt.remove(i)
x_pos.remove(ele[1])
out_txt = np.array(txt)[np.argsort(x_pos)]
output_df.append(getText(out_txt).split(' '))
return output_df, y_list
def getText(out_txt):
"""
given a list of strings found in a doppler image line, return a complete string with spacing
formed correctly for gaps between numbers, words, hyphens and periods.
:param out_txt: list of strings
:return final_o: ordered list of strings
"""
final_o = ''
for o in out_txt:
if o.isdigit():
final_o += o
elif o == '.':
final_o += o
elif len(o) > 0 and len(final_o) > 1:
if final_o[-1][0].isdigit():
final_o += ' ' + o
else:
final_o += o + ' '
else:
final_o = final_o + o + ' '
return final_o
def isNumber(i):
"""
:param i: string
:return: boolean whether string is a digit between 0 and 10.
"""
if i in [str(n) for n in np.arange(0,10)]:
return True
else:
return False
def distanceFromNum(i,txt,x_pos):
"""
:param i:
:param txt:
:param x_pos:
:return:
"""
if not isNumber(i):
return False
num_pos = x_pos[txt.index(i)]
dist = 100
for x in x_pos:
if x != num_pos:
dist = (np.min([dist,np.abs(num_pos-x)]))
if dist > 12:
return True
return False
if __name__ == '__main__':
main()
"""
dummy test to run without gooey frontend - pases the argsobj dict.
class argsobj(object):
def __init__(self):
self.inputfile = None
#self.inputfile = "C:\\Users\\gordo\\Google Drive\\GitHub\\DopplerText\\data\\IMG_20180516_1_3.dcm"
#self.inputdir = None
self.inputdir = "C:\\Users\\gordo\\Google Drive\\GitHub\\DopplerText\\data"
self.outputfile = "test1.csv"
args = argsobj()
runFile(args)"""