-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFL_test.py
382 lines (265 loc) · 9.88 KB
/
FL_test.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import numpy as np
import matplotlib.pyplot as plt
from MANGA_bench import Noodle
import ConfigParser
import pyfits
import ADEUtils as ADE
from glob import glob
import pickle
import os
from matplotlib.backends.backend_pdf import PdfPages as PDF
def fratio(searchstr,EEfigs,apfigs,bigfig,EE=0.50):
'''pronounced frat-eeo, not f ratio'''
print int(EE*100)
pp = PDF(EEfigs)
pp2 = PDF(apfigs)
pp3 = PDF(bigfig)
inifiles = glob(searchstr)
datadict = get_sizes(inifiles,pp,EE=EE)
pp.close()
ratiodict = {}
aps = np.array([])
ratios = np.array([])
for apsize in datadict.keys():
x = np.array(datadict[apsize].keys(),dtype=float)
r = np.array(datadict[apsize].values())
fit_coef = ADE.polyclip(x,r,1,niter=100).c
slope = fit_coef[0]
N = (2.*slope)**-1
fit = np.poly1d(fit_coef)
ratiodict[apsize] = {'f/#':N,
'fl':float(apsize)/N}
aps = np.append(aps,float(apsize))
ratios = np.append(ratios,N)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,r,marker='s',linestyle='')
fitx = np.linspace(x.min(),x.max(),50)
ax.plot(fitx,fit(fitx),'k:',label='linear fit')
ax.set_xlabel('back distance - C [mm]')
ax.set_ylabel('beam radius [mm]')
ax.legend(loc=0)
ax.set_title('Aperture: {:n} mm\nN: {:3.2f}'.format(float(apsize),N))
pp2.savefig(fig)
pp2.close()
ratiofit = ADE.polyclip(aps**-1,ratios,1,niter=50,clip_high=1,clip_low=1)
# ratiofit = np.poly1d(np.polyfit(aps**-1,ratios,1))
ratioslope = ratiofit.c[0]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(aps**-1,ratios,marker='s',linestyle='')
fitaps = np.linspace(aps.min(),aps.max(),50)
ax.plot(fitaps**-1,ratiofit(fitaps**-1),':',label='linear fit')
ax.set_xlabel('1/D [$mm^{-1}$]')
ax.set_ylabel('f-ratio')
# ax.set_ylim(0, ratiofit(fitaps.max())*1.1)
ax.legend(loc=0)
ax.set_title('slope = {:n} mm'.format(ratioslope))
pp3.savefig(fig)
pp3.close()
return ratiodict
def EE_test(searchstr,EElist):
inifiles = glob(searchstr)
FLlist = np.array([])
for EE in EElist:
print '************ EE'+str(int(EE*100))+' ***************'
datadict = get_sizes(inifiles,EErad=EE)
ratiodict = {}
aps = np.array([])
ratios = np.array([])
for apsize in datadict.keys():
x = np.array(datadict[apsize].keys(),dtype=float)
r = np.array(datadict[apsize].values())
fit_coef = np.polyfit(x,r,1)
slope = fit_coef[0]
fit = np.poly1d(fit_coef)
ratiodict[apsize] = {'f/#':(2.*slope)**-1,
'fl':float(apsize)/(2.*slope)}
aps = np.append(aps,float(apsize))
ratios = np.append(ratios,(2.*slope)**-1)
ratiocoef = np.polyfit(aps**-1,ratios,1)
ratioslope = ratiocoef[0]
ratiofit = np.poly1d(ratiocoef)
FLlist = np.append(FLlist,ratioslope)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.array(EElist),FLlist)
fig.show()
return np.array(EElist),FLlist
def ratio_boot(ratiodict,numtrys):
D = np.array([])
N = np.array([])
for ap in ratiodict.keys():
D = np.append(D,float(ap))
N = np.append(N,ratiodict[ap]['f/#'])
slopes = np.array([])
for i in range(numtrys):
sampleidx = np.random.randint(D.size, size = D.size)
tempD = D[sampleidx]
tempN = N[sampleidx]
slopes = np.append(slopes,np.polyfit(D**-1,N,1)[0])
print 'After {:n} trys:\n\tmean: {:3.4f} mm\n\tstd: {:3.4f} mm'.\
format(numtrys,slopes.mean(),slopes.std())
return slopes
def get_sizes(inifiles,pp,EE=0.5):
datadict = {}
for ini in inifiles:
#for L2
# apsize = ini.split('m')[0]
# backd = ini.split('m')[2][1:5]
#for L3
apsize = ini.split('m')[0].split('_')[1]
backd = ini.split('m')[2][1:5]
print (apsize,backd)
datafile = yaki(ini)
print 'Got {}, reducing...'.format(datafile)
radius = omnom(datafile,pp,EEcut=EE)
try: datadict[apsize][backd] = radius
except KeyError: datadict[apsize] = {backd: radius}
return datadict
def yaki(ini):
options = ConfigParser.ConfigParser()
options.read(ini)
picklename = '.'.join(ini.split('.')[:-1])+'.pkl'
try:
N = pickle.load(open(picklename,'rb'))
print "loaded pickel from {}".format(picklename)
except IOError:
N = Noodle(options)
N.build_run()
pickle.dump(N,open(picklename,'wb'))
key = N.ratios.keys()[0]
# for L3
return N.ratios[key]['data']['V']['L3']['final']
#for L2
# return N.ratios[key]['data']['V']['direct']['final']
def omnom(fitsfile,pp,EEcut=0.5,fitsexten=0):
data = pyfits.open(fitsfile)[fitsexten].data
r, sb, err = ADE.annulize(data,300)
# r *= 0.024
flux = np.cumsum(sb)
EE = flux/flux.max()
cutr = np.where(EE >= EEcut)[0][0]
EEfit = np.poly1d(np.polyfit(r[:cutr],EE[:cutr],2))
fitr = np.linspace(r.min(),r.max(),500)
fitEE = EEfit(fitr)
r1 = np.interp(1.0,fitEE,fitr)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(r,EE,marker='.',linestyle='',markersize=0.5)
ax.plot(fitr,fitEE,':')
ax.set_xlabel('r [mm]')
ax.set_ylabel('EE')
ax.axvline(r1,linestyle='-',alpha=0.4)
ax.axhline(1.0,linestyle=':',color='k',alpha=0.2)
ax.set_ylim(0,1.1)
ax.set_xlim(0,1.3*r1)
ax.set_title('{}\nr: {:3.2f} mm'.format(fitsfile,r1))
pp.savefig(fig)
return r1
######################################
def nomlom(fitsfile,x,fitsexten=0):
data = pyfits.open(fitsfile)[0].data
r, sb, err = ADE.annulize(data,300)
# r *= 0.024
flux = np.cumsum(sb)
EE = flux/flux.max()
radii = np.array([])
print ' Extracting radii for EE = 1/x:'
for i in x:
print '\tx = {:n}'.format(i)
targetEE = 1./i
radii = np.append(radii,np.interp(targetEE,EE,r))
return radii
def size_get(inifiles):
datadict = {}
x = np.arange(1.05,5,0.5)
for ini in inifiles:
#for L2
apsize = ini.split('m')[0]
backd = ini.split('m')[2][1:5]
#for L3
# apsize = ini.split('m')[0].split('_')[1]
# backd = ini.split('m')[2][1:5]
print (apsize,backd)
datafile = yaki(ini)
print 'Got {}, reducing...'.format(datafile)
radii = nomlom(datafile,x)
for i,r in zip(x,radii):
try:
datadict[apsize][i] = np.vstack((datadict[apsize][i],np.array([backd,r],dtype=np.float32)))
except KeyError:
try: datadict[apsize][i] = np.array([backd,r],dtype=np.float32)
except KeyError:
datadict[apsize] = {i: np.array([backd,r],dtype=np.float32)}
return datadict
def sortio(searchstr,Nfigs,apfigs,bigfig):
inifiles = glob(searchstr)
datadict = size_get(inifiles)
ratiodict = {}
aps = np.array([])
ratios = np.array([])
pp = PDF(Nfigs)
pp2 = PDF(apfigs)
pp3 = PDF(bigfig)
for apsize in datadict.keys():
print '\nApsize = {:n} mm:\n\t{:7}{:7}\n\t'.format(int(apsize),'x','N_x')+'-'*10
xvec = np.array([])
Nprime = np.array([])
sortedkeys = datadict[apsize].keys()[:]
sortedkeys.sort()
for x in sortedkeys[::-1]:
d = datadict[apsize][x].T[0]
r = datadict[apsize][x].T[1]
Npcoef = np.polyfit(d,r,1)
xvec = np.append(xvec,x)
Nprime = np.append(Nprime,(2.*Npcoef[0])**-1)
print '\t{:4.3f} {:4.3f}'.format(x,(2.*Npcoef[0])**-1)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(d,r,marker='s',linestyle='')
fitvec = np.linspace(d.min(),d.max(),50)
ax.plot(fitvec,np.poly1d(Npcoef)(fitvec),':')
ax.set_title('Aperture: {:3n} mm\n$x={:3.2f}\Rightarrow EE={:3.2f}$\n$N_x$: ${:3.2f}$'.\
format(int(apsize),x,1./x,(2.*Npcoef[0])**-1),
fontsize=10)
ax.set_xlabel('d')
ax.set_ylabel('r')
pp.savefig()
bigNcoef = np.polyfit(xvec,Nprime,1)
print bigNcoef
N = bigNcoef[0]
print (2.*N)**-1
Nfit = np.poly1d(bigNcoef)
ratiodict[apsize] = {'f/#':N,
'fl':float(apsize)*N}
aps = np.append(aps,float(apsize))
ratios = np.append(ratios,N)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(xvec,Nprime,marker='s',linestyle='')
fitx = np.linspace(xvec.min(),xvec.max(),50)
ax.plot(fitx,Nfit(fitx),'k:',label='linear fit')
ax.set_xlabel('$x$',fontsize=14)
ax.set_ylabel('$N_x$',fontsize=14)
ax.legend(loc=0)
ax.set_title('Aperture: {:n} mm\nN: {:3.4f}'.format(float(apsize),N))
pp2.savefig(fig)
pp.close()
pp2.close()
ratiofit = ADE.polyclip(aps**-1,ratios,1,niter=50,clip_high=1,clip_low=1)
# ratiofit = np.poly1d(np.polyfit(aps**-1,ratios,1))
ratioslope = ratiofit.c[0]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(aps**-1,ratios,marker='s',linestyle='')
fitaps = np.linspace(aps.min(),aps.max(),50)
ax.plot(fitaps**-1,ratiofit(fitaps**-1),':',label='linear fit')
ax.set_xlabel('1/D [$mm^{-1}$]')
ax.set_ylabel('f-ratio')
# ax.set_ylim(0, ratiofit(fitaps.max())*1.1)
ax.legend(loc=0)
ax.set_title('slope = {:n} mm'.format(ratioslope))
pp3.savefig(fig)
pp3.close()
return ratiodict