From f664694d1d8f93e875d6ec51d8d0586c3c6e528b Mon Sep 17 00:00:00 2001 From: gobbo_a Date: Thu, 8 Jan 2026 14:25:51 +0100 Subject: [PATCH] Update psss_panel --- .../src/main/pkg/script/psss/Correlation.py | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 psss-panel/src/main/pkg/script/psss/Correlation.py diff --git a/psss-panel/src/main/pkg/script/psss/Correlation.py b/psss-panel/src/main/pkg/script/psss/Correlation.py new file mode 100644 index 0000000..b2d4b6d --- /dev/null +++ b/psss-panel/src/main/pkg/script/psss/Correlation.py @@ -0,0 +1,108 @@ +import math +import sys, traceback +from mathutils import fit_polynomial, PolynomialFunction +from plotutils import plot_line, plot_function +import org.apache.commons.math3.stat.correlation.PearsonsCorrelation as PearsonsCorrelation +import ch.psi.pshell.bs.PipelineServer as PipelineServer + + +TYPE_CHANNEL = 0 +TYPE_STREAM = 1 +TYPE_CAMERA= 2 + +if get_exec_pars().source == CommandSource.ui: + dx = "SARFE10-PSSS059:SPECTRUM_Y_SUM" + dy = "SARFE10-PBPS053:INTENSITY" + window = 40 + offset=-2 + p = plot(None)[0] + linear_fit = True + quadratic_fit = True + +corr = None +pars_lin = None +pars_quad = None + + +for s in p.getAllSeries(): + p.removeSeries(s) + +stream = Stream("correlation_stream", dispatcher) + + +def add_point(x,y): + global marker + sd.appendData(x, y) + if len(sd.x) > window: + #Remove First Element + sd.token.remove(0) + ax = sd.x + ay = sd.y + if len(ax)>2: + x1, x2 = min(ax), max(ax) + res = (x2-x1)/100 + if x1!=x2: + #Display correlation + corr= PearsonsCorrelation().correlation(to_array(ax,'d'), to_array(ay,'d')) + s = "Correlation=" + str(round(corr,4)) + #print s + if get_exec_pars().source == CommandSource.ui: + if marker is not None: + p.removeMarker(marker) + marker = p.addMarker(x2+res, p.AxisId.X, s, p.getBackground()) + marker.setLabelPaint(Color.WHITE) + if linear_fit: + #Calculate, print and plot linear fit + pars_lin = (a0,a1) = fit_polynomial(ay, ax, 1) + #print "Fit lin a1:" , a1, " a0:",a0 + y1 = poly(x1, pars_lin) + y2 = poly(x2, pars_lin) + plot_line(p, x1, y1, x2, y2, width = 2, color = Color.BLUE, name = "Fit Linear") + if quadratic_fit: + #Calculate, print and plot quadratic fit + pars_quad = (a0,a1,a2) = fit_polynomial(ay, ax, 2) + #print "Fit quad a2:" , a2, "a1:" , a1, " a0:",a0 + fitted_quad_function = PolynomialFunction(pars_quad) + ax = frange(x1, x2, res, True) + plot_function(p, fitted_quad_function, "Fit Quadratic", ax, color=Color.GREEN) + + + +try: + stream.addScalar(dx, dx, 1, 0) + stream.addScalar(dy, dy, 1, 0) + stream.initialize() + stream.start(True) + + p.addSeries(LinePlotSeries("Data")) + sd=p.getSeries(0) + sd.setLinesVisible(False) + sd.setPointSize(4) + + stream_buffer = LimitedSizeDict(size_limit=51) + + if get_exec_pars().source == CommandSource.ui: + if globals().has_key("marker"): + p.removeMarker(marker) + marker=None + + while(True): + #Sample and plot data + stream.waitValueNot(stream.take(), 10000) + cache = stream.take() + pid = cache.getPulseId() + x=cache.getValue(dx) + y=cache.getValue(dy) + if offset==0: + add_point(x,y) + else: + stream_buffer[pid] = (x,y) + if offset<0: + if stream_buffer.has_key(pid+offset): + add_point(x,stream_buffer[pid+offset][1]) + else: + if stream_buffer.has_key(pid-offset): + add_point(stream_buffer[pid-offset][0], y) + +finally: + stream.close() \ No newline at end of file