Exemplo n.º 1
0
def test_star_type_probability_all_params():
    """
    Test for star_type_probability.py
    """

    # download the needed files
    pdf1d_fname = download_rename("beast_example_phat_pdf1d.fits")
    pdf2d_fname = download_rename("beast_example_phat_pdf2d.fits")
    star_prob_fname = download_rename("beast_example_phat_startype.fits")

    # run star_type_probability
    star_prob = star_type_probability.star_type_probability(
        pdf1d_fname,
        pdf2d_fname,
        output_filebase=None,
        ext_O_star_params={
            'min_M_ini': 10,
            'min_Av': 0.5,
            'max_Av': 5
        })

    # expected output table
    expected_star_prob = Table.read(star_prob_fname)

    # compare to new table
    compare_tables(expected_star_prob, Table(star_prob))
Exemplo n.º 2
0
    def test_star_type_probability_no_Av(self):
        """
        Test for star_type_probability.py
        """
        # download the needed files
        pdf2d_fname = download_rename("beast_example_phat_pdf2d_no_Av.fits")
        star_prob_fname = download_rename(
            "beast_example_phat_startype_no_Av.fits")

        # run star_type_probability
        star_prob = star_type_probability.star_type_probability(
            self.pdf1d_fname_cache,
            pdf2d_fname,
            output_filebase=None,
            ext_O_star_params={
                "min_M_ini": 10,
                "min_Av": 0.5,
                "max_Av": 5
            },
        )

        # expected output table
        expected_star_prob = Table.read(star_prob_fname)

        # compare to new table
        compare_tables(expected_star_prob, Table(star_prob))
def test_padova_isochrone_download():
    # download the cached version
    iso_fname_cache = download_rename("beast_example_phat_iso.csv")

    # download the file live from the website
    savename = "/tmp/padova_iso.csv"
    iso_fname, oiso = make_iso_table(
        "test",
        iso_fname=savename,
        logtmin=6.0,
        logtmax=10.13,
        dlogt=1.0,
        z=[0.03, 0.019, 0.008, 0.004],
    )

    # read the cached and new tables using astropy tables
    table_cache = Table.read(iso_fname_cache,
                             format="ascii.csv",
                             comment="#",
                             delimiter=",")
    table_new = Table.read(iso_fname,
                           format="ascii.csv",
                           comment="#",
                           delimiter=",")

    # compare
    compare_tables(table_cache, table_new)
Exemplo n.º 4
0
    def test_padova_isochrone_download(self):
        """
        Generate the padova isochrone table and compare the result to a cached version.
        """
        # download the file live from the website
        savename = tempfile.NamedTemporaryFile(suffix=".csv").name
        infoname = tempfile.NamedTemporaryFile(suffix=".asdf").name
        (iso_fname, g) = make_iso_table(
            "test",
            iso_fname=savename,
            logtmin=self.settings.logt[0],
            logtmax=self.settings.logt[1],
            dlogt=self.settings.logt[2],
            z=self.settings.z,
            info_fname=infoname,
        )

        # read the cached and new tables using astropy tables
        table_cache = Table.read(self.iso_fname_cache,
                                 format="ascii.csv",
                                 comment="#",
                                 delimiter=",")
        table_new = Table.read(iso_fname,
                               format="ascii.csv",
                               comment="#",
                               delimiter=",")

        # compare
        compare_tables(table_cache, table_new)
Exemplo n.º 5
0
    def test_simobs(self):
        """
        Simulate observations using cached versions of the sed grid and noise model
        and compare the result to a cached version.
        """
        # download files specific to this test
        simobs_fname_cache = download_rename("beast_example_phat_simobs.fits")

        # get the physics model grid - includes priors
        modelsedgrid = SEDGrid(self.seds_fname_cache)

        # read in the noise model - includes bias, unc, and completeness
        noisegrid = noisemodel.get_noisemodelcat(self.noise_fname_cache)

        table_new = gen_SimObs_from_sedgrid(
            modelsedgrid,
            noisegrid,
            nsim=100,
            compl_filter="max",
            ranseed=1234,
        )

        # check that the simobs files are exactly the same
        table_cache = Table.read(simobs_fname_cache)

        # to avoid issues with uppercase vs lowercase column names, make them all
        # the same before comparing
        for col in table_new.colnames:
            table_new[col].name = col.upper()
        for col in table_cache.colnames:
            table_cache[col].name = col.upper()

        compare_tables(table_cache, table_new)
