Vector.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python3
  2. try:
  3. import cPickle as pickle
  4. except ImportError:
  5. import pickle
  6. import os
  7. import sys
  8. sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + \
  9. '/../src/feature-extractor')
  10. import sample
  11. import typing
  12. from typing import List
  13. class FeatureVector:
  14. def __init__(self, s: sample):
  15. self.activefeatures = []
  16. # list of key, value tuples represnting values for features.
  17. self.sampleInfo = s
  18. if isinstance(s, sample.Sample):
  19. self.classification = s.user
  20. else:
  21. self.classification = "DUMMY DATA - DO NOT USE"
  22. # set which features are active using a binary list
  23. # ~~ex: For 2nd, 4th and 5th features to be active, pass [0,1,0,1,1]~~
  24. # ex: ["total_time", "average_iat", "dead_time"]
  25. def set_features(self, features: typing.List[str]):
  26. self.activefeatures = [self.sampleInfo[feature] for feature in features]
  27. def __repr__(self):
  28. return str(self.activefeatures)
  29. def writePickledData(filename):
  30. v = FeatureVector()
  31. v.features = [1, 2, 3, 4]
  32. v.activefeatures = [1, 2, 3, 4]
  33. vs = [v,v]
  34. with open(filename, 'wb') as file:
  35. pickle.dump(vs, file)
  36. def readPickledData(filename):
  37. with open(filename, 'rb') as file:
  38. x = pickle.load(file)
  39. # print(x)
  40. return x
  41. def main():
  42. # fv = FeatureVector()
  43. # writePickledData("test.bin")
  44. # readPickledData("test.txt")
  45. # s = sample.Sample([{"delta": 0, "time": '09/19/18 13:55:26', }], open("./results.txt"))
  46. # s.dead_time = 30
  47. # s.average_iat = 3
  48. # s.total_time = 30
  49. s = {"dead_time": 30, "average_iat": 3, "total_time": 30}
  50. fv = FeatureVector(s)
  51. fv.set_features(["total_time", "average_iat", "dead_time"])
  52. print(fv)
  53. if __name__ == '__main__':
  54. main()