This commit is contained in:
2021-04-28 09:29:19 +02:00
parent 84e20e54cc
commit a9f0de27e6
1030 changed files with 14567 additions and 227310 deletions

10
script/test/SpectrumX.py Normal file
View File

@@ -0,0 +1,10 @@
class SpextrumX(Waveform):
def calc(self):
l = len(wf1.read())
frange(10, 100, 90/l)
x = x + 0.1
return ret
add_device(SinusoidWaveform("wf1"), True)

View File

@@ -0,0 +1,3 @@
tscan(sin, 10, 0.1)
add_data_file("test/out.h5")
add_data_file("/Users/gobbo_a/bst.scd")

14
script/test/TestDate.py Normal file
View File

@@ -0,0 +1,14 @@
cycle=0
frame_index=0
short_name=False
set_exec_pars(open=True)
root = "" if short_name else (get_exec_pars().path + "/")
prefix = get_context().setup.expandPath("i{date}%02d_{dseq}%03d")
print prefix
if cycle < 0:
ret = root + prefix + ".tif"
else:
ret= root + prefix + "/i{seq}%03d"+ "_" + str(cycle) + ("_%d" % frame_index) + ".tif"
print ret

View File

@@ -29,7 +29,7 @@ import traceback
import Jama.Matrix
diffcalc_path = os.path.abspath(get_context().setup.expandPath("{script}/Lib/diffcalc"))
diffcalc_path = os.path.abspath(expand_path("{script}/Lib/diffcalc"))
if not diffcalc_path in sys.path:
sys.path.append(diffcalc_path)

View File

@@ -0,0 +1,11 @@
from sessions import *
inject()
s=1
for s in range(3):
session_start("Sample" + str(s))
session_set_metadata("Index",s)
session_add_file("/Users/gobbo_a/test.py")
for i in range(5):
tscan(sin,10,0.1, open=False)
session_stop()

71
script/test/TestRSync.py Normal file
View File

@@ -0,0 +1,71 @@
import ch.psi.utils.Sys
RSYNC_GENERATE_USER_KEY = True
XTERM = "LANG=C /opt/X11/bin/xterm" if ch.psi.utils.Sys.getOSFamily().name()=="Mac" else "xterm"
user = "gobbo_a"
aux_file = os.path.expanduser("~/.rsync.tmp")
fix_permissions=True
do_print=True
def remove_user_key(do_print=True):
cmd = "rm ~/.ssh/ke;"
cmd = cmd + "rm ~/.ssh/ke.pub"
ret = exec_cmd(cmd, False)
if do_print:
if not ret.strip():
ret = "Success removing ssh keys"
print ret
def reset_user_key(do_print=True):
remove_user_key(do_print)
cmd = "ssh-keygen -N '' -f ~/.ssh/ke -t rsa;"
ret = exec_cmd(cmd)
if do_print:
print ret
if (os.path.isfile(aux_file)):
os.remove(aux_file)
with open(aux_file, "w") as fh:
fh.write("Cannot access file: " + aux_file)
os.chmod(aux_file, 0o777)
success_msg = 'Success transfering authorization key for: ' + user
cmd = 'echo Authorizing: ' + user + ";"
cmd = cmd + 'echo Invalid user or password > ' + aux_file + ";"
cmd = cmd + "export PK_SUCCESS=FAILURE;"
if RSYNC_GENERATE_USER_KEY:
reset_user_key(do_print)
cmd = cmd + "export PK=`cat ~/.ssh/ke.pub`;"
else:
cmd = cmd + "export PK=`cat ~/.ssh/id_rsa.pub`;"
cmd = cmd + 'echo Invalid user or password > ' + aux_file + ";"
cmd = cmd + 'su - ' + user + ' bash -c "'
cmd = cmd + 'echo $PK >> .ssh/authorized_keys;'
#cmd = cmd + 'sort .ssh/authorized_keys | uniq > .ssh/authorized_keys.uniq;'
#cmd = cmd + 'mv .ssh/authorized_keys.uniq .ssh/authorized_keys;'
if fix_permissions:
cmd = cmd + 'chmod g-w ~' + ";"
cmd = cmd + 'echo ' + success_msg + ";"
cmd = cmd + 'echo ' + success_msg + " > " + aux_file + ";"
cmd = cmd + '"'
#xterm_options = '-hold -T "Authentication" -into 44040199' #Get Winfow ID with 'wmctrl -lp'
xterm_options = '-T "Authentication" -fa monaco -fs 14 -bg black -fg green -geometry 80x15+400+100'
try:
cmd = "su - gobbo_a;ls"
ret = exec_cmd(XTERM + " " + xterm_options + " -e '" + cmd + "'")
with open (aux_file, "r") as myfile:
ret=myfile.read()
#;if [ "$depth" -eq "1" ]; then echo ' + success_msg + '; fi')
if not success_msg in ret:
raise Exception (ret)
except:
if RSYNC_GENERATE_USER_KEY:
remove_user_key(do_print)
raise Exception ("Error authenticating user: " + str(sys.exc_info()[1]))
finally:
if (os.path.isfile(aux_file)):
os.remove(aux_file)