Exemplo n.º 6
0
    def test_ast_pick_models(self):
        """
        Generate the artifial star test (AST) inputs using a cached version of
        the sed grid and compare the result to a cached version.
        """
        # download files specific to this test
        cached_table_filename = download_rename(
            "phat_small/cache_inputAST.txt")

        mag_cuts = [1.0]

        seds_fname = self.seds_fname_cache
        if self.dset != "phat":
            seds_fname = download_rename(
                "phat_small/beast_example_phat_seds.grid.hd5")
        else:
            seds_fname = self.seds_fname_cache

        outname = tempfile.NamedTemporaryFile(suffix=".txt").name
        make_ast_input_list.pick_models(
            seds_fname,
            self.settings.filters,
            mag_cuts,
            outfile=outname,
            ranseed=1234,
        )

        table_new = Table.read(outname, format="ascii")

        # download cached version of the file and compare it to new file
        table_cache = Table.read(cached_table_filename,
                                 format="csv",
                                 delimiter=" ")
        compare_tables(table_new, table_cache)
Exemplo n.º 7
0
    def test_compare_spec_type_inFOV(self):
        """
        Test for compare_spec_type.  Inputs and expected outputs created by
        running generate_files_for_tests.py in beast-examples/metal_small.

        In this version, the stars are in the imaging field of view.
        """

        # download cached file
        compare_spec_type_fname = download_rename(
            f"{self.basename}_compare_spec_type.asdf")
        with asdf.open(compare_spec_type_fname) as af:
            compare_spec_type_info = copy.deepcopy(af.tree)

        # run compare_spec_type
        spec_type = compare_spec_type(
            self.obs_fname_cache,
            self.stats_fname_cache,
            **compare_spec_type_info["input"],
        )

        # expected output table
        expected_table = Table(compare_spec_type_info["output"])

        # compare to new table
        compare_tables(expected_table, Table(spec_type), rtol=2e-3)
Exemplo n.º 8
0
    def test_star_type_probability_all_params(self):
        """
        Test for star_type_probability.  Inputs and expected outputs created by
        running generate_files_for_tests.py in beast-examples/metal_small.

        In this version, all required parameters are present.
        """
        # download cached file
        star_prob_fname = download_rename(
            f"{self.basename}_star_type_probability.asdf")
        with asdf.open(star_prob_fname) as af:
            star_prob_info = copy.deepcopy(af.tree)

        # run star_type_probability
        star_prob = star_type_probability.star_type_probability(
            self.pdf1d_fname_cache,
            self.pdf2d_fname_cache,
            **star_prob_info["input"],
        )

        # expected output table
        expected_star_prob = Table(star_prob_info["output"])

        # compare to new table
        compare_tables(expected_star_prob, Table(star_prob))
Exemplo n.º 9
0
    def test_create_physicsmodel_no_subgrid(self):
        """
        Test create_physicsmodel.py, assuming no subgrids
        """

        # run create_physicsmodel
        create_physicsmodel.create_physicsmodel(self.settings,
                                                nsubs=self.settings.n_subgrid,
                                                nprocs=1)

        # check that files match
        # - isochrones
        table_cache = Table.read(
            self.iso_fname_cache,
            format="ascii.csv",
            comment="#",
            delimiter=",",
        )
        table_new = Table.read(
            f"./{self.settings.project}/{self.settings.project}_iso.csv",
            format="ascii.csv",
            comment="#",
            delimiter=",",
        )
        compare_tables(table_cache, table_new)
        # - spectra with priors
        compare_hdf5(
            self.priors_fname_cache,
            f"./{self.settings.project}/{self.settings.project}_spec_w_priors.grid.hd5",
        )
        # - SEDs grid
        compare_hdf5(
            self.seds_fname_cache,
            f"./{self.settings.project}/{self.settings.project}_seds.grid.hd5",
        )
