-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dff.py
446 lines (423 loc) · 17.8 KB
/
test_dff.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import glob
import logging
import os
import unittest
from pathlib import Path
import numpy as np
import pandas as pd
import common
import dff
from parameter import *
from concurrent.futures import ProcessPoolExecutor
import concurrent
import sys
"""
------------------------------------------------------------
Unit tests for the dff module
------------------------------------------------------------
# Run these tests using `python -m unittest test_dff`
# Or run a specific test using something like:
# python -m unittest test_dff.DffTest.test_bvt
------------------------------------------------------------
"""
class DffTest(common.CommonTetsMethods, unittest.TestCase):
def setUp(self):
logging.basicConfig(filename=dff.OUTPUT_LOG_FILE,
level=logging.INFO, format="")
common.logger = logging.getLogger(__name__)
if not common.logger.handlers:
common.logger.addHandler(common.loghandler())
def test_bvt(self):
# Whole duration parameter
param = Parameters()
in_col_names = {
common.INPUT_COL0_TS: 'float64',
common.INPUT_COL1_MI: 'float64',
common.INPUT_COL2_FREEZE: 'int',
}
ts = [*range(0, 40, 1)]
fz = [0] * 10 + [1] * 5 + [0] * 5 + [1] * 3 + \
[0] * 3 + [1, 0, 1, 0] + [1] * 5 + [0] * 5
mi = [1] * 40
list_of_tuples = list(zip(ts, mi, fz))
binary_df = pd.DataFrame(list_of_tuples, columns=in_col_names.keys()).astype(
in_col_names)
data = [*range(0, 40, 1)]
timeshift_val = 0
success, results = dff.process(
param, "", binary_df, timeshift_val, None, data, ts)
self.assertTrue(success)
auc_0s_sum = results[0]
auc_0s_cnt = results[1]
out_df_0s = results[2]
auc_0s_sum_not = results[3]
auc_0s_cnt_not = results[4]
out_df_0s_not = results[5]
auc_1s_sum = results[6]
auc_1s_cnt = results[7]
out_df_1s = results[8]
auc_1s_sum_not = results[9]
auc_1s_cnt_not = results[10]
out_df_1s_not = results[11]
self.assertEqual(auc_0s_cnt, fz.count(0))
self.assertEqual(auc_0s_sum, 443)
self.assertEqual(auc_0s_sum_not, 0)
self.assertEqual(auc_0s_cnt_not, 0)
self.assertTrue(out_df_0s_not.empty)
self.assertEqual(auc_1s_sum, 337)
self.assertEqual(auc_1s_cnt, fz.count(1))
self.assertEqual(auc_1s_sum_not, 0)
self.assertEqual(auc_1s_cnt_not, 0)
self.assertTrue(out_df_1s_not.empty)
auc_0s_avg = dff.compute_avg(auc_0s_sum, auc_0s_cnt)
auc_1s_avg = dff.compute_avg(auc_1s_sum, auc_1s_cnt)
self.assertEqual(round(auc_0s_avg, 3), 17.72)
self.assertEqual(round(auc_1s_avg, 3), 22.467)
o_data = {
dff.OUTPUT_COL0_TS: [0.0, 15.0, 23.0, 27.0, 29.0, 35.0],
dff.OUTPUT_COL1_LEN: [9.0, 4.0, 2.0, 0.0, 0.0, 4.0],
dff.OUTPUT_COL2_MI_AVG: [1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
dff.OUTPUT_COL3_DATA_AUC: [45.0, 85.0, 72.0, 27.0, 29.0, 185.0],
dff.OUTPUT_COL4_DATA_AVG: [4.5, 17.0, 24.0, 27.0, 29.0, 37.0],
}
out_df_0s_expected = pd.DataFrame(o_data)
self.assertTrue(out_df_0s_expected.equals(out_df_0s))
o_data = {
dff.OUTPUT_COL0_TS: [10.0, 20.0, 26.0, 28.0, 30.0],
dff.OUTPUT_COL1_LEN: [4.0, 2.0, 0.0, 0.0, 4.0],
dff.OUTPUT_COL2_MI_AVG: [1.0, 1.0, 1.0, 1.0, 1.0],
dff.OUTPUT_COL3_DATA_AUC: [60.0, 63.0, 26.0, 28.0, 160.0],
dff.OUTPUT_COL4_DATA_AVG: [12.0, 21.0, 26.0, 28.0, 32.0],
}
out_df_1s_expected = pd.DataFrame(o_data)
self.assertTrue(out_df_1s_expected.equals(out_df_1s))
# Parameter that encompasses the whole duration
param_val = {
Parameters.PARAM_TIME_WINDOW_START_LIST: [0],
Parameters.PARAM_TIME_WINDOW_DURATION: [40],
}
param = Parameters()
PARAM_NAME = "param1"
df = pd.DataFrame(param_val)
param.set_param_value(PARAM_NAME, df)
success, results = dff.process(
param, PARAM_NAME, binary_df, timeshift_val, None, data, ts
)
self.assertTrue(success)
auc_0s_sum = results[0]
auc_0s_cnt = results[1]
out_df_0s = results[2]
auc_0s_sum_not = results[3]
auc_0s_cnt_not = results[4]
out_df_0s_not = results[5]
auc_1s_sum = results[6]
auc_1s_cnt = results[7]
out_df_1s = results[8]
auc_1s_sum_not = results[9]
auc_1s_cnt_not = results[10]
out_df_1s_not = results[11]
self.assertEqual(auc_0s_cnt, fz.count(0))
self.assertEqual(auc_0s_sum, 443)
self.assertEqual(auc_0s_sum_not, 0)
self.assertEqual(auc_0s_cnt_not, 0)
self.assertTrue(out_df_0s_not.empty)
self.assertEqual(auc_1s_sum, 337)
self.assertEqual(auc_1s_cnt, fz.count(1))
self.assertEqual(auc_1s_sum_not, 0)
self.assertEqual(auc_1s_cnt_not, 0)
self.assertTrue(out_df_1s_not.empty)
self.assertTrue(out_df_0s_expected.equals(out_df_0s))
self.assertTrue(out_df_1s_expected.equals(out_df_1s))
self.assertEqual(round(auc_0s_avg, 3), 17.72)
self.assertEqual(round(auc_1s_avg, 3), 22.467)
# Restricted parameter
param_val = {
Parameters.PARAM_TIME_WINDOW_START_LIST: [0, 10, 20, 30, 40],
Parameters.PARAM_TIME_WINDOW_DURATION: [5, np.nan, np.nan, np.nan, np.nan],
}
param = Parameters()
PARAM_NAME = "param1"
df = pd.DataFrame(param_val)
param.set_param_value(PARAM_NAME, df)
success, results = dff.process(
param, PARAM_NAME, binary_df, timeshift_val, None, data, ts
)
self.assertTrue(success)
auc_0s_sum = results[0]
auc_0s_cnt = results[1]
out_df_0s = results[2]
auc_0s_sum_not = results[3]
auc_0s_cnt_not = results[4]
out_df_0s_not = results[5]
auc_1s_sum = results[6]
auc_1s_cnt = results[7]
out_df_1s = results[8]
auc_1s_sum_not = results[9]
auc_1s_cnt_not = results[10]
out_df_1s_not = results[11]
self.assertEqual(auc_0s_cnt, 9)
self.assertEqual(auc_0s_sum, 87)
# The _Not's should be the rest
self.assertEqual(auc_0s_cnt + auc_0s_cnt_not, fz.count(0))
self.assertEqual(auc_0s_sum_not, 356)
self.assertEqual(auc_1s_sum, 283)
self.assertEqual(auc_1s_cnt, 13)
self.assertEqual(auc_1s_cnt + auc_1s_cnt_not, fz.count(1))
self.assertEqual(auc_1s_sum_not, 54)
o_data = {
dff.OUTPUT_COL0_TS: [0.0, 23.0],
dff.OUTPUT_COL1_LEN: [5.0, 2.0],
dff.OUTPUT_COL2_MI_AVG: [1.0, 1.0],
dff.OUTPUT_COL3_DATA_AUC: [15.0, 72.0],
dff.OUTPUT_COL4_DATA_AVG: [2.5, 24.0],
}
out_df_0s_expected = pd.DataFrame(o_data)
self.assertTrue(out_df_0s_expected.equals(out_df_0s))
o_data = {
dff.OUTPUT_COL0_TS: [6.0, 15.0, 27.0, 29.0, 35.0],
dff.OUTPUT_COL1_LEN: [3.0, 4.0, 0.0, 0.0, 4.0],
dff.OUTPUT_COL2_MI_AVG: [1.0, 1.0, 1.0, 1.0, 1.0],
dff.OUTPUT_COL3_DATA_AUC: [30.0, 85.0, 27.0, 29.0, 185.0],
dff.OUTPUT_COL4_DATA_AVG: [7.5, 17.0, 27.0, 29.0, 37.0],
}
out_df_0s_not_expected = pd.DataFrame(o_data)
self.assertTrue(out_df_0s_not_expected.equals(out_df_0s_not))
o_data = {
dff.OUTPUT_COL0_TS: [10.0, 20.0, 30.0],
dff.OUTPUT_COL1_LEN: [4.0, 2.0, 4.0],
dff.OUTPUT_COL2_MI_AVG: [1.0, 1.0, 1.0],
dff.OUTPUT_COL3_DATA_AUC: [60.0, 63.0, 160.0],
dff.OUTPUT_COL4_DATA_AVG: [12.0, 21.0, 32.0],
}
out_df_1s_expected = pd.DataFrame(o_data)
self.assertTrue(out_df_1s_expected.equals(out_df_1s))
o_data = {
dff.OUTPUT_COL0_TS: [26.0, 28.0],
dff.OUTPUT_COL1_LEN: [0.0, 0.0],
dff.OUTPUT_COL2_MI_AVG: [1.0, 1.0],
dff.OUTPUT_COL3_DATA_AUC: [26.0, 28.0],
dff.OUTPUT_COL4_DATA_AVG: [26.0, 28.0],
}
out_df_1s_not_expected = pd.DataFrame(o_data)
self.assertTrue(out_df_1s_not_expected.equals(out_df_1s_not))
auc_0s_avg = dff.compute_avg(auc_0s_sum, auc_0s_cnt)
auc_1s_avg = dff.compute_avg(auc_1s_sum, auc_1s_cnt)
self.assertEqual(round(auc_0s_avg, 3), 9.667)
self.assertEqual(round(auc_1s_avg, 3), 21.769)
auc_0s_avg_not = dff.compute_avg(auc_0s_sum_not, auc_0s_cnt_not)
auc_1s_avg_not = dff.compute_avg(auc_1s_sum_not, auc_1s_cnt_not)
self.assertEqual(round(auc_0s_avg_not, 3), 22.25)
self.assertEqual(round(auc_1s_avg_not, 3), 27.0)
# Test for combined paramaters
# Note: This test case uses the variable output values from the previous
# test case of restricted parameters. Do not change the values of
# those variables between these test cases.
param_val_1 = {
Parameters.PARAM_TIME_WINDOW_START_LIST: [0, 12, 33, 41],
Parameters.PARAM_TIME_WINDOW_DURATION: [2, np.nan, np.nan, np.nan],
}
PARAM_NAME_1 = "param_1"
param_val_2 = {
Parameters.PARAM_TIME_WINDOW_START_LIST: [1, 10, 20, 30],
Parameters.PARAM_TIME_WINDOW_DURATION: [3, np.nan, np.nan, np.nan],
}
PARAM_NAME_2 = "param_2"
param_val_3 = {
Parameters.PARAM_TIME_WINDOW_START_LIST: [
4,
13,
14,
23,
24,
31,
32,
40,
43,
44,
],
Parameters.PARAM_TIME_WINDOW_DURATION: [
1,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
],
}
PARAM_NAME_3 = "param_3"
param = Parameters()
df = pd.DataFrame(param_val_1)
param.set_param_value(PARAM_NAME_1, df)
df = pd.DataFrame(param_val_2)
param.set_param_value(PARAM_NAME_2, df)
df = pd.DataFrame(param_val_3)
param.set_param_value(PARAM_NAME_3, df)
expected_df = pd.DataFrame(
{
Parameters.PARAM_TIME_WINDOW_START_LIST: [0.0, 10.0, 20.0, 30.0, 40.0],
Parameters.PARAM_TIME_WINDOW_END_LIST: [5.0, 15.0, 25.0, 35.0, 45.0],
}
)
combinded_df = param.get_combined_params_ts_series(0)
self.assertTrue(combinded_df.equals(expected_df))
success, results = dff.process(
param, None, binary_df, timeshift_val, None, data, ts)
self.assertTrue(success)
auc_0s_sum = results[0]
auc_0s_cnt = results[1]
out_df_0s = results[2]
auc_0s_sum_not = results[3]
auc_0s_cnt_not = results[4]
out_df_0s_not = results[5]
auc_1s_sum = results[6]
auc_1s_cnt = results[7]
out_df_1s = results[8]
auc_1s_sum_not = results[9]
auc_1s_cnt_not = results[10]
out_df_1s_not = results[11]
self.assertEqual(auc_0s_cnt, 9)
self.assertEqual(auc_0s_sum, 87)
# The _Not's should be the rest
self.assertEqual(auc_0s_cnt + auc_0s_cnt_not, fz.count(0))
self.assertEqual(auc_0s_sum_not, 356)
self.assertEqual(auc_1s_sum, 283)
self.assertEqual(auc_1s_cnt, 13)
self.assertEqual(auc_1s_cnt + auc_1s_cnt_not, fz.count(1))
self.assertEqual(auc_1s_sum_not, 54)
self.assertTrue(out_df_0s_expected.equals(out_df_0s))
self.assertTrue(out_df_0s_not_expected.equals(out_df_0s_not))
self.assertTrue(out_df_1s_expected.equals(out_df_1s))
self.assertTrue(out_df_1s_not_expected.equals(out_df_1s_not))
def test_real_data(self):
parent_dir = os.path.join(os.getcwd(), "test_data")
input_dirs_to_test = glob.glob(
os.path.join(parent_dir, "dff_test_*"))
inputs = []
for d in input_dirs_to_test:
# Skip output dirs
if "_output" in d:
continue
input_dir = os.path.join(parent_dir, d)
csv_path = glob.glob(os.path.join(input_dir, "*.csv"))
for f in csv_path:
f = os.path.basename(f)
inputs.append([input_dir, f])
futures = []
futures_dict = {}
with ProcessPoolExecutor(initializer=initialize) as executor:
for input in inputs:
input_dir = input[0]
filename = input[1]
f = executor.submit(self.process, input_dir, filename)
common.logger.info('Submitted input dir: %s, file: %s', input_dir, filename)
futures.append(f)
futures_dict[f] = input_dir + "/" + filename
for f in concurrent.futures.as_completed(futures):
common.logger.info('Finished processing: %s', futures_dict[f])
for input in inputs:
input_dir = input[0]
filename = input[1]
self.validate_real_data_for_input_dir(input_dir, filename)
@staticmethod
def process(input_dir: str, dff_filename: str):
parameter_obj = Parameters()
try:
parameter_obj.parse(input_dir)
except ValueError as e:
common.logger.warning(e)
dff.main(input_dir, parameter_obj)
# Calls dff module's `main` routine and then validates that the output folder
# matches the expected output folder.
def validate_real_data_for_input_dir(self, input_dir: str, dff_filename: str):
parameter_obj = Parameters()
try:
parameter_obj.parse(input_dir)
except ValueError as e:
common.logger.warning(e)
#dff.main(input_dir, parameter_obj)
dir_for_file = os.path.splitext(dff_filename)[0]
expected_output_dir = os.path.join(
input_dir + "_output_expected", dir_for_file)
output_dir = os.path.join(input_dir + "_output", dir_for_file)
csv_path = glob.glob(os.path.join(expected_output_dir, "*.csv"))
num_files_compared = 0
# Validate that the files in the output dir match the expected output dir
for expected_csv_file in csv_path:
file_name = os.path.basename(expected_csv_file)
output_csv_file = os.path.join(output_dir, file_name)
super().compare_csv_files(expected_csv_file, output_csv_file)
num_files_compared += 1
# Make sure that at least 1 file was compared
common.logger.info(
"Number of files successfully compared: %d", num_files_compared
)
self.assertGreater(num_files_compared, 0)
def test_generate_data_file(self):
input_dir = os.path.join(os.getcwd(), "test_data", "dff_test_1")
output_dir = os.path.join(
os.getcwd(), "test_data", "dff_test_1_output")
path = Path(output_dir)
path.mkdir(parents=True, exist_ok=True)
ts_file = os.path.join(input_dir, "timeCorrection_BLA.hdf5")
dff_file = os.path.join(input_dir, "dff_BLA.hdf5")
csv_file = os.path.join(input_dir, "0111_PV_c4m1- Index.csv")
timeshift_val, num_rows_processed = common.get_timeshift_from_input_file(
csv_file
)
success, binary_df = common.parse_input_file_into_df(
csv_file, common.NUM_INITIAL_ROWS_TO_SKIP + num_rows_processed
)
self.assertTrue(success)
timeshift_val = round(timeshift_val, 2)
ts = dff.read_hdf5("", ts_file, "timestampNew")
binary_df[common.INPUT_COL0_TS] = (
binary_df[common.INPUT_COL0_TS] + timeshift_val
)
z_score = dff.read_hdf5("", dff_file, "data")
index = 0
ts_index = 0
z_score_avg_list = []
z_score_count_list = []
while index < len(binary_df.index) - 1 and ts_index < len(ts):
ts_start = binary_df.iloc[index][common.INPUT_COL0_TS]
index += 1
ts_end = binary_df.iloc[index][common.INPUT_COL0_TS]
# print(ts_start, "<->", ts_end)
if ts[len(ts) - 1] < ts_start:
# print("Reached the end of the ts file, breaking")
break
while ts[ts_index] < ts_start:
ts_index += 1
z_score_sum = 0
z_score_cnt = 0
# print(ts_index)
while ts_index < len(ts) and ts[ts_index] < ts_end:
z_score_sum += z_score[ts_index]
ts_index += 1
z_score_cnt += 1
z_score_avg_list.append(z_score_sum)
z_score_count_list.append(z_score_cnt)
z_score_avg_list.append(0)
z_score_count_list.append(0)
# df = pd.DataFrame({"timestamp": timestamp, "dff": z_score})
binary_df.insert(3, "dff", z_score_avg_list, True)
binary_df.insert(4, "dff_count", z_score_count_list, True)
zero_binary_df = binary_df[binary_df[common.INPUT_COL2_FREEZE] == 0]
output_file = os.path.join(output_dir, "0_data.csv")
zero_binary_df.to_csv(output_file, index=False, header=True)
one_binary_df = binary_df[binary_df[common.INPUT_COL2_FREEZE] == 1]
output_file = os.path.join(output_dir, "1_data.csv")
one_binary_df.to_csv(output_file, index=False, header=True)
output_file = os.path.join(output_dir, "data.csv")
binary_df.to_csv(output_file, index=False, header=True)
def initialize():
logging.basicConfig(filename=dff.OUTPUT_LOG_FILE,
level=logging.INFO, format="")
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.INFO)
common.logger = logging.getLogger(__name__)
common.logger.addHandler(stdout_handler)