forked from woodywff/deepcorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
51 lines (47 loc) · 2.65 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
from tqdm.notebook import tqdm
import pdb
def accuracy(logits, y_truth):
'''
logits.shape == y_truth.shape == [batch_size,]
logits: output from sigmoid.
'''
batch_size = y_truth.shape[0]
return np.sum((logits >= 0.5) == y_truth)/batch_size
class GAN_Simulator():
'''
Simulate GAN on the ingress of Tor only.
'''
def __init__(self, flow_size=300, duplex=False):
'''
duplex: If True, modify all the traffic, otherwise, only change the output from here.
'''
self.flow_size = flow_size
self.duplex = duplex
return
def __call__(self, dataset):
# pdb.set_trace()
for i in tqdm(range(len(dataset)), desc='Through GAN simulator'):
noise_time_here = np.random.rand(self.flow_size)
noise_size_here = np.random.rand(self.flow_size)*1000
if self.duplex:
noise_time_there = np.random.rand(self.flow_size)
noise_size_there = np.random.rand(self.flow_size)*1000
dataset[i]['here'][0]['->'][:self.flow_size] = (dataset[i]['here'][0]['->'][:self.flow_size] \
+ noise_time_here).tolist()
dataset[i]['there'][0]['<-'][:self.flow_size] = (dataset[i]['there'][0]['<-'][:self.flow_size] \
+ noise_time_here).tolist()
dataset[i]['here'][1]['->'][:self.flow_size] = (dataset[i]['here'][1]['->'][:self.flow_size] \
+ noise_size_here).tolist()
dataset[i]['there'][1]['<-'][:self.flow_size] = (dataset[i]['there'][1]['<-'][:self.flow_size] \
+ noise_size_here).tolist()
if self.duplex:
dataset[i]['there'][0]['->'][:self.flow_size] = (dataset[i]['there'][0]['->'][:self.flow_size] \
+ noise_time_there).tolist()
dataset[i]['here'][0]['<-'][:self.flow_size] = (dataset[i]['here'][0]['<-'][:self.flow_size] \
+ noise_time_there).tolist()
dataset[i]['there'][1]['->'][:self.flow_size] = (dataset[i]['there'][1]['->'][:self.flow_size] \
+ noise_size_there).tolist()
dataset[i]['here'][1]['<-'][:self.flow_size] = (dataset[i]['here'][1]['<-'][:self.flow_size] \
+ noise_size_there).tolist()
return dataset