Exemplo n.º 10
0
def test_pick_models():
    # download the needed files
    vega_fname = download_rename("vega.hd5")
    filename = download_rename("beast_example_phat_seds.grid.hd5")

    filters = [
        "HST_WFC3_F275W",
        "HST_WFC3_F336W",
        "HST_ACS_WFC_F475W",
        "HST_ACS_WFC_F814W",
        "HST_WFC3_F110W",
        "HST_WFC3_F160W",
    ]
    mag_cuts = [1.0]

    make_ast_input_list.pick_models(
        filename,
        filters,
        mag_cuts,
        vega_fname=vega_fname,
        outfile="/tmp/test_inputAST.txt",
        ranseed=1234,
    )

    table_new = Table.read("/tmp/test_inputAST.txt", format="ascii")

    # download cached version of the file and compare it to new file
    cached_table_filename = download_rename("cache_inputAST.txt")
    table_cache = Table.read(cached_table_filename,
                             format="csv",
                             delimiter=" ")
    compare_tables(table_new, table_cache)
Exemplo n.º 11
0
def test_trim_grid():

    # download the needed files
    vega_fname = download_rename('vega.hd5')
    seds_fname = download_rename('beast_example_phat_seds.grid.hd5')
    noise_fname = download_rename('beast_example_phat_noisemodel.hd5')

    # download cached version of noisemodel on the sed grid
    simobs_fname_cache = download_rename('beast_example_phat_simobs.fits')

    ################

    # get the physics model grid - includes priors
    modelsedgrid = FileSEDGrid(seds_fname)

    # read in the noise model - includes bias, unc, and completeness
    noisegrid = noisemodel.get_noisemodelcat(noise_fname)

    table_new = gen_SimObs_from_sedgrid(modelsedgrid,
                                        noisegrid,
                                        nsim=100,
                                        compl_filter="f475w",
                                        ranseed=1234,
                                        vega_fname=vega_fname)

    # check that the simobs files are exactly the same
    table_cache = Table.read(simobs_fname_cache)

    compare_tables(table_cache, table_new)
Exemplo n.º 12
0
    def test_create_physicsmodel_with_subgrid(self):
        """
        Test create_physicsmodel.py, assuming two subgrids
        """

        # run create_physicsmodel
        create_physicsmodel.create_physicsmodel(
            self.settings_sg, nsubs=self.settings_sg.n_subgrid, nprocs=1)

        # check that files match

        # - isochrones
        table_cache = Table.read(
            self.iso_fname_cache,
            format="ascii.csv",
            comment="#",
            delimiter=",",
        )
        table_new = Table.read(
            "beast_metal_small_subgrids/beast_metal_small_subgrids_iso.csv",
            format="ascii.csv",
            comment="#",
            delimiter=",",
        )
        compare_tables(table_cache, table_new)

        # - spectra with priors
        compare_hdf5(
            self.priors_fname_cache,
            "./beast_metal_small_subgrids/beast_metal_small_subgrids_spec_w_priors.grid.hd5",
        )
        compare_hdf5(
            self.priors_sub0_fname_cache,
            "beast_metal_small_subgrids/beast_metal_small_subgrids_spec_w_priors.gridsub0.hd5",
        )
        compare_hdf5(
            self.priors_sub1_fname_cache,
            "beast_metal_small_subgrids/beast_metal_small_subgrids_spec_w_priors.gridsub1.hd5",
        )

        # - SEDs grid
        compare_hdf5(
            self.seds_sub0_fname_cache,
            "beast_metal_small_subgrids/beast_metal_small_subgrids_seds.gridsub0.hd5",
        )
        compare_hdf5(
            self.seds_sub1_fname_cache,
            "beast_metal_small_subgrids/beast_metal_small_subgrids_seds.gridsub1.hd5",
        )

        # - list of subgrids
        with open("./beast_metal_small_subgrids/subgrid_fnames.txt") as f:
            temp = f.read()
        subgrid_list = [x for x in temp.split("\n") if x != ""]
        expected_list = [
            "beast_metal_small_subgrids/beast_metal_small_subgrids_seds.gridsub0.hd5",
            "beast_metal_small_subgrids/beast_metal_small_subgrids_seds.gridsub1.hd5",
        ]
        assert subgrid_list == expected_list, "subgrid_fnames.txt has incorrect content"
Exemplo n.º 13
0
def test_fit_grid():

    # download the needed files
    vega_fname = download_rename('vega.hd5')
    obs_fname = download_rename('b15_4band_det_27_A.fits')
    noise_trim_fname = download_rename(
        'beast_example_phat_noisemodel_trim.grid.hd5')
    seds_trim_fname = download_rename('beast_example_phat_seds_trim.grid.hd5')

    # download cached version of fitting results
    stats_fname_cache = download_rename('beast_example_phat_stats.fits')
    pdf1d_fname_cache = download_rename('beast_example_phat_pdf1d.fits')

    ################

    # read in the the AST noise model
    noisemodel_vals = noisemodel.get_noisemodelcat(noise_trim_fname)

    # read in the observed data
    filters = [
        'HST_WFC3_F275W', 'HST_WFC3_F336W', 'HST_ACS_WFC_F475W',
        'HST_ACS_WFC_F814W', 'HST_WFC3_F110W', 'HST_WFC3_F160W'
    ]
    basefilters = ['F275W', 'F336W', 'F475W', 'F814W', 'F110W', 'F160W']
    obs_colnames = [f.lower() + '_rate' for f in basefilters]

    obsdata = get_obscat(obs_fname,
                         filters,
                         obs_colnames,
                         vega_fname=vega_fname)
    # output files
    stats_fname = '/tmp/beast_example_phat_stats.fits'
    pdf1d_fname = '/tmp/beast_example_phat_pdf1d.fits'
    lnp_fname = '/tmp/beast_example_phat_lnp.hd5'

    fit.summary_table_memory(obsdata,
                             noisemodel_vals,
                             seds_trim_fname,
                             threshold=-10.,
                             save_every_npts=100,
                             lnp_npts=60,
                             stats_outname=stats_fname,
                             pdf1d_outname=pdf1d_fname,
                             lnp_outname=lnp_fname)

    # check that the stats files are exactly the same
    table_cache = Table.read(stats_fname_cache)
    table_new = Table.read(stats_fname)

    compare_tables(table_cache, table_new)

    # lnp files not checked as they are randomly sparsely sampled
    #   hence will be different every time the fitting is run

    # check that the pdf1d files are exactly the same
    compare_fits(pdf1d_fname_cache, pdf1d_fname)
Exemplo n.º 14
0
def test_convert_hd5_to_fits():

    # Pick some random .hd5 file to convert
    data_fname = download_rename("M31-B09-EAST_tinychunk.phot.hdf5")
    data_fname_cache = download_rename("M31-B09-EAST_tinychunk.st.fits")

    # Convert the file
    st_file(data_fname)

    # Compare the contents of the new file to the cached version
    data = Table(fits.getdata(data_fname.replace("phot.hdf5", "st.fits")))
    data_cache = Table(fits.getdata(data_fname_cache))

    compare_tables(data_cache, data)
Exemplo n.º 15
0
    def test_fit_grid(self):
        """
        Fit a cached version of the observations with cached version of the
        trimmed sed grid and noisemodel and compare the result to cached
        versions of the stats and pdf1d files.
        """
        # read in the the AST noise model
        noisemodel_vals = noisemodel.get_noisemodelcat(
            self.noise_trim_fname_cache)

        # read in the observed data
        obsdata = Observations(self.obs_fname_cache, self.settings.filters,
                               self.settings.obs_colnames)
        # output files
        stats_fname = tempfile.NamedTemporaryFile(suffix=".fits").name
        pdf1d_fname = tempfile.NamedTemporaryFile(suffix=".fits").name
        pdf2d_fname = tempfile.NamedTemporaryFile(suffix=".fits").name
        lnp_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name

        fit.summary_table_memory(
            obsdata,
            noisemodel_vals,
            self.seds_trim_fname_cache,
            threshold=-10.0,
            save_every_npts=100,
            lnp_npts=500,
            max_nbins=200,
            stats_outname=stats_fname,
            pdf1d_outname=pdf1d_fname,
            pdf2d_outname=pdf2d_fname,
            pdf2d_param_list=["Av", "M_ini", "logT"],
            lnp_outname=lnp_fname,
            surveyname=self.settings.surveyname,
        )

        # check that the stats files are exactly the same
        table_cache = Table.read(self.stats_fname_cache)
        table_new = Table.read(stats_fname)

        compare_tables(table_cache, table_new)

        # lnp files not checked as they are randomly sparsely sampled
        #   hence will be different every time the fitting is run

        # check that the pdf1d/pdf2d files are exactly the same
        compare_fits(self.pdf1d_fname_cache, pdf1d_fname)
        compare_fits(self.pdf2d_fname_cache, pdf2d_fname)
