-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanonispyfit.py
executable file
·86 lines (65 loc) · 2.12 KB
/
nanonispyfit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 12 22:57:34 2016
@author: jack
"""
import numpy as _np
from scipy import optimize as _optimize
### internal functions not loaded by core_functions:
def _plane(a0, a1, b1, x0, y0):
return lambda x,y: a0 +a1*(x-x0) +b1*(y-y0)
def _planemoments(data):
a0 = _np.abs(data).min()
index = (data-a0).argmin()
x, y = data.shape
x0 = float(index / x)
y0 = float(index % y)
a1 = 0.0
b1 = 0.0
return a0, a1, b1, x0, y0
def _fitplane(data):
params = _planemoments(data)
errorfunction = lambda p: _np.ravel(_plane(*p)(*_np.indices(data.shape)) - data)
p, success = _optimize.leastsq(errorfunction, params)
return p
def _return_plane(params, data):
_fit_data = _plane(*params)
return _fit_data(*_np.indices(data.shape))
###externally visible functions loaded into gui:
def no_filter(scan_image):
return scan_image
def plane_fit_2d(scan_image, region=None):
'''
Parameters
----------
scan_image : 2d array
image to be plane fit.
region : list of 2 tuples, optional
pass region of image to plane fit: [(x1, y1), (x2, y2)]
Returns
-------
TYPE
plane-subtracted image array
'''
if region == None:
return scan_image - _return_plane(_fitplane(scan_image),scan_image)
else:
scan_fit_area = scan_image[region[0][0]:region[1][0], region[0][1]:region[1][1]]
return scan_image - _return_plane(_fitplane(scan_fit_area), scan_image)
def row_line_fit(scan_image):
x = _np.arange(scan_image.shape[0])
for i in range(scan_image.shape[1]):
fit = _np.polyfit(x,scan_image[i,],1)
fit = _np.polyval(fit, x)
scan_image[i,] -= fit
return scan_image
def subtract_average(scan_image):
return scan_image - _np.mean(scan_image)
def row_parabolic_fit(scan_image):
x = _np.arange(scan_image.shape[0])
for i in range(scan_image.shape[1]):
fit = _np.polyfit(x,scan_image[i,],2)
fit = _np.polyval(fit, x)
scan_image[i,] -= fit
return scan_image