-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxi2d.py
409 lines (353 loc) · 13.2 KB
/
xi2d.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/python
# Filename: xi2d.py
import numpy as np
import matplotlib.pyplot as plt
import sys
import copy
def getflip(xi,n1d):
xiflip = xi.copy()
for i in range(n1d):
for j in range(n1d):
xiflip[i,j] = xi[j,i]
return xiflip
def inverseremapf(xiremapval,myxi):
return myxi.min()*10**(xiremapval*np.log10(myxi.max()/myxi.min()))
def remapf(xival,myxi):
return np.log10(xival/myxi.min())/np.log10(myxi.max()/myxi.min())
class xi2d:
def __init__(self,xi2dfname):
try:
rsig, rpi, xi = np.loadtxt(xi2dfname,unpack=True)
n1d = int(np.sqrt(len(rsig)))
if(np.fabs(n1d**2-len(rsig)) > 0):
print 'Have not implemented non-square xi2ds. Try back later!'
sys.exit(1)
## make it a list, could have come from more than one place.
self.fname = [xi2dfname]
self.n1d = n1d
self.nrsig = n1d
self.nrpi = n1d
self.ntot = len(rsig)
self.rsig = rsig
self.rpi = rpi
self.xi = xi
self.rsig1d = rsig.reshape(n1d,n1d)[:,0]
self.rpi1d = rpi.reshape(n1d,n1d)[0,:]
## compute linear spacings.
drsig = (self.rsig1d[1:]-self.rsig1d[:-1]).mean()
drpi = (self.rpi1d[1:]-self.rpi1d[:-1]).mean()
if (np.fabs(self.rsig1d[1:]-self.rsig1d[:-1] - drsig) > 0.0001*drsig).any():
## not linear binning!
self.drsig = 0.
else:
self.drsig = drsig
if (np.fabs(self.rpi1d[1:]-self.rpi1d[:-1] - drpi) > 0.0001*drpi).any():
## not linear binning!
self.drpi = 0.
else:
self.drpi = drpi
except:
# if(0==1):
print 'bad xi2d file.'
self = None
if(self):
## let's finde out if this is a data of theory curve.
fbase = xi2dfname.split('.'+xi2dfname.split('.')[-1])[0]
try:
ifpDR = open(fbase+'.DRopt2','r')
line = ifpDR.readline()
line = ifpDR.readline()
DDwgt = float(line.split(':')[1].split(',')[0])
RRwgt = float(line.split(':')[1].split(',')[1])
DRfac = float(DDwgt)/float(RRwgt)
ifpDR.close()
## set weight to sum of data weights.
self.weight = DDwgt
## didn't work, not sure why.
#except IOerror as e:
except:
# print 'could not open',fbase+'.DRopt2'
# print 'assuming this is a theory xi2d rather than data'
self.weight = 1.0
def __str__(self):
mystr = "(%d, %d) sized xi2d from [%s]. Range = [[%f, %f], [%f, %f]]\n" % (self.nrsig,self.nrpi,", ".join(self.fname),self.rsig.min(), self.rsig.max(),self.rpi.min(),self.rpi.max())
return mystr
def checkbinning(self,other):
"""
Returns 0 if binning is the same, 1 otherwise
"""
if(self.n1d != other.n1d):
return 1
if(self.ntot != other.ntot):
return 1
if(self.nrsig != other.nrsig):
return 1
if(self.nrpi != other.nrpi):
return 1
if((np.fabs(self.rsig - other.rsig) > 2.0e-4).any()):
return 1
if((np.fabs(self.rpi - other.rpi) > 2.0e-4).any()):
return 1
return 0
def __add__(self,other):
if(self.checkbinning(other) == 0):
xinew = copy.deepcopy(self)
xinew.xi = (self.xi*self.weight + other.xi*other.weight)/(self.weight+other.weight)
xinew.weight = (self.weight+other.weight)
## this needs to be generalized if you want to add ones that have already been added.
xinew.fname = self.fname + other.fname
else:
print 'add fail! binning misaligned'
xinew = None
return xinew
def xiinterp(self, rsigarr,rpiarr):
""" Quick n dirty bilinear interpolation in xi
Make this operate on input array of values.
should work for a single float value as well.
"""
if ((type(rsigarr) is type(0.0)) | (type(rsigarr) is type(0))):
rsigarr = np.array([rsigarr])
rpiarr = np.array([rpiarr])
## make sure it's type numpy
rsigarr = np.array(rsigarr)
rpiarr = np.array(rpiarr)
ix = (rsigarr-self.rsig1d.min())/self.drsig
tmp = np.where(ix < 0)[0]
ix[tmp] = 0
tmp = np.where(ix > (self.nrsig)-1)[0]
ix[tmp] = self.nrsig-1.001
iy = (rpiarr-self.rpi1d.min())/self.drpi
tmp = np.where(iy < 0)[0]
iy[tmp] = 0
tmp = np.where(iy > (self.nrpi)-1)[0]
iy[tmp] = self.nrpi-1.001
iix = np.array([int(val) for val in ix])
iiy = np.array([int(val) for val in iy])
assert (iix >= 0).all()
assert (iix <= self.nrsig-2).all()
assert (iiy >= 0).all()
assert (iiy <= self.nrpi-2).all()
xi11 = self.xi[iix*(self.nrpi) + iiy]
xi21 = self.xi[(iix+1)*(self.nrpi) + iiy]
xi12 = self.xi[iix*(self.nrpi) + iiy+1]
xi22 = self.xi[(iix+1)*(self.nrpi) + iiy+1]
## fraction of a bin left over.
fx = ix -iix
fy = iy -iiy
assert (fx >= 0.).all()
assert (fx <= 1.).all()
assert (fy >= 0.).all()
assert (fy <= 1.).all()
xival = xi11*(1.-fx)*(1.-fy) + \
xi21*fx*(1.-fy) + \
xi12*(1-fx)*(fy) + \
xi22*fx*(fy)
if(len(xival) == 1):
xival = xival[0]
return xival
def getclevels(self,ncontours=None,cratio=0.7,dc=0.02,ximin=1.0e-3,ximax=8.0,logopt=1,useself=False):
"""
Function to intelligently guess contour level values.
If useself = False, uses default or input values.
If True, uses min/max of self.xi
"""
if(useself == True):
if(logopt == 1):
ximax = self.xi.max()
xx = np.where(xi > 0.)
ximin = (self.xi[xx]).min()
if(ximax < 0.): logopt = 0
if(logopt != 1):
ximax = self.xi.max()
ximin = self.xi.min()
if ncontours is not None and ncontours > 2:
if(logopt == 1):
cratio = np.exp(np.log(ximin/ximax)/float(ncontours-1))
dc = 0
else:
cratio = 1.0
dc = (ximax-ximin)/float(ncontours-1)
cval = ximax
tclevlist = [cval]
if(logopt == 1):
ximin = np.fabs(ximin)
cnt = 0
while(cval > ximin*0.999 and cnt < 1000):
cval = cval*cratio
tclevlist.append(cval)
cnt += 1
else:
cnt = 0
while(cval > ximin-N.fabs(0.001*ximin) and cnt < 1000):
cval = cval-dc
tclevlist.append(cval)
cnt += 1
if ncontours is not None:
if len(tclevlist) > ncontours:
tclevlist = tclevlist[:ncontours]
return tclevlist
def symmetrize(self,symmetrizeopt=1):
n1d = self.n1d
if(symmetrizeopt == 1):
n1dS = 2*self.n1d
off = self.n1d
rsigS = np.zeros([n1dS,n1dS])
rpiS = np.zeros([n1dS,n1dS])
xiS = np.zeros([n1dS,n1dS])
for i in range(0,self.n1d):
for j in range(0,self.n1d):
rsigS[off+i, off+j] = self.rsig.reshape(n1d,n1d)[i,j]
rsigS[off-i-1, off+j] = -self.rsig.reshape(n1d,n1d)[i,j]
rsigS[off-i-1, off-j-1] = -self.rsig.reshape(n1d,n1d)[i,j]
rsigS[off+i, off-j-1] = self.rsig.reshape(n1d,n1d)[i,j]
rpiS[off+i, off+j] = self.rpi.reshape(n1d,n1d)[i,j]
rpiS[off-i-1, off+j] = self.rpi.reshape(n1d,n1d)[i,j]
rpiS[off-i-1, off-j-1] = -self.rpi.reshape(n1d,n1d)[i,j]
rpiS[off+i, off-j-1] = -self.rpi.reshape(n1d,n1d)[i,j]
xiS[off+i, off+j] = self.xi.reshape(n1d,n1d)[i,j]
xiS[off-i-1, off+j] = self.xi.reshape(n1d,n1d)[i,j]
xiS[off-i-1, off-j-1] = self.xi.reshape(n1d,n1d)[i,j]
xiS[off+i, off-j-1] = self.xi.reshape(n1d,n1d)[i,j]
rsigS1d = rsigS[:,0]
rpiS1d = rpiS[0,:]
else:
rsigS = self.rsig
rpiS = self.rpi
xiS = self.xi
n1dS = self.n1d
rsigS1d = self.rsig1d
rpiS1d = self.rpi1d
return rsigS1d, rpiS1d, xiS, n1dS
def addcontour(self,ax,symmetrizeopt=1,clevlist=[],color='k'):
"""
adds a contour plot of the current object to axis ax.
"""
if(clevlist == []):
clevlist=self.getclevels()
rsigS1d, rpiS1d, xiS, n1dS = self.symmetrize(symmetrizeopt)
xiflip = getflip(xiS.reshape(n1dS,n1dS),n1dS)
cc = ax.contour(rsigS1d,rpiS1d,xiflip,clevlist,colors=color,linewidths=2)
return cc
## things look screwy if you don't symmetrize, so we don't allow unsymmetrized for now.
## remap options do some sort of transformation on xi to make the contours nicer.
## so far
def adddensity(self,ax,fig,remapopt=1):
"""
Make a density plot.
remapopt = 0 does nothing to xi
remapopt = 1 makes density plot of log(xi) [this will die if xi < 0!]
add more options as needed.
"""
rsigS1d, rpiS1d, xiS, n1dS = self.symmetrize(1)
if(remapopt == 1):
# xiremap = (np.log10(xiS)/np.log10(self.xi.max()/self.xi.min()))
xiremap = remapf(xiS,xiS)
else:
xiremap = xiS
xiflip = getflip(xiremap.reshape(n1dS,n1dS),n1dS)
xl=self.rsig.max()
yl=self.rpi.max()
cax = ax.imshow(xiflip,extent=[-xl,xl,-yl,yl])
return cax
def makecontourplot(self,ax=None,symmetrizeopt=1,clevlist=[],color='k',span=None):
"""
Make a contour plot owned by this object if ax=None, returns the axis.
Otherwise, just add the contour plot to input axis ax and beautify.
Optional span = [xmin, xmax, ymin, ymax]
"""
if ax is None:
## we want aspect ratio to be 1.!!
ff = plt.figure(figsize=[6,6])
ax=ff.add_subplot(1,1,1)
else:
ff = None
self.addcontour(ax,symmetrizeopt,clevlist,color)
if span is None:
if(symmetrizeopt == 1):
span = [-self.rsig.max(), self.rsig.max(), -self.rpi.max(), self.rpi.max()]
else:
span = [self.rsig.min(), self.rsig.max(), self.rpi.min(), self.rpi.max()]
ax.axis(span)
if ff is not None:
ax.set_xlabel(r'$r_{\sigma} \, [h^{-1} {\rm Mpc}]$',fontsize=16)
ax.set_ylabel(r'$r_{\pi} \, [h^{-1} {\rm Mpc}]$',fontsize=16)
return ff, ax
def makedensityplotold(self,ax=None,span=None,remapopt=1,fontsize=16,fsize=6):
"""
Make a density plot owned by this object if ax=None, returns the axis.
Otherwise, just add the density plot to input axis ax and beautify.
Optional span = [xmin, xmax, ymin, ymax]
remapopt = 0 does nothing, remapopt = 1 takes a log of xi.
"""
if ax is None:
ff = plt.figure(figsize=[fsize,fsize])
ax=ff.add_subplot(1,1,1)
self.adddensity(ax,remapopt)
if span is None:
span = [-self.rsig.max(), self.rsig.max(), -self.rpi.max(), self.rpi.max()]
ax.axis(span)
# ax.set_xlabel(r'$r_{\sigma} \, [h^{-1} {\rm Mpc}]$',fontsize=fontsize)
# ax.set_ylabel(r'$r_{\pi} \, [h^{-1} {\rm Mpc}]$',fontsize=fontsize)
return ff, ax
def makedensityplot(self,ff=None,ax=None,span=None,remapopt=1,fontsize=16,fsize=6, \
showColorBar=False,\
cticksarr=np.array([0.021,0.2,2,20]),\
ctickslbl=['0.02','0.2','2','20']):
"""
Make a density plot owned by this object if ax=None, returns the axis.
Otherwise, just add the density plot to input axis ax and beautify.
Optional span = [xmin, xmax, ymin, ymax]
remapopt = 0 does nothing, remapopt = 1 takes a log of xi.
"""
if ax is None:
## we want aspect ratio to be 1.!!
ff = plt.figure(figsize=[fsize+2,fsize]); ax = ff.add_subplot(111,aspect='equal')
# else:
# print 'w ax input unwritten'
# return None
cax = self.adddensity(ax,remapopt)
if span is None:
span = [-self.rsig.max(), self.rsig.max(), -self.rpi.max(), self.rpi.max()]
ax.axis(span)
if showColorBar is True:
if remapopt == 1 and cticksarr is not None:
print 'remap cticks to ', cticksarr, remapf(cticksarr,self.xi)
cbar = ff.colorbar(cax, ticks=remapf(cticksarr,self.xi))
if len(ctickslbl) == len(cticksarr):
cbar.ax.set_yticklabels(ctickslbl)
if remapopt != 1 and cticksarr is not None:
cbar = ff.colorbar(cax, ticks=cticksarr)
if len(ctickslbl) == len(cticksarr):
cbar.ax.set_yticklabels(ctickslbl)
if cticksarr is None:
cbar = ff.colorbar()
# ax.set_xlabel(r'$r_{\sigma} \, [h^{-1} {\rm Mpc}]$',fontsize=fontsize)
# ax.set_ylabel(r'$r_{\pi} \, [h^{-1} {\rm Mpc}]$',fontsize=fontsize)
return ff, ax
if __name__ == "__main__":
if(0==1):
myxireal = xi2d(xi2dfname="makeHJcatalogv2zspace2.cat.zspace-1.xiell.nbins900.butterfly")
myxi = xi2d(xi2dfname="makeHJcatalogv2zspace2.cat.zspace2.xiell.nbins900.butterfly")
myxifine = xi2d(xi2dfname="makeHJcatalogv2zspace2.cat.zspace2.xiell.nbins3600.butterflyfine")
myxicoarse = xi2d(xi2dfname="makeHJcatalogv2zspace2.cat.zspace2.xiell.nbins225.butterflycoarse")
plt.figure()
# myxireal.plotcontour(color='k')
myxi.plotcontour(color='b')
myxifine.plotcontour(color='g')
myxicoarse.plotcontour(color='r')
plt.savefig("contoursv0.png")
## try reading in data.
xiN = xi2d(xi2dfname='../boss/zdistvXlogbinsompcleverLSbutterfly/outputmksamplelatestdr10v7/collidedBR-collate-cmass-dr10v7-N-FBBRang_xigrid.butterfly')
xiS = xi2d(xi2dfname='../boss/zdistvXlogbinsompcleverLSbutterfly/outputmksamplelatestdr10v7/collidedBR-collate-cmass-dr10v7-S-FBBRang_xigrid.butterfly')
if(xiN is not None and xiS is not None):
print xiN.weight, xiS.weight, xiN.weight/(xiN.weight+xiS.weight)
else:
print 'blerg errors'
sys.exit(1)
xidata = xiN + xiS
print xiN
print xiS
print xidata
### xi data makes sense now!
# for i in range(xidata.ntot):
# print xidata.rsig[i], xiN.rsig[i], xidata.rpi[i], xiN.rpi[i], xidata.xi[i], xiN.xi[i], xiS.xi[i], (xiN.xi[i]*xiN.weight+xiS.xi[i]*xiS.weight)/xidata.weight