Exemplo n.º 16
0
def test_compare_spec_type_notFOV():
    """
    Test for compare_spec_type.  The spectrally-typed stars aren't real sources,
    they're just invented for the purposes of documenting/testing the code.

    In this version, the stars are NOT in the imaging field of view.
    """

    # download the needed files
    obs_fname = download_rename("b15_4band_det_27_A.fits")
    stats_fname = download_rename("beast_example_phat_stats.fits")

    # run compare_spec_type
    spec_type = compare_spec_type(
        obs_fname,
        stats_fname,
        [1.0],  # RA
        [1.0],  # Dec
        ['B'],  # Spectral type
        [4],  # Subtype
        ['V'],  # Luminosity class
        match_radius=0.2  # Match radius (arcsec)
    )

    # expected output table
    expected_table = Table({
        'spec_ra': [1.0],
        'spec_dec': [1.0],
        'spec_type': ['B 4 V'],
        'spec_teff': [None],
        'spec_logg': [None],
        'phot_cat_ind': [None],
        'stats_cat_ind': [None],
        'beast_teff_p50': [None],
        'beast_teff_p16': [None],
        'beast_teff_p84': [None],
        'beast_logg_p50': [None],
        'beast_logg_p16': [None],
        'beast_logg_p84': [None],
        'teff_sigma': [None],
        'logg_sigma': [None],
    })

    # compare to new table
    compare_tables(expected_table, Table(spec_type))
Exemplo n.º 17
0
def test_compare_spec_type_inFOV():
    """
    Test for compare_spec_type.  The spectrally-typed stars aren't real sources,
    they're just invented for the purposes of documenting/testing the code.

    In this version, the stars are in the imaging field of view.
    """

    # download the needed files
    obs_fname = download_rename("b15_4band_det_27_A.fits")
    stats_fname = download_rename("beast_example_phat_stats.fits")

    # run compare_spec_type
    spec_type = compare_spec_type(
        obs_fname,
        stats_fname,
        [11.2335881, 11.23342557],  # RA
        [41.9001895, 41.90006316],  # Dec
        ['A', 'G'],  # Spectral type
        [2, 7],  # Subtype
        ['II', 'II'],  # Luminosity class
        match_radius=0.2  # Match radius (arcsec)
    )

    # expected output table
    expected_table = Table({
        'spec_ra': [11.2335881, 11.23342557],
        'spec_dec': [41.9001895, 41.90006316],
        'spec_type': ['A 2 II', 'G 7 II'],
        'spec_teff': [9000.0, 4916.666666666667],
        'spec_logg': [2.7164474106543732, 1.7184474106543735],
        'phot_cat_ind': [27, 8],
        'stats_cat_ind': [27, 8],
        'beast_teff_p50': [9046.250020338754, 4528.230977991138],
        'beast_teff_p16': [8643.670633196869, 4335.617282355577],
        'beast_teff_p84': [9536.391362054928, 4729.401710221546],
        'beast_logg_p50': [2.714286917261312, 1.7684285714285717],
        'beast_logg_p16': [2.636272525730954, 1.7014832653061227],
        'beast_logg_p84': [2.799534708811963, 1.8353738775510207],
        'teff_sigma': [-0.11488422362383206, 1.9308757510045778],
        'logg_sigma': [0.025343687546173433, -0.7465969411324851]
    })

    # compare to new table
    compare_tables(expected_table, Table(spec_type), rtol=2e-3)
