-
Notifications
You must be signed in to change notification settings - Fork 0
/
qso_DRW_plotting_2_bias.py
283 lines (215 loc) · 9.76 KB
/
qso_DRW_plotting_2_bias.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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 18 17:48:01 2014
@author: suberlak
plotting log(tau) vs log(sigma) for the
simulated DRW sample
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from math import isinf
# set_prior = TRUE in Javelin
#results='qso_drw_analysis/javelin_drw_test_chain_results_with_prior_all.txt'
# set_prior = FALSE in Javelin
#results='qso_drw_analysis/javelin_drw_test_chain_results_no_prior_603.txt'
prior = 'else' # or 'no'
length = 'long' # or 'medium' 'long'
n_errors= 2 # also need to change number of error when calling function err_rows_extract below
# Define plot size
x_min = -0.15
x_max = 2
y_min = -0.3
y_max = 2
if prior== 'yes':
if length == 'short' : # results with Javelin Prior , short LC length
results_jav = 'qso_drw_analysis/javelin_drw_test_chain_results_with_prior_all.txt'
results_ch='qso_drw_S_M_Chelsea_results/fitsshort.dat'
n_errors = 3
if length == 'medium' : # results with Javelin Prior , medium LC length
results_jav = 'qso_drw_medium_analysis/javelin_drw_test_chain_results_with_prior_all.txt'
results_ch='qso_drw_S_M_Chelsea_results/fitsmed.dat'
n_errors = 2
if prior =='no':
if length == 'short' : # results without Javelin Prior , short LC length
results_jav = 'qso_drw_analysis/javelin_drw_test_chain_results_no_prior_all.txt'
results_ch='qso_drw_S_M_Chelsea_results/fitsshort.dat'
n_errors = 3
if length == 'medium' : # results without Javelin Prior , medium LC length
results_jav = 'qso_drw_medium_analysis/javelin_drw_test_chain_results_no_prior_all.txt'
results_ch='qso_drw_S_M_Chelsea_results/fitsmed.dat'
n_errors = 2
if prior == 'else' :
results = 'qso_drw_S_M_Chelsea_results/fitslong.dat'
src = 'Chelsea'
n_errors = 3
fig_name=['qso_drw_S_M_Chelsea_results/drw_'+length+'_prior-'+prior+'_err','_log_sigma_vs_log_tau.png']
print 'Using results from', results, 'n_errors=,', n_errors
output_ch = np.loadtxt(results_ch, dtype='str')
output_jav = np.loadtxt(results_jav , dtype='str')
# CHELSEA RESULTS
name_ch_raw =output_ch[:,0].astype(str)
log_10_tau = output_ch[:,2].astype(np.float) # NOTE : already log_10
log_10_sigma_hat = output_ch[:,3].astype(np.float)
tau = np.power(10,log_10_tau)
sigma_hat = np.power(10,log_10_sigma_hat)
sigma = sigma_hat * np.sqrt(tau / (2.0*365.0))
log_10_sigma = np.log10(sigma)
# JAVELIN RESULTS
name_jav =output_jav[:,0].astype(str)
sigma_max_jav = output_jav[:,1].astype(np.float) # extracted as max value of the chain 2D histogram
tau_max_jav = output_jav[:,2].astype(np.float)
sigma_l_jav= output_jav[:,3].astype(np.float)
sigma_m_jav_raw = output_jav[:,4].astype(np.float) # extracted as the median value of the chain
sigma_m_jav = sigma_m_jav_raw
sigma_h_jav= output_jav[:,5].astype(np.float)
tau_l_jav = output_jav[:,6].astype(np.float)
tau_m_jav = output_jav[:,7].astype(np.float)
tau_h_jav = output_jav[:,8].astype(np.float)
# transform chelsea naming into javelin names
name_ch = name_ch_raw
for i in range(len(name_ch)):
name_ch[i] = name_ch_raw[i][:-6]
def name_matching():
matched_sigma_ch = np.empty(0, dtype=float)
matched_tau_ch = np.empty(0, dtype=float)
matched_name_ch = np.empty(0, dtype=str)
a = min(len(name_ch),len(name_jav)) < len(name_ch)
b = len(name_ch) == len(name_jav)
if a or b :
# case where either javelin data shorter than Chelsea, or they have same
# length
print '\n Matching Chelsea to Javelin...'
for i in range(len(name_jav)):
name = name_jav[i]
index = np.where(name_ch == name)[0][0]
matched_sigma_ch = np.append(matched_sigma_ch,log_10_sigma[index] )
matched_tau_ch = np.append(matched_tau_ch,log_10_tau[index] )
matched_name_ch = np.append(matched_name_ch, name_ch[index])
if not a : # case where chelsea data shorter than Javelin
print 'Something is wrong : Chelsea shorter than Javelin...'
return matched_sigma_ch, matched_tau_ch, matched_name_ch
def err_rows_extract(name_list, err_pos, n_errors):
'''
Since Javelin and Chelsea data is not ordered , name lists are
different for each set
Form a three-column list of indices for each error:
col0 are indices of rows with err1
col1 are indices of rows with err2
col2 are indices of rows with err3
Handle separately two cases, whether we have data with
err1,2,3 or only err1,2
'''
if n_errors == 3 :
ind=[0,0,0]
name = name_list
#upprint len(name)
for i in range(0,len(name)):
err = name[i][err_pos] # take err id value (1,2, or 3) from filename
for j in range(1,4):
if err == str(j) :
ind[j-1] = np.append(ind[j-1],i)
err1 = ind[0][1:]
err2 = ind[1][1:]
err3 = ind[2][1:]
upind = [err1,err2,err3]
if n_errors == 2:
ind=[0,0]
name = name_list
# print len(name)
for i in range(0,len(name)):
err = name[i][err_pos] # take err id value (1 or 2) from filename
for j in range(1,3):
if err == str(j) :
ind[j-1] = np.append(ind[j-1],i)
err1 = ind[0][1:]
err2 = ind[1][1:]
upind = [err1,err2]
return upind
def load_x_y(x_arr, y_arr, err_indices, ka , x_limits, y_limits):
indices = err_indices
print '\n Loading x and y ... '
x = x_arr[indices]
y = y_arr[indices]
# sieve out suspiciously bad values , based only on x and y
if ka < 0 :
xinf = np.asarray(map(isinf,x),dtype=bool)
yinf = np.asarray(map(isinf,y),dtype=bool)
ttlinf = xinf + yinf
# ttlwh = np.where(ttlinf == True) list of good indices
gi = -ttlinf # good_indices
non_inf = len(np.where(gi == True)[0])
else : #(ALWAYS)
# separate treatment of the high error test
xinf = np.asarray(map(isinf,x),dtype=bool)
yinf = np.asarray(map(isinf,y),dtype=bool)
ttlinf = xinf + yinf
# ttlwh = np.where(ttlinf == True) list of good indices
gi = -ttlinf # good_indices
ysmall = np.where(y < y_limits[0])
ylarge = np.where(y > y_limits[1])
xsmall = np.where(x < x_limits[0])
xlarge = np.where(x > x_limits[1])
gi[xsmall] = False
gi[ysmall] = False
gi[xlarge] = False
gi[ylarge] = False
non_inf = len(np.where(gi == True)[0])
percent = (float(non_inf) / float(len(x))) * 100.0
print 'Out of ', len(x),' rows, we have ', non_inf, ' of those that match', \
'the criteria of ', x_limits[0],' < x <', x_limits[1],' and ', y_limits[0],\
' < y < ',y_limits[1], 'and only those are used for plotting ... '
return x[gi], y[gi], non_inf, percent
log_sigma_ch , log_tau_ch , matched_name_ch = name_matching()
for k in range(1,n_errors+1): #n_errors+1
# looping over err1, err2 , err3, selecting appropriate rows
print '\nFor err', k
x_lim = [x_min, x_max]
y_lim = [y_min,y_max]
# We only load x and y that are within the limits of my histogram, to have the same
# pixel size for both distributions... (regardless of how much I'm removing by my boundaries)
jav_indices = err_rows_extract(name_jav,-1,n_errors)
assert len(jav_indices) == n_errors
# Check that Chelsea matched rows are exactly the ones that correspond to javelin
truth = matched_name_ch[jav_indices[0]] == name_jav[jav_indices[0]]
assert len(np.where(truth == False)[0]) == 0
# And since I have matched Chelsea to Javelin results, now the rows with corresponding
# err1,2,3, are the same
sigma = np.power(10,log_sigma_ch)
tau = np.power(10,log_tau_ch)
# Remove all values that are not within bounds specified
# since values plotted are logs of ratios, I'm feeding here
# already logs of ratios
x , y, num, percent = load_x_y(sigma ,tau,jav_indices[k-1], k, x_lim, y_lim)
print '\n Plotting coloured hist for log_tau vs log_sigma for Chelsea fitting... '
assert len(log_sigma_ch) == len(log_tau_ch) ==len(sigma_m_jav) ==len(tau_m_jav)
# Define number of bins at the beginning, especially if it is shared between the histograms...
nbins =50
plt.clf()
fig1 = plt.figure()
# Define the canvas to work on and the grid
fig1 = plt.figure(figsize=[10,8])
gs = GridSpec(100,100,bottom=0.18,left=0.18,right=0.88)
# First histogram : Chelsea results
H, xedges,yedges = np.histogram2d(x,y,bins=nbins)
H = np.rot90(H)
H = np.flipud(H)
Hmasked = np.ma.masked_where(H==0,H)
# First histogram : make axis, and plot all that is needed
ax1 = fig1.add_subplot(gs[:,:90]) #
pcObject1 = ax1.pcolormesh(xedges, yedges, Hmasked)
#plt.xlim((x_min,x_max))
#plt.ylim((y_min,y_max))
title = 'DRW '+ length +', err'+str(k)+', prior='+prior+', cross-matched, '+str(percent)[:5]+'% points'
plt.title(title)
plt.axhline(0, color = 'r', lw=2)
plt.axvline(0,color='r',lw=2)
plt.ylabel(r'$\log_{10}{ \, \left( \tau_{jav} / \tau_{ch} \right)}$',fontsize=15)
plt.xlabel(r'$\log_{10}{ \, \left( \sigma_{jav} / \sigma_{ch} \right)}$',fontsize=15)
# Add the colorbar
axC = fig1.add_subplot(gs[:,95:])
cbar = fig1.colorbar(pcObject1,ax=ax1, cax=axC, orientation='vertical')
# cbar.ax.set_ylabel('Counts')
fname2 = fig_name[0]+str(k)+fig_name[1]
plt.savefig(fname2)
print 'File saved is ', fname2