View File

@@ -0,0 +1,13 @@
channel="TESTIOC:TESTSINUS:SinCalc"
pv1 = ReadonlyProcessVariable("C1", channel)
pv2 = ReadonlyProcessVariable("C2", channel)
pv1.config.offset = 10
pv1.config.scale = 2
pv2.config.offset = 20
pv1.config.scale = 4
pv1.initialize()
pv2.initialize()
tscan([pv1, pv2], 10, 0.1)

View File

@@ -0,0 +1,55 @@
SCI_METADATA = {
"beamlineParameters": {
"Monostripe": "Ru/C",
"Ring current": {
"v": 0.402246,
"u": "A"
},
"Beam energy": {
"v": 22595,
"u": "eV"
}
},
"detectorParameters": {
"Objective": 20,
"Scintillator": "LAG 20um",
"Exposure time": {
"v": 0.4,
"u": "s"
}
},
"scanParameters": {
"Number of projections": 1801,
"Rot Y min position": {
"v": 0,
"u": "deg"
},
"Inner scan flag": 0,
"File Prefix": "817b_B2_",
"Sample In": {
"v": 0,
"u": "m"
},
"Number of darks": 10,
"Rot Y max position": {
"v": 180,
"u": "deg"
},
"Angular step": {
"v": 0.1,
"u": "deg"
},
"Number of flats": 120,
"Sample Out": {
"v": -0.005,
"u": "m"
},
"Flat frequency": 0,
"Number of inter-flats": 0
}
}
print "scientificMetadata", SCI_METADATA

View File