Exemplo n.º 18
0
def test_pick_models():
    # download the needed files
    vega_fname = download_rename('vega.hd5')
    filename = download_rename('beast_example_phat_seds.grid.hd5')

    filters = ['HST_WFC3_F275W', 'HST_WFC3_F336W', 'HST_ACS_WFC_F475W',
               'HST_ACS_WFC_F814W', 'HST_WFC3_F110W', 'HST_WFC3_F160W']
    mag_cuts = [1.]

    table_out = make_ast_input_list.pick_models(filename, filters, mag_cuts, vega_fname=vega_fname,
                                                outfile='/tmp/test_inputAST.txt', ranseed=1234)

    table_new = Table.read('/tmp/test_inputAST.txt', format='ascii')

    # download cached version of the file and compare it to new file
    cached_table_filename = download_rename('cache_inputAST.txt')
    table_cache = Table.read(cached_table_filename, format='csv', delimiter=' ')
    compare_tables(table_new, table_cache)
Exemplo n.º 19
0
    def test_star_type_probability_no_Av(self):
        """
        Test for star_type_probability.

        In this version, A_V was not saved in the 2D PDFs.
        """

        # download cached file
        star_prob_fname = download_rename(
            f"{self.basename}_star_type_probability.asdf")
        with asdf.open(star_prob_fname) as af:
            star_prob_info = copy.deepcopy(af.tree)

        # edit the 2D PDF file to not have A_V info
        temp_pdf2d_fname = tempfile.NamedTemporaryFile(suffix=".fits").name
        temp_hdu_list = []
        with fits.open(self.pdf2d_fname_cache) as hdu:
            for ext in hdu:
                if "Av+" in ext.name or "+Av" in ext.name:
                    continue
                temp_hdu_list.append(ext)
            fits.HDUList(temp_hdu_list).writeto(temp_pdf2d_fname)

        # edit the expected output to have NaNs in columns that require A_V
        # (currently, that's all columns)
        expected_star_prob = Table(star_prob_info["output"])
        for col in expected_star_prob.colnames:
            if col == "ext_O_star":
                expected_star_prob[col] = np.nan
            if col == "dusty_agb":
                expected_star_prob[col] = np.nan

        # run star_type_probability
        star_prob = star_type_probability.star_type_probability(
            self.pdf1d_fname_cache,
            temp_pdf2d_fname,
            **star_prob_info["input"],
        )

        # compare to expected table
        compare_tables(expected_star_prob, Table(star_prob))
Exemplo n.º 20
0
    def test_compare_spec_type_notFOV(self):
        """
        Test for compare_spec_type.  In this version, the stars are NOT in the
        imaging field of view.
        """
        # run compare_spec_type
        spec_type = compare_spec_type(
            self.obs_fname_cache,
            self.stats_fname_cache,
            [1.0],  # RA
            [1.0],  # Dec
            ["B"],  # Spectral type
            [4],  # Subtype
            ["V"],  # Luminosity class
            match_radius=0.2,  # Match radius (arcsec)
        )

        # expected output table
        expected_table = Table(
            {
                "spec_ra": [1.0],
                "spec_dec": [1.0],
                "spec_type": ["B 4 V"],
                "spec_teff": [None],
                "spec_logg": [None],
                "phot_cat_ind": [None],
                "stats_cat_ind": [None],
                "beast_teff_p50": [None],
                "beast_teff_p16": [None],
                "beast_teff_p84": [None],
                "beast_logg_p50": [None],
                "beast_logg_p16": [None],
                "beast_logg_p84": [None],
                "teff_sigma": [None],
                "logg_sigma": [None],
            }
        )

        # compare to new table
        compare_tables(expected_table, Table(spec_type))
