-
Notifications
You must be signed in to change notification settings - Fork 0
/
tune.py
182 lines (111 loc) · 4.97 KB
/
tune.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 12 22:10:26 2019
@author: thele
"""
import sys
import json
from .Sampler_factory import Paper_sampler
from .Investigation.Investigation_factory import Investigation_stage
from .main_utils.utils import Timer, plot_conditional_idx_improvment
from .main_utils.model_surf_utils import show_gpr_gpc, show_dummy_device
from .Playground.mock_device import build_mock_device_with_json
def tune_with_pygor_from_file(config_file):
with open(config_file) as f:
configs = json.load(f)
pygor_path = configs.get('path_to_pygor',None)
if pygor_path is not None:
sys.path.insert(0,pygor_path)
import Pygor
pygor = Pygor.Experiment(xmlip=configs.get('ip',None))
gates = configs['gates']
plunger_gates = configs['plunger_gates']
chan_no = configs['chan_no']
grouped = any(isinstance(i, list) for i in gates)
if grouped:
def jump(params,plungers=False):
if plungers:
labels = plunger_gates
else:
labels = gates
for i,gate_group in enumerate(labels):
pygor.setvals(gate_group,[params[i]]*len(gate_group))
return params
else:
def jump(params,plungers=False):
#print(params)
if plungers:
labels = plunger_gates
else:
labels = gates
pygor.setvals(labels,params)
return params
def measure():
cvl = pygor.do0d()[chan_no][0]
return cvl
def check():
return pygor.getvals(plunger_gates)
assert len(gates) == len(configs['general']['origin'])
inv_timer = Timer()
investigation_stage = Investigation_stage(jump,measure,check,configs['investigation'],inv_timer)
tune(jump,measure,investigation_stage,configs)
def tune_with_playground(config_file):
with open(config_file) as f:
configs = json.load(f)
device = build_mock_device_with_json(configs['playground'])
if configs['playground'].get('plot',False): show_dummy_device(device,configs)
plunger_gates = configs['plunger_gates']
def jump(params,inv=False):
if inv:
return params
else:
return device.jump(params)
measure = device.measure
check = lambda: device.check(plunger_gates)
inv_timer = Timer()
investigation_stage = Investigation_stage(jump,measure,check,configs['investigation'],inv_timer)
results,sampler = tune(jump,measure,investigation_stage,configs)
fields = ['vols_pinchoff','conditional_idx','origin']
if configs['playground'].get('plot',False):
show_gpr_gpc(sampler.gpr, configs, *sampler.t.get(*fields), gpc=sampler.gpc.predict_comb_prob)
plot_conditional_idx_improvment(sampler.t['conditional_idx'],configs)
return results,sampler
def tune_from_file(jump,measure,check,config_file):
with open(config_file) as f:
configs = json.load(f)
inv_timer = Timer()
investigation_stage = Investigation_stage(jump,measure,check,configs['investigation'],inv_timer)
results,sampler = tune(jump,measure,investigation_stage,configs)
return results,sampler
def tune(jump,measure,investigation_stage,configs):
configs['jump'] = jump
configs['measure'] = measure
configs['investigation_stage_class'] = investigation_stage
ps = Paper_sampler(configs)
for i in range(configs['general']['num_samples']):
print("============### ITERATION %i ###============"%i)
results = ps.do_iter()
for key,item in results.items():
print("%s:"%(key),item[-1])
return results, ps
def tune_origin_variable(jump,measure,par_invstage,child_invstage,par_configs,child_configs):
par_configs['jump'],child_configs['jump'] = jump,jump
par_configs['measure'],child_configs['measure'] = measure,measure
par_configs['investigation_stage_class'], child_invstage['investigation_stage_class'] = par_invstage, child_invstage
par_ps = Paper_sampler(par_configs)
child_ps_list = []
ps = par_ps
par_flag = True
for i in range(par_configs['general']['num_samples']):
print("============### ITERATION %i ###============"%i)
results = ps.do_iter()
for key,item in results.items():
print("%s:"%(key),item[-1])
if par_flag:
if new_origin_condition(ps):
child_configs_new = config_constructor(child_configs,results)
child_ps_list+=[Paper_sampler(child_configs_new)]
ps,par_flag = task_selector(par_ps,child_ps_list,i)
if __name__ == '__main__':
pass
#tune_with_pygor_from_file('tuning_config.json')