TestML.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from utils import transport
  2. from utils.ml import ML, AnomalyDetection
  3. import unittest
  4. import json
  5. import os
  6. path = os.environ['MONITOR_CONFIG_PATH']
  7. f = open(path)
  8. CONFIG = json.loads( f.read())
  9. f.close()
  10. factory = transport.DataSourceFactory()
  11. class TestML(unittest.TestCase):
  12. def setUp(self):
  13. ref = CONFIG['store']['class']['read']
  14. p = CONFIG['store']['args']
  15. p['qid'] = ['apps']
  16. self.greader = factory.instance(type=ref,args=p)
  17. def test_has_date(self):
  18. r = self.greader.read()
  19. self.assertTrue(r)
  20. def test_Filter(self):
  21. r = self.greader.read()
  22. r = r['apps']
  23. x = ML.Filter('label','Google Chrome',r)
  24. for row in x:
  25. self.assertTrue(row['label'] == 'Google Chrome')
  26. def test_Extract(self):
  27. r = self.greader.read()
  28. r = r['apps']
  29. x = ML.Filter('label','Google Chrome',r)
  30. x_ = ML.Extract(['cpu_usage','memory_usage'], x)
  31. self.assertTrue (len (x) == len(x_))
  32. pass
  33. def test_Learn(self):
  34. ref = CONFIG['store']['class']['read']
  35. p = CONFIG['store']['args']
  36. greader = factory.instance(type=ref,args=p)
  37. data = greader.read()
  38. data = data['apps']
  39. lhandler = AnomalyDetection()
  40. features = CONFIG['learner']['anomalies']['features']
  41. label = CONFIG['learner']['anomalies']['label']
  42. lhandler.learn(data,'label','Google Chrome',features,label)
  43. if __name__ == '__main__' :
  44. unittest.main()