-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_correlation_matrix.py
167 lines (120 loc) · 5.88 KB
/
generate_correlation_matrix.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
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import time
import os
from adds import correlation_from_covariance, read_predictions
font = {'family' : 'serif', 'weight' : 'normal','size' : 34}
matplotlib.rc('font', **font)
cl = ['red', 'blue', 'orange', 'purple', 'cyan', 'grey',
'green','magenta', 'tan', 'green','magenta', 'tan',
'green','magenta', 'tan']
lw = 2
skewer_length = 20
trim = 16
dir_dataset = 'bh2igm/dataset_files/'
dir_output = 'bh2igm/'
# dataset_file_filter = 'ml_outputs_J'
dir_output = 'bh2igm/ml_outputs_mflux0.1350_fwhm6.00_bins1067_noise0.02_z5.00/'
#dir_output = 'bh2igm/ml_outputs_mflux0.3216_fwhm6.00_bins1014_noise0.02_z4.40/'
#dir_output = 'bh2igm/ml_outputs_trim16_mflux0.4255_fwhm6.00_bins978_noise0.02_z4.00/'
#dir_dataset = 'bh2igm/dataset_files/'
models = [
'planck1_20_1024', 'planck1_20_1024_cold', 'planck1_20_1024_hot',
'planck1_20_1024_zr525', 'planck1_20_1024_zr525_cold', 'planck1_20_1024_zr525_hot',
'planck1_20_1024_zr675', 'planck1_20_1024_zr675_cold', 'planck1_20_1024_zr675_hot',
'planck1_20_1024_zr750', 'planck1_20_1024_zr750_cold', 'planck1_20_1024_zr750_hot',
'planck1_20_1024_g10', 'planck1_20_1024_g14', 'planck1_20_1024_g16',
]
redshifts = [4.0]
#models = ["nyx_zre6", "nyx_zre7", "nyx_zre7_hot", "nyx_zre7_cold", "nyx_zre8"]
###############################################################################
def concat_models(densityw_res, tempw_res, pixels, realizations=40):
intialization = False
for ri in range(realizations):
si = np.random.randint(0, pixels)
concat = np.hstack((
np.roll(densityw_res, si, axis=1), np.roll(tempw_res, si, axis=1)))
if intialization==False:
intialization=True
concat_dataset = concat
else:
concat_dataset = np.vstack((concat_dataset, concat))
return concat_dataset
def get_cov(dir_dataset, dir_output, model, redshift):
flux,_,_, densityw, tempw, densityw_mean, densityw_upper_1sigma , \
densityw_lower_1sigma, tempw_mean, tempw_upper_1sigma , tempw_lower_1sigma = \
read_predictions(dir_dataset, dir_output, model, redshift)
# ##remove trimmed pixels
# tempw = tempw[:,trim:]
# densityw = densityw[:,trim:]
# flux = flux[:,trim:]
densityw_std = densityw_upper_1sigma - densityw_mean
tempw_std = tempw_upper_1sigma - tempw_mean
pixels = densityw_std.shape[1]
pixelsh = np.int32(pixels/2)
start_time = time.time()
#X - E(X) = covaraince
densityw_res = (densityw - densityw_mean)/densityw_std
tempw_res = (tempw - tempw_mean)/tempw_std
concat_dataset = concat_models(densityw_res, tempw_res, pixels, realizations=40)
print('sec ',time.time() - start_time, concat_dataset.shape)
corr = np.cov(concat_dataset, rowvar=False)
corr = correlation_from_covariance(corr)
print('corr', np.min(corr), np.max(corr), corr.shape)
save_file = dir_output+'corr_'+model+'.npy'
print('saving ', save_file)
with open(save_file, 'wb') as f:
np.save(f, corr)
return corr
# file_list = os.listdir(dir_output)
# dir_output_folder_list = [filename for filename in file_list
# if dataset_file_filter in filename]
# for file_folder in dir_output_folder_list:
# for mi, model in enumerate(models):
# string_split = (file_folder).split('_')
# quasar = string_split[2]
# redshift = np.float32(string_split[7][1:])
for zi, redshift in enumerate(redshifts):
for mi, model in enumerate(models):
corr = get_cov(dir_dataset, dir_output, model, redshift)
###########################################################################
axis = np.arange(np.int32(corr.shape[1]/2)) * (skewer_length/np.int32(corr.shape[1]/2))
axish = np.arange(np.int32(corr.shape[1]/4)) * (skewer_length/np.int32(corr.shape[1]/4))
half_pixels = np.int32(corr.shape[1]/2)
# Where we want the ticks, in pixel locations
ticks = np.linspace(0, corr.shape[1]/2, 5)
# What those pixel locations correspond to in data coordinates.
# Also set the float format here
ticklabels = [i for i in np.int32(ticks*skewer_length/np.int32(corr.shape[1]/2))]
fig, ax = plt.subplots(2, 2, figsize=(12, 12))
fig.subplots_adjust(wspace=0.001, hspace=0.001)
ax[0,0].imshow(corr[:half_pixels, :half_pixels], vmin=0, vmax=1, label=model)
ax[0,0].set_xticklabels([])
ax[0,0].set_yticks(ticks)
ax[0,0].set_yticklabels(ticklabels)
ax[0,0].set_ylabel(r'${\rm Mpc/h}$')
ax[0,0].set_aspect("auto")
cbar = ax[0,1].imshow(corr[:half_pixels, half_pixels:], vmin=0, vmax=1, label=model)
ax[0,1].set_xticklabels([])
ax[0,1].set_yticklabels([])
ax[0,1].set_aspect("auto")
cax = fig.add_axes([.95, .2, 0.02, .6])
fig.colorbar(cbar, orientation='vertical', cax=cax)
ax[1,0].imshow(corr[half_pixels:, :half_pixels], vmin=0, vmax=1, label=model)
ax[1,0].set_xticks(ticks)
ax[1,0].set_xticklabels(ticklabels)
ticklabels[0] = ''
ax[1,0].set_yticks(ticks)
ax[1,0].set_yticklabels(ticklabels)
ax[1,0].set_xlabel(r'${\rm Mpc/h}$')
ax[1,0].set_ylabel(r'${\rm Mpc/h}$')
ax[1,0].set_aspect("auto")
ax[1,1].imshow(corr[half_pixels:, half_pixels:], vmin=0, vmax=1, label=model)
ax[1,1].set_yticklabels([])
ax[1,1].set_xticks(ticks)
ticklabels[0] = ''
ax[1,1].set_xticklabels(ticklabels)
ax[1,1].set_xlabel(r'${\rm Mpc/h}$')
ax[1,1].set_aspect("auto")
fig.savefig('corr_'+model+'.pdf', format='pdf', dpi=90, bbox_inches = 'tight')