def __save__(self, f): self.toSIUnits() if self.tags != None: X = [] Y = [] T = [] for (x, y, t) in zip(self.X, self.Y, self.tags): X.append("%.9g" % x) Y.append("%.9g" % y) T.append(t) data = numpy.array([X, Y, T]) format = "%s" else: data = numpy.array([self.X, self.Y]) format = "%.9g" d = dict() d[DimArray.__unit_x__] = Unit.unitStr(self.unit_x) d[DimArray.__unit_y__] = Unit.unitStr(self.unit_y) d[DimArray.__name_x__] = self.name_x d[DimArray.__name_y__] = self.name_y # if self.tags != None: # d[Plottable.__TAGS__] = self.tags c = ConfigParser.ConfigParser() utils.dict_CP.saveDictToConfParser(d, c, DimArray.__SECTION__) c.write(f) f.write("[" + Plottable.__DATA__ + "]\n") numpy.savetxt(f, data.transpose(), delimiter=",", fmt=format) #,fmt = ["%.6g","%.6g","%s"])#,fmt = "%.9g")
def __save__(self,f): self.toSIUnits() if self.tags != None: X = [] Y = [] T = [] for (x,y,t) in zip(self.X,self.Y,self.tags): X.append("%.9g"%x) Y.append("%.9g"%y) T.append(t) data = numpy.array([X,Y,T]) format = "%s" else: data = numpy.array([self.X,self.Y]) format = "%.9g" d = dict() d[DimArray.__unit_x__] = Unit.unitStr(self.unit_x) d[DimArray.__unit_y__] = Unit.unitStr(self.unit_y) d[DimArray.__name_x__] = self.name_x d[DimArray.__name_y__] = self.name_y # if self.tags != None: # d[Plottable.__TAGS__] = self.tags c = ConfigParser.ConfigParser() utils.dict_CP.saveDictToConfParser(d,c,DimArray.__SECTION__) c.write(f) f.write("["+ Plottable.__DATA__ + "]\n") numpy.savetxt(f,data.transpose(),delimiter = ",",fmt = format)#,fmt = ["%.6g","%.6g","%s"])#,fmt = "%.9g")
def __init__(self,rawX,rawY,rbw,**kwds): # loadSave.Saveable.__init__(self,**kwds) __Trace__.__init__(self,rawX,rawY) try: self.measParam["rbw"] = rbw except (AttributeError,RuntimeError): self.measParam = {"rbw" : rbw} rbw = Unit.toSI(rbw).asNumber() Fittable.__init__(self,X = rawX,Y = Unit.dBm2W(rawY)/rbw,unit_y = Unit.W/Unit.Hz,**kwds)
def test_convert_us(self): us = [ Unit(1, 10, KEY_PRESS_UNIT_TYPE), Unit(2, 11, KEY_PRESS_UNIT_TYPE), Unit(3, 10, KEY_RELEASE_UNIT_TYPE), Unit(4, 11, KEY_RELEASE_UNIT_TYPE), ] self.assertEqual(get_inputs_sequence(us, False), [Input(10, 1, 3), Input(11, 2, 4)])
def generate_V(X, best_X, grp_size, max_cft_pkt): V = Unit(grp_size, max_cft_pkt) V.mal = best_X.mal - X.mal for i in range(grp_size): for j in range(int(round(X.mal[i][1]))): V.craft[i][j] = best_X.craft[i][j] - X.craft[i][j] return V
def convert_y(self,newUnit): """converts the y axis of the array (changes the numerical values) into the newUnit if compatible""" if self.unit_y is Unit.dBm: if Unit.isUnit(newUnit,Unit.W): self.setXY(self.X,10**(self.Y/10)*0.001) self.set_unit_y(newUnit) return else: raise IncompatibleUnitsError(Unit.dBm,newUnit) fact = Unit.inUnit(self.unit_y,newUnit) self.Y = self.Y*fact self.unit_y = newUnit
def convert_y(self, newUnit): """converts the y axis of the array (changes the numerical values) into the newUnit if compatible""" if self.unit_y is Unit.dBm: if Unit.isUnit(newUnit, Unit.W): self.setXY(self.X, 10**(self.Y / 10) * 0.001) self.set_unit_y(newUnit) return else: raise IncompatibleUnitsError(Unit.dBm, newUnit) fact = Unit.inUnit(self.unit_y, newUnit) self.Y = self.Y * fact self.unit_y = newUnit
def __init__(self, Y=None, X=None, unit_y=1, unit_x=1, name_y=None, name_x=None, tags=None): self.X = X self.Y = Y self.unit_x = unit_x self.unit_y = unit_y self.tags = tags if (X == None) and (Y == None): self.X = [] self.Y = [] else: if X == None: self.Y = numpy.array(Y) self.X = numpy.array(range(0, len(Y))) else: if Y == None: self.X = numpy.array(X) self.Y = numpy.zeros(len(X)) else: self.X = numpy.array(X) self.Y = numpy.array(Y) if isinstance(unit_x, basestring): unit_x = Unit.unitFromStr(unit_x) if isinstance(unit_y, basestring): unit_y = Unit.unitFromStr(unit_y) if unit_x is None: unit_x = 1 if unit_y is None: unit_y = 1 self.unit_x = unit_x self.unit_y = unit_y if name_y == None: name_y = Unit.getName(self.unit_y) if name_y == "": name_y = "Y" if name_x == None: name_x = Unit.getName(self.unit_x) if name_x == "": name_x = "X" self.name_y = name_y self.name_x = name_x
def __init__(self, rawX, rawY, rbw, **kwds): # loadSave.Saveable.__init__(self,**kwds) __Trace__.__init__(self, rawX, rawY) try: self.measParam["rbw"] = rbw except (AttributeError, RuntimeError): self.measParam = {"rbw": rbw} rbw = Unit.toSI(rbw).asNumber() Fittable.__init__(self, X=rawX, Y=Unit.dBm2W(rawY) / rbw, unit_y=Unit.W / Unit.Hz, **kwds)
def __init__(self,Y = None,X = None,unit_y = 1,unit_x = 1,name_y = None,name_x = None,tags = None): self.X = X self.Y = Y self.unit_x = unit_x self.unit_y = unit_y self.tags = tags if (X == None)and(Y==None): self.X = [] self.Y = [] else: if X==None: self.Y = numpy.array(Y) self.X = numpy.array(range(0,len(Y))) else: if Y == None: self.X = numpy.array(X) self.Y = numpy.zeros(len(X)) else: self.X = numpy.array(X) self.Y = numpy.array(Y) if isinstance(unit_x,basestring): unit_x = Unit.unitFromStr(unit_x) if isinstance(unit_y,basestring): unit_y = Unit.unitFromStr(unit_y) if unit_x is None: unit_x = 1 if unit_y is None: unit_y = 1 self.unit_x = unit_x self.unit_y = unit_y if name_y == None: name_y = Unit.getName(self.unit_y) if name_y == "": name_y = "Y" if name_x == None: name_x = Unit.getName(self.unit_x) if name_x == "": name_x = "X" self.name_y = name_y self.name_x = name_x
def __init__(self, rawX, rawY, **kwds): # loadSave.Saveable.__init__(self,**kwds) __Trace__.__init__(self, rawX, rawY) Fittable.__init__(self, X=rawX, Y=Unit.dB2Number(rawY), unit_y=None, **kwds)
def getAppropriateUnitY(self): ran = max(self.Y) - min(self.Y) if ran != 0: unitUp = int(numpy.log10(ran))/3 new_u = Unit.upgradeUnit(self.unit_y,unitUp) else: new_u = self.unit_y return new_u
def __init__(self, rawX, rawY, **kwds): # loadSave.Saveable.__init__(self,**kwds) __Trace__.__init__(self, rawX, rawY) Fittable.__init__(self, X=rawX, Y=Unit.dBm2W(rawY), unit_y=Unit.W, **kwds)
def getAppropriateUnitY(self): ran = max(self.Y) - min(self.Y) if ran != 0: unitUp = int(numpy.log10(ran)) / 3 new_u = Unit.upgradeUnit(self.unit_y, unitUp) else: new_u = self.unit_y return new_u
def test_remove(self): l = [ Unit(10, 10, KEY_PRESS_UNIT_TYPE), Unit(10, 11, KEY_PRESS_UNIT_TYPE), Unit(10, 10, KEY_PRESS_UNIT_TYPE), Unit(10, 11, KEY_PRESS_UNIT_TYPE), Unit(11, 10, KEY_RELEASE_UNIT_TYPE), Unit(11, 10, KEY_RELEASE_UNIT_TYPE), ] self.assertEqual(remove_duplicates(l), [ Unit(10, 10, KEY_PRESS_UNIT_TYPE), Unit(10, 11, KEY_PRESS_UNIT_TYPE), Unit(11, 10, KEY_RELEASE_UNIT_TYPE), ])
def convert_x(self,newUnit): """converts the x axis of the array (changes the numerical values) into the newUnit if compatible""" fact = Unit.inUnit(self.unit_x,newUnit) self.X = self.X*fact self.unit_x = newUnit
def toSIUnits(self): self.convert_x(Unit.SIunit(self.unit_x)) self.convert_y(Unit.SIunit(self.unit_y)) return self
def __init__(self,rawX,rawY,**kwds): # loadSave.Saveable.__init__(self,**kwds) __Trace__.__init__(self,rawX,rawY) Fittable.__init__(self,X = rawX,Y = Unit.dBm2W(rawY),unit_y = Unit.W,**kwds)
def __init__(self,rawX,rawY,**kwds): # loadSave.Saveable.__init__(self,**kwds) __Trace__.__init__(self,rawX,rawY) Fittable.__init__(self,X = rawX,Y = Unit.dB2Number(rawY),unit_y = None,**kwds)
def convert_x(self, newUnit): """converts the x axis of the array (changes the numerical values) into the newUnit if compatible""" fact = Unit.inUnit(self.unit_x, newUnit) self.X = self.X * fact self.unit_x = newUnit
def initialize( grp_size, # Number of pkts in each group last_end_time, groupList, # Pcap info in current group max_time_extend, # maximum time overhead (l_t) max_cft_pkt, # maximum crafted traffic overhead (l_c) min_time_extend, max_crafted_pkt_prob, ): X = Unit(grp_size, max_cft_pkt) # position vector ics_time = 0 # accumulated increased ITA for i in range(grp_size): if i == 0: itv = groupList[i].time - last_end_time else: itv = groupList[i].time - groupList[i - 1].time # ics_time += random.uniform(0,max_time_extend)*itv ics_time += random.uniform(min_time_extend, max_time_extend) * itv X.mal[i][0] = groupList[i].time + ics_time max_mal_itv = (groupList[-1].time - last_end_time) * (max_time_extend + 1) # building slot map slot_num = grp_size * max_cft_pkt slot_itv = max_mal_itv / slot_num # initializing crafted pkts crafted_pkt_prob = random.uniform(0, max_crafted_pkt_prob) nxt_mal_no = 0 proto_max_lmt = [] # maximum protocol layer number for i in range(grp_size): if groupList[i].haslayer(TCP) or groupList[i].haslayer( UDP) or groupList[i].haslayer(ICMP): proto_max_lmt.append(3.) elif groupList[i].haslayer(IP) or groupList[i].haslayer( IPv6) or groupList[i].haslayer(ARP): proto_max_lmt.append(2.) elif groupList[i].haslayer(Ether): proto_max_lmt.append(1.) else: proto_max_lmt.append(0.) for i in range(slot_num): slot_time = i * slot_itv + last_end_time if slot_time >= X.mal[nxt_mal_no][0]: nxt_mal_no += 1 if nxt_mal_no == grp_size: break if (not decide_has_pkt(crafted_pkt_prob) ) or X.mal[nxt_mal_no][1] == max_cft_pkt: continue cft_no = int(round(X.mal[nxt_mal_no][1])) if proto_max_lmt[nxt_mal_no] == 3.: X.craft[nxt_mal_no][cft_no][1] = random.choice([1., 2., 3.]) mtu = 1460 elif proto_max_lmt[nxt_mal_no] == 2.: X.craft[nxt_mal_no][cft_no][1] = random.choice([1., 2.]) mtu = 1480 elif proto_max_lmt[nxt_mal_no] == 1.: X.craft[nxt_mal_no][cft_no][1] = 1. mtu = 1500 else: continue X.craft[nxt_mal_no][cft_no][0] = X.mal[nxt_mal_no][0] - slot_time X.craft[nxt_mal_no][cft_no][2] = random.uniform(0, mtu) X.mal[nxt_mal_no][1] += 1. return X, proto_max_lmt
def __loadFromCP__(self,cp): d = utils.dict_CP.configParserToDict(cp,DimArray.__SECTION__) self.unit_x = Unit.unitFromStr(d[DimArray.__unit_x__]) self.unit_y = Unit.unitFromStr(d[DimArray.__unit_y__]) self.name_x = d[DimArray.__name_x__] self.name_y = d[DimArray.__name_y__]
def __loadFromCP__(self, cp): d = utils.dict_CP.configParserToDict(cp, DimArray.__SECTION__) self.unit_x = Unit.unitFromStr(d[DimArray.__unit_x__]) self.unit_y = Unit.unitFromStr(d[DimArray.__unit_y__]) self.name_x = d[DimArray.__name_x__] self.name_y = d[DimArray.__name_y__]
def test_remove(self): l = [ Unit(1, 10, KEY_PRESS_UNIT_TYPE), Unit(2, 11, KEY_PRESS_UNIT_TYPE), Unit(3, 10, KEY_PRESS_UNIT_TYPE), Unit(3.1, 10, KEY_PRESS_UNIT_TYPE), Unit(4, 11, KEY_PRESS_UNIT_TYPE), Unit(5, 11, KEY_RELEASE_UNIT_TYPE), Unit(6, 10, KEY_RELEASE_UNIT_TYPE), ] self.assertEqual(remove_intermediate_pressing(l), [ Unit(1, 10, KEY_PRESS_UNIT_TYPE), Unit(2, 11, KEY_PRESS_UNIT_TYPE), Unit(5, 11, KEY_RELEASE_UNIT_TYPE), Unit(6, 10, KEY_RELEASE_UNIT_TYPE), ])