-
Notifications
You must be signed in to change notification settings - Fork 1
/
core_app_methods.py
110 lines (94 loc) · 3.95 KB
/
core_app_methods.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
__author__ = 'ank'
from chr_sep_human import human_loop as p_loop
from chr_sep_human import human_afterloop as p_afterloop
import os, errno
from pickle import load, dump
from kivy.clock import Clock
from mock import MagicMock, Mock
def safe_mkdir(path):
try:
os.mkdir(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
def count_images(path):
namelist = []
for name in os.listdir(path):
if os.path.isfile(os.path.join(path, name)) and name.split('.')[-1] in ['jpeg', 'jpg', 'tif',' tiff']:
namelist.append(name)
return len(namelist)
def loop_dir(image_directory, widget):
progress_bar, text_field = widget.progress_bar, widget.text_field
progress_bar.value = 1
cim = count_images(image_directory)
if not cim:
t_to_add = 'Failed to find any .jpeg, .jpg, .tif or .tiff in the directory'
text_field.text = text_field.text+t_to_add+'\n'
progress_bar.value = 1000
widget.append_to_consommables(t_to_add)
return ''
increment = 1000/cim
afterloop_list = []
buffer_directory = os.path.join(image_directory,'buffer')
safe_mkdir(buffer_directory)
for fle in os.listdir(image_directory):
print fle
prefix, suffix = ('_'.join(fle.split('.')[:-1]), fle.split('.')[-1])
if suffix in ['jpeg', 'jpg', 'tif',' tiff']:
buffer_path = os.path.join(buffer_directory, prefix)
pre_time = p_loop(buffer_path, os.path.join(image_directory,fle), widget.stack_type)
t_to_add = "file %s pre-processed in %s seconds" %(fle, "{0:.2f}".format(pre_time))
afterloop_list.append((pre_time, prefix, buffer_path))
progress_bar.value = progress_bar.value + increment
widget.append_to_consommables(t_to_add)
dump((image_directory, afterloop_list), open('DO_NOT_TOUCH.dmp','wb'))
progress_bar.value = 1000
return ''
def loop_fle(image_directory, file, widget):
progress_bar, text_field = widget.progress_bar, widget.text_field
progress_bar.value = 300
widget.append_to_consommables('starting to process fle %s'%file)
afterloop_list = []
buffer_directory = os.path.join(image_directory,'buffer')
safe_mkdir(buffer_directory)
prefix, suffix = ('_'.join(file.split('.')[:-1]), file.split('.')[-1])
if suffix in ['jpeg', 'jpg', 'tif',' tiff']:
buffer_path = os.path.join(buffer_directory, prefix)
print buffer_path
safe_mkdir(buffer_path)
pre_time = p_loop(buffer_path, os.path.join(image_directory, file), widget.stack_type)
t_to_add = "file %s pre-processed in %s seconds" %(file, "{0:.2f}".format(pre_time))
afterloop_list.append((pre_time, prefix, buffer_path))
else:
t_to_add = 'file %s has a wrong extension'%file
widget.append_to_consommables(t_to_add)
progress_bar.value = 1000
dump((image_directory, afterloop_list), open('DO_NOT_TOUCH.dmp', 'wb'))
return ''
def afterloop(widget):
progress_bar, text_field = widget.progress_bar, widget.text_field
imdir, afterloop_list = load(open('DO_NOT_TOUCH.dmp','rb'))
output_directory = os.path.join(imdir, 'output')
safe_mkdir(output_directory)
for pre_time, fle_name, buffer_path in afterloop_list:
t_to_add = p_afterloop(output_directory, pre_time, fle_name, buffer_path)
text_field.text = text_field.text+t_to_add+'\n'
class progbar(object):
def __init__(self):
self.value = 0
class text_fields(object):
def __init__(self):
self.text = ''
class wdg(object):
def __init__(self, st_tp):
self.progress_bar = progbar()
self.text_field = text_fields()
self.stack_type = st_tp
if __name__ == "__main__":
fname = 'img_000000002__000.tif'
test_f = 'L:/Akn/mammalian/human chromosome spreads/10-7-14 rpe/rpe WT/rpe WT images 2'
st_tp = 0
loop_fle(test_f, fname, wdg(st_tp))
afterloop(wdg(st_tp))
pass