-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenCrop.py
296 lines (255 loc) · 9.36 KB
/
screenCrop.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
296
'''
Created on Sep 5, 2017
Loads embryo images generated by CV1000 and crops/orients and saves separate embryos based on DIC (C3) central image
'''
date = '20180808T122436'
'''directly edit parameters'''
loadFolder = 'Z:/'
folderIn = loadFolder + 'CV1000/' + date # Input folder
trackingFile = 'Z:/Experiment_tracking_sheets/EMBD_fileNames_Tracking_Sheet.csv'
aspectRatioFile = 'Z:/cropped/aspects.csv'
z = 18 # number of z planes
nT = 31 # number of time points
corrDrift = True
removeBG = True
attCorrect = True
apRotate = True
nWells = 1 # number of wells (14)
pointVisits = 1 # number of point visits (4)
'''read parameters from config file parameters.py'''
import parameters
if parameters.use_config:
print("using parameters defined in the CONFIGURATION file (parameters.py)")
date = parameters.date
loadFolder = parameters.loadFolder
folderIn = parameters.folderIn
trackingFile = parameters.trackingFile
aspectRatioFile = parameters.aspectRatioFile
z = parameters.z
nT = parameters.nT
corrDrift = parameters.corrDrift
removeBG = parameters.removeBG
attCorrect = parameters.attCorrect
apRotate = parameters.apRotate
nWells = parameters.nWells
pointVisits = parameters.pointVisits
else:
print("using parameters defined in the SOURCE CODE")
import glob, csv, cv2, os, shutil
import numpy as np
from findEmbryo import showIm
from myFunc import clearFolder
from cropAPI import cropEmbs
from tkinter import messagebox as tkMessageBox
debug = False # use to debug the program (line 101)
def getConditions(date, fileName):
''' loads RNAi strains for a specified date from a csv file '''
global RNAi, strains
# csvFile = csv.reader(open(fileName, 'rb'), delimiter=',')
csvFile = csv.reader(open(fileName, 'rU'), delimiter=',') # universal
fileData = []
for row in csvFile:
fileData.append(row[1:-1])
myDate = [s for s in fileData if s[0] == date]
myDate = sorted(myDate, key=lambda well: well[2])
RNAi = [s[3] for s in myDate]
strains = [s[4] for s in myDate]
return
def loadImages(folder, well, j):
'''
loads images from a folder and splits them in separate point visits
Parameters:
folder : folder to read images from
Return:
allImgs: list of 4 different point visits with 3 channels in each. images are numpy arrays.
'''
imc1, imc2, imc3 = [], [], []
folderNames = glob.glob(folder + '/Well{0:0>3}/*F{1:0>3}*C1.tif'.format(well, j))
folderNames.sort()
for fileName in folderNames:
imc1.append(cv2.imread(fileName, -1))
folderNames = glob.glob(folder + '/Well{0:0>3}/*F{1:0>3}*C2.tif'.format(well, j))
folderNames.sort()
for fileName in folderNames:
imc2.append(cv2.imread(fileName, -1))
folderNames = glob.glob(folder + '/Well{0:0>3}/*F{1:0>3}*C3.tif'.format(well, j))
folderNames.sort()
for fileName in folderNames:
imc3.append(cv2.imread(fileName, -1))
if len(imc1) > 0:
imc1 = np.reshape(imc1, (nT, z, imc1[0].shape[-2], imc1[0].shape[-1]))
imc2 = np.reshape(imc2, (nT, z, imc2[0].shape[-2], imc2[0].shape[-1]))
imc3 = np.reshape(imc3, (nT, z, imc3[0].shape[-2], imc3[0].shape[-1]))
allImgs = np.stack((imc1, imc2, imc3), axis=2)
return allImgs
else:
return np.array((imc1, imc2, imc3))
def getAllEmb(folder):
'''
Finds and saves all embryos
ParametersL
folder: folder to load embryos
Return:
None
'''
global RNAi, strains
print('STARTED!!!')
getConditions(date, trackingFile)
totalEmb = 0
embs = []
for well in range(nWells):
for j in range(pointVisits):
if debug:
well, j = 2, 1 # well, field
imgs = loadImages(folder, well + 1, j + 1)
if imgs.size > 0:
print('loaded well {0} point {1}'.format(well + 1, j + 1))
if strains[well] == 'MS':
featureList = [201, 201, None]
else:
featureList = [41, 41, None]
es, rs = cropEmbs(imgs, 2, corrDrift, attCorrect, 0.1, removeBG, featureList, 0.315, EmbdScreen=True)
embs.append([well, j, es, rs])
totalEmb += len(embs[-1]) - 2
else:
print('no images well {0} point {1}'.format(well + 1, j + 1))
del imgs
print('Done analyzing, ready to save!')
checks = np.array([checkEmbDebris(e[2 * 3 * z + 3 * z // 2 + 2]) for well, j, es, rs in embs for e in es])
totalEmb = checks.size
uniqeRNAi = np.array(list(set(RNAi)))
embN = np.ones([len(uniqeRNAi), 2])
xembN = np.ones([len(uniqeRNAi), 2])
if not debug:
for well, j, es, rs in embs:
if strains[well] != 'MS':
k = 0
else:
k = 1
for l in range(len(es)):
e = es[l]
r = rs[l]
print('{0} embryos left, saving...'.format(totalEmb))
i = checks.size - totalEmb
if checks[i] == 1:
saveEmb(e, j, int(embN[np.where(uniqeRNAi == RNAi[well])[0], k]), well, checks[i], r)
embN[np.where(uniqeRNAi == RNAi[well])[0], k] += 1
elif checks[i] == 2:
saveEmb(e, j, int(xembN[np.where(uniqeRNAi == RNAi[well])[0], k]), well, checks[i], r)
xembN[np.where(uniqeRNAi == RNAi[well])[0], k] += 1
totalEmb -= 1
def checkEmbDebris(im):
''' lets user debug supplied image, and returns 1 for yes (save) and 0 for not and 2 for special case'''
code = showIm(im)
if code == ord('d') or code == ord('D'):
result = tkMessageBox.askquestion("Delete", "Are You Sure?", icon='warning')
if result == 'yes':
return 0
else:
return checkEmbDebris(im)
elif code == ord('x') or code == ord('X'):
return 2
else:
return 1
def saveEmb(imgs, point, i, well, check, r):
'''
Saves embryo images according to a certain pattern
Parameters:
imgs: numpy array with images in czt order
f: point visit number
i: embryo number
r: aspect ratio
'''
imgs = np.reshape(imgs, (nT, z, 3, imgs.shape[-2], imgs.shape[-1]))
im1 = imgs[:, :, 0]
im2 = imgs[:, :, 1]
im3 = imgs[:, :, 2]
strain = strains[well]
ri = RNAi[well]
if ri != 'EMBD0000':
folderOut = loadFolder + 'cropped/{0}/{1}/'.format(ri, strain) # outputFolder
else:
folderOut = loadFolder + 'cropped/EMBD0000/{0}/{1}/'.format(strain, date) # outputFolder
if check == 2:
folderOut = folderOut + 'x'
else:
folderOut = folderOut
j = 1
folder = folderOut + 'Emb{0}/'.format(j)
while os.path.exists(folder):
fileName = glob.glob(folder + '*_T01_Z01_C1.tif')
if len(fileName) > 0:
fileName = fileName[0].split('/')[-1]
if fileName.split('_')[2] == date:
if i == 1:
break
else:
i -= 1
j += 1
folder = folderOut + 'Emb{0}/'.format(j)
else:
j += 1
folder = folderOut + 'Emb{0}/'.format(j)
fileName = '{0}_Emb{1}_{2}_W{3:0>2}F{4}_'.format(ri, j, date, well + 1, point + 1)
''' correct attenuation and save local '''
if not os.path.exists(folder):
os.makedirs(folder)
else:
print('file exist, clearing folder', folder)
clearFolder(folder)
if os.path.exists(folder + 'batch-output'):
shutil.rmtree(folder + 'batch-output')
saveImgs(im1, folder, fileName, 1)
saveImgs(im2, folder, fileName, 2)
saveImgs(im3, folder, fileName, 3)
''' populate aspect ratio file '''
print('saveEmb aspect j=', j)
addAspect(r, date, ri, j)
def saveImgs(imgs, folder, fileName, c):
imgs = np.reshape(imgs, (-1, imgs.shape[-2], imgs.shape[-1])).astype(np.uint16)
for i in range(imgs.shape[0]):
cv2.imwrite(folder + fileName + 'T{0:0>2}_Z{1:0>2}_C{2}.tif'.format(i // z + 1, i % z + 1, c), imgs[i])
def addAspect(r, date, ri, j):
'''
Adds aspect ratio of an embryo into the csv file
Parameters:
r: aspect ratio
date: date
ri: RNAi conditions
j: embryo number
'''
import operator
newData = []
oldData = loadAspects(aspectRatioFile)
i = 0
while i <= len(oldData):
if oldData[i][0] != ri:
newData.append(oldData[i])
elif oldData[i][1] != date:
newData.append(oldData[i])
elif int(oldData[i][2]) < j:
newData.append(oldData[i])
i += 1
newData.append([ri, date, '{0:0>3}'.format(j), str(r)])
newData = sorted(newData, key=operator.itemgetter(1, 2, 3))
saveAspects(aspectRatioFile, newData)
def loadAspects(fileName):
'''
reads aspect ratios from file. The aspect ratios are sorted by rnai condition, date, embryo number.
fileName: name of the file to read from
'''
fileData = []
try:
csvFile = csv.reader(open(fileName, 'rU'), delimiter=',') # universal
for row in csvFile:
fileData.append(row)
except:
pass
return fileData
def saveAspects(fileName, data):
with open(fileName, 'w') as f:
writer = csv.writer(f, delimiter=',')
writer.writerows(data)
if __name__ == '__main__':
getAllEmb(folderIn)
print('ALL DONE!!!!! :)')