Пример #1
0
def make_set(ntrain,
             slines,
             outroot=None,
             tol=1 * u.arcsec,
             igmsp_survey='SDSS_DR7',
             frac_without=0.,
             seed=1234,
             zmin=None,
             zmax=4.5,
             high=False,
             slls=False,
             mix=False,
             low_s2n=False):
    """ Generate a training set

    Parameters
    ----------
    ntrain : int
      Number of training sightlines to generate
    slines : Table
      Table of sightlines without DLAs (usually from SDSS or BOSS)
    igmsp_survey : str, optional
      Dataset name for spectra
    frac_without : float, optional
      Fraction of sightlines (on average) without a DLA
    seed : int, optional
    outroot : str, optional
      Root for output filenames
        root+'.fits' for spectra
        root+'.json' for DLA info
    zmin : float, optional
      Minimum redshift for training; defaults to min(slines['ZEM'])
    zmax : float, optional
      Maximum redshift to train on
    mix : bool, optional
      Mix of SLLS and DLAs
    low_s2n : bool, optional
      Reduce the S/N artificially, i.e. add noise

    Returns
    -------

    """
    from linetools.spectra.utils import collate

    # Init and checks
    igmsp = IgmSpec()
    assert igmsp_survey in igmsp.groups
    rstate = np.random.RandomState(seed)
    rfrac = rstate.random_sample(ntrain)
    if zmin is None:
        zmin = np.min(slines['ZEM'])
    rzem = zmin + rstate.random_sample(ntrain) * (zmax - zmin)
    fNHI = init_fNHI(slls=slls, mix=mix, high=high)

    all_spec = []
    full_dict = {}
    # Begin looping
    for qq in range(ntrain):
        print("qq = {:d}".format(qq))
        full_dict[qq] = {}
        # Grab sightline
        isl = np.argmin(np.abs(slines['ZEM'] - rzem[qq]))
        full_dict[qq]['sl'] = isl  # sightline
        specl, meta = igmsp.spectra_from_coord(
            (slines['RA'][isl], slines['DEC'][isl]),
            groups=['SDSS_DR7'],
            tol=tol,
            verbose=False)
        assert len(meta) == 1
        spec = specl
        # Meta data for header
        mdict = {}
        for key in meta.keys():
            mdict[key] = meta[key][0]
        mhead = Header(mdict)
        # Clear?
        if rfrac[qq] < frac_without:
            spec.meta['headers'][0] = mdict.copy()  #mhead
            all_spec.append(spec)
            full_dict[qq]['nDLA'] = 0
            continue
        # Insert at least one DLA
        spec, dlas = insert_dlas(spec,
                                 mhead['zem_GROUP'],
                                 rstate=rstate,
                                 fNHI=fNHI,
                                 slls=slls,
                                 mix=mix,
                                 high=high,
                                 low_s2n=low_s2n)
        spec.meta['headers'][0] = mdict.copy()  #mhead
        all_spec.append(spec)
        full_dict[qq]['nDLA'] = len(dlas)
        for kk, dla in enumerate(dlas):
            full_dict[qq][kk] = {}
            full_dict[qq][kk]['NHI'] = dla.NHI
            full_dict[qq][kk]['zabs'] = dla.zabs

    # Generate one object
    final_spec = collate(all_spec)
    # Write?
    if outroot is not None:
        # Spectra
        final_spec.write_to_hdf5(outroot + '.hdf5')
        # Dict -> JSON
        gdict = ltu.jsonify(full_dict)
        ltu.savejson(outroot + '.json', gdict,
                     overwrite=True)  #, easy_to_read=True)
    # Return
    return final_spec, full_dict
Пример #2
0
def fig_lowz_hiz(outfil='fig_loz_hiz.png'):
    """ Show varying IGM transmission
    """
    #hdlls_path = '/u/xavier/paper/LLS/Optical/Data/DR1/Spectra/'
    esi_path = '/u/xavier/Keck/ESI/RedData/'
    hst_path = '/u/xavier/HST/Cycle23/z1IGM/Archive/PG1206+459/'
    #
    igmsp = IgmSpec()
    idicts = [
        dict(filename='Data/3C273_STIS_E140M_F.fits'),
        dict(filename=hst_path + 'PG1206+459_E230M_f.fits'),
        dict(coord='J212329.50-005052.9', group=['HD-LLS_DR1']),
        dict(coord='J113621.00+005021.0', group=['HD-LLS_DR1']),
        dict(coord='J113246.5+120901.6', group=['ESI_DLA']),
        dict(filename=esi_path +
             'J1148+5251/SDSSJ1148+5251_stack.fits'),  # z=6
    ]
    lbls = [
        'HST/STIS: 3C273',
        'HST/STIS: PG1206+459',
        'Keck/HIRES: J2123-0050',  # 2.26
        'Magellan/MIKE: J1136+0050',  # 3.43
        'Keck/ESI: J1132+1209',  # 5.17
        'Keck/ESI: J1148+5251',  # 6.4
    ]
    zems = [0.17, 1.16, 2.26, 3.43, 5.17, 6.4]
    xrest = np.array([1080, 1200.])
    ymnx = [-0.1, 1.1]

    # Cut down
    idicts = [idicts[0], idicts[3]]
    lbls = [lbls[0], lbls[3]]
    zems = [zems[0], zems[3]]

    lw = 1.
    csz = 19.

    # Start the plot
    #fig = plt.figure(figsize=(5.0, 8.0))
    fig = plt.figure(figsize=(8.0, 5.0))

    plt.clf()
    gs = gridspec.GridSpec(len(lbls), 1)

    # Loop
    for qq, lbl in enumerate(lbls):

        # Grab data
        idict = idicts[qq]
        if 'coord' in idict.keys():
            qdict = {}
            for key in idict.keys():
                if key not in ['coord', 'group']:
                    qdict[key] = idict[key]
            spec, meta = igmsp.spectra_from_coord(
                idict['coord'], tol=5. * u.arcsec,
                groups=idict['group'])  #, query_dict=qdict)
            if meta is None:
                print("Bad coord?")
                pdb.set_trace()
            elif len(meta) > 1:
                pdb.set_trace()
        else:
            spec = lsio.readspec(idict['filename'])

        if lbl == 'HST/STIS: 3C273':
            hdu = fits.open('Data/3C273_STIS_E140M_c.fits')
            conti_3c273 = hdu[0].data
            spec.co = conti_3c273
            spec.normed = True

        # Spectrum
        ax = plt.subplot(gs[qq])
        ax.set_xlim(xrest * (1 + zems[qq]) / 1215.67 - 1)
        ax.set_ylim(ymnx)
        if qq == 3:
            ax.set_ylabel('Normalized Flux')
        if qq == len(lbls) - 1:
            ax.set_xlabel(r'Redshift of Ly$\alpha$')

        ax.plot(spec.wavelength.value / 1215.6701 - 1,
                spec.flux,
                'k',
                linewidth=lw)

        # Label
        #ax.text(0.05, 0.95, lbl+' zem={:0.1f}'.format(zems[qq]), color='blue',
        #    transform=ax.transAxes, size=csz, ha='left', bbox={'facecolor':'white'})
        #
        set_fontsize(ax, 12.)

    # Layout and save
    #plt.subplots_adjust(hspace=0)
    plt.tight_layout(pad=0.2, h_pad=0.0, w_pad=0.4)
    plt.savefig(outfil, dpi=600)
    plt.close()
    # Finish
    print('Writing {:s}'.format(outfil))