This commit is contained in:
1
script/TestScript.py
Normal file
1
script/TestScript.py
Normal file
@@ -0,0 +1 @@
|
||||
tscan(ai1, 10, 1.0)
|
||||
116
script/cpython2.py
Normal file
116
script/cpython2.py
Normal file
@@ -0,0 +1,116 @@
|
||||
###################################################################################################
|
||||
# This moddule is called by demo scripts to execute and embed CPython.
|
||||
# Must be put in the scripts folder, or else in the python path.
|
||||
###################################################################################################
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
#import matplotlib
|
||||
#matplotlib.use('TkAgg')
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
#try:
|
||||
# import tkinter as tk
|
||||
#except:
|
||||
# import Tkinter as tk
|
||||
|
||||
|
||||
def calc(array):
|
||||
return np.transpose(array + array)
|
||||
|
||||
|
||||
def test_pandas():
|
||||
s = pd.Series([1,3,5,np.nan,6,8])
|
||||
print (s)
|
||||
dates = pd.date_range('20130101', periods=6)
|
||||
print (dates)
|
||||
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
|
||||
print (df)
|
||||
df2 = pd.DataFrame({ 'A' : 1.,
|
||||
'B' : pd.Timestamp('20130102'),
|
||||
'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
|
||||
'D' : np.array([3] * 4,dtype='int32'),
|
||||
'E' : pd.Categorical(["test","train","test","train"]),
|
||||
'F' : 'foo' })
|
||||
print (df2)
|
||||
print (df2.dtypes)
|
||||
print (df.head())
|
||||
print (df.tail(3))
|
||||
print (df.values)
|
||||
print (df.describe())
|
||||
print (df.T)
|
||||
print (df.sort_index(axis=1, ascending=False))
|
||||
#print (df.sort_values(by='B'))
|
||||
print (df['A'])
|
||||
print (df[0:3])
|
||||
print (df.mean())
|
||||
return str(df.mean())
|
||||
|
||||
|
||||
def test_tkinter():
|
||||
root = tk.Tk()
|
||||
listb = tk.Listbox(root)
|
||||
for item in ["Hello", "World"]:
|
||||
listb.insert(0,item)
|
||||
listb.pack()
|
||||
root.mainloop()
|
||||
|
||||
|
||||
def test_matplotlib(start,stop,step):
|
||||
import threading
|
||||
x = np.arange(start,stop,step)
|
||||
y = np.exp(-x)
|
||||
|
||||
# example variable error bar values
|
||||
yerr = 0.1 + 0.2*np.sqrt(x)
|
||||
xerr = 0.1 + yerr
|
||||
|
||||
# First illustrate basic pyplot interface, using defaults where possible.
|
||||
plt.figure()
|
||||
plt.errorbar(x, y, xerr=0.2, yerr=0.4)
|
||||
plt.title("Simplest errorbars, 0.2 in x, 0.4 in y")
|
||||
|
||||
# Now switch to a more OO interface to exercise more features.
|
||||
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
|
||||
ax = axs[0,0]
|
||||
ax.errorbar(x, y, yerr=yerr, fmt='o')
|
||||
ax.set_title('Vert. symmetric')
|
||||
|
||||
# With 4 subplots, reduce the number of axis ticks to avoid crowding.
|
||||
ax.locator_params(nbins=4)
|
||||
|
||||
ax = axs[0,1]
|
||||
ax.errorbar(x, y, xerr=xerr, fmt='o')
|
||||
ax.set_title('Hor. symmetric')
|
||||
|
||||
ax = axs[1,0]
|
||||
ax.errorbar(x, y, yerr=[yerr, 2*yerr], xerr=[xerr, 2*xerr], fmt='--o')
|
||||
ax.set_title('H, V asymmetric')
|
||||
|
||||
ax = axs[1,1]
|
||||
ax.set_yscale('log')
|
||||
# Here we have to be careful to keep all y values positive:
|
||||
ylower = np.maximum(1e-2, y - yerr)
|
||||
yerr_lower = y - ylower
|
||||
|
||||
ax.errorbar(x, y, yerr=[yerr_lower, 2*yerr], xerr=xerr,
|
||||
fmt='o', ecolor='g', capthick=2)
|
||||
ax.set_title('Mixed sym., log y')
|
||||
|
||||
fig.suptitle('Variable errorbars')
|
||||
|
||||
plt.show()
|
||||
return [start,stop,step]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
x = np.arange(0, 5, 0.1)
|
||||
y = np.sin(x)
|
||||
plt.plot(x, y)
|
||||
plt.show()
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
1/0
|
||||
def test():
|
||||
###
|
||||
pass
|
||||
82
script/test_camserver.py
Normal file
82
script/test_camserver.py
Normal file
@@ -0,0 +1,82 @@
|
||||
#START AS: pshell console -dev -v -k -b -g -t test_camserver,1,5 -r false (-cl FINE)(--debug -y)
|
||||
import ch.psi.pshell.screenpanel.CamServerViewer as CamServerViewer
|
||||
import traceback
|
||||
|
||||
SHOW_RENDERER=False
|
||||
CAMERA="simulation"
|
||||
CHANNEL_IMG = "TEST:FPICTURE"
|
||||
CHANNEL_WIDTH = "TEST:WIDTH"
|
||||
CHANNEL_HEIGHT = "TEST:HEIGHT"
|
||||
INTERVAL = 1.0
|
||||
SHOW_PROFILE = False
|
||||
SHOW_FIT = True
|
||||
#IMAGE_TYPE = PV.Type.SHORT
|
||||
IMAGE_TYPE = PV.Type.BYTE
|
||||
#IMAGE_SIZE = Dimension(500,500)
|
||||
#IMAGE_SIZE = Dimension(255,255)
|
||||
IMAGE_SIZE = Dimension(300,218)
|
||||
MAX_IMAGE_SIZE = 65500 if PV.Type.BYTE else 65500/2
|
||||
DEBUG = (not Setup.isServerMode()) or Setup.isDebug()
|
||||
PLOT = DEBUG
|
||||
|
||||
img = PV.waveform(CHANNEL_IMG, IMAGE_TYPE, MAX_IMAGE_SIZE)
|
||||
width = PV.scalar(CHANNEL_WIDTH, PV.Type.INT)
|
||||
height = PV.scalar(CHANNEL_HEIGHT, PV.Type.INT)
|
||||
|
||||
viewer = CamServerViewer()
|
||||
viewer.applyOptions();
|
||||
viewer.initialize(CamServerViewer.SourceSelecionMode.Single);
|
||||
viewer.setStream(CAMERA)
|
||||
viewer.camera.config.colormap=Colormap.Flame
|
||||
viewer.camera.config.colormapAutomatic = True
|
||||
|
||||
viewer.setShowProfile(SHOW_PROFILE)
|
||||
viewer.setShowFit(SHOW_FIT)
|
||||
r=viewer.renderer
|
||||
r.setPenProfile(Pen(Color.GRAY, 0))
|
||||
viewer.setPenFit(Pen(Color.WHITE, 0))
|
||||
viewer.setPenCross(Pen(Color.WHITE, 0))
|
||||
|
||||
if SHOW_RENDERER:
|
||||
f=SwingUtils.showFrame(App.getMainFrame(), "CamServerViewer", Dimension(450,250), r)
|
||||
f.setAlwaysOnTop(True)
|
||||
else:
|
||||
r.device.addListener(r)
|
||||
p=None
|
||||
|
||||
try:
|
||||
r.waitImage(10000)
|
||||
while True:
|
||||
i=r.getImage(True, IMAGE_SIZE)
|
||||
i=ImagingUtils.grayscale(i)
|
||||
d = Data(i)
|
||||
a = d.array
|
||||
if IMAGE_TYPE != PV.Type.BYTE:
|
||||
a=Convert.toUnsigned(a)
|
||||
img.write(a)
|
||||
width.write(d.width)
|
||||
height.write(d.height)
|
||||
if PLOT:
|
||||
matrix = Convert.reshape(a, d.height, d.width)
|
||||
if IMAGE_TYPE == PV.Type.BYTE:
|
||||
matrix=Convert.toUnsigned(matrix)
|
||||
#p=plot(matrix)[0]
|
||||
if p is None or (s.numberOfBinsX!=d.width) []or (s.numberOfBinsY!=d.height):
|
||||
p=plot(matrix)[0]
|
||||
p.setColormap(Colormap.Grayscale)
|
||||
p.getAxis(p.AxisId.Y).inverted=True
|
||||
s=p.getSeries(0)
|
||||
else:
|
||||
s.setData(Convert.toDouble(matrix))
|
||||
if INTERVAL:
|
||||
time.sleep(INTERVAL)
|
||||
r.waitNext(10000) #If stops receiving, tries restarting
|
||||
except:
|
||||
width.write(0)
|
||||
height.write(0)
|
||||
if DEBUG:
|
||||
print "Camera image processing exception:"
|
||||
print sys.exc_info()[1]
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
viewer.setStream(None)
|
||||
90
script/test_channels.py
Normal file
90
script/test_channels.py
Normal file
@@ -0,0 +1,90 @@
|
||||
if not "_pvs" in globals():
|
||||
_pvs = {}
|
||||
|
||||
def get_waveform_pv(name, typ='double', size=None):
|
||||
if name not in _pvs:
|
||||
class Waveform(RegisterBase, RegisterArray):
|
||||
def __init__(self, name):
|
||||
RegisterBase.__init__(self, name)
|
||||
self.fixed_size = None
|
||||
self.clear()
|
||||
|
||||
def clear(self):
|
||||
self.val = None
|
||||
#if self.fixed_size:
|
||||
# self.val = [float("nan")] * self.fixed_size
|
||||
#else:
|
||||
# self.val = [float("nan")]
|
||||
|
||||
def doRead(self):
|
||||
return self.val
|
||||
|
||||
def doWrite(self, val):
|
||||
if val is None or len(val) == 0:
|
||||
self.clear()
|
||||
elif self.fixed_size:
|
||||
self.clear()
|
||||
del val[self.fixed_size:]
|
||||
self.val[:len(val)] = val
|
||||
else:
|
||||
self.val = val
|
||||
print self.val
|
||||
|
||||
def getSize(self):
|
||||
return len(self.val)
|
||||
|
||||
def fixSize(self, size):
|
||||
self.fixed_size = size
|
||||
self.clear()
|
||||
|
||||
waveform = Waveform(name)
|
||||
#waveform.fixSize(size)
|
||||
waveform.initialize()
|
||||
cas = CAS(name, waveform, typ)
|
||||
_pvs[name] = [waveform, cas]
|
||||
return _pvs[name][0]
|
||||
|
||||
|
||||
def get_scalar_pv(name, typ='double', size=None):
|
||||
if name not in _pvs:
|
||||
class Scalar(RegisterBase):
|
||||
def __init__(self, name):
|
||||
RegisterBase.__init__(self, name)
|
||||
self.fixed_size = None
|
||||
self.clear()
|
||||
|
||||
def clear(self):
|
||||
self.val = float("nan")
|
||||
|
||||
def doRead(self):
|
||||
return self.val
|
||||
|
||||
def doWrite(self, val):
|
||||
if val is None:
|
||||
self.clear()
|
||||
else:
|
||||
self.val = val
|
||||
|
||||
scalar = Scalar(name)
|
||||
scalar.initialize()
|
||||
cas = CAS(name, scalar, typ)
|
||||
_pvs[name] = [scalar, cas]
|
||||
return _pvs[name][0]
|
||||
|
||||
|
||||
pv = get_waveform_pv("TEST_CHANNELS:ai1","double")
|
||||
sc = get_scalar_pv("TEST_CHANNELS:ai2","double")
|
||||
|
||||
|
||||
def after_read(record, scan):
|
||||
#print scan.result[ai1] + [record[ai1],]
|
||||
pv.write(scan.result[ai1])
|
||||
sc.write(record.index)
|
||||
|
||||
|
||||
r= tscan((ai1, ai1), 5, 0.1, after_read=after_read, save=False)
|
||||
pv.write(r[ai1])
|
||||
sc.write(len(r))
|
||||
|
||||
print pv.read()
|
||||
print sc.read()
|
||||
22
script/test_channels2.py
Normal file
22
script/test_channels2.py
Normal file
@@ -0,0 +1,22 @@
|
||||
wf = PV.waveform("TEST:WAVEFORM", PV.Type.DOUBLE, 5)
|
||||
sc = PV.scalar("TEST:SCALAR", PV.Type.DOUBLE)
|
||||
st = PV.scalar("TEST:STATE", PV.Type.STRING)
|
||||
|
||||
if st.read()=="Busy":
|
||||
print "Ongoing operation"
|
||||
raise Exception ("Ongoing operation")
|
||||
st.write("Busy")
|
||||
|
||||
def after_read(record, scan):
|
||||
#print scan.result[ai1] + [record[ai1],]
|
||||
wf.write(scan.result[ai1])
|
||||
sc.write(record.index)
|
||||
|
||||
try:
|
||||
r= tscan((ai1, ai1), 5, 1.5, after_read=after_read, save=False)
|
||||
wf.write(r[ai1])
|
||||
sc.write(len(r))
|
||||
print wf.read()
|
||||
print sc.read()
|
||||
finally:
|
||||
st.write("Ready")
|
||||
@@ -2,11 +2,12 @@
|
||||
data2d = [ [1.0, 2.0, 3.0, 4.0, 5.0], [2.0, 3.0, 4.0, 5.0, 6.0, ], [3.0, 4.0, 5.0, 6.0, 7.0]]
|
||||
|
||||
|
||||
path = "group/data4"
|
||||
path = "group/data4"+ str(time.time())
|
||||
create_dataset(path, 'd', False, (0,0))
|
||||
for row in data2d:
|
||||
append_dataset(path, row)
|
||||
read =load_data(path)
|
||||
print read.tolist()
|
||||
plot(read)
|
||||
run("test_sleep")
|
||||
set_return(True)
|
||||
@@ -3,7 +3,7 @@ a = [1,2,3]
|
||||
d = to_array(a)
|
||||
l=to_list(d)
|
||||
|
||||
print (a)
|
||||
print (d)
|
||||
print (l)
|
||||
print (type(a)==type(l))
|
||||
print (a==l)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
print "->START"
|
||||
run("test_data_txt")
|
||||
time.sleep(0.5)
|
||||
run("test_data_txt")
|
||||
@@ -7,4 +8,5 @@ time.sleep(0.5)
|
||||
run("test_data_txt")
|
||||
time.sleep(0.5)
|
||||
run("test_data_txt")
|
||||
set_return(True)
|
||||
print "->FINISH"
|
||||
set_return(True)
|
||||
|
||||
3
script/test_plot_type.py
Normal file
3
script/test_plot_type.py
Normal file
@@ -0,0 +1,3 @@
|
||||
ascan([m1,m2], [ai1], [0.0, 0.0], [1.0, 1.0], [2,4], latency=0.1,zigzag=True, plot_types={"ai1":1}, passes=2)
|
||||
lscan(m1, wf1, 0.0, 1.0,5, latency=0.2, plot_types={"wf1":1})
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
print "Sleeping"
|
||||
sleep(1.0)
|
||||
print "Done"
|
||||
64
script/test_snapshot.py
Normal file
64
script/test_snapshot.py
Normal file
@@ -0,0 +1,64 @@
|
||||
###################################################################################################
|
||||
# Use of Snapshot class to read and restore the state of devices
|
||||
###################################################################################################
|
||||
|
||||
#Initial state
|
||||
ao1.write(1.0)
|
||||
ao2.write(2.0)
|
||||
|
||||
#Snapshot creation: list of read-writable devices and an optional name.
|
||||
#The name is only needed for saving/loading from files, so multiple different snapshots can be saved at a time.
|
||||
#If ommited then name is set to "default", and it will overrite other snapshotws when saved.
|
||||
s=Snapshot([ao1, ao2], "snapshot1")
|
||||
|
||||
#Take snapshot
|
||||
errors = s.take(Snapshot.Mode.PARALLEL)
|
||||
if len(errors)>0: #take() returns return errors as a dict device -> exception. If empty then all devices were successfully read.
|
||||
device, e = errors.keys()[0], errors[device]
|
||||
raise Exception("Error taking " + device.name + " - " + str(e.message))
|
||||
#Do stuff
|
||||
ao1.write(4.0)
|
||||
ao2.write(5.0)
|
||||
time.sleep(2.0)
|
||||
|
||||
#Restore
|
||||
errors = s.restore(Snapshot.Mode.PARALLEL)
|
||||
if len(errors)>0: #restore() returns errors as a dict device -> exception. If empty then all devices were successfully restored.
|
||||
device, e = errors.keys()[0], errors[device]
|
||||
raise Exception("Error restoring " + device.name + " - " + str(e))
|
||||
|
||||
#Mode for take and restore can be:
|
||||
#Snapshot.Mode.PARALLEL: all the devices are accessed in parallel, returning all errors.
|
||||
#Snapshot.Mode.SERIES: all the devices are accessed sequentially, returning all errors.
|
||||
#Snapshot.Mode.STOP_ON_ERROR: all the devices are access sequentially, but stops upon the first error.
|
||||
|
||||
#Saving and loading from files
|
||||
errors = s.take(Snapshot.Mode.PARALLEL)
|
||||
if len(errors)>0:
|
||||
raise errors[0]
|
||||
timestamp = s.save()
|
||||
|
||||
#Do stuff
|
||||
ao1.write(8.0)
|
||||
ao2.write(9.0)
|
||||
time.sleep(2.0)
|
||||
|
||||
#If you had still the reference to s when loading, you could use it to load
|
||||
s.load(timestamp) #Loads the snapshot from that timestamp, or the most recent if timestamp is ommited
|
||||
s.restore()
|
||||
if len(errors)>0:
|
||||
raise errors.values(0)
|
||||
|
||||
ao1.write(6.0)
|
||||
ao2.write(4.0)
|
||||
time.sleep(2.0)
|
||||
|
||||
#If you must load from file in a different script, then you must create a snapshot having the same name and set of devices.
|
||||
|
||||
s2=Snapshot([ao1, ao2], "snapshot1")
|
||||
s2.load() #Loads the last snapshot sved
|
||||
s2.restore()
|
||||
if len(errors)>0:
|
||||
raise errors.values(0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user