forked from mfenner1/py_coloc_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefinitionsAndUtils.py
353 lines (293 loc) · 10.9 KB
/
DefinitionsAndUtils.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#
# This file is pure python.
#
#
# Two utilities that should disappear by making a class
# for experiments that can be used as a dictionary key
# (do I want to be a be able to specify repetition and slice
# as well? ... ie specify an image with that key?)
#
# see expSetupKey below
#
def dictToKeyTuple(d):
return tuple(d.items())
# modified APR 20
# return tuple( (k,d[k]) for k in d )
def keyTupleToDict(t):
return dict(t)
##################################################
##################################################
# Color names for RGB Image in PIL
##################################################
##################################################
R, G, B = 0, 1, 2
colors = (R,G,B)
colorNames = ("R", "G", "B")
colorToName = {R:"R", G:"G", B:"B"}
colorPairs = [(R,G), (R,B), (G,B)]
colorPairNames = ["RG", "RB", "GB"]
colorToExp = {R:"BDNF", G:"TrkB.tc", B:"Organelle"}
numImagePoints = 1024 * 1024
numIntensityValues = 256
numIntensityPairs = numIntensityValues ** 2
numIntensityTriples = numIntensityValues ** 3
##################################################
##################################################
# Getting to files from identifying information
##################################################
##################################################
imageDataPath = "/home/mfenner/scipy_prep/data/input/All_stacks_60x/"
fileNameFormat = {}
fileNameFormat["Endosomes"] = \
"%(Organelle)s/%(Time)dminend/ser%(Series)d/" + \
"%(Time)dm60xendser%(Series)d%(Slice)s.TIF"
fileNameFormat["Vesicles"] = \
"%(Organelle)s/%(Stain)s/%(Time)dmin/ser%(Series)d/" + \
"%(Time)dm60x%(ReducedStain)sser%(Series)d%(Slice)s.TIF"
fileNameFormat["Lysosomes"] = \
"%(Organelle)s/%(Time)dmin/ser%(Series)d/" + \
"%(Time)dm60xac17ser%(Series)d%(Slice)s.TIF"
# this is here b/c it works with the filename formats
# convert a number to the string form needed for the
# file path (1-3 needs 1,2,3; 1-12 needs 01, 02, ..., 11, 12)
def makeSliceValues(n):
if 10 <= n <99:
formatString = "%02d"
elif 1 <= n <= 9:
formatString = "%1d"
return [formatString%x for x in range(1,n+1)]
# FIXME
# should probably make empty stain values
# "" (empty string) instead of None
#
def reduceStain(sta):
try:
return sta[:3].lower()
except TypeError:
if sta == None:
return None
else:
raise TypeError
##################################################
##################################################
# Setting up experimental conditions
##################################################
##################################################
organelles = ["Endosomes", "Vesicles", "Lysosomes"]
# FIXME None --> "" ??? might be better
stains = {"Endosomes" : [None],
"Vesicles" : ["Arf", "Rab4"],
"Lysosomes" : [None]}
stainStrs = {"Endosomes" : ["NA"],
"Vesicles" : ["Arf", "Rab4"],
"Lysosomes" : ["NA"]}
series = [x for x in range(1,7)]
times = [10, 15, 30, 60, 120]
def simplifyOrgStain(org, stain):
simpleName = org
if simpleName == "Vesicles":
simpleName += "(" + stain + ")"
return simpleName
organelleStainStrings = ["Endosomes", "Lysosomes",
"Vesicles(Arf)", "Vesicles(Rab4)"]
#
# ser is really not part of the condition, but it simplifies things to
# specify it here
#
allExpCnds = [ {"Organelle":o, "Stain":sta, "ReducedStain":reduceStain(sta),
"Series":ser, "Time":t}
for o in organelles
for sta in stains[o]
for t in times
for ser in series]
allNonSerExpCnds = [ {"Organelle":o, "Stain":sta,
"ReducedStain":reduceStain(sta), "Time":t}
for o in organelles
for sta in stains[o]
for t in times]
coefficients = ["Pearson", "Manders",
"Coloc(m)1", "Coloc(m)2",
"Overlap(k)1", "Overlap(k)2"]
#
# FIXME
# perhaps the allExpCnds should be instances of expSetupKey
#
class expSetupKey:
def __init__(self, org, sta, time):
self.organelle = org
self.stain = sta
self.time = time
def __hash__(self):
return hash(self.organelle) ^ hash(self.stain) ^ hash(self.time)
def __cmp__(self, other):
return cmp(self.organelle, other.organelle) and \
cmp(self.stain, other.stain) and \
cmp(self.time, other.time)
##################################################
##################################################
# Computing the predicted thresholds for background
# from the linear model learned on expert values
##################################################
##################################################
#
# these should be determined from the LM formula
# brittle if LM changes FIXME
#
# iles = (.8, .9)
# def applyTM2(example, color):
# # even worse: ex["Slice"] is a string
# prediction = 17.9453 # intercept
# prediction += float(example["Slice"]) * 0.2175
# if color == "G":
# prediction += -14.1770
# elif color == "R":
# prediction += -15.3520
# prediction += example[color+"8D"] * -0.3105
# prediction += example[color+"9D"] * 0.7656
# return prediction
# def applyLM(example, color)
# prediction = 17.9453 # intercept
# if color == "G":
# prediction += -14.1770
# elif color == "R":
# prediction += -15.3520
# prediction += example[color+"8D"] * -0.3105
# prediction += example[color+"9D"] * 0.7656
# return prediction
##################################################
##################################################
# Read expert thresholds from file into dictionary
##################################################
##################################################
#
# this is used to work with the expert thresholded files
# (1) develop LM; (2) compare expert against predicted; (3) other???
#
def readThresholdFileAsDictionaries(thresholdFilename):
tfile = open(thresholdFilename)
# not pretty, needed for fileNameFormats b/c they expect certain things
# (can they be made "agnostic"? probably if everything is a string
# but some have to be used as number ... for example in computing
# predicted thresholds)
forceString = ("Slice",)
forceInteger = ("Series", "Time")
#
# get dictionary keys from header line in file
#
orderedKeys = tfile.readline().strip().split(",")
orderedKeys = [k.strip("\"") for k in orderedKeys]
#
# build a list of examples: each example is a dict
# with keys from orderedKeys
#
thresholdExamples = []
for line in tfile.readlines():
if line.startswith("#"):
continue
exampleDict = {}
# walk in lock step across the ordered keys
# and the values on this line ... put them
# together as exampleDict[key] = value
# with a couple special cases
for value, key in zip(line.split(","), orderedKeys):
value = value.strip()
try:
result = eval(value)
except NameError: # variable symbol is unquoted string
result = str(value)
#
# forced keys enforce a conversion
#
if key in forceString:
result = str(result)
elif key in forceInteger:
result = int(result)
if key == "Slice" and result == "10":
# back pad previous Slice values (9 of them)
# add 10
# FIXME: April 14, 2010
# ACK! not necessarily ... have to walk back until
# we get a previous Repetition or back to 0
# ugh!!!
# hopefully fixed, apr 14, 2010
#for prevEx in thresholdExamples[-9:]:
# prevEx["Slice"] = "0" + prevEx["Slice"]
thisSeries = exampleDict["Series"]
prevIdx = -1
prevEx = thresholdExamples[prevIdx]
while prevEx["Series"] == thisSeries:
prevEx["Slice"] = "0" + prevEx["Slice"]
prevIdx = prevIdx - 1
try:
prevEx = thresholdExamples[prevIdx]
except IndexError:
break
elif key == "Stain":
# add key for reducedStain value
exampleDict["ReducedStain"] = result[:3].lower()
exampleDict[key] = result
thresholdExamples.append(exampleDict)
tfile.close()
return thresholdExamples
# from a list of dictionaries of (expert) thresholded examples
# return groups of stacks (found by new slice >= new slice + 1 and
# series == series)
#
# added >= slice to allow for skipped slices due to bad data/image
#
# we do this b/c we process a stack at a time for
# coloc. coefficients
def breakIntoStacks(all):
startIndex = 0
while startIndex < len(all):
initial=all[startIndex]
nextSlice = int(initial["Slice"]) + 1
thisSeries = initial["Series"]
currentImgStack = [initial]
i = startIndex + 1
while i < len(all) and \
int(all[i]["Slice"]) >= nextSlice and \
all[i]["Series"] == thisSeries:
currentImgStack.append(all[i])
nextSlice += 1
i += 1
yield currentImgStack
startIndex = i
def breakByConditions(all):
def makeCnd(d):
return (d["Organelle"], d["Stain"], d["Time"])
currentStartIndex = 0
currentCnd = makeCnd(all[currentStartIndex])
while currentStartIndex < len(all):
currentCndImgList = [all[currentStartIndex]]
nextIndex = currentStartIndex + 1
nextCnd = makeCnd(all[nextIndex])
while nextCnd == currentCnd:
currentCndImgList.append(all[nextIndex])
nextIndex += 1
try:
nextCnd = makeCnd(all[nextIndex])
except IndexError: # nextIndex == len(all)
break # so exit loop, yield this stack,
# nextIdx -> currIdx and end outer loop
# possibly could: yield current
# then: raise StopIteration
yield currentCndImgList
currentStartIndex = nextIndex
currentCnd = nextCnd
def breakConditionIntoStacks(cnd):
startIndex = 0
while startIndex < len(cnd):
initial=cnd[startIndex]
nextSlice = int(initial["Slice"]) + 1
thisSeries = initial["Series"]
currentImgStack = [initial]
i = startIndex + 1
while i < len(cnd) and \
int(cnd[i]["Slice"]) >= nextSlice and \
cnd[i]["Series"] == thisSeries:
currentImgStack.append(cnd[i])
nextSlice += 1
i += 1
yield currentImgStack
startIndex = i