Example #1
0
def preprocess(source_filename, output_filename, snapshotCoords, length, delay, skip = 0):

    (path, filename, name, extension) = common.pfne(source_filename)

    idx = 0
    
    Tr = getTobbiTransformationMatrix(snapshotCoords)

    with open(source_filename, 'rU') as f, open(output_filename, 'w') as w:

        # Print a header for the output file.
        _printHeader(w)

        # Read lines.
        header = f.readline().split('\n')[0]
        wholefile = f.readlines()
        for line in wholefile:

            # parse the line
            timestamp, event, fixX, fixY, gzX, gzY = parseTobbiLine(header, line.split('\n')[0])

            # skip
            if int(timestamp) < skip :
                continue            

            # delay
            if int(timestamp) < delay :
                continue
            else :
                timestamp = int(timestamp) - delay

            # length
            if timestamp > length :
                break

            # Print
            # The number of Origin's columns is 9.
            origin = line.split('\n')[0].split('\t')[1:9]
            # try :
            w.write("{}\t{}".format(timestamp, '\t'.join(origin)))

            # Transformation
            try :
                fixation = getUnitCoord(Tr, int(fixX), int(fixY))
                w.write("\t{0:.3f}\t{1:.3f}".format(fixation[0], fixation[1]))
            except ValueError :
                fixation = ['', '']
                w.write("\t\t")
            try :
                gaze = getUnitCoord(Tr, int(gzX), int(gzY))
                w.write("\t{0:.3f}\t{1:.3f}\n".format(gaze[0], gaze[1]))
            except ValueError :
                gaze = ['', '']
                w.write("\t\t\n")
Example #2
0
def main():
    GAT = False
    # Define Filenames
    DELAY_FILENAME = "info/delay.csv" if not GAT else "info/gat.csv"
    SNAPSHOT_FILENAME = "info/snapshot.tsv"
    source = "raw/pororo_*.tsv"
    output = "data/"
    verbose = True

    try:
        opts, args = \
            getopt.getopt(sys.argv[1:], "vs:o:")

    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)

    for option, value in opts:

        if option == "-v":
            verbose = True

        if option in ("-s", "--source"):
            source = value

        if option in ("-o", "--output"):
            output = value

    # get file name list
    filenameList = glob.glob(source)

    snapshotCoordsList = common.readData(SNAPSHOT_FILENAME, '\t', False, verbose)
    delayList = common.readData(DELAY_FILENAME, ',', False, verbose)

    for fullname in filenameList:
        
        print "[01] Reading", fullname

        (path, filename, name, extension) = common.pfne(fullname)

        # snapshot coords
        snapshotCoords = common.findOne(name, snapshotCoordsList)
        tuples = []

        for i in range(4):
            tuples.append([float(snapshotCoords[i*2+0]), float(snapshotCoords[i*2+1])])

        length, delay, skip = [int(i) for i in common.findOne(name, delayList)]
        if verbose:
            print "delay => ", delay, "skip => ", skip, "length =>", length

        if GAT:
            _splited = re.split('s03p0\d', filename)
            output_filename = _splited[0] + 'GAT' + _splited[1]
        else:
            output_filename = filename

        # Do prepocess and store to a given output filename.
        if verbose:
            print "preprocess({}, {}, snapshotCoords, {}, {})"\
                .format(path + os.sep + filename, output + output_filename, length, delay, skip)
        
        preprocess(path + os.sep + filename, output + output_filename, tuples, length, delay, skip)
Example #3
0
def printFixations(source_filename, output_dir, timings, prior, post, skip):
    idx = 0
    fixations = []
    originalFixations = []
    timestamps = []

    (path, filename, name, extension) = common.pfne(source_filename)

    with open(source_filename, 'rU') as f, \
         open(output_dir + os.sep + name + ".fix", 'w') as w:
        # Print a header for the output file.
        _printHeader(w)

        header = f.readline()
        wholefile = f.readlines()
        for line in wholefile:
            
            # parse the line
            timestamp, event, fixX, fixY = parseTobbiLine(header, line)

            # skip
            if int(timestamp) < skip :
                continue
            else :
                timestamp = int(timestamp) - skip

            # process conditions
            if idx < len(timings):
                # Check if it's within interval.
                diff = int(timestamp) - int(timings[idx])
                if (DEBUG):
                    print "[03] timestamp=", timestamp, ", timings[idx]=", timings[idx]
                if diff > post and 0 < len(fixations):
                    if (DEBUG): 
                        print "[03] Process", len(fixations), "fixations for", \
                            timings[idx] 
                    _printFixations(w, timings[idx], timestamps, fixations)
                    fixations = []
                    originalFixations = []
                    timestamps = []
                    idx = idx + 1
                # Sliding for the case of the part 2 data.
                elif diff > -prior:
                    while int(timestamp) - int(timings[idx]) > -prior and idx < len(timings):
                        idx = idx + 1
                    if (DEBUG):
                        print "[03] Increase idx as", idx, "(", timings[idx], ")."

                # Queue a fixation coordinate
                if diff <= -prior and diff >= -post and event == "Fixation":
                    try:
                        fixation = [fixX, fixY]
                    except ValueError:
                        # Caution: Missing one of x, y fixation points.
                        if (DEBUG): print "[03] Missing fixation point of x:", \
                            fixX, "y:", fixY, "at", timestamp
                        # Exclude missing fixation data.
                        continue

                    if 0 == len(fixations) or \
                        fixations[-1] != fixation:
                        fixations.append(fixation)
                        timestamps.append(timestamp)
                        if (DEBUG):
                            print "[03] Queue a fixation."
                

            # process reminders
            else:
                _printFixations(w, timings[idx-1], timestamps, fixations)