This commit is contained in:
2024-06-10 10:44:16 +02:00
parent 9256184430
commit 3093557c99
79 changed files with 2747 additions and 189 deletions
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
#BYTE waveform: OK
#BYTE waveform: OK
# field(FTVL, 'BYTE')
caput("TESTIOC:TESTBA:MyBA", "foo")
print caget("TESTIOC:TESTBA:MyBA")
+64
View File
@@ -0,0 +1,64 @@
# Run detached as:
# pshell -p=DemoScan.java -d -sbar -tbar -l -dplt
from mathutils import fit_harmonic, HarmonicOscillator
if get_exec_pars().innerArgs is None:
print 1
START = 0.0
END = 0.400
STEPS = 10
PLOT = None
if PLOT is None:
PLOT = plot(None,title="HFit")[0]
PLOT.clear()
sd = lscan(inp, (sin,out,arr), START, END, STEPS, 0.1)
ydata = sd[sin]
xdata = sd[inp]
max_y= max(ydata)
index_max = ydata.index(max_y)
max_x= xdata[index_max]
start,end = min(xdata), max(xdata)
(amplitude, angular_frequency, phase) = fit_harmonic(ydata, xdata)
fitted_harmonic_function = HarmonicOscillator(amplitude, angular_frequency, phase)
print "amplitude = ", amplitude
print "angular frequency = ", angular_frequency
print "phase = ", phase
f = angular_frequency/ (2* math.pi)
print "frequency = ", f
resolution = 0.01
fit_y = []
for x in frange(start,end,resolution, True):
fit_y.append(fitted_harmonic_function.value(x))
fit_x = frange(start, end+resolution, resolution)
PLOT.addSeries(LinePlotSeries("data"))
PLOT.addSeries(LinePlotSeries("fit"))
PLOT.getSeries(0).setData(xdata, ydata)
PLOT.getSeries(1).setData(fit_x, fit_y)
#m = (phase + math.pi)/ angular_frequency
m = -phase / angular_frequency
if (m<start):
m+=(1.0/f)
if start <= m <=end:
print "fit = ", m
PLOT.addMarker(m, None, "Fit="+str(round(m ,2)), Color.MAGENTA.darker())
valid_fit = True
else:
print "max = ",max_x
PLOT.addMarker(max_x, None, "Max="+str(round(max_x ,2)), Color.MAGENTA.darker())
valid_fit = False
set_return([amplitude, angular_frequency, phase, valid_fit, m])
+102
View File
@@ -0,0 +1,102 @@
DETS = (sin,arr, image.getDataMatrix())
s1 = tscan(DETS, 10, 0.1)
s2 = lscan(motor, DETS, -2.0, 2.0, 10, latency=0.2, relative=True, print_scan=True)
print s2[motor]
print s2["sin"]
s3 = lscan(pe, DETS, 0.0, 10.0, 10, latency=0.1, passes=4, zigzag=True)
s4 = ascan((motor,pe), DETS, (0.0,0.0), (2.0,1.0), (8,8), latency=0.1, zigzag=True)
vector = [ 1, 3, 5, 10, 25, 40, 45, 47, 49]
s5 = vscan(pe, DETS, vector, False, latency=0.5, print_scan=True)
def spiral_gen(radius, step, resolution=.1, angle=0.0, origin=[0.0, 0.0]):
d = 0.0
while d * math.hypot(math.cos(angle), math.sin(angle)) < radius:
pos=[ origin[0] + d * math.cos(angle), \
origin[1] + d * math.sin(angle) ]
yield(pos)
d+=step
angle+=resolution
s6 = vscan((pe,pe2),(sin),spiral_gen(10, 0.1), latency=0.1, range = [-10.0,10.0, -10.0, 10.0])
s7 = rscan(pe, DETS, [(0,5,5), (10,15,20), (20,25,5)] , 0.1)
s8 = cscan(motor, DETS, -2.0, 2.0 , steps=10, relative=True)
motor.moveRelAsync(3.0)
mscan(motor.readback, DETS, -1, 2.0, async=False)
#Scan Callbacks
def before_read(position, scan):
outi.write(1)
outi.write(0)
print "In position: " + str(position[0]) + ", " + str(position[1])
def after_read(rec, scan):
print "Aquired frame %i: at %f, %f: %f" % (rec.index, rec[pe], rec["pe2"], rec[DETS[0]])
s9= lscan((pe,pe2), (DETS), (0,0), (4,8), steps=20, latency = 0.01, before_read=before_read, after_read=after_read)
#Setting attributes to the scan group
path = get_exec_pars().scanPath
set_attribute(path, "AttrString", "Value")
set_attribute(path, "AttrInteger", 1)
set_attribute(path, "AttrDouble", 2.0)
set_attribute(path, "AttrBoolean", True)
#Manipulating sampled data
s10= lscan(pe, DETS, 0, 40, 40, 0.01, False)
s11= lscan(pe, DETS, 0, 40, 40, 0.01, False)
from operator import add
result = map(add, s10[sin], s11[sin])
plot(result)
#Pseudo-devices
class Clock(Readable):
def read(self):
return time.clock()
class PseudoSensor(Readable):
def read(self):
v = arr.take() #Gets the CACHED waveform
return reduce(lambda x, y: x + y, v) / len(v)
class PseudoPositioner(Writable):
def write(self,pos):
print "Step = " + str(pos)
clock=Clock()
averager=PseudoSensor()
positioner=PseudoPositioner()
s12 = lscan(positioner,(sin, averager,clock),0, 40,20,0.1)
#Resampling
index=0
def after_read(rec):
if rec[pe2] < 0:
time.sleep(1.0)
rec.invalidate()
s13 = lscan(pe, DETS+(pe2,), 0.0, 0.4, 10, 1.0, after_read=after_read)
#Averaging
av = create_averager(sin, 3, 0.1)
res= lscan(pe, (av, av.samples), 0, 20, 10)
+24
View File
@@ -0,0 +1,24 @@
DETECTORS = (sin,det.dataMatrix)
#lscan(m1, DETECTORS, 1, 3, 10, before_read=trigger_detector)
#Pseudo motor for my motor functions, e.g. move_sim_x
#Detector = PEEMCAM
class MyMotor(Writable):
def write(self, pos):
#m1.move(pos)
move_sim_x(pos)
my_motor=MyMotor()
vector = [ [1,1] , [1,2] , [1,3] , [1,4] ,
[1.5,2.5] ,
[2,1] , [2,2] , [2,3] , [2,4] ,
[2.5,2.5] ,
[3,1] , [3,2] , [3,3] , [3,4] ]
r3 = vscan((my_motor,m2),DETECTORS, vector, False, 0.1, title = "2D Vector")
plot(r3[sin])
+3
View File
@@ -0,0 +1,3 @@
tscan(Ophyd(det1), 10, 0.1)
+33
View File
@@ -0,0 +1,33 @@
class DeviceListener():
def __init__(self, name=None):
self.name = name
self.proxy=jproxy(self, ['ch.psi.pshell.device.DeviceListener'])
def get_proxy(self):
return self.proxy
def onStateChanged(self, device, state, former):
raise Exception ("Not implemented")
def onValueChanged(self, device, value, former):
raise Exception ("Not implemented")
def onValueChanging(self, device, value, former):
raise Exception ("Not implemented")
def onCacheChanged(self, device, value, former, timestamp, valueChange):
raise Exception ("Not implemented")
class Listener (DeviceListener):
def __init__(self):
DeviceListener.__init__(self)
def onValueChanged(self, device, value, former):
print (value)
listener = DeviceListener()
sin.addListener(listener)
sin.removeListener(listener)
+29
View File
@@ -0,0 +1,29 @@
print ("Starting...")
import matplotlib
from ch.psi.pshell.imaging import Utils as ImagingUtils
#matplotlib.use('TkAgg')
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
f = expand_path("{images}/output.png")
plt.savefig(f)
img = ImagingUtils.newImage(f)
show_panel(img, "tst")
print ("Done")
+94
View File
@@ -0,0 +1,94 @@
class ReaderWrapper():
def __init__(self, dev):
self.dev=dev
self.name=dev.getName()
self.parent = None
try:
self.source = self.dev.getChannelName()
except:
self.source = "Unknown"
try:
try:
self.shape = [self.dev.getHeight(), self.dev.getWidth()]
self.shape_str = "["+str(self.shape[0])+"]"+"["+str(self.shape[1])+"]"
except:
self.shape = [self.dev.getSize()]
self.shape_str = "["+str(self.shape[0])+"]"
except:
self.shape = []
self.shape_str=""
try:
self.precision = self.dev.getPrecision()
except:
self.precision = None
try:
v = self.dev.take()
if v is None: raise Exception("")
except:
v = self.dev.read()
if type(v) is str:
self.dtype = 'string'
elif type(v) is int:
self.dtype = 'integer'
elif type(v) is bool:
self.dtype = 'boolean'
else:
#try:
# len(v)
# self.dtype = 'array'
#except:
# self.dtype = 'number'
self.dtype = 'number'
self.description = {self.name: { \
'dtype':self.dtype , \
'shape': self.shape, \
'source': self.source, \
'precision': self.precision \
}}
self.cfg_description = {"shape_str":{"dtype":"string", 'shape':(len(self.shape_str),), "source":""}, }
self.configuration = {"shape_str":{"value":self.shape_str, "timestamp":time.time()}}
self.name = self.name+self.shape_str
def read(self):
if is_main_thread():
value = self.dev.read()
else:
global __return__,__exception__
__return__ = __exception__ = None
handler.queue.append(("read", (self.dev)))
while (__return__ is None) and (__exception__ is None):
time.sleep(0.01)
if __exception__ is not None:
raise __exception__
value=__return__
if True: #Lself.dtype == "array":
value = numpy.array(value)
return {self.name:{"value":value, "timestamp":time.time()}}
def describe(self):
self.description = {self.name: { \
'dtype':self.dtype , \
'shape': self.shape, \
'source': self.source, \
'precision': self.precision \
}}
return self.description
def describe_configuration(self):
#print(3)
return self.cfg_description
def read_configuration(self):
#print(4)
return self.configuration
det4=ReaderWrapper(get_device("arr"))
dets = [det4,]
RE(count(dets, num=5, delay=0.5))
+6
View File
@@ -0,0 +1,6 @@
add_device(Stream("st1", dispatcher), True)
st1.start()
show_panel(st1)
+97
View File
@@ -0,0 +1,97 @@
def to_array(obj, type = None, primitive = True):
"""Convert Python list to Java array.
Args:
obj(list): Original data.
type(str): array type 'b' = byte, 'h' = short, 'i' = int, 'l' = long, 'f' = float, 'd' = double,
'c' = char, 'z' = boolean, 's' = String, 'o' = Object
Returns:
Java array.
"""
if obj is None:
return None
if type is None:
type = 'o'
enforceArrayType=False
else:
enforceArrayType=True
if type[0] == '[':
type = type[1:]
element_type = ScriptUtils.getPrimitiveType(type) if primitive else ScriptUtils.getType(type)
def convert_1d_array(obj):
if type == 'c':
ret = reflect.Array.newInstance(element_type,len(obj))
for i in range(len(obj)): ret[i] = chr(obj[i])
return ret
if type == 'z':
ret = reflect.Array.newInstance(element_type,len(obj))
for i in range(len(obj)):
ret[i]= True if obj[i] else False
return ret
if type == 'o':
ret = reflect.Array.newInstance(element_type,len(obj))
for i in range(len(obj)):
ret[i]= obj[i]
return ret
if type == "s":
return Convert.toStringArray(obj)
if primitive:
ret = Convert.wrapperArrayToPrimitiveArray(obj, element_type)
else:
ret = reflect.Array.newInstance(element_type,len(obj))
for i in range(len(obj)): ret[i] = Convert.toType(obj[i],element_type)
return ret
if is_array(obj):
if enforceArrayType:
if Arr.getComponentType(obj) != element_type:
rank = Arr.getRank(obj)
if (rank== 1):
obj=convert_1d_array(obj)
elif (rank>1):
pars, aux = [element_type], obj
for i in range(rank):
pars.append(len(aux))
aux = aux[0]
#TODO: OVERLOADING BUG
#ret = reflect.Array.newInstance(*pars)
ret = Arr.newInstance(*pars)
for i in range(len(obj)):
ret[i]=to_array(obj[i], type)
obj = ret
elif is_list(obj):
if type=='o':
ret = reflect.Array.newInstance(element_type, len(obj))
for i in range (len(obj)):
if is_list(obj[i]) or is_array(obj[i]):
ret[i] = to_array(obj[i],type)
else:
ret[i] = obj[i]
obj=ret
elif len(obj)>0 and (is_list(obj[0]) or is_array(obj[0])):
pars, aux = [element_type], obj
while len(aux)>0 and (is_list(aux[0]) or is_array(aux[0])):
pars.append(len(aux))
aux = aux[0]
pars.append(0)
#ret = reflect.Array.newInstance(*pars)
ret = Arr.newInstance(*pars)
for i in range(len(obj)):
ret[i]=to_array(obj[i], type)
obj=ret
else:
obj= convert_1d_array(obj)
return obj
import random
ret = []
for i in range (10):
ret.append(random.random())
try:
a1 = to_array(ret,'d')
print (a1)
except:
traceback.print_exc()
+8 -4
View File
@@ -9,12 +9,18 @@ motor.delay = 1.1 # simulate slow motor movement
ch1 = EpicsSignal("TESTIOC:TESTSINUS:SinCalc")
#TODO: DEmonstrate use of waveform and areadetector (Manual scan setup with the indexes in name)
#TODO: Demonstrate use of waveform and areadetector (Manual scan setup with the indexes in name)
#ch2 = EpicsSignal("TESTIOC:TESTWF2:MyWF", name="arr[10]")
#ch3 = EpicsSignal("TESTIOC:TESTWF2:MyWF", name="img[3][2]")det3=ReaderWrapper(sin)
"""
dets = [det1, det2]
det4=ReaderWrapper(arr)
det5=ReaderWrapper(get_device("det").getDataMatrix() )
dets = [det1, det2, ch1, det3, det4, det5] #TODO: fix formatting for arrays
dets = [det1, det2, ch1, det3]
RE(count(dets, num=5, delay=0.5))
RE(scan(dets, motor, 0, 1, 5))
RE(rel_scan(dets, motor, -1, 1, 10))
@@ -23,8 +29,6 @@ RE(grid_scan(dets, motor1, -1.5, 1.5, 3, motor2, -0.1, 0.1, 5))
RE(list_grid_scan(dets, motor1, [1, 1, 2, 3, 5], motor2, [25, 16, 9]))
det4=ReaderWrapper(arr)
det5=ReaderWrapper(get_device("det").getDataMatrix() )
m1=MoverWrapper(get_device("motor"))
dets = [det1, det2, ch1, det3, det4, det5]
RE(scan(dets, m1, 0, 1, 5))
+29 -11
View File
@@ -178,9 +178,7 @@ def on_abort(parent_thread):
print ("Abort command: Run Engine pause request")
RE.request_pause()
return
tid=parent_thread.ident
exception = KeyboardInterrupt
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exception))
_default_abort(parent_thread)
def on_close(parent_thread):
on_abort(parent_thread)
@@ -399,13 +397,7 @@ class ReaderWrapper():
self.precision = self.dev.getPrecision()
except:
self.precision = None
self.description = {self.name: { \
'dtype':'number', \
'shape': self.shape, \
'source': self.source, \
'precision': self.precision \
}}
self.dtype = 'number'
self.cfg_description = {"shape_str":{"dtype":"string", 'shape':(len(self.shape_str),), "source":""}, }
self.configuration = {"shape_str":{"value":self.shape_str, "timestamp":time.time()}}
self.name = self.name+self.shape_str
@@ -422,7 +414,13 @@ class ReaderWrapper():
raise __exception__
return {self.name:{"value":__return__, "timestamp":time.time()}}
def describe(self):
def describe(self):
self.description = {self.name: { \
'dtype':self.dtype , \
'shape': self.shape, \
'source': self.source, \
'precision': self.precision \
}}
return self.description
def describe_configuration(self):
@@ -470,6 +468,26 @@ class MoverWrapper(ReaderWrapper):
self.dev.stop()
#Wraps an Ophyd device as a Readable, so can be used in *scan commands
class Ophyd(Readable):
def __init__(self, dev):
for k in dev.describe().keys():
Nameable.__init__(self, k, Readable.__interfaces__ )
break
self.dev=dev
def read(self):
v = self.dev.read()
if (v is None) or (len(v)==0):
return None
v = v[self.getName()]
try:
timestamp = int(v["timestamp"]*1000)
except:
timestamp = time.time()
ret = TimestampedValue (v["value"], timestamp)
return ret
RE = RunEngine({}, during_task=EventProcessingTask())
if CAN_PAUSE:
+8
View File
@@ -0,0 +1,8 @@
from ophyd.sim import det1, det2, det3, det4, det, motor, motor1, motor2, motor3,img, sig, direct_img, pseudo1x3
from ophyd import Signal
from ophyd.signal import EpicsSignal
def read(name):
return globals()[name].read()
+79
View File
@@ -0,0 +1,79 @@
###################################################################################################
#Data Manipulation: Using the data access API to generate and retrieve data
###################################################################################################
#Creating a 1D dataset from an array
path="group/data1"
data1d = [1.0, 2.0, 3.0, 4.0, 5.0]
save_dataset(path, data1d)
#Reading ii back
read =load_data(path)
print (list(read))
assert data1d==list(read)
plot(read)
#Creating a 2D dataset from an array with some attributes
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/data2"
save_dataset(path, data2d)
set_attribute(path, "AttrString", "Value")
set_attribute(path, "AttrInteger", 1)
set_attribute(path, "AttrDouble", 2.0)
set_attribute(path, "AttrBoolean", True)
#Reading it back
read =load_data(path)
print ([list(a) for a in read])
plot(read)
#Creating a 3D dataset from an array
data3d = [ [ [1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7]], [ [3,2,3,4,5], [4,3,4,5,6], [5,4,5,6,7]]]
path="group/data3"
save_dataset(path, data3d)
#Reading it back
read =load_data(path,0)
print ([list(a) for a in read])
read =load_data(path,1)
print ([list(a) for a in read])
#Creating a INT dataset adding elements one by one
path = "group/data4"
create_dataset(path, 'i')
for i in range(10):
append_dataset(path,i)
#Creating a 2D data FLOAT dataset adding lines one by one
path = "group/data5"
create_dataset(path, 'd', False, (0,0))
for row in data2d:
append_dataset(path, row)
#Creating a Table (compund type)
path = "group/data6"
names = ["a", "b", "c", "d"]
table_types = ["d", "d", "d", "[d"]
lenghts = [0,0,0,5]
table = [ [1,2,3,[0,1,2,3,4]],
[2,3,4,[3,4,5,6,7]],
[3,4,5,[6,7,8,9,4]] ]
create_table(path, names, table_types, lenghts)
for row in table:
append_table(path, row)
flush_data()
#Read it back
read =load_data(path)
print (read)
#Writing scalars (datasets with rank 0)
save_dataset("group/val1", 1)
save_dataset("group/val2", 3.14)
save_dataset("group/val3", "test")
print (load_data("group/val1"))
print (load_data("group/val2"))
print (load_data("group/val3"))
+27
View File
@@ -0,0 +1,27 @@
import threading
from threading import Thread
from _thread import interrupt_main
import sys
# task executed in a new thread
def task():
# block for a moment
sleep(3)
# interrupt the main thread
print('Interrupting main thread now')
interrupt_main()
# start the new thread
thread = Thread(target=task)
thread.start()
print(threading.current_thread() == threading.main_thread())
# handle being interrupted
try:
# wait around
while True:
print('Main thread waiting...')
sleep(0.5)
except KeyboardInterrupt:
# terminate main thread
print('Main interrupted! Exiting.')
+7 -1
View File
@@ -27,4 +27,10 @@ def gfitoff(x, y, off=None, amp=None, com=None, sigma=None):
def gauss_fn(x, a, b, c, d):
return a + b * np.exp(-(np.power((x - c), 2) / (2 * np.power(d, 2))))
return a + b * np.exp(-(np.power((x - c), 2) / (2 * np.power(d, 2))))
"""
x=np.array([-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=np.array([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 ([off, amp, com, sigma])
"""
+27 -2
View File
@@ -19,14 +19,39 @@ def test():
return np.ones(5)
def test2(name, x=None, y=None):
def test2(name, x=None, y=None, **kwargs):
print (name,x,y)
ret = y*x
print (ret)
print(kwargs.get("z", 0.0))
return ret
def add(x,y,z):
return x+y+z
def read_dev(dev):
return dev.read()
return dev.read()
def print_dict(d):
for k in d.keys():
print (k, d[k])
ret = {}
ret.update(d)
return ret
def get_tuple():
return (1,2,3)
#x=np.array([0,1,2,3,4,5,6,7,8,9])
#y=np.array([1,2,3,6,9,6,3,2,1,0])
#print( linfit(x,y) )
#x=(0,1,2,3,4,5,6,7,8,9)
#y=(1,2,3,6,9,6,3,2,1,0)
#print( linfit(x,y) )
#print_dict({"a":1, "b":2})
+14 -9
View File
@@ -1,14 +1,19 @@
from jeputils import import_py
import matplotlib
matplotlib.use('TkAgg')
import_py("cpython/gfitoff", "gfitoff")
import matplotlib.pyplot as plt
from mathutils import Gaussian
x=to_array([-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],'d')
y=to_array([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],'d')
[off, amp, com, sigma] = gfitoff(x, y, off=None, amp=None, com=None, sigma=None)
print "Fit: ", [off, amp, com, sigma]
g = Gaussian(amp, com, sigma)
plot([y, [g.value(i)+off for i in x]], ["data", "fit"], xdata = x)
fig, ax = plt.subplots()
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
plt.show()
+2 -2
View File
@@ -125,7 +125,7 @@ def on_session_finished(id):
###################################################################################################
# Image filters
###################################################################################################
"""
class DataFilter(Filter.DataFilter):
def process(self, data):
data=data.copy()
@@ -148,7 +148,7 @@ image.addListener(image_filter)
add_device(image_filter, True)
image.refresh()
"""
+8 -4
View File
@@ -10,7 +10,11 @@ class DevConfig(DeviceConfig):
class Dev(DeviceBase):
def __init__(self, name):
#DeviceBase.__init__(self, name, DevConfig())
DeviceBase.__init__(self, name, DeviceConfig({"i":1, "d":1.0, "b":True, "s": "Test"}))
raise Exception("test ex")
add_device(Dev("test_cfg"), True)
DeviceBase.__init__(self, name, DeviceConfig({"intval":1, "floatval":1.0, "boolval":True, "strval": "Test"}))
add_device(Dev("testdev"), True)
print testdev.config
print testdev.config.getValue("intval")
print testdev.config.getValue("floatval")
print testdev.config.getValue("boolval")
print testdev.config.getValue("strval")
+24
View File
@@ -0,0 +1,24 @@
from ijutils import *
def get_file(index):
scan = "/Users/gobbo_a/dev/pshell/config/home/data/2024_03/20240319/20240319_105604_TeztTiff/scan 1/"
return scan + ("det image_%04d.tiff" % (index))
imgs_per_row=3
imgs_per_col=2
images = []
for i in range(6):
images.append(get_file(i))
composite =create_composite_image(images[0], imgs_per_row, imgs_per_col)
display_composite_image(composite)
p=None
for i in range(len(images)):
x,y = i%imgs_per_row, i/imgs_per_row
append_composite_image(composite, images[i], x, y)
display_composite_image(composite)
time.sleep(0.5)
+22
View File
@@ -0,0 +1,22 @@
baseurl = "https://data-api.psi.ch/sf"
filename = "/Users/gobbo_a/dev/back/test.h5"
channels = ["SARFE10-PSSS059:FIT-COM", "S10BC01-DBPM010:Q1"]
start = "2023-12-10T08:50:00.000Z" #args.start.astimezone(pytz.timezone('UTC')).strftime("%Y-%m-%dT%H:%M:%S.%fZ") # isoformat() # "2019-12-13T09:00:00.000000000Z"
end = "2023-12-10T08:50:05.000Z" # args.end.astimezone(pytz.timezone('UTC')).strftime("%Y-%m-%dT%H:%M:%S.%fZ") # .isoformat() # "2019-12-13T09:00:00.100000000Z"
api = DataAPI(baseurl)
ret = api.queryData(channels, start, end)
print ret
channel_data = ret[0]["data"]
y1=[v["value"] for v in channel_data]
x1=[v["globalSeconds"] for v in channel_data]
channel_data = ret[1]["data"]
y2=[v["value"] for v in channel_data]
x2=[v["globalSeconds"] for v in channel_data]
plot([y1, y2], channels, xdata=[x1, x2])
+3 -2
View File
@@ -3,8 +3,9 @@ from jeputils import import_py
import_py("CPython/linfit", "linfit")
import_py("CPython/gfitoff", "gfitoff")
x=[0,1,2,3,4,5,6,7,8,9]
y=[1,2,3,6,9,6,3,2,1,0]
x=to_array([0,1,2,3,4,5,6,7,8,9], 'd')
y=to_array([1,2,3,6,9,6,3,2,1,0], 'd')
(p, x_fit, y_fit, R2) = linfit(x,y)
#print "Fit: ", (p, x_fit, y_fit, R2)
plot((y,y_fit), name=("data", "fit"),xdata=(x,x_fit))
+267
View File
@@ -0,0 +1,267 @@
###################################################################################################
# Facade to JEP: Embedded Python
###################################################################################################
#Matplotlib won't work out of the box because it's default backend (Qt) uses signals, which only works in
#the main thread. Ideally should find a fix, in order to mark the running thread as the main.
#As a workaround, one can use the Tk backend:
#
#import matplotlib
#matplotlib.use('TkAgg')
#In principle just add JEP jar and library to the extensions folder.
#
#Alternatively on Linux:
# Python 2:
# - Add <python home>/lib/python3.X/site-packages/jep to LD_LIBRARY_PATH
# - Add <python home>/lib/python3.X/site-packages/jep/jep-X.X.X.jar to the class path
#
#Python3:
# - Add JEP library folder to LD_LIBRARY_PATH
# - If using OpenJDK, add also python <python home>/lib folder to LD_LIBRARY_PATH
# - Set LD_PRELOAD=<python home>/lib/libpython3.5m.so
import sys
import os
import jep.Jep
import jep.SharedInterpreter
import jep.NDArray
import java.lang.Thread
import org.python.core.PyArray as PyArray
import java.lang.String as String
import java.util.List
import java.util.Map
import java.util.HashMap
import ch.psi.pshell.scripting.ScriptUtils as ScriptUtils
from startup import to_array, get_context, _get_caller, Convert, Arr
__jep = {}
def __get_jep():
t = java.lang.Thread.currentThread()
if not t in __jep:
init_jep()
return __jep[t]
def __close_jep():
t = java.lang.Thread.currentThread()
if t in __jep:
__jep[t].close()
def init_jep():
#TODO: Should do it but generates errors
#__close_jep()
j = jep.SharedInterpreter()
try:
#Faster, but statements must be complete
j.setInteractive(False)
except:
pass # Removed in 4.2
__jep[java.lang.Thread.currentThread()] = j
j.eval("import sys")
#sys.argv is not present in JEP and may be needed for certain modules (as Tkinter)
j.eval("sys.argv = ['PShell']");
#Add standard script path to python path
j.eval("sys.path.append('" + get_context().setup.getScriptPath() + "')")
#Redirect stdout
j.eval("class JepStdout:\n" +
" def write(self, str):\n" +
" self.str += str\n" +
" def clear(self):\n" +
" self.str = ''\n" +
" def flush(self):\n" +
" pass\n")
j.eval("sys.stdout=JepStdout()");
j.eval("sys.stderr=JepStdout()");
j.eval("sys.stdout.clear()")
j.eval("sys.stderr.clear()")
#Import reload on Python 3
j.eval("try:\n" +
" reload # Python 2.7\n" +
"except NameError:\n" +
" try:\n" +
" from importlib import reload # Python 3.4+\n" +
" except ImportError:\n" +
" from imp import reload # Python 3.0 - 3.3\n")
def __print_stdout():
j=__get_jep()
output = None
err = None
try:
output = j.getValue("sys.stdout.str")
err = j.getValue("sys.stderr.str")
j.eval("sys.stdout.clear()")
j.eval("sys.stderr.clear()")
except:
pass
if (output is not None) and len(output)>0:
print output
if (err is not None) and len(err)>0:
print >> sys.stderr, err
def run_jep(script_name, vars = {}):
global __jep
script = get_context().scriptManager.library.resolveFile(script_name)
if script is None :
script= os.path.abspath(script_name)
j=__get_jep()
for v in vars:
j.set(v, vars[v])
try:
j.runScript(script)
finally:
__print_stdout()
def eval_jep(line):
j=__get_jep()
try:
j.eval(line)
finally:
__print_stdout()
def set_jep(var, value):
j=__get_jep()
j.set(var, value)
def get_jep(var):
j=__get_jep()
return j.getValue(var)
def call_jep(module, function, args = [], kwargs = {}, reload=False):
j=__get_jep()
if "/" in module:
script = get_context().scriptManager.library.resolveFile(module)
if "\\" in script:
#Windows paths
module_path = script[0:script.rfind("\\")]
module = script[script.rfind("\\")+1:]
else:
#Linux paths
module_path = script[0:script.rfind("/")]
module = script[script.rfind("/")+1:]
eval_jep("import sys")
eval_jep("sys.path.append('" + module_path + "')")
if module.endswith(".py"):
module = module[0:-3]
f = module+"_" + function+"_"+str(j.hashCode())
try:
if reload:
eval_jep("import " + module)
eval_jep("_=reload(" + module+")")
eval_jep("from " + module + " import " + function + " as " + f)
if (kwargs is not None) and (len(kwargs)>0):
#invoke with kwargs only available in JEP>3.8
hm=java.util.HashMap()
hm.update(kwargs)
#The only way to get the overloaded method...
m = j.getClass().getMethod("invoke", [String, ScriptUtils.getType("[o"), java.util.Map])
ret = m.invoke(j, [f, to_array(args,'o'), hm])
else:
ret = j.invoke(f, args)
finally:
__print_stdout()
return ret
#Converts pythonlist or Java array to numpy array
def to_npa(data, dimensions = None, type = None):
if (not isinstance(data, PyArray)) or (type is not None):
data = to_array(data,'d' if type is None else type)
if dimensions is None:
return jep.NDArray(data)
return jep.NDArray(data, dimensions)
#recursivelly converts all NumPy arrays to Java arrys
def rec_from_npa(obj):
if isinstance(obj, jep.NDArray):
ret = obj.data
if len(obj.dimensions)>1:
ret=Convert.reshape(ret, obj.dimensions)
return ret
if isinstance(obj, java.util.List) or isinstance(obj,tuple) or isinstance(obj,list):
ret=[]
for i in range(len(obj)):
ret.append(rec_from_npa(obj[i]))
if isinstance(obj,tuple):
return type(ret)
return ret
if isinstance(obj, java.util.Map) or isinstance(obj,dict):
ret = {} if isinstance(obj,dict) else java.util.HashMap()
for k in obj.keys():
ret[k] = rec_from_npa(obj[k])
return ret
return obj
#recursivelly converts all Java arrays to NumPy arrys
def rec_to_npa(obj):
if isinstance(obj, PyArray):
dimensions = Arr.getShape(obj)
if len(dimensions)>1:
obj = Convert.flatten(obj)
return to_npa(obj, dimensions = dimensions)
if isinstance(obj, java.util.List) or isinstance(obj,tuple) or isinstance(obj,list):
ret=[]
for i in range(len(obj)):
ret.append(rec_to_npa(obj[i]))
if isinstance(obj,tuple):
return tuple(ret)
return ret
if isinstance(obj, java.util.Map) or isinstance(obj,dict):
ret = {} if isinstance(obj,dict) else java.util.HashMap()
for k in obj.keys():
ret[k] = rec_to_npa(obj[k])
return ret
return obj
def call_py(module, function, reload_function, *args, **kwargs):
"""
Calls a CPython function recursively crecursively converting Java arrays in arguments to NumPy,
and NumPy arrays in return values to Java arrays.
"""
ret = call_jep(module, function, rec_to_npa(args), rec_to_npa(kwargs), reload=reload_function)
return rec_from_npa(ret)
def import_py(module, function):
"""
Adds a CPython function to globals, creating a wrapper call to JEP, with
recurvive convertion of Java arrays in arguments to NumPy arrays,
and NumPy arrays in return values to Java arrays.
"""
def jep_wrapper(*args, **kwargs):
reload_function = jep_wrapper.reload
jep_wrapper.reload = False
print module, function, reload_function
return call_py(module, function, reload_function, *args, **kwargs)
jep_wrapper.reload=True
_get_caller().f_globals[function] = jep_wrapper
return jep_wrapper
import_py("CPython/linfit", "linfit")
import_py("CPython/gfitoff", "gfitoff")
x=[0,1,2,3,4,5,6,7,8,9]
y=[1,2,3,6,9,6,3,2,1,0]
(p, x_fit, y_fit, R2) = linfit(x,y)
#print "Fit: ", (p, x_fit, y_fit, R2)
plot((y,y_fit), name=("data", "fit"),xdata=(x,x_fit))
from mathutils import Gaussian
x=to_array([-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],'d')
y=to_array([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],'d')
[off, amp, com, sigma] = gfitoff(x, y, off=None, amp=None, com=None, sigma=None)
#print "Fit: ", [off, amp, com, sigma]
g = Gaussian(amp, com, sigma)
plot([y, [g.value(i)+off for i in x]], ["data", "fit"], xdata = x)
+28
View File
@@ -0,0 +1,28 @@
from jeputils import *
read_ophyd = import_py("CPython/Ophyd", "read")
class Ophyd(Readable):
def __init__(self, name):
self.dev=name
def getName(self):
return self.dev
def read(self):
v = read_ophyd(self.name)
if (v is None) or (len(v)==0):
return None
v = v[self.name]
try:
timestamp = int(v["timestamp"]*1000)
except:
timestamp = time.time()
ret = TimestampedValue (v["value"], timestamp)
return ret
dets = [Ophyd("det1"), Ophyd("det2")]
tscan(dets, 10, 0.1)
+1 -2
View File
@@ -18,8 +18,7 @@
<script>import time
def process(x):
time.sleep(0.1)
#print "==&gt;" + str(x.getValue())
</script>
#print "==&gt;" + str(x.getValue())</script>
</action>
<detector xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ScalarDetector" name="TESTIOC:TESTCALCOUT:Output" id="id348623"/>
<detector xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ScalarDetector" name="TESTIOC:TESTSINUS:SinCalc" id="id367393"/>
+1 -1
View File
@@ -24,6 +24,6 @@ def process():
</dimension>
</scan>
<visualization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="LinePlot" x="id278043" y="id348623 id367393"/>
<visualization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MatrixPlotArray" y="id278043" z="id980818" type="3D"/>
<visualization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MatrixPlotArray" y="id278043" z="id980818" type="2D"/>
<visualization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="LinePlotArray" y="id980818" maxSeries="2" offset="3" size="5"/>
</configuration>
+19
View File
@@ -0,0 +1,19 @@
import javax.imageio.ImageIO as ImageIO
def get_file(index):
scan = "/Users/gobbo_a/dev/pshell/config/home/data/2024_03/20240314/20240314_141635_console/scan 1/"
return scan + ("det image_%04d.tiff" % (index))
images = []
for i in range(4):
filename = get_file(i)
images.append(ImageIO.read(File(filename)))
combinedImage = BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
g = combinedImage.getGraphics();
g.drawImage(images[0], 0, 0, None);
g.drawImage(images[1], 200, 0, None);
g.drawImage(images[2], 0, 100, None);
g.drawImage(images[3], 200, 100, None);
ImageIO.write(combinedImage, "TIFF", File(get_file(5)));
+35
View File
@@ -0,0 +1,35 @@
from ijutils import *
def get_file(index):
scan = "/Users/gobbo_a/dev/pshell/config/home/data/2024_03/20240319/20240319_105604_TeztTiff/scan 1/"
return scan + ("det image_%04d.tiff" % (index))
imgs_per_row=2
imgs_per_col=2
images = []
for i in range(4):
filename = get_file(i)
images.append(ImagePlus(filename))
proc=images[0].getProcessor()
combined_image = ImagePlus("Combined Image", proc.createProcessor(proc.getWidth()*imgs_per_row, imgs_per_col * proc.getHeight()))
p=None
for i in range(len(images)):
proc = images[i].getProcessor()
x,y = i%imgs_per_row, i/imgs_per_row
combined_image.getProcessor().insert(proc, x * proc.getWidth(), y * proc.getHeight())
data = Convert.transpose(get_ip_array(combined_image))
if p is None:
p=plot(data, title="Combined")[0]
else:
p.getSeries(0).setData(data)
time.sleep(1.0)
#plot(Convert.transpose(get_ip_array(combined_image)))
#save_image(combined_image, get_file(6), "tiff")
+78
View File
@@ -0,0 +1,78 @@
###################################################################################################
#Data Manipulation: Using the data access API to generate and retrieve data
###################################################################################################
#Creating a 1D dataset from an array
path="group/data1"
data1d = [1.0, 2.0, 3.0, 4.0, 5.0]
save_dataset(path, data1d)
#Reading ii back
read =load_data(path)
print read.tolist()
assert data1d==read.tolist()
plot(read)
#Creating a 2D dataset from an array with some attributes
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/data2"
save_dataset(path, data2d)
set_attribute(path, "AttrString", "Value")
set_attribute(path, "AttrInteger", 1)
set_attribute(path, "AttrDouble", 2.0)
set_attribute(path, "AttrBoolean", True)
#Reading it back
read =load_data(path)
print read.tolist()
plot(read)
#Creating a 3D dataset from an array
data3d = [ [ [1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7]], [ [3,2,3,4,5], [4,3,4,5,6], [5,4,5,6,7]]]
path="group/data3"
save_dataset(path, data3d)
#Reading it back
read =load_data(path,0)
print read.tolist()
read =load_data(path,1)
print read.tolist()
#Creating a INT dataset adding elements one by one
path = "group/data4"
create_dataset(path, 'i')
for i in range(10):
append_dataset(path,i)
#Creating a 2D data FLOAT dataset adding lines one by one
path = "group/data5"
create_dataset(path, 'd', False, (0,0))
for row in data2d:
append_dataset(path, row)
#Creating a Table (compund type)
path = "group/data6"
names = ["a", "b", "c", "d"]
table_types = ["d", "d", "d", "[d"]
lenghts = [0,0,0,5]
table = [ [1,2,3,[0,1,2,3,4]],
[2,3,4,[3,4,5,6,7]],
[3,4,5,[6,7,8,9,4]] ]
create_table(path, names, table_types, lenghts)
for row in table:
append_table(path, row)
flush_data()
#Read it back
read =load_data(path)
print read
#Writing scalars (datasets with rank 0)
save_dataset("group/val1", 1)
save_dataset("group/val2", 3.14)
save_dataset("group/val3", "test")
print load_data("group/val1")
print load_data("group/val2")
print load_data("group/val3")
+27
View File
@@ -0,0 +1,27 @@
from ijutils import *
def before_read(position, scan):
ProviderTIFF.setMetadata({"index": scan.recordIndex})
num_images = 4
r=tscan((sin, det.dataMatrix), num_images, 0.1, before_read=before_read)
data_folder = expand_path("{data}/" + r.path.replace(" | ", "/"))
imgs_per_row=2
imgs_per_col=num_images/2
combined_image = None
for i in range(num_images):
filename = data_folder + ("/%s_%04d.tiff" % (det.dataMatrix.getName(),i,))
img = ImagePlus(filename)
proc=img.getProcessor()
if not combined_image:
combined_image = ImagePlus("Combined Image", proc.createProcessor(proc.getWidth()*imgs_per_row, imgs_per_col * proc.getHeight()))
x,y = i%imgs_per_row, i/imgs_per_row
combined_image.getProcessor().insert(proc, x * proc.getWidth(), y * proc.getHeight())
filename = data_folder + ("/%s_combined.tiff" % (det.dataMatrix.getName(),))
save_image(combined_image, filename, "tiff", metadata={"index": -1})
+27
View File
@@ -0,0 +1,27 @@
class Test (PositionerBase):
def __init__(self, name):
PositionerBase.__init__(self, name, PositionerConfig())
self.rbk = 0.0
self.stp = 0.0
self.moving = False
def doRead(self):
return self.stp
def doWrite(self, value):
if value!=self.stp:
self.stp=value
self.moving = True
def doReadReadback(self):
if self.moving:
off = self.stp - self.rbk
if abs(off)<=1:
self.rbk = self.stp
self.moving = False
else:
self.rbk = self.rbk +1.0 if (self.stp > self.rbk) else self.rbk -1.0
return self.rbk
add_device(Test("test_positioner"), True)
test_positioner.polling=1000
+11 -8
View File
@@ -3,17 +3,20 @@ url="tcp://localhost:9999"
st1 = Stream("st1", url, SocketType.PULL)
#st1.parent.config.headerReservingAllocator = True
st1.parent.config.analizeHeader = False
st1.initialize()
st1.start()
st1.waitCacheChange(60000)
#show_panel(st1)
add_device(st1)
try:
bscan (st1, 5, 5, save=False)
#bscan (st1, 5, 5, save=False)
#show_panel(st1)
pass
finally:
st1.close()
#p.close()
#st1.close()
pass
+16
View File
@@ -0,0 +1,16 @@
"""
data3d = [ [ [1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7]], [ [3,2,3,4,5], [4,3,4,5,6], [5,4,5,6,7]]]
path="group/data3"
save_dataset(path, data3d)
#Reading it back
read =load_data(path,0)
print read.tolist()
read =load_data(path,1)
print read.tolist()
"""
#Creating a 2D dataset from an array with some attributes
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/data2"
save_dataset(path, data2d)
+299
View File
@@ -0,0 +1,299 @@
###################################################################################################
# Facade to JEP: Embedded Python
###################################################################################################
#Matplotlib won't work out of the box because it's default backend (Qt) uses signals, which only works in
#the main thread. Ideally should find a fix, in order to mark the running thread as the main.
#As a workaround, one can use the Tk backend:
#
#import matplotlib
#matplotlib.use('TkAgg')
#In principle just add JEP jar and library to the extensions folder.
#
#Alternatively on Linux:
# Python 2:
# - Add <python home>/lib/python3.X/site-packages/jep to LD_LIBRARY_PATH
# - Add <python home>/lib/python3.X/site-packages/jep/jep-X.X.X.jar to the class path
#
#Python3:
# - Add JEP library folder to LD_LIBRARY_PATH
# - If using OpenJDK, add also python <python home>/lib folder to LD_LIBRARY_PATH
# - Set LD_PRELOAD=<python home>/lib/libpython3.5m.so
import sys
import os
import jep.Jep
import jep.SharedInterpreter
import jep.NDArray
import java.lang.Thread
import org.python.core.PyArray as PyArray
import java.lang.String as String
import java.util.List
import java.util.Map
import java.util.HashMap
import java.util.ArrayList
import java.util.Collections
import ch.psi.pshell.scripting.ScriptUtils as ScriptUtils
from startup import to_array, get_context, _get_caller, Convert, Arr
__jep = {}
def __get_jep():
t = java.lang.Thread.currentThread()
if not t in __jep:
init_jep()
return __jep[t]
def __close_jep():
t = java.lang.Thread.currentThread()
if t in __jep:
__jep[t].close()
def init_jep():
#TODO: Should do it but generates errors
#__close_jep()
j = jep.SharedInterpreter()
try:
#Faster, but statements must be complete
j.setInteractive(False)
except:
pass # Removed in 4.2
__jep[java.lang.Thread.currentThread()] = j
j.eval("import sys")
#sys.argv is not present in JEP and may be needed for certain modules (as Tkinter)
j.eval("sys.argv = ['PShell']");
#Add standard script path to python path
j.eval("sys.path.append('" + get_context().setup.getScriptPath() + "')")
#Redirect stdout
j.eval("class JepStdout:\n" +
" def write(self, str):\n" +
" self.str += str\n" +
" def clear(self):\n" +
" self.str = ''\n" +
" def flush(self):\n" +
" pass\n")
j.eval("sys.stdout=JepStdout()");
j.eval("sys.stderr=JepStdout()");
j.eval("sys.stdout.clear()")
j.eval("sys.stderr.clear()")
#Import reload on Python 3
j.eval("try:\n" +
" reload # Python 2.7\n" +
"except NameError:\n" +
" try:\n" +
" from importlib import reload # Python 3.4+\n" +
" except ImportError:\n" +
" from imp import reload # Python 3.0 - 3.3\n")
def __print_stdout():
j=__get_jep()
output = None
err = None
try:
output = j.getValue("sys.stdout.str")
err = j.getValue("sys.stderr.str")
j.eval("sys.stdout.clear()")
j.eval("sys.stderr.clear()")
except:
pass
if (output is not None) and len(output)>0:
print output
if (err is not None) and len(err)>0:
print >> sys.stderr, err
def run_jep(script_name, vars = {}):
global __jep
script = get_context().scriptManager.library.resolveFile(script_name)
if script is None :
script= os.path.abspath(script_name)
j=__get_jep()
for v in vars:
j.set(v, vars[v])
try:
j.runScript(script)
finally:
__print_stdout()
def eval_jep(line):
j=__get_jep()
try:
j.eval(line)
finally:
__print_stdout()
def set_jep(var, value):
j=__get_jep()
j.set(var, value)
def get_jep(var):
j=__get_jep()
return j.getValue(var)
def call_jep(module, function, args = [], kwargs = {}, reload=False):
j=__get_jep()
if isinstance(args, java.util.List):
args=list(args)
if "/" in module:
script = get_context().scriptManager.library.resolveFile(module)
if "\\" in script:
#Windows paths
module_path = script[0:script.rfind("\\")]
module = script[script.rfind("\\")+1:]
else:
#Linux paths
module_path = script[0:script.rfind("/")]
module = script[script.rfind("/")+1:]
eval_jep("import sys")
eval_jep("sys.path.append('" + module_path + "')")
if module.endswith(".py"):
module = module[0:-3]
f = module+"_" + function+"_"+str(j.hashCode())
try:
if reload:
eval_jep("import " + module)
eval_jep("_=reload(" + module+")")
eval_jep("from " + module + " import " + function + " as " + f)
if (kwargs is not None) and (len(kwargs)>0):
#invoke with kwargs only available in JEP>3.8
hm=java.util.HashMap()
hm.update(kwargs)
#The only way to get the overloaded method...
m = j.getClass().getMethod("invoke", [String, ScriptUtils.getType("[o"), java.util.Map])
ret = m.invoke(j, [f, to_array(args,'o'), hm])
else:
ret = j.invoke(f, args)
finally:
__print_stdout()
return ret
#Converts pythonlist or Java array to numpy array
def to_npa(data, dimensions = None, type = None):
if (not isinstance(data, PyArray)) or (type is not None):
data = to_array(data,'d' if type is None else type)
if dimensions is None:
return jep.NDArray(data)
return jep.NDArray(data, dimensions)
#recursivelly converts all NumPy arrays to Java arrys
def rec_from_npa(obj):
ret = obj
if isinstance(obj, jep.NDArray):
ret = obj.data
if len(obj.dimensions)>1:
ret=Convert.reshape(ret, obj.dimensions)
elif isinstance(obj, java.util.List) or isinstance(obj,tuple) or isinstance(obj,list):
ret=[]
for i in range(len(obj)):
ret.append(rec_from_npa(obj[i]))
#if isinstance(obj,tuple) or obj.getClass().getSimpleName().startswith("Unmodifiable"): #TODO: Is it a good check for immutability?
# ret=tuple(ret)
elif isinstance(obj, java.util.Map) or isinstance(obj,dict):
ret = {}
for k in obj.keys():
ret[k] = rec_from_npa(obj[k])
return ret
#recursivelly converts all Java arrays to NumPy arrays
def rec_to_npa(obj):
ret = obj
if isinstance(obj, PyArray):
dimensions = Arr.getShape(obj)
if len(dimensions)>1:
obj = Convert.flatten(obj)
ret = to_npa(obj, dimensions = dimensions)
elif isinstance(obj, java.util.List) or isinstance(obj,tuple) or isinstance(obj,list):
ret = java.util.ArrayList()
for i in range(len(obj)):
ret.add(rec_to_npa(obj[i]))
#if isinstance(obj,tuple):
# ret = java.util.Collections.unmodifiableList(ret);
elif isinstance(obj, java.util.Map) or isinstance(obj,dict):
ret = java.util.HashMap()
for k in obj.keys():
ret[k] = rec_to_npa(obj[k])
return ret
def call_py(module, function, reload_function, *args, **kwargs):
"""
Calls a CPython function recursively converting Java arrays in arguments to NumPy,
and NumPy arrays in return values to Java arrays.
"""
ret = call_jep(module, function, rec_to_npa(args), rec_to_npa(kwargs), reload=reload_function)
return rec_from_npa(ret)
def import_py(module, function):
"""
Adds a CPython function to globals, creating a wrapper call to JEP, with
recurvive convertion of Java arrays in arguments to NumPy arrays,
and NumPy arrays in return values to Java arrays.
"""
def jep_wrapper(*args, **kwargs):
reload_function = jep_wrapper.reload
jep_wrapper.reload = False
return call_py(module, function, reload_function, *args, **kwargs)
jep_wrapper.reload=True
_get_caller().f_globals[function] = jep_wrapper
return jep_wrapper
"""
eval_jep("from java.lang import System")
eval_jep("s = 'Hello World'");
eval_jep("System.out.println(s)")
eval_jep("print(s)")
eval_jep("print(s[1:-1])")
eval_jep("import numpy")
r1=call_jep("numpy", "ones", [5])
r2=run_jep("cpython/gfitoff")
"""
"""
import_py("CPython/gfitoff", "gfitoff")
from mathutils import Gaussian
x=to_array([-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],'d')
y=to_array([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],'d')
[off, amp, com, sigma] = gfitoff(x, y, off=None, amp=None, com=None, sigma=None)
#print "Fit: ", [off, amp, com, sigma]
g = Gaussian(amp, com, sigma)
plot([y, [g.value(i)+off for i in x]], ["data", "fit"], xdata = x)
"""
import_py("CPython/linfit", "linfit")
import_py("CPython/linfit", "print_dict")
import_py("CPython/linfit", "get_tuple")
import_py("CPython/linfit", "test2")
#x=to_array([0,1,2,3,4,5,6,7,8,9], 'd')
#y=to_array([1,2,3,6,9,6,3,2,1,0], 'd')
x=[0,1,2,3,4,5,6,7,8,9]
y=[1,2,3,6,9,6,3,2,1,0]
x=(0,1,2,3,4,5,6,7,8,9)
y=(1,2,3,6,9,6,3,2,1,0)
a=java.util.ArrayList()
for i in x:
a.add(i)
(p, x_fit, y_fit, R2) = linfit(x,y)
print "Fit: ", (p, x_fit, y_fit, R2)
plot((y,y_fit), name=("data", "fit"),xdata=(x,x_fit))
print("-----")
t=get_tuple()
d={"a":1, "b":2}
r=print_dict(d)