Commit 7a04282a authored by Advanced Instrumentation's avatar Advanced Instrumentation
Browse files

focus optimization HeNe test

parent 326c5a5f
......@@ -51,7 +51,67 @@ class ParabolicMirror:
def home(self):
for motor in self.motors:
motor.go_home()
motor.go_home()
def optimize(self, focus, wanted_focus_um, n_av = 10, sleep_time = 0, output = True, maxiter = 100, size_wanted = 6):
def to_min():
FWHMxs = []
FWHMys = []
for i in range(n_av):
measurement = focus.measure(full = True)
FWHMxs.append(measurement['FWHMx'])
FWHMys.append(measurement['FWHMy'])
del measurement
res = np.array(FWHMx)**2 + np.array(FWHMy)**2
return np.mean(res), np.std(res, ddof = -1)
def change_parameter(y, z):
config = {'pos_Pay': y, 'pos_Paz': z}
self.set_config(config)
return True
def opt_f(x):
y = x[0]
z = x[1]
change_parameter(y, z)
import time
time.sleep(sleep_time)
return to_min()
config0 = self.get_config()
x0 = [config0['pos_Pay'], config0['pos_Paz']]
# directly Nedler-Mead
res = scipy.optimize.minimize(opt_f, x0, method='Nelder-Mead', options={'disp': output,
'initial_simplex': None,
'maxiter': maxiter,
'xatol': 0.00001,
'return_all': False,
'fatol': size_wanted**2*2})
return res
# first measure
#loop till focus small enough
# optimize Pax
# optimize Pay
# optimize Cz
......@@ -315,6 +315,9 @@ def main(devices, io):
for i in range(loop_inttext.value):
import IPython
IPython.display.clear_output(wait = True)
import time
t = time.time()
......@@ -334,8 +337,7 @@ def main(devices, io):
else:
time.sleep(1/rate_text.value - (time.time() -t))
import IPython
IPython.display.clear_output(wait = True)
class DataStreamProcess(Process):
def __init__(self, connec, *args, **kwargs):
......@@ -367,7 +369,6 @@ def main(devices, io):
plt.draw_all(force = True)
else:
loop_measure()
print ('Cheese')
......
......@@ -564,7 +564,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -590,7 +590,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 9,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -621,7 +621,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 10,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -635,7 +635,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 12,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -647,7 +647,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"set rate cannot be reached.\n"
"test_device1 measured\n",
"maximal possible rate: 329.0\n"
]
}
],
......@@ -741,6 +742,183 @@
"dm.get_config(devices)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function std in module numpy.core.fromnumeric:\n",
"\n",
"std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<class 'numpy._globals._NoValue'>)\n",
" Compute the standard deviation along the specified axis.\n",
" \n",
" Returns the standard deviation, a measure of the spread of a distribution,\n",
" of the array elements. The standard deviation is computed for the\n",
" flattened array by default, otherwise over the specified axis.\n",
" \n",
" Parameters\n",
" ----------\n",
" a : array_like\n",
" Calculate the standard deviation of these values.\n",
" axis : None or int or tuple of ints, optional\n",
" Axis or axes along which the standard deviation is computed. The\n",
" default is to compute the standard deviation of the flattened array.\n",
" \n",
" .. versionadded:: 1.7.0\n",
" \n",
" If this is a tuple of ints, a standard deviation is performed over\n",
" multiple axes, instead of a single axis or all the axes as before.\n",
" dtype : dtype, optional\n",
" Type to use in computing the standard deviation. For arrays of\n",
" integer type the default is float64, for arrays of float types it is\n",
" the same as the array type.\n",
" out : ndarray, optional\n",
" Alternative output array in which to place the result. It must have\n",
" the same shape as the expected output but the type (of the calculated\n",
" values) will be cast if necessary.\n",
" ddof : int, optional\n",
" Means Delta Degrees of Freedom. The divisor used in calculations\n",
" is ``N - ddof``, where ``N`` represents the number of elements.\n",
" By default `ddof` is zero.\n",
" keepdims : bool, optional\n",
" If this is set to True, the axes which are reduced are left\n",
" in the result as dimensions with size one. With this option,\n",
" the result will broadcast correctly against the input array.\n",
" \n",
" If the default value is passed, then `keepdims` will not be\n",
" passed through to the `std` method of sub-classes of\n",
" `ndarray`, however any non-default value will be. If the\n",
" sub-classes `sum` method does not implement `keepdims` any\n",
" exceptions will be raised.\n",
" \n",
" Returns\n",
" -------\n",
" standard_deviation : ndarray, see dtype parameter above.\n",
" If `out` is None, return a new array containing the standard deviation,\n",
" otherwise return a reference to the output array.\n",
" \n",
" See Also\n",
" --------\n",
" var, mean, nanmean, nanstd, nanvar\n",
" numpy.doc.ufuncs : Section \"Output arguments\"\n",
" \n",
" Notes\n",
" -----\n",
" The standard deviation is the square root of the average of the squared\n",
" deviations from the mean, i.e., ``std = sqrt(mean(abs(x - x.mean())**2))``.\n",
" \n",
" The average squared deviation is normally calculated as\n",
" ``x.sum() / N``, where ``N = len(x)``. If, however, `ddof` is specified,\n",
" the divisor ``N - ddof`` is used instead. In standard statistical\n",
" practice, ``ddof=1`` provides an unbiased estimator of the variance\n",
" of the infinite population. ``ddof=0`` provides a maximum likelihood\n",
" estimate of the variance for normally distributed variables. The\n",
" standard deviation computed in this function is the square root of\n",
" the estimated variance, so even with ``ddof=1``, it will not be an\n",
" unbiased estimate of the standard deviation per se.\n",
" \n",
" Note that, for complex numbers, `std` takes the absolute\n",
" value before squaring, so that the result is always real and nonnegative.\n",
" \n",
" For floating-point input, the *std* is computed using the same\n",
" precision the input has. Depending on the input data, this can cause\n",
" the results to be inaccurate, especially for float32 (see example below).\n",
" Specifying a higher-accuracy accumulator using the `dtype` keyword can\n",
" alleviate this issue.\n",
" \n",
" Examples\n",
" --------\n",
" >>> a = np.array([[1, 2], [3, 4]])\n",
" >>> np.std(a)\n",
" 1.1180339887498949\n",
" >>> np.std(a, axis=0)\n",
" array([ 1., 1.])\n",
" >>> np.std(a, axis=1)\n",
" array([ 0.5, 0.5])\n",
" \n",
" In single precision, std() can be inaccurate:\n",
" \n",
" >>> a = np.zeros((2, 512*512), dtype=np.float32)\n",
" >>> a[0, :] = 1.0\n",
" >>> a[1, :] = 0.1\n",
" >>> np.std(a)\n",
" 0.45000005\n",
" \n",
" Computing the standard deviation in float64 is more accurate:\n",
" \n",
" >>> np.std(a, dtype=np.float64)\n",
" 0.44999999925494177\n",
"\n"
]
}
],
"source": [
"help(np.std)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function rand:\n",
"\n",
"rand(...) method of mtrand.RandomState instance\n",
" rand(d0, d1, ..., dn)\n",
" \n",
" Random values in a given shape.\n",
" \n",
" Create an array of the given shape and populate it with\n",
" random samples from a uniform distribution\n",
" over ``[0, 1)``.\n",
" \n",
" Parameters\n",
" ----------\n",
" d0, d1, ..., dn : int, optional\n",
" The dimensions of the returned array, should all be positive.\n",
" If no argument is given a single Python float is returned.\n",
" \n",
" Returns\n",
" -------\n",
" out : ndarray, shape ``(d0, d1, ..., dn)``\n",
" Random values.\n",
" \n",
" See Also\n",
" --------\n",
" random\n",
" \n",
" Notes\n",
" -----\n",
" This is a convenience function. If you want an interface that\n",
" takes a shape-tuple as the first argument, refer to\n",
" np.random.random_sample .\n",
" \n",
" Examples\n",
" --------\n",
" >>> np.random.rand(3,2)\n",
" array([[ 0.14022471, 0.96360618], #random\n",
" [ 0.37601032, 0.25528411], #random\n",
" [ 0.49313049, 0.94909878]]) #random\n",
"\n"
]
}
],
"source": [
"help(np.random.rand)"
]
},
{
"cell_type": "code",
"execution_count": null,
......
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