Exemplo n.º 21
0
    def test_compare_spec_type_inFOV(self):
        """
        Test for compare_spec_type.  The spectrally-typed stars aren't real sources,
        they're just invented for the purposes of documenting/testing the code.

        In this version, the stars are in the imaging field of view.
        """
        # run compare_spec_type
        spec_type = compare_spec_type(
            self.obs_fname_cache,
            self.stats_fname_cache,
            [11.2335881, 11.23342557],  # RA
            [41.9001895, 41.90006316],  # Dec
            ["A", "G"],  # Spectral type
            [2, 7],  # Subtype
            ["II", "II"],  # Luminosity class
            match_radius=0.2,  # Match radius (arcsec)
        )

        # expected output table
        expected_table = Table({
            "spec_ra": [11.2335881, 11.23342557],
            "spec_dec": [41.9001895, 41.90006316],
            "spec_type": ["A 2 II", "G 7 II"],
            "spec_teff": [9000.0, 4916.666666666667],
            "spec_logg": [2.7164474106543732, 1.7184474106543735],
            "phot_cat_ind": [27, 8],
            "stats_cat_ind": [27, 8],
            "beast_teff_p50": [9046.250020338754, 4528.230977991138],
            "beast_teff_p16": [8643.670633196869, 4335.617282355577],
            "beast_teff_p84": [9536.391362054928, 4729.401710221546],
            "beast_logg_p50": [2.714286917261312, 1.7684285714285717],
            "beast_logg_p16": [2.636272525730954, 1.7014832653061227],
            "beast_logg_p84": [2.799534708811963, 1.8353738775510207],
            "teff_sigma": [-0.11488422362383206, 1.9308757510045778],
            "logg_sigma": [0.025343687546173433, -0.7465969411324851],
        })

        # compare to new table
        compare_tables(expected_table, Table(spec_type), rtol=2e-3)
Exemplo n.º 22
0
 def test_calc_depth_from_completeness(self):
     """
     Test for calculate_depth.py
     """
     # calculate depth for 50% and 75% completeness
     depth = calc_depth_from_completeness.calc_depth(
         self.seds_fname_cache,
         self.noise_fname_cache,
         completeness_value=[0.5, 0.75],
         vega_mag=True,
     )
     # expected results
     expected_dict = {
         "HST_WFC3_F275W": [25.000309202589012, 24.80610510139205],
         "HST_WFC3_F336W": [24.65974845352875, 24.338061586936263],
         "HST_ACS_WFC_F475W": [np.nan, np.nan],
         "HST_ACS_WFC_F814W": [np.nan, 24.368742437736692],
         "HST_WFC3_F110W": [np.nan, np.nan],
         "HST_WFC3_F160W": [21.99298441116123, 21.504534701422067],
     }
     # compare them
     compare_tables(Table(expected_dict), Table(depth))
Exemplo n.º 23
0
def test_simobs():

    # download the needed files
    vega_fname = download_rename("vega.hd5")
    seds_fname = download_rename("beast_example_phat_seds.grid.hd5")
    noise_fname = download_rename("beast_example_phat_noisemodel.grid.hd5")

    # download cached version of noisemodel on the sed grid
    simobs_fname_cache = download_rename("beast_example_phat_simobs.fits")

    ################

    # get the physics model grid - includes priors
    modelsedgrid = SEDGrid(seds_fname)

    # read in the noise model - includes bias, unc, and completeness
    noisegrid = noisemodel.get_noisemodelcat(noise_fname)

    table_new = gen_SimObs_from_sedgrid(
        modelsedgrid,
        noisegrid,
        nsim=100,
        compl_filter="f475w",
        ranseed=1234,
        vega_fname=vega_fname,
    )

    # check that the simobs files are exactly the same
    table_cache = Table.read(simobs_fname_cache)

    # to avoid issues with uppercase vs lowercase column names, make them all
    # the same before comparing
    for col in table_new.colnames:
        table_new[col].name = col.upper()
    for col in table_cache.colnames:
        table_cache[col].name = col.upper()

    compare_tables(table_cache, table_new)
