-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinFiber.py
More file actions
272 lines (229 loc) · 8.72 KB
/
WinFiber.py
File metadata and controls
272 lines (229 loc) · 8.72 KB
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
import os
import psutil
import subprocess
import time
import pyautogui
from pathlib import Path
import sys
import logging
pyautogui.PAUSE = 1
pyautogui.FAILSAFE = True
def ask_choice(question, options):
while True:
raw_input = None
print('\n' + question)
for n, option in enumerate(options):
print('\t{}: {}'.format(n+1, option[0]))
try:
raw_input = input('Your choice: ')
value = int(raw_input)
if len(options) >= value > 0:
break
else:
print('Please try again - {} is not a valid choice. '.format(raw_input))
except ValueError:
print('Please try again - {} is not a number. '.format(raw_input))
return options[value-1][1]
def checkIfProcessRunning(processName):
for proc in psutil.process_iter():
try:
if processName.lower() in proc.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
def isresponding(name):
os.system('tasklist /FI "IMAGENAME eq %s" /FI "STATUS eq running" > tmp.txt' % name)
tmp = open('tmp.txt', 'r')
a = tmp.readlines()
tmp.close()
if a[-1].split()[0] == name:
return True
else:
return False
def WinFiberOpen(input_name):
pyautogui.hotkey('ctrl', 'o')
pyautogui.typewrite(input_name)
pyautogui.press('enter')
def WinFiberZoom():
pyautogui.moveTo(800, 150)
pyautogui.drag(0, 5, 1, button='right')
def WinFiberZoom2():
pyautogui.moveTo(800, 150)
pyautogui.drag(0, 750, 3, button='right')
def WinFiberScreenshot(output_name):
img = pyautogui.screenshot()
try:
img.save(output_name)
except (IOError, PermissionError):
time.sleep(120)
pass
def WinFiberMoveZ():
pyautogui.moveTo(450, 815)
pyautogui.dragTo(500, 675, 3, button="left")
def WinFiberSetup():
pyautogui.click(1694, 720) # untick shadows
pyautogui.click(1780, 639) # color vessels
pyautogui.click(1828, 690) # color vessels
def WinFiberExport(out):
pyautogui.hotkey('ctrl', 's')
pyautogui.typewrite(out)
pyautogui.press('tab')
pyautogui.press('tab')
pyautogui.press('space')
pyautogui.press('tab')
pyautogui.press('space')
pyautogui.press('tab')
pyautogui.press('space')
pyautogui.press('enter')
def WinFiberRemoveLoops(output_name):
pyautogui.click(80, 76)
pyautogui.click(93, 107)
time.sleep(30)
img = pyautogui.screenshot()
try:
img.save(output_name)
except(IOError, PermissionError):
time.sleep(120)
pass
time.sleep(5)
pyautogui.press('enter')
def checkDir(dir):
try:
if not os.path.isdir(dir):
os.mkdir(dir)
except PermissionError:
time.sleep(120)
pass
def ask_dir(dir, str):
if not os.path.isdir(dir):
while True:
makedir = ask_choice('This path does not exist. Would you like to create it?', [
['yes', True],
['no', False],
])
if makedir:
os.makedirs(dir)
break
else:
dir = input(str)
def open_program(path_name):
return subprocess.Popen(path_name)
def close_program(p):
p.terminate()
try:
winfiber_path = Path(sys.argv[1])
except IndexError:
winfiber_path = input('Please provide the path to the WinFiber3D executable. ')
winfiber_path = winfiber_path.replace('"', '')
winfiber_path = Path(winfiber_path)
if not winfiber_path or not os.access(str(winfiber_path), os.X_OK):
winfiber_path = input('Please provide the path to the WinFiber3D executable. ')
winfiber_path = winfiber_path.replace('"', '')
winfiber_path = Path(winfiber_path)
try:
input_path = Path(sys.argv[2])
except IndexError:
input_path = input('Please provide the path to the mv3d files. ')
input_path = input_path.replace('"', '')
input_path = Path(input_path)
if not os.path.isdir(input_path):
while True:
input_path = input(
"This dir doesn't seem to exist. Please check your input. ")
input_path = input_path.replace('"', '')
input_path = Path(input_path)
try:
output_path = Path(sys.argv[3])
except IndexError:
output_path = input('Please provide the path where the exported files will be saved. ')
output_path = output_path.replace('"', '')
output_path = Path(output_path)
ask_dir(output_path, 'Please provide the path where the exported files will be saved. ')
log_file = os.path.join(output_path, "Log.txt")
logging.basicConfig(filename=log_file, filemode='a', level=logging.DEBUG,
format='%(asctime)s | %(levelname)s >> %(message)s', datefmt='%Y/%m/%d %H:%M:%S')
logging.info('Start binarization script')
logging.info('Preferences')
logging.info(' WinFiber3D dir: %s', winfiber_path)
logging.info(' Input dir: %s', input_path)
logging.info(' Output dir: %s', output_path)
problem_list = []
with open(log_file, 'r') as f:
temp = [line.strip() for line in f.readlines()]
for n in temp:
if 'File not processed' in n:
problem_list.append(n.split(": ")[1])
time.sleep(5)
print("Starting...")
p = open_program(str(winfiber_path))
time.sleep(10)
pyautogui.hotkey('win', 'up')
x = 0
for root, dirs, files in os.walk(input_path):
for file in files:
if file.lower().endswith(".mv3d"):
input_file = os.path.join(root, file)
try:
base_name = file.split("_skeletonize")[0]
except IndexError:
base_name = input_file
try:
len_no = int(root.split("len_")[1])
except IndexError:
len_no = None
export_dir_out = output_path
screenshot_dir_out = os.path.join(export_dir_out, "screenshots")
checkDir(screenshot_dir_out)
loopremoval_dir_out = os.path.join(screenshot_dir_out, "loop-removal")
checkDir(loopremoval_dir_out)
if len_no:
screenshot_file_xy = base_name + "-len_" + str(len_no) + "_XY.png"
screenshot_file_z = base_name + "-len_" + str(len_no) + "_Z.png"
screenshot_file_loop = base_name + "-len_" + str(len_no) + "_looprem.png"
export_file_wloops = base_name + "-len_" + str(len_no) + ".xls"
export_file_loops_removed = base_name + "-len_" + str(len_no) + "_loops_removed.xls"
else:
screenshot_file_xy = base_name + "_XY.png"
screenshot_file_z = base_name + "_Z.png"
screenshot_file_loop = base_name + "_looprem.png"
export_file_wloops = base_name + ".xls"
export_file_loops_removed = base_name + "_loops_removed.xls"
if not os.path.exists(os.path.join(export_dir_out, export_file_loops_removed)):
if input_file not in problem_list:
try:
WinFiberOpen(input_file)
time.sleep(30)
x += 1
if isresponding("WinFiber.exe"):
if x == 1:
WinFiberSetup()
else:
time.sleep(1)
if len_no == 2:
WinFiberZoom2()
else:
WinFiberZoom()
WinFiberScreenshot(os.path.join(screenshot_dir_out, screenshot_file_xy)) # XY screenshot
WinFiberMoveZ()
WinFiberScreenshot(os.path.join(screenshot_dir_out, screenshot_file_z)) # z screenshot
WinFiberExport(os.path.join(export_dir_out, export_file_wloops))
WinFiberRemoveLoops(os.path.join(loopremoval_dir_out, screenshot_file_loop))
WinFiberExport(os.path.join(export_dir_out, export_file_loops_removed))
else:
time.sleep(10)
logging.info(' File not processed: %s', input_file)
problem_list.append(input_file)
time.sleep(3)
p.kill()
time.sleep(20)
p = open_program(str(winfiber_path))
time.sleep(15)
pyautogui.hotkey('win', 'up')
x = 0
continue
except:
time.sleep(120)
pass
time.sleep(60)
close_program(p)