variance.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import getopt
5 import math
6 from oct2py import octave
7 
8 def angle_difference(alpha, beta):
9  return math.atan2(math.sin(alpha-beta), math.cos(alpha-beta));
10 
11 def main(argv):
12  prog = argv[0]
13 
14  recordfile = ''
15  precision = -1
16 
17  # parse input
18  try:
19  (opts, args) = getopt.getopt(argv[1:],"hr:p:",["recordfile=","precision="])
20  except getopt.GetoptError:
21  print prog + ' -r <recordfile> -p <precision>'
22  sys.exit(2)
23  for opt, arg in opts:
24  if opt == '-h':
25  print prog + ' -r <recordfile> -p <precision>'
26  sys.exit()
27  elif opt in ("-r", "--recordfile"):
28  recordfile = arg
29  elif opt in ("-p", "--precision"):
30  precision = abs(float(arg))
31 
32  # verify input
33  if recordfile == '' or precision == -1 or len(args) != 0:
34  print prog + ' -r <recordfile> -p <precision>'
35  sys.exit(2)
36 
37  pos = recordfile.rfind("/")
38  variancefile = recordfile[:pos+1] + 'variance.csv'
39  parameterfile = recordfile[:pos+1] + 'parameter.csv'
40 
41  # read in expectations and samples from record file and prepare variance estimation
42  boxes = {}
43  count_all = 0
44  variance_global = (0, 0, 0, 0)
45  with open(recordfile, 'r') as f:
46  for line in f:
47  if line == 'exp_length;exp_angle;exp_orientation;length;angle;orientation\n':
48  continue
49 
50  tmp = line.split(';')
51  if len(tmp) != 6:
52  raise ValueError('Not a valid line: ' + line)
53  count_all = count_all + 1
54 
55  # get expected values
56  el = float(tmp[0])
57  ea = float(tmp[1])
58  eo = float(tmp[2])
59 
60  # get observed values
61  ol = float(tmp[3])
62  oa = float(tmp[4])
63  oo = float(tmp[5])
64 
65  # calculate local squared deviation
66  dl2 = (ol - el)**2
67  da2 = angle_difference(oa, ea)**2
68  do2 = angle_difference(oo, eo)**2
69 
70  # build id based on precision
71  id = '{}:{}:{}'.format(int(math.floor(abs(el/precision))), int(math.floor(abs(10*ea/precision))), int(math.floor(abs(10*eo/precision))))
72 
73  # update global squared deviation
74  variance_global = (variance_global[0] + 1, variance_global[1] + dl2, variance_global[2] + da2, variance_global[3] + do2)
75  if id in boxes.keys():
76  (n, sl2, sa2, so2) = boxes[id]
77  boxes[id] = (n + 1, sl2 + dl2, sa2 + da2, so2 + do2)
78  else:
79  boxes[id] = (1, dl2, da2, do2)
80 
81  # estimate variance based on samples and their expectation
82  variance_global = (variance_global[1]/variance_global[0], variance_global[2]/variance_global[0], variance_global[3]/variance_global[0])
83  print variance_global
84  variance = {}
85  suspicious = []
86  for id in boxes.keys():
87  (n, sl2, sa2, so2) = boxes[id]
88  variance[id] = (sl2/n, sa2/n, so2/n)
89 
90  if variance[id][0] > 2*variance_global[0] or variance[id][1] > 2*variance_global[1] or variance[id][2] > 2*variance_global[2]:
91  suspicious.append(id)
92 
93  # look for supsicous data
94  if len(suspicious) > 0:
95  count_susp = 0
96  print "Suspicious:"
97  for id in suspicious:
98  n = boxes[id][0]
99  count_susp = count_susp + n
100  print id + " (" + str(n) + "): " + str(variance[id])
101  if len(suspicious) > 0:
102  print "At all " + str(count_susp) + " from " + str(count_all) + " measurements are suspicious"
103 
104  # protocol expectations and variances
105  with open(recordfile, 'r') as rf:
106  with open(variancefile, 'w') as vf:
107  for line in rf:
108  if line == 'exp_length;exp_angle;exp_orientation;length;angle;orientation\n':
109  vf.write('exp_length;exp_angle;exp_orientation;var_length;var_angle;var_orientation;length;angle;orientation\n')
110  continue
111 
112  tmp = line.split(';')
113  if len(tmp) != 6:
114  raise ValueError('Not a valid line: ' + line)
115 
116  # get expected values
117  el = float(tmp[0])
118  ea = float(tmp[1])
119  eo = float(tmp[2])
120 
121  # build id based on precision
122  id = '{}:{}:{}'.format(int(math.floor(abs(el/precision))), int(math.floor(abs(10*ea/precision))), int(math.floor(abs(10*eo/precision))))
123 
124  # get estimated variance
125  (vl, va, vo) = variance[id]
126  assert vl > 0 and va > 0 and vo > 0
127 
128  # write out expectation and estimated variance
129  if id not in suspicious:
130  vf.write('{};{};{};{};{};{};{};{};{}\n'.format(el, ea, eo, vl, va, vo, tmp[3], tmp[4], tmp[5]))
131 
132  print 'Successfully created file \"' + variancefile + '\"'
133 
134  # estimate parameters
135  p = octave.parameter(variancefile)
136  print p
137 
138  with open(parameterfile, 'w') as f:
139  f.write(';par_length_x;par_length_c;par_angle_x;par_angle_c;par_orientation_x;par_orientation_c\n')
140  f.write('var_length;{};{};{};{};{};{}\n'.format(p[0][0], p[0][1], p[0][2], p[0][3], p[0][4], p[0][5]))
141  f.write('var_angle;{};{};{};{};{};{}\n'.format(p[1][0], p[1][1], p[1][2], p[1][3], p[1][4], p[1][5]))
142  f.write('var_orientation;{};{};{};{};{};{}\n'.format(p[2][0], p[2][1], p[2][2], p[2][3], p[2][4], p[2][5]))
143 
144  print 'Successfully created file \"' + parameterfile + '\"'
145 
146 if __name__ == '__main__':
147  main(sys.argv)
148 
def main(argv)
Definition: variance.py:11
def angle_difference(alpha, beta)
Definition: variance.py:8


tuw_marker_noise
Author(s): Markus Macsek
autogenerated on Mon Jun 10 2019 15:39:06