Skip to content

Commit

Permalink
Added x@Min/x@Max regular and Meas, optimized Meas code to avoid unne…
Browse files Browse the repository at this point in the history
…cessary recalcs
  • Loading branch information
cdrtm committed Jun 9, 2021
1 parent 61ff2a8 commit 20beb54
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 43 deletions.
54 changes: 29 additions & 25 deletions pydatview/GUIInfoPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ def __init__(self, parent):
self.ColsReg.append({'name':'Std/Mean (TI)', 'al':'R' , 'm':'y0TI' , 's' :False})
self.ColsReg.append({'name':'Min' , 'al':'R' , 'm':'y0Min' , 's' :True})
self.ColsReg.append({'name':'Max' , 'al':'R' , 'm':'y0Max' , 's' :True})
self.ColsReg.append({'name':'x@Min' , 'al':'R' , 'm':'xAtYMin' , 's' :False})
self.ColsReg.append({'name':'x@Max' , 'al':'R' , 'm':'xAtYMax' , 's' :False})
self.ColsReg.append({'name':'Abs. Max' , 'al':'R' , 'm':'yAbsMax', 's' :False})
self.ColsReg.append({'name':'Range' , 'al':'R' , 'm':'yRange', 's' :True})
self.ColsReg.append({'name':'dx' , 'al':'R' , 'm':'dx' , 's' :True})
self.ColsReg.append({'name':'Meas 1' , 'al':'R' , 'm':'meas' , 's' :False})
self.ColsReg.append({'name':'Meas 2' , 'al':'R' , 'm':'meas' , 's' :False})
self.ColsReg.append({'name':'Mean (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsReg.append({'name':'Min (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsReg.append({'name':'Max (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsReg.append({'name':'Meas 1' , 'al':'R' , 'm':'meas1' , 's' :False})
self.ColsReg.append({'name':'Meas 2' , 'al':'R' , 'm':'meas2' , 's' :False})
self.ColsReg.append({'name':'Mean (Meas)' , 'al':'R' , 'm':'yMeanMeas' , 's' :False})
self.ColsReg.append({'name':'Min (Meas)' , 'al':'R' , 'm':'yMinMeas' , 's' :False})
self.ColsReg.append({'name':'Max (Meas)' , 'al':'R' , 'm':'yMaxMeas' , 's' :False})
self.ColsReg.append({'name':'x@Min (Meas)' , 'al':'R' , 'm':'xAtYMinMeas' , 's' :False})
self.ColsReg.append({'name':'x@Max (Meas)' , 'al':'R' , 'm':'xAtYMaxMeas' , 's' :False})
self.ColsReg.append({'name':'xMin' , 'al':'R' , 'm':'xMin' , 's' :False})
self.ColsReg.append({'name':'xMax' , 'al':'R' , 'm':'xMax' , 's' :False})
self.ColsReg.append({'name':'xRange' , 'al':'R' , 'm':'xRange', 's' :False})
Expand All @@ -117,6 +121,8 @@ def __init__(self, parent):
self.ColsFFT.append({'name':'Std' , 'al':'R' , 'm':'y0Std' , 's' :True})
self.ColsFFT.append({'name':'Min' , 'al':'R' , 'm':'y0Min' , 's' :True})
self.ColsFFT.append({'name':'Max' , 'al':'R' , 'm':'y0Max' , 's' :True})
self.ColsFFT.append({'name':'x@Min' , 'al':'R' , 'm':'xAtYMin' , 's' :False})
self.ColsFFT.append({'name':'x@Max' , 'al':'R' , 'm':'xAtYMax' , 's' :False})
self.ColsFFT.append({'name':'Mean(FFT)' , 'al':'R' , 'm':'yMean' , 's' :False})
self.ColsFFT.append({'name':'Std(FFT)' , 'al':'R' , 'm':'yStd' , 's' :False})
self.ColsFFT.append({'name':'Min(FFT)' , 'al':'R' , 'm':'yMin' , 's' :True})
Expand All @@ -130,11 +136,13 @@ def __init__(self, parent):
self.ColsFFT.append({'name':'nWin(FFT)' , 'al':'R' , 'f':lambda x:x.Info('LWin') , 's' :False})
self.ColsFFT.append({'name':'nFFT(FFT)' , 'al':'R' , 'f':lambda x:x.Info('nFFT') , 's' :False})
self.ColsFFT.append({'name':'n(FFT)' , 'al':'R' , 'm':'ylen' , 's' :True})
self.ColsFFT.append({'name':'Meas 1' , 'al':'R' , 'm':'meas' , 's' :False})
self.ColsFFT.append({'name':'Meas 2' , 'al':'R' , 'm':'meas' , 's' :False})
self.ColsFFT.append({'name':'Mean (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsFFT.append({'name':'Min (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsFFT.append({'name':'Max (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsFFT.append({'name':'Meas 1' , 'al':'R' , 'm':'meas1' , 's' :False})
self.ColsFFT.append({'name':'Meas 2' , 'al':'R' , 'm':'meas2' , 's' :False})
self.ColsFFT.append({'name':'Mean (Meas)' , 'al':'R' , 'm':'yMeanMeas' , 's' :False})
self.ColsFFT.append({'name':'Min (Meas)' , 'al':'R' , 'm':'yMinMeas' , 's' :False})
self.ColsFFT.append({'name':'Max (Meas)' , 'al':'R' , 'm':'yMaxMeas' , 's' :False})
self.ColsFFT.append({'name':'x@Min (Meas)' , 'al':'R' , 'm':'xAtYMinMeas' , 's' :False})
self.ColsFFT.append({'name':'x@Max (Meas)' , 'al':'R' , 'm':'xAtYMaxMeas' , 's' :False})
self.ColsFFT.append({'name':'n ' , 'al':'R' , 'm':'n0' , 's' :True})
self.ColsMinMax=[]
self.ColsMinMax.append({'name':'Directory' , 'al':'L' , 'm':'baseDir', 's':False})
Expand Down Expand Up @@ -162,16 +170,20 @@ def __init__(self, parent):
self.ColsPDF.append({'name':'Std' , 'al':'R' , 'm':'y0Std' , 's' :True})
self.ColsPDF.append({'name':'Min' , 'al':'R' , 'm':'y0Min' , 's' :True})
self.ColsPDF.append({'name':'Max' , 'al':'R' , 'm':'y0Max' , 's' :True})
self.ColsPDF.append({'name':'x@Min' , 'al':'R' , 'm':'xAtYMin' , 's' :False})
self.ColsPDF.append({'name':'x@Max' , 'al':'R' , 'm':'xAtYMax' , 's' :False})
self.ColsPDF.append({'name':'Mean(PDF)' , 'al':'R' , 'm':'yMean' , 's' :False})
self.ColsPDF.append({'name':'Std(PDF)' , 'al':'R' , 'm':'yStd' , 's' :False})
self.ColsPDF.append({'name':'Min(PDF)' , 'al':'R' , 'm':'yMin' , 's' :True})
self.ColsPDF.append({'name':'Max(PDF)' , 'al':'R' , 'm':'yMax' , 's' :True})
self.ColsPDF.append({'name':u'\u222By(PDF)' , 'al':'R' , 'm':'inty' , 's' :True})
self.ColsPDF.append({'name':'Meas 1' , 'al':'R' , 'm':'meas' , 's' :False})
self.ColsPDF.append({'name':'Meas 2' , 'al':'R' , 'm':'meas' , 's' :False})
self.ColsPDF.append({'name':'Mean (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsPDF.append({'name':'Min (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsPDF.append({'name':'Max (Meas)' , 'al':'R' , 'm':'yMeas' , 's' :False})
self.ColsPDF.append({'name':'Meas 1' , 'al':'R' , 'm':'meas1' , 's' :False})
self.ColsPDF.append({'name':'Meas 2' , 'al':'R' , 'm':'meas2' , 's' :False})
self.ColsPDF.append({'name':'Mean (Meas)' , 'al':'R' , 'm':'yMeanMeas' , 's' :False})
self.ColsPDF.append({'name':'Min (Meas)' , 'al':'R' , 'm':'yMinMeas' , 's' :False})
self.ColsPDF.append({'name':'Max (Meas)' , 'al':'R' , 'm':'yMaxMeas' , 's' :False})
self.ColsPDF.append({'name':'x@Min (Meas)' , 'al':'R' , 'm':'xAtYMinMeas' , 's' :False})
self.ColsPDF.append({'name':'x@Max (Meas)' , 'al':'R' , 'm':'xAtYMaxMeas' , 's' :False})
self.ColsPDF.append({'name':'n(PDF)' , 'al':'R' , 'm':'ylen' , 's' :True})
self.ColsCmp=[]
self.ColsCmp.append({'name':'Directory' , 'al':'L' , 'm':'baseDir' , 's':False})
Expand Down Expand Up @@ -307,16 +319,8 @@ def _showStats(self,erase=True):
for j,c in enumerate(selCols):
# TODO: could be nicer:
if 'm' in c.keys():
if c['name'] == 'Meas 1':
v,sv=getattr(PD,c['m'])(self.meas_xy1)
elif c['name'] == 'Meas 2':
v,sv=getattr(PD,c['m'])(self.meas_xy2)
elif c['name'] == 'Mean (Meas)':
v, sv = getattr(PD, c['m'])(self.meas_xy1, self.meas_xy2, 'mean')
elif c['name'] == 'Max (Meas)':
v, sv = getattr(PD, c['m'])(self.meas_xy1, self.meas_xy2, 'max')
elif c['name'] == 'Min (Meas)':
v, sv = getattr(PD, c['m'])(self.meas_xy1, self.meas_xy2, 'min')
if c['m'] in ('meas1', 'meas2'):
v,sv=getattr(PD,c['m'])(self.meas_xy1, self.meas_xy2)
else:
v,sv=getattr(PD,c['m'])()
else:
Expand Down
106 changes: 88 additions & 18 deletions pydatview/plotdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,16 @@ def _post_init(PD, Options={}):
PD._y0Max = PD._yMax
PD._x0Min = PD._xMin
PD._x0Max = PD._xMax
PD._x0AtYMin = PD._xAtYMin
PD._x0AtYMax = PD._xAtYMax
PD._y0Std = PD.yStd()
PD._y0Mean = PD.yMean()
PD._n0 = (n,'{:d}'.format(n))
PD.x0 =PD.x
PD.y0 =PD.y
# Store xyMeas input values so we don't need to recompute xyMeas in case they didn't change
PD.xyMeasInput1, PD.xyMeasInput2 = None, None
PD.xyMeas1, PD.xyMeas2 = None, None

def __repr__(s):
s1='id:{}, it:{}, ix:{}, iy:{}, sx:"{}", sy:"{}", st:{}, syl:{}\n'.format(s.id,s.it,s.ix,s.iy,s.sx,s.sy,s.st,s.syl)
Expand Down Expand Up @@ -261,8 +266,10 @@ def computeRange(PD):
"""
PD._xMin = PD._xMinCalc()
PD._xMax = PD._xMaxCalc()
PD._yMin = PD._yMinCalc()
PD._yMin = PD._yMinCalc()
PD._yMax = PD._yMaxCalc()
PD._xAtYMin = PD._xAtYMinCalc(PD._yMin[0])
PD._xAtYMax = PD._xAtYMaxCalc(PD._yMax[0])


# --------------------------------------------------------------------------------}
Expand All @@ -288,6 +295,26 @@ def _yMaxCalc(PD):
s=pretty_num(v)
return (v,s)

def _xAtYMinCalc(PD, yMin):
if PD.xIsString:
return PD.x[0],PD.x[0].strip()
elif PD.xIsDate:
return PD.x[0],'{}'.format(PD.x[0])
else:
v = PD.x[np.where(PD.y == yMin)[0][0]]
s=pretty_num(v)
return (v,s)

def _xAtYMaxCalc(PD, yMax):
if PD.xIsString:
return PD.x[-1],PD.x[-1].strip()
elif PD.xIsDate:
return PD.x[-1],'{}'.format(PD.x[-1])
else:
v = PD.x[np.where(PD.y == yMax)[0][0]]
s=pretty_num(v)
return (v,s)

def _xMinCalc(PD):
if PD.xIsString:
return PD.x[0],PD.x[0].strip()
Expand All @@ -314,6 +341,12 @@ def xMin(PD):
def xMax(PD):
return PD._xMax

def xAtYMin(PD):
return PD._xAtYMin

def xAtYMax(PD):
return PD._xAtYMax

def yMin(PD):
return PD._yMin

Expand Down Expand Up @@ -468,43 +501,80 @@ def intyx2(PD):
s=pretty_num(v)
return v,s

def meas(PD, xymeas):
def meas1(PD, xymeas1, xymeas2):
if PD.xyMeasInput1 is not None and PD.xyMeasInput1 == xymeas1:
yv = PD.xyMeas1[1]
s = pretty_num(yv)
else:
xv, yv, s = PD._meas(xymeas1)
PD.xyMeas1 = [xv, yv]
PD.xyMeasInput1 = xymeas1
return yv, s

def meas2(PD, xymeas1, xymeas2):
if PD.xyMeasInput2 is not None and PD.xyMeasInput2 == xymeas2:
yv = PD.xyMeas2[1]
s = pretty_num(yv)
else:
xv, yv, s = PD._meas(xymeas2)
PD.xyMeas2 = [xv, yv]
PD.xyMeasInput2 = xymeas2
return yv, s

def yMeanMeas(PD):
return PD._measCalc('mean')

def yMinMeas(PD):
return PD._measCalc('min')

def yMaxMeas(PD):
return PD._measCalc('max')

def xAtYMinMeas(PD):
return PD._measCalc('xmin')

def xAtYMaxMeas(PD):
return PD._measCalc('xmax')

def _meas(PD, xymeas):
try:
v='NA'
xv, yv = 'NA', 'NA'
xy = np.array([PD.x, PD.y]).transpose()
points = find_closest(xy, [xymeas[0], xymeas[1]], False)
if points.ndim == 1:
v = points[1]
s=pretty_num(v)
xv, yv = points[0:2]
s = pretty_num(yv)
else:
v = points[0, 1]
xv, yv = points[0, 0], points[0, 1]
s = ' / '.join([str(p) for p in points[:, 1]])
except (IndexError, TypeError):
v='NA'
xv, yv = 'NA', 'NA'
s='NA'
return v,s
return xv, yv, s

def yMeas(PD, xymeas1, xymeas2, mode):
def _measCalc(PD, mode):
if PD.xyMeas1 is None or PD.xyMeas2 is None:
return 'NA', 'NA'
try:
xy = np.array([PD.x, PD.y]).transpose()
points_left = find_closest(xy, [xymeas1[0], xymeas1[1]], True)
points_right = find_closest(xy, [xymeas2[0], xymeas2[1]], True)
v = 'NA'
left_index = np.where(PD.x == points_left[0])[0][0]
right_index = np.where(PD.x == points_right[0])[0][0]
left_index = np.where(PD.x == PD.xyMeas1[0])[0][0]
right_index = np.where(PD.x == PD.xyMeas2[0])[0][0]
if left_index == right_index:
raise IndexError
if left_index > right_index:
left_index, right_index = right_index, left_index
if mode == 'mean':
y_val = np.mean(PD.y[left_index:right_index])
v = np.nanmean(PD.y[left_index:right_index])
elif mode == 'min':
y_val = np.min(PD.y[left_index:right_index])
v = np.nanmin(PD.y[left_index:right_index])
elif mode == 'max':
y_val = np.max(PD.y[left_index:right_index])
v = np.nanmax(PD.y[left_index:right_index])
elif mode == 'xmin':
v = PD.x[left_index + np.where(PD.y[left_index:right_index] == np.nanmin(PD.y[left_index:right_index]))[0][0]]
elif mode == 'xmax':
v = PD.x[left_index + np.where(PD.y[left_index:right_index] == np.nanmax(PD.y[left_index:right_index]))[0][0]]
else:
raise NotImplementedError('Error: Mode ' + mode + ' not implemented')
v = y_val
s = pretty_num(v)
except (IndexError, TypeError):
v = 'NA'
Expand Down

0 comments on commit 20beb54

Please sign in to comment.