-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifvesiclemoves.py
203 lines (111 loc) · 6.38 KB
/
ifvesiclemoves.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
import tifffile as tf
import numpy as np
from skimage.feature import peak_local_max
from skimage.filters import gaussian
from matplotlib import pyplot as plt
def subtract_moved_vesicles(self,frames,start_frame,trap_positions=None,labels=None):
#frames: A TiffFile object from which we import frames as a numpy array
#start_frame: The frame defining the start of the experiment.
#the TrapGetter object which contains the list of initial vesicle positions and the corresponding labels
frames = frames.asarray()
subtracted_frames = frames[:-1] - frames[1:]
interficiendum = None
if subtracted_frames.shape[0] > start_frame + 1200:
end_frame = start_frame +1200
else:
end_frame = subtracted_frames.shape[0]
for frame_index in range(start_frame,end_frame):
frame = subtracted_frames[frame_index]
frame = gaussian(frame,10)
pos_peaks = peak_local_max(frame,min_distance=40,threshold_rel=0.95)
neg_peaks = peak_local_max(np.max(frame)-frame,min_distance=40,threshold_rel=0.95)
pos_peaks = np.array(pos_peaks)
neg_peaks = np.array(neg_peaks)
if len(pos_peaks) >0 and len(neg_peaks) > 0:
#if we find a positive and a negative peak in the subtracted frame, we believe that a vesicle has moved
#remains to pair the negative peak and positive peak to make sure a vesicle moved and didnt just burst
try:
pair_vectors = pos_peaks[np.newaxis] - neg_peaks
except ValueError:
pair_vectors = pos_peaks[np.newaxis]-neg_peaks[0]
for peak in neg_peaks[1:]:
pair_vector = pos_peaks[np.newaxis]-peak
pair_vectors = np.vstack((pair_vectors,pair_vector))
print(pair_vectors,pair_vectors.shape)
if pair_vectors.shape[1] ==1:
if interficiendum is None:
interficiendum = neg_peaks
else:
interficiendum = np.vstack((interficiendum,neg_peaks))
else:
pair = pair_vectors[np.absolute(pair_vectors[:,:,0]) < 60]
if pair_vectors.shape[1] == 1:
if interficiendum is None:
interficiendum = neg_peaks[pair_vectors[:,0] < 60]
else:
interficiendum = np.vstack(interficiendum,neg_peaks[pair_vectors[:,0] < 60])
else:
pair = pair_vectors[pair_vectors[:,:,1] < 0]
pair = pair[pair[:,0] < 60]
#if there is at least a pair of positive and negative peaks which are vertically less than 60 pixels away and arranged horizontally so the positive peak is on the left
#then we choose to bin the vesicle which in the previous frame was in the position of the nearest negative peak to a positive peak
if neg_peaks.shape[0] > 1 and pos_peaks.shape[0] == 1 and len(pair) and len(pair[0]) > 0:
peak = neg_peaks[[np.absolute(pair_vectors[:,:,0]) == np.min(np.absolute(pair_vectors[:,:,0]))][0][0]]
else:
peak = neg_peaks
if interficiendum is None:
interficiendum = peak
else:
interficiendum = np.vstack((interficiendum,peak))
if trap_positions is None or labels is None:
return -1
trap_positions += [0,5]
separations = trap_positions[:,:,np.newaxis] - interficiendum.T
separations = np.linalg.norm(separations,axis = 1)
killlabels = labels[np.sum(separations < 10, axis = 1) == True]
print(killlabels)
return killlabels
def findmovedvesicles(frame):
frame = gaussian(frame,10)
pos_peaks = peak_local_max(frame,min_distance=30,threshold_rel=0.9)
neg_peaks = peak_local_max(np.max(frame)-frame,min_distance=40,threshold_rel=0.9)
pos_peaks = np.array(pos_peaks)
neg_peaks = np.array(neg_peaks)
'''
plt.imshow(frame,zorder = 1)
if len(pos_peaks) > 0:
plt.scatter(pos_peaks[:,1],pos_peaks[:,0],marker = '+', s = 6,c = 'r',zorder = 2)
if len(neg_peaks) > 0:
plt.scatter(neg_peaks[:,1],neg_peaks[:,0],marker = '+',s = 6, c= 'r',zorder = 2)
plt.show()
'''
interficiendum = None
if len(pos_peaks) >0 and len(neg_peaks) > 0:
#if we find a positive and a negative peak in the subtracted frame, we believe that a vesicle has moved
#remains to pair the negative peak and positive peak to make sure a vesicle moved and didnt just burst
try:
pair_vectors = pos_peaks[np.newaxis] - neg_peaks
except ValueError:
pair_vectors = pos_peaks[np.newaxis]-neg_peaks[0]
for peak in neg_peaks[1:]:
pair_vector = pos_peaks[np.newaxis]-peak
pair_vectors = np.vstack((pair_vectors,pair_vector))
print(pair_vectors,pair_vectors.shape)
if pair_vectors.shape[1] ==1:
interficiendum = neg_peaks
else:
pair = pair_vectors[np.absolute(pair_vectors[:,:,0]) < 60]
if pair_vectors.shape[1] == 1:
interficiendum = neg_peaks[pair_vectors[:,0] < 60]
else:
pair = pair_vectors[pair_vectors[:,:,1] < 0]
pair = pair[pair[:,0] < 60]
#if there is at least a pair of positive and negative peaks which are vertically less than 60 pixels away and arranged horizontally so the positive peak is on the left
#then we choose to bin the vesicle which in the previous frame was in the position of the nearest negative peak to a positive peak
if neg_peaks.shape[0] > 1 and pos_peaks.shape[0] == 1 and len(pair) and len(pair[0]) > 0:
peak = neg_peaks[[np.absolute(pair_vectors[:,:,0]) == np.min(np.absolute(pair_vectors[:,:,0]))][0][0]]
else:
peak = neg_peaks
interficiendum = peak
print(interficiendum)
return interficiendum