From 738b121749f2cf4713185a2568f2e7cc286f7138 Mon Sep 17 00:00:00 2001 From: Christopher Date: Mon, 28 Nov 2022 11:16:40 +0100 Subject: [PATCH] Added correlation --- bin/psss_panel_editor | 1 - devices/histo_center.properties | 6 +- nb/PSSS/nbproject/project.properties | 4 +- plugins/PSSS.form | 91 +++++++++------------- plugins/PSSS.java | 83 ++++++++------------ script/Lib/builtin_classes.py | 27 +++++-- script/Lib/builtin_functions.py | 17 +++++ script/Lib/builtin_utils.py | 1 + script/Lib/data_utils.py | 76 +++++++++++++++++++ script/Lib/startup_c.py | 15 +++- script/local.py | 20 ++++- script/psss/Correlation.py | 108 +++++++++++++++++++++++++++ script/test/test_corr.py | 5 ++ 13 files changed, 336 insertions(+), 118 deletions(-) create mode 100644 script/Lib/data_utils.py create mode 100644 script/psss/Correlation.py create mode 100644 script/test/test_corr.py diff --git a/bin/psss_panel_editor b/bin/psss_panel_editor index d27b66e..39134e9 100755 --- a/bin/psss_panel_editor +++ b/bin/psss_panel_editor @@ -17,7 +17,6 @@ case $i in esac done - /opt/gfa/pshell/pshell \ -version=v1_17 -last \ -z \ diff --git a/devices/histo_center.properties b/devices/histo_center.properties index b655324..a4c65ac 100755 --- a/devices/histo_center.properties +++ b/devices/histo_center.properties @@ -1,7 +1,7 @@ -#Tue Sep 27 11:58:29 CEST 2022 +#Mon Nov 28 11:10:36 CET 2022 bins=1000 -min=11480.451953125 -max=11580.451953125 +min=12345.1192578125 +max=12445.1192578125 precision=-1 description=null numberOfSamples=10000 diff --git a/nb/PSSS/nbproject/project.properties b/nb/PSSS/nbproject/project.properties index 4cc27ca..605a602 100644 --- a/nb/PSSS/nbproject/project.properties +++ b/nb/PSSS/nbproject/project.properties @@ -35,13 +35,13 @@ dist.jlink.dir=${dist.dir}/jlink dist.jlink.output=${dist.jlink.dir}/PSSS endorsed.classpath= excludes= -file.reference.latest.jar=/opt/gfa/pshell/latest file.reference.psss_panel-plugins=../../plugins file.reference.psss_panel-script=../../script +file.reference.v1_18=/opt/gfa/pshell/v1_18 includes=**/*.java jar.compress=false javac.classpath=\ - ${file.reference.latest.jar} + ${file.reference.v1_18} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false diff --git a/plugins/PSSS.form b/plugins/PSSS.form index 35534ad..429b604 100755 --- a/plugins/PSSS.form +++ b/plugins/PSSS.form @@ -936,7 +936,7 @@ - + @@ -972,29 +972,27 @@ - + - - - - + + - + @@ -1007,30 +1005,26 @@ - - - + - + - - - - - - - - + + + + + + - + @@ -1045,7 +1039,7 @@ - + @@ -1064,30 +1058,12 @@ - - - - - - - - - - - - - - - - - - @@ -1156,29 +1132,36 @@ - + - - - - - - - - + + - + + + + + + + + - - - - + + + + + + + + - + + + diff --git a/plugins/PSSS.java b/plugins/PSSS.java index 2794b8b..cc34560 100755 --- a/plugins/PSSS.java +++ b/plugins/PSSS.java @@ -129,11 +129,9 @@ public class PSSS extends Panel { } } textDevX.setEnabled(state==State.Ready); - textDevY.setEnabled(state==State.Ready); - spinnerInterval.setEnabled(state==State.Ready); + comboDevY.setEnabled(state==State.Ready); spinnerWindow.setEnabled(state==State.Ready); - comboTypeX.setEnabled(state==State.Ready); - comboTypeY.setEnabled(state==State.Ready); + spinnerOffset.setEnabled(state==State.Ready); checkLinear.setEnabled(state==State.Ready); checkQuadratic.setEnabled(state==State.Ready); @@ -324,10 +322,7 @@ public class PSSS extends Panel { jPanel10 = new javax.swing.JPanel(); jLabel20 = new javax.swing.JLabel(); textDevX = new javax.swing.JTextField(); - textDevY = new javax.swing.JTextField(); jLabel21 = new javax.swing.JLabel(); - jLabel22 = new javax.swing.JLabel(); - spinnerInterval = new javax.swing.JSpinner(); jLabel23 = new javax.swing.JLabel(); spinnerWindow = new javax.swing.JSpinner(); buttonStart = new javax.swing.JButton(); @@ -339,8 +334,9 @@ public class PSSS extends Panel { textQuadratic = new javax.swing.JTextField(); checkLinear = new javax.swing.JCheckBox(); checkQuadratic = new javax.swing.JCheckBox(); - comboTypeX = new javax.swing.JComboBox(); - comboTypeY = new javax.swing.JComboBox(); + jLabel27 = new javax.swing.JLabel(); + spinnerOffset = new javax.swing.JSpinner(); + comboDevY = new javax.swing.JComboBox<>(); plotCorrelation = new ch.psi.pshell.plot.LinePlotJFree(); tab.addChangeListener(new javax.swing.event.ChangeListener() { @@ -918,16 +914,9 @@ public class PSSS extends Panel { textDevX.setText("SARFE10-PSSS059:SPECTRUM_Y_SUM"); - textDevY.setText("SARFE10-PBPS053:INTENSITY"); - jLabel21.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel21.setText("Y device:"); - jLabel22.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); - jLabel22.setText("Interval (s):"); - - spinnerInterval.setModel(new javax.swing.SpinnerNumberModel(0.1d, 0.001d, null, 1.0d)); - jLabel23.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel23.setText("Window size:"); @@ -963,11 +952,12 @@ public class PSSS extends Panel { checkQuadratic.setText("Quadratic fit"); - comboTypeX.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Channel", "Stream", "Camera" })); - comboTypeX.setSelectedIndex(1); + jLabel27.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); + jLabel27.setText("Pulse ID Offset:"); - comboTypeY.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Channel", "Stream", "Camera" })); - comboTypeY.setSelectedIndex(1); + spinnerOffset.setModel(new javax.swing.SpinnerNumberModel(0, -50, 50, 1)); + + comboDevY.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "SARFE10-PBPS053:INTENSITY", "SAROP21-PBPS103:INTENSITY", "SAROP11-PBPS110:INTENSITY", "SAROP31-PBPS113:INTENSITY", "SAROP11-PBPS122:INTENSITY", "SAROP21-PBPS133:INTENSITY", "SAROP31-PBPS149:INTENSITY" })); javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10); jPanel10.setLayout(jPanel10Layout); @@ -983,27 +973,28 @@ public class PSSS extends Panel { .addComponent(jLabel26, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel25, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel24, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jLabel22, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel21, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel20, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jLabel23, javax.swing.GroupLayout.Alignment.TRAILING)) + .addComponent(jLabel23, javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(jLabel27, javax.swing.GroupLayout.Alignment.TRAILING)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel10Layout.createSequentialGroup() .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(comboTypeY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(comboTypeX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textCorrelation, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) - .addComponent(spinnerInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(spinnerWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textLinear, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) .addComponent(textQuadratic, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) .addComponent(checkLinear) - .addComponent(checkQuadratic)) + .addComponent(checkQuadratic) + .addComponent(spinnerOffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) .addComponent(textDevX) - .addComponent(textDevY))) + .addComponent(comboDevY, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) ); + + jPanel10Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {spinnerOffset, spinnerWindow}); + jPanel10Layout.setVerticalGroup( jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel10Layout.createSequentialGroup() @@ -1011,27 +1002,23 @@ public class PSSS extends Panel { .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel20) .addComponent(textDevX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(comboTypeX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGap(39, 39, 39) .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel21) - .addComponent(textDevY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGap(3, 3, 3) - .addComponent(comboTypeY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(18, 18, 18) - .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel22) - .addComponent(spinnerInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboDevY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(40, 40, 40) .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel23) .addComponent(spinnerWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel27) + .addComponent(spinnerOffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(checkLinear) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(checkQuadratic) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE) .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) .addComponent(textCorrelation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel24)) @@ -1043,7 +1030,7 @@ public class PSSS extends Panel { .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) .addComponent(jLabel26) .addComponent(textQuadratic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE) .addComponent(buttonStart) .addGap(45, 45, 45)) ); @@ -1058,7 +1045,7 @@ public class PSSS extends Panel { .addContainerGap() .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(plotCorrelation, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)) + .addComponent(plotCorrelation, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)) ); jPanel9Layout.setVerticalGroup( jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -1170,11 +1157,9 @@ public class PSSS extends Panel { textQuadratic.setText(""); HashMap args = new HashMap(); args.put("dx", textDevX.getText()); - args.put("dy", textDevY.getText()); - args.put("interval", spinnerInterval.getValue()); + args.put("dy", comboDevY.getSelectedItem()); args.put("window", spinnerWindow.getValue()); - args.put("dxtype", comboTypeX.getSelectedIndex()); - args.put("dytype", comboTypeY.getSelectedIndex()); + args.put("offset", spinnerOffset.getValue()); args.put("linear_fit", checkLinear.isSelected()); args.put("quadratic_fit", checkQuadratic.isSelected()); @@ -1202,8 +1187,7 @@ public class PSSS extends Panel { private javax.swing.JButton buttonStart; private javax.swing.JCheckBox checkLinear; private javax.swing.JCheckBox checkQuadratic; - private javax.swing.JComboBox comboTypeX; - private javax.swing.JComboBox comboTypeY; + private javax.swing.JComboBox comboDevY; private ch.psi.pshell.swing.DiscretePositionerSelector discretePositionerSelector1; private ch.psi.pshell.swing.DiscretePositionerSelector discretePositionerSelector2; private ch.psi.pshell.swing.HistogramGeneratorPanel histogramGeneratorFwhm; @@ -1223,11 +1207,11 @@ public class PSSS extends Panel { private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel20; private javax.swing.JLabel jLabel21; - private javax.swing.JLabel jLabel22; private javax.swing.JLabel jLabel23; private javax.swing.JLabel jLabel24; private javax.swing.JLabel jLabel25; private javax.swing.JLabel jLabel26; + private javax.swing.JLabel jLabel27; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; @@ -1273,12 +1257,11 @@ public class PSSS extends Panel { private javax.swing.JSpinner spToEn; private javax.swing.JSpinner spinnerAverage; private javax.swing.JSpinner spinnerEnergy; - private javax.swing.JSpinner spinnerInterval; + private javax.swing.JSpinner spinnerOffset; private javax.swing.JSpinner spinnerWindow; private javax.swing.JTabbedPane tab; private javax.swing.JTextField textCorrelation; private javax.swing.JTextField textDevX; - private javax.swing.JTextField textDevY; private javax.swing.JTextField textLinear; private javax.swing.JTextField textQuadratic; // End of variables declaration//GEN-END:variables diff --git a/script/Lib/builtin_classes.py b/script/Lib/builtin_classes.py index 234a51b..f3d467e 100644 --- a/script/Lib/builtin_classes.py +++ b/script/Lib/builtin_classes.py @@ -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 diff --git a/script/Lib/builtin_functions.py b/script/Lib/builtin_functions.py index dd09312..0172620 100644 --- a/script/Lib/builtin_functions.py +++ b/script/Lib/builtin_functions.py @@ -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. diff --git a/script/Lib/builtin_utils.py b/script/Lib/builtin_utils.py index 899497b..80530d8 100644 --- a/script/Lib/builtin_utils.py +++ b/script/Lib/builtin_utils.py @@ -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 diff --git a/script/Lib/data_utils.py b/script/Lib/data_utils.py new file mode 100644 index 0000000..38766ed --- /dev/null +++ b/script/Lib/data_utils.py @@ -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) \ No newline at end of file diff --git a/script/Lib/startup_c.py b/script/Lib/startup_c.py index 1343d04..f5b6b98 100644 --- a/script/Lib/startup_c.py +++ b/script/Lib/startup_c.py @@ -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 diff --git a/script/local.py b/script/local.py index 00d3248..6f2eca1 100755 --- a/script/local.py +++ b/script/local.py @@ -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 \ No newline at end of file + 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) \ No newline at end of file diff --git a/script/psss/Correlation.py b/script/psss/Correlation.py new file mode 100644 index 0000000..b2d4b6d --- /dev/null +++ b/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 diff --git a/script/test/test_corr.py b/script/test/test_corr.py new file mode 100644 index 0000000..fc730fd --- /dev/null +++ b/script/test/test_corr.py @@ -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) \ No newline at end of file