Blame view

modules/observables.py 10.7 KB
d943e502   Thomas Fitoussi   Update 'analysis'...
1
import matplotlib.cm as colormap
d88319ae   Thomas Fitoussi   Update analysis 2...
2
3
4
5
from matplotlib.pyplot     import figure, show, legend, gca, setp
from matplotlib.gridspec   import GridSpec
from numpy                 import sort, log10, histogram, pi, arange, where, ma, zeros, append
from numpy                 import rot90, flipud, histogram2d, logspace, linspace, size, loadtxt
188a8c6a   Thomas Fitoussi   Update analysis 2...
6
7
8
9
from mpl_toolkits.mplot3d  import Axes3D
from modules.read          import ReadResults
from modules.analytic      import Analytic_delay_vs_theta, Analytic_observables_vs_energy
from modules.constants     import degre, yr
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
10
11

#=========================== CORELATION BETWEEN OBSERVABLES ===========================#
d88319ae   Thomas Fitoussi   Update analysis 2...
12
13
14
15
16
17
18
19
20
21
22
def observables_vs_energy(energy,theta,delay,weight,Erange=[1e-3,1e5],nbBins=-1):#28):
   # nbBins=16, Erange=[1e-1,1e3] <=> Arlen 2014 (energy binning)
   if (nbBins==-1):
      bins=append(logspace(log10(Erange[0]),log10(0.9),100),logspace(0,log10(Erange[1]),16))
      nbBins=size(bins)-1
   if (nbBins==-2): # <=> Arlen 2014 (energy binning)
      nbBins=16 
      Erange=[1e-1,1e3]
      bins=logspace(log10(Erange[0]),log10(Erange[1]),nbBins+1)
   else:
      bins=logspace(log10(Erange[0]),log10(Erange[1]),nbBins+1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
23
   ener = (bins[1:nbBins+1]*bins[0:nbBins])**0.5
d88319ae   Thomas Fitoussi   Update analysis 2...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   #dtheta, dE = histogram(energy,bins,weights=weight*theta)
   #dt,     dE = histogram(energy,bins,weights=weight*delay)
   #dN,     dE = histogram(energy,bins,weights=weight)
   dt = zeros(nbBins,dtype="float64")
   dtheta = zeros(nbBins,dtype="float64")
   for i in arange(nbBins):
      mask = (energy >= bins[i]) & (energy < bins[i+1])
      w = sum(weight[mask])
      t = sum(weight[mask]*delay[mask])
      angle = sum(weight[mask]*theta[mask])
      
      if w !=0:
         dt[i]=t/w
         dtheta[i]=angle/w
   return ener, dtheta, dt
188a8c6a   Thomas Fitoussi   Update analysis 2...
39

d88319ae   Thomas Fitoussi   Update analysis 2...
40
def observables_vs_delay(energy,theta,delay,weight,time_range=[10**-0.5,10**8.5],nbBins=100):#9): 
188a8c6a   Thomas Fitoussi   Update analysis 2...
41
42
43
   # <=> Taylor 2011 (time binning)
   bins=logspace(log10(time_range[0]),log10(time_range[1]),nbBins+1)
   time = (bins[1:nbBins+1]*bins[0:nbBins])**0.5
d88319ae   Thomas Fitoussi   Update analysis 2...
44
45
46
47
48
49
50
51
52
53
54
55
56
   dE = zeros(nbBins,dtype="float64")
   dtheta = zeros(nbBins,dtype="float64")
   for i in arange(nbBins):
      mask = (delay >= bins[i]) & (delay < bins[i+1])
      w = sum(weight[mask])
      E = sum(weight[mask]*energy[mask])
      angle = sum(weight[mask]*theta[mask])
      if w !=0:
         dE[i]=E/w
         dtheta[i]=angle/w
   return time, dtheta, dE

def drawObservables(fileId,psf=180,Nb=-1,plot_generation_density=False,plot_others_codes=False):
188a8c6a   Thomas Fitoussi   Update analysis 2...
57
   #ax = figure(figsize=(12,9)).add_subplot(111,projection='3d')
d88319ae   Thomas Fitoussi   Update analysis 2...
58
59
60
61
62
63
   fig = figure(figsize=(20,18))
   gs = GridSpec(2, 2, height_ratios=[1,1], width_ratios=[1,1]) 
   fig.subplots_adjust(hspace=0,wspace=0)
   ax0 = fig.add_subplot(gs[0])
   ax2 = fig.add_subplot(gs[2],sharex=ax0)
   ax3 = fig.add_subplot(gs[3],sharey=ax2)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
64
65
66

   fileId = "Simulations/"+fileId

30f3bf5d   Thomas Fitoussi   Adapt reading of ...
67
   i=0
d88319ae   Thomas Fitoussi   Update analysis 2...
68
   nbBins = 250
188a8c6a   Thomas Fitoussi   Update analysis 2...
69
   generation, weight, energy, delay, pos, theta = ReadResults(fileId,cols=[0,1,2,3,4,8])
d88319ae   Thomas Fitoussi   Update analysis 2...
70
   cond = (pos*degre < psf) & (theta>0) & (delay>0) & (generation%2 == 0)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
71
72
   generation = generation[cond]
   energy = energy[cond]
d943e502   Thomas Fitoussi   Update 'analysis'...
73
   delay = delay[cond]/yr
d88319ae   Thomas Fitoussi   Update analysis 2...
74
75
   theta = theta[cond]*degre
   weight = weight[cond]
188a8c6a   Thomas Fitoussi   Update analysis 2...
76

d88319ae   Thomas Fitoussi   Update analysis 2...
77
78
79
80
81
82
   print "theta range:  [",min(theta),", ",max(theta),"] (degre)", energy[theta==max(theta)]," GeV", generation[theta==max(theta)]
   print "delay range:  [",min(delay),", ",max(delay),"] (yr)"
   print "energy range: [",min(energy),", ",max(energy),"] (GeV)"

   theta_range = [1e-5,200]
   delay_range = [1e-4,5e9]
188a8c6a   Thomas Fitoussi   Update analysis 2...
83
   energy_range = [1e-3,1e4]
d88319ae   Thomas Fitoussi   Update analysis 2...
84
85
86
   colors=['b','g']

   if plot_generation_density: 
188a8c6a   Thomas Fitoussi   Update analysis 2...
87
      cmaps=[colormap.Blues,colormap.Greens,colormap.Reds]
d88319ae   Thomas Fitoussi   Update analysis 2...
88
      for gen in [2,4]:#sort(list(set(generation))):
188a8c6a   Thomas Fitoussi   Update analysis 2...
89
90
91
         cond = (generation==gen)
         H, xedges, yedges = histogram2d(log10(energy[cond]),log10(delay[cond]),bins=nbBins,weights=weight[cond])
         H = flipud(rot90(ma.masked_where(H==0,H)))
d88319ae   Thomas Fitoussi   Update analysis 2...
92
93
         im1 = ax0.pcolormesh(10**xedges,10**yedges,log10(H),cmap=cmaps[i])   

188a8c6a   Thomas Fitoussi   Update analysis 2...
94
95
         H, xedges, yedges = histogram2d(log10(energy[cond]),log10(theta[cond]),bins=nbBins,weights=weight[cond])
         H = flipud(rot90(ma.masked_where(H==0,H)))
d88319ae   Thomas Fitoussi   Update analysis 2...
96
97
         im2 = ax2.pcolormesh(10**xedges,10**yedges,log10(H),cmap=cmaps[i])      

188a8c6a   Thomas Fitoussi   Update analysis 2...
98
99
100
         H, xedges, yedges = histogram2d(log10(delay[cond]),log10(theta[cond]),bins=nbBins,weights=weight[cond])
         H = flipud(rot90(ma.masked_where(H==0,H)))
         im3 = ax3.pcolormesh(10**xedges,10**yedges,log10(H),cmap=cmaps[i])
188a8c6a   Thomas Fitoussi   Update analysis 2...
101

d88319ae   Thomas Fitoussi   Update analysis 2...
102
103
104
105
106
         ener,angle,dt = observables_vs_energy(energy[cond],theta[cond],delay[cond],weight[cond],nbBins=Nb)
         ax0.plot(ener,dt,color=colors[i],linewidth=2,label="gen = %.0f"%(i+1))
         ax2.plot(ener,angle,color=colors[i],linewidth=2,label="gen = %.0f"%(i+1))
         dt,angle,ener = observables_vs_delay(energy[cond],theta[cond],delay[cond],weight[cond],nbBins=Nb)
         ax3.plot(dt,angle,color=colors[i],linewidth=2,label="gen = %.0f"%(i+1))
188a8c6a   Thomas Fitoussi   Update analysis 2...
107
         i+=1
d88319ae   Thomas Fitoussi   Update analysis 2...
108
109
110
111
112
113
114
115
116
117
118
119
120
            
   else:
      H, xedges, yedges = histogram2d(log10(energy),log10(delay),bins=nbBins,weights=weight)
      H = flipud(rot90(ma.masked_where(H==0,H)))
      im1 = ax0.pcolormesh(10**xedges,10**yedges,log10(H),cmap=colormap.YlOrBr)   

      H, xedges, yedges = histogram2d(log10(energy),log10(theta),bins=nbBins,weights=weight)
      H = flipud(rot90(ma.masked_where(H==0,H)))
      im2 = ax2.pcolormesh(10**xedges,10**yedges,log10(H),cmap=colormap.YlOrBr)      
    
      H, xedges, yedges = histogram2d(log10(delay),log10(theta),bins=nbBins,weights=weight)
      H = flipud(rot90(ma.masked_where(H==0,H)))
      im3 = ax3.pcolormesh(10**xedges,10**yedges,log10(H),cmap=colormap.YlOrBr)
188a8c6a   Thomas Fitoussi   Update analysis 2...
121

d88319ae   Thomas Fitoussi   Update analysis 2...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
      for gen in [2,4]:#sort(list(set(generation))):
         cond = (generation==gen)
         ener,angle,dt = observables_vs_energy(energy[cond],theta[cond],delay[cond],weight[cond],nbBins=Nb)
         ax0.plot(ener,dt,color=colors[i],linewidth=2,label="gen = %.0f"%(i+1))
         ax2.plot(ener,angle,color=colors[i],linewidth=2,label="gen = %.0f"%(i+1))
         dt,angle,ener = observables_vs_delay(energy[cond],theta[cond],delay[cond],weight[cond],nbBins=Nb)
         ax3.plot(dt,angle,color=colors[i],linewidth=2,label="gen = %.0f"%(i+1))
         i+=1       
            
   ener,angle,dt = observables_vs_energy(energy,theta,delay,weight,nbBins=Nb)
   ax0.plot(ener,dt,color='r',linewidth=2,label="all gen")
   ax2.plot(ener,angle,color='r',linewidth=2,label="all gen")
   dt,angle,ener = observables_vs_delay(energy,theta,delay,weight,nbBins=Nb)
   ax3.plot(dt,angle,color='r',linewidth=2,label="all gen")
188a8c6a   Thomas Fitoussi   Update analysis 2...
136
137
138
139
140
141

   nE = 5000 
   Emin = 1.e-3
   Emax = 1e4
   E = Emin*(Emax/Emin)**(arange(nE)/(nE-1.))
   theta_fit, delay_fit = Analytic_observables_vs_energy(E,"../"+fileId)
d88319ae   Thomas Fitoussi   Update analysis 2...
142
   ax0.plot(E,delay_fit/yr,'--k',linewidth=2)
188a8c6a   Thomas Fitoussi   Update analysis 2...
143
144
145
146
147
148
149
150
151
   ax2.plot(E,theta_fit,'--k',linewidth=2)
   ax3.scatter(delay_fit/yr,theta_fit,color='k',marker="+")
    
   if plot_others_codes:
      # Results from Arlen 2014 - fig. 2a, 2b
      # =====================================
      #   - binning in energy : log, 16 bins between 1e-1 et 1e3 GeV
      #   - PSF = 10 deg
      data = loadtxt(fileId+'/Arlen2014-fig2a.csv', delimiter=',')
d88319ae   Thomas Fitoussi   Update analysis 2...
152
      ax0.plot(data[:,0],data[:,1],color="m",linestyle='--',linewidth=2,label="Arlen 2014 - no EBL")
188a8c6a   Thomas Fitoussi   Update analysis 2...
153
      data = loadtxt(fileId+'/Arlen2014-fig2b.csv', delimiter=',')
d88319ae   Thomas Fitoussi   Update analysis 2...
154
      ax0.plot(data[:,0],data[:,1],color="m",linestyle='-.',linewidth=2,label="Arlen 2014 - EBL")
188a8c6a   Thomas Fitoussi   Update analysis 2...
155
156
157
158

      # Results from Taylor 2011 - fig. 2
      # =================================
      data = loadtxt(fileId+'/Taylor2011-fig2.csv', delimiter=',')
d88319ae   Thomas Fitoussi   Update analysis 2...
159
      ax0.plot(data[:,0]*1e-9,data[:,1],color="m",linestyle=':',linewidth=2,label="Taylor 2011")  
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
160
161
162
163
164
165

   #ax.legend(loc="best")
   #ax.set_xlabel("energy [GeV]")
   #ax.set_ylabel("$\\theta$ [deg]")
   #ax.set_zlabel("Time delay [s]")

d88319ae   Thomas Fitoussi   Update analysis 2...
166
167
168
169
170
171
172
173
174
175
   #ax3.set_title(fileId+" - selection %0.1f"%psf)
   ax0.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

   ax0.grid(b=True,which='major')
   ax0.set_xscale('log')
   ax0.set_yscale('log')
   ax0.set_xlim(energy_range)
   ax0.set_ylim(delay_range)
   ax0.set_ylabel("Time delay [yrs]")
   setp(ax0.get_xticklabels(), visible=False)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
176
177

   ax2.grid(b=True,which='major')
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
178
179
   ax2.set_xscale('log')
   ax2.set_yscale('log')
188a8c6a   Thomas Fitoussi   Update analysis 2...
180
181
182
183
   ax2.set_xlim(energy_range)
   ax2.set_ylim(theta_range)
   ax2.set_xlabel("energy [GeV]")
   ax2.set_ylabel("$\\theta$ [deg]")
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
184
185

   ax3.grid(b=True,which='major')
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
186
187
   ax3.set_xscale('log')
   ax3.set_yscale('log')
188a8c6a   Thomas Fitoussi   Update analysis 2...
188
189
190
   ax3.set_xlim(delay_range)
   ax3.set_ylim(theta_range)
   ax3.set_xlabel("Time delay [yrs]")
d88319ae   Thomas Fitoussi   Update analysis 2...
191
   setp(ax3.get_yticklabels(), visible=False)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
192
193

   show()
188a8c6a   Thomas Fitoussi   Update analysis 2...
194
195
196
197
198
199
200
201

def drawDelays_vs_energy(files,psf=180,plot_others_codes=False):
   ax1 = figure(figsize=(12,9)).add_subplot(111)
   nbBins = 100

   for fileId0 in files:
      fileId = "Simulations/simple case/"+fileId0
      gen, weight, energy, delay, pos, arrival_angle = ReadResults(fileId,cols=[0,1,2,3,4,8])
d88319ae   Thomas Fitoussi   Update analysis 2...
202
203
204
205
206
207
208
209
      #dt,dtheta,ener = observables_vs_delay(energy,arrival_angle*degre,delay/yr,weight)
      #ax1.plot(ener,dt,marker='*',linestyle=":",linewidth=2) 
      cond = (arrival_angle*degre < psf) & (gen%2==0) #PSF_Taylor2011(energy) 
      energy = energy[cond]
      arrival_angle = arrival_angle[cond]
      delay = delay[cond]
      weight = weight[cond]
      ener,dtheta,dt = observables_vs_energy(energy,arrival_angle*degre,delay/yr,weight,nbBins=-2)
188a8c6a   Thomas Fitoussi   Update analysis 2...
210
211
212
213
214
215
      p0, =ax1.plot(ener,dt,marker='*',linewidth=2,label=fileId0) 
      
      nth = 5000
      thmin = 0.1
      thmax = 1e4
      E = thmin*(thmax/thmin)**(arange(nth)/(nth-1.))
d88319ae   Thomas Fitoussi   Update analysis 2...
216
      #ax1.plot(E,Analytic_observables_vs_energy(E,fileId)[1]/yr,color=p0.get_color(),linestyle="-")
188a8c6a   Thomas Fitoussi   Update analysis 2...
217
                                                     
d88319ae   Thomas Fitoussi   Update analysis 2...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
   if plot_others_codes:
      # Results from Arlen 2014 - fig. 2a, 2b
      # =====================================
      #   - binning in energy : log, 16 bins between 1e-1 et 1e3 GeV
      #   - PSF = 10 deg
      data = loadtxt(fileId+'/Arlen2014-fig2a.csv', delimiter=',')
      p1,= ax1.plot(data[:,0],data[:,1],color="r",linestyle='--',linewidth=2,label="Arlen 2014 - fig 2a")
      data = loadtxt(fileId+'/Arlen2014-fig2b.csv', delimiter=',')
      p2,= ax1.plot(data[:,0],data[:,1],color="r",linestyle='-.',linewidth=2,label="Arlen 2014 - fig 2b")
   
      # Results from Taylor 2011 - fig. 2
      # =================================
      #data = loadtxt(fileId+'/Taylor2011-fig2.csv', delimiter=',')
      #p3,= ax1.plot(data[:,0]*1e-9,data[:,1],color=p0.get_color(),linestyle=':',linewidth=2)
   
      #leg1 = legend([p0,p1,p2], ["our code","Arlen 2014 - fig 2a",
      #    "Arlen 2014 - fig 2b"], loc=1)
                                                              
188a8c6a   Thomas Fitoussi   Update analysis 2...
236
237
238
   ax1.grid(b=True,which='major')  
   ax1.set_xscale('log')
   ax1.set_yscale('log')
d88319ae   Thomas Fitoussi   Update analysis 2...
239
240
   #ax1.set_ylim([1,1e8])
   #ax1.set_xlim([1e-1,1e3])
188a8c6a   Thomas Fitoussi   Update analysis 2...
241
242
   ax1.set_xlabel("energy [GeV]")
   ax1.set_ylabel("Time delay [yrs]") 
d88319ae   Thomas Fitoussi   Update analysis 2...
243
244
   ax1.legend(loc="best")#2)
   #ax1 = gca().add_artist(leg1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
245
246

   show()