Go to the documentation of this file.00001
00002
00003
00004
00005
00006 import argparse
00007 import sys
00008 import os
00009 import numpy
00010
00011
00012 def read_file_list(filename):
00013 file = open(filename)
00014 data = file.read()
00015 lines = data.replace(","," ").replace("\t"," ").split("\n")
00016 list = [[v.strip() for v in line.split(" ") if v.strip()!=""] for line in lines if len(line)>0 and line[0]!="#"]
00017 list = [(float(l[0]),l[1:]) for l in list if len(l)>1]
00018 return dict(list)
00019
00020 def associate(first_list, second_list,offset,max_difference):
00021 first_keys = first_list.keys()
00022 second_keys = second_list.keys()
00023 potential_matches = [(abs(a - (b + offset)), a, b)
00024 for a in first_keys
00025 for b in second_keys
00026 if abs(a - (b + offset)) < max_difference]
00027 potential_matches.sort()
00028 matches = []
00029 for diff, a, b in potential_matches:
00030 if a in first_keys and b in second_keys:
00031 first_keys.remove(a)
00032 second_keys.remove(b)
00033 matches.append((a, b))
00034
00035 matches.sort()
00036 return matches
00037
00038 if __name__ == '__main__':
00039
00040
00041 parser = argparse.ArgumentParser(description='''
00042 This script takes two data files with timestamps and associates them
00043 ''')
00044 parser.add_argument('first_file', help='first text file (format: timestamp data)')
00045 parser.add_argument('second_file', help='second text file (format: timestamp data)')
00046 parser.add_argument('--first_only', help='only output associated lines from first file', action='store_true')
00047 parser.add_argument('--offset', help='time offset added to the timestamps of the second file (default: 0.0)',default=0.0)
00048 parser.add_argument('--max_difference', help='maximally allowed time difference for matching entries (default: 0.02)',default=0.02)
00049 args = parser.parse_args()
00050
00051 first_list = read_file_list(args.first_file)
00052 second_list = read_file_list(args.second_file)
00053
00054 matches = associate(first_list, second_list,float(args.offset),float(args.max_difference))
00055
00056 if args.first_only:
00057 for a,b in matches:
00058 print("%f %s"%(a," ".join(first_list[a])))
00059 else:
00060 for a,b in matches:
00061 print("%f %s %f %s"%(a," ".join(first_list[a]),b-args.offset," ".join(second_list[b])))
00062
00063