Skip to content
Snippets Groups Projects
Commit 38290805 authored by Francois POLACK's avatar Francois POLACK
Browse files

feat: Added detrending by a polynomial function of given degree

	HeightMap save and load function to save to file and reload
	computation results
parent 2954ea5d
Branches
Tags
No related merge requests found
...@@ -40,7 +40,7 @@ import ZygoXYZ ...@@ -40,7 +40,7 @@ import ZygoXYZ
import ZygoDatx import ZygoDatx
from processing import * from processing import *
from activefigure import * from activefigure import *
import pickle
class HeightMap(object): class HeightMap(object):
'''! @brief The HeightMap object- '''! @brief The HeightMap object-
...@@ -57,9 +57,9 @@ class HeightMap(object): ...@@ -57,9 +57,9 @@ class HeightMap(object):
## the class constructor ## the class constructor
# #
# All public variables of the class are defined in this function # All public variables of the class are defined in this function
def __init__(self): def __init__(self,*,file=None):
self.filepath=''
## (float,float) The pixel dimension [x,y] (in m). ## (float,float) The pixel dimension [x,y] (in m).
self.pixel=[0,0] self.pixel=[0,0]
...@@ -86,7 +86,7 @@ class HeightMap(object): ...@@ -86,7 +86,7 @@ class HeightMap(object):
## the fit parameter (rx, pitch, ry, roll, z0) [unused] ## the fit parameter (rx, pitch, ry, roll, z0) [unused]
self.fitpars = None self.fitpars = None
## slope rms ## slope rms CPSD
self.srms = None self.srms = None
## slope PSD frequency array ## slope PSD frequency array
...@@ -129,6 +129,7 @@ class HeightMap(object): ...@@ -129,6 +129,7 @@ class HeightMap(object):
print("Unknown file type:", Path(filename).suffix) print("Unknown file type:", Path(filename).suffix)
return False return False
if datfile.openFile(filename): #if false invalid file message is printed if datfile.openFile(filename): #if false invalid file message is printed
self.filepath=filename
self.rawZ=self.z=np.ma.masked_invalid( datfile.getHeight()) self.rawZ=self.z=np.ma.masked_invalid( datfile.getHeight())
if self.z.size==0: if self.z.size==0:
return False return False
...@@ -276,6 +277,17 @@ class HeightMap(object): ...@@ -276,6 +277,17 @@ class HeightMap(object):
format(rx, pitch, ry, roll, Z0)) format(rx, pitch, ry, roll, Z0))
#end remove_toroid #end remove_toroid
def detrend_poly2D(self,powers, rot=0):
"""! Detrend the height data by removing a best fit polynomial in X and Y
@param powers the list of X and Y powers of the polynomial this list can be created by processing::makePowerList
@param rot an optional roration angle if the polynomial function in rad
the function will print the fit coefficients and return the fit residual in self.ze
"""
(coefficients, self.ze)=fit2D_poly(self.x,self.y,self.z, powers, rot)
print( "coefficients")
for index,(i,j) in enumerate(powers):
print("x^",i,"y^",j," x ",coefficients[index])
def compute_x_slope(self) : def compute_x_slope(self) :
"""! @brief Calculate on the ROI, the slope in the x direction, from the height data. """! @brief Calculate on the ROI, the slope in the x direction, from the height data.
...@@ -522,6 +534,14 @@ class HeightMap(object): ...@@ -522,6 +534,14 @@ class HeightMap(object):
plt.title('Cumulated RMS tangential slopes {}'.format(comment)) plt.title('Cumulated RMS tangential slopes {}'.format(comment))
# end plot_slope_rms # end plot_slope_rms
def save(self, filepath=''):
if not Path(filepath).parent.is_dir():
tkinter.Tk().withdraw()
filepath=filedialog.asksavefilename(title="open a hmap file", filetypes=(("superflat hmap","*.hmap"), ("all","*.*") ) )
print("writing to", filepath)
with open(filepath, 'wb') as f:
pickle.dump(self, f)
def testplot(self, data, scale=1, percent=5): def testplot(self, data, scale=1, percent=5):
uu=data*scale uu=data*scale
...@@ -550,3 +570,15 @@ class HeightMap(object): ...@@ -550,3 +570,15 @@ class HeightMap(object):
plt.xlabel('x (mm)') plt.xlabel('x (mm)')
plt.ylabel('y (mm)') plt.ylabel('y (mm)')
plt.title("testplot") plt.title("testplot")
# this function return a HeightMap object from a previously saved .hmap file
def load(self, filepath=''):
if not Path(filepath).is_file():
tkinter.Tk().withdraw()
filepath=filedialog.askopenfilename(title="open a hmap file", filetypes=(("superflat hmap","*.hmap"), ("all","*.*") ) )
if filepath=='' :
return None
print("loading from", filepath)
with open(filepath, 'rb') as f:
return pickle.load(f)
...@@ -228,3 +228,18 @@ def spline_interpolate(data, window=('tukey', 0.2) ): ...@@ -228,3 +228,18 @@ def spline_interpolate(data, window=('tukey', 0.2) ):
print ("percent of invalid data=", 100*invalids/(data.shape[0]*data.shape[1]) ) print ("percent of invalid data=", 100*invalids/(data.shape[0]*data.shape[1]) )
print("output shape", outdata.shape) print("output shape", outdata.shape)
return outdata return outdata
def makePowerList(nx,ny, N):
"""! Create a power list suitable for use with fit2D_poly where
@param nx is the maximum degree in X
@param ny is the maximum degree in Y
@param N is the maximum total degree in X and Y
"""
plist=[]
for npow in range(N+1):
for ix in range(min(npow,nx),-1,-1):
iy=npow-ix
if iy>ny :
break
plist.append((ix,iy))
return plist
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment