Commit 85de1a5c authored by Nick Sauerwein's avatar Nick Sauerwein
Browse files

change tomography to tomopy

parent 115e259c
......@@ -345,7 +345,7 @@ class measureset:
def make_widget(self):
heading = widgets.HTML('<b><br>Make measurements</b>')
self.loop_inttext = widgets.IntText(value = 10, description = 'loop')
self.loop_inttext = widgets.IntText(value = 1, description = 'loop')
self.output_check = widgets.Checkbox(description = 'output')
self.save_check = widgets.Checkbox(description = 'save')
......@@ -353,7 +353,7 @@ class measureset:
self.measure_bh = widgets.HBox([self.loop_inttext,
self.output_check,
self.save_check])
self.rate_text = widgets.FloatText(value = 5, description = 'rate')
self.rate_text = widgets.FloatText(value = 3, description = 'rate')
self.save_folder = widgets.Text(description = 'save in ',value = self.io.path)
self.save_folder.on_submit(self.change_path)
......@@ -386,7 +386,7 @@ class measureset:
def show(self):
display(self.widget)
def measure(self, bt = None, comment = '', name = None):
def measure(self, bt = None, comment = '', name = None, delete = True):
devices2measure = self.configset.find_devices2measure()
parallel = False
......@@ -405,9 +405,9 @@ class measureset:
def loop_measure(parallel = False, connec = None):
measurements = []
for i in range(self.loop_inttext.value):
import IPython
IPython.display.clear_output(wait = True)
if delete:
import IPython
IPython.display.clear_output(wait = True)
import time
t = time.time()
......@@ -431,8 +431,8 @@ class measureset:
self.configset.find_devices2measure(),
self.plot_configurator.plot_config, 0.01)
print ('maximal possible rate: ',np.round(1/(time.time() -t),1))
if (1/self.rate_text.value - (time.time() -t) < 0):
print ('maximal possible rate: ',np.round(1/(time.time() - t),1))
if (1/self.rate_text.value - (time.time() - t) < 0):
print ('set rate cannot be reached.')
else:
time.sleep(1/self.rate_text.value - (time.time() -t))
......@@ -661,10 +661,28 @@ class variableset:
self.select_device = widgets.Select(description = 'device: ',options = self.device_dict)
self.select_device.observe(self.on_value_change_device)
self.select_device.observe(self.on_value_change_device, names = 'value')
self.select_parameter = widgets.Select(description = 'parameter: ', options = {'no device selected': None})
if not self.measure:
self.select_parameter.observe(self.on_value_change_parameter, names = 'value')
self.boundmin = widgets.FloatText(description = 'min: ', value = 0.)
self.boundmax = widgets.FloatText(description = 'max: ', value = 0.)
self.uncertainty = widgets.FloatText(description = 'uncertainty: ', value = 1.)
self.widget = widgets.VBox([widgets.HBox([self.namehtml, self.select_device, self.select_parameter]),
widgets.HBox([self.boundmin, self.boundmax, self.uncertainty])])
self.on_value_change_device()
return
self.on_value_change_device()
self.widget = widgets.HBox([self.namehtml, self.select_device, self.select_parameter])
......@@ -695,6 +713,21 @@ class variableset:
self.select_parameter.options = scannable
if len(scannable) > 0:
self.select_parameter.value = scannable[0]
else:
self.select_parameter.value = None
def on_value_change_parameter(self, dum= None):
if self.get_parameter() == None:
return
device = self.select_device.value
config = dm.get_config([device])
self.boundmin.value = config[self.get_device_name()][self.get_parameter()]
self.boundmax.value = self.boundmin.value
def get_device_name(self):
......@@ -703,6 +736,11 @@ class variableset:
def get_parameter(self):
return self.select_parameter.value
def bound(self):
if self.measure:
return [None, None]
return [self.boundmin.value, self.boundmax.value]
class optimizer:
......@@ -712,7 +750,7 @@ class optimizer:
self.measureset = measureset
self.configset = configset
self.devices = measureset.devices
self.io = measureset.devices
self.io = measureset.io
self.variables_change = []
self.variables_formula = []
......@@ -755,8 +793,10 @@ class optimizer:
self.go_button.on_click(self.optimize)
self.n_steps = widgets.IntText(description = 'Steps: ', value = 10)
self.n_steps = widgets.IntText(description = 'Steps: ', value = 30)
self.file_load = widgets.Text(description = 'load results: ', value = 'None')
self.file_save = widgets.Text(description = 'save results: ', value = 'None')
self.widget = widgets.VBox([heading,
text1,
self.num_change,
......@@ -765,7 +805,7 @@ class optimizer:
self.num_formula,
self.VBox2,
self.formula_text,
self.n_steps,
widgets.HBox([self.n_steps, self.file_load, self.file_save]),
self.go_button], layout = box_layout)
def set_num_change(self, dum = None):
......@@ -781,7 +821,7 @@ class optimizer:
self.VBox2.children = [vc.widget for vc in self.variables_formula]
def update_variables(self):
measurements = self.measureset.measure()
measurements = self.measureset.measure(delete = False)
for vc in self.variables_change:
exec('global '+vc.name+'; '+vc.name+' = '+str(measurements[0]['config'][vc.get_device_name()][vc.get_parameter()]))
......@@ -811,10 +851,22 @@ class optimizer:
pars0 += [config0[vc.get_device_name()][vc.get_parameter()]]
def to_optimize(pars):
scaling = np.array([vc.uncertainty.value for vc in self.variables_change])
bounds = np.array([vc.bound() for vc in self.variables_change])
pars0 = np.array(pars0)/scaling
def to_optimize(pars):
pars = pars * scaling
config = dm.get_config(self.devices)
print ('Changing:')
for i,vc in enumerate(self.variables_change):
print (vc.get_device_name()+'/'+vc.get_parameter()+' = '+str(pars[i]))
config[vc.get_device_name()][vc.get_parameter()] = pars[i]
......@@ -825,33 +877,145 @@ class optimizer:
return to_opt
def after_it(res):
global n_done
print ('###################')
print ('step '+str(n_done)+' done')
scaledbounds = np.transpose(np.transpose(bounds)/scaling)
from skopt import gp_minimize
x0 = None
y0 = None
len_x0 = 0
if self.file_load.value != 'None':
from skopt import load
res0 = load(self.io.path+self.file_load.value+'.pkl')
x0 = res0.x_iters
y0 = res0.func_vals
len_x0 = len(x0)
res = gp_minimize(to_optimize, # the function to minimize
scaledbounds, # the bounds on each dimension of x
n_calls=self.n_steps.value, # the number of evaluations of f
n_random_starts=10, # the number of random initialization points
noise=1, # the noise level (optional)
acq_optimizer='lbfgs',
x0 = x0,
y0 = y0)
if self.file_save.value != 'None':
from skopt import dump
dump(res, self.io.path+self.file_save.value+'.pkl', store_objective=False)
from skopt.plots import plot_convergence
plt.figure()
plot_convergence(res)
if len(bounds) == 1:
from skopt.acquisition import gaussian_ei
for vc in self.variables_change:
print (vc.name +' = '+ str(eval(vc.name)))
for vf in self.variables_formula:
print (vf.name +' = '+ str(eval(vf.name)))
x = np.linspace(scaledbounds[0][0], scaledbounds[0][1], 400).reshape(-1, 1)
x_gp = res.space.transform(x.tolist())
print ('to_opt = '+str(eval(self.formula_text.value)))
num_models = len(res.models)
assert (n_done != self.n_steps.value)
times = (num_models)//5
if num_models == 0:
print ('no models made')
return
n_done += 1
old_x_iters = res.x_iters[:len_x0]
old_func_vals = res.func_vals[:len_x0]
plt.figure(figsize = (8, 14))
# Plot the 5 iterations following the 5 random points
for n in range(5):
plt.subplot(5, 2, 2*n+1)
gp = res.models[- (5- n)*times]
curr_x_iters = res.x_iters[len_x0:len_x0+10+n*times]
curr_func_vals = res.func_vals[len_x0:len_x0+10+n*times]
# Plot GP(x) + contours
y_pred, sigma = gp.predict(x_gp, return_std=True)
plt.plot(x, y_pred, "g--", label=r"$\mu_{GP}(x)$")
plt.fill(np.concatenate([x, x[::-1]]),
np.concatenate([y_pred - 1.9600 * sigma,
(y_pred + 1.9600 * sigma)[::-1]]),
alpha=.2, fc="g", ec="None")
# Plot sampled points
plt.plot(curr_x_iters, curr_func_vals,
"r.", markersize=8, label="Observations")
plt.plot(old_x_iters, old_func_vals,
'b.', markersize = 2, label = "Observations (loaded data)")
# Adjust plot layout
plt.grid()
if n == 0:
plt.legend(loc="best", prop={'size': 6}, numpoints=1)
if n != 4:
plt.tick_params(axis='x', which='both', bottom='off',
top='off', labelbottom='off')
# Plot EI(x)
plt.subplot(5, 2, 2*n+2)
acq = gaussian_ei(x_gp, gp, y_opt=np.min(curr_func_vals))
plt.plot(x, acq, "b", label="EI(x)")
plt.fill_between(x.ravel(), -2.0, acq.ravel(), alpha=0.3, color='blue')
next_x = res.x_iters[10+n*times]
next_acq = gaussian_ei(res.space.transform([next_x]), gp, y_opt=np.min(curr_func_vals))
plt.plot(next_x, next_acq, "bo", markersize=6, label="Next query point")
# Adjust plot layout
plt.grid()
if n == 0:
plt.legend(loc="best", prop={'size': 6}, numpoints=1)
if n != 4:
plt.tick_params(axis='x', which='both', bottom='off',
top='off', labelbottom='off')
plt.ylim((0,None))
else:
from skopt.plots import plot_evaluations
from skopt.plots import plot_objective
plt.figure()
plot_evaluations(res)
plt.figure()
plot_objective(res)
plt.show()
global n_done
n_done = 0
# directly Nedler-Mead
import scipy.optimize
res = scipy.optimize.minimize(to_optimize, pars0, method='Nelder-Mead',callback = after_it)
print (res)
#if len(par0) > 1:
# import cma
# cma.fmin(to_optimize, pars0, 1.,options = {'bounds': list(np.transpose(bounds)/scaling), 'tolx': 1e-3, 'maxiter': self.n_steps.value, 'verb_plot': self.n_steps.value})
#else:
#from scipy.optimize import differential_evolution
#res = differential_evolution(to_optimize, bounds, strategy='best1bin', maxiter=self.n_steps.value, popsize=7)
# import noisyopt
# res = noisyopt.minimizeSPSA(to_optimize, pars0, bounds=np.transpose(np.transpose(bounds)/scaling), niter=self.n_steps.value,paired = False)
# print (res)
......
This diff is collapsed.
File mode changed from 100644 to 100755
No preview for this file type
% columns="iteration, evaluation, sigma, max axis length, min axis length, all principle axes lengths (sorted square roots of eigenvalues of C)", seed=1037295, Thu Jul 13 17:00:41 2017
0 0 1.0 1.00002500031 1.0 1.0 1.00002500031
1 6 0.915660140826 1.00002500031 1.0 1.0 1.00002500031
2 12 0.890360413058 0.968078193678 0.885294233445 0.885294233445 0.968078193678
3 18 0.71461903787 1.01062831124 0.804779737728 0.804779737728 1.01062831124
4 24 0.58870709103 0.904639802402 0.703681552377 0.703681552377 0.904639802402
5 30 0.619661499094 0.826787465017 0.622982684259 0.622982684259 0.826787465017
6 36 0.622161542581 0.920906048612 0.533629151044 0.533629151044 0.920906048612
7 42 0.58389487754 0.827152264022 0.567822878616 0.567822878616 0.827152264022
8 48 0.716232701306 0.825444141487 0.523349699701 0.523349699701 0.825444141487
9 54 0.587835012459 0.850188898258 0.558999817557 0.558999817557 0.850188898258
10 60 0.490613962725 0.84311290571 0.488341706866 0.488341706866 0.84311290571
% columns="iteration, evaluation, min max(neg(.)) min(pos(.)) max correlation, correlation matrix principle axes lengths (sorted square roots of eigenvalues of correlation matrix)", seed=1037295, Thu Jul 13 17:00:41 2017
0 0 0.0 0 0 0.0 1 1
1 6 0.0746812192916 0.0746812192916 0.0746812192916 0.0746812192916 1 1
2 12 0.201487094004 0.201487094004 0.201487094004 0.201487094004 1 1
3 18 0.181999216377 0.181999216377 0.181999216377 0.181999216377 1 1
4 24 0.204280718157 0.204280718157 0.204280718157 0.204280718157 0.892030987042 1.0973972472
5 30 0.396054944787 0.396054944787 0.396054944787 0.396054944787 0.892030987042 1.0973972472
6 36 0.362179980185 0.362179980185 0.362179980185 0.362179980185 0.798636350172 1.16712466351
7 42 0.448939791536 0.448939791536 0.448939791536 0.448939791536 0.798636350172 1.16712466351
8 48 0.194099827468 0.194099827468 0.194099827468 0.194099827468 0.897719428625 1.09274874855
9 54 0.0844621370705 0.0844621370705 0.0844621370705 0.0844621370705 0.897719428625 1.09274874855
10 60 0.14528845785 0.14528845785 0.14528845785 0.14528845785 0.924506107146 1.07018150697
% # columns="iteration, evaluation, sigma, axis ratio, bestever, best, median, worst objective function value, further objective values of best", seed=1037295, Thu Jul 13 17:00:41 2017
1 6 0.915660140826 1.00002500031 0.2733936687819944 2.7339366878199439e-01 3.29976280104 6.68648765513
2 12 0.890360413058 1.09351010896 0.2733936687819944 6.8794648686030913e-01 1.27079110755 5.08783490619
3 18 0.71461903787 1.25578250031 0.2733936687819944 3.8175160986960760e-01 1.52927807942 3.88414236728
4 24 0.58870709103 1.28558123962 0.08158138277203436 8.1581382772034361e-02 1.40932009229 1.74944487382
5 30 0.619661499094 1.32714357222 0.08158138277203436 1.6270546118481122e-01 0.378484893925 0.704343954145
6 36 0.622161542581 1.72574164438 0.08158138277203436 3.0180752682955569e-01 0.624826369466 1.17400662726
7 42 0.58389487754 1.45670823627 0.08158138277203436 8.1786695191235534e-02 0.67303712729 1.42798017316
8 48 0.716232701306 1.57723247373 0.08158138277203436 2.8997803712731912e-01 0.438139433758 1.85367140117
9 54 0.587835012459 1.52091086894 0.08158138277203436 1.1229985529676285e-01 0.183680885131 0.818595129993
10 60 0.490613962725 1.72648146545 0.08158138277203436 8.5745081736729123e-02 0.275049230949 1.10624243472
% # columns=["iteration, evaluation, sigma, void, void, stds==sigma*sqrt(diag(C))", seed=1037295, Thu Jul 13 17:00:41 2017
0 0 1.0 0 0 1.0 1.00002500031
1 6 0.915660140826 0 0 0.814118326968 0.883226735586
2 12 0.890360413058 0 0 0.768733317659 0.854981216248
3 18 0.71461903787 0 0 0.538121795987 0.614935141954
4 24 0.58870709103 0 0 0.393278759831 0.46352915738
5 30 0.619661499094 0 0 0.403195802873 0.516135015552
6 36 0.622161542581 0 0 0.409424105578 0.476260153902
7 42 0.58389487754 0 0 0.37660435621 0.437097147782
8 48 0.716232701306 0 0 0.420018280001 0.602589858691
9 54 0.587835012459 0 0 0.295619083926 0.483960199682
10 60 0.490613962725 0 0 0.225072640243 0.362804292453
% # columns="iteration, evaluation, void, void, void, xmean", seed=1037295, Thu Jul 13 17:00:41 2017 # scaling_of_variables: 1, typical_x: 0
0 0 0 0.0 nan 3.03790580166 7.27449869244
1 6 0 0.0 nan 2.77006232086 6.58648699893
2 12 0 0.0 nan 3.39441435733 7.42285437593
3 18 0 0.0 nan 3.18886535887 7.49829440899
4 24 0 0.0 nan 3.21846420778 7.14105220778
5 30 0 0.0 nan 2.9474384779 6.75126197005
6 36 0 0.0 nan 2.69005282395 6.80602650468
7 42 0 0.0 nan 3.11751197926 7.18066483834
8 48 0 0.0 nan 3.14222822301 6.55398450123
9 54 0 0.0 nan 3.05332036601 7.15991625534
10 60 0 0.0 nan 2.90940730892 7.05654763396
% # iter+eval+sigma+0+fitness+xbest, seed=1037295, Thu Jul 13 17:00:41 2017
1 6 0.915660140826 0 0.273393668782 2.57539594548 6.74624357304
2 12 0.890360413058 0 0.68794648686 3.12382126913 7.81116513993
3 18 0.71461903787 0 0.38175160987 3.18124483828 7.58175038388
4 24 0.58870709103 0 0.081581382772 3.15265078499 7.14907626925
5 30 0.619661499094 0 0.162705461185 2.7919920992 6.76206278424
6 36 0.622161542581 0 0.30180752683 2.48207480965 7.03884646543
7 42 0.58389487754 0 0.0817866951912 3.22128943125 7.09903224251
8 48 0.716232701306 0 0.289978037127 3.03748285121 6.51688063056
9 54 0.587835012459 0 0.112299855297 2.926317437 7.29265282049
10 60 0.490613962725 0 0.0857450817367 2.91954232833 7.16011240792
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment