def __init__(self, z, lmu, nuInu, kx=2, ky=2, model=''): """ Initiate EBL photon density model class. Parameters ---------- z: `~numpy.ndarray` or list source redshift, m-dimensional lmu: `~numpy.ndarray` or list Wavelengths in micro m nuInu: `~numpy.ndarray` or list n x m array with EBL photon density in nW / sr / m^2 {options} kx: int order of interpolation spline along energy axis, default: 2 ky: int order of interpolation spline along energy axis, default: 2 """ self._z = np.array(z) self._loglmu = np.log10(lmu) self._nuInu = np.log10(nuInu) self.__eblSpline = RBSpline(self._loglmu, self._z, self._nuInu, kx=kx, ky=ky) self._model = model return
def __init__(self, z, EGeV, tau, kx=2, ky=2): """ Initiate Optical depth model class. Parameters ---------- z: `~numpy.ndarray` or list source redshift, m-dimensional EGeV: `~numpy.ndarray` or list Energies in GeV, n-dimensional tau: `~numpy.ndarray` or list n x m array with optical depth values {options} kx: int order of interpolation spline along energy axis, default: 2 ky: int order of interpolation spline along energy axis, default: 2 """ self._z = np.array(z) self._logEGeV = np.log10(EGeV) self._tau = np.array(tau) self.__tauSpline = RBSpline(self._logEGeV, self._z, self._tau, kx=kx, ky=ky) return
def __init__(self,pos_init,lmbd1,lmbd2,hess_lmbd2,xi2,x_min,x_max,y_min,y_max,padding_factor=0.01): self._lmbd1_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],lmbd1.T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._lmbd2_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],lmbd2.T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._hess_lmbd2_xx_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],hess_lmbd2[:,:,0,0].T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._hess_lmbd2_xy_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],hess_lmbd2[:,:,0,1].T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._hess_lmbd2_yx_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],hess_lmbd2[:,:,1,0].T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._hess_lmbd2_yy_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],hess_lmbd2[:,:,1,1].T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._xi2_x_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],xi2[:,:,0].T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) self._xi2_y_spline = RBSpline(pos_init[1,0,:],pos_init[0,:,0],xi2[:,:,1].T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1)
def z(self, z, kx=2, ky=2): self._z = z self.__eblSpline = RBSpline(self._loglnu, self._z, self._nuInu, kx=kx, ky=ky) return
def nuInu(self, nuInu, kx=2, ky=2): self._nuInu = np.log10(nuInu) self.__eblSpline = RBSpline(self._loglnu, self._z, self._nuInu, kx=kx, ky=ky) return
def loglmu(self, lmu, kx=2, ky=2): self._loglmu = np.log10(lmu) self.__eblSpline = RBSpline(self._loglnu, self._z, self._nuInu, kx=kx, ky=ky) return
def tau(self, tau, kx=2, ky=2): self._tau = tau self.__tauSpline = RBSpline(self._logEGeV, self._z, self._tau, kx=kx, ky=ky) return
def logEGeV(self, EGeV, kx=2, ky=2): self._logEGeV = np.log10(EGeV) self.__tauSpline = RBSpline(self._logEGeV, self._z, self._tau, kx=kx, ky=ky) return
def z(self, z, kx=2, ky=2): self._z = z self.__tauSpline = RBSpline(self._logEGeV, self._z, self._tau, kx=kx, ky=ky) return
def readfile(self, file_name='None',model = 'kneiske', path='/afs/desy.de/user/m/meyerm/projects/blazars/EBLmodelFiles/'): """ Read in Model file. Parameters ---------- file_name: string, full path to EBL model file, with first column with log(wavelength), first row with redshift and nu I nu values otherwise. If none, model files are used model: string, EBL model to use if file_name == None. Currently supported models are listed in Notes Section path: string, if environment variable EBL_FILE_PATH is not set, this path will be used. Returns ------- Nothing Notes ----- Supported EBL models: Name: Publication: franceschini Franceschini et al. (2008) http://www.astro.unipd.it/background/ kneiske Kneiske & Dole (2010) finke Finke et al. (2012) http://www.phy.ohiou.edu/~finke/EBL/ dominguez Dominguez et al. (2011) inuoe Inuoe et al. (2013) http://www.slac.stanford.edu/~yinoue/Download.html gilmore Gilmore et al. (2012) (fiducial model) """ self.model = model try: ebl_file_path = os.environ['EBL_FILE_PATH'] except KeyError: warnings.warn("The EBL File environment variable is not set! Using {0} as path instead.".format(path), RuntimeWarning) ebl_file_path = path if model == 'kneiske' or model == 'dominguez' or model == 'finke': if file_name == 'None': if model == 'kneiske': file_name = os.path.join(ebl_file_path , 'tau_ebl_cmb_kneiske.dat') if model == 'dominguez': file_name = os.path.join(ebl_file_path , 'tau_dominguez10.dat') if model == 'finke': file_name = os.path.join(ebl_file_path , 'tau_modelC_Finke.txt') data = np.loadtxt(file_name) self.z = data[0,1:] self.tau = data[1:,1:] if model == 'kneiske': self.logEGeV = data[1:,0] else: self.logEGeV = np.log10(data[1:,0]*1e3) elif model == 'franceschini': if file_name == 'None': file_name = os.path.join(ebl_file_path , 'tau_fran08.dat') data = np.loadtxt(file_name,usecols=(0,2)) self.logEGeV = np.log10(data[0:50,0]*1e3) self.tau = np.zeros((len(self.logEGeV),len(data[:,1])/len(self.logEGeV))) self.z = np.zeros((len(data[:,1])/len(self.logEGeV),)) for i in range(len(data[:,1])/len(self.logEGeV)): self.tau[:,i] = data[i*50:i*50+50,1] self.z[i] += 1e-3*(i+1.) elif model == 'inoue': if file_name == 'None': file_name = os.path.join(ebl_file_path , 'tau_gg_baseline.dat') data = np.loadtxt(file_name) self.z = data[0,1:] self.tau = data[1:,1:] self.logEGeV = np.log10(data[1:,0]*1e3) elif model == 'gilmore': if file_name == 'None': file_name = os.path.join(ebl_file_path , 'opdep_fiducial.dat') data = np.loadtxt(file_name) self.z = data[0,1:] self.tau = data[1:,1:] self.logEGeV = np.log10(data[1:,0]/1e3) else: raise ValueError("Unknown EBL model chosen!") # Add zeros #self.z = np.insert(self.z,0,0.) #self.tau = np.insert(self.tau,0,0.,axis=1) self.tauSpline = RBSpline(self.logEGeV,self.z,self.tau,kx=2,ky=2) return
def advect_strainlines(integrator,fixed_step_integrators,h,atol,rtol,nx=1000,ny=500,x_min=0.,x_max=2.,y_min=0.,y_max=1.,t_start=0.,t_end=20.): lmbd1_,lmbd2_,hess_lmbd2_,xi1_,xi2_ = characteristics(integrator,fixed_step_integrators,h,atol,rtol,nx,ny,x_min,x_max,y_min,y_max) grid_ = padded_grid_of_particles(nx,ny,x_min,x_max,y_min,y_max) x_ = grid_[0,::ny+4] y_ = grid_[1,:ny+4] x = x_[2:-2] y = y_[2:-2] _inner_mask = np.zeros((nx+4,ny+4),dtype=np.bool) _inner_mask[2:-2,2:-2] = True _inner_mask = _inner_mask.reshape((nx+4)*(ny+4)) grid = grid_[:,_inner_mask] lmbd1 = lmbd1_[_inner_mask] lmbd2 = lmbd2_[_inner_mask] hess_lmbd2 = hess_lmbd2_[_inner_mask,:,:] xi1 = xi1_[_inner_mask,:] xi2 = xi2_[_inner_mask,:] mask_ab = find_ab_mask(lmbd1,lmbd2,hess_lmbd2,xi2) num_horz_g0 = 4 num_vert_g0 = 4 g0 = grid[:,np.logical_and(mask_ab,find_partial_g0_mask(nx,ny,num_horz_g0,num_vert_g0))] slines = [] max_iter = 10000 stride = 0.001 l_f_max = 0.2 l_min = 1. tol_alpha = 1.e-6 padding_factor = 0.01 inAB = InABDomain(grid.reshape(2,nx,ny),lmbd1.reshape(nx,ny),lmbd2.reshape(nx,ny),hess_lmbd2.reshape(nx,ny,2,2),xi2.reshape(nx,ny,2),x_min,x_max,y_min,y_max,padding_factor) in_num_dom = InNumericalDomain(x_min,x_max,y_min,y_max,nx,ny) lmbd1_spline = RBSpline(y,x,lmbd1.reshape(nx,ny).T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) lmbd2_spline = RBSpline(y,x,lmbd2.reshape(nx,ny).T,bbox=[y_min-(y_max-y_min)*padding_factor,y_max+(y_max-y_min)*padding_factor,x_min-(x_max-x_min)*padding_factor,x_max+(x_max-x_min)*padding_factor],kx=1,ky=1) alpha = Alpha(lmbd1_spline,lmbd2_spline) rhs_f = LinearSpecialDerivative(grid.reshape(2,nx,ny),xi1.reshape(nx,ny,2)) rhs_b = LinearSpecialDerivative(grid.reshape(2,nx,ny),xi1.reshape(nx,ny,2)) nproc = 16 print(g0.shape) div = np.floor(np.linspace(0,g0.shape[1],nproc+1)).astype(int) qs = [mp.Queue() for j in range(nproc)] ps = [mp.Process(target = computestrainlines, args=(g0[:,div[j]:div[j+1]],max_iter,rhs_f,rhs_b,stride,l_f_max,l_min,alpha,tol_alpha,inAB,in_num_dom,lmbd2_spline,qs[j])) for j in range(nproc)] for p in ps: p.start() for j,q in enumerate(qs): slines += q.get() for p in ps: p.join() ensure_path_exists('precomputed_strainlines/{}'.format(integrator.__name__)) if integrator.__name__ in fixed_step_integrators: # with open('precomputed_strainlines/{}/strainlines_h={}_max_iter={}_stride={}_l_f_max={}_l_min={}_tol_alpha={}.pkl'.format(integrator.__name__,h,max_iter,stride,l_f_max,l_min,tol_alpha),'wb') as output: # pickle.dump(slines,output,pickle.HIGHEST_PROTOCOL) np.save('precomputed_strainlines/{}/strainlines_h={}_max_iter={}_stride={}_l_f_max={}_l_min={}_tol_alpha={}.npy'.format(integrator.__name__,h,max_iter,stride,l_f_max,l_min,tol_alpha),slines) else: #with open('precomputed_strainlines/{}/strainlines_atol={}_rtol={}_max_iter={}_stride={}_l_f_max={}_l_min={}_tol_alpha={}.pkl'.format(integrator.__name__,atol,rtol,max_iter,stride,l_f_max,l_min,tol_alpha),'wb') as output: # pickle.dump(slines,output,pickle.HIGHEST_PROTOCOL) np.save('precomputed_strainlines/{}/strainlines_atol={}_rtol={}_max_iter={}_stride={}_l_f_max={}_l_min={}_tol_alpha={}.npy'.format(integrator.__name__,atol,rtol,max_iter,stride,l_f_max,l_min,tol_alpha),slines)
def __init__(self, llh, taublr, EGeVllh, EGeVtau, fluxllh, rtau, kx=2, ky=2, fit_mode='mle', covar=None): """ Initialize the class Parameters ---------- llh: `~numpy.ndarray` if fit_mode = 'mle': n x m dimensional cube, dimension n: energy bins, dimension m: flux bins, each entry gives the log likelihood value for that energy bin and flux else if fit_mode = 'chi2': flux measurements taublr: `~numpy.ndarray` i x k dimensional cube with optical depths for i energies and k distances EGeVllh:`~numpy.ndarray` if fit_mode = 'mle': n dimensional array with central bin energies for llh cube else if fit_mode = 'chi2': energies corresponding to the flux measurements EGeVtau:`~numpy.ndarray` i dimensional array with central bin energies for tau cube flux_llh:`~numpy.ndarray` if fit_mode = 'mle': m dimensional array with central flux bin values for llh cube or n x m dimensional array with central flux bin values for llh cube for each energy bin else if fit_mode = 'chi2': the flux measurement errors rtau :`~numpy.ndarray` k dimensional array with central distance bin values for tau cube covar: array-like covariance matrix of flux measurements, only used if fit_mode = 'chi2' fit_mode: str either 'mle' or 'chi2'. If 'mle' a maximum likelihood estimate is performed and a least square fit otherwise. """ self._taublr = taublr self._EGeVtau = EGeVtau self._rtau = rtau self._profile_llh = None self._profile_par_names = None self._profile_scale = None self._scale_name = None self._index_name = None self._norm_name = None self._fit_mode = fit_mode self._EGeVllh = EGeVllh self._fit_mode = fit_mode if fit_mode == 'mle': self._fluxllh = fluxllh self._llh = llh self._llh_intp = [] # piece-wise interpolation of llh if len(self._fluxllh.shape) == 1: self._fluxllh[self._fluxllh == 0.] = 1e-40 * np.ones( np.sum(self._fluxllh == 0.)) for i, l in enumerate(self._llh): self._llh_intp.append( USpline(np.log(self._fluxllh), l, k=2, s=0, ext='extrapolate')) elif len(self._fluxllh.shape) == 2: for i, l in enumerate(self._llh): self._fluxllh[i][self._fluxllh[i] == 0.] = \ 1e-40 * np.ones(np.sum(self._fluxllh[i] == 0.)) self._llh_intp.append( USpline(np.log(self._fluxllh[i]), l, k=2, s=0, ext='extrapolate')) elif fit_mode == 'chi2': self._y = llh self._dy = fluxllh if covar is None: self._cov_inv = None else: self._cov_inv = np.linalg.inv(covar) else: raise ValueError( "Unknown fit mode chosen, must be either 'mle' or 'chi2'") # interpolate taublr self.__tauSpline = RBSpline(np.log(EGeVtau), np.log(rtau), self._taublr, kx=kx, ky=ky) return
def add_llhprofile(self, profile, x, y, xname='Index', yname='Prefactor', scale=1., scale_name='Scale', index_name='Index', norm_name='Prefactor', logx=False, logy=False, **kwargs): """ Add a 2d likelihood surface for model parameters Parameters ---------- profile: array-like n x m dimensional array with profile likelihood x: array-like n dimensional array, x values at which likelihood is evaluated y: array-like m dimensional array, y values at which likelihood is evaluated {options} xname: str parameter name of x values. default: "Index" (i.e. power-law index) yname: str parameter name of y values. default: "Prefactor" (i.e. power-law index) logx: bool if True, interpolate x as log10 logy: bool if True, interpolate y as log10 scale: float if one parameter is a prefactor, this is the Scale (pivot energy) that was used when likelihood was extracted. Default: 1. scale_name: str parameter name for scale, Default: "Scale" index_name: str parameter name for index, Default: "Index" norm_name: str parameter name for normalization, Default: "Prefactor" kwargs are passed to Rectilinear 2D Interpolation Notes: ------ Profile likelihood will be added to full likelihood, even if it is derived for a limited energy range. You have to make sure during the fit by contraining fit parameters that the energy range is correct """ kwargs.setdefault('kx', 2) kwargs.setdefault('ky', 2) self._scale_name = scale_name self._index_name = index_name self._norm_name = norm_name if logx: xint = np.log10(x) else: xint = x if logy: yint = np.log10(y) else: yint = y profile_int = RBSpline(xint, yint, profile, **kwargs) if logx and logy: self._profile_llh = lambda x, y: profile_int( np.log10(x), np.log10(y)) elif logx: self._profile_llh = lambda x, y: profile_int(np.log10(x), y) elif logy: self._profile_llh = lambda x, y: profile_int(x, np.log10(y)) else: self._profile_llh = lambda x, y: profile_int(x, y) self._profile_par_names = [xname, yname] self._profile_scale = scale return
def __init__( self, file_name='None', model='kneiske', path='/afs/desy.de/user/m/meyerm/projects/blazars/EBLmodelFiles/'): """ Initiate EBL model class. Parameters ---------- file_name: string, full path to EBL model file, with first column with log(wavelength), first row with redshift and nu I nu values otherwise. If none, model files are used model: string, EBL model to use if file_name == None. Currently supported models are listed in Notes Section path: string, if environment variable EBL_FILE_PATH is not set, this path will be used. Returns ------- Nothing Notes ----- Supported EBL models: Name: Publication: franceschini Franceschini et al. (2008) http://www.astro.unipd.it/background/ kneiske Kneiske & Dole (2010) dominguez Dominguez et al. (2011) inuoe Inuoe et al. (2013) http://www.slac.stanford.edu/~yinoue/Download.html gilmore Gilmore et al. (2012) (fiducial model) finke Finke et al. (2012) http://www.phy.ohiou.edu/~finke/EBL/ """ self.z = np.array([]) #redshift self.logl = np.array([]) #log (wavelength / micron) self.nuInu = np.array([]) #log (nu I_nu / [nW / Hz / sr]) self.model = model #model if file_name == 'None': try: ebl_file_path = os.environ['EBL_FILE_PATH'] except KeyError: logging.warning( "The EBL File environment variable is not set! Using {0} as path instead." .format(path)) ebl_file_path = path if model == 'kneiske': file_name = join(ebl_file_path, 'ebl_nuFnu_tanja.dat') elif model == 'franceschini': file_name = join(ebl_file_path, 'ebl_franceschini.dat') elif model == 'dominguez': file_name = join(ebl_file_path, 'ebl_dominguez.dat') elif model == 'inoue': file_name = join(ebl_file_path, 'EBL_z_0_baseline.dat') logging.warning("Inoue model is only provided for z = 0!") elif model == 'gilmore': file_name = join(ebl_file_path, 'eblflux_fiducial.dat') elif model == 'cuba': file_name = join(ebl_file_path, 'CUBA_UVB.dat') elif model == 'finke': file_name = join(ebl_file_path, 'ebl_modelC_Finke.txt') else: raise ValueError("Unknown EBL model chosen!") data = np.loadtxt(file_name) if model == 'inoue': self.z = np.array([0.]) self.logl = np.log10(data[:, 0]) self.nuInu = np.log10(data[:, 1]) self.eblSpline = interp1d(self.logl, self.nuInu) return elif model == 'gilmore': self.z = data[0, 1:] self.logl = np.log10( data[1:, 0] * 1e-4) # convert from Angstrom to micro meter self.nuInu = data[1:, 1:] self.nuInu[self.nuInu == 0.] = 1e-20 * np.ones( np.sum(self.nuInu == 0.)) self.nuInu = ( self.nuInu.T * data[1:, 0] ).T * 1e4 * 1e-7 * 1e9 # convert from ergs/s/cm^2/Ang/sr to nW/m^2/sr self.nuInu = np.log10(self.nuInu) elif model == 'cuba': self.z = data[0, 1:-1] self.logl = np.log10(data[1:, 0] * 1e-4) self.nuInu = data[1:, 1:-1] # replace zeros by 1e-40 idx = np.where(data[1:, 1:-1] == 0.) self.nuInu[idx] = np.ones(np.sum(self.nuInu == 0.)) * 1e-20 self.nuInu = np.log10( self.nuInu.transpose() * SI_c / (10.**self.logl * 1e-6)).transpose() # in erg / cm^2 / s / sr self.nuInu += 6 # in nW / m^2 / sr # check where logl is not strictly increasing idx = np.where(np.diff(self.logl) == 0.) for i in idx[0]: self.logl[i + 1] = (self.logl[i + 2] + self.logl[i]) / 2. else: self.z = data[0, 1:] self.logl = np.log10(data[1:, 0]) self.nuInu = np.log10(data[1:, 1:]) if model == 'finke': self.logl = self.logl[::-1] - 4. self.nuInu = self.nuInu[::-1] else: data = np.loadtxt(file_name) self.z = data[0, 1:] self.logl = np.log10(data[1:, 0]) self.nuInu = np.log10(data[1:, 1:]) self.eblSpline = RBSpline( self.logl, self.z, self.nuInu, kx=2, ky=2) # reutrns log10(nuInu) for log10(lambda) self.steps_mu = 50 # steps for integration self.steps_e = 50 # steps for integration self.steps_z = 50 # steps for integration return