-
Notifications
You must be signed in to change notification settings - Fork 0
/
annular_sector_extraction_loop.py
292 lines (228 loc) · 8.9 KB
/
annular_sector_extraction_loop.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 24 14:04:38 2020
@author: jkin0004
"""
import numpy as np
import math
import rheoSANS_fitOpt_Functions as rsf
import matplotlib.pyplot as plt
def extractLoop():
indexSelect = '2-11'
indexNums = rsf.evaluate_files_list(indexSelect)
describer = 'ann_sim_200bns_thx0p015'
saveSet = '1'
centre = np.pi/2
width = np.pi/6
radius = 0.07
thx = 0.015
sans = rsf.sans2d()
sans.qmin = 0.007
# for nums in indexNums:
# sans.getData(str(nums))
# interp, zzq = sans.interpData(sans.expData.data)
# interp.sample = sans.expData.sample
# interp.shear = sans.expData.shear
#
# q, I_q, I_err = sector(sans.expData, centre=centre, width=width,
# save=saveSet, describer=describer)
#
# # q, I_q, I_err = annular(interp, radius=radius, thx=thx,
# # save=saveSet, describer=describer)
# del sans.expData
# fig = plt.figure()
#
# kwargs = {'xscale': 'linear', 'yscale': 'linear'}
#
# ax = fig.add_axes([1, 1, 1, 1], **kwargs)
#
# ax.errorbar(q, I_q, yerr=I_err, linestyle='', marker='o', markersize=1)
for nums in indexNums:
sans.getSim(str(nums))
sans.expData = sans.simImport
interp, zzq = sans.interpData(sans.expData.data)
interp.sample = sans.simImport.sample
print(nums)
interp.shear = sans.simImport.shear[0]
print(interp.shear)
# q, I_q, I_err = sector(sans.expData, centre=centre, width=width,
# save=saveSet, describer=describer)
q, I_q, I_err = annular(interp, radius=radius, thx=thx,
save=saveSet, describer=describer)
del sans.simImport
fig = plt.figure()
kwargs = {'xscale': 'linear', 'yscale': 'linear'}
# ax = fig.add_axes([1, 1, 1, 1], **kwargs)
#
# ax.errorbar(q, I_q, yerr=I_err, linestyle='', marker='o', markersize=1)
return
def sector(dataSet, centre, width, save='0', describer=None, nbins=100, loc='py_sect_radAve/'):
xcentre = 0
ycentre = 0
sectcent = centre # In radians
sectwid = width # In radians
# dataSet should be a sasView 2D data object
# data_sqrd = dataSet**2
mag = dataSet.q_data # q values
sectang = []
for i in range(len(mag)):
if dataSet.qy_data[i] > 0:
sectang.append(math.atan(dataSet.qx_data[i]/dataSet.qy_data[i]))
elif dataSet.qy_data[i] < 0:
sectang.append(math.atan(dataSet.qx_data[i]/dataSet.qy_data[i]) + np.pi)
sectang = np.array(sectang)
cwmax = sectcent+(0.5*sectwid) # Max bound for top sector
cwmin = sectcent-(0.5*sectwid) # Min bound for top sector
cwmax_ref = sectcent+(0.5*sectwid)+math.pi # Max bound for bottom sector
cwmin_ref = sectcent-(0.5*sectwid)+math.pi # Min bound for bottom sector
# Sorting according to angle
sortI = np.argsort(sectang)
sectang = sectang[sortI]
mag = mag[sortI]
err = dataSet.err_data[sortI]
data = dataSet.data[sortI]
seccrop_data = np.zeros_like(data)
seccrop_err = np.zeros_like(err)
seccrop_mag = np.zeros_like(mag)
seccrop_ang = np.zeros_like(sectang)
# Find logic gates
posLog = np.logical_and(cwmin < sectang, sectang < cwmax)
negLog = np.logical_and(cwmin_ref < sectang, sectang < cwmax_ref)
# Find values according to logic gates
seccrop_data[posLog] = data[posLog]
seccrop_err[posLog] = err[posLog]
seccrop_mag[posLog] = mag[posLog]
seccrop_ang[posLog] = sectang[posLog]
seccrop_data[negLog] = data[negLog]
seccrop_err[negLog] = err[negLog]
seccrop_mag[negLog] = mag[negLog]
seccrop_ang[negLog] = sectang[negLog]
zeros = seccrop_mag != 0 # Find zeros
# remove zeros
seccrop_data = seccrop_data[zeros]
seccrop_err = seccrop_err[zeros]
seccrop_mag = seccrop_mag[zeros]
seccrop_ang = seccrop_ang[zeros]
# Sort by ascending q
sortq = np.argsort(seccrop_mag)
seccrop_data = seccrop_data[sortq]
seccrop_err = seccrop_err[sortq]
seccrop_mag = seccrop_mag[sortq]
seccrop_ang = seccrop_ang[sortq]
# Make 100 bins spaced log linearly between min and max q
nbs = nbins
minMag = np.min(seccrop_mag)
maxMag = np.max(seccrop_mag)
logMinMag = np.log10(minMag)
logMaxMag = np.log10(maxMag)
logLinear = np.linspace(logMinMag, logMaxMag, nbs)
bins = 10**logLinear
binindex = np.zeros([nbs]) # number points summed per bin
bintotal = np.zeros([nbs]) # summed intensity
errtotal = np.zeros([nbs]) # summed error
for i in range(nbs - 1):
for ii in range(len(seccrop_data)):
if seccrop_mag[ii] >= bins[i]:
if seccrop_mag[ii] <= bins[i + 1]:
binindex[i] = binindex[i] + 1
bintotal[i] = bintotal[i] + seccrop_data[ii]
errtotal[i] = errtotal[i] + seccrop_err[ii]
# print(errtotal[i])
binZeros = binindex != 0
bins = bins[binZeros]
binindex = binindex[binZeros]
bintotal = bintotal[binZeros]
errtotal = errtotal[binZeros]
# print(errtotal)
binave = bintotal/binindex
errave = errtotal/binindex
# allerror = [err, seccrop_err, errtotal, errave]
if save == '1':
fileType = '.dat'
fileName = describer + '_' + \
str(dataSet.sample[0]) + 'wt' + '_' + str(dataSet.shear) + 'ps'
location = '../2D_annular_sector_extraction/' + loc
fullName = location + fileName + fileType
with open(fullName, 'wt') as fh:
fh.write("q I(q) err_I\n")
for x, y, z in zip(bins, binave, errave):
fh.write("%g %g %g\n" % (x, y, z))
return bins, binave, errave
def annular(dataSet, radius, thx, save='0', describer=None, nbins=100, loc='py_annular_exp/'):
radius = radius
thx = thx
mag = dataSet.q_data
# Draw a set of x,y points for the circles chosen
theta = np.linspace(0, 2*np.pi, 314)
cx = radius*np.cos(theta)
cy = radius*np.sin(theta)
cxouter = (radius + thx)*np.cos(theta)
cyouter = (radius + thx)*np.sin(theta)
# Capture points that fall within the annular rings based on their magnitude
I_ann = np.logical_and(radius < mag, mag < (radius+thx))
annul_x = dataSet.qx_data[I_ann]
annul_y = dataSet.qy_data[I_ann]
annul_I = dataSet.data[I_ann]
annul_err = dataSet.err_data[I_ann]
annul_mag = dataSet.q_data[I_ann]
# Calculate the angles for the obtained points. Zero is twleve o clock (vertically up on y-axis)
annul_ang = []
for i in range(len(annul_mag)):
if annul_y[i] > 0:
annul_ang.append(math.atan(annul_x[i]/annul_y[i]))
elif annul_y[i] < 0:
annul_ang.append(math.atan(annul_x[i]/annul_y[i]) + np.pi)
annul_ang = np.array(annul_ang)
# Sorts data to give as a function of increasing angle
sortI = np.argsort(annul_ang)
annul_ang = annul_ang[sortI]
annul_mag = annul_mag[sortI]
annul_x = annul_x[sortI]
annul_y = annul_y[sortI]
annul_I = annul_I[sortI]
annul_err = annul_err[sortI]
# Data binning
nbsa = nbins
# nbsa = 100
deltheta = 2*np.pi/nbsa
binsa = np.linspace(-np.pi/2, 3*np.pi/2, nbsa)
binindexa = np.zeros([nbsa]) # number points summed per bin
bintotala = np.zeros([nbsa]) # summed intensity
errtotala = np.zeros([nbsa]) # summed error
for i in range(nbsa - 1):
for ii in range(len(annul_mag)):
if annul_ang[ii] >= binsa[i]:
if annul_ang[ii] <= binsa[i + 1]:
binindexa[i] = binindexa[i] + 1
bintotala[i] = bintotala[i] + annul_I[ii]
errtotala[i] = errtotala[i] + annul_err[ii]
# print(errtotal[i])
binZeros = binindexa != 0
binsa = binsa[binZeros]
binindexa = binindexa[binZeros]
bintotala = bintotala[binZeros]
errtotala = errtotala[binZeros]
# print(errtotal)
binavea = bintotala/binindexa
erravea = errtotala/binindexa
# print(errave)
if save == '1':
fileType = '.dat'
# fileName = describer + '_' + \
# str(dataSet.sample[0]) + '_' + str(dataSet.shear[0][0:-14]) + 'ps'
fileName = describer + '_' + \
str(dataSet.sample[0]) + 'wt' + '_' + str(dataSet.shear) + 'ps'
location = '../2D_annular_sector_extraction/' + loc
fullName = location + fileName + fileType
with open(fullName, 'wt') as fh:
fh.write("q I(q) err_I\n")
for x, y, z in zip(binsa, binavea, binavea):
fh.write("%g %g %g\n" % (x, y, z))
annul_data = []
annul_data.append(annul_x)
annul_data.append(annul_y)
annul_data.append(annul_I)
annul_data.append(I_ann)
return binsa, binavea, erravea, annul_data
# return annul_x, annul_y, annul_I, I_ann