Skip to content

Commit c48f7ee

Browse files
author
shcrela
committed
with the instruction on how to recover the corrected data
1 parent 72e08dc commit c48f7ee

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

UntiltPlane.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,53 @@
77

88
class UntiltPlane(object):
99
"""Level the plane if the sample is tilted.
10-
10+
1111
Left click on the image to select the points which will be used to create
1212
the offset plane. On right-click, this plane will be substracted from
1313
your image's original data, and the image will be redrawn with updated values.
1414
Attention: The resulting data will always have zero as its' minimal value.
1515
That is because after substracting the plane, we also substract the minimum
1616
of that result.
1717
Important Note: There is a strong assumption that the offset plane is linear.
18-
18+
1919
Parameters:
2020
-----------
2121
data: 2D numpy.ndarray
22-
Your input data. NaNs are fine.
22+
Your input data. NaNs are fine.
2323
a, b, c: floats
2424
The initial parameters of the flat offset plane.
2525
Defaults: `a = np.nanmin(data)`, `b = 0`, `c = 0`
2626
ftol: float
2727
What precision of the fit is satisfactory? Default is 1e-10
2828
**kwargs:
2929
Arguments passed on to matplotlib.figure
30-
30+
3131
Output:
3232
----------
3333
corrected_data: numpy.ndarray
3434
The updated ("untilted") values of your input array.
3535
plane_params: tuple(float, float, float)
3636
The tuple of fitted plane parameters (a, b, c). You can also recover them
3737
as `a`, `b` and `c`
38-
39-
40-
38+
39+
40+
4141
Example:
4242
---------
4343
```
4444
# You just need to run something like:
4545
>>>my_untilter = UntiltPlane(data)
4646
# Then, if you want to recover the corrected data:
47-
>>>corrected_data = my.untilter.corrected_data
47+
>>>corrected_data = my_untilter.corrected_data
4848
# If you wish to reconstruct the offset plane:
4949
>>>a, b, c = my_untilter.plan_params
5050
>>>offset_plane = np.fromfunction(lambda y, x: a + b*x + c*y, data.shape)
5151
```
5252
"""
53-
53+
5454
def __init__(self, data, a="min", b=0, c=0, ftol=1e-10, **kwargs):
55-
56-
self.data = data
55+
56+
self.data = data
5757
if a == "min":
5858
self.a = np.nanmin(self.data)
5959
else:
@@ -66,12 +66,12 @@ def __init__(self, data, a="min", b=0, c=0, ftol=1e-10, **kwargs):
6666
self.plane_data = []
6767
self.my_points = []
6868

69-
69+
7070
figsize = kwargs.pop("figsize", (12, 8))
7171
facecolor = kwargs.pop("facecolor", "oldlace")
7272
self.fig, self.aximg = plt.subplots(figsize=figsize, facecolor=facecolor,
7373
**kwargs)
74-
74+
7575
self.img = self.aximg.imshow(self.data, cmap="cividis")
7676
self.aximg.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
7777
self.aximg.set_title("Left-click to assign the points that will define\n"+
@@ -82,7 +82,7 @@ def __init__(self, data, a="min", b=0, c=0, ftol=1e-10, **kwargs):
8282

8383
def flat_plane_z(self, y, x):
8484
return self.a + self.b*x + self.c*y
85-
85+
8686
def optimize_flat_plane_z(self, independent_coords, a, b, c):
8787
x = independent_coords[:, 0]
8888
y = independent_coords[:, 1]
@@ -105,15 +105,15 @@ def onclick(self, event):
105105
elif event.button != 1:
106106
self.fig.canvas.mpl_disconnect(self.cid)
107107
self.detilt()
108-
108+
109109
def detilt(self):
110110
"""Remove the points, fit the (linear) plane, and update the image"""
111-
111+
112112
# Remove all the points:
113113
for point in self.my_points:
114114
point.remove()
115115
self.my_points = []
116-
116+
117117
clicked_data = np.array(self.plane_data)
118118
independent_coords = clicked_data[:, :2]
119119
measured_z = clicked_data[:, 2]
@@ -127,18 +127,18 @@ def detilt(self):
127127
initial_guess,
128128
method="lm",
129129
ftol=self.ftol)
130-
131-
130+
131+
132132
self.a, self.b, self.c = self.plane_params
133133
base_plane = np.fromfunction(self.flat_plane_z, self.data.shape)
134134
self.corrected_data = self.data - base_plane
135135
# Correct the offset (not sure whether we should do this or not)
136136
self.corrected_data -= np.nanmin(self.corrected_data)
137-
137+
138138
im_min, im_max = 0, np.nanmax(self.corrected_data)
139139
gs = gridspec.GridSpec(1,2)
140140
self.aximg.set_position(gs[0].get_position(self.fig))
141-
self.aximg.set_subplotspec(gs[0])
141+
self.aximg.set_subplotspec(gs[0])
142142
self.axcorr = self.fig.add_subplot(gs[1])
143143
self.axcorr.imshow(self.corrected_data, cmap="cividis")
144144
self.axcorr.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)

test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@
1616
raw = np.flipud(np.loadtxt(filename, delimiter=','))
1717

1818
my_plane = UntiltPlane(raw)
19+
20+
# %%
21+
######## Run the line below only afer you've finished the above ########
22+
######## (shift + enter to run cell by cell) ########
23+
my_corrected_data = my_plane.corrected_data

0 commit comments

Comments
 (0)