-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakedat.py
135 lines (107 loc) · 4.53 KB
/
makedat.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
import numpy as np
import csv
import os
import pyccl as ccl
from astropy import units as u
from astropy.coordinates import SkyCoord
from argparse import ArgumentParser
import tqdm
# Need to set the scratch database and write directly here
# if we want to remove the gunzip in the scripts
# scratch_dirbase = '/astro/u/anze/work/prakruth/lya-4pcf/scratch'
h=.7
cosmo = ccl.Cosmology(
Omega_c=0.27, Omega_b=0.045, h=h, sigma8=0.8, n_s=0.96)
def process_sample(sample, north, test, shuffle=False, seed=None):
alpha = .1
lya_signal = []
lya_random = []
qid, ras, decs, zs, weight, delt = sample.T
if not test:
if north:
w=np.where((ras>1)&(ras<5))
else:
w=np.where((ras<=1)|(ras>=5))
else:
if north:
w=np.where((ras>2)&(ras<2.1))
else:
w=np.where((ras>2.5)|(ras<2.6))
qid, ras, decs, zs, weight, delt = sample[w].T
print (f"Cutting to {len(ras)} pixels. North = {north} Test = {test}")
if shuffle:
print ('shuffling ra/dec')
rng = np.random.default_rng(seed=seed)
qtable = np.load('./qidZ_lintable.npy')
# tqid, tra, tdec = qtable.T
tqid, tra, tdec, tz, tzbin = qtable.T
tqid = tqid.astype(int) ## they just happen to go 1..len()
assert(np.all(tqid == np.arange(len(tqid))))
qid = qid.astype(int)-1
qid_ndx = np.unique(qid)
print ('total qid:',len(tqid), 'in sample:', len(qid_ndx))
xmap_diff = 1000000
xmap = np.zeros(len(tqid),int)+xmap_diff #to make sure we throw index error if fuck up
print(tzbin, np.unique(tzbin))
for z in np.unique(tzbin):
bin_z = np.where(tzbin == z)[0]
remap = rng.permutation(bin_z)
xmap[bin_z] = remap
print('unmodded xmaps', np.where(xmap == xmap_diff)[0])
# remap = rng.permutation(len(qid_ndx))
# xmap[qid_ndx] = qid_ndx[remap]
##debug: null reshuflle## remap = np.arange(len(qid_ndx))
new_ras = np.zeros(len(ras))
new_decs = np.zeros(len(decs))
new_ras = tra[xmap[qid]]
new_decs = tdec[xmap[qid]]
assert (np.all(new_ras!=0))
ras = new_ras
decs = new_decs
print ("done shuffling")
print ("calculating distances...")
sig_weights = weight * (1+ alpha * delt) # Signal
rand_weights = -1 * weight
radii = ccl.comoving_radial_distance(cosmo, 1/(1+zs))
c = SkyCoord(ras*u.rad, decs*u.rad, distance=radii*u.Mpc)
goodndx = (weight > .1) * (np.abs(delt) < 5)
goodc = c[goodndx]
goodsw = sig_weights[goodndx]
goodrw = rand_weights[goodndx]
glen = len(goodc)
pos = goodc.cartesian.xyz.T
lya_signal = np.zeros((glen, 4))
lya_random = np.zeros_like(lya_signal)
lya_signal[:,:3] = pos
lya_signal[:,3] = goodsw
lya_random[:,:3] = pos
lya_random[:,3] = goodrw
return lya_signal, lya_random
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("mode", choices=["SIGNAL", "SIMUL"])
parser.add_argument("-n", type=int, default=10)
parser.add_argument("--test", action="store_true")
args = parser.parse_args()
# print(args)
print ("reading...")
fsample = np.load('./qid_obj.npy')
if args.mode == "SIGNAL":
# ls, lr = process_sample(fsample, north=True, test=args.test)
# np.savetxt('./data/signal/sigN.data.gz', ls, fmt='%1.6f', delimiter=' ')
# np.savetxt('./data/signal/ranN.ran.00.gz', lr, fmt='%1.6f', delimiter=' ')
ls, lr = process_sample(fsample, north=False, test=args.test)
np.savetxt('./data/signal/sigS.data.gz', ls, fmt='%1.6f', delimiter=' ')
np.savetxt('./data/signal/ranS.ran.00.gz', lr, fmt='%1.6f', delimiter=' ')
else:
# os.makedirs(f'./data/simul{args.n}N', exist_ok=True)
os.makedirs(f'./data/simul{args.n}S', exist_ok=True)
print(f"Made dir simul{args.n}")
# ms, mr = process_sample(fsample,north=True, test=args.test, shuffle=True, seed=args.n)
# print ('writing N...')
# np.savetxt(f'./data/simul{args.n}N/sig.data.gz', ms, fmt='%1.6f', delimiter=' ')
# np.savetxt(f'./data/simul{args.n}N/ran.ran.00.gz', mr, fmt='%1.6f', delimiter=' ')
ms, mr = process_sample(fsample,north=False, test=args.test, shuffle=True, seed=1000000+args.n)
print ('writing S...')
np.savetxt(f'./data/simul{args.n}S/sig.data.gz', ms, fmt='%1.6f', delimiter=' ')
np.savetxt(f'./data/simul{args.n}S/ran.ran.00.gz', mr, fmt='%1.6f', delimiter=' ')