10
10
from osi3 .osi_groundtruth_pb2 import GroundTruth
11
11
from osi3 .osi_sensordata_pb2 import SensorData
12
12
import warnings
13
- warnings .simplefilter ('default' )
14
13
15
- SEPARATOR = b'$$__$$'
14
+ warnings .simplefilter ("default" )
15
+
16
+ SEPARATOR = b"$$__$$"
16
17
SEPARATOR_LENGTH = len (SEPARATOR )
17
18
BUFFER_SIZE = 1000000
18
19
@@ -31,7 +32,7 @@ def get_size_from_file_stream(file_object):
31
32
MESSAGES_TYPE = {
32
33
"SensorView" : SensorView ,
33
34
"GroundTruth" : GroundTruth ,
34
- "SensorData" : SensorData
35
+ "SensorData" : SensorData ,
35
36
}
36
37
37
38
@@ -49,15 +50,15 @@ def __init__(self, path=None, type_name="SensorView"):
49
50
def from_file (self , path , type_name = "SensorView" , max_index = - 1 , format_type = None ):
50
51
"""Import a scenario from a file"""
51
52
52
- if path .lower ().endswith ((' .lzma' , ' .xz' )):
53
+ if path .lower ().endswith ((" .lzma" , " .xz" )):
53
54
self .scenario_file = lzma .open (path , "rb" )
54
55
else :
55
56
self .scenario_file = open (path , "rb" )
56
57
57
58
self .type_name = type_name
58
59
self .format_type = format_type
59
60
60
- if self .format_type == ' separated' :
61
+ if self .format_type == " separated" :
61
62
# warnings.warn("The separated trace files will be completely removed in the near future. Please convert them to *.osi files with the converter in the main OSI repository.", PendingDeprecationWarning)
62
63
self .timestep_count = self .retrieve_message_offsets (max_index )
63
64
else :
@@ -73,7 +74,7 @@ def retrieve_message_offsets(self, max_index):
73
74
scenario_size = get_size_from_file_stream (self .scenario_file )
74
75
75
76
if max_index == - 1 :
76
- max_index = float (' inf' )
77
+ max_index = float (" inf" )
77
78
78
79
buffer_deque = deque (maxlen = 2 )
79
80
@@ -100,7 +101,7 @@ def retrieve_message_offsets(self, max_index):
100
101
self .scenario_file .seek (message_offset )
101
102
102
103
while eof and found != - 1 :
103
- buffer = buffer [found + SEPARATOR_LENGTH :]
104
+ buffer = buffer [found + SEPARATOR_LENGTH :]
104
105
found = buffer .find (SEPARATOR )
105
106
106
107
buffer_offset = scenario_size - len (buffer )
@@ -126,7 +127,7 @@ def retrieve_message(self):
126
127
self .message_offsets = [0 ]
127
128
eof = False
128
129
129
- # TODO Implement buffering for the scenarios
130
+ # TODO Implement buffering for the scenarios
130
131
self .scenario_file .seek (0 )
131
132
serialized_message = self .scenario_file .read ()
132
133
INT_LENGTH = len (struct .pack ("<L" , 0 ))
@@ -135,8 +136,12 @@ def retrieve_message(self):
135
136
i = 0
136
137
while i < len (serialized_message ):
137
138
message = MESSAGES_TYPE [self .type_name ]()
138
- message_length = struct .unpack ("<L" , serialized_message [i :INT_LENGTH + i ])[0 ]
139
- message .ParseFromString (serialized_message [i + INT_LENGTH :i + INT_LENGTH + message_length ])
139
+ message_length = struct .unpack (
140
+ "<L" , serialized_message [i : INT_LENGTH + i ]
141
+ )[0 ]
142
+ message .ParseFromString (
143
+ serialized_message [i + INT_LENGTH : i + INT_LENGTH + message_length ]
144
+ )
140
145
i += message_length + INT_LENGTH
141
146
self .message_offsets .append (i )
142
147
@@ -153,7 +158,7 @@ def get_message_by_index(self, index):
153
158
Get a message by its index. Try first to get it from the cache made
154
159
by the method ``cache_messages_in_index_range``.
155
160
"""
156
- return next (self .get_messages_in_index_range (index , index + 1 ))
161
+ return next (self .get_messages_in_index_range (index , index + 1 ))
157
162
158
163
def get_messages (self ):
159
164
return self .get_messages_in_index_range (0 , len (self .message_offsets ))
@@ -164,26 +169,28 @@ def get_messages_in_index_range(self, begin, end):
164
169
"""
165
170
self .scenario_file .seek (self .message_offsets [begin ])
166
171
abs_first_offset = self .message_offsets [begin ]
167
- abs_last_offset = self .message_offsets [end ] \
168
- if end < len (self .message_offsets ) \
172
+ abs_last_offset = (
173
+ self .message_offsets [end ]
174
+ if end < len (self .message_offsets )
169
175
else self .retrieved_scenario_size
176
+ )
170
177
171
178
rel_message_offsets = [
172
179
abs_message_offset - abs_first_offset
173
180
for abs_message_offset in self .message_offsets [begin :end ]
174
181
]
175
182
176
183
if self .format_type == "separated" :
177
- message_sequence_len = abs_last_offset - \
178
- abs_first_offset - SEPARATOR_LENGTH
179
- serialized_messages_extract = self .scenario_file .read (
180
- message_sequence_len )
184
+ message_sequence_len = abs_last_offset - abs_first_offset - SEPARATOR_LENGTH
185
+ serialized_messages_extract = self .scenario_file .read (message_sequence_len )
181
186
182
187
for rel_index , rel_message_offset in enumerate (rel_message_offsets ):
183
188
rel_begin = rel_message_offset
184
- rel_end = rel_message_offsets [rel_index + 1 ] - SEPARATOR_LENGTH \
185
- if rel_index + 1 < len (rel_message_offsets ) \
189
+ rel_end = (
190
+ rel_message_offsets [rel_index + 1 ] - SEPARATOR_LENGTH
191
+ if rel_index + 1 < len (rel_message_offsets )
186
192
else message_sequence_len
193
+ )
187
194
message = MESSAGES_TYPE [self .type_name ]()
188
195
serialized_message = serialized_messages_extract [rel_begin :rel_end ]
189
196
message .ParseFromString (serialized_message )
@@ -212,27 +219,35 @@ def get_messages_in_index_range(self, begin, end):
212
219
213
220
def make_readable (self , name , interval = None , index = None ):
214
221
self .scenario_file .seek (0 )
215
- serialized_message = self .scenario_file .read ()
222
+ serialized_message = self .scenario_file .read ()
216
223
message_length = len (serialized_message )
217
224
218
225
if message_length > 1000000000 :
219
226
# Throw a warning if trace file is bigger than 1GB
220
- gb_size_input = round (message_length / 1000000000 , 2 )
221
- gb_size_output = round (3.307692308 * message_length / 1000000000 , 2 )
222
- warnings .warn (f"The trace file you are trying to make readable has the size { gb_size_input } GB. This will generate a readable file with the size { gb_size_output } GB. Make sure you have enough disc space and memory to read the file with your text editor." , ResourceWarning )
223
-
224
- with open (name , 'a' ) as f :
225
-
227
+ gb_size_input = round (message_length / 1000000000 , 2 )
228
+ gb_size_output = round (3.307692308 * message_length / 1000000000 , 2 )
229
+ warnings .warn (
230
+ f"The trace file you are trying to make readable has the size { gb_size_input } GB. This will generate a readable file with the size { gb_size_output } GB. Make sure you have enough disc space and memory to read the file with your text editor." ,
231
+ ResourceWarning ,
232
+ )
233
+
234
+ with open (name , "a" ) as f :
226
235
if interval is None and index is None :
227
236
for i in self .get_messages ():
228
237
f .write (str (i ))
229
-
238
+
230
239
if interval is not None and index is None :
231
- if type (interval ) == tuple and len (interval ) == 2 and interval [0 ]< interval [1 ]:
240
+ if (
241
+ type (interval ) == tuple
242
+ and len (interval ) == 2
243
+ and interval [0 ] < interval [1 ]
244
+ ):
232
245
for i in self .get_messages_in_index_range (interval [0 ], interval [1 ]):
233
246
f .write (str (i ))
234
247
else :
235
- raise Exception ("Argument 'interval' needs to be a tuple of length 2! The first number must be smaller then the second." )
248
+ raise Exception (
249
+ "Argument 'interval' needs to be a tuple of length 2! The first number must be smaller then the second."
250
+ )
236
251
237
252
if interval is None and index is not None :
238
253
if type (index ) == int :
0 commit comments