From e0cb8c5abe763960c025ddc19495e2765893678c Mon Sep 17 00:00:00 2001 From: sfop Date: Wed, 17 May 2017 08:52:23 +0200 Subject: [PATCH] Startup --- devices/CurrentCamera.properties | 18 ++++++++--------- devices/cam1.properties | 6 +++--- devices/cam2.properties | 6 +++--- plugins/GunScan.java | 4 ++-- script/CPython/gfitoff.py | 30 ++++++++++++++++++++++++++++ script/CPython/hfitoff.py | 23 +++++++++++++++++++++ script/CPython/wrapper.py | 10 ++++++++++ script/RFscan/GunScan.py | 5 +++-- script/test/TestGaussFitOffset.py | 15 ++++++++++++++ script/test/TestHarmonicFitOffset.py | 9 +++++++++ script/tmp.py | 5 +++++ 11 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 script/CPython/gfitoff.py create mode 100644 script/CPython/hfitoff.py create mode 100644 script/CPython/wrapper.py create mode 100644 script/test/TestGaussFitOffset.py create mode 100644 script/test/TestHarmonicFitOffset.py create mode 100644 script/tmp.py diff --git a/devices/CurrentCamera.properties b/devices/CurrentCamera.properties index 2c566b0..f3866e0 100644 --- a/devices/CurrentCamera.properties +++ b/devices/CurrentCamera.properties @@ -1,4 +1,4 @@ -#Thu May 11 11:21:30 CEST 2017 +#Wed May 17 08:34:25 CEST 2017 colormap=Flame colormapAutomatic=true colormapMax=0.0 @@ -6,11 +6,11 @@ colormapMin=0.0 flipHorizontally=false flipVertically=false grayscale=false -imageHeight=-1 -imageWidth=-1 +imageHeight=2160 +imageWidth=2560 invert=false -regionStartX=0 -regionStartY=0 +regionStartX=1 +regionStartY=1 rescaleFactor=1.0 rescaleOffset=0.0 roiHeight=-1 @@ -21,9 +21,9 @@ rotation=0.0 rotationCrop=false scale=1.0 serverURL=localhost\:10000 -spatialCalOffsetX=NaN -spatialCalOffsetY=NaN -spatialCalScaleX=NaN -spatialCalScaleY=NaN +spatialCalOffsetX=-1057.4895329398094 +spatialCalOffsetY=-1420.5549062527236 +spatialCalScaleX=-8.510638153514359 +spatialCalScaleY=-8.235817137431614 spatialCalUnits=mm transpose=false diff --git a/devices/cam1.properties b/devices/cam1.properties index cb9fed6..abb4f4e 100644 --- a/devices/cam1.properties +++ b/devices/cam1.properties @@ -1,4 +1,4 @@ -#Thu May 11 13:01:43 CEST 2017 +#Thu May 11 14:22:56 CEST 2017 colormap=Temperature colormapAutomatic=true colormapMax=30000.0 @@ -6,8 +6,8 @@ colormapMin=0.0 flipHorizontally=false flipVertically=false grayscale=false -imageHeight=0 -imageWidth=0 +imageHeight=1628 +imageWidth=1280 invert=false offsetX=0.0 offsetY=0.0 diff --git a/devices/cam2.properties b/devices/cam2.properties index 219f20e..5a95c37 100644 --- a/devices/cam2.properties +++ b/devices/cam2.properties @@ -1,4 +1,4 @@ -#Thu May 11 13:01:43 CEST 2017 +#Thu May 11 14:22:56 CEST 2017 colormap=Grayscale colormapAutomatic=true colormapMax=255.0 @@ -6,8 +6,8 @@ colormapMin=0.0 flipHorizontally=false flipVertically=false grayscale=false -imageHeight=0 -imageWidth=0 +imageHeight=1680 +imageWidth=1744 invert=false offsetX=0.0 offsetY=0.0 diff --git a/plugins/GunScan.java b/plugins/GunScan.java index 9d77384..63d64de 100644 --- a/plugins/GunScan.java +++ b/plugins/GunScan.java @@ -22,7 +22,7 @@ import javax.swing.JSpinner; public class GunScan extends Panel { LinePlotErrorSeries seriesEnergy = new LinePlotErrorSeries("Energy"); - LinePlotErrorSeries seriesEnergySpread = new LinePlotErrorSeries("Energy Spread"); + LinePlotErrorSeries seriesEnergySpread = new LinePlotErrorSeries("Energy Spread", null, 2); double dispersion; double energy0; @@ -30,7 +30,7 @@ public class GunScan extends Panel { initComponents(); plot.setStyle(LinePlotJFree.Style.ErrorY); plot.addSeries(seriesEnergy); - plot.addSeries(seriesEnergySpread); + plot.addSeries(seriesEnergySpread); plot.getAxis(Plot.AxisId.X).setLabel("Gun Phase"); plot.getAxis(Plot.AxisId.Y).setLabel("Energy (MeV)"); plot.getAxis(Plot.AxisId.Y2).setLabel("Energy Spread (MeV)"); diff --git a/script/CPython/gfitoff.py b/script/CPython/gfitoff.py new file mode 100644 index 0000000..31a1e1b --- /dev/null +++ b/script/CPython/gfitoff.py @@ -0,0 +1,30 @@ +import numpy as np +import scipy.optimize + + +def gfitoff(x, y, off=None, amp=None, com=None, sigma=None): + if off is None: + off = y.min() # good enough starting point for offset + + if com is None: + com = x[y.argmax()] + + if amp is None: + amp = y.max() - off + + # For normalised gauss curve sigma=1/(amp*sqrt(2*pi)) + if sigma is None: + surface = np.trapz((y-off), x=x) + sigma = surface / (amp * np.sqrt(2 * np.pi)) + try: + popt, pcov = scipy.optimize.curve_fit(gauss_fn, x, y, p0=[off, amp, com, sigma], method='lm') + popt[3] = abs(popt[3]) # sigma should be returned as positive + except Exception as e: + print("Gauss fitting not successful.\n" + str(e)) + popt = [off, amp, com, abs(sigma)] + + return popt + + +def gauss_fn(x, a, b, c, d): + return a + b * np.exp(-(np.power((x - c), 2) / (2 * np.power(d, 2)))) \ No newline at end of file diff --git a/script/CPython/hfitoff.py b/script/CPython/hfitoff.py new file mode 100644 index 0000000..be809fa --- /dev/null +++ b/script/CPython/hfitoff.py @@ -0,0 +1,23 @@ +import numpy as np +from scipy.optimize import leastsq + + +def hfitoff(data, xdeg): + """ + Harmonic fit with offset + """ + start, end = min(xdeg), max(xdeg) + guess_amplitude = 2 ** 0.5 * np.std(data) + guess_phase = 0 + guess_offset = np.mean(data) + xrad = xdeg / 180 * np.pi + optimize_func = lambda x: x[0] * np.sin(xrad + x[1]) + x[2] - data + fit_amplitude, fit_phase, fit_offset = leastsq(optimize_func, [guess_amplitude, guess_phase, guess_offset])[0] + fit_xdeg = np.linspace(start, end, 100) + fit_data = fit_amplitude * np.sin(fit_xdeg / 180 * np.pi + fit_phase) + fit_offset + ph_crest = 90 - fit_phase / np.pi * 180 + xdeg_max = xdeg[np.argmax(data)] + if start <= ph_crest <= end: + return (fit_amplitude, fit_phase, fit_offset, True, ph_crest, fit_xdeg, fit_data) + else: + return (fit_amplitude, fit_phase, fit_offset, False, xdeg_max, fit_xdeg, fit_data) diff --git a/script/CPython/wrapper.py b/script/CPython/wrapper.py new file mode 100644 index 0000000..46d632e --- /dev/null +++ b/script/CPython/wrapper.py @@ -0,0 +1,10 @@ +from jeputils import * + +def hfitoff(data, xdeg): + ret = call_jep("CPython/hfitoff", "hfitoff", [to_npa(data),to_npa(xdeg)]) + print ret + return (ret[0], ret[1], ret[2],ret[3], ret[4], ret[5].data,ret[6].data) + +def gfitoff(x, y, off=None, amp=None, com=None, sigma=None): + ret = call_jep("CPython/gfitoff", "gfitoff", [to_npa(x), to_npa(y), off, amp, com, sigma]) + return ret if ret is None or is_list(ret) else ret.data \ No newline at end of file diff --git a/script/RFscan/GunScan.py b/script/RFscan/GunScan.py index a9b16ca..46e1e05 100644 --- a/script/RFscan/GunScan.py +++ b/script/RFscan/GunScan.py @@ -18,7 +18,8 @@ else: disp = args[5] energy0 = args[6] -phase = ControlledVariable("Phase", "SINEG01-RSYS:SET-VSUM-PHASE", "SINEG01-RSYS:GET-VSUM-PHASE") +#phase = ControlledVariable("Phase", "SINEG01-RSYS:SET-VSUM-PHASE", "SINEG01-RSYS:GET-VSUM-PHASE") +phase = ControlledVariable("Phase", "STEST01-RSYS:SET-VSUM-PHASE", "STEST01-RSYS:GET-VSUM-PHASE") phase.config.minValue =-180.0 phase.config.maxValue = 360.0 phase.config.precision = 3 @@ -28,7 +29,7 @@ phase.initialize() phase0 = phase.read() -kill_camtool() +#kill_camtool() check_camtool() camtool.start("SINBD01-DSCR010") wait_camtool_message() diff --git a/script/test/TestGaussFitOffset.py b/script/test/TestGaussFitOffset.py new file mode 100644 index 0000000..075f25b --- /dev/null +++ b/script/test/TestGaussFitOffset.py @@ -0,0 +1,15 @@ +run("CPython/wrapper") + +x=[-200.30429237268825, -200.2650700434188, -200.22115208318002, -199.9457671375377, -199.86345548879072, -199.85213073174933, -199.35687977133284, -199.13811861090275, -197.97304970346386, -197.2952215624348, -195.09076092936948, -192.92276048970703, -191.96871876227698, -189.49577852322938, -187.9652790409825, -183.63756456925222, -180.04899765472996, -178.43839623242422, -174.07311671294445, -172.0410133577918, -165.90824309893102, -160.99771795989466, -159.30176653939253, -154.27688897558514, -152.0854103810786, -145.75652847587313, -140.80843828908465, -139.23982133191495, -134.27073891256106, -132.12649284133064, -125.95947209775511, -121.00309550337462, -119.26736932643232, -114.2706655484383, -112.07393889578914, -105.72295990367157, -100.8088439880125, -99.2034906238494, -94.30042325164636, -92.15010048151461, -85.92203653534293, -81.03913275494665, -79.27412793784428, -74.33487658582118, -72.06274362408762, -65.76562628131825, -60.91255356825276, -59.20334389560392, -54.33286972659312, -52.19387171350535, -45.94978737932291, -41.03014719193582, -39.301602568238906, -34.35572209014114, -32.04464301272608, -25.8221033382824, -20.922074315528747, -19.21590299233186, -14.31090212502093, -12.217203140101386, -5.9283722049240435, -0.9863587170369246, 0.7408048387279834, 5.71126832601389, 7.972628957879352, 14.204559894256546, 19.11839959633025, 20.8218087836657, 25.678748486941828, 27.822718344586864, 34.062659474970715, 38.9745656819391, 40.77409719734158, 45.72080631619803, 47.974156754056835, 54.23453768983539, 59.12020360609568, 60.77306570712026, 65.70734521458867, 67.8344660434617, 74.03187028154134, 78.96532114824849, 80.76070945985495, 85.74802197591286, 87.9140889204674, 94.18082276873524, 99.25790470037091, 100.68454787413205, 105.7213026221542, 107.79483801526698, 113.99555681638138, 119.0707052529143, 120.72715813056156, 125.77551384921307, 127.91257836719551, 134.2011330887875, 139.23043006997628, 140.71673537840158, 145.76288138835983, 147.80216629676042, 154.06420451405637, 159.0846626604798, 160.76183155710717, 165.73699067536242, 167.9265357747636, 173.96705069576544, 178.2522282751915, 179.9042617354548, 183.54586165856657, 185.23269803071796, 189.41678143751972, 191.87149157986588, 192.8741468985015, 195.0241934550453, 195.966634211846, 197.9821647518146, 198.99006812859284, 199.33202054855676, 199.91897441965887, 200.11536227958896, 200.22280936469997, 200.25181179127208] +y=[11.0, 6.0, 8.0, 5.0, 11.0, 7.0, 18.0, 11.0, 12.0, 10.0, 8.0, 6.0, 16.0, 4.0, 12.0, 9.0, 15.0, 14.0, 8.0, 20.0, 15.0, 8.0, 9.0, 11.0, 13.0, 12.0, 13.0, 15.0, 13.0, 20.0, 10.0, 7.0, 17.0, 11.0, 20.0, 13.0, 13.0, 23.0, 14.0, 10.0, 17.0, 15.0, 20.0, 16.0, 14.0, 13.0, 18.0, 22.0, 9.0, 20.0, 12.0, 14.0, 17.0, 19.0, 14.0, 14.0, 23.0, 19.0, 15.0, 20.0, 20.0, 21.0, 20.0, 23.0, 22.0, 15.0, 10.0, 17.0, 21.0, 15.0, 23.0, 23.0, 25.0, 18.0, 16.0, 21.0, 22.0, 16.0, 16.0, 14.0, 19.0, 20.0, 18.0, 20.0, 23.0, 13.0, 16.0, 20.0, 25.0, 15.0, 15.0, 17.0, 22.0, 26.0, 19.0, 30.0, 25.0, 17.0, 17.0, 23.0, 16.0, 27.0, 21.0, 21.0, 26.0, 27.0, 21.0, 17.0, 20.0, 20.0, 21.0, 19.0, 25.0, 19.0, 13.0, 23.0, 20.0, 20.0, 18.0, 20.0, 19.0, 25.0] + +[off, amp, com, sigma] = gfitoff(x, y, off=None, amp=None, com=None, sigma=None) +print "Fit: ", [off, amp, com, sigma] +from mathutils import Gaussian + +#Plotting results +from mathutils import Gaussian +g = Gaussian(amp, com, sigma) +fit = [g.value(i)+off for i in x] + +plot([y, fit], ["data", "fit"], xdata = x) diff --git a/script/test/TestHarmonicFitOffset.py b/script/test/TestHarmonicFitOffset.py new file mode 100644 index 0000000..14346fb --- /dev/null +++ b/script/test/TestHarmonicFitOffset.py @@ -0,0 +1,9 @@ +run("CPython/wrapper") + +y = [0,2,5,8,15,25, 20, 16,4,-2,-5] +x = [0,10,20,30,40,50, 60, 70, 80, 90, 100] + +r = hfitoff(y, x) +print r + +plot([y,r[6]], ["data", "fit"], [x, r[5]]) diff --git a/script/tmp.py b/script/tmp.py new file mode 100644 index 0000000..581aeeb --- /dev/null +++ b/script/tmp.py @@ -0,0 +1,5 @@ +run("hfitoff_wrapper") +y = [0,2,5,8,15,25, 20, 16,4,-2,-5] +x = [0,10,20,30,40,50, 60, 70, 80, 90, 100] +r = hfitoff(y, x) +plot([y,r[6]], ["data", "fit"], [x, r[5]])