-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathosi_general_validator.py
executable file
·234 lines (195 loc) · 6.22 KB
/
osi_general_validator.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
"""
Main class and entry point of the OSI Validator.
"""
import argparse
from tqdm import tqdm
from osi3trace.osi_trace import OSITrace
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), "."))
# Import local files
try:
import osi_rules
import osi_validator_logger
import osi_rules_checker
import linked_proto_field
except Exception as e:
print(
"Make sure you have installed the requirements with 'pip install -r requirements.txt'!"
)
print(e)
def check_positive_int(value):
ivalue = int(value)
if ivalue < 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
def command_line_arguments():
"""Define and handle command line interface"""
dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
parser = argparse.ArgumentParser(
description="Validate data defined at the input", prog="osivalidator"
)
parser.add_argument(
"--data",
help="Path to the file with OSI-serialized data.",
type=str,
required=True,
)
parser.add_argument(
"--rules",
"-r",
help="Directory with yml files containing rules. ",
default=os.path.join(dir_path, "rules"),
type=str,
)
parser.add_argument(
"--type",
"-t",
help="Name of the type used to serialize data. Default is SensorView.",
choices=["SensorView", "GroundTruth", "SensorData"],
type=str,
required=False,
)
parser.add_argument(
"--output",
"-o",
help="Output folder of the log files.",
default="output_logs",
type=str,
required=False,
)
parser.add_argument(
"--timesteps",
help="Number of timesteps to analyze. If -1, all.",
type=int,
default=-1,
required=False,
)
parser.add_argument(
"--debug", help="Set the debug mode to ON.", action="store_true"
)
parser.add_argument(
"--verbose", "-v", help="Set the verbose mode to ON.", action="store_true"
)
parser.add_argument(
"--parallel",
"-p",
help="(Ignored) Set parallel mode to ON.",
default=False,
required=False,
action="store_true",
)
parser.add_argument(
"--format",
"-f",
help="(Ignored) Set the format type of the trace.",
choices=[None],
default=None,
type=str,
required=False,
)
parser.add_argument(
"--blast",
"-bl",
help="Set the maximum in-memory storage count of OSI messages during validation.",
default=500,
type=check_positive_int,
required=False,
)
parser.add_argument(
"--buffer",
"-bu",
help="(Ignored) Set the buffer size to retrieve OSI messages from trace file. Set it to 0 if you do not want to use buffering at all.",
default=0,
type=check_positive_int,
required=False,
)
return parser.parse_args()
LOGS = []
LOGGER = osi_validator_logger.OSIValidatorLogger()
VALIDATION_RULES = osi_rules.OSIRules()
def detect_message_type(path: str):
"""Automatically detect the message type from the file name.
If it cannot be detected, the function return SensorView as default.
Args:
path (str): Path incl. filename of the trace file
Returns:
Str: Message type as string, e.g. SensorData, SensorView etc.
"""
filename = os.path.basename(path)
if filename.find("_sd_") != -1:
return "SensorData"
if filename.find("_sv_") != -1:
return "SensorView"
if filename.find("_gt_") != -1:
return "GroundTruth"
return "SensorView"
def main():
"""Main method"""
# Handling of command line arguments
args = command_line_arguments()
if not args.type:
args.type = detect_message_type(args.data)
# Instantiate Logger
print("Instantiate logger ...")
directory = args.output
if not os.path.exists(directory):
os.makedirs(directory)
LOGGER.init(args.debug, args.verbose, directory)
# Read data
print("Reading data ...")
trace = OSITrace(path=args.data, type_name=args.type)
# Collect Validation Rules
print("Collect validation rules ...")
try:
VALIDATION_RULES.from_yaml_directory(args.rules)
except Exception as e:
trace.close()
print("Error collecting validation rules:", e)
exit(1)
# Pass all timesteps or the number specified
if args.timesteps != -1:
max_timestep = args.timesteps
LOGGER.info(None, f"Pass the {max_timestep} first timesteps")
else:
LOGGER.info(None, "Pass all timesteps")
max_timestep = None
total_length = os.path.getsize(args.data)
current_pos = 0
with tqdm(total=total_length, unit="B", unit_scale=True, unit_divisor=1024) as pbar:
for index, message in enumerate(trace):
if index % args.blast == 0:
LOGS = []
if max_timestep and index >= max_timestep:
pbar.update(total_length - current_pos)
break
try:
process_message(message, index, args.type)
except Exception as e:
print(str(e))
new_pos = trace.file.tell()
pbar.update(new_pos - current_pos)
current_pos = new_pos
trace.close()
display_results()
if get_num_logs() > 0:
exit(1)
def process_message(message, timestep, data_type):
"""Process one message"""
rule_checker = osi_rules_checker.OSIRulesChecker(LOGGER)
timestamp = rule_checker.set_timestamp(message.timestamp, timestep)
LOGGER.log_messages[timestep] = []
LOGGER.debug_messages[timestep] = []
LOGGER.info(None, f"Analyze message of timestamp {timestamp}", False)
# Check common rules
getattr(rule_checker, "check_children")(
linked_proto_field.LinkedProtoField(message, name=data_type),
VALIDATION_RULES.get_rules().get_type(data_type),
)
LOGS.extend(LOGGER.log_messages[timestep])
# Synthetize Logs
def display_results():
return LOGGER.synthetize_results(LOGS)
def get_num_logs():
return len(LOGS)
if __name__ == "__main__":
main()