-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
164 lines (129 loc) · 5.24 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import hyperparams
import argparse
import numpy as np
import simulations as sim
import os
import torch
# np.random.seed(42)
# torch.manual_seed(42)
parser = argparse.ArgumentParser()
#parameters for training
parser.add_argument('--nn', type=str, default='linear')
parser.add_argument('--layers', type=int, nargs='+', default=[160, 160, 160])
parser.add_argument('--lr', type=float, default=1e-3)
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--attention', action='store_true', default=False)
parser.add_argument('--bidirectional', action='store_true', default=False)
parser.add_argument('--supervised', action='store_true', default=False)
parser.add_argument('--cpu', action='store_true', default=False)
parser.add_argument('--exp', type=int, default=0)
#parameters for evaluation
parser.add_argument('--var_seq', action='store_true', default=False)
parser.add_argument('--var_hct', action='store_true', default=False)
parser.add_argument('--results', action='store_true', default=False)
args = parser.parse_args()
hp = hyperparams.Hyperparams()
hp.training.lr = args.lr
hp.training.batch_size = args.batch_size
hp.network.nn = args.nn
hp.network.layers = args.layers
hp.network.attention = args.attention
hp.network.bidirectional = args.bidirectional
hp.supervised = args.supervised
if args.cpu:
hp.device = torch.device('cpu')
# create save name for framework
hp.exp_name = ''
arg_dict = vars(args)
for i, arg in enumerate(arg_dict):
if i == len(arg_dict)-6:
hp.exp_name += str(arg_dict[arg])
break
else:
hp.exp_name += '{}_'.format(arg_dict[arg])
if args.exp!=0:
hp.exp_name += '_'+str(args.exp)
print(hp.exp_name)
if os.path.exists(hp.create_name):
hp.create_data = False
else:
hp.create_data = True
# execute the non-linear least squares method
if hp.network.nn == 'lsq':
hp.simulations.num_samples_leval = 10000
SNRs = [5, 7, 10, 15, 20, 25, 40, 100]
datapoints = [80, 90, 100, 110, 120, 130, 140, 150, 160]
Hcts = [0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6]
if args.var_seq:
params = np.zeros((len(datapoints), 4, 2))
for i, datapoint in enumerate(datapoints):
hp.acquisition.rep2 = datapoint+1
file = hp.create_name.replace('.p', '')+'_seq_'+str(hp.acquisition.rep2-1)+'.p'
if os.path.exists(file):
hp.create_data = False
else:
hp.create_data = True
params[i] = sim.run_simulations(hp, args=args, SNR=20, eval=True)
np.save('results'+hp.exp_name+'_seq.npy', params)
elif args.var_hct:
params = np.zeros((len(Hcts), 4, 2))
for i, Hct in enumerate(Hcts):
file = hp.create_name.replace('.p', '')+'_hct_'+str(Hct)+'.p'
if os.path.exists(file):
hp.create_data = False
else:
hp.create_data = True
hp.aif.Hct = Hct
params[i] = sim.run_simulations(hp, args=args, SNR=20, eval=True)
np.save('results'+hp.exp_name+'_hct.npy', params)
else:
params = np.zeros((len(SNRs), 4, 2))
for i, SNR in enumerate(SNRs):
file = hp.create_name.replace('.p', '')+'_'+str(SNR)+'.p'
if os.path.exists(file):
hp.create_data = False
else:
hp.create_data = True
params[i] = sim.run_simulations(hp, args=args, SNR=20, eval=True)
np.save('results'+hp.exp_name+'.npy', params)
# train a neural network based approach
elif not args.results:
sim.run_simulations(hp, args, SNR='all')
# passing --results will perform evaluation on the given framework
else:
hp.simulations.num_samples = 10000
SNRs = [5, 7, 10, 15, 20, 25, 40, 100]
datapoints = [80, 90, 100, 110, 120, 130, 140, 150, 160]
Hcts = [0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6]
if args.var_seq:
params = np.zeros((len(datapoints), 4, 2))
for i, datapoint in enumerate(datapoints):
hp.acquisition.rep2 = datapoint+1
file = hp.create_name.replace('.p', '')+'_seq_'+str(hp.acquisition.rep2-1)+'.p'
if os.path.exists(file):
hp.create_data = False
else:
hp.create_data = True
params[i] = sim.run_simulations(hp, args=args, SNR=20, eval=True)
np.save('results'+hp.exp_name+'_var_seq.npy', params)
elif args.var_hct:
params = np.zeros((len(Hcts), 4, 2))
for i, Hct in enumerate(Hcts):
file = hp.create_name.replace('.p', '')+'_hct_'+str(Hct)+'.p'
if os.path.exists(file):
hp.create_data = False
else:
hp.create_data = True
hp.aif.Hct = Hct
params[i] = sim.run_simulations(hp, args=args, SNR=20, eval=True)
np.save('results'+hp.exp_name+'_var_hct.npy', params)
else:
params = np.zeros((len(SNRs), 4, 2))
for i, SNR in enumerate(SNRs):
file = hp.create_name.replace('.p', '')+'_'+str(SNR)+'.p'
if os.path.exists(file):
hp.create_data = False
else:
hp.create_data = True
params[i] = sim.run_simulations(hp, args=args, SNR=SNR, eval=True)
np.save('results'+hp.exp_name+'.npy', params)