-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingClass.py
274 lines (245 loc) · 11.6 KB
/
RingClass.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
'''
Created on Jun 10, 2014
@author: renat
contractile ring object class
'''
import csv
from myMath import getSubIndex, fitLine,getAngle, rotate
from scipy.optimize import leastsq
from myCKfunc import fitRingSizeHalf
from myFunc import a16a8, pil16pil8
import numpy as np
class RingClass(object):
def __init__(self, ringFile, timeStep=35.7):
self.file = ringFile
self.label = ringFile.split('/')[-1]
self.timeStep = timeStep # time in seconds between each ring
self.ringSizeThresh = 0.3 # the threshold for the ring size to consider for ring closure angle
self.rLinMax = 0.8 # maximum ring size to consider for linear rate fitting
self.rLinMin = 0.3 # minimum ring size to consider for linear rate fitting
self.nSlices = None
self.aveR0 = 1.1
self.dataList=None
def rescaleTime(self, scaling=0):
self.timeScaling = scaling
# 0 - exponential model
# 1 - exponential with arbitrary defined time origin
# 2 - linear fit
# 3 - fit to the average from the flow
if scaling==0: self.rescaleTimeModel()
elif scaling==1: self.rescaleTimeModelPolym()
elif scaling==2: self.rescaleTimeLinear()
elif scaling==3: self.rescaleTimeAvgFit()
else: self.rescaleTimeFuncFit(scaling)
def loadRing(self):
''' loads information from a Ring.csv file '''
def getCenterXOldStyle(row):
''' returns centerX position from old style stored data '''
centers = [float(row[2][1:-1])]
for i in range(3, len(row)):
centers.append(float(row[i][:-1]))
return np.mean(centers), np.std(centers)
def getCenterYOldStyle(rowPrev,row):
''' returns centerY position from old style stored data '''
centersY = []
for i in range(len(rowPrev)):
try: centersY.append(float(rowPrev[i]))
except: pass
for i in range(len(row)):
try: centersY.append(float(row[i]))
except: pass
return np.array(centersY), np.ones_like(centersY)*0.3
print('loading ring ', self.label)
csvFile = csv.reader(open(self.file, 'rb'), delimiter=' ')
ringIndex, centerX, centerY, radius, centerXerr, centerYerr, radiuserr = [], [], [], [], [], [], []
dist, disterr, embCenterY, embCenterYerr = [], [], [], []
flag, flagEmbC, oldEmbCY = False, False, False
rowPrev=None
for row in csvFile:
if row[0]=='ringPosition' or row[0]=='ringCenterY': self.embRingPos = int(float(row[2]))
if row[0]=='angle': self.embAngle = float(row[2])
if row[0]=='embryoCenterX':
try:
self.embCenterX, self.embCenterXerr = int(float(row[2])), float(row[3])
self.expand=True
except:
self.embCenterX, self.embCenterXerr = getCenterXOldStyle(row)
self.expand=False
if row[0]== 'embryoDiam':
self.embryoDiam, self.embryoDiamErr = float(row[2]),0.3#float(row[3])
if row[0]== 'timePoint':
flag = True
flagEmbC = False
if flagEmbC:
embCenterY.append(float(row[1]))
embCenterYerr.append(float(row[2]))
if oldEmbCY:
embCenterY, embCenterYerr = getCenterYOldStyle(rowPrev, row)
oldEmbCY = False
if row[0]=='embryoCenterY':
oldEmbCY = True
rowPrev=row
if row[0]== 'timePint,': flagEmbC = True
if row[0]== 'nSlides': self.nSlices = int(row[2])
if flag:
try:
ringIndex.append(int(row[0]))
centerX.append(float(row[1]))
centerXerr.append(float(row[2]))
centerY.append(float(row[3]))
centerYerr.append(float(row[4]))
radius.append(float(row[5]))
radiuserr.append(float(row[6]))
dist.append(np.sqrt(centerX[-1]**2+centerY[-1]**2))
disterr.append((abs(centerX[-1]*centerXerr[-1])+abs(centerY[-1]*centerYerr[-1]))/dist[-1])
except:
pass
self.embCenterY = np.array(embCenterY)
self.embCenterYerr = np.array(embCenterYerr)
self.ringIndex = np.array(ringIndex) #+1 because flow is imaged before ring and -2 because flow starts at 0s
self.centerX = np.array(centerX)
self.centerY = np.array(centerY)
self.radius = np.array(radius)
self.centerXerr = np.array(centerXerr)
self.centerYerr = np.array(centerYerr)
self.radiuserr = np.array(radiuserr)
self.dist = np.array(dist)
self.disterr = np.array(disterr)
self.getAngle()
def loadRingImages(self):
import Image
self.nSlices = 15
self.zPixelScale = 10
fileName = self.file[:-3]+'tif'
im = Image.open(fileName)
im.seek((self.ringIndex[0])*self.nSlices)
dataList = []
imList = []
try:
while True:
if np.max(np.asarray(im))>0:
dataList.append(a16a8(np.asarray(im.rotate(self.embAngle, expand=self.expand))))
imList.append(pil16pil8(im.rotate(self.embAngle, expand=self.expand)))
else:
dataList.append(dataList[-1])
imList.append(imList[-1])
im.seek(im.tell()+1)
except EOFError:
pass # end of sequence
del im
if len(imList)%self.nSlices>0: imList = imList[:-(len(imList)%self.nSlices)]
if len(dataList)%self.nSlices>0: dataList = dataList[:-(len(dataList)%self.nSlices)]
self.dataList = dataList
def rescaleTimeLinear(self):
''' determines rate and time zero from fit to an average ring size curve and rescales time '''
if self.radius.size<=3: index = np.arange(self.radius.size)
else:
index = getSubIndex(self.radius[::-1], self.rLinMin, self.rLinMax)
index = self.radius.size - index -1
if index.size<3: index=np.arange(3)
slope, inter, se, ie = fitLine(self.time[index], self.radius[index])
self.zeroTime = (1. - inter)/slope
self.tck = -1./slope
self.halfTime = (0.5 - inter)/slope
self.tau = -np.log(2.*self.aveR0)/2./slope
self.fitR0 = self.aveR0
#
def rescaleTimeAvgFit(self):
def getR(t, err=False):
''' the data is from Khaliullin paper
ave tck=200 +/- 30 s'''
time=np.arange(-0.2,1.2,0.02)
Rexp = np.fromfile('/home/renat/Documents/work/python/FlowCombine/ringRadiusExp.dat', dtype=np.float32)[2:]
RexpErr=Rexp[Rexp.size/2:]
Rexp=Rexp[:Rexp.size/2]
if err: return np.interp(t, time, Rexp, 1), np.interp(t, time, RexpErr)
return np.interp(t, time, Rexp, 1)
def resid(params):
tck, t0 = params
t = (self.time - t0)/tck
r = getR(t)
return (r - self.radius)#/self.radiuserr
self.rescaleTimeLinear()
pars, ier = leastsq(resid, (self.tck, self.zeroTime))
self.tck, self.zeroTime = pars
self.halfTime = self.zeroTime+0.5*self.tck
self.tau = np.log(2.*self.aveR0)/2.*self.tck
self.Rfit=1
def rescaleTimeFuncFit(self, func):
def resid(params):
tck, t0 = params
t = (self.time - t0)/tck
r = func(t)
return (r - self.radius)#/self.radiuserr
self.rescaleTimeLinear()
pars, ier = leastsq(resid, (self.tck, self.zeroTime))
self.tck, self.zeroTime = pars
self.halfTime = self.zeroTime+0.5*self.tck
self.tau = np.log(2.*self.aveR0)/2.*self.tck
self.Rfit=1
def rescaleTimeModel(self):
self.Rfit = fitRingSizeHalf(self.time, self.radius, None)
if self.Rfit!=None:
self.fitR0=self.Rfit.best_values['r0']
self.tau = self.Rfit.best_values['tau']
self.halfTime = self.Rfit.best_values['t0']
self.tck = 2.*self.tau/np.log(2.*self.aveR0)
self.zeroTime = self.halfTime-0.5*self.tck
else:
self.tau, self.halfTime, self.tck, self.zeroTime = None, None
def getNormT(self, t=None):
if t==None: t=self.time
if self.timeScaling==0 or self.timeScaling==1: return (t-self.halfTime)/self.tau
else: return (t-self.zeroTime)/self.tck
def getRealT(self, t=None):
if t==None: t=self.time
return (t-self.zeroTime)
def getNormR(self):
return self.fitR0*np.power(self.radius/self.fitR0,np.log(2.*self.aveR0)/np.log(2.*self.fitR0))
def getNormT_Ret(self):
tau, t0 = self.RfitRet.best_values['tau'], self.RfitRet.best_values['t0']
return (self.time-t0)/tau
def getNormR_Ret(self):
tau, d, b = self.RfitRet.best_values['tau'], self.RfitRet.best_values['d'], self.RfitRet.best_values['b']
return ((np.power(self.radius*2.,d)-1)/d/b/tau+1)/2.
def getAngle(self):
''' calculates angle (direction) of the ring movement in the division plane '''
x = np.concatenate(([0], self.centerX))
y = np.concatenate(([0], self.centerY))
xe = np.concatenate(([0.01], self.centerXerr)) #the (0,0) center errorbar is calculated from embryo center estimation errorbars/embryo radius
ye = np.concatenate(([0.01], self.centerYerr))
index = np.concatenate(([0],np.where(self.radius>=self.ringSizeThresh)[0]+1))
self.angle = getAngle(x, y, xe, ye, index)
self.angle+=np.pi/2 #adding pi/2 because angle is calculated between ring direction and x-axis, but imaging plane is 90 degree with x-axis
if self.angle>=2*np.pi: self.angle-=2*np.pi
if self.angle<0: self.angle+=2*np.pi
def getRadius(self, time):
''' returns interpolated ring radius at normalized time '''
if np.min(np.abs(time-self.time))<0.01: return self.radius[np.argmin(np.abs(time-self.time))]
else: return np.interp(time, self.time, self.radius)
def getRadiusErr(self, time):
''' returns interpolated error of the ring radius at time '''
return np.interp(time, self.time, self.radiuserr)
def getDist(self, radius):
''' returns interpolated ring distance from the center of the embryo at time '''
return np.interp(radius, self.radius[::-1], self.dist[::-1], left=0, right=0)
def getDistErr(self, radius):
''' returns interpolated error of the ring distance from the center of the embryo at time '''
return np.interp(radius, self.radius[::-1], self.disterr[::-1])
def getCenterRot(self, radius, angle):
cx = self.getCenterX(radius)
cy = self.getCenterY(radius)
ce = np.sqrt(self.getCenterXErr(radius)**2+self.getCenterYErr(radius)**2)
xNew, yNew = rotate(cx, cy, angle)
return xNew, yNew, ce
def getCenterX(self, radius):
''' interpolates position of the ring center for given ring size '''
return np.interp(radius, self.radius[::-1], self.centerX[::-1])
def getCenterY(self, radius):
return np.interp(radius, self.radius[::-1], self.centerY[::-1])
def getCenterXErr(self, radius):
return np.interp(radius, self.radius[::-1], self.centerXerr[::-1])
def getCenterYErr(self, radius):
return np.interp(radius, self.radius[::-1], self.centerYerr[::-1])
if __name__ == '__main__':
pass