Exemple #1
0
def cosmo_part(cosmo, zs):
    x0 = cosmo.pars()
    J = np.zeros((len(zs), len(x0)))
    eps = np.sqrt(np.sqrt(np.finfo(float).eps))
    f0 = cosmo.mu(zs)  #- 10
    for i in range(len(x0)):
        h1 = eps * np.abs(x0[i])
        if h1 == 0:
            h1 = eps
        pars = [p for p in x0]
        pars[i] += h1
        indexes = np.zeros(len(pars)).astype(bool)
        indexes[i] = True
        cosmo.update_pars(pars[i], indexes)
        J[:, i] = (cosmo.mu(zs) - f0) / h1
    return J
 def construct_jacobian(self):
     print 'Constructing the Jacobian ...'
     d, p, cosmo, base = self.data, self.params, self.model_cosmo, self.spline_base
     zs = d['z'].reshape((len(d), 1))
     J = np.zeros((len(d), len(p.free)))
     x0 = cosmo.pars()
     eps = np.sqrt(np.sqrt(np.finfo(float).eps))
     #eps = np.finfo(float).eps
     f0 = cosmo.mu(zs)
     c = d['c']
     for par in self.free_cosmo_params:
         h1 = 1e-4
         p2 = self.params.copy()
         p2[par] = self.params[par].free + h1
         J[:,
           p[par].indexof()] = (self.new_cosmo_model(p2).mu(zs) - f0) / h1
     if 'theta_salt' not in fixed_pars:
         J[:, p['theta_salt'].indexof()] = base.eval(np.array(
             d['l_eff'])).toarray()
     for j in np.unique(d['#SN']):
         idx = d['#SN'] == j
         snd = d[idx]
         taille = len(snd)
         if 'c' not in fixed_pars:
             J[idx, p['c'].indexof(j)] = beta + np.polyval(
                 color_law_params, transfo(snd['l_eff']))
         if 'mB' not in fixed_pars:
             J[idx, p['mB'].indexof(j)] = np.ones(len(snd))
     for band in 'grizy':
         idxband = d['band'] == band
         if 'zp' + band not in fixed_pars:
             J[idxband, p['zp' + band].indexof()] = 1.
         if 'dl' + band not in fixed_pars:
             J[idxband, p['dl' + band].indexof()] = (
                 1. / (1 + d['z'][idxband]) *
                 (base.deriv(np.array(d['l_eff'][idxband])) *
                  params['theta_salt'].free + 1. /
                  (lv - lb) * np.polyval(np.polyder(color_law_params),
                                         transfo(d['l_eff'][idxband])))).T
     if 'beta' not in fixed_pars:
         J[:, p['beta'].indexof()] = d['c'].reshape((len(d), 1))
     kl = self.color_law_degree
     if 'color_law' not in fixed_pars:
         J[:, p['color_law'].indexof()] = np.array(
             [c * transfo(d['l_eff'])**(kl - k) for k in range(kl + 1)]).T
     self.J = J
     self.Jr = J
     Cm = (sparse.coo_matrix(np.diag(1. / d['snr']**2))).toarray()
     self.C = Cm
     self.Cr = Cm
     print '... DONE !'
     return J, Cm
Exemple #3
0
 def construct_jacobian(self):
     pedestal = self.pedestal
     print 'Constructing the Jacobian ...'
     d, p, cosmo, base = self.data, self.params, self.model_cosmo, self.spline_base
     zs = d['z'].reshape((len(d), 1))
     x0 = cosmo.pars()
     eps = np.sqrt(np.sqrt(np.finfo(float).eps))
     #eps = np.finfo(float).eps
     f0 = cosmo.mu(zs)
     c = d['c']
     all_lines = np.arange(len(d))
     for par in self.free_cosmo_params:
         h1 = 1e-4
         p2 = self.params.copy()
         p2[par] = self.params[par].free + h1
         self.update_lines(all_lines, p[par].indexof(), ((self.new_cosmo_model(p2).mu(zs) - f0)/h1).flatten())
     if 'theta_salt' not in self.fixed_pars:
         bev = base.eval(np.array(d['l_eff']))
         self.lines = np.hstack((self.lines, bev.row)); self.cols = np.hstack((self.cols, bev.col+ p['theta_salt'].indexof(0)))
         self.J_dat = np.hstack((self.J_dat, bev.data))
         #self.update_lines(bev.row, bev.col + p['theta_salt'].indexof(0), bev.data)
     if 'c' not in self.fixed_pars: 
         self.update_lines_optimized(np.arange(len(d)), p['c'].indexof(d['#SN']), beta + np.polyval(color_law_params, transfo(d['l_eff'])))
     if 'mB' not in self.fixed_pars:
         self.update_lines_optimized(np.arange(len(d)), p['mB'].indexof(d['#SN']), np.ones(len(d)))
     for band in self.bands:
         idxband = d['band']==band
         if 'zp'+band not in self.fixed_pars:
             self.update_lines(all_lines[idxband], p['zp'+band].indexof(), 1.)
         if 'dl'+band not in self.fixed_pars:
             #if 'theta_salt' not in self.fixed_pars:
             self.update_lines(all_lines[idxband], p['dl'+band].indexof(), 
                               (1./(1+d['z'][idxband])*(base.deriv(np.array(d['l_eff'][idxband])) * self.params['theta_salt'].full + d['c'][idxband]*1./(lv-lb)*np.polyval(np.polyder(color_law_params), transfo(d['l_eff'][idxband])))).flatten())
     if 'beta' not in self.fixed_pars:
         self.update_lines(all_lines, p['beta'].indexof(), c)
     kl = self.color_law_degree
     if 'color_law' not in self.fixed_pars:
         self.update_lines(all_lines, p['color_law'].indexof(), np.array([c*transfo(d['l_eff'])**(kl-k) for k in range(kl+1)]).flatten())
     self.Clines = all_lines
     self.Ccols = all_lines
     self.C_dat = 1./d['snr']**2 + pedestal**2
     print '... DONE !'
     self.W = sparse.coo_matrix((1./(1./np.array(d['snr']**2)+pedestal**2), (all_lines, all_lines)))
 def construct_jacobian(self):
     print 'Constructing the Jacobian ...'
     d, p, cosmo, base = self.data, self.params, self.model_cosmo, self.spline_base
     zs = d['z'].reshape((len(d), 1))
     x0 = cosmo.pars()
     eps = np.sqrt(np.sqrt(np.finfo(float).eps))
     #eps = np.finfo(float).eps
     f0 = cosmo.mu(zs)
     c = d['c']
     all_lines = np.arange(len(d))
     for par in self.free_cosmo_params:
         h1 = 1e-4
         p2 = self.params.copy()
         p2[par] = self.params[par].free + h1
         self.update_lines(all_lines, p[par].indexof(),
                           ((self.new_cosmo_model(p2).mu(zs) - f0) /
                            h1).flatten())
     if 'theta_salt' not in fixed_pars:
         bev = base.eval(np.array(d['l_eff']))
         self.lines = np.hstack((self.lines, bev.row))
         self.cols = np.hstack(
             (self.cols, bev.col + p['theta_salt'].indexof(0)))
         self.J_dat = np.hstack((self.J_dat, bev.data))
         #self.update_lines(bev.row, bev.col + p['theta_salt'].indexof(0), bev.data)
     for j in np.unique(d['#SN']):
         idx = d['#SN'] == j
         snd = d[idx]
         taille = len(snd)
         if 'c' not in fixed_pars:
             self.update_lines(
                 all_lines[idx], p['c'].indexof(j),
                 beta + np.polyval(color_law_params, transfo(snd['l_eff'])))
         if 'mB' not in fixed_pars:
             self.update_lines(all_lines[idx], p['mB'].indexof(j),
                               np.ones(len(snd)))
     for band1 in np.unique(d['band']):
         idxband = d['band'] == band1
         ###
         ### ATTENTION CA CHANGE ICI EN FONCTION DE L UNITE DE ZP ET DL
         ###
         band = band1.replace('::', '_')
         if 'ZP_' + band not in fixed_pars:
             self.update_lines(all_lines[idxband],
                               p['ZP_' + band].indexof(), 1.)
         if 'DL_' + band not in fixed_pars:
             derder = base.deriv(np.array(
                 d['l_eff'][idxband])) * params['theta_salt'].full
             if np.isnan(derder).sum() != 0:
                 self.jambon += list(d['band'][idxband][np.isnan(derder)])
             self.update_lines(
                 all_lines[idxband], p['DL_' + band].indexof(),
                 (1. / (1 + d['z'][idxband]) *
                  (derder + d['c'][idxband] * 1. /
                   (lv - lb) * np.polyval(np.polyder(color_law_params),
                                          transfo(d['l_eff'][idxband])))
                  ).flatten())
     if 'beta' not in fixed_pars:
         self.update_lines(all_lines, p['beta'].indexof(), c)
     kl = self.color_law_degree
     if 'color_law' not in fixed_pars:
         self.update_lines(
             all_lines, p['color_law'].indexof(),
             np.array([
                 c * transfo(d['l_eff'])**(kl - k) for k in range(kl + 1)
             ]).flatten())
     self.Clines = np.arange(len(d))
     self.Ccols = np.arange(len(d))
     self.C_dat = 1. / d['snr']**2 + color_disp_func(d['l_eff'])**2
     print '... DONE !'
     self.W = sparse.coo_matrix(
         ((1. /
           (1. / np.array(d['snr']**2) + color_disp_func(d['l_eff'])**2)),
          (all_lines, all_lines)))