| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- try:
- import cPickle as pickle
- except ImportError:
- import pickle
- import sample
- import typing
- from typing import List
- class FeatureVector:
- def __init__(self):
- self.features = []
- self.activefeatures = []
- # list of key, value tuples represnting values for features.
- self.sampleInfo = {}
- self.classification = None
- # set which features are active using a binary list
- # ex: For 2nd, 4th and 5th features to be active, pass [0,1,0,1,1]
- def setFeatures(self, binlist):
- activefeatures = []
- if len(binlist) != len(self.features):
- print("Feature choice list must equal length of feature list")
- for b in range(len(binlist)):
- if binlist[b] == 1:
- activefeatures.append(self.features[b])
- self.activefeatures = activefeatures
- def __repr__(self):
- return str(self.features)
- # use to make a sample into a vector
- def SampleToFeatureVector(s: sample):
- print(s.__activities)
- v = FeatureVector()
- for a in s.__activities.keys():
- for d in s.__activities[a].keys():
- # a + d looks like: high.total_packets
- v.sampleInfo.update({a + "." + d: s.__activities[a][d]})
- v.features.extend(a["high"].values())
- v.features.extend(a["mid"].values())
- v.features.extend(a["low"].values())
- v.sampleInfo.update(("total_time", s.total_time))
- v.sampleInfo.update(("average_iat", s.average_iat))
- v.sampleInfo.update(("dead_time", s.dead_time))
- v.features.append(s.total_time)
- v.features.append(s.average_iat)
- v.features.append(s.dead_time)
- return v
- def writePickledData(filename):
- v = FeatureVector()
- v.features = [1, 2, 3, 4]
- v.activefeatures = [1, 2, 3, 4]
- vs = [v,v]
- with open(filename, 'wb') as file:
- pickle.dump(vs, file)
- def readPickledData(filename):
- with open(filename, 'rb') as file:
- x = pickle.load(file)
- # print(x)
- return x
- def main():
- fv = FeatureVector()
- # writePickledData("test.bin")
- # readPickledData("test.txt")
- s = sample.Sample([{"delta": 0, "time": '09/19/18 13:55:26', }], open("./results.txt"))
- s.dead_time = 30
- s.average_iat = 3
- s.total_time = 30
- fv = SampleToFeatureVector(s)
- if __name__ == '__main__':
- main()
|