Пример #1
0
def test_init_from_fits():
    """ Load FNConstraint from a set of FITS files
    """
    # Load
    all_fN_cs = FNConstraint.load_defaults()
    # Test
    assert len(all_fN_cs) == 8
    fN_dtype = [fc.fN_dtype for fc in all_fN_cs]
    itau = fN_dtype.index('teff')
    np.testing.assert_allclose(all_fN_cs[itau].data['TEFF'], 0.19778812)
Пример #2
0
def test_init_from_fits():
    """ Load FNConstraint from a set of FITS files
    """
    # Load
    all_fN_cs = FNConstraint.load_defaults()
    # Test
    assert len(all_fN_cs) == 8
    fN_dtype = [fc.fN_dtype for fc in all_fN_cs]
    itau = fN_dtype.index('teff')
    np.testing.assert_allclose(all_fN_cs[itau].data['TEFF'], 0.19778812)
Пример #3
0
def set_fn_data(flg=2, sources=None, extra_fNc=[]):
    """ Load up f(N) data

    Parameters
    ----------
    flg : int, optional
      2 : z~2 constraints
      5 : z~5 constraints
    sources : list, optional
      References for constraints
    extra_fNc : list, optional

    Returns
    -------
    fN_data :: List of fN_Constraint Classes
    """
    fN_cs = []
    if flg == 2:
        if sources is None:
            sources = ['OPB07', 'OPW12', 'OPW13', 'K05', 'K13R13', 'N12']

        fn_file = pyigm_path + '/data/fN/fn_constraints_z2.5_vanilla.fits'
        k13r13_file = pyigm_path + '/data/fN/fn_constraints_K13R13_vanilla.fits'
        n12_file = pyigm_path + '/data/fN/fn_constraints_N12_vanilla.fits'
        all_fN_cs = FNConstraint.from_fitsfile(
            [fn_file, k13r13_file, n12_file])

        # Add on, e.g. user-supplied
        if len(extra_fNc) > 0:
            raise IOError("NOT READY FOR THIS YET")
            #for src in extra_fNc:
            #	all_fN_cs.append(xifd.fN_data_from_ascii_file(os.path.abspath(src)))

        # Include good data sources
        for fN_c in all_fN_cs:
            # In list?
            if fN_c.ref in sources:
                print('Using {:s} as a constraint'.format(fN_c.ref))
                # Append
                fN_cs.append(fN_c)
                # Pop
                idx = sources.index(fN_c.ref)
                sources.pop(idx)

        # Check that all the desired sources were used
        if len(sources) > 0:
            pdb.set_trace()
    elif flg == 5:
        if sources is None:
            sources = ['Worseck+14', 'Crighton+15', 'Crighton+16', 'Becker+13']
        chk_sources = sources[:]

        #all_fN_cs = FNConstraint.from_fitsfile([fn_file,k13r13_file,n12_file])

        # MFP (Worseck+14)
        fN_MFPa = FNConstraint('MFP',
                               4.56,
                               ref='Worseck+14',
                               flavor='\\lmfp',
                               data=dict(MFP=22.2, SIG_MFP=2.3,
                                         COSM='VANILLA'))
        fN_MFPb = FNConstraint('MFP',
                               4.86,
                               ref='Worseck+14',
                               flavor='\\lmfp',
                               data=dict(MFP=15.1, SIG_MFP=1.8,
                                         COSM='VANILLA'))
        fN_MFPc = FNConstraint('MFP',
                               5.16,
                               ref='Worseck+14',
                               flavor='\\lmfp',
                               data=dict(MFP=10.3, SIG_MFP=1.6,
                                         COSM='VANILLA'))
        fN_MFP = [fN_MFPa, fN_MFPb, fN_MFPc]
        # LLS (Crighton+16)
        fN_LLSa = FNConstraint('LLS',
                               np.mean([3.75, 4.40]),
                               ref='Crighton+16',
                               flavor='\\tlox',
                               data=dict(LX=0.628,
                                         SIG_LX=0.095,
                                         TAU_LIM=2.,
                                         COSM='VANILLA'))
        fN_LLSb = FNConstraint('LLS',
                               np.mean([4.40, 4.70]),
                               ref='Crighton+16',
                               flavor='\\tlox',
                               data=dict(LX=0.601,
                                         SIG_LX=0.102,
                                         TAU_LIM=2.,
                                         COSM='VANILLA'))
        fN_LLSc = FNConstraint('LLS',
                               np.mean([4.70, 5.40]),
                               ref='Crighton+16',
                               flavor='\\tlox',
                               data=dict(LX=0.928,
                                         SIG_LX=0.151,
                                         TAU_LIM=2.,
                                         COSM='VANILLA'))
        fN_LLS = [fN_LLSa, fN_LLSb, fN_LLSc]
        # DLA (Crighton+15)
        tau_lim = 10.**(20.3 - 17.19)
        fN_DLAa = FNConstraint('DLA',
                               np.mean([3.56, 4.45]),
                               ref='Crighton+15',
                               flavor='\\tdlox',
                               data=dict(LX=0.059,
                                         SIG_LX=0.018,
                                         COSM='VANILLA',
                                         TAU_LIM=tau_lim))
        fN_DLAb = FNConstraint('DLA',
                               np.mean([4.45, 5.31]),
                               ref='Crighton+15',
                               flavor='\\tdlox',
                               data=dict(LX=0.095,
                                         SIG_LX=0.022,
                                         COSM='VANILLA',
                                         TAU_LIM=tau_lim))
        fN_DLAc = FNConstraint(
            'fN',
            np.mean([3.6, 5.2]),
            ref='Crighton+15',
            flavor='f(N)',
            data=dict(COSM='VANILLA',
                      NPT=5,
                      FN=np.array([
                          -22.1247392, -22.12588672, -22.51361414, -22.7732822,
                          -23.76709909
                      ]),
                      SIG_FN=np.array([[
                          0.24127323, 0.17599877, 0.17613792, 0.14095363,
                          0.30129492
                      ],
                                       [
                                           0.21437162, 0.15275017, 0.12551036,
                                           0.12963855, 0.17654378
                                       ]]),
                      BINS=np.array([[20.175, 20.425, 20.675, 20.925, 21.05],
                                     [20.675, 20.925, 21.175, 21.425,
                                      22.05]])))

        fN_DLA = [fN_DLAa, fN_DLAb, fN_DLAc]
        # tau_eff (Becker+13)
        b13_tab2 = Table.read(pyigm.__path__[0] +
                              '/data/teff/becker13_tab2.dat',
                              format='ascii')
        fN_teff = []
        for row in b13_tab2:
            if row['z'] < 4.:
                continue
            # calculate
            teff = -1 * np.log(row['F'])
            sigteff = row['s(F)'] / row['F']
            # Generate
            fN = FNConstraint('teff',
                              row['z'],
                              ref='Becker+13',
                              flavor='\\tlya',
                              data=dict(Z_TEFF=row['z'],
                                        TEFF=teff,
                                        SIG_TEFF=sigteff,
                                        COSM='N/A',
                                        NHI_MNX=[11., 22.]))
            # Append
            fN_teff.append(fN)
        # Collate
        all_fN_cs = fN_MFP + fN_DLA + fN_teff + fN_LLS

        # Include good data sources
        for fN_c in all_fN_cs:
            # In list?
            if fN_c.ref in sources:
                print('Using {:s} as a constraint'.format(fN_c.ref))
                # Append
                fN_cs.append(fN_c)
                # Pop
                try:
                    idx = chk_sources.index(fN_c.ref)
                except ValueError:
                    pass
                else:
                    chk_sources.pop(idx)

        # Check that all the desired sources were used
        if len(chk_sources) > 0:
            pdb.set_trace()

    return fN_cs
