|
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
|
|
|
d = {'col1': [1, 2, 3, 4, 5]} |
|
13
|
|
|
df = pd.DataFrame(data=d) |
|
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
|
|
|
d = {'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=d) |
|
36
|
|
|
new_track = msd.msd_calc(df, 5) |
|
37
|
|
|
|
|
38
|
|
|
npt.assert_equal(np.array([0, 2, 8, 18, 32]).astype('float64'), new_track['MSDs']) |
|
39
|
|
|
npt.assert_equal(np.array([0, 0.25, 0.25, 0.25, 0.25]).astype('float64'), new_track['Gauss']) |
|
40
|
|
|
|
|
41
|
|
|
d = {'Frame': [1, 2, 3, 4, 5], |
|
42
|
|
|
'X': [5, 6, 7, 8, 9], |
|
43
|
|
|
'Y': [6, 7, 8, 9, 10]} |
|
44
|
|
|
df = pd.DataFrame(data=d) |
|
45
|
|
|
new_track = msd.msd_calc(df) |
|
46
|
|
|
|
|
47
|
|
|
npt.assert_equal(np.array([0, 2, 8, 18, 32, np.nan, np.nan, np.nan, np.nan, |
|
48
|
|
|
np.nan]).astype('float64'), new_track['MSDs']) |
|
49
|
|
|
npt.assert_equal(np.array([0, 0.25, 0.25, 0.25, 0.25, np.nan, np.nan, np.nan, |
|
50
|
|
|
np.nan, np.nan]).astype('float64'), new_track['Gauss']) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_all_msds(): |
|
55
|
|
|
|
|
56
|
|
|
d = {'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], |
|
57
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
|
58
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
|
59
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6]} |
|
60
|
|
|
df = pd.DataFrame(data=d) |
|
61
|
|
|
|
|
62
|
|
|
di = {'Frame': [float(i) for i in[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]], |
|
63
|
|
|
'Track_ID': [float(i) for i in[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]], |
|
64
|
|
|
'X': [float(i) for i in[5, 6, 7, 8, 9, 1, 2, 3, 4, 5]], |
|
65
|
|
|
'Y': [float(i) for i in[6, 7, 8, 9, 10, 2, 3, 4, 5, 6]], |
|
66
|
|
|
'MSDs': [float(i) for i in[0, 2, 8, 18, 32, 0, 2, 8, 18, 32]], |
|
67
|
|
|
'Gauss': [0, 0.25, 0.25, 0.25, 0.25, 0, 0.25, 0.25, 0.25, 0.25]} |
|
68
|
|
|
cols = ['Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss'] |
|
69
|
|
|
|
|
70
|
|
|
dfi = pd.DataFrame(data=di)[cols] |
|
71
|
|
|
|
|
72
|
|
|
pdt.assert_frame_equal(dfi, msd.all_msds(df)[cols]) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def test_make_xyarray(): |
|
76
|
|
|
|
|
77
|
|
|
d = {'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4], |
|
78
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
|
79
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
|
80
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6]} |
|
81
|
|
|
df = pd.DataFrame(data=d) |
|
82
|
|
|
|
|
83
|
|
|
length = max(df['Frame']) + 1 |
|
84
|
|
|
f_array, t_array, x_array, y_array = msd.make_xyarray(df, length=length) |
|
85
|
|
|
|
|
86
|
|
|
tt_array = np.array([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]).astype(float) |
|
87
|
|
|
ft_array = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]).astype(float) |
|
88
|
|
|
xt_array = np.array([[5, 1], [6, 2], [7, 3], [8, 4], [9, 5]]).astype(float) |
|
89
|
|
|
yt_array = np.array([[6, 2], [7, 3], [8, 4], [9, 5], [10, 6]]).astype(float) |
|
90
|
|
|
|
|
91
|
|
|
npt.assert_equal(t_array, tt_array) |
|
92
|
|
|
npt.assert_equal(f_array, ft_array) |
|
93
|
|
|
npt.assert_equal(x_array, xt_array) |
|
94
|
|
|
npt.assert_equal(y_array, yt_array) |
|
95
|
|
|
|
|
96
|
|
|
#Second test |
|
97
|
|
|
d = {'Frame': [0, 1, 2, 3, 4, 2, 3, 4, 5, 6], |
|
98
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
|
99
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
|
100
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6]} |
|
101
|
|
|
df = pd.DataFrame(data=d) |
|
102
|
|
|
|
|
103
|
|
|
length = max(df['Frame']) + 1 |
|
104
|
|
|
f_array, t_array, x_array, y_array = msd.make_xyarray(df, length=length) |
|
105
|
|
|
|
|
106
|
|
|
tt_array = np.array([[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]).astype(float) |
|
107
|
|
|
ft_array = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]]).astype(float) |
|
108
|
|
|
xt_array = np.array([[5, np.nan], [6, np.nan], [7, 1], [8, 2], [9, 3], [np.nan, 4], [np.nan, 5]]).astype(float) |
|
109
|
|
|
yt_array = np.array([[6, np.nan], [7, np.nan], [8, 2], [9, 3], [10, 4], [np.nan, 5], [np.nan, 6]]).astype(float) |
|
110
|
|
|
|
|
111
|
|
|
npt.assert_equal(t_array, tt_array) |
|
112
|
|
|
npt.assert_equal(f_array, ft_array) |
|
113
|
|
|
npt.assert_equal(x_array, xt_array) |
|
114
|
|
|
npt.assert_equal(y_array, yt_array) |
|
115
|
|
|
|
|
116
|
|
|
def test_all_msds2(): |
|
117
|
|
|
|
|
118
|
|
|
d = {'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4], |
|
119
|
|
|
'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], |
|
120
|
|
|
'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5], |
|
121
|
|
|
'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6]} |
|
122
|
|
|
df = pd.DataFrame(data=d) |
|
123
|
|
|
|
|
124
|
|
|
di = {'Frame': [float(i) for i in[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]], |
|
125
|
|
|
'Track_ID': [float(i) for i in[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]], |
|
126
|
|
|
'X': [float(i) for i in[5, 6, 7, 8, 9, 1, 2, 3, 4, 5]], |
|
127
|
|
|
'Y': [float(i) for i in[6, 7, 8, 9, 10, 2, 3, 4, 5, 6]], |
|
128
|
|
|
'MSDs': [float(i) for i in[0, 2, 8, 18, 32, 0, 2, 8, 18, 32]], |
|
129
|
|
|
'Gauss': [0, 0.25, 0.25, 0.25, 0.25, 0, 0.25, 0.25, 0.25, 0.25]} |
|
130
|
|
|
cols = ['Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss'] |
|
131
|
|
|
|
|
132
|
|
|
dfi = pd.DataFrame(data=di)[cols] |
|
133
|
|
|
|
|
134
|
|
|
length = max(df['Frame']) + 1 |
|
135
|
|
|
pdt.assert_frame_equal(dfi, msd.all_msds2(df, frames=length)[cols]) |