avoid blocking with matplotlib, parameter save stuff

This commit is contained in:
2022-09-09 10:05:44 +02:00
parent 0a15899ee6
commit f70421d2f3
3 changed files with 101 additions and 68 deletions

View File

@@ -6,8 +6,11 @@
# #yaml is fixed and not altened by program
import logging
import re
_log = logging.getLogger(__name__)
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QApplication, QMainWindow #QLineEdit, QWidget, QGridLayout, QLabel
import json
@@ -189,7 +192,7 @@ class AppCfg(QSettings):
val=json.loads(val) # , object_hook=MyJsonDecoder)
else:
if key==AppCfg.DT_MISC:
val={'show_plots': True, 'vel_scl': 1.0, 'pt2pt_time': 10.0,'sync_mode':1,'sync_flag':3}
val={'show_plots': True, 'vel_scl': 1.0, 'pt2pt_time': 10.0,'sync_mode':1,'sync_flag':3,'verbose':0xff}
else:
val={}
elif key in (AppCfg.GEO_BEAM_SZ,AppCfg.GEO_BEAM_POS,):
@@ -257,6 +260,17 @@ bit 1=2 : simulated frame trigger
pt2pt_time : time point to point (needed sor sync code)
'''
tip_verbose='''\
verbose bits:
0x01 basic info
0x02 plot sorting steps
0x04 list program
0x04 upload progress
0x08 plot gather path
0x10 plot pvt trajectory (before motion)
0x20 print sync details
'''
params=[
{'name':'geometry','type':'group','expanded':False,'children':[
{'name': AppCfg.GEO_BEAM_SZ, 'title':'size of the beam', 'type':'group', 'children':[
@@ -264,34 +278,43 @@ bit 1=2 : simulated frame trigger
{'name':'height', 'type':'float', 'value':geo_beam_sz[1], 'step':0.1,'suffix':' um'},
#{'name':'TEST', 'type':'float', 'value':10.5, 'step':0.001, 'decimals':5},
]},
{'name':AppCfg.GEO_CAM_TRF, 'value':cfg.value(AppCfg.GEO_CAM_TRF), 'type':'str'},
{'name':AppCfg.GEO_CAM_PARAM, 'title':'camera parameters', 'type':'group', 'expanded':True, 'children':[
{'name':'gain', 'value':geo_cam_param.get('gain'), 'type':'float', 'step':1, },
{'name':'exposure','value':geo_cam_param.get('exposure'),'type':'float', 'step':1, },
{'name':'binning', 'value':geo_cam_param.get('binning'), 'type':'str'},
{'name':'roi', 'value':geo_cam_param.get('roi'), 'type':'str'},
{'name':'mono8', 'value':geo_cam_param.get('mono8'), 'type':'bool'},
]},
]},
{'name': AppCfg.DFT_POS_PST, 'title':'post sample tube reference positions','type':'group','expanded':False,'children':[
{'name':'in X upstream' ,'value':dft_pos_pst.get('x_in_us' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'in Y upstream' ,'value':dft_pos_pst.get('y_in_us' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'in X downstream' ,'value':dft_pos_pst.get('x_in_ds' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'in Y downstream' ,'value':dft_pos_pst.get('y_in_ds' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'out X delta' ,'value':dft_pos_pst.get('x_out_delta',0),'type':'float','limits':(-32,32),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'out Y delta' ,'value':dft_pos_pst.get('y_out_delta',0),'type':'float','limits':(-32,32),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'in Z' ,'value':dft_pos_pst.get('z_in' ,0),'type':'float','limits':(-8 ,5 ),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'out Z' ,'value':dft_pos_pst.get('z_out' ,0),'type':'float','limits':(-8 ,5 ),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'x_in_us', 'title':'in X upstream' ,'value':dft_pos_pst.get('x_in_us' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'y_in_us', 'title':'in Y upstream' ,'value':dft_pos_pst.get('y_in_us' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'x_in_ds', 'title':'in X downstream' ,'value':dft_pos_pst.get('x_in_ds' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'y_in_ds', 'title':'in Y downstream' ,'value':dft_pos_pst.get('y_in_ds' ,0),'type':'float','limits':(-45,15),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'x_out_delta','title':'out X delta' ,'value':dft_pos_pst.get('x_out_delta',0),'type':'float','limits':(-32,32),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'y_out_delta','title':'out Y delta' ,'value':dft_pos_pst.get('y_out_delta',0),'type':'float','limits':(-32,32),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'z_in', 'title':'in Z' ,'value':dft_pos_pst.get('z_in' ,0),'type':'float','limits':(-8 ,5 ),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'z_out', 'title':'out Z' ,'value':dft_pos_pst.get('z_out' ,0),'type':'float','limits':(-8 ,5 ),'step':0.1,'decimals':5,'suffix':' mm'},
{'name':'set_in', 'title':'use current position as "in"', 'type':'action'},
{'name':'set_out', 'title':'use current position as "out"', 'type':'action'},
]},
{'name': AppCfg.DFT_POS_COL, 'title':'collimator reference positions', 'type':'group','expanded':False, 'children':[
{'name':'in X', 'value':dft_pos_col.get('x_in',0), 'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'in Y', 'value':dft_pos_col.get('y_in',0), 'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'out X', 'value':dft_pos_col.get('x_out',0),'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'out Y', 'value':dft_pos_col.get('y_out',0),'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'x_in', 'title':'in X', 'value':dft_pos_col.get('x_in',0), 'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'y_in', 'title':'in Y', 'value':dft_pos_col.get('y_in',0), 'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'x_out','title':'out X', 'value':dft_pos_col.get('x_out',0),'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'y_out','title':'out Y', 'value':dft_pos_col.get('y_out',0),'type':'float', 'limits':(-15.9, 15.9 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'set_in', 'title':'use current position as "in"', 'type':'action'},
{'name':'set_out', 'title':'use current position as "out"', 'type':'action'},
]},
{'name': AppCfg.DFT_POS_BKLGT, 'title':'Back Light reference positions', 'type':'group','expanded':False, 'children':[
{'name':'In position', 'value':dft_pos_bklgt.get('pos_in',0), 'type':'float', 'limits':(-30000, 10 ),'step':10, 'decimals':5, 'suffix':'ustep'},
{'name':'Out position', 'value':dft_pos_bklgt.get('pos_out',0), 'type':'float', 'limits':(-1000, 10 ),'step':10, 'decimals':5, 'suffix':'ustep'},
{'name':'pos_in', 'title':'In position', 'value':dft_pos_bklgt.get('pos_in',0), 'type':'float', 'limits':(-30000, 10 ),'step':10, 'decimals':5, 'suffix':'ustep'},
{'name':'pos_out','title':'Out position', 'value':dft_pos_bklgt.get('pos_out',0), 'type':'float', 'limits':(-1000, 10 ),'step':10, 'decimals':5, 'suffix':'ustep'},
]},
{'name': AppCfg.DFT_POS_DET, 'title':'detector reference positions', 'type':'group','expanded':False, 'children':[
{'name':'In position', 'value':dft_pos_det.get('pos_in',0), 'type':'float', 'limits':(-20, 20 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'Out position', 'value':dft_pos_det.get('pos_out',0), 'type':'float', 'limits':(-20, 20 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'pos_in', 'title':'In position', 'value':dft_pos_det.get('pos_in',0), 'type':'float', 'limits':(-20, 20 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'pos_out','title':'Out position', 'value':dft_pos_det.get('pos_out',0), 'type':'float', 'limits':(-20, 20 ),'step':0.1, 'decimals':5, 'suffix':' mm'},
{'name':'set_in', 'title':'use current position as "in"', 'type':'action'},
{'name':'set_out', 'title':'use current position as "out"', 'type':'action'},
]},
@@ -299,26 +322,20 @@ bit 1=2 : simulated frame trigger
{'name':'deltatau motors', 'value':gbl_dev_prefix[0],'type':'str' },
{'name':'smaract motors', 'value':gbl_dev_prefix[1],'type':'str' },
]},
{'name':AppCfg.GEO_CAM_PARAM, 'title':'camera parameters', 'type':'group','expanded':False, 'children':[
{'name':'gain', 'value':geo_cam_param.get('gain',0), 'type':'float', 'step':1, },
{'name':'exposure', 'value':geo_cam_param.get('exposure',0), 'type':'float', 'step':1, },
{'name':'binning', 'value':geo_cam_param.get('binning','[1,1]'),'type':'str'},
{'name':'roi', 'value':geo_cam_param.get('roi','[1,2,3,4]'),'type':'str'},
{'name':'mono8', 'value':geo_cam_param.get('mono8','0'), 'type':'str'},
]},
{'name':'Delta Tau Parameters', 'type':'group','expanded':False, 'children':[
{'name':AppCfg.DT_HOST ,'title':'host name (host[:port:port_gather])','value':dt_host ,'type':'str'} ,
{'name':AppCfg.DT_MISC, 'title':'miscellaneous', 'type':'group', 'children':[
{'name':'show plots after collection', 'value':dt_misc['show_plots'], 'type':'bool','tip':"This is a checkbox"},
{'name':'velocity_scale', 'value':dt_misc['vel_scl'], 'type':'float', 'limits':(0, 1), 'step':0.1, 'tip':"This is a checkbox"},
{'name':'pt2pt_time', 'value':dt_misc['pt2pt_time'], 'type':'float', 'step':0.1,'tip':"This is a checkbox"},
{'name':'sync_mode', 'value':dt_misc['sync_mode'], 'type':'int', 'tip':tip_sync_mode},
{'name':'sync_flag', 'value':dt_misc['sync_flag'], 'type':'int', 'tip':tip_sync_flag},
{'name':'velocity_scale', 'value':dt_misc['vel_scl'], 'type':'float', 'limits':(0, 1), 'step':0.1, 'tip':"This is a checkbox"},
{'name':'pt2pt_time', 'value':dt_misc['pt2pt_time'],'type':'float', 'step':0.1,'tip':"This is a checkbox"},
{'name':'sync_mode', 'value':dt_misc['sync_mode'], 'type':'int', 'tip':tip_sync_mode},
{'name':'sync_flag', 'value':dt_misc['sync_flag'], 'type':'int', 'tip':tip_sync_flag},
{'name':'verbose', 'value':dt_misc['verbose'], 'type':'int', 'tip':tip_verbose},
]},
]},
{'name':'miscellaneous', 'type':'group','expanded':False, 'children':[
{'name':AppCfg.GEO_CAM_TRF, 'value':cfg.value(AppCfg.GEO_CAM_TRF), 'type':'str'},
]},
#{'name':'miscellaneous', 'type':'group','expanded':False, 'children':[
# {'name':AppCfg.GEO_CAM_TRF, 'value':cfg.value(AppCfg.GEO_CAM_TRF), 'type':'str'},
#]},
#{'name':'Save/Restore functionality', 'type':'group','expanded':False, 'children':[
# {'name':'Save State', 'type':'action'},
# {'name':'Restore State', 'type':'action', 'children':[
@@ -367,9 +384,7 @@ bit 1=2 : simulated frame trigger
parent=param.parent()
par_nm=parent.name()
nm=param.name()
if par_nm in(AppCfg.DT_HOST):
cfg.setValue(nm,param.value())
elif par_nm==AppCfg.GEO_BEAM_SZ:
if par_nm==AppCfg.GEO_BEAM_SZ:
v = np.array(tuple(map(lambda x: x.value(),parent.children())))
cfg.setValue(par_nm,v)
try:
@@ -378,39 +393,55 @@ bit 1=2 : simulated frame trigger
_log.warning('can not set beam size to application window')
else:
bm.setSize(v)
elif par_nm==AppCfg.GEO_CAM_PARAM:
d=dict(map(lambda x:(x.name(),x.value()), parent.children()))
try:
d['roi']=tuple(map(int,re.findall('(?:\d+)',d['roi'])))
d['binning']=tuple(map(int,re.findall('(?:\d+)',d['binning'])))
except TypeError:
_log.warning(f'failed to parse {AppCfg.GEO_CAM_PARAM}:{d}')
cfg.setValue(par_nm, d)
elif nm in(AppCfg.GEO_CAM_TRF):
s=param.value()
#https://docs.python.org/3/library/re.html#simulating-scanf + no grouping
trf=np.array(tuple(map(float,re.findall('[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?',s)))).reshape(3,3)
cfg.setValue(nm,trf)
try:
grp=self.parent()._goImgGrp
except AttributeError as e:
_log.warning('can not set transformation to application window')
else:
tr=QtGui.QTransform() # prepare ImageItem transformation:
tr.setMatrix(trf[0, 0], trf[1, 0], trf[2, 0], # (-1, 0, 0,
trf[0, 1], trf[1, 1], trf[2, 1], # 0,-1, 0,
trf[0, 2], trf[1, 2], trf[2, 2]) # 0, 0, 1)
grp.setTransform(tr) # assign transform
elif par_nm == AppCfg.DFT_POS_PST:
k=tuple(map(lambda x: x.name(),parent.children()))
k=('x_in_us','y_in_us','x_in_ds','y_in_ds','x_out_delta','y_out_delta','z_in','z_out',)
v = tuple(map(lambda x: x.value(),parent.children()))
d=dict(zip(k,v))
d=dict(map(lambda x:(x.name(),x.value()), parent.children()))
for k in ('set_in','set_out'): del d[k]
cfg.setValue(par_nm,d)
elif par_nm == AppCfg.DFT_POS_COL:
k=tuple(map(lambda x: x.name(),parent.children()))
k=('x_in','y_in','x_out','y_out')
v = tuple(map(lambda x: x.value(),parent.children()))
d=dict(zip(k,v))
d=dict(map(lambda x:(x.name(),x.value()), parent.children()))
for k in ('set_in','set_out'): del d[k]
cfg.setValue(par_nm,d)
elif par_nm == AppCfg.DFT_POS_BKLGT:
k=tuple(map(lambda x: x.name(),parent.children()))
k=('pos_in','pos_out')
v = tuple(map(lambda x: x.value(),parent.children()))
d=dict(zip(k,v))
d=dict(map(lambda x:(x.name(),x.value()), parent.children()))
cfg.setValue(par_nm,d)
elif par_nm == AppCfg.DFT_POS_DET:
k=tuple(map(lambda x: x.name(),parent.children()))
k=('pos_in','pos_out')
v = tuple(map(lambda x: x.value(),parent.children()))
d=dict(zip(k,v))
d=dict(map(lambda x:(x.name(),x.value()), parent.children()))
for k in ('set_in','set_out'): del d[k]
cfg.setValue(par_nm,d)
elif par_nm==AppCfg.GBL_DEV_PREFIX:
v=tuple(map(lambda x:x.value(), parent.children()))
cfg.setValue(par_nm, v)
elif nm in (AppCfg.DT_HOST):
cfg.setValue(nm, param.value())
elif par_nm==AppCfg.DT_MISC:
k=('show_plots','vel_scl','pt2pt_time','sync_mode','sync_flag')
v=tuple(map(lambda x:x.value(), parent.children()))
d=dict(zip(k,v))
d=dict(map(lambda x:(x.name(),x.value()), parent.children()))
cfg.setValue(par_nm, d)
else:
_log.warning(f'can\'t save parameter:{childName} change:{change} data:{str(data)}')
raise
cfg.sync()
def cb_valueChanging(self,param, value):

View File

@@ -40,5 +40,4 @@ class Deltatau:
self._comm=comm=None
self._gather=gather=None
#return
verbose=0x00
self._shapepath=sp=shapepath.ShapePath(comm, gather, verbose, sync_mode=1, sync_flag=3)
self._shapepath=sp=shapepath.ShapePath(comm, gather, verbose=0xff, sync_mode=1, sync_flag=3)

View File

@@ -47,6 +47,9 @@ import sys, os, time
import json, re
import random, signal,threading
import matplotlib as mpl
mpl.use('Qt5Agg') # needed to avoid blocking of ui !
#import Wigis #ZAC: orig. code
#import jungfrau_widget #ZAC: orig. code
#import bernina_pulse_picker #ZAC: orig. code
@@ -1413,19 +1416,18 @@ class WndSwissMx(QMainWindow, Ui_MainWindow):
_log.warning(f'no scan parameters for object->skipped:{go}')
continue
p=param['points']
trf=np.asmatrix(param['trf']*1000) #fix shear/rotation um->um
trf=np.asmatrix(param['trf']*1000) #fix shear/rotation mm->um
pos=np.array(param['grid']['pos']) #in um
pitch=np.array(param['grid']['pitch']) #in um
#t=np.matrix((0,0,1)).T
#for i in range(p.shape[0]):
# t.A[:2,0]=pos+p[i,:]*pitch
# p[i,:]=(trf*t).A.ravel()
# Same but much faster !
m=np.hstack((p*pitch+pos,np.ones((p.shape[0],1))))
#p=(trf*np.asmatrix(m).T).T.A
p=(np.asmatrix(m)*trf).A
param['points']=p
# add up to 100 test fiducial
vb=self.vb
@@ -1564,19 +1566,19 @@ class WndSwissMx(QMainWindow, Ui_MainWindow):
self._testCode=tc={'idx':0}
step=tc['idx']
def testMatplotlib():
import matplotlib.pyplot as plt
x=np.linspace(0.1, 2*np.pi, 41)
y=np.exp(np.sin(x))
plt.stem(x, y)
plt.show(block=False)
step=1
if step==0:
vb=self.vb
vb.autoRange(items=(self._goImg,))
elif step==1:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.1, 2*np.pi, 41)
y=np.exp(np.sin(x))
plt.stem(x, y)
plt.show()
testMatplotlib()
if step==3:
grp=pg.ItemGroup()
vb.addItem(grp)
@@ -1597,7 +1599,7 @@ class WndSwissMx(QMainWindow, Ui_MainWindow):
0, 0, 1)
grp.setTransform(tr)
print(vb.childGroup.childItems())
#print(vb.childGroup.childItems())
pass
def prepare_left_tabs(self):
@@ -2002,6 +2004,7 @@ class WndSwissMx(QMainWindow, Ui_MainWindow):
app._jungfrau=jf=detector.Jungfrau()
sp=dt._shapepath
sp.verbose=dt_misc['verbose']
sp.points=kwargs['points']
#sp.gen_grid_points(w=15, h=15, pitch=3, rnd=0, ofs=(0, +2000))
#sp.gen_grid_points(w=5, h=10, pitch=1, rnd=0, ofs=(0, 0));sp.sort_points(False, 10);sp.points