Added correlation

This commit is contained in:
2022-11-28 11:16:40 +01:00
parent 057b1c0d7b
commit 738b121749
13 changed files with 336 additions and 118 deletions
+21 -6
View File
@@ -35,6 +35,7 @@ import ch.psi.pshell.core.ContextAdapter as ContextListener
import ch.psi.pshell.core.Context
import ch.psi.pshell.core.InlineDevice as InlineDevice
import ch.psi.pshell.data.DataManager as DataManager
import ch.psi.pshell.data.DataSlice as DataSlice
import ch.psi.pshell.data.PlotDescriptor as PlotDescriptor
import ch.psi.pshell.data.Table as Table
@@ -45,11 +46,11 @@ import ch.psi.pshell.data.ProviderCSV as ProviderCSV
import ch.psi.pshell.data.ProviderFDA as ProviderFDA
import ch.psi.pshell.data.Converter as DataConverter
import ch.psi.pshell.data.Layout as Layout
import ch.psi.pshell.data.LayoutBase as LayoutBase
import ch.psi.pshell.data.LayoutDefault as LayoutDefault
import ch.psi.pshell.data.LayoutTable as LayoutTable
import ch.psi.pshell.data.LayoutFDA as LayoutFDA
import ch.psi.pshell.data.LayoutSF as LayoutSF
import ch.psi.pshell.data.LayoutBase as LayoutBase
import ch.psi.pshell.data.LayoutDefault as LayoutDefault
import ch.psi.pshell.data.LayoutTable as LayoutTable
import ch.psi.pshell.data.LayoutFDA as LayoutFDA
import ch.psi.pshell.data.LayoutSF as LayoutSF
import ch.psi.pshell.device.Camera as Camera
import ch.psi.pshell.device.CameraBase as CameraBase
@@ -78,6 +79,7 @@ import ch.psi.pshell.device.Register as Register
import ch.psi.pshell.device.Register.RegisterArray as RegisterArray
import ch.psi.pshell.device.Register.RegisterNumber as RegisterNumber
import ch.psi.pshell.device.Register.RegisterBoolean as RegisterBoolean
import ch.psi.pshell.device.Register.RegisterString as RegisterString
import ch.psi.pshell.device.RegisterCache as RegisterCache
import ch.psi.pshell.device.ReadonlyRegister.ReadonlyRegisterArray as ReadonlyRegisterArray
import ch.psi.pshell.device.ReadonlyRegister.ReadonlyRegisterMatrix as ReadonlyRegisterMatrix
@@ -179,6 +181,9 @@ import ch.psi.pshell.imaging.RegisterMatrixSource as RegisterMatrixSource
import ch.psi.pshell.imaging.ImageListener as ImageListener
import ch.psi.pshell.imaging.ImageMeasurement as ImageMeasurement
import ch.psi.pshell.imaging.CameraSource as CameraSource
import ch.psi.pshell.imaging.DeviceSource as DeviceSource
import ch.psi.pshell.imaging.ColormapSource as ColormapSource
import ch.psi.pshell.imaging.StreamSource as StreamSource
import ch.psi.pshell.imaging.ColormapAdapter as ColormapAdapter
import ch.psi.pshell.imaging.FileSource as FileSource
import ch.psi.pshell.imaging.MjpegSource as MjpegSource
@@ -240,6 +245,7 @@ import ch.psi.pshell.bs.Stream as Stream
import ch.psi.pshell.bs.Provider as Provider
import ch.psi.pshell.bs.ProviderConfig.SocketType as SocketType
import ch.psi.pshell.bs.Dispatcher as Dispatcher
import ch.psi.pshell.bs.StreamChannel as StreamChannel
import ch.psi.pshell.bs.Scalar as Scalar
import ch.psi.pshell.bs.Waveform as Waveform
import ch.psi.pshell.bs.Matrix as Matrix
@@ -248,10 +254,19 @@ import ch.psi.pshell.bs.CameraServer as CameraServer
import ch.psi.pshell.bs.PipelineServer as PipelineServer
import ch.psi.pshell.bs.ProviderConfig as ProviderConfig
import ch.psi.pshell.bs.StreamConfig as StreamConfig
import ch.psi.pshell.bs.ScalarConfig as ScalarConfig
import ch.psi.pshell.bs.StreamChannelConfig as StreamChannelConfig
import ch.psi.pshell.bs.WaveformConfig as WaveformConfig
import ch.psi.pshell.bs.MatrixConfig as MatrixConfig
import ch.psi.pshell.camserver.PipelineClient as PipelineClient
import ch.psi.pshell.camserver.CameraClient as CameraClient
import ch.psi.pshell.camserver.ProxyClient as ProxyClient
import ch.psi.pshell.camserver.PipelineStream as PipelineStream
import ch.psi.pshell.camserver.PipelineSource as PipelineSource
import ch.psi.pshell.camserver.CameraStream as CameraStream
import ch.psi.pshell.camserver.CameraSource as CameraSource
import ch.psi.pshell.camserver.CamServerService as CamServerService
import ch.psi.pshell.detector.DetectorConfig as DetectorConfig
import ch.psi.pshell.ui.App as App
+17
View File
@@ -764,6 +764,23 @@ def create_table(path, names, types=None, lengths=None, features=None):
type_classes.append(ScriptUtils.getType(types[i]))
get_context().dataManager.createDataset(path, names, type_classes, lengths, features)
def create_link(path, target_path, target_root=None):
"""Create a internal or external link.
Args:
path(str): Path to link relative to the current persistence context root.
target_path(str): Path to data on the target
target_root(str, optional): If defined creates an external link to the target root.
Otherwise creates an internal link in the current persistence context.
Returns:
None
"""
if target_root:
get_context().dataManager.createLink(path,target_root, target_path)
else:
get_context().dataManager.createLink(path, target_path)
def append_dataset(path, data, index=None, type='d', shape=None):
"""Append data to dataset.
+1
View File
@@ -22,6 +22,7 @@ import java.awt.Point as Point
import java.awt.Dimension as Dimension
import java.awt.Rectangle as Rectangle
import java.awt.Font as Font
import java.io.File as File
import org.python.core.PyArray as PyArray
import org.python.core.PyFunction as PyFunction
import org.python.core.PyMethod as PyMethod
+76
View File
@@ -0,0 +1,76 @@
###################################################################################################
# Data utilities
###################################################################################################
from startup import *
from ijutils import load_array, save_image
def save_as_tiff(data, filename, parallel=True, metadata={}):
"""
Save data as tiff file
Args:
data(array, Data or ImageProcessor): Data to be saved
filename(str): File Name
parallel(bool, optiona): if True image is saved in separated thread.
metadata(dict, optional): written to tiff info property.
"""
def _save_as_tiff(data, filename, metadata={}):
try:
if isinstance(data,PyArray):
ip = load_array(data)
elif type(data) == Data:
ip = load_array(data.matrix)
else:
ip = data
#Metadata
info = "Timestamp: " + time.strftime("%y/%m/%d %H:%M:%S",time.localtime())
for key,val in metadata.items():
info = info + "\n" + str(key) + ": " + str(val)
ip.setProperty("Info", info)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
save_image(ip, filename,"tiff")
except:
print sys.exc_info()
if parallel:
return fork((_save_as_tiff,(data, filename, metadata)),)
else:
_save_as_tiff(data, filename, metadata)
def save_as_hdf5(data, filename=None, dm=None, path="/data", parallel=True, metadata={}):
"""
Save data as hdf5 file
Args:
data(array, Data): Data to be saved
filename(str, optional): File to save to (if equals None then dm must be set )
dm(str, optional): DataManager to save to
parallel(bool, optional): if True image is saved in separated thread.
metadata(dict, optional): written to tiff info property.
"""
def _save_as_hdf5(data, filename, dm, path, metadata={}):
try:
if type(data) == Data:
data = data.matrix
if filename is not None:
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
dm = DataManager(filename, "h5")
try:
dm.setDataset(path, data)
for key,val in metadata.items():
dm.setAttribute(path, key, val)
finally:
if filename is not None:
dm.close()
except:
print sys.exc_info()
if parallel:
return fork((_save_as_hdf5,(data, filename, dm, path, metadata)),)
else:
_save_as_hdf5(data, filename, dm, path, metadata)
+14 -1
View File
@@ -407,6 +407,9 @@ ReadableMatrixSource=RegisterMatrixSource.ReadableMatrixSource
from ch.psi.pshell.imaging import ImageListener as ImageListener
from ch.psi.pshell.imaging import ImageMeasurement as ImageMeasurement
from ch.psi.pshell.imaging import CameraSource as CameraSource
from ch.psi.pshell.imaging import DeviceSource as DeviceSource
from ch.psi.pshell.imaging import ColormapSource as ColormapSource
from ch.psi.pshell.imaging import StreamSource as StreamSource
from ch.psi.pshell.imaging import ColormapAdapter as ColormapAdapter
from ch.psi.pshell.imaging import FileSource as FileSource
from ch.psi.pshell.imaging import MjpegSource as MjpegSource
@@ -454,6 +457,7 @@ from ch.psi.pshell.bs import Stream as Stream
from ch.psi.pshell.bs import Provider as Provider
from ch.psi.pshell.bs import Dispatcher as Dispatcher
from ch.psi.pshell.bs import Scalar as Scalar
from ch.psi.pshell.bs import StreamChannel as StreamChannel
from ch.psi.pshell.bs import Waveform as Waveform
from ch.psi.pshell.bs import Matrix as Matrix
from ch.psi.pshell.bs import StreamCamera as StreamCamera
@@ -461,10 +465,19 @@ from ch.psi.pshell.bs import CameraServer as CameraServer
from ch.psi.pshell.bs import PipelineServer as PipelineServer
from ch.psi.pshell.bs import ProviderConfig as ProviderConfig
from ch.psi.pshell.bs import StreamConfig as StreamConfig
from ch.psi.pshell.bs import ScalarConfig as ScalarConfig
from ch.psi.pshell.bs import StreamChannelConfig as StreamChannelConfig
from ch.psi.pshell.bs import WaveformConfig as WaveformConfig
from ch.psi.pshell.bs import MatrixConfig as MatrixConfig
from ch.psi.pshell.camserver import PipelineClient as PipelineClient
from ch.psi.pshell.camserver import CameraClient as CameraClient
from ch.psi.pshell.camserver import ProxyClient as ProxyClient
from ch.psi.pshell.camserver import CameraSource as CameraSource
from ch.psi.pshell.camserver import CameraStream as CameraStream
from ch.psi.pshell.camserver import PipelineSource as PipelineSource
from ch.psi.pshell.camserver import PipelineStream as PipelineStream
from ch.psi.pshell.camserver import CamServerService as CamServerService
from ch.psi.pshell.detector import DetectorConfig as DetectorConfig
from ch.psi.pshell.ui import App as App
+19 -1
View File
@@ -230,4 +230,22 @@ def elog(title, message, attachments = [], author = None, category = "Info", dom
try:
return int(out[out.find("ID=") +3 : ])
except:
print out
print out
from collections import OrderedDict
class LimitedSizeDict(OrderedDict):
def __init__(self, *args, **kwds):
self.size_limit = kwds.pop("size_limit", None)
OrderedDict.__init__(self, *args, **kwds)
self._check_size_limit()
def __setitem__(self, key, value):
OrderedDict.__setitem__(self, key, value)
self._check_size_limit()
def _check_size_limit(self):
if self.size_limit is not None:
while len(self) > self.size_limit:
self.popitem(last=False)
+108
View File
@@ -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()
+5
View File
@@ -0,0 +1,5 @@
s1 = Stream("s1", dispatcher)
s1.addScalar("SARFE10-PSSS059:SPECTRUM_Y_SUM", "SARFE10-PSSS059:SPECTRUM_Y_SUM", 1, 0)
s1.addScalar("SARFE10-PBPS053:INTENSITY" , "SARFE10-PBPS053:INTENSITY", 1, 0)
s1.initialize()
bscan(s1, 10, 3.0)