def aedat3ToAbstract(input_file): UI().objectUI.showMessage("Starting to read aedat3 file", "w") f = open(input_file, "rb") event_list = [] # Read comments. while True: if f.readline().strip() == "#!END-HEADER".encode(): break UI().objectUI.showMessage( "Starting to read the data (no progress bar available)", "w") # Read events while True: headers = f.read(28) if len(headers) != 28: break num_events = int.from_bytes(headers[20:24], "little") for _ in range(num_events): data = int.from_bytes(f.read(4), "little") ts = int.from_bytes(f.read(4), "little") x = (data >> 17) & 8191 y = (data >> 2) & 8191 p = (data >> 1) & 1 event_list.append(Event(x, y, p, nsecsToSecs(ts))) UI().objectUI.showMessage("Finishing reading the aedat3 file", "c") return event_list
def abstractToBin(event_list, output_file): UI().objectUI.showMessage("Starting to write bin file", "w") f = open(output_file, "wb") num_progress = getNumProgress(len(event_list)) for i, e in enumerate(event_list): if i % num_progress == 0: UI().objectUI.sumProgress() p = '1' if e.pol else '0' ts = '{0:023b}'.format(int(secsToNsecs(e.ts))) p_ts = p + ts # Raises exception if param bigger of what format accepts try: f.write(struct.pack('<B', e.x)) f.write(struct.pack('<B', e.y)) f.write(struct.pack('<B', int(p_ts[:8], 2))) f.write(struct.pack('>H', int(p_ts[8:], 2))) except Exception: raise Exception("Larger parameter than the bin format accepts") UI().objectUI.sumProgress(True) f.close() UI().objectUI.showMessage("Finishing writing the bin file", "c")
def abstractToRosbag(event_list, output_file): UI().objectUI.showMessage("Starting to write bag file", "w") bag = rosbag.Bag(output_file, "w") c = Config() if c.rosbag: topic = c.config_data["rosbag"]["topic"] else: topic = UI().objectUI.simpleInput( "Introduce the name of the topic where the events are going to be write: " ) num_progress = getNumProgress(len(event_list)) for i, event in enumerate(event_list): if i % num_progress == 0: UI().objectUI.sumProgress() e = _Event() e.x = event.x e.y = event.y e.polarity = event.pol e.ts = rospy.Time.from_sec(event.ts) bag.write(topic, e) UI().objectUI.sumProgress(True) bag.close() UI().objectUI.showMessage("Finishing writing the bag file", "c")
def abstractToAedat2(event_list, output_file): UI().objectUI.showMessage("Starting to write aedat2 file", "w") file = open(output_file, "wb") for comment in cte.INITIAL_COMMENTS_AEDAT2: file.write(comment.encode()) num_progress = getNumProgress(len(event_list)) for i, e in enumerate(event_list): if i % num_progress == 0: UI().objectUI.sumProgress() x = '{0:07b}'.format(e.x) y = '{0:07b}'.format(e.y) if len(x) != 7 or len(y) != 7: raise Exception("In AEDAT 2.0 x and y must be smaller than 128") p = '1' if e.pol else '0' address = "0" + y + x + p ts = secsToNsecs(e.ts) if ts >= TS_MAX: raise Exception( "Error, timestamp bigger than 4 bytes, cannot convert to aedat 2" ) file.write(struct.pack('>I', int(address, 2))) file.write(struct.pack('>I', int(ts))) UI().objectUI.sumProgress(True) file.close() UI().objectUI.showMessage("Finishing writing the aedat2 file", "c")
def binToAbstract(input_file): UI().objectUI.showMessage("Starting to read bin file", "w") f = open(input_file, "rb") event_list = [] num_progress = getNumProgress(os.path.getsize(input_file) / 5) i = 0 while True: if i % num_progress == 0: UI().objectUI.sumProgress() i += 1 data = f.read(5) if len(data) != 5: break x = int(data[0]) y = int(data[1]) p_ts = int.from_bytes(data[2:], "big") p = p_ts >> 23 ts = p_ts & 8388607 event_list.append(Event(x, y, p, nsecsToSecs(ts))) UI().objectUI.sumProgress(True) UI().objectUI.showMessage("Finishing reading the bin file", "c") return event_list
def aedat2ToAbstract(input_file): UI().objectUI.showMessage("Starting to read aedat2 file", "w") # Read comments. tam_comments = 0 in_comment = False f = open(input_file, "rb") while True: char = f.read(1)[0] if char == 35: in_comment = True elif char == 10 and in_comment: in_comment = False elif not in_comment: break tam_comments += 1 f.close() # Read events f = open(input_file, "rb") f.read(tam_comments) byt = bytearray() is_address = True event_list = [] x = 0 y = 0 p = 0 num_progress = getNumProgress(os.path.getsize(input_file) - tam_comments) i = 0 for char in f.read(): if i % num_progress == 0: UI().objectUI.sumProgress() i += 1 byt += bytearray(char.to_bytes(1, 'big')) if len(byt) == 4: num = struct.unpack('>I', byt)[0] byt = bytearray() cad = '{0:016b}'.format(num) if is_address: is_address = False y = int(cad[1:8], 2) x = int(cad[8:15], 2) p = cad[15] == '1' else: is_address = True ts = nsecsToSecs(int(cad, 2)) event_list.append(Event(x, y, p, ts)) UI().objectUI.sumProgress(True) UI().objectUI.showMessage("Finishing reading the aedat2 file", "c") return event_list
def abstractToMatlab(event_list, output_file): UI().objectUI.showMessage("Starting to write mat file", "w") file_data = {} c = Config() if c.matlab: struct_type = c.config_data["matlab"]["default"] else: struct_type = UI().objectUI.chooseWindow( "Which struct type do you prefer? ", cte.MATLAB_TYPES_ADMITTED) arrays = [[], [], [], []] for ev in event_list: arrays[0].append(double(ev.x)) arrays[1].append(double(ev.y)) arrays[2].append(double(ev.pol)) arrays[3].append(double(secsToNsecs(ev.ts))) # 1 struct if struct_type == cte.MATLAB_TYPES_ADMITTED[0]: if c.matlab: struct_name = c.config_data["matlab"]["1 struct"]["struct_name"] data_names = c.config_data["matlab"]["1 struct"]["names"] else: struct_name = UI().objectUI.simpleInput( "What is the name of the struct?: ") data_names = UI().objectUI.multiInputsWindow( "How are the names of these parameters", cte.MATLAB_STRUCT_NAMES) file_data[struct_name] = {} for arr, name in zip(arrays, data_names): file_data[struct_name][name] = arr # Matrix nx4 elif struct_type == cte.MATLAB_TYPES_ADMITTED[1]: if c.matlab: struct_name = c.config_data["matlab"]["Matrix nx4"]["struct_name"] else: struct_name = UI().objectUI.simpleInput( "What is the name of the struct?: ") file_data[struct_name] = column_stack( (arrays[0], arrays[1], arrays[2], arrays[3])) # 4 structs (one for each event's parameter) elif struct_type == cte.MATLAB_TYPES_ADMITTED[2]: if c.matlab: struct_names = c.config_data["matlab"]["4 structs"]["names"] else: struct_names = UI().objectUI.multiInputsWindow( "How are the names of these structs", cte.MATLAB_STRUCT_NAMES) for arr, name in zip(arrays, struct_names): file_data[name] = arr UI().objectUI.showMessage( "Starting to save the data (no progress bar available)", "w") sio.savemat(output_file, file_data, oned_as="column") UI().objectUI.showMessage("Finishing writing the mat file", "c")
def aedat4ToAbstract(input_file): UI().objectUI.showMessage("Starting to read aedat4 file", "w") decoder = aedat.Decoder(input_file) event_list = [] UI().objectUI.showMessage( "Starting to read the data (no progress bar available)", "w") for packet in decoder: if 'events' in packet: for ev in packet['events']: event_list.append( Event(ev[1], ev[2], ev[3], nsecsToSecs(ev[0]))) UI().objectUI.showMessage("Finishing reading the aedat4 file", "c") return event_list
def main(): UI("terminal") Config("src/config/config.json") # testFileAndType("data/aedat/aedat4/Cars_sequence.aedat4", "aedat") testTypes("aedat", "aedat")
def abstractToText(event_list, output_file): UI().objectUI.showMessage("Starting to write txt file", "w") f = open(output_file, "w") num_progress = getNumProgress(len(event_list)) for i, event in enumerate(event_list): if i % num_progress == 0: UI().objectUI.sumProgress() f.write("{:.9f} {} {} {}\n".format( event.ts, event.x, event.y, int(event.pol), )) UI().objectUI.sumProgress(True) f.close() UI().objectUI.showMessage("Finishing writing the txt file", "c")
def textToAbstract(input_file): UI().objectUI.showMessage("Starting to read txt file", "w") events = open(input_file).readlines() events_list = [] num_progress = getNumProgress(len(events)) for i, event in enumerate(events): if i % num_progress == 0: UI().objectUI.sumProgress() data = event.strip('\n').split(' ') events_list.append( Event( int(data[1]), # Integer int(data[2]), # Integer int(data[3]) == 1, # Boolean float(data[0]) # Double )) UI().objectUI.sumProgress(True) UI().objectUI.showMessage("Finishing reading the txt file", "c") return events_list
def abstractToAedat3(event_list, output_file): UI().objectUI.showMessage("Starting to write aedat3 file", "w") f = open(output_file, "wb") for comment in cte.INITIAL_COMMENTS_AEDAT3: f.write(comment.encode()) # Header b = bytearray(28) b[20:24] = struct.pack('<I', int(len(event_list))) f.write(b) # Events num_progress = getNumProgress(len(event_list)) for i, e in enumerate(event_list): if i % num_progress == 0: UI().objectUI.sumProgress() x = '{0:015b}'.format(e.x) y = '{0:015b}'.format(e.y) if len(x) != 15 or len(y) != 15: raise Exception("In aedat3 x and y must be smaller than 32768") p = '1' if e.pol else '0' address = x + y + p + "0" ts = secsToNsecs(e.ts) if ts >= TS_MAX: raise Exception( "Error, timestamp bigger than 4 bytes, cannot convert to aedat3" ) f.write(struct.pack('<I', int(address, 2))) f.write(struct.pack('<I', int(ts))) UI().objectUI.sumProgress(True) f.close() UI().objectUI.showMessage("Finishing writing the aedat3 file", "c")
def main(): # Args parse. input_file, output_file, input_type, output_type, use_config, config_path, ui_type = parseArguments( ) # Init config features. if use_config: Config(config_path) # Create UI. if ui_type == "graphic": UI("graphic") elif ui_type == "terminal": UI("terminal") # Init UI. try: UI().objectUI.initialWindow(convert, input_file, output_file, input_type, output_type, use_config, config_path) except Exception as e: UI().objectUI.errorWindow(e)
def rosbagToAbstract(input_file): UI().objectUI.showMessage("Starting to read bag file", "w") bag = rosbag.Bag(input_file) c = Config() if c.rosbag: topic = c.config_data["rosbag"]["topic"] else: topics = bag.get_type_and_topic_info().topics topic = UI().objectUI.chooseWindow( "Which is the topic that contains the events?: ", topics) event_list = [] num_progress = getNumProgress(bag.get_message_count(topic)) i = 0 for topic, msg, t in bag.read_messages(topics=topic): if i % num_progress == 0: UI().objectUI.sumProgress() i += 1 aux_list = [] if "EventArray" in str(type(msg)): # msg._type aux_list = msg.events else: aux_list.append(msg) for event in aux_list: event_list.append( Event(event.x, event.y, event.polarity, combine(event.ts.secs, event.ts.nsecs))) bag.close() UI().objectUI.sumProgress(True) UI().objectUI.showMessage("Finishing reading the bag file", "c") return event_list
def abstractToAedat(event_list, output_file): c = Config() if c.aedat: version = c.config_data["aedat"]["version"] else: version = UI().objectUI.chooseWindow("Choose a version: ", cte.AEDAT_ACCEPTED_VERSIONS) if version == cte.AEDAT_ACCEPTED_VERSIONS[0]: abstractToAedat2(event_list, output_file) elif version == cte.AEDAT_ACCEPTED_VERSIONS[1]: abstractToAedat3(event_list, output_file) elif version == cte.AEDAT_ACCEPTED_VERSIONS[2]: abstractToAedat4(event_list, output_file)
def matlabToAbstract(input_file): UI().objectUI.showMessage("Starting to read mat file", "w") structs_types = list(sio.whosmat(input_file)) file_data = sio.loadmat(input_file) is_matrix = False arrays = [] events = [] c = Config() # Only one struct if len(structs_types) == 1: struct_name, struct_size, _ = structs_types[0] # 1 struct if struct_size == (1, 1): UI().objectUI.showMessage("Detected 1 struct format", "i") struct = file_data[struct_name] if c.matlab: names = c.config_data["matlab"]["1 struct"]["names"] else: names = list( map( lambda name: UI().objectUI.chooseWindow( "Where is {}?: ".format(name), list(struct.dtype.fields)), cte.MATLAB_STRUCT_NAMES)) arrays = list(map(lambda name: struct[0][name][0], names)) # Matrix nx4 elif struct_size[1] == 4: UI().objectUI.showMessage("Detected matrix nx4 format", "i") is_matrix = True matrix = file_data[struct_name] if c.matlab: indexes = c.config_data["matlab"]["Matrix nx4"]["indexes"] else: ln = matrix[-1] UI().objectUI.showMessage( "The last line of the matrix is [{}, {}, {}, {}]".format( ln[0], ln[1], ln[2], ln[3]), "i") indexes = list( map( lambda name: UI().objectUI.chooseWindow( "Where is {}?: ".format(name), ln, True), cte.MATLAB_STRUCT_NAMES)) arrays = list(map(lambda index: matrix[:, index], indexes)) # 4 structs (one for each event's parameter) elif len(structs_types) == 4: UI().objectUI.showMessage("Detected 4 structs format", "i") options = list(map(lambda st: st[0], structs_types)) if c.matlab: names = c.config_data["matlab"]["4 structs"]["names"] else: names = list( map( lambda name: UI().objectUI.chooseWindow( "Where is {}?: ".format(name), options), cte.MATLAB_STRUCT_NAMES)) arrays = list(map(lambda name: file_data[name], names)) if len(arrays) == 0: raise Exception("No admitted format while reading mat file") i = 0 num_progress = getNumProgress(len(arrays[0])) if is_matrix: def get_val(v): return v else: def get_val(v): return v[0] for x, y, pol, ts in zip(arrays[0], arrays[1], arrays[2], arrays[3]): if i % num_progress == 0: UI().objectUI.sumProgress() i += 1 events.append( Event(get_val(x), get_val(y), get_val(pol), nsecsToSecs(get_val(ts)))) UI().objectUI.sumProgress(True) UI().objectUI.showMessage("Finishing reading the mat file", "c") return events
def abstractToAedat4(event_list, output_file): UI().objectUI.showMessage("Not implemented yet.", "w")