Пример #4
0
def test_init():
    tst = FNConstraint('fN')
    assert tst.fN_dtype == 'fN'
Пример #5
0
def tst_fn_data(fN_model=None, model_two=None, data_list=None, outfil=None):
    """ Make a matplotlib plot like the final figure from P14
    See the Notebook for Bokeh plots
    Taken from xastropy without actually trying to run

    Parameters
    ----------
    fN_model
    model_two
    data_list
    outfil
    """
    import matplotlib as mpl
    mpl.rcParams['font.family'] = 'STIXGeneral-Regular' # Not for PDF
    mpl.rcParams['lines.linewidth'] = 2

    from matplotlib import pyplot as plt
    #from matplotlib.backends.backend_pdf import PdfPages

    # fN data
    all_fN_cs = FNConstraint.load_defaults()

    # Remove K12
    if data_list is None:
        fN_cs = [fN_c for fN_c in all_fN_cs if ((fN_c.ref != 'K02') & (fN_c.ref != 'PW09'))]
    else:
        fN_cs = [fN_c for fN_c in all_fN_cs if fN_c.ref in data_list]
    fN_dtype = [fc.fN_dtype for fc in fN_cs]

    fig = plt.figure(figsize=(8, 5))
    fig.clf()
    main = fig.add_axes( [0.1, 0.1, 0.8, 0.8] ) # xypos, xy-size

    # f(N) data
    main.set_ylabel(r'$\log f(N_{\rm HI})$')
    main.set_xlabel(r'$\log N_{\rm HI}$')
    main.set_ylim(-25., -9)

    for fN_c in fN_cs:
        if fN_c.fN_dtype == 'fN':
            # Length
            ip = range(fN_c.data['NPT'])
            val = np.where(fN_c.data['FN'][ip] > -90)[0]
            if len(val) > 0:
                ipv = np.array(ip)[val]
                xval = np.median(fN_c.data['BINS'][:,ipv],0)
                xerror = [ fN_c.data['BINS'][1,ipv]-xval, xval-fN_c.data['BINS'][0,ipv] ]
                yerror = [ fN_c.data['SIG_FN'][1,ipv], fN_c.data['SIG_FN'][0,ipv] ] # Inverted!
                main.errorbar(xval, fN_c.data['FN'][ipv], xerr=xerror, yerr=yerror, fmt='o',
                              label=fN_c.ref,capthick=2)
    main.legend(loc='lower left', numpoints=1)

    # Model?
    if fN_model is not None:
        xplt = 12.01 + 0.01*np.arange(1100)
        yplt = fN_model.evaluate(xplt, 2.4)
        main.plot(xplt,yplt,'-',color='black')
        print(xplt[0],yplt[0])
    if model_two is not None:
        xplt = 12.01 + 0.01*np.arange(1100)
        yplt = model_two.evaluate(xplt, 2.4)
        main.plot(xplt,yplt,'-',color='gray')

    # Extras
    #mpl.rcParams['lines.capthick'] = 2

    inset = fig.add_axes( [0.55, 0.6, 0.25, 0.25] ) # xypos, xy-size
    inset.set_ylabel('Value') # LHS
    inset.xaxis.set_major_locator(plt.FixedLocator(range(5)))
    inset.xaxis.set_major_formatter(plt.FixedFormatter(['',r'$\tau_{\rm eff}^{\rm Ly\alpha}$',
                                                        r'$\ell(X)_{\rm LLS}$',
                                                        r'$\lambda_{\rm mfp}^{912}$', '']))
    inset.set_ylim(0., 0.6)

    # tau_eff
    flg_teff = 1
    try:
        itau = fN_dtype.index('teff') # Passes back the first one
    except:
        flg_teff = 0

    if flg_teff:
        teff=float(fN_cs[itau].data['TEFF'])
        D_A = 1. - np.exp(-1. * teff)
        SIGDA_LIMIT = 0.1  # Allows for systemtics and b-value uncertainty
        sig_teff = np.max([fN_cs[itau].data['SIG_TEFF'], (SIGDA_LIMIT*teff)])
        # Plot
        inset.errorbar(1, teff, sig_teff, fmt='_', capthick=2)
        # Model
        if fN_model is not None:
            model_teff = tau_eff.lyman_ew(1215.6701*(1+fN_cs[itau].zeval), fN_cs[itau].zeval+0.1,
                                          fN_model, NHI_MIN=fN_cs[itau].data['NHI_MNX'][0],
                                          NHI_MAX=fN_cs[itau].data['NHI_MNX'][1])
            inset.plot(1, model_teff, 'ko')
            #xdb.set_trace()

    ## #######
    # LLS constraint
    flg_LLS = 1
    try:
        iLLS = fN_dtype.index('LLS') # Passes back the first one
    except:
        #raise ValueError('fN.data: Missing LLS type')
        flg_LLS = 0
    if flg_LLS:
        inset.errorbar(2, fN_cs[iLLS].data['LX'], yerr=fN_cs[iLLS].data['SIG_LX'],
                       fmt='_', capthick=2)
        # Model
        if fN_model != None:
            lX = fN_model.calculate_lox(fN_cs[iLLS].zeval,
                                        17.19+np.log10(fN_cs[iLLS].data['TAU_LIM']), 22.)
            inset.plot(2, lX, 'ko')

    ## #######
    # MFP constraint
    flg_MFP = 1
    try:
        iMFP = fN_dtype.index('MFP') # Passes back the first one
    except:
        #raise ValueError('fN.data: Missing MFP type')
        flg_MFP = 0

    if flg_MFP:
        inset2 = inset.twinx()
        inset2.errorbar(3, fN_cs[iMFP].data['MFP'], yerr=fN_cs[iMFP].data['SIG_MFP'],
                        fmt='_', capthick=2)
        inset2.set_xlim(0,4)
        inset2.set_ylim(0,350)
        inset2.set_ylabel('(Mpc)')

        # Model
        if fN_model is not None:
            #fN_model.zmnx = (0.1, 20.) # Reset for MFP calculation
            mfp = fN_model.mfp(fN_cs[iMFP].zeval)
            inset2.plot(3, mfp, 'ko')

    # Show
    if outfil != None:
        plt.savefig(outfil,bbox_inches='tight')
    else:
        plt.show()
