Exemple #1
0
Fichier : model.py Projet : nhmc/H2
def ymodel(wa, pars):
    """Generate the model normalised flux array for every region.

    Note this needs the global variables `lines_vary` and `regions` to
    be defined.
    """
    linepars = pars
    dz = 0
    if options['wa_vary']:
        linepars, regpars = pars[:-len(regions)], pars[-len(regions):]
    lines = copy_par_to_lines(linepars, lines_vary)
    fl = []
    for ireg, (wa, tau0, sp, (i, j),tname) in enumerate(regions):
        tau = tau0.copy()
        for l in lines:
            if options['wa_vary']:
                dz = regpars[ireg] / wa[len(wa)//2] * (l.z + 1)

            trans = atom[l.name.strip()]
            tau += calc_iontau(wa, trans, l.z+1 + dz, l.logN, l.b)

        dwpix = wa[1] - wa[0]
        flmodel0 = convolve_with_COS_FOS(np.exp(-tau), wa, dwpix)
        flmodel = np.interp(sp.wa[i:j], wa, flmodel0)
        fl.extend(flmodel)
        
    return fl
Exemple #2
0
    def update_model(self):
        lines = lines_from_f26(self.opt.f26)

        wa = self.wa
        if self.opt.wadiv is not None:
            wa = self.wa1

        # first remove models that no longer present in lines
        for l in set(self.taus).difference(lines):
            del self.taus[l]
            del self.models[l]
            del self.ticks[l]
            for tr_id in self.artists['models']:
                self.artists['models'][tr_id][l].remove()
                del self.artists['models'][tr_id][l]

        dtype = np.dtype([('name', 'S10'), ('wa', 'f8'), ('z', 'f8'),
                          ('wa0', 'f8'), ('ind', 'i4')])
        # now add the new models from lines
        new_lines = set(lines).difference(self.taus)
        print len(new_lines), 'new lines'
        for l in new_lines:
            #print 'z, logN, b', z, logN, b
            ion, z, b, logN = l
            if ion in ('__', '<>', '<<', '>>'):
                continue
            maxdv = 20000 if logN > 18 else 500
            t, tick = calc_iontau(wa,
                                  self.opt.atom[ion],
                                  z + 1,
                                  logN,
                                  b,
                                  ticks=True,
                                  maxdv=maxdv,
                                  logNthresh_LL=self.opt.logNthresh_LL)
            self.taus[l] = t
            self.models[l] = np.exp(-t)
            temp = np.empty(0, dtype=dtype)
            if tick:
                ions = [ion] * len(tick)
                temp = np.rec.fromarrays([ions] + zip(*tick), dtype=dtype)
            self.ticks[l] = temp

        self.allticks = []
        if len(self.ticks) > 0:
            self.allticks = np.concatenate([self.ticks[l] for l in self.ticks
                                            ]).view(np.recarray)
            self.allticks.sort(order='wa')
        tau = np.zeros_like(wa)
        for line in self.taus:
            tau += self.taus[line]

        self.tau = tau
        self.model = np.exp(-tau)
Exemple #3
0
Fichier : model.py Projet : nhmc/H2
def find_tau(wa, lines):
    """ generate a tau array given a list of transitions and a
    wavelength array.

    The transition list is in a vp.lines format
    """ 
    tau = np.zeros_like(wa)
    for l in lines:
        trans = atom[l.name.strip()]
        tau += calc_iontau(wa, trans, l.z + 1, l.logN, l.b, maxdv=None) 

    return tau
Exemple #4
0
    def update_model(self):
        lines = lines_from_f26(self.opt.f26)

        wa = self.wa
        if self.opt.wadiv is not None:
            wa = self.wa1

        # first remove models that no longer present in lines
        for l in set(self.taus).difference(lines):
            del self.taus[l]
            del self.models[l]
            del self.ticks[l]
            for tr_id in self.artists['models']:
                self.artists['models'][tr_id][l].remove()
                del self.artists['models'][tr_id][l]

        dtype = np.dtype([('name', 'S10'), ('wa', 'f8'), ('z', 'f8'),
                          ('wa0', 'f8'), ('ind', 'i4')])
        # now add the new models from lines
        new_lines = set(lines).difference(self.taus)
        print len(new_lines), 'new lines'
        for l in new_lines:
            #print 'z, logN, b', z, logN, b
            ion, z, b, logN = l
            if ion in ('__', '<>', '<<', '>>'):
                continue
            maxdv = 20000 if logN > 18 else 500
            t, tick = calc_iontau(wa, self.opt.atom[ion], z + 1, logN, b,
                                  ticks=True, maxdv=maxdv,
                                  logNthresh_LL=self.opt.logNthresh_LL)
            self.taus[l] = t
            self.models[l] = np.exp(-t)
            temp = np.empty(0, dtype=dtype)
            if tick:
                ions = [ion] * len(tick)
                temp = np.rec.fromarrays([ions] + zip(*tick), dtype=dtype)
            self.ticks[l] = temp


        self.allticks = []
        if len(self.ticks) > 0:
            self.allticks =  np.concatenate(
                [self.ticks[l] for l in self.ticks]).view(np.recarray)
            self.allticks.sort(order='wa')
        tau = np.zeros_like(wa)
        for line in self.taus:
            tau += self.taus[line]

        self.tau = tau
        self.model = np.exp(-tau)
Exemple #5
0
Fichier : model.py Projet : nhmc/H2
def plot_model(pars):
    """
    `regions`, `x` must be defined in the model.py module namespace
    """
    from run_emcee.plot_mcmc import get_fig_axes, get_nrows_ncols

    linepars = pars
    dz = 0
    if options['wa_vary']:
        linepars, regpars = pars[:-len(regions)], pars[-len(regions):]
    lines = copy_par_to_lines(linepars, lines_vary)
    
    nrows, ncols = get_nrows_ncols(len(regions))
    fig, axes = get_fig_axes(nrows, ncols, len(regions), width=8.2)
    fig.subplots_adjust(wspace=1e-6, hspace=1e-6, right=1, top=1,
                        left=0.07, bottom=0.06)
    z0 = lines.z[1]
    zp1 = lines.z.mean()
    colours = dict(J0='purple',J1='g',J2='r',J3='b')

    for ind, (wa, tau0, sp, (i, j), tname) in enumerate(regions):

        tau = tau0.copy()
        for l in lines:
            if options['wa_vary']:
                #import pdb; pdb.set_trace()
                dz = regpars[ind] / wa[len(wa)//2] * (l.z + 1)
                #print l.z, dz, l.logN
            
            trans = atom[l.name.strip()]
            tau += calc_iontau(wa, trans, l.z+1 + dz, l.logN, l.b)

        dwpix = wa[1] - wa[0]
        flmodel0 = convolve_with_COS_FOS(np.exp(-tau), wa, dwpix)
        ymod = np.interp(sp.wa[i:j], wa, flmodel0)


        ax = axes[ind]
        i1 = max(i-50, 0)
        j1 = min(j+50, len(sp.wa)-1)
        nfl = sp.fl[i1:j1] / sp.co[i1:j1]
        ner = sp.er[i1:j1] / sp.co[i1:j1]

        tstr, t = findtrans(tname, atom)
        obswa = t.wa * (1 + z0)
        dv = c_kms * (sp.wa[i:j] / obswa - 1)
        dv1 = c_kms * (sp.wa[i1:j1] / obswa - 1)

        tstr = tstr[2:]
        
        ax.axhline(0, color='0.5',lw=0.5)
        ax.axhline(1, color='k', lw=0.5, ls='--')
        ax.fill_between([-150, 150], -0.35, -0.15, color='0.9')
        ax.axhline(-0.25, color='k', lw=0.25)
        ax.plot(dv1, nfl, color='0.7', lw=0.5, drawstyle='steps-mid')
        ax.plot(dv, sp.fl[i:j]/sp.co[i:j], 'k', lw=0.5, drawstyle='steps-mid')
        ax.plot(dv, ymod, color='0.3')
        #ax.axvline(0, color='0.5', lw=0.5)

        ax.plot([-23]*2, [1.1, 1.4], '0.5')
        ax.plot([0]*2, [1.1, 1.4], '0.5')
        
        puttext(0.95, 0.95, tstr, ax, fontsize=9, va='top', ha='right',
                color=colours[tstr[:2]])

        resid = (sp.fl[i:j]/sp.co[i:j] - ymod) / sp.er[i:j] * sp.co[i:j]

        ax.plot(dv, -0.25 + resid*0.1, '.', color='0.3', ms=2)
        ax.set_ylim(-0.5, 1.9)
        ax.set_xlim(-120, 120)
        ax.set_yticks([0, 0.5, 1, 1.5])
        ax.set_yticklabels(['0.0', '0.5', '1.0', '1.5'])
        ax.set_xticks([-100, -50, 0, 50, 100])
        ax.set_xticklabels(['', '-50', '0', '50', ''])
        if ind+1 < (ncols*(nrows-1)):
            ax.set_xticklabels([])
        if ind % ncols:
            ax.set_yticklabels([])

    fig.text(0.5, 0.00, 'Velocity offset (km/s)', fontsize=12, ha='center',va='bottom')
    fig.text(0.015, 0.5, 'Transmission', fontsize=12, rotation=90,va='center')
    ndf = len(ydata) - len(pars)
    print (((ydata - ymodel(x, pars))/ ysigma) **2).sum() / ndf
    pl.savefig('fig/model.pdf')
    pl.savefig('fig/model.png',dpi=300)

    return fig
Exemple #6
0
def model(*par):
    tau = np.zeros_like(X)
    for i in xrange(len(par) // 3):
        z, logN, b = par[3 * i:3 * i + 3]
        tau += calc_iontau(X, trans, z + 1, logN, b)
    return np.exp(-tau)
Exemple #7
0
def model(*par):
    tau = np.zeros_like(X)
    for i in xrange(len(par)//3):
        z,logN,b = par[3*i:3*i+3]
        tau += calc_iontau(X, trans, z+1, logN, b)
    return np.exp(-tau)