-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_binary.py
175 lines (155 loc) · 6.16 KB
/
test_binary.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
#!/usr/bin/python
import logging
import unittest
import numpy as np
import pandas as pd
import binary as b
import common
from parameter import *
"""
------------------------------------------------------------
Unit tests for the binary.py module
------------------------------------------------------------
# Run these tests using `python -m unittest test_binary`
------------------------------------------------------------
"""
input_data1 = {
common.INPUT_COL0_TS: [1, 2, 3, 4, 5, 10, 20, 30, 100, 200, 300, 310],
common.INPUT_COL1_MI: [1, 2, 3, 4, 5, 6, 7.1, 8.2, 9.3, 10, 11, 20],
common.INPUT_COL2_FREEZE: [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0],
}
output_data1 = {
b.OUTPUT_COL0_TS: [1.0, 3.0, 4.0, 10.0, 20.0, 300.0],
b.OUTPUT_COL1_MI: [1, 3, 4, 6, 7.1, 11],
b.OUTPUT_COL2_MI_AVG: [1.5, 1.5, 4.5, 4.5, 8.65, 8.65],
b.OUTPUT_COL3_FREEZE_TP: [
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
],
}
output_data1_min_t_1_before = {
b.OUTPUT_COL0_TS: [1.0, 3.0, 4.0, 10.0, 20.0],
b.OUTPUT_COL1_MI: [1, 3, 4, 6, 7.1],
b.OUTPUT_COL2_MI_AVG: [1.5, 1.5, 4.5, 4.5, 8.65],
b.OUTPUT_COL3_FREEZE_TP: [
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
b.ONE_TO_ZERO,
],
}
output_data1_min_t_4_after = {
b.OUTPUT_COL0_TS: [4.0, 10.0, 20.0],
b.OUTPUT_COL1_MI: [4, 6, 7.1],
b.OUTPUT_COL2_MI_AVG: [4.5, 4.5, 8.65],
b.OUTPUT_COL3_FREEZE_TP: [b.ONE_TO_ZERO, b.ZERO_TO_ONE, b.ONE_TO_ZERO],
}
output_data1_min_t_5_before = {
b.OUTPUT_COL0_TS: [10.0, 20.0],
b.OUTPUT_COL1_MI: [6, 7.1],
b.OUTPUT_COL2_MI_AVG: [4.5, 8.65],
b.OUTPUT_COL3_FREEZE_TP: [b.ZERO_TO_ONE, b.ONE_TO_ZERO],
}
test_p1 = {
Parameters.PARAM_TIME_WINDOW_START_LIST: [1, 100.0, 300.0],
Parameters.PARAM_TIME_WINDOW_DURATION: [10, np.nan, np.nan],
}
out_p1 = {
b.OUTPUT_COL0_TS: [1.0, 3.0, 4.0, 10.0, 300.0],
b.OUTPUT_COL1_MI: [1.0, 3.0, 4.0, 6.0, 11.0],
b.OUTPUT_COL2_MI_AVG: [1.5, 1.5, 4.5, 4.5, 8.65],
b.OUTPUT_COL3_FREEZE_TP: [
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
b.ONE_TO_ZERO,
b.ZERO_TO_ONE,
b.ZERO_TO_ONE,
],
}
out_p1_nop = {
b.OUTPUT_COL0_TS: [20.0],
b.OUTPUT_COL1_MI: [7.1],
b.OUTPUT_COL2_MI_AVG: [8.65],
b.OUTPUT_COL3_FREEZE_TP: [b.ONE_TO_ZERO],
}
class TestBinary(common.CommonTetsMethods, unittest.TestCase):
def setUp(self):
self.input_df1 = pd.DataFrame(input_data1)
self.p1_df = pd.DataFrame(test_p1)
logging.basicConfig(filename=b.OUTPUT_LOG_FILE,
level=logging.DEBUG, format="")
common.logger = logging.getLogger(__name__)
if not common.logger.handlers:
common.logger.addHandler(common.loghandler())
def validate_df(self, df, expected_data):
expected_df = pd.DataFrame(expected_data)
self.assertTrue(expected_df.equals(df.astype(expected_df.dtypes)))
def validate_min_t(self, min_t_before, min_t_after, df, exp_out):
out_df = b.apply_min_time_duration_criteria(
min_t_before, min_t_after, df)
# print(out_df)
out_df.reset_index(drop=True, inplace=True)
self.validate_df(out_df, exp_out)
def test_process_input_df(self):
exp_out_df = pd.DataFrame(output_data1)
result, out_df = b.process_input_df(self.input_df1)
self.assertTrue(result)
self.assertTrue(exp_out_df.equals(out_df.astype(exp_out_df.dtypes)))
self.validate_min_t(1, 0, out_df, output_data1_min_t_1_before)
self.validate_min_t(5, 0, out_df, output_data1_min_t_5_before)
self.validate_min_t(0, 4, out_df, output_data1_min_t_4_after)
def test_param_processing(self):
parameter_obj = Parameters()
param_name = "test_p1"
parameter_obj.set_param_value(param_name, self.p1_df)
out_df = b.process_input_df(self.input_df1)[1]
nop_df = out_df[:]
temp_out_df, nop_df = b.process_param(
parameter_obj, param_name, out_df, nop_df)
temp_out_df.reset_index(drop=True, inplace=True)
self.validate_df(temp_out_df, out_p1)
self.validate_df(nop_df, out_p1_nop)
def test_real_data(self):
input_dir = os.path.join(os.getcwd(), "test_data", "binary")
output_dir_base = os.path.join(
os.getcwd(), "test_data", "binary_output")
expected_output_dir_base = os.path.join(
os.getcwd(), "test_data", "binary_output_expected"
)
successfully_parsed_files, unsuccessfully_parsed_files = b.main(
input_dir, False, None
)
self.assertEqual(len(unsuccessfully_parsed_files), 0)
success_normalized_path = []
for file in successfully_parsed_files:
success_normalized_path.append(os.path.normpath(file))
num_files_compared = 0
csv_path = glob.glob(os.path.join(input_dir, "*.csv"))
# Validate that each of the input files were successfully parsed and matches expected
for input_csv_file in csv_path:
self.assertTrue((input_csv_file in success_normalized_path))
file_name_without_ext, ext = os.path.splitext(
os.path.basename(input_csv_file)
)
expected_output_dir = os.path.join(
expected_output_dir_base, file_name_without_ext
)
common.logger.info("Expected output dir: %s", expected_output_dir)
output_dir = os.path.join(output_dir_base, file_name_without_ext)
common.logger.info("Output dir: %s", output_dir)
csv_path = glob.glob(os.path.join(expected_output_dir, "*.csv"))
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)