Пример #6
0
def set_fn_data(flg=2, sources=None, extra_fNc=[]):
    """ Load up f(N) data

    Parameters
    ----------
    flg : int, optional
      2 : z~2 constraints
      5 : z~5 constraints
    sources : list, optional
      References for constraints
    extra_fNc : list, optional

    Returns
    -------
    fN_data :: List of fN_Constraint Classes
    """
    fN_cs = []
    if flg == 2:
        if sources is None:
            sources = ['OPB07', 'OPW12', 'OPW13', 'K05', 'K13R13', 'N12']

        fn_file = pyigm_path+'/data/fN/fn_constraints_z2.5_vanilla.fits'
        k13r13_file = pyigm_path+'/data/fN/fn_constraints_K13R13_vanilla.fits'
        n12_file = pyigm_path+'/data/fN/fn_constraints_N12_vanilla.fits'
        all_fN_cs = FNConstraint.from_fitsfile([fn_file,k13r13_file,n12_file])

        # Add on, e.g. user-supplied
        if len(extra_fNc) > 0:
            raise IOError("NOT READY FOR THIS YET")
            #for src in extra_fNc:
            #	all_fN_cs.append(xifd.fN_data_from_ascii_file(os.path.abspath(src)))

        # Include good data sources
        for fN_c in all_fN_cs:
            # In list?
            if fN_c.ref in sources:
                print('Using {:s} as a constraint'.format(fN_c.ref))
                # Append
                fN_cs.append(fN_c)
                # Pop
                idx = sources.index(fN_c.ref)
                sources.pop(idx)

        # Check that all the desired sources were used
        if len(sources) > 0:
            pdb.set_trace()
    elif flg == 5:
        if sources is None:
            sources = ['Worseck+14', 'Crighton+15', 'Crighton+16', 'Becker+13']
        chk_sources = sources[:]

        #all_fN_cs = FNConstraint.from_fitsfile([fn_file,k13r13_file,n12_file])

        # MFP (Worseck+14)
        fN_MFPa = FNConstraint('MFP', 4.56, ref='Worseck+14', flavor='\\lmfp', data=dict(MFP=22.2,SIG_MFP=2.3, COSM='VANILLA'))
        fN_MFPb = FNConstraint('MFP', 4.86, ref='Worseck+14', flavor='\\lmfp', data=dict(MFP=15.1,SIG_MFP=1.8, COSM='VANILLA'))
        fN_MFPc = FNConstraint('MFP', 5.16, ref='Worseck+14', flavor='\\lmfp', data=dict(MFP=10.3,SIG_MFP=1.6, COSM='VANILLA'))
        fN_MFP = [fN_MFPa, fN_MFPb, fN_MFPc]
        # LLS (Crighton+16)
        fN_LLSa = FNConstraint('LLS', np.mean([3.75,4.40]), ref='Crighton+16', flavor='\\tlox', data=dict(LX=0.628,SIG_LX=0.095, TAU_LIM=2., COSM='VANILLA'))
        fN_LLSb = FNConstraint('LLS', np.mean([4.40,4.70]), ref='Crighton+16', flavor='\\tlox', data=dict(LX=0.601,SIG_LX=0.102, TAU_LIM=2., COSM='VANILLA'))
        fN_LLSc = FNConstraint('LLS', np.mean([4.70, 5.40]), ref='Crighton+16', flavor='\\tlox', data=dict(LX=0.928,SIG_LX=0.151, TAU_LIM=2., COSM='VANILLA'))
        fN_LLS = [fN_LLSa, fN_LLSb, fN_LLSc]
        # DLA (Crighton+15)
        tau_lim = 10.**(20.3-17.19)
        fN_DLAa = FNConstraint('DLA', np.mean([3.56,4.45]), ref='Crighton+15', flavor='\\tdlox',
                               data=dict(LX=0.059, SIG_LX=0.018, COSM='VANILLA', TAU_LIM=tau_lim))
        fN_DLAb = FNConstraint('DLA', np.mean([4.45,5.31]), ref='Crighton+15', flavor='\\tdlox',
                               data=dict(LX=0.095, SIG_LX=0.022, COSM='VANILLA', TAU_LIM=tau_lim))
        fN_DLAc = FNConstraint('fN', np.mean([3.6,5.2]), ref='Crighton+15', flavor='f(N)',
                               data=dict(COSM='VANILLA', NPT=5,
                                         FN=np.array([-22.1247392 , -22.12588672, -22.51361414, -22.7732822 , -23.76709909]),
                                         SIG_FN=np.array([[ 0.24127323,  0.17599877,  0.17613792,  0.14095363,  0.30129492],
                                                        [ 0.21437162,  0.15275017,  0.12551036,  0.12963855,  0.17654378]]),
                                         BINS=np.array([[ 20.175,  20.425,  20.675,  20.925,  21.05 ],
                                                        [ 20.675,  20.925,  21.175,  21.425,  22.05 ]])))

        fN_DLA = [fN_DLAa, fN_DLAb, fN_DLAc]
        # tau_eff (Becker+13)
        b13_tab2 = Table.read(pyigm.__path__[0]+'/data/teff/becker13_tab2.dat', format='ascii')
        fN_teff = []
        for row in b13_tab2:
            if row['z'] < 4.:
                continue
            # calculate
            teff = -1*np.log(row['F'])
            sigteff = row['s(F)']/row['F']
            # Generate
            fN = FNConstraint('teff', row['z'], ref='Becker+13', flavor='\\tlya',
                              data=dict(Z_TEFF=row['z'], TEFF=teff, SIG_TEFF=sigteff,
                                        COSM='N/A', NHI_MNX=[11.,22.]))
            # Append
            fN_teff.append(fN)
        # Collate
        all_fN_cs = fN_MFP + fN_DLA + fN_teff + fN_LLS

        # Include good data sources
        for fN_c in all_fN_cs:
            # In list?
            if fN_c.ref in sources:
                print('Using {:s} as a constraint'.format(fN_c.ref))
                # Append
                fN_cs.append(fN_c)
                # Pop
                try:
                    idx = chk_sources.index(fN_c.ref)
                except ValueError:
                    pass
                else:
                    chk_sources.pop(idx)

        # Check that all the desired sources were used
        if len(chk_sources) > 0:
            pdb.set_trace()

    return fN_cs