Exemplo n.º 24
0
def test_sedgrid(cformat, cback, copygrid):
    """
    Tests of the SEDGrid class
    """
    n_bands = 3
    filter_names = ["BAND1", "BAND2", "BAND3"]
    n_models = 100
    lamb = [1.0, 2.0, 3.0]
    seds = np.zeros((n_models, n_bands))
    cov_diag = np.full((n_models, n_bands), 0.1)
    n_offdiag = ((n_bands**2) - n_bands) // 2
    cov_offdiag = np.full((n_models, n_offdiag), 1.0)
    cols = {"Av": [1.0, 1.1, 1.3], "Rv": [2.0, 3.0, 4.0]}
    header = {"Origin": "test_code"}
    gtable = Table(cols)
    gtable.meta = header

    tgrid = SEDGrid(
        lamb,
        seds=seds,
        grid=gtable,
        header=header,
        cov_diag=cov_diag,
        cov_offdiag=cov_offdiag,
        backend="memory",
    )
    tgrid.header["filters"] = " ".join(filter_names)

    # check that the grid has the expected properties
    expected_props = [
        "lamb",
        "seds",
        "cov_diag",
        "cov_offdiag",
        "grid",
        "nbytes",
        "filters",
        "header",
        "keys",
    ]
    for cprop in expected_props:
        assert hasattr(tgrid, cprop), f"missing {cprop} property"

    np.testing.assert_allclose(tgrid.lamb, lamb, err_msg="lambdas not equal")
    np.testing.assert_allclose(tgrid.seds, seds, err_msg="seds not equal")
    np.testing.assert_allclose(tgrid.cov_diag,
                               cov_diag,
                               err_msg="covdiag not equal")
    np.testing.assert_allclose(tgrid.cov_offdiag,
                               cov_offdiag,
                               err_msg="covoffdiag not equal")
    assert isinstance(tgrid.nbytes,
                      (int, np.integer)), "grid nbytes property not integer"
    compare_tables(tgrid.grid, gtable)
    assert tgrid.grid.keys() == list(cols.keys()), "colnames of grid not equal"
    assert tgrid.filters == filter_names, "filters of grid not equal"

    # test writing and reading to disk
    print(f"testing {cformat} file format")
    tfile = NamedTemporaryFile(suffix=cformat)

    # write the file
    tgrid.write(tfile.name)

    # read in the file using different backends
    if (cback == "disk") and (cformat == ".fits"):  # not supported
        return True

    print(f"    testing {cback} backend")
    dgrid_in = SEDGrid(tfile.name, backend=cback)

    # test making a copy
    print(f"    testing copygrid={copygrid}")
    if copygrid:
        dgrid = dgrid_in.copy()
    else:
        dgrid = dgrid_in
    print(dgrid)

    for cprop in expected_props:
        assert hasattr(dgrid, cprop), f"missing {cprop} property"

    # check that the grid has the expected values

    # this test is having a problem in the online travis ci
    # it someone manages to access another file with HST filter names!
    # no idea way.  Works fine offline.
    # assert dgrid.filters == filter_names, "{cformat} file filters not equal"

    assert len(dgrid) == n_bands, f"{cformat} file len not equal"

    np.testing.assert_allclose(
        dgrid.lamb, lamb, err_msg=f"{cformat} file grid lambdas not equal")
    np.testing.assert_allclose(dgrid.seds,
                               seds,
                               err_msg=f"{cformat} file grid seds not equal")
    np.testing.assert_allclose(
        dgrid.cov_diag,
        cov_diag,
        err_msg=f"{cformat} file grid cov_diag not equal",
    )
    np.testing.assert_allclose(
        dgrid.cov_offdiag,
        cov_offdiag,
        err_msg=f"{cformat} file grid cov_offdiag not equal",
    )
    assert isinstance(
        dgrid.nbytes,
        (int, np.integer)), f"{cformat} file grid nbytes property not integer"

    dTable = dgrid.grid
    if (cback == "disk") and (cformat == ".hdf"):
        dTable = read_table_hdf5(dgrid.grid)
    compare_tables(dTable, gtable, otag=f"{cformat} file")

    assert dTable.keys() == list(
        cols.keys()), f"{cformat} file colnames of grid not equal"

    assert dgrid.keys() == tgrid.keys(
    ), f"{cformat} file colnames of grid not equal"

    # final copy - needed for disk backend to get the now defined variables
    print(dgrid)

    dgrid_fin = dgrid.copy()

    print(dgrid_fin)