-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_evaluate.py
More file actions
228 lines (204 loc) · 6.47 KB
/
test_evaluate.py
File metadata and controls
228 lines (204 loc) · 6.47 KB
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
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/DeepLabCut
#
# Please see AUTHORS for contributors.
# https://github.com/DeepLabCut/DeepLabCut/blob/master/AUTHORS
#
# Licensed under GNU Lesser General Public License v3.0
#
import numpy as np
import pandas as pd
import pytest
import deeplabcut.pose_estimation_tensorflow as pet
from deeplabcut.pose_estimation_tensorflow.core.evaluate import (
get_available_requested_snapshots,
get_snapshots_by_index,
)
def make_single_animal_rmse_df(
bodyparts,
train_indices,
test_indices,
error_data=None,
) -> pd.DataFrame:
if error_data is None:
error_data = np.ones((len(train_indices) + len(test_indices), len(bodyparts)))
return pd.DataFrame(error_data, columns=bodyparts)
def make_multi_animal_rmse_df(
scorer,
individuals,
bodyparts,
train_indices,
test_indices,
error_data=None,
) -> pd.DataFrame:
columns = pd.MultiIndex.from_product(
[[scorer], individuals, bodyparts],
names=["scorer", "individuals", "bodyparts"],
)
if error_data is None:
error_data = np.ones((len(train_indices) + len(test_indices), len(individuals) * len(bodyparts)))
return pd.DataFrame(error_data, columns=columns)
KEYPOINT_ERROR_NAMES = [
"Train error (px)",
"Test error (px)",
"Train error (px) with p-cutoff",
"Test error (px) with p-cutoff",
]
KEYPOINT_ERROR_TEST_DATA = [
(
{
"df_error": make_single_animal_rmse_df(
bodyparts=["leg", "arm", "head"],
train_indices=[0, 1, 3],
test_indices=[2, 4],
),
"train_indices": [0, 1, 3],
"test_indices": [2, 4],
},
{
"leg": [1.0, 1.0], # train, test
"arm": [1.0, 1.0], # train, test
"head": [1.0, 1.0], # train, test
},
),
(
{
"df_error": make_single_animal_rmse_df(
bodyparts=["leftHand", "rightHand"],
train_indices=[0, 2],
test_indices=[1, 3],
error_data=[
[1.0, np.nan],
[1.0, 0.0],
[0.0, 10.0],
[5.0, 5.0],
],
),
"train_indices": [0, 2],
"test_indices": [1, 3],
},
{
"leftHand": [0.5, 3.0], # train, test
"rightHand": [10.0, 2.5], # train, test
},
),
(
{
"df_error": make_single_animal_rmse_df(
bodyparts=["leg", "arm", "head"],
train_indices=[0, 1, 3],
test_indices=[2, 4],
),
"train_indices": [0, 1, 3],
"test_indices": [2, 4],
},
{
"leg": [1.0, 1.0], # train, test
"arm": [1.0, 1.0], # train, test
"head": [1.0, 1.0], # train, test
},
),
(
{
"df_error": make_multi_animal_rmse_df(
scorer="john",
individuals=["individual_1", "individual_2"],
bodyparts=["leftArm", "rightArm"],
train_indices=[0, 1, 3],
test_indices=[2],
error_data=[
# individual_1, individual2
# leftArm, rightArm, leftArm, rightArm
[1.0, np.nan, 1.0, 2.0],
[2.0, 0.0, 1.0, np.nan],
[3.0, 10.0, 1.0, np.nan],
[10.0, 4.0, np.nan, np.nan],
],
),
"train_indices": [0, 1, 3],
"test_indices": [2],
},
{
"leftArm": [3.0, 2.0], # train, test
"rightArm": [2.0, 10.0], # train, test
},
),
]
@pytest.mark.parametrize("inputs, expected_values", KEYPOINT_ERROR_TEST_DATA)
def test_evaluate_keypoint_error(inputs, expected_values):
keypoint_error = pet.keypoint_error(
inputs["df_error"],
inputs["df_error"],
inputs["train_indices"],
inputs["test_indices"],
)
print(inputs["df_error"])
print(keypoint_error)
for bodypart, mean_errors in expected_values.items():
for error_name in KEYPOINT_ERROR_NAMES:
if "train" in error_name.lower():
mean_error = mean_errors[0]
else:
mean_error = mean_errors[1]
assert keypoint_error.loc[error_name, bodypart] == mean_error
def test_get_available_requested_snapshots_ok():
"""Test that the correct snapshots are returned."""
available = ["snapshot-1", "snapshot-2"]
requested = ["snapshot-2", "snapshot-3"]
snapshots = get_available_requested_snapshots(
requested_snapshots=requested,
available_snapshots=available,
)
assert snapshots == ["snapshot-2"]
def test_get_available_requested_snapshots_error():
"""Test that a ValueError is raised when requested snapshots are not available."""
with pytest.raises(ValueError):
get_available_requested_snapshots(
requested_snapshots=["snapshot-2"],
available_snapshots=["snapshot-1", "snapshot-3"],
)
def test_get_snapshots_by_index_int_ok():
"""Test that the correct snapshots are returned."""
available = ["snapshot-1", "snapshot-2", "snapshot-3"]
# positive int
snapshots = get_snapshots_by_index(
idx=2,
available_snapshots=available,
)
assert snapshots == ["snapshot-3"]
# negative int
snapshots = get_snapshots_by_index(
idx=-2,
available_snapshots=available,
)
assert snapshots == ["snapshot-2"]
# all snapshots
snapshots = get_snapshots_by_index(
idx="all",
available_snapshots=available,
)
assert snapshots == ["snapshot-1", "snapshot-2", "snapshot-3"]
def test_get_snapshots_by_index_error():
"""Test that a ValueError is raised when the index is out of range or invalid
str."""
available = ["snapshot-1", "snapshot-2", "snapshot-3"]
# positive int
with pytest.raises(IndexError):
get_snapshots_by_index(
idx=5,
available_snapshots=available,
)
# negative int
with pytest.raises(IndexError):
get_snapshots_by_index(
idx=-4,
available_snapshots=available,
)
# invalid str
with pytest.raises(IndexError):
get_snapshots_by_index(
idx="1",
available_snapshots=available,
)