forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jackknife_limits.py
155 lines (100 loc) · 4.87 KB
/
jackknife_limits.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
import numpy as np
import matplotlib.pyplot as plt
import astropy.io.fits as fits
from collections import OrderedDict
from astropy.table import Table
jk_limits = {'JK0': {'ra_min': 129., 'ra_max': 133., 'dec_min': -2., 'dec_max': 3.},
'JK1': {'ra_min': 133., 'ra_max': 137., 'dec_min': -2., 'dec_max': 3.},
'JK2': {'ra_min': 137., 'ra_max': 141., 'dec_min': -2., 'dec_max': 3.},
'JK3': {'ra_min': 174., 'ra_max': 178., 'dec_min': -3., 'dec_max': 2.},
'JK4': {'ra_min': 178., 'ra_max': 182., 'dec_min': -3., 'dec_max': 2.},
'JK5': {'ra_min': 182., 'ra_max': 186., 'dec_min': -3., 'dec_max': 2.},
'JK6': {'ra_min': 211.5, 'ra_max': 215.5, 'dec_min': -2., 'dec_max': 3.},
'JK7': {'ra_min': 215.5, 'ra_max': 219.5, 'dec_min': -2., 'dec_max': 3.},
'JK8': {'ra_min': 219.5, 'ra_max': 223.5, 'dec_min': -2., 'dec_max': 3.}}
def set_jackknife(ras, decs, limits=None, debug=True):
result = np.array(['None'] * len(ras), dtype=str)
if limits == None:
limits = jk_limits
for strip in limits.keys():
ra_min = limits[strip]['ra_min']
ra_max = limits[strip]['ra_max']
dec_min = limits[strip]['dec_min']
dec_max = limits[strip]['dec_max']
in_strip = (ras >= ra_min) & (ras <= ra_max) & (decs >= dec_min) & (decs <= dec_max)
result[in_strip] = strip
if debug:
print(strip, limits[strip])
return result
def plot_jackknife(dat):
fig, ax = plt.subplots(1,1, figsize=(10,10))
for idx in np.unique(dat['JK']):
sub = dat[dat['JK'] == idx]
ax.scatter(sub['RA'], sub['DEC'], s=0.25, label=idx)
ax.set_xlabel('RA [deg.]')
ax.set_ylabel('DEC [deg.]')
ax.legend(frameon=True, ncol=6)
return fig, ax
def solve_jackknife(rand, ndiv=2):
'''
Splits up dat and rand into jackknife areas based on (ra, dec) in (ndiv x ndiv) chunks.
'''
njack = ndiv * ndiv
jk_volfrac = (njack - 1.) / njack
dpercentile = 100. / ndiv
percentiles = np.arange(dpercentile, 100. + dpercentile, dpercentile)
jk = 0
limits = OrderedDict()
for ra_per in percentiles:
# Given a vector V of length N, the q-th percentile of V is the q-th ranked value in a sorted copy of V.
# https://docs.scipy.org/doc/numpy-1.9.2/reference/generated/numpy.percentile.html
rahigh = np.percentile(rand[f'RANDOM_RA'], ra_per)
ralow = np.percentile(rand[f'RANDOM_RA'], ra_per - dpercentile)
print('{:.6f}\t{:.6f}'.format(ralow, rahigh))
for dec_per in percentiles:
isin = (rand['RANDOM_RA'] >= ralow) & (rand['RANDOM_RA'] <= rahigh)
dechigh = np.percentile(rand[f'RANDOM_DEC'][isin], dec_per)
declow = np.percentile(rand[f'RANDOM_DEC'][isin], dec_per - dpercentile)
print('\t{:.6f}\t{:.6f}'.format(declow, dechigh))
limits[f'JK{jk}'] = {'ra_min': float(ralow), 'ra_max': float(rahigh), 'dec_min': float(declow), 'dec_max': float(dechigh)}
jk += 1
jks = set_jackknife(rand['RANDOM_RA'], rand['RANDOM_DEC'], limits=limits)
return njack, jk_volfrac, limits, jks
def jackknife_mean(fpath):
print('Appending JK mean and error to lumfn. extension.')
with fits.open(fpath, mode='update') as hdulist:
nphi = 0
phis = []
for i, hdu in enumerate(hdulist):
# skip primary.
if i > 0:
phis.append(hdu.data['PHI_IVMAX'])
nphi += 1
phis = np.array(phis)
mean = np.mean(phis, axis=0)
err = np.std(phis, axis=0)
hdr = hdulist['LUMFN'].header
lumfn = hdulist['LUMFN'].data
lumfn = Table(lumfn, names=lumfn.names)
lumfn['PHI_IVMAX_JK'] = mean
lumfn['PHI_IVMAX_ERROR_JK'] = err
lumfn.pprint()
lumfn = fits.BinTableHDU(lumfn, name='LUMFN', header=hdr)
hdulist[1] = lumfn
hdulist.flush()
hdulist.close()
if __name__ == '__main__':
import pylab as pl
from astropy.table import Table
from findfile import findfile
field = 'G9'
version = 'v2'
dpath = findfile(ftype='gold', dryrun=False, survey='gama', version=version)
rpath = findfile(ftype='randoms', dryrun=False, field=field, survey='gama', version=version)
dat = Table.read(dpath)
dat = dat[dat['FIELD'] == field]
rand = Table.read(rpath)
dat, rand = set_jackknife(dat, rand, ndiv=4)
print(np.unique(dat['JK'].data))
fig, ax = plot_jackknife(dat)
fig.savefig('fig.pdf')