1
|
|
|
import pandas as pd |
2
|
|
|
import numpy as np |
3
|
|
|
import skimage.io as sio |
4
|
|
|
import numpy.ma as ma |
5
|
|
|
import pandas.util.testing as pdt |
6
|
|
|
import numpy.testing as npt |
7
|
|
|
import diff_classifier.msd as msd |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def test_nth_diff(): |
11
|
|
|
|
12
|
|
|
data1 = {'col1': [1, 2, 3, 4, 5]} |
13
|
|
|
df = pd.DataFrame(data=data1) |
14
|
|
|
|
15
|
|
|
test_d = {'col1': [1, 1, 1, 1]} |
16
|
|
|
test_df = pd.DataFrame(data=test_d) |
17
|
|
|
|
18
|
|
|
pdt.assert_series_equal(msd.nth_diff(df['col1'], 1), test_df['col1']) |
19
|
|
|
|
20
|
|
|
# test2 |
21
|
|
|
df = np.ones((5, 10)) |
22
|
|
|
test_df = np.zeros((5, 9)) |
23
|
|
|
npt.assert_equal(msd.nth_diff(df, 1, 1), test_df) |
24
|
|
|
|
25
|
|
|
df = np.ones((5, 10)) |
26
|
|
|
test_df = np.zeros((4, 10)) |
27
|
|
|
npt.assert_equal(msd.nth_diff(df, 1, 0), test_df) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_msd_calc(): |
31
|
|
|
|
32
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5], |
33
|
|
|
'X': [5, 6, 7, 8, 9], |
34
|
|
|
'Y': [6, 7, 8, 9, 10]} |
35
|
|
|
df = pd.DataFrame(data=data1) |
36
|
|
|
new_track = msd.msd_calc(df, 5) |
37
|
|
|
|
38
|
|
|
npt.assert_equal(np.array([0, 2, 8, 18, 32] |
39
|
|
|
).astype('float64'), new_track['MSDs']) |
40
|
|
|
npt.assert_equal(np.array([0, 0.25, 0.25, 0.25, 0.25] |
41
|
|
|
).astype('float64'), new_track['Gauss']) |
42
|
|
|
|
43
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5], |
44
|
|
|
'X': [5, 6, 7, 8, 9], |
45
|
|
|
'Y': [6, 7, 8, 9, 10]} |
46
|
|
|
df = pd.DataFrame(data=data1) |
47
|
|
|
new_track = msd.msd_calc(df) |
48
|
|
|
|
49
|
|
|
npt.assert_equal(np.array([0, 2, 8, 18, 32, np.nan, np.nan, np.nan, np.nan, |
50
|
|
|
np.nan]).astype('float64'), new_track['MSDs']) |
51
|
|
|
npt.assert_equal(np.array([0, 0.25, 0.25, 0.25, 0.25, np.nan, np.nan, |
52
|
|
|
np.nan, np.nan, np.nan] |
53
|
|
|
).astype('float64'), new_track['Gauss']) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_all_msds(): |
57
|
|
|
|
58
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], |
59
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
60
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
61
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6]} |
62
|
|
|
df = pd.DataFrame(data=data1) |
63
|
|
|
|
64
|
|
|
di = {'Frame': [float(i) for i in[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]], |
65
|
|
|
'Track_ID': [float(i) for i in[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]], |
66
|
|
|
'X': [float(i) for i in[5, 6, 7, 8, 9, 1, 2, 3, 4, 5]], |
67
|
|
|
'Y': [float(i) for i in[6, 7, 8, 9, 10, 2, 3, 4, 5, 6]], |
68
|
|
|
'MSDs': [float(i) for i in[0, 2, 8, 18, 32, 0, 2, 8, 18, 32]], |
69
|
|
|
'Gauss': [0, 0.25, 0.25, 0.25, 0.25, 0, 0.25, 0.25, 0.25, 0.25]} |
70
|
|
|
cols = ['Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss'] |
71
|
|
|
|
72
|
|
|
dfi = pd.DataFrame(data=di)[cols] |
73
|
|
|
|
74
|
|
|
pdt.assert_frame_equal(dfi, msd.all_msds(df)[cols]) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_make_xyarray(): |
78
|
|
|
|
79
|
|
|
data1 = {'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4], |
80
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
81
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
82
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6], |
83
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
84
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
85
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
86
|
|
|
df = pd.DataFrame(data=data1) |
87
|
|
|
|
88
|
|
|
length = max(df['Frame']) + 1 |
89
|
|
|
xyft = msd.make_xyarray(df, length=length) |
90
|
|
|
|
91
|
|
|
tt_array = np.array([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]).astype(float) |
92
|
|
|
ft_array = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]).astype(float) |
93
|
|
|
xt_array = np.array([[5, 1], [6, 2], [7, 3], [8, 4], [9, 5]]).astype(float) |
94
|
|
|
yt_array = np.array([[6, 2], [7, 3], [8, 4], [9, 5], [10, 6]]).astype(float) |
95
|
|
|
|
96
|
|
|
npt.assert_equal(xyft['tarray'], tt_array) |
97
|
|
|
npt.assert_equal(xyft['farray'], ft_array) |
98
|
|
|
npt.assert_equal(xyft['xarray'], xt_array) |
99
|
|
|
npt.assert_equal(xyft['yarray'], yt_array) |
100
|
|
|
|
101
|
|
|
# Second test |
102
|
|
|
data1 = {'Frame': [0, 1, 2, 3, 4, 2, 3, 4, 5, 6], |
103
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
104
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
105
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6], |
106
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
107
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
108
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
109
|
|
|
df = pd.DataFrame(data=data1) |
110
|
|
|
|
111
|
|
|
length = max(df['Frame']) + 1 |
112
|
|
|
xyft = msd.make_xyarray(df, length=length) |
113
|
|
|
|
114
|
|
|
tt_array = np.array([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]] |
115
|
|
|
).astype(float) |
116
|
|
|
ft_array = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]] |
117
|
|
|
).astype(float) |
118
|
|
|
xt_array = np.array([[5, np.nan], [6, np.nan], [7, 1], [8, 2], [9, 3], |
119
|
|
|
[np.nan, 4], [np.nan, 5]]).astype(float) |
120
|
|
|
yt_array = np.array([[6, np.nan], [7, np.nan], [8, 2], [9, 3], [10, 4], |
121
|
|
|
[np.nan, 5], [np.nan, 6]]).astype(float) |
122
|
|
|
|
123
|
|
|
npt.assert_equal(xyft['tarray'], tt_array) |
124
|
|
|
npt.assert_equal(xyft['farray'], ft_array) |
125
|
|
|
npt.assert_equal(xyft['xarray'], xt_array) |
126
|
|
|
npt.assert_equal(xyft['yarray'], yt_array) |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
def test_all_msds2(): |
130
|
|
|
|
131
|
|
|
data1 = {'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4], |
132
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
133
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
134
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6], |
135
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
136
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
137
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
138
|
|
|
df = pd.DataFrame(data=data1) |
139
|
|
|
|
140
|
|
|
di = {'Frame': [float(i) for i in[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]], |
141
|
|
|
'Track_ID': [float(i) for i in[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]], |
142
|
|
|
'X': [float(i) for i in[5, 6, 7, 8, 9, 1, 2, 3, 4, 5]], |
143
|
|
|
'Y': [float(i) for i in[6, 7, 8, 9, 10, 2, 3, 4, 5, 6]], |
144
|
|
|
'MSDs': [float(i) for i in[0, 2, 8, 18, 32, 0, 2, 8, 18, 32]], |
145
|
|
|
'Gauss': [0, 0.25, 0.25, 0.25, 0.25, 0, 0.25, 0.25, 0.25, 0.25], |
146
|
|
|
'Quality': [float(i) for i in[10, 10, 10, 10, 10, |
147
|
|
|
10, 10, 10, 10, 10]], |
148
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
149
|
|
|
'Mean_Intensity': [float(i) for i in[10, 10, 10, 10, 10, |
150
|
|
|
10, 10, 10, 10, 10]]} |
151
|
|
|
cols = ['Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss', 'Quality', |
152
|
|
|
'SN_Ratio', 'Mean_Intensity'] |
153
|
|
|
|
154
|
|
|
dfi = pd.DataFrame(data=di)[cols] |
155
|
|
|
|
156
|
|
|
length = max(df['Frame']) + 1 |
157
|
|
|
pdt.assert_frame_equal(dfi, msd.all_msds2(df, frames=length)[cols]) |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
def test_geomean_msdisp(): |
161
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], |
162
|
|
|
'Track_ID': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], |
163
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
164
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6], |
165
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
166
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
167
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
168
|
|
|
|
169
|
|
|
geomean_t = np.array([2., 8., 18., 32.]) |
170
|
|
|
geostder_t = np.array([]) |
171
|
|
|
df = pd.DataFrame(data=data1) |
172
|
|
|
msds = msd.all_msds2(df) |
173
|
|
|
msds.to_csv('msd_test.csv') |
174
|
|
|
|
175
|
|
|
geomean, geostder = msd.geomean_msdisp('test', umppx=1, fps=1, upload=False) |
176
|
|
|
npt.assert_equal(np.round(np.exp(geomean[geomean.mask == False].data), 1), |
177
|
|
|
geomean_t) |
178
|
|
|
npt.assert_equal(np.round(np.exp(geostder[geostder.mask == False].data), 1), |
179
|
|
|
geostder_t) |
180
|
|
|
|
181
|
|
|
# test 2 |
182
|
|
|
data1 = {'Frame': [1, 2, 1, 2], |
183
|
|
|
'Track_ID': [1, 1, 2, 2], |
184
|
|
|
'X': [1, 2, 3, 4], |
185
|
|
|
'Y': [1, 2, 3, 4], |
186
|
|
|
'Quality': [10, 10, 10, 10], |
187
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1], |
188
|
|
|
'Mean_Intensity': [10, 10, 10, 10]} |
189
|
|
|
df = pd.DataFrame(data=data1) |
190
|
|
|
msds = msd.all_msds2(df) |
191
|
|
|
msds.to_csv('msd_test.csv') |
192
|
|
|
geomean, geostder = msd.geomean_msdisp('test', umppx=1, fps=1, upload=False) |
193
|
|
|
npt.assert_equal(geomean, np.nan*np.ones(651)) |
194
|
|
|
npt.assert_equal(geostder, np.nan*np.ones(651)) |
195
|
|
|
|
196
|
|
|
# test 3 |
197
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], |
198
|
|
|
'Track_ID': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], |
199
|
|
|
'X': [5, 6, 7, 8, 9, 2, 4, 6, 8, 10], |
200
|
|
|
'Y': [6, 7, 8, 9, 10, 6, 8, 10, 12, 14], |
201
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
202
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
203
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
204
|
|
|
df = pd.DataFrame(data=data1) |
205
|
|
|
geomean_t = np.array([4., 16., 36., 64.]) |
206
|
|
|
geostder_t = np.array([2., 2., 2., 2]) |
207
|
|
|
msds = msd.all_msds2(df) |
208
|
|
|
msds.to_csv('msd_test.csv') |
209
|
|
|
|
210
|
|
|
geomean, geostder = msd.geomean_msdisp('test', umppx=1, fps=1, upload=False) |
211
|
|
|
npt.assert_equal(np.round(np.exp(geomean[geomean.mask == False].data), 1), |
212
|
|
|
geomean_t) |
213
|
|
|
npt.assert_equal(np.round(np.exp(geostder[geostder.mask == False].data), 1), |
214
|
|
|
geostder_t) |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
def test_binning(): |
218
|
|
|
experiments = [] |
219
|
|
|
for num in range(8): |
|
|
|
|
220
|
|
|
experiments.append('test_{}'.format(num)) |
221
|
|
|
bins_t = {'test_W0': ['test_0', 'test_1'], |
222
|
|
|
'test_W1': ['test_2', 'test_3'], |
223
|
|
|
'test_W2': ['test_4', 'test_5'], |
224
|
|
|
'test_W3': ['test_6', 'test_7']} |
225
|
|
|
bin_names_t = ['test_W0', 'test_W1', 'test_W2', 'test_W3'] |
226
|
|
|
slices, bins, bin_names = msd.binning(experiments) |
227
|
|
|
|
228
|
|
|
assert slices == 2 |
229
|
|
|
assert bins == bins_t |
230
|
|
|
assert bin_names == bin_names_t |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
def test_precision_weight(): |
234
|
|
|
experiments = [] |
235
|
|
|
geomean = {} |
236
|
|
|
geostder = {} |
237
|
|
|
for num in range(4): |
|
|
|
|
238
|
|
|
name = 'test_{}'.format(num) |
239
|
|
|
experiments.append(name) |
240
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], |
241
|
|
|
'Track_ID': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], |
242
|
|
|
'X': [x*(num+1) for x in [5, 6, 7, 8, 9, 2, 4, 6, 8, 10]], |
243
|
|
|
'Y': [x*(num+1) for x in [6, 7, 8, 9, 10, 6, 8, 10, 12, 14]], |
244
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
245
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
246
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
247
|
|
|
df = pd.DataFrame(data=data1) |
248
|
|
|
msds = msd.all_msds2(df) |
249
|
|
|
msds.to_csv('msd_test_{}.csv'.format(num)) |
250
|
|
|
geomean[name], geostder[name] = msd.geomean_msdisp(name, umppx=1, fps=1, |
251
|
|
|
upload=False) |
252
|
|
|
|
253
|
|
|
slices, bins, bin_names = msd.binning(experiments, wells=1) |
254
|
|
|
weights, w_holder = msd.precision_weight(experiments, geostder) |
255
|
|
|
weights_t = np.array([8.3, 8.3, 8.3, 8.3]) |
256
|
|
|
npt.assert_equal(np.round(weights[weights.mask == False].data, 1), |
257
|
|
|
weights_t) |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
def test_precision_averaging(): |
261
|
|
|
experiments = [] |
262
|
|
|
geomean = {} |
263
|
|
|
geostder = {} |
264
|
|
|
for num in range(4): |
|
|
|
|
265
|
|
|
name = 'test_{}'.format(num) |
266
|
|
|
experiments.append(name) |
267
|
|
|
data1 = {'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], |
268
|
|
|
'Track_ID': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], |
269
|
|
|
'X': [x*(num+1) for x in [5, 6, 7, 8, 9, 2, 4, 6, 8, 10]], |
270
|
|
|
'Y': [x*(num+1) for x in [6, 7, 8, 9, 10, 6, 8, 10, 12, 14]], |
271
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
272
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
273
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
274
|
|
|
df = pd.DataFrame(data=data1) |
275
|
|
|
msds = msd.all_msds2(df) |
276
|
|
|
msds.to_csv('msd_test_{}.csv'.format(num)) |
277
|
|
|
geomean[name], geostder[name] = msd.geomean_msdisp(name, umppx=1, fps=1, |
278
|
|
|
upload=False) |
279
|
|
|
|
280
|
|
|
slices, bins, bin_names = msd.binning(experiments, wells=1) |
281
|
|
|
weights, w_holder = msd.precision_weight(experiments, geostder) |
282
|
|
|
geodata = msd.precision_averaging(experiments, geomean, geostder, weights, |
283
|
|
|
save=False) |
284
|
|
|
|
285
|
|
|
geostd_t = np.array([0.3, 0.3, 0.3, 0.3]) |
286
|
|
|
geo_t = np.array([19.6, 78.4, 176.4, 313.5]) |
287
|
|
|
npt.assert_equal(np.round(geodata.geostd[geodata.geostd.mask == False].data, |
288
|
|
|
1), geostd_t) |
289
|
|
|
npt.assert_equal(np.round( |
290
|
|
|
np.exp(geodata.geomean[ |
291
|
|
|
geodata.geomean.mask == False].data), 1), geo_t) |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
def test_random_walk(): |
295
|
|
|
xi = np.array([0., 1., 2., 2., 1.]) |
296
|
|
|
yi = np.array([0., 0., 0., 1., 1.]) |
297
|
|
|
x, y = msd.random_walk(nsteps=5) |
298
|
|
|
npt.assert_equal(xi, x) |
299
|
|
|
npt.assert_equal(yi, y) |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
def test_random_traj_dataset(): |
303
|
|
|
di = {'Frame': [float(i) for i in[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]], |
304
|
|
|
'Track_ID': [float(i) for i in[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]], |
305
|
|
|
'X': np.array([1., 1.93045975532, 1.0, 1.0, 1.0, 0.0, 0.288183500979, 0.576367001958, |
306
|
|
|
0.864550502937, 0.864550502937]), |
307
|
|
|
'Y': np.array([1., 1., 1., 0.06954024468115816, 1.0, 4.0, 4.0, 4.0, 4.0, 4.288183500978857 |
308
|
|
|
]), |
309
|
|
|
'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], |
310
|
|
|
'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], |
311
|
|
|
'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]} |
312
|
|
|
cols = ['Frame', 'Track_ID', 'X', 'Y'] |
313
|
|
|
dfi = pd.DataFrame(data=di)[cols] |
314
|
|
|
|
315
|
|
|
pdt.assert_frame_equal(dfi, msd.random_traj_dataset(nframes=5, nparts=2, |
316
|
|
|
fsize=(0, 5))[cols]) |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
def test_plot_all_experiments(): |
320
|
|
|
print('To do later.') |
321
|
|
|
|