1010from osi3 .osi_groundtruth_pb2 import GroundTruth
1111from osi3 .osi_sensordata_pb2 import SensorData
1212import warnings
13- warnings .simplefilter ('default' )
1413
15- SEPARATOR = b'$$__$$'
14+ warnings .simplefilter ("default" )
15+
16+ SEPARATOR = b"$$__$$"
1617SEPARATOR_LENGTH = len (SEPARATOR )
1718BUFFER_SIZE = 1000000
1819
@@ -31,7 +32,7 @@ def get_size_from_file_stream(file_object):
3132MESSAGES_TYPE = {
3233 "SensorView" : SensorView ,
3334 "GroundTruth" : GroundTruth ,
34- "SensorData" : SensorData
35+ "SensorData" : SensorData ,
3536}
3637
3738
@@ -49,15 +50,15 @@ def __init__(self, path=None, type_name="SensorView"):
4950 def from_file (self , path , type_name = "SensorView" , max_index = - 1 , format_type = None ):
5051 """Import a scenario from a file"""
5152
52- if path .lower ().endswith ((' .lzma' , ' .xz' )):
53+ if path .lower ().endswith ((" .lzma" , " .xz" )):
5354 self .scenario_file = lzma .open (path , "rb" )
5455 else :
5556 self .scenario_file = open (path , "rb" )
5657
5758 self .type_name = type_name
5859 self .format_type = format_type
5960
60- if self .format_type == ' separated' :
61+ if self .format_type == " separated" :
6162 # 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)
6263 self .timestep_count = self .retrieve_message_offsets (max_index )
6364 else :
@@ -73,7 +74,7 @@ def retrieve_message_offsets(self, max_index):
7374 scenario_size = get_size_from_file_stream (self .scenario_file )
7475
7576 if max_index == - 1 :
76- max_index = float (' inf' )
77+ max_index = float (" inf" )
7778
7879 buffer_deque = deque (maxlen = 2 )
7980
@@ -100,7 +101,7 @@ def retrieve_message_offsets(self, max_index):
100101 self .scenario_file .seek (message_offset )
101102
102103 while eof and found != - 1 :
103- buffer = buffer [found + SEPARATOR_LENGTH :]
104+ buffer = buffer [found + SEPARATOR_LENGTH :]
104105 found = buffer .find (SEPARATOR )
105106
106107 buffer_offset = scenario_size - len (buffer )
@@ -126,7 +127,7 @@ def retrieve_message(self):
126127 self .message_offsets = [0 ]
127128 eof = False
128129
129- # TODO Implement buffering for the scenarios
130+ # TODO Implement buffering for the scenarios
130131 self .scenario_file .seek (0 )
131132 serialized_message = self .scenario_file .read ()
132133 INT_LENGTH = len (struct .pack ("<L" , 0 ))
@@ -135,8 +136,12 @@ def retrieve_message(self):
135136 i = 0
136137 while i < len (serialized_message ):
137138 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+ )
140145 i += message_length + INT_LENGTH
141146 self .message_offsets .append (i )
142147
@@ -153,7 +158,7 @@ def get_message_by_index(self, index):
153158 Get a message by its index. Try first to get it from the cache made
154159 by the method ``cache_messages_in_index_range``.
155160 """
156- return next (self .get_messages_in_index_range (index , index + 1 ))
161+ return next (self .get_messages_in_index_range (index , index + 1 ))
157162
158163 def get_messages (self ):
159164 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):
164169 """
165170 self .scenario_file .seek (self .message_offsets [begin ])
166171 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 )
169175 else self .retrieved_scenario_size
176+ )
170177
171178 rel_message_offsets = [
172179 abs_message_offset - abs_first_offset
173180 for abs_message_offset in self .message_offsets [begin :end ]
174181 ]
175182
176183 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 )
181186
182187 for rel_index , rel_message_offset in enumerate (rel_message_offsets ):
183188 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 )
186192 else message_sequence_len
193+ )
187194 message = MESSAGES_TYPE [self .type_name ]()
188195 serialized_message = serialized_messages_extract [rel_begin :rel_end ]
189196 message .ParseFromString (serialized_message )
@@ -212,27 +219,35 @@ def get_messages_in_index_range(self, begin, end):
212219
213220 def make_readable (self , name , interval = None , index = None ):
214221 self .scenario_file .seek (0 )
215- serialized_message = self .scenario_file .read ()
222+ serialized_message = self .scenario_file .read ()
216223 message_length = len (serialized_message )
217224
218225 if message_length > 1000000000 :
219226 # 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 :
226235 if interval is None and index is None :
227236 for i in self .get_messages ():
228237 f .write (str (i ))
229-
238+
230239 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+ ):
232245 for i in self .get_messages_in_index_range (interval [0 ], interval [1 ]):
233246 f .write (str (i ))
234247 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+ )
236251
237252 if interval is None and index is not None :
238253 if type (index ) == int :
0 commit comments