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
示例#2
0
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 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 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 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 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")
示例#9
0
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 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