@@ -6,14 +6,14 @@ ip = load_image("/Users/gobbo_a/dev/pshell/config/home/images/img.tiff", title="
#Basic image manipulation: creation, copying, padding, saving
resized = resize(ip, 300,300)
save_image(resized, get_context().setup.expandPath("{images}/resized.tiff") ,"tiff")
save_image(resized, expand_path("{images}/resized.tiff") ,"tiff")
crop=sub_image(ip,10,20,50,30)
bin_im = binning(ip,2)
new_im = new_image(256, 256, "color")
copy_image_to(bin_im, new_im, 20, 20)
pad_im = pad_image(ip, 1, 2, 3, 4, Color.RED)
stack=create_stack([ip,resized,crop, bin_im, new_im, pad_im], title = "Basic Functions")
save_image(stack, get_context().setup.expandPath("{images}/stack.tiff") ,"tiff")
save_image(stack, expand_path("{images}/stack.tiff") ,"tiff")
stack.show()

View File

@@ -19,9 +19,24 @@ channels = [ 'Int8Waveform', 'Int16Waveform', 'Int32Waveform', 'Int64Waveform'
'Float32Waveform', 'Float64Waveform', 'BoolWaveform']
#for c in channels:
# add_device(Waveform(c, s, c, 10), True)
#s.setIncomplete("fill_null")
add_device(s, True)
set_device_alias(stream, "PulseID")
bscan(s , 100, save=False)
try:
add_device(s, True)
set_device_alias(stream, "PulseID")
s.start()
#for i in range(10):
# s.waitCacheChange(5000)
# print s.take().keys(), s.take().values()
bscan(s , 10, save=False)
for v in s.take().items(): print v[0], v[1]
finally:
s.close()

17
script/test/plot1.py Normal file
View File

@@ -0,0 +1,17 @@
print "##"
print args
#show_message("Starting", blocking = False)
try:
print get_attributes("/camera1/image", root=args[0])
a=load_data("/camera1/image", root=args[0])
except:
try:
print get_attributes("/scan 1/sin", root=args[0])
print get_data_info("/scan 1/sin", root=args[0])
a=load_data("/scan 1/sin", root=args[0])
except:
print "Invalid"
raise Exception("Invalid file")
plot(a)
#show_message("Ok")

View File

@@ -3,6 +3,7 @@ import ch.psi.pshell.device.Register.RegisterArray as RegisterArray
camera = "SLG-LCAM-C102"
SPECTRUM_SIZE = 659
Y_ROI = (0,199)
class Scalar(RegisterBase):
def __init__(self, name):
@@ -38,12 +39,14 @@ add_device(Scalar("smin"), True)
add_device(Scalar("smax"), True)
a,b=[],[]
for i in range(SPECTRUM_SIZE):
a.append(i)
b.append(0)
sx.write(a)
sy.write(b)
#CAS.setServerPort(54321)
cas1 = CAS(camera + ":SPECTRUM_Y", sy, 'double')
cas6 = CAS(camera + ":SPECTRUM_X", sx, 'double')
cas2 = CAS(camera + ":SPECTRUM_CENTER", sc, 'double')
@@ -51,6 +54,9 @@ cas3 = CAS(camera + ":SPECTRUM_FWHM", sf, 'double')
cas4 = CAS(camera + ":SPC_ROI_YMIN", smin, 'double')
cas5 = CAS(camera + ":SPC_ROI_YMAX", smax, 'double')
caput(camera + ":SPC_ROI_YMIN", Y_ROI[0])
caput(camera + ":SPC_ROI_YMAX", Y_ROI[1])
print caget(camera + ":SPECTRUM_Y").tolist()
print caget(camera + ":SPECTRUM_X").tolist()

298
script/test/sessions.py Normal file
View File

@@ -0,0 +1,298 @@
from startup import get_context
import ch.psi.utils.SciCat as SciCat
import java.lang.Boolean
def _sm():
return get_context().sessionManager
def session_start(name, metadata=None):
""" Starts new session. If a session os open, completes it first.
Args:
name(str): Session name.
metadata(dict): Map of initial metdata parameters
If None(Default) use the default metadata definition.
Returns:
session id (int)
"""
return _sm().start(name, metadata)
def session_stop():
""" Stops current session, if open.
"""
return _sm().stop()
def session_pause():
""" Pauses current session, if open.
"""
return _sm().pause()
def session_resume():
""" Resumes current session, if paused.
"""
return _sm().resume()
def session_cancel():
""" Cancels current session, if started and empty (no generated data).
"""
return _sm().cancel()
def session_id():
""" Returns current session id (0 if no session is started).
Returns:
session id (int)
"""
return _sm().getCurrentId()
def session_name():
""" Returns current session name ("unknown" if no session is started)
Returns:
session name(str)
"""
return _sm().getCurrentName()
def session_started():
""" Returns true if a session is started.
Returns:
bool
"""
return _sm().isStarted()
def session_paused():
""" Returns true if current session is paused.
Returns:
bool
"""
return _sm().isPaused()
def session_add_file(path):
""" Adds additional file to session, if opened.
Args:
path(str): Relative to data path or absolute.
"""
return _sm().addAdditionalFile(path)
def session_ids():
""" Returns list of completed sessions.
Returns:
list of int
"""
return _sm().getIDs()
def session_get_name(id=None):
""" Return the name of a session.
Args:
id(int): Session id. Default (None) is the current session.
Returns:
session name (str)
"""
return _sm().getName() if id is None else _sm().getName(id)
def session_get_state(id=None):
""" Returns the session state
Args:
id(int): Session id. Default (None) is the current session.
Returns:
session state (str)
"""
return _sm().getState() if id is None else _sm().getState(id)
def session_get_start(id=None):
""" Returns the start timestamp of a session.
Args:
id(int): Session id. Default (None) is the current session.
Returns:
long
"""
return _sm().getStart() if id is None else _sm().getStart(id)
def session_get_stop(id):
""" Returns the stop timestamp of a completed session.
Args:
id(int): Session id.
Returns:
Timestamp (long)
"""
return _sm().getStop(id)
def session_get_root(id=None):
""" Returns the root data path of a session.
Args:
id(int): Session id. Default (None) is the current session.
Returns:
str
"""
return _sm().getRoot() if id is None else _sm().getRoot(id)
def session_get_info(id=None):
""" Returns the session information.
Args:
id(int): Session id. Default (None) is the current session.
Returns:
session info (dict)
"""
return _sm().getInfo() if id is None else _sm().getInfo(id)
def session_get_metadata(id=None):
""" Returns a session info metadata.
Args:
id(int): Session id. Default (None) is the current session.
Returns:
session metadata (dict)
"""
return _sm().getMetadata() if id is None else _sm().getMetadata(id)
def session_set_metadata(key, value,id=None):
""" Set session metadata entry.
Args:
key(str): Metadata key
value(obj): Metadata value
id(int): Session id. Default (None) is the current session.
"""
return _sm().setMetadata(key, value) if id is None else _sm().setMetadata(id,key, value)
def session_get_metadata_keys():
""" Return the default metadata definition for samples.
Returns:
list of map entries
"""
return [str(e.key) for e in _sm().getMetadataDefinition()]
def session_get_metadata_type(key):
""" Return the metadata type for a given key:
String, Integer, Double, Boolean, List or Map.
Args:
key(str): Metadata key.
Returns:
str
"""
return str(_sm().getMetadataType(key))
def session_get_metadata_default(key):
""" Return the metadata default value for a given key.
Args:
key(str): Metadata key.
Returns:
Object
"""
return _sm().getMetadataDefault(key)
def session_get_runs(id=None, relative=True):
""" Return the runs of a session.
Args:
id(int): Session id. Default (None) is the current session.
relative(bool): if True use relative file names (for files under the data root path)
Returns:
List of dicts
"""
return _sm().getRuns(java.lang.Boolean(relative)) if id is None else _sm().getRuns(id, relative)
def session_set_run_enabled(enabled, id=None, index=-1):
""" Enable or disable a run.
Args:
enabled(bool): true for enabling, false for disabling
id(int): Session id. Default (None) is the current session.
index: Index of the run. Default (-1) for the last run.
Returns:
Object
"""
return _sm().setRunEnabled(index, enabled) if id is None else _sm().setRunEnabled(id, index, enabled)
def session_get_additional_files(id=None, relative=True):
""" Return additional files of a session.
Args:
id(int): Session id. Default (None) is the current session.
relative(bool): if True use relative file names (for files under the data root path)
Returns:
List of str
"""
return _sm().getAdditionalFiles(java.lang.Boolean(relative)) if id is None else _sm().getAdditionalFiles(id, relative)
def session_get_file_list(id=None, relative=True):
""" Return complete list of data files of a session.
Args:
id(int): Session id. Default (None) is the current session.
relative(bool): if True use relative file names (for files under the data root path)
Returns:
List of str
"""
return _sm().getFileList(java.lang.Boolean(relative)) if id is None else _sm().getFileList(id, relative)
def session_create_zip(file_name, id=None, preserve_folder_structure=True):
""" Create ZIP file with session contents
Args:
file_name(str): name of the zip file
id(int): Session id. Default (None) is the current session.
preserve_folder_structure: if False all data files are added to the root of the file.
if True the folder structure under data root is preserved.
"""
return _sm().createZipFile(file_name, preserve_folder_structure) if id is None else _sm().createZipFile(id, file_name, preserve_folder_structure)
def ingest_scicat(id, matadata={}, parameters=None):
""" Ingest a completed session to SciCat
Args:
id(int): Session id.
creation_location_name(str):
Returns:
Dataset ID in case of success. Otherwise throws an exception.
"""
sciCat= SciCat()
result = sciCat.ingest(id, matadata, parameters);
print result.output
if not result.success:
raise Exception ("Error ingesting session " + str(id))
return result.datasetId

View File

@@ -1,58 +1,31 @@
"""
Function fitting and peak search with mathutils facade
"""
from mathutils import fit_polynomial,fit_gaussian, fit_harmonic, calculate_peaks
from mathutils import PolynomialFunction, Gaussian, HarmonicOscillator
import math
start = 0
end = 10
step_size = 0.1
result= lscan(sout,sinp,start,end,[step_size,],0.01)
readable = result.getReadable(0)
positions = result.getPositions(0)
def get_function_data(function, start, end, resolution):
ret = []
for x in frange(start, end, resolution, True):
fit_polinomial.append(function.value(x))
pars_polynomial = (a0, a1, a2, a3, a4, a5, a6) = fit_polynomial(readable, positions, 6)
fitted_polynomial_function = PolynomialFunction(pars_polynomial)
print pars_polynomial
from mathutils import fit_gaussian, Gaussian
from plotutils import plot_function, plot_data
import math
ydata = [0, 1,2,3,4,5,6,7,8,9,8,7,6,5,4,4,3,3,2,2,1,0]
xdata = range(len(ydata))
(normalization, mean, sigma) = fit_gaussian(readable, positions)
fitted_gaussian_function = Gaussian(normalization, mean, sigma)
print (normalization, mean, sigma)
(amplitude, angular_frequency, phase) = fit_harmonic(readable, positions)
fitted_harmonic_function = HarmonicOscillator(amplitude, angular_frequency, phase)
print (amplitude, angular_frequency, phase)
resolution = step_size/100
fit_polinomial = []
fit_gaussian = []
fit_harmonic = []
for x in frange(start,end,resolution, True):
fit_polinomial.append(fitted_polynomial_function.value(x))
fit_gaussian.append(fitted_gaussian_function.value(x))
fit_harmonic.append(fitted_harmonic_function.value(x))
x = frange(start, end+resolution, resolution)
fit = []
resolution = float(xdata[0] - xdata[1])/100
xfit = frange(start,end,resolution, True)
for x in xfit:
fit.append(fitted_gaussian_function.value(x))
plots = plot([readable, fit_polinomial, fit_gaussian, fit_harmonic] ,
["data", "polinomial", "gaussian", "harmonic"], xdata = [positions,x,x,x], title="Data")
for p in peaks:
print "Max: " + str(p)
plots[0].addMarker(p, None, "Max=" + str(round(p,2)), None)
import java.awt.Color
plots[0].addMarker(mean, None, "Mean=" + str(round(mean,2)), java.awt.Color.LIGHT_GRAY)
peaks = calculate_peaks(fitted_polynomial_function, start, end)
#plots = plot([ydata, fit],["data", "gaussian"], xdata = [xdata,xfit], title="Fit")
#plots[0].addMarker(mean, None, "Mean=" + str(round(mean,2)), java.awt.Color.LIGHT_GRAY)
p = plot(None, title="Fit")[0]

View File

@@ -0,0 +1,28 @@
"""
Function fitting and peak search with mathutils facade
"""
from mathutils import fit_gaussian, Gaussian
from plotutils import plot_function, plot_data
import math
def plot_fit(xdata, ydata, gauss_pars=None, title = "Fit"):
if gauss_pars is None:
gauss_pars = fit_gaussian(ydata, xdata)
(normalization, mean_value, sigma) = gauss_pars
fitted_gaussian_function = Gaussian(normalization, mean, sigma)
print (normalization, mean_value, sigma)
p = plot(None, title=title)[0]
p.clear()
plot_data(p, ydata, "Data", xdata=xdata, show_points = True, color=Color.BLUE)
fit_range = frange(xdata[0],xdata[-1],float(xdata[1]-xdata[0])/100, True)
plot_function(p, fitted_gaussian_function, "Fit", fit_range, show_points=False, color=Color.RED)
p.setLegendVisible(True)
p.addMarker(mean_value, None, "Mean=" + str(round(mean_value,2)), Color.LIGHT_GRAY)
ydata = [0, 1,2,3,4,5,6,7,8,9,8,7,6,5,4,4,3,3,2,2,1,0]
xdata = range(len(ydata))
plot_fit(xdata, ydata)

View File

@@ -0,0 +1,6 @@
import java.lang.System as System
for i in range(10):
#print ",",
System.out.print(",")
time.sleep(0.2)
System.out.println("")

37
script/test/test_psss.py Normal file
View File

@@ -0,0 +1,37 @@
p=plot(None)[0]
import ch.psi.pshell.plot.RangeSelectionPlot as RangeSelectionPlot
import org.jfree.ui.RectangleAnchor as RectangleAnchor
import org.jfree.ui.TextAnchor as TextAnchor
def plot_psss(p):
"""
if len(p.getMarkers())==0:
m1=p.addMarker(0,None,"",Color.WHITE)
m2=p.addMarker(0,None,"",Color.WHITE)
m2.setLabelAnchor(RectangleAnchor.TOP)
else:
m1,m2 = p.getMarkers()
"""
if len(p.getMarkers())==0:
m=p.addIntervalMarker(0,0, None,"", RangeSelectionPlot().getSelectionColor())
m.setLabelAnchor(RectangleAnchor.BOTTOM)
else:
m = p.getMarkers()[0]
x,y = frange(0,40,1.0), frange(100,180,2.0)
if (x is None) or (y is None):
p.getSeries(0).clear()
else:
p.getSeries(0).setData(x,y)
#m1.value, m2.value= psss_center.take() - psss_fwhm.take(), psss_center.take() + psss_fwhm.take()
#m2.label = str(psss_center.take())
m.startValue, m.endValue = 10.0, 20.0
m.label = str(15.0)
while True:
plot_psss(p)
time.sleep(1.0)