tests.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import time
  2. import logging
  3. import unittest
  4. from collections import defaultdict
  5. import tempfile
  6. import scipy
  7. from lws.localize import Environment, HMMLocalizer, LMSELocalizer, Measurements, errorsFromLists, PFLocalizer
  8. from lws.evaluate import Evaluator
  9. from utils.path import path
  10. HERE = path(__file__).parent.abspath()
  11. OBJFILE = HERE.joinpath('data/umic.obj')
  12. LOCFILE = HERE.joinpath('data/locations.txt')
  13. VI_PATH_MAT5 = HERE.joinpath('data/apdata/iconia.basic5mat/umic_%s.dat')
  14. SYNTHETIC_VI_PATH = HERE.joinpath('data/apdata/iconia_1/umic_%s.dat')
  15. TRACKED_PATH = path(r'D:\loco-dev\dirk\thesis\results\tracked_path')
  16. # Hmmf, this is a bad way
  17. TMP_DIR = path(tempfile.gettempdir()).joinpath('lws_tests')
  18. if not TMP_DIR.exists():
  19. TMP_DIR.mkdir()
  20. APS = [
  21. ('104', 44.8, 15.0, -1.0),
  22. ('106', 36.9, 7.5, 5.1),
  23. ('107', 35.3, 14.2, 1.0),
  24. ('108', 36.1, 4.4, 1.0),
  25. ('109p2', 51.7, 3.0, 1.0),
  26. ('110p2', 45.8, 15.0, 3.2),
  27. ('114', 36.6, 6.5, -1.5),
  28. ('171', 36.5, 9.55, 2.0),
  29. ('172', 23.4, 5.8, 2.6),
  30. ('edueg031', 41.6, 7.4, -1.0),
  31. ('eduegcorr1', 12.6, 4.9, -0.75),
  32. ('eduegcorr2', 31.6, 11.3, -1.0),
  33. ('eduegdemo', 28.7, 14.6, -2.6),
  34. ('eduog1108', 11.6, 6.4, 2.0),
  35. ('eduog1server', 48.5, 6.4, 2.0),
  36. ('eduog2206', 5.7, 6.35, 5.2),
  37. ('eduog2corr', 32.0, 11.2, 5.5),
  38. ('eduog2kitchen', 19.2, 9.2, 5.5),
  39. ('freya', 32.6, 10.5, 1.0),
  40. ('gaia', 45.8, 10.6, 1.0),
  41. ('hyperion', 39.3, 1.0, 0.1),
  42. ('iris', 9.8, 10.4, 0.1),
  43. ]
  44. class LwsTest(unittest.TestCase):
  45. synth_track_0 = HERE.joinpath('data/synthetic/measurements_0.txt')
  46. def setUp(self):
  47. self.tmp_dir = TMP_DIR
  48. def tearDown(self):
  49. pass
  50. #self.tmp_dir.rmtree()
  51. def testSyntheticNoNoiseSequential(self):
  52. self.env = Environment(objfile=OBJFILE, locationfile=LOCFILE,
  53. tmpdir=self.tmp_dir, aps=APS, vi_path=SYNTHETIC_VI_PATH, davalues=[])
  54. ms = Measurements(fname=self.synth_track_0)
  55. localizer = LMSELocalizer(self.env, cubewidth=2, verbose=False)
  56. full_run_paths, _ = localizer.evaluateMeasurements(ms)
  57. t = time.time()
  58. seq_path = []
  59. for m in ms:
  60. paths, _ = localizer.evaluateNext(Measurements([m]))
  61. self.assertEqual(len(paths['seq']), 1)
  62. seq_path.append(paths['seq'][0])
  63. self.assertEqual(seq_path[0], (89, 33, 11))
  64. for i, (p1, p2) in enumerate(zip(seq_path, full_run_paths['seq'])):
  65. self.assertEqual(p1, p2, 'pos %s: %s != %s' % (i, p1, p2))
  66. print '%.3f sec' % (time.time() - t)
  67. localizer = HMMLocalizer(self.env, cubewidth=2, verbose=False)
  68. t = time.time()
  69. full_run_paths, _ = localizer.evaluateMeasurements(ms)
  70. print 'full %.3f sec' % (time.time() - t)
  71. localizer.reset()
  72. t = time.time()
  73. seq_path = []
  74. for m in ms:
  75. paths, _ = localizer.evaluateNext(Measurements([m]))
  76. #~ self.assertEqual(len(paths['seq']), 1)
  77. for p in paths['seq']:
  78. seq_path.append(p)
  79. self.assertEqual(seq_path[0], (89, 33, 11))
  80. for i, (p1, p2) in enumerate(zip(seq_path, full_run_paths['seq'])):
  81. self.assertEqual(p1, p2, 'pos %s: %s != %s' % (i, p1, p2))
  82. print 'sequential %.3f sec' % (time.time() - t)
  83. def testSyntheticNoNoise(self):
  84. self.env = Environment(objfile=OBJFILE, locationfile=LOCFILE,
  85. tmpdir=self.tmp_dir, aps=APS, vi_path=SYNTHETIC_VI_PATH, davalues=[])
  86. ms = Measurements(fname=self.synth_track_0)
  87. ## Test LMSE
  88. localizer = LMSELocalizer(self.env, cubewidth=2, verbose=True)
  89. paths, measurements = localizer.evaluateMeasurements(ms)
  90. self.assertEqual(len(paths), 1)
  91. # ['end', 'seq', 'seq_avg']
  92. real_path, seq_path = localizer.toRealWordPaths(measurements, paths['seq'])
  93. err3ds = errorsFromLists(real_path, seq_path)
  94. self.assertLess(scipy.mean(err3ds), 0.43) # seen 0.416467731725
  95. ## Test HMM
  96. localizer = HMMLocalizer(self.env, cubewidth=2, prune_to=10000, num_threads=4, verbose=True)
  97. paths, measurements = localizer.evaluateMeasurements(ms)
  98. self.assertEqual(len(paths), 3)
  99. real_path, est_path, seq_avg_path = localizer.toRealWordPaths(measurements, paths['end'], paths['seq_avg'])
  100. err3ds = errorsFromLists(real_path, est_path)
  101. self.assertLess(scipy.mean(err3ds), 0.42) # seen 0.409063706859
  102. err3ds = errorsFromLists(real_path, seq_avg_path)
  103. self.assertLess(scipy.mean(err3ds), 0.48) # seen 0.466431152044
  104. ## Test PF
  105. localizer = PFLocalizer(self.env, cubewidth=2, num_particles=10000, do_blocking=True, verbose=True)
  106. paths, measurements = localizer.evaluateMeasurements(ms)
  107. self.assertEqual(len(paths), 3)
  108. real_path, est_path, seq_avg_path = localizer.toRealWordPaths(measurements, paths['end'], paths['seq_avg'])
  109. err3ds = errorsFromLists(real_path, est_path)
  110. self.assertLess(scipy.mean(err3ds), 1.0) # seen 0.993223448396
  111. err3ds = errorsFromLists(real_path, seq_avg_path)
  112. self.assertLess(scipy.mean(err3ds), 0.53) # seen 0.5188913299808956
  113. #~ print err3d, err2d
  114. def testEvalAll(self):
  115. #~ pfconfig = {'num_particles': 10000, 'do_blocking': True, 'smooth': 1.0}
  116. #~ optrun = 'basic5mat'
  117. #~ device= 'iconia'
  118. #~ evaluator = Evaluator(optrun, device, self.env, 1, TRACKED_PATH,
  119. #~ algo='pf', verbose=False, output_html=False, pfconfig=pfconfig,
  120. #~ errortype='mean', interpolate=False)
  121. print 'stuff'
  122. if __name__ == "__main__":
  123. logging.basicConfig(level=logging.DEBUG)
  124. suite = unittest.TestSuite()
  125. #~ suite.addTest(LwsTest("testSyntheticNoNoise"))
  126. suite.addTest(LwsTest("testSyntheticNoNoiseSequential"))
  127. unittest.TextTestRunner(verbosity=2).run(suite)