-
Notifications
You must be signed in to change notification settings - Fork 3
/
hvf_bulk_processing.py
255 lines (171 loc) · 8.66 KB
/
hvf_bulk_processing.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
###############################################################################
# hvf_bulk_processing.py
#
# Description:
# Given a directory of HVF files, outputs them in a CSV file format. See
# code for ordering/column info
#
# Usage:
# python hvf_bulk_processing -i <directory_of_images>
# Outputs a spreadsheet TSV file "output_spreadsheet.tsv" from images
#
# python hvf_bulk_processing -t <directory_of_text_files>
# Outputs a spreadsheet TSV file "output_spreadsheet.tsv" from JSON
# text files
#
# python hvf_bulk_processing -s <directory_of_images>
# Outputs a directory of JSON text files into directory
# "serialized_hvf"; makes directory if does not exist
#
# python hvf_bulk_processing -d <directory_of_dicom_files>
# Outputs a directory of JSON text files into directory
# "serialized_hvf"; makes directory if does not exist
#
# python hvf_bulk_processing -f <path_to_tsv_file>
# Outputs a directory of JSON text files into directory
# "serialized_hvf"; makes directory if does not exist
#
###############################################################################
# Import necessary packages
import cv2;
import sys;
import argparse;
import difflib;
from shutil import copyfile
import os
import numpy as np
#import dlib
# Import the HVF_Object class
from hvf_extraction_script.hvf_data.hvf_object import Hvf_Object;
# Import logger class to handle any messages:
from hvf_extraction_script.utilities.logger import Logger;
# Import file utilities
from hvf_extraction_script.utilities.file_utils import File_Utils;
# Import tester class:
from hvf_extraction_script.hvf_manager.hvf_export import Hvf_Export;
# Construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image_directory", required=False,
help="path to directory of images to convert to spreadsheet")
ap.add_argument("-t", "--text_directory", required=False,
help="path to directory of text files to convert to spreadsheet")
ap.add_argument("-s", "--save_images", required=False,
help="path to directory of image files to read and save as text documents")
ap.add_argument("-f", "--import_file", required=False,
help="path to TSV file to import and save as text documents")
ap.add_argument("-d", "--dicom_file", required=False,
help="path to directory of DICOM files to convert to text documents")
args = vars(ap.parse_args())
###############################################################################
# HELPER METHODS ##############################################################
###############################################################################
###############################################################################
# From a directory of images, returns a dictionary of HVF objects:
def get_dict_of_hvf_objs_from_imgs(directory):
# Read in images from directory:
list_of_image_file_extensions = [".bmp", ".jpg", ".jpeg", ".png"];
list_of_img_paths = File_Utils.get_files_within_dir(directory, list_of_image_file_extensions);
dict_of_hvf_objs = {};
for hvf_img_path in list_of_img_paths:
path, filename = os.path.split(hvf_img_path)
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF image " + filename);
hvf_img = File_Utils.read_image_from_file(hvf_img_path);
hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_img);
hvf_obj.release_saved_image();
dict_of_hvf_objs[filename] = hvf_obj;
return dict_of_hvf_objs;
###############################################################################
# From a directory of text files, returns a dictionary of HVF objects:
def get_dict_of_hvf_objs_from_text(directory):
# Read in text files from directory:
list_of_file_extensions = [".txt"];
list_of_txt_paths = File_Utils.get_files_within_dir(directory, list_of_file_extensions);
dict_of_hvf_objs = {};
for hvf_txt_path in list_of_txt_paths:
path, filename = os.path.split(hvf_txt_path)
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF text file " + filename);
hvf_txt = File_Utils.read_text_from_file(hvf_txt_path);
hvf_obj = Hvf_Object.get_hvf_object_from_text(hvf_txt);
dict_of_hvf_objs[filename] = hvf_obj
return dict_of_hvf_objs;
###############################################################################
# BULK PROCESSING #############################################################
###############################################################################
Logger.set_logger_level(Logger.DEBUG_FLAG_SYSTEM);
# If flag, then do unit tests:
if (args["image_directory"]):
# Grab the argument directory for readability
directory = args["image_directory"];
dict_of_hvf_objs = get_dict_of_hvf_objs_from_imgs(directory);
return_string = Hvf_Export.export_hvf_list_to_spreadsheet(dict_of_hvf_objs)
File_Utils.write_string_to_file(return_string, "output_spreadsheet.tsv")
elif (args["text_directory"]):
# Grab the argument directory for readability
directory = args["text_directory"];
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "========== START READING ALL TEXT FILES ==========");
dict_of_hvf_objs = get_dict_of_hvf_objs_from_text(directory);
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "========== FINISHED READING ALL TEXT FILES ==========");
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "========== START EXPORT ==========");
return_string = Hvf_Export.export_hvf_list_to_spreadsheet(dict_of_hvf_objs)
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "========== WRITING EXPORT SPREADSHEET ==========");
File_Utils.write_string_to_file(return_string, "output_spreadsheet.tsv")
elif (args["save_images"]):
directory = args["save_images"];
save_dir = "serialized_hvfs"
if not os.path.isdir(save_dir):
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Making new save directory: " + save_dir);
os.mkdir(save_dir);
list_of_image_file_extensions = [".bmp", ".jpg", ".jpeg", ".png"];
list_of_img_paths = File_Utils.get_files_within_dir(directory, list_of_image_file_extensions);
for hvf_img_path in list_of_img_paths:
path, filename = os.path.split(hvf_img_path)
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF image " + filename);
hvf_img = File_Utils.read_image_from_file(hvf_img_path);
try:
hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_img);
file_path = os.path.join(save_dir, str(filename)+".txt");
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Writing text serialization file " + filename);
File_Utils.write_string_to_file(hvf_obj.serialize_to_json(), file_path)
except:
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "============= FAILURE on serializing " + filename);
elif (args["import_file"]):
path_to_tsv_file = args["import_file"];
save_dir = "serialized_hvfs"
if not os.path.isdir(save_dir):
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Making new save directory: " + save_dir);
os.mkdir(save_dir);
tsv_file_string = File_Utils.read_text_from_file(path_to_tsv_file);
dict_of_hvf_objs = Hvf_Export.import_hvf_list_from_spreadsheet(tsv_file_string);
for filename in dict_of_hvf_objs.keys():
hvf_obj = dict_of_hvf_objs.get(filename)
hvf_serialized = hvf_obj.serialize_to_json()
try:
filename_root, ext = os.path.splitext(filename);
if not (ext == "txt"):
filename = filename + ".txt";
file_path = os.path.join(save_dir, str(filename));
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Writing text serialization file " + filename);
File_Utils.write_string_to_file(hvf_serialized, file_path)
except:
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "============= FAILURE on serializing " + filename);
elif (args["dicom_file"]):
directory = args["dicom_file"];
save_dir = "serialized_hvfs"
if not os.path.isdir(save_dir):
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Making new save directory: " + save_dir);
os.mkdir(save_dir);
list_of_file_extensions = [".dcm"];
list_of_paths = File_Utils.get_files_within_dir(directory, list_of_file_extensions);
for hvf_dcm_path in list_of_paths:
path, filename = os.path.split(hvf_dcm_path)
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF DICOM " + filename);
hvf_dicom_ds = File_Utils.read_dicom_from_file(hvf_dcm_path);
try:
hvf_obj = Hvf_Object.get_hvf_object_from_dicom(hvf_dicom_ds);
file_path = os.path.join(save_dir, str(filename)+".txt");
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Writing text serialization file " + filename);
File_Utils.write_string_to_file(hvf_obj.serialize_to_json(), file_path)
except:
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "============= FAILURE on serializing " + filename);
else:
Logger.get_logger().log_msg(Logger.DEBUG_FLAG_ERROR, "No input directory given");