-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtest_util.py
321 lines (241 loc) · 10.4 KB
/
test_util.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import os
import tempfile
import warnings
import pandas as pd
import pytest
from pycytominer.cyto_utils.util import (
check_aggregate_operation,
check_compartments,
check_consensus_operation,
check_correlation_method,
check_fields_of_view,
check_fields_of_view_format,
check_image_features,
extract_image_features,
get_default_compartments,
get_pairwise_correlation,
load_known_metadata_dictionary,
)
tmpdir = tempfile.gettempdir()
def test_check_compartments():
valid = ["cells"]
assert check_compartments(valid) is None
valid = ["CeLLs"]
assert check_compartments(valid) is None
valid = "cells"
assert check_compartments(valid) is None
valid = "CeLLs"
assert check_compartments(valid) is None
valid = ["cells", "nuclei", "cytoplasm"]
assert check_compartments(valid) is None
valid = ["CeLLs", "nucLEI", "CYTOplasm"]
assert check_compartments(valid) is None
def test_check_compartments_not_valid():
warnings.simplefilter("always")
with warnings.catch_warnings(record=True) as w:
not_valid = ["SOMETHING"]
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert "Non-canonical compartment detected: SOMETHING" in str(w[-1].message)
with warnings.catch_warnings(record=True) as w:
not_valid = "SOMETHING" # Also works with strings
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert "Non-canonical compartment detected: SOMETHING" in str(w[-1].message)
with warnings.catch_warnings(record=True) as w:
not_valid = ["CelLs", "CytopLasM", "SOMETHING"]
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert "Non-canonical compartment detected: CelLs, CytopLasM, SOMETHING" in str(
w[-1].message
)
with warnings.catch_warnings(record=True) as w:
not_valid = ["CelLs", "CytopLasM", "SOMETHING", "NOTHING"]
check_compartments(not_valid)
assert issubclass(w[-1].category, UserWarning)
assert (
"Non-canonical compartment detected: CelLs, CytopLasM, SOMETHING, NOTHING"
in str(w[-1].message)
)
def test_get_default_compartments():
default_comparments = get_default_compartments()
assert default_comparments == ["cells", "cytoplasm", "nuclei"]
def test_load_known_metadata_dictionary():
meta_cols = ["ObjectNumber", "ImageNumber", "TableNumber"]
meta_df = pd.DataFrame({
"compartment": ["cells"] * 3 + ["nuclei"] * 3 + ["cytoplasm"] * 3,
"feature": meta_cols * 3,
})
metadata_file = os.path.join(tmpdir, "metadata_temp.txt")
meta_df.to_csv(metadata_file, sep="\t", index=False)
result = load_known_metadata_dictionary(metadata_file)
expected_result = {
"cells": meta_cols,
"nuclei": meta_cols,
"cytoplasm": meta_cols,
}
assert result == expected_result
def test_check_correlation_method():
method = check_correlation_method(method="PeaRSon")
expected_method = "pearson"
assert method == expected_method
with pytest.raises(AssertionError) as nomethod:
method = check_correlation_method(method="DOES NOT EXIST")
assert "not supported, select one of" in str(nomethod.value)
def test_check_aggregate_operation_method():
operation = check_aggregate_operation(operation="MEAn")
expected_op = "mean"
assert operation == expected_op
with pytest.raises(AssertionError) as nomethod:
check_aggregate_operation(operation="DOES NOT EXIST")
assert "not supported, select one of" in str(nomethod.value)
def test_check_consensus_operation_method():
for test_operation in ["MeaN", "meDIAN", "modZ"]:
operation = check_consensus_operation(operation=test_operation)
expected_op = test_operation.lower()
assert operation == expected_op
with pytest.raises(AssertionError) as nomethod:
check_consensus_operation(operation="DOES NOT EXIST")
assert "not supported, select one of" in str(nomethod.value)
def test_check_fields_of_view():
data_fields_of_view = [1, 3, 4, 5]
valid_input_fields_of_view = [1, 3, 4]
assert check_fields_of_view(data_fields_of_view, valid_input_fields_of_view) is None
valid_input_fields_of_view = [5, 4, 1]
assert check_fields_of_view(data_fields_of_view, valid_input_fields_of_view) is None
valid_input_fields_of_view = [4, 3, 1, 5]
assert check_fields_of_view(data_fields_of_view, valid_input_fields_of_view) is None
invalid_input_fields_of_view = [2, 6, 7]
with pytest.raises(ValueError) as err:
check_fields_of_view(data_fields_of_view, invalid_input_fields_of_view)
assert (
str(err)
== "Some of the input fields of view are not present in the image table."
)
def test_check_fields_of_view_format():
valid_input_fields_of_view = "all"
assert (
check_fields_of_view_format(valid_input_fields_of_view)
== valid_input_fields_of_view
)
valid_input_fields_of_view = ["1", 3, 4]
assert check_fields_of_view_format(valid_input_fields_of_view) == [1, 3, 4]
valid_input_fields_of_view = ["3", "1", "5"] # valid but not recommended
assert check_fields_of_view_format(valid_input_fields_of_view) == [3, 1, 5]
invalid_input_fields_of_view = 1
with pytest.raises(TypeError) as err:
check_fields_of_view_format(invalid_input_fields_of_view)
assert (
str(err)
== f"Variable of type list expected, however type {type(invalid_input_fields_of_view)} was passed."
)
invalid_input_fields_of_view = ["test", 2, 3]
with pytest.raises(TypeError) as err:
check_fields_of_view_format(invalid_input_fields_of_view)
assert (
str(err)
== "Variables of type int expected, however some of the input fields of view are not integers."
)
def test_check_image_features():
data_image_cols = [
"Count_Cells",
"Granularity_1_Mito",
"Texture_Variance_RNA_20_00",
"Texture_InfoMeas2_DNA_5_02",
]
valid_image_feature_groups = ["Count", "Granularity"]
assert check_image_features(valid_image_feature_groups, data_image_cols) is None
valid_image_feature_groups = ["Count", "Granularity", "Texture"]
assert check_image_features(valid_image_feature_groups, data_image_cols) is None
invalid_image_feature_groups = ["Count", "IncorrectFeatureGroup"]
with pytest.raises(ValueError) as err:
check_image_features(invalid_image_feature_groups, data_image_cols)
assert (
str(err)
== "Some of the input image features are not present in the image table."
)
def test_check_image_features_image_table():
data_image_cols = [
"Metadata_Count_Cells",
"Image_Granularity_1_Mito",
"Image_Texture_Variance_RNA_20_00",
"Image_Texture_InfoMeas2_DNA_5_02",
]
valid_image_feature_groups = ["Count", "Granularity"]
assert check_image_features(valid_image_feature_groups, data_image_cols) is None
valid_image_feature_groups = ["Count", "Granularity", "Texture"]
assert check_image_features(valid_image_feature_groups, data_image_cols) is None
invalid_image_feature_groups = ["Count", "IncorrectFeatureGroup"]
with pytest.raises(ValueError) as err:
check_image_features(invalid_image_feature_groups, data_image_cols)
assert (
str(err)
== "Some of the input image features are not present in the image table."
)
def test_extract_image_features():
image_df = pd.DataFrame({
"TableNumber": ["x_hash", "y_hash"],
"ImageNumber": ["x", "y"],
"Metadata_Plate": ["plate", "plate"],
"Metadata_Well": ["A01", "A01"],
"Count_Cells": [50, 50],
"Granularity_1_Mito": [3.0, 4.0],
"Texture_Variance_RNA_20_00": [12.0, 14.0],
"Texture_InfoMeas2_DNA_5_02": [5.0, 1.0],
"ImageQuality_XX_YY_ZZ": [10, 20],
})
image_feature_categories = ["Count", "Granularity", "ImageQuality"]
expected_result = pd.DataFrame({
"TableNumber": ["x_hash", "y_hash"],
"ImageNumber": ["x", "y"],
"Metadata_Plate": ["plate", "plate"],
"Metadata_Well": ["A01", "A01"],
"Metadata_Count_Cells": [50, 50],
"Image_Granularity_1_Mito": [3.0, 4.0],
"Image_ImageQuality_XX_YY_ZZ": [10, 20],
})
result = extract_image_features(
image_feature_categories,
image_df,
["TableNumber", "ImageNumber"],
["Metadata_Plate", "Metadata_Well"],
)
pd.testing.assert_frame_equal(
expected_result.sort_index(axis=1), result.sort_index(axis=1)
)
def _assert_pairwise_corr_helper(data_df, expected_result):
"""Assert `get_pairwise_correlation` and `pd.DataFrame.corr` get the same
output. It also checks if the first correlation value match the `expected_result`.
"""
cor_df, pair_df = get_pairwise_correlation(data_df, method="pearson")
pd.testing.assert_frame_equal(cor_df, data_df.corr(method="pearson"))
x_y_cor = pair_df.query("correlation != 0").round(1).correlation.values[0]
assert x_y_cor == expected_result
def test_get_pairwise_correlation():
data_df = pd.concat([
pd.DataFrame({"x": [1, 3, 8], "y": [5, 3, 1]}),
pd.DataFrame({"x": [1, 3, 5], "y": [8, 3, 1]}),
]).reset_index(drop=True)
expected_result = -0.8
_assert_pairwise_corr_helper(data_df, expected_result)
def test_pairwise_corr_with_nan():
data_df = pd.concat([
pd.DataFrame({"x": [1, 3, 8, 3], "y": [5, 3, 1, None]}),
pd.DataFrame({"x": [1, 3, 5, None], "y": [8, 3, 1, 3]}),
]).reset_index(drop=True)
expected_result = -0.8
_assert_pairwise_corr_helper(data_df, expected_result)
def test_pairwise_corr_with_inf():
data_df = pd.concat([
pd.DataFrame({"x": [1, 3, 8, 3], "y": [5, 3, 1, float("inf")]}),
pd.DataFrame({"x": [1, 3, 5, float("inf")], "y": [8, 3, 1, 3]}),
]).reset_index(drop=True)
expected_result = -0.8
_assert_pairwise_corr_helper(data_df, expected_result)
def test_pairwise_corr_with_inf_and_nan():
data_df = pd.concat([
pd.DataFrame({"x": [1, 3, 8, 3], "y": [5, 3, 1, None]}),
pd.DataFrame({"x": [1, 3, 5, float("inf")], "y": [8, 3, 1, 3]}),
]).reset_index(drop=True)
expected_result = -0.8
_assert_pairwise_corr_helper(data_df, expected_result)