- Timestamp:
- 07/19/15 15:05:42 (9 years ago)
- Location:
- trunk/lib/nanownlib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/nanownlib/__init__.py
r11 r13 176 176 my_ip = getLocalIP(target_ip, target_port) 177 177 my_iface = getIfaceForIP(my_ip) 178 return subprocess.Popen(['chrt', '-r', '99', ' ./bin/csamp', my_iface, my_ip,178 return subprocess.Popen(['chrt', '-r', '99', 'nanown-csamp', my_iface, my_ip, 179 179 target_ip, "%d" % target_port, output_file, '0']) 180 180 … … 256 256 suspect += 'R' # reordered received packets 257 257 258 packet_rtt = last_rcvd['observed'] - last_sent['observed']259 if packet_rtt < 0:260 sys.stderr.write("WARN: Negative packet_rtt. last_rcvd=%s,last_sent=%s\n" % (last_rcvd, last_sent))261 262 258 last_sent_ack = None 263 259 try: 264 last_sent_ack = min(((p[' observed'],p) for p in packets265 if p['sent']==0 and p['payload_len']+last_sent['tcpseq'] ==p['tcpack']))[1]260 last_sent_ack = min(((p['tcpack'],p['observed'],p) for p in packets 261 if p['sent']==0 and p['payload_len']+last_sent['tcpseq']>=p['tcpack']))[2] 266 262 267 263 except Exception as e: 268 264 sys.stderr.write("WARN: Could not find last_sent_ack.\n") 269 265 266 packet_rtt = last_rcvd['observed'] - last_sent['observed'] 270 267 tsval_rtt = None 271 268 if None not in (timestamp_precision, last_sent_ack): 272 269 tsval_rtt = int(round((last_rcvd['tsval'] - last_sent_ack['tsval'])*timestamp_precision)) 273 270 271 if packet_rtt < 0 or (tsval_rtt != None and tsval_rtt < 0): 272 #sys.stderr.write("WARN: Negative packet or tsval RTT. last_rcvd=%s,last_sent=%s\n" % (last_rcvd, last_sent)) 273 suspect += 'N' 274 274 275 return {'packet_rtt':packet_rtt, 275 276 'tsval_rtt':tsval_rtt, … … 279 280 280 281 281 # trimeanand mad for each dist of differences282 # septasummary and mad for each dist of differences 282 283 def evaluateTrim(db, unusual_case, strim, rtrim): 283 284 cursor = db.conn.cursor() … … 292 293 FROM (SELECT probes.sample s,packet_rtt FROM probes,trim_analysis WHERE sent_trimmed=:strim AND rcvd_trimmed=:rtrim AND trim_analysis.probe_id=probes.id AND probes.test_case=:unusual_case AND probes.type in ('train','test')) u 293 294 """ 294 295 #TODO: check for "N" in suspect field and return a flag 296 295 297 params = {"strim":strim,"rtrim":rtrim,"unusual_case":unusual_case} 296 298 cursor.execute(query, params) 297 299 differences = [row[0] for row in cursor] 298 300 299 return ubersummary(differences),mad(differences)301 return septasummary(differences),mad(differences) 300 302 301 303 … … 362 364 for strim in range(0,num_sent): 363 365 for rtrim in range(0,num_rcvd): 366 #print(strim,rtrim) 364 367 if strim == 0 and rtrim == 0: 365 368 continue # no point in doing 0,0 again -
trunk/lib/nanownlib/stats.py
r11 r13 166 166 #return statistics.mean((l1,l2,l3,m,r3,r2,r1)) 167 167 168 169 def septasummary(values, distance=25): 170 left2 = 50-distance 171 left3 = 50-(distance/2.0) 172 left1 = left2/2.0 173 right2 = 50+distance 174 right3 = 50+(distance/2.0) 175 right1 = (right2+100)/2.0 176 l1,l2,l3,m,r3,r2,r1 = numpy.percentile(values, (left1,left2,left3,50,right3,right2,right1)) 177 return (l1+l2+l3+m+r3+r2+r1)/7.0 178 168 179 169 180 def tsvalwmean(subseries): … … 254 265 ubersummaryTest = functools.partial(summaryTest, ubersummary) 255 266 quadsummaryTest = functools.partial(summaryTest, quadsummary) 267 septasummaryTest = functools.partial(summaryTest, septasummary) 256 268 257 269 def rmse(expected, measurements): … … 327 339 else: 328 340 return 0 341 342 343 from pykalman import KalmanFilter 344 def pyKalman4DTest(params, greater, samples): 345 kp = params['kparams'] 346 #kp['initial_state_mean']=[quadsummary([s['unusual_packet'] for s in samples]), 347 # quadsummary([s['other_packet'] for s in samples]), 348 # numpy.mean([s['unusual_tsval'] for s in samples]), 349 # numpy.mean([s['other_tsval'] for s in samples])] 350 kf = KalmanFilter(n_dim_obs=4, n_dim_state=4, **kp) 351 smooth,covariance = kf.smooth([(s['unusual_packet'],s['other_packet'],s['unusual_tsval'],s['other_tsval']) 352 for s in samples]) 353 m = numpy.mean(smooth) 354 if greater: 355 if m > params['threshold']: 356 return 1 357 else: 358 return 0 359 else: 360 if m < params['threshold']: 361 return 1 362 else: 363 return 0 364 -
trunk/lib/nanownlib/train.py
r11 r13 300 300 301 301 302 from pykalman import KalmanFilter 303 _pykalman4d_params = None 304 _pykalman4d_params = {'observation_covariance': [[11960180434.411114, 4760272534.795976, 8797551081.431936, 6908794128.927051], [4760272534.795962, 12383598172.428213, 5470747537.2599745, 11252625555.297853], [8797551081.431955, 5470747537.2601185, 1466222848395.7058, 72565713883.12643], [6908794128.927095, 11252625555.297981, 72565713883.12654, 1519760903943.507]], 'transition_offsets': [592.5708159274, 583.3804671015271, 414.4187239098291, 562.166786712371], 'observation_offsets': [165.2279084503762, 157.76807691937614, 168.4235495099334, 225.33433430227353], 'initial_state_covariance': [[33599047.5, -18251285.25, 3242535690.59375, -8560730487.84375], [-18251285.25, 9914252.3125, -1761372688.59375, 4650260880.1875], [3242535690.59375, -1761372688.59375, 312926663745.03125, -826168494791.7188], [-8560730487.84375, 4650260880.1875, -826168494791.7188, 2181195982530.4688]], 'initial_state_mean': [12939012.5625, 12934563.71875, 13134751.608, 13138990.9985], 'transition_covariance': [[2515479496.145993, -401423541.70620924, 1409951418.1627903, 255932902.74454522], [-401423541.706214, 2744353887.676857, 1162316.2019491254, 1857251491.3987627], [1409951418.1628358, 1162316.2020361447, 543279068599.8229, -39399311190.5746], [255932902.74459982, 1857251491.398838, -39399311190.574585, 537826124257.5266]], 'observation_matrices': [[1.4255288693095167, -0.4254638445329988, 0.0003406844036817347, -0.0005475021956726778], [-0.46467270827589857, 1.4654311778340343, -0.0003321330280128265, -0.0002853945703691352], [-0.2644570970067974, -0.33955835481495455, 1.7494161615202275, -0.15394117603733548], [-0.3419097544041847, -0.23992883666045373, -0.15587790880447727, 1.7292393175137022]], 'transition_matrices': [[0.52163952865412, 0.47872618354122665, -0.0004322286766109684, 0.00017293351811531466], [0.5167436693545113, 0.48319044922845933, 7.765428142114672e-05, -0.00021518950285326355], [0.2091705950622469, 0.41051399729482796, 0.19341113299389256, 0.19562916616052917], [0.368592004009912, 0.22263632461118732, 0.20756792378812872, 0.20977025833570906]]} 305 _pykalman4d_good_threshold = 2009.25853272 306 _pykalman4d_params = None 307 308 _pykalman4d_params = {'observation_covariance': [[32932883342.63772, 18054300398.442295, 27538911550.824535, 17152378956.778696], [18054300398.446983, 436546443436.5115, 37327644533.69647, 424485386677.31274], [27538911550.838238, 37327644533.706024, 3276324705772.982, 456017515263.88715], [17152378956.788027, 424485386677.317, 456017515263.88245, 3767844180658.1724]], 'observation_matrices': [[1.025773112769464, -0.028755990114063934, 0.0003540921897382532, 0.0025748564713126143], [-0.8595457826320256, 1.8607522167556567, -0.003520779053701517, 0.002309145982167138], [-0.5806427858959466, 0.22466075141448982, 1.6247192012813798, -0.27363797512617793], [-0.5853369461874607, 0.262177909212312, -0.28415108658843735, 1.6020343138710018]], 'initial_state_mean': [0.0, 0.0, 0.0, 0.0], 'observation_offsets': [549.4498515668686, 484.2106453284049, 648.556719142234, 380.10978090584763], 'transition_covariance': [[4147844406.7768326, -1308763245.5992138, 2920744388.523955, 860096280.797968], [-1308763245.5998695, 171190325905.83395, 3557618712.218984, 165332873663.83142], [2920744388.532502, 3557618712.2283373, 1054894349089.0673, -117551209299.73402], [860096280.805706, 165332873663.83963, -117551209299.73474, 1223605046475.7324]], 'transition_offsets': [1156.9264087977374, 1150.752680207601, 1312.2595286459816, 1267.4069537452415], 'initial_state_covariance': [[667999273207241.0, 669330484615232.1, 713726904326576.2, 731731206363217.4], [669330484615390.9, 670664348906228.8, 715149243295271.9, 733189424910272.2], [713726904326843.4, 715149243295370.6, 762584802695960.9, 781821582244358.5], [731731206363417.0, 733189424910299.0, 781821582244278.6, 801543624134758.0]], 'transition_matrices': [[0.9680677036616316, 0.03260717171917804, 0.0005279411071512641, -0.0012363486571871363], [0.9555219601128613, 0.03851351491891819, 0.00411268796118236, 0.0017357967358293536], [0.622254432930994, -0.2583795512595657, 0.31745705251401546, 0.32357126976364725], [0.6644076824932768, -0.33545285094373867, 0.3295778964272671, 0.34682391469482354]]} 309 _pykalman4d_good_threshold = -253.849393803 310 def trainPyKalman4D(db, unusual_case, greater, num_observations): 311 global _pykalman4d_params 312 global _pykalman4d_good_threshold 313 db.resetOffsets() 314 315 if _pykalman4d_params == None: 316 train = db.subseries('train',unusual_case, offset=0) 317 null = db.subseries('train_null',unusual_case, offset=0) 318 train_array = numpy.asarray([(s['unusual_packet'],s['other_packet'],s['unusual_tsval'],s['other_tsval']) 319 for s in train]) 320 null_array = numpy.asarray([(s['unusual_packet'],s['other_packet'],s['unusual_tsval'],s['other_tsval']) 321 for s in null]) 322 kf = KalmanFilter(n_dim_obs=4, n_dim_state=4) 323 #initial_state_mean=[quadsummary([s['unusual_packet'] for s in train]), 324 # quadsummary([s['other_packet'] for s in train]), 325 # numpy.mean([s['unusual_tsval'] for s in train]), 326 # numpy.mean([s['other_tsval'] for s in train])]) 327 328 kf = kf.em(train_array+null_array[0:50000], n_iter=10, 329 em_vars=('transition_matrices', 330 'observation_matrices', 331 'transition_offsets', 332 'observation_offsets', 333 'transition_covariance', 334 'observation_covariance', 335 'initial_state_covariance')) 336 _pykalman4d_params = {'transition_matrices': kf.transition_matrices.tolist(), 337 'observation_matrices': kf.observation_matrices.tolist(), 338 'transition_offsets': kf.transition_offsets.tolist(), 339 'observation_offsets': kf.observation_offsets.tolist(), 340 'transition_covariance': kf.transition_covariance.tolist(), 341 'observation_covariance': kf.observation_covariance.tolist(), 342 'initial_state_mean': kf.initial_state_mean.tolist(), 343 'initial_state_covariance': kf.initial_state_covariance.tolist()} 344 print(_pykalman4d_params) 345 346 kf = KalmanFilter(n_dim_obs=4, n_dim_state=4, **_pykalman4d_params) 347 smoothed,covariance = kf.smooth(train_array) 348 null_smoothed,covariance = kf.smooth(null_array) 349 350 kp = _pykalman4d_params.copy() 351 #kp['initial_state_mean']=[quadsummary([s['unusual_packet'] for s in train]), 352 # quadsummary([s['other_packet'] for s in train]), 353 # numpy.mean([s['unusual_tsval'] for s in train]), 354 # numpy.mean([s['other_tsval'] for s in train])] 355 #kf = KalmanFilter(n_dim_obs=4, n_dim_state=4, **kp) 356 #null_smoothed,covariance = kf.smooth(null_array) 357 358 _pykalman4d_good_threshold = (numpy.mean([m[0]-m[1] for m in smoothed])+numpy.mean([m[0]-m[1] for m in null_smoothed]))/2.0 359 print(_pykalman4d_good_threshold) 360 361 362 def trainAux(params, num_trials): 363 estimator = functools.partial(pyKalman4DTest, params, greater) 364 estimates = bootstrap3(estimator, db, 'train', unusual_case, num_observations, num_trials) 365 null_estimates = bootstrap3(estimator, db, 'train_null', unusual_case, num_observations, num_trials) 366 367 bad_estimates = len([e for e in estimates if e != 1]) 368 bad_null_estimates = len([e for e in null_estimates if e != 0]) 369 370 false_negatives = 100.0*bad_estimates/num_trials 371 false_positives = 100.0*bad_null_estimates/num_trials 372 return false_positives,false_negatives 373 374 params = {'threshold':_pykalman4d_good_threshold, 'kparams':_pykalman4d_params} 375 376 wt = WorkerThreads(2, trainAux) 377 num_trials = 50 378 performance = [] 379 for t in range(-80,100,20): 380 thresh = _pykalman4d_good_threshold + abs(_pykalman4d_good_threshold)*(t/100.0) 381 params['threshold'] = thresh 382 wt.addJob(thresh, (params.copy(),num_trials)) 383 wt.wait() 384 while not wt.resultq.empty(): 385 job_id,errors = wt.resultq.get() 386 fp,fn = errors 387 #performance.append(((fp+fn)/2.0, job_id, fn, fp)) 388 performance.append((abs(fp-fn), job_id, fn, fp)) 389 performance.sort() 390 #pprint.pprint(performance) 391 best_threshold = performance[0][1] 392 #print("best_threshold:", best_threshold) 393 params['threshold']=best_threshold 394 395 wt.stop() 396 397 return {'trial_type':"train", 398 'num_observations':num_observations, 399 'num_trials':num_trials, 400 'params':json.dumps(params, sort_keys=True), 401 'false_positives':performance[0][3], 402 'false_negatives':performance[0][2]} 403 404 405 302 406 classifiers = {'boxtest':{'train':trainBoxTest, 'test':multiBoxTest, 'train_results':[]}, 303 407 'midsummary':{'train':functools.partial(trainSummary, midsummary), 'test':midsummaryTest, 'train_results':[]}, 304 'ubersummary':{'train':functools.partial(trainSummary, ubersummary), 'test':ubersummaryTest, 'train_results':[]},408 #'ubersummary':{'train':functools.partial(trainSummary, ubersummary), 'test':ubersummaryTest, 'train_results':[]}, 305 409 'quadsummary':{'train':functools.partial(trainSummary, quadsummary), 'test':quadsummaryTest, 'train_results':[]}, 306 'tsvalwmean':{'train':trainTsval, 'test':tsvalwmeanTest, 'train_results':[]}, 410 'septasummary':{'train':functools.partial(trainSummary, septasummary), 'test':septasummaryTest, 'train_results':[]}, 411 #'pykalman4d':{'train':trainPyKalman4D, 'test':pyKalman4DTest, 'train_results':[]}, 412 #'tsvalwmean':{'train':trainTsval, 'test':tsvalwmeanTest, 'train_results':[]}, 307 413 #'kalman':{'train':trainKalman, 'test':kalmanTest, 'train_results':[]}, 308 414 #'_trimean':{'train':None, 'test':trimeanTest, 'train_results':[]},
Note: See TracChangeset
for help on using the changeset viewer.