-
Notifications
You must be signed in to change notification settings - Fork 2
/
polar_pattern.py
262 lines (199 loc) · 8.04 KB
/
polar_pattern.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
"""
Polar Reprojections
"""
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
def plot_polar_pattern(data, origin, boxs, rdf, drdf):
"""Plots an image reprojected into polar coordinages with the origin
at "origin" (a tuple of (x0, y0), defaults to the center of the image)"""
origin = [origin[1], origin[0]]
polar_grid, r, theta, pmrdf, psrdf = reproject_image_into_polar(data, origin, boxs)
a = 0.1
log_pattern = np.rot90(np.log(1+a*polar_grid))
rdf = np.array(rdf)
drdf = np.array(drdf)
print(pmrdf.shape, psrdf.max())
dpmrdf = np.linspace(drdf.min(), drdf.max(), pmrdf.shape[0])
print(dpmrdf.shape)
rdf /= rdf.max()
pmrdf /= pmrdf.max()
psrdf /= psrdf.max()
plt.figure()
plt.plot(drdf, rdf, c='b', alpha=1, linewidth=2)
plt.plot(dpmrdf, pmrdf, c='r', alpha=1, linewidth=2)
plt.plot(dpmrdf, psrdf, c='g', alpha=1, linewidth=2)
plt.imshow(log_pattern, cmap='binary', extent=(drdf.min(), drdf.max(), rdf.min(), rdf.max()+rdf.max()*.2), origin='lower')
plt.axis('auto')
plt.title('Diffraction Pattern Intensity Profile')
plt.xlabel('Scattering Vector (1/A)')
plt.ylabel('Intensity')
plt.yticks([])
plt.xlim(0, drdf.max())
plt.ylim(0, rdf.max()+rdf.max()*.2)
#plt.ylim(plt.ylim()[::-1])
# plt.xlabel('R Coordinate (pixels)')
# plt.title('Pattern in Polar Coordinates')
plt.show()
plt.savefig('polar.png', dpi=600, format='png')
def reproject_image_into_polar(data, origin, boxs):
"""Reprojects a 2D numpy array ("data") into a polar coordinate system.
"origin" is a tuple of (x0, y0) and defaults to the center of the image."""
Cr = np.around(origin)
print(origin)
#print(Cr[0]-boxs,Cr[0]+boxs,Cr[1]-boxs,Cr[1]+boxs)
#data = data[int(Cr[0]-boxs):int(Cr[0]+boxs),int(Cr[1]-boxs):int(Cr[1]+boxs)]
ny, nx = data.shape[:2]
print(data.shape[:2], boxs)
#plt.figure()
#plt.imshow(data,cmap='gray')
#origin = (nx//2, ny//2)
origin = origin[::-1]
print(origin)
# Determine that the min and max r and theta coords will be...
x, y = index_coords(data, origin=origin)
r, theta = cart2polar(x, y)
# Make a regular (in polar space) grid based on the min and max r & theta
r_i = np.arange(boxs) #linspace(0, boxs, nx)
theta_i = np.linspace(theta.min(), theta.max(), ny)
theta_grid, r_grid = np.meshgrid(theta_i, r_i)
# Project the r and theta grid back into pixel coordinates
xi, yi = polar2cart(r_grid, theta_grid)
xi += origin[0] # We need to shift the origin back to
yi += origin[1] # back to the lower-left corner...
xi, yi = xi.flatten(), yi.flatten()
coords = np.vstack((xi, yi)) # (map_coordinates requires a 2xn array)
#print(coords.shape)
# Reproject each band individually and the restack
# (uses less memory than reprojection the 3-dimensional array in one step)
bands = []
band = data.T
zi = sp.ndimage.map_coordinates(band, coords, order=1)
bands = (zi.reshape((int(boxs), ny)))
output = bands
print(output.shape[:2])
#plt.figure()
#plt.imshow(output)
#plt.show()
pmrdf, psrdf, prrdf= polar_mean(output)
return output[:,int(-boxs/2-boxs/3):int(-boxs/2+boxs/8)], r_i, theta_i, pmrdf, psrdf, prrdf #[:,-boxs/2-boxs/2.5:-boxs/2+boxs/2.5]
def index_coords(data, origin):
"""Creates x & y coords for the indicies in a numpy array "data".
"origin" defaults to the center of the image. Specify origin=(0,0)
to set the origin to the lower left corner of the image."""
ny, nx = data.shape[:2]
origin_x, origin_y = origin
x, y = np.meshgrid(np.arange(nx), np.arange(ny))
x -= origin_x.astype(np.int64)
y -= origin_y.astype(np.int64)
return x, y
def cart2polar(x, y):
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y, x)
return r, theta
def polar2cart(r, theta):
x = r * np.cos(theta)
y = r * np.sin(theta)
return x, y
def polar_mean(output):
#print(output.shape[:2])
pr, pt = output.shape[:2]
#print(output[:50,:250], output[-50:,:250])
out_mean = output.mean(axis=1)
ptv = []
index = (output[:,:50]>out_mean[50]).nonzero()
#print(index, len(index), len(index[0]))
#print(output[50].shape, output[50], out_mean[50]*.5)
for i in range(pr):
index = (output[i]>out_mean[i]*.4).nonzero()
#print(len(index))
ptv += [float(len(index[0]))]
#print(output[:50,:250], output[-50:,:250])
#print(len(ptv),ptv)
ptv = np.array(ptv)
ptv[ptv==0]=1
#print(len(ptv),ptv)
out_median = np.median(output, axis=1)
return out_mean, output.sum(axis=1)/ptv, out_median
def make_profile_rings(pro_intens, basis, origin, boxs, is_linear = 0):
print(origin)
#print(Cr[0]-boxs,Cr[0]+boxs,Cr[1]-boxs,Cr[1]+boxs)
#data = data[int(Cr[0]-boxs):int(Cr[0]+boxs),int(Cr[1]-boxs):int(Cr[1]+boxs)]
#nx = 2*len(pro_base)
#print(nx, boxs)
if is_linear:
print(origin, boxs)
origin = (len(basis), len(basis))
pro_base_l = basis
pro_intens_l = pro_intens
img_size = boxs*2 #-9
lin_index = np.linspace(0,2*len(basis),img_size)-len(basis)
else:
theta_2 = basis
origin = (len(theta_2), len(theta_2))
print(origin)
pro_base = (2*np.sin(((theta_2/180)*np.pi)/2.))/.5
pro_base_concat = np.concatenate((-pro_base[:0:-4],pro_base[::4]))
pro_base_concat = (pro_base_concat/pro_base.max()) * len(pro_base)
img_size = len(pro_base_concat)
# plt.figure()
# plt.plot(pro_base, pro_intens, 'r')
pro_base_l = np.linspace(0,max(pro_base),len(pro_base))
# plt.plot(pro_base_l, pro_intens, 'b')
pro_intens_l = np.interp(pro_base_l, pro_base, pro_intens)
# plt.plot(pro_base_l, pro_intens_l, 'g.')
#
# plt.figure()
# plt.plot(pro_base_concat)
lin_index = np.linspace(0,2*len(pro_base),img_size)-len(pro_base)
# plt.plot(lin_index)
# make a polar grid
origin_x, origin_y = origin
# x, y = meshgrid(pro_base_concat, pro_base_concat)
#x -= origin_x
#y -= origin_y
# r, theta = cart2polar(x, y)
x_l,y_l = np.meshgrid(lin_index,lin_index)
r_l, theta_l = cart2polar(x_l, y_l)
# print(r, r.max())
# plt.figure()
# plt.imshow(r, cmap='gray')
# plt.figure()
# #plt.plot(r[r.shape[0]//2])
# #plt.plot(r_l[r_l.shape[0]//2])
# plt.plot(-(r[r.shape[0]//2]-r_l[r_l.shape[0]//2]))
#plt.show()
# Make a regular (in polar space) grid based on the min and max r & theta
# r_i = linspace(0, boxs-1, nx)
# theta_i = linspace(0, 2*pi, nx)
# theta_grid, r_grid = meshgrid(theta_i, r_i)
# Project the r and theta grid back into pixel coordinates
#xi, yi = polar2cart(r_grid, theta_grid)
#xi += origin[0] # We need to shift the origin back to
#yi += origin[1] # back to the lower-left corner...
#xi, yi = xi.flatten(), yi.flatten()
r_l = r_l.flatten()
coords = np.vstack((r_l, np.zeros(len(r_l)))) # (map_coordinates requires a 2xn array)
print(coords)
#print(coords.shape)
# Reproject each band individually and the restack
# (uses less memory than reprojection the 3-dimensional array in one step)
bands = []
band = np.array([pro_intens_l,np.ones(len(pro_intens_l))]).T
zi = sp.ndimage.map_coordinates(band, coords, order=2)
bands = (zi.reshape((int(img_size), int(img_size))))
output = bands
#pmrdf, psrdf, prrdf= polar_mean(output)
#plt.figure()
#plt.imshow(output, cmap='gray')
#plt.plot(origin[0]/4,origin[1]/4,'+')
#plt.show()
return output
if __name__ == "__main__":
data = np.loadtxt("../tem/temfig/au_pro.txt")
theta_2 = data[:,0]
intensity = data[:,1]
intensity /= intensity.max()
print(data.shape, len(theta_2), len(intensity))
make_profile_rings(intensity, theta_2, (100,100), 200)