Esempio n. 1
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",
        )
Esempio n. 2
0
def test_toothpick_noisemodel():

    # download the needed files
    asts_fname = download_rename('fake_stars_b15_27_all.hd5')
    filter_fname = download_rename('filters.hd5')
    vega_fname = download_rename('vega.hd5')
    hst_fname = download_rename('hst_whitedwarf_frac_covar.fits')
    seds_fname = download_rename('beast_example_phat_seds.grid.hd5')

    # download cached version of noisemodel on the sed grid
    noise_fname_cache = download_rename('beast_example_phat_noisemodel.hd5')

    ################
    # get the modesedgrid on which to generate the noisemodel
    modelsedgrid = FileSEDGrid(seds_fname)

    # absflux calibration covariance matrix for HST specific filters (AC)
    filters = [
        'HST_WFC3_F275W', 'HST_WFC3_F336W', 'HST_ACS_WFC_F475W',
        'HST_ACS_WFC_F814W', 'HST_WFC3_F110W', 'HST_WFC3_F160W'
    ]
    absflux_a_matrix = hst_frac_matrix(filters,
                                       hst_fname=hst_fname,
                                       filterLib=filter_fname)

    # generate the AST noise model
    noise_fname = '/tmp/beast_example_phat_noisemodel.hd5'
    noisemodel.make_toothpick_noise_model(noise_fname,
                                          asts_fname,
                                          modelsedgrid,
                                          absflux_a_matrix=absflux_a_matrix,
                                          vega_fname=vega_fname)

    # compare the new to the cached version
    compare_hdf5(noise_fname_cache, noise_fname)
Esempio n. 3
0
    def test_trim_grid(self):
        """
        Generate trim the sed grid and noise model using cached versions of the
        both and compare the result to a cached version.
        """
        # read in the observed data
        obsdata = Observations(self.obs_fname_cache, self.settings.filters,
                               self.settings.obs_colnames)

        # get the modesedgrid
        modelsedgrid = SEDGrid(self.seds_fname_cache)

        # read in the noise model just created
        noisemodel_vals = noisemodel.get_noisemodelcat(self.noise_fname_cache)

        # trim the model sedgrid
        seds_trim_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name
        noise_trim_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name

        trim_models(
            modelsedgrid,
            noisemodel_vals,
            obsdata,
            seds_trim_fname,
            noise_trim_fname,
            sigma_fac=3.0,
        )

        # compare the new to the cached version
        compare_hdf5(self.seds_trim_fname_cache, seds_trim_fname, ctype="seds")
        compare_hdf5(self.noise_trim_fname_cache,
                     noise_trim_fname,
                     ctype="noise")
Esempio n. 4
0
    def test_make_extinguished_sed_grid(self):
        """
        Generate the extinguished SED grid using a cached version of the
        spectral grid with priors and compare the result to a cached version.
        """

        g_pspec = SpectralGrid(self.priors_fname_cache, backend="memory")

        # generate the SED grid by integrating the filter response functions
        #   effect of dust extinction applied before filter integration
        #   also computes the dust priors as weights
        seds_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name
        infoname = tempfile.NamedTemporaryFile(suffix=".asdf").name
        (seds_fname, g) = make_extinguished_sed_grid(
            "test",
            g_pspec,
            self.settings.filters,
            seds_fname=seds_fname,
            extLaw=self.settings.extLaw,
            av=self.settings.avs,
            rv=self.settings.rvs,
            fA=self.settings.fAs,
            rv_prior_model=self.settings.rv_prior_model,
            av_prior_model=self.settings.av_prior_model,
            fA_prior_model=self.settings.fA_prior_model,
            add_spectral_properties_kwargs=self.settings.
            add_spectral_properties_kwargs,
            info_fname=infoname,
        )

        # compare the new to the cached version
        compare_hdf5(self.seds_fname_cache, seds_fname)
Esempio n. 5
0
    def test_make_kurucz_tlusty_spectral_grid(self):
        """
        Generate the spectral grid based on Kurucz and Tlusty stellar atmosphere
        models based on a cached set of isochrones and compare the result to a cached
        version.
        """
        # read in the cached isochrones
        oiso = ezIsoch(self.iso_fname_cache)

        # calculate the redshift
        redshift = (self.settings.velocity / const.c).decompose().value

        # make the spectral grid
        spec_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name
        (spec_fname, g) = make_spectral_grid(
            "test",
            oiso,
            osl=self.settings.osl,
            redshift=redshift,
            distance=self.settings.distances,
            distance_unit=self.settings.distance_unit,
            spec_fname=spec_fname,
            # filterLib=filter_fname,
            extLaw=self.settings.extLaw,
            add_spectral_properties_kwargs=self.settings.
            add_spectral_properties_kwargs,
        )

        # compare the new to the cached version
        compare_hdf5(self.spec_fname_cache, spec_fname)
Esempio n. 6
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"
Esempio n. 7
0
def test_make_kurucz_tlusty_spectral_grid():

    # download the needed files
    kurucz_fname = download_rename("kurucz2004.grid.fits")
    tlusty_fname = download_rename("tlusty.lowres.grid.fits")
    filter_fname = download_rename("filters.hd5")
    iso_fname = download_rename("beast_example_phat_iso.csv")

    # download cached version of spectral grid
    spec_fname_cache = download_rename("beast_example_phat_spec_grid.hd5")

    ################
    # generate the same spectral grid from the code

    # read in the cached isochrones
    oiso = ezIsoch(iso_fname)

    # define the distance
    distances = [24.47]
    distance_unit = units.mag

    velocity = -300 * units.km / units.s
    redshift = (velocity / const.c).decompose().value

    # define the spectral libraries to use
    osl = stellib.Tlusty(filename=tlusty_fname) + stellib.Kurucz(
        filename=kurucz_fname)

    # define the extinction curve to use
    extLaw = extinction.Gordon16_RvFALaw()

    filters = [
        "HST_WFC3_F275W",
        "HST_WFC3_F336W",
        "HST_ACS_WFC_F475W",
        "HST_ACS_WFC_F814W",
        "HST_WFC3_F110W",
        "HST_WFC3_F160W",
    ]
    add_spectral_properties_kwargs = dict(filternames=filters)

    spec_fname = "/tmp/beast_example_phat_spec_grid.hd5"
    spec_fname, g = make_spectral_grid(
        "test",
        oiso,
        osl=osl,
        redshift=redshift,
        distance=distances,
        distance_unit=distance_unit,
        spec_fname=spec_fname,
        filterLib=filter_fname,
        extLaw=extLaw,
        add_spectral_properties_kwargs=add_spectral_properties_kwargs,
    )

    # compare the new to the cached version
    compare_hdf5(spec_fname_cache, spec_fname)
Esempio n. 8
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.grid.hd5")
    obs_fname = download_rename("b15_4band_det_27_A.fits")

    # download cached version of noisemodel on the sed grid
    noise_trim_fname_cache = download_rename(
        "beast_example_phat_noisemodel_trim.grid.hd5")
    seds_trim_fname_cache = download_rename(
        "beast_example_phat_seds_trim.grid.hd5")

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

    # 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 = Observations(obs_fname,
                           filters,
                           obs_colnames,
                           vega_fname=vega_fname)

    # get the modesedgrid
    modelsedgrid = SEDGrid(seds_fname)

    # read in the noise model just created
    noisemodel_vals = noisemodel.get_noisemodelcat(noise_fname)

    # trim the model sedgrid
    seds_trim_fname = "beast_example_phat_seds_trim.grid.hd5"
    noise_trim_fname = seds_trim_fname.replace("_seds", "_noisemodel")

    trim_models(
        modelsedgrid,
        noisemodel_vals,
        obsdata,
        seds_trim_fname,
        noise_trim_fname,
        sigma_fac=3.0,
    )

    # compare the new to the cached version
    compare_hdf5(seds_trim_fname_cache, seds_trim_fname, ctype="seds")
    compare_hdf5(noise_trim_fname_cache, noise_trim_fname, ctype="noise")
Esempio n. 9
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')
    obs_fname = download_rename('b15_4band_det_27_A.fits')

    # download cached version of noisemodel on the sed grid
    noise_trim_fname_cache = download_rename(
        'beast_example_phat_noisemodel_trim.grid.hd5')
    seds_trim_fname_cache = download_rename(
        'beast_example_phat_seds_trim.grid.hd5')

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

    # 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)

    # get the modesedgrid
    modelsedgrid = FileSEDGrid(seds_fname)

    # read in the noise model just created
    noisemodel_vals = noisemodel.get_noisemodelcat(noise_fname)

    # trim the model sedgrid
    seds_trim_fname = 'beast_example_phat_sed_trim.grid.hd5'
    noise_trim_fname = seds_trim_fname.replace('_sed', '_noisemodel')

    trim_models(modelsedgrid,
                noisemodel_vals,
                obsdata,
                seds_trim_fname,
                noise_trim_fname,
                sigma_fac=3.)

    # compare the new to the cached version
    compare_hdf5(seds_trim_fname_cache, seds_trim_fname, ctype='seds')
    compare_hdf5(noise_trim_fname_cache, noise_trim_fname, ctype='noise')
Esempio n. 10
0
def test_make_kurucz_tlusty_spectral_grid():

    # download the needed files
    kurucz_fname = download_rename('kurucz2004.grid.fits')
    tlusty_fname = download_rename('tlusty.lowres.grid.fits')
    filter_fname = download_rename('filters.hd5')
    iso_fname = download_rename('beast_example_phat_iso.csv')

    # download cached version of spectral grid
    spec_fname_cache = download_rename('beast_example_phat_spec_grid.hd5')

    ################
    # generate the same spectral grid from the code

    # read in the cached isochrones
    oiso = ezIsoch(iso_fname)

    # define the distance
    distances = [24.47]
    distance_unit = units.mag

    velocity = -300 * units.km / units.s
    redshift = (velocity / const.c).decompose().value

    # define the spectral libraries to use
    osl = stellib.Tlusty(filename=tlusty_fname) \
        + stellib.Kurucz(filename=kurucz_fname)

    filters = [
        'HST_WFC3_F275W', 'HST_WFC3_F336W', 'HST_ACS_WFC_F475W',
        'HST_ACS_WFC_F814W', 'HST_WFC3_F110W', 'HST_WFC3_F160W'
    ]
    add_spectral_properties_kwargs = dict(filternames=filters)

    spec_fname = '/tmp/beast_example_phat_spec_grid.hd5'
    spec_fname, g = make_spectral_grid(
        'test',
        oiso,
        osl=osl,
        redshift=redshift,
        distance=distances,
        distance_unit=distance_unit,
        spec_fname=spec_fname,
        filterLib=filter_fname,
        add_spectral_properties_kwargs=add_spectral_properties_kwargs)

    # compare the new to the cached version
    compare_hdf5(spec_fname_cache, spec_fname)
Esempio n. 11
0
    def test_create_obsmodel_no_subgrid(self):
        """
        Test create_obsmodel.py, assuming no subgrids
        """
        print("running test_create_obsmodel_no_subgrid")

        # run create_obsmodel
        create_obsmodel.create_obsmodel(
            self.settings, use_sd=False, nsubs=self.settings.n_subgrid, nprocs=1,
        )

        # check that files match
        compare_hdf5(
            self.noise_fname_cache,
            "beast_metal_small/beast_metal_small_noisemodel.grid.hd5",
        )
Esempio n. 12
0
def test_make_extinguished_sed_grid():

    # download the needed files
    priors_fname = download_rename("beast_example_phat_spec_w_priors.grid.hd5")
    filter_fname = download_rename("filters.hd5")

    # download cached version of sed grid
    seds_fname_cache = download_rename("beast_example_phat_seds.grid.hd5")

    ################
    # generate the same extinguished SED grid from the code

    # Add in the filters
    filters = [
        "HST_WFC3_F275W",
        "HST_WFC3_F336W",
        "HST_ACS_WFC_F475W",
        "HST_ACS_WFC_F814W",
        "HST_WFC3_F110W",
        "HST_WFC3_F160W",
    ]
    add_spectral_properties_kwargs = dict(filternames=filters)

    g_pspec = grid.FileSpectralGrid(priors_fname, backend="memory")

    # generate the SED grid by integrating the filter response functions
    #   effect of dust extinction applied before filter integration
    #   also computes the dust priors as weights
    seds_fname = "/tmp/beast_example_phat_sed.grid.hd5"
    seds_fname, g_seds = make_extinguished_sed_grid(
        "test",
        g_pspec,
        filters,
        seds_fname=seds_fname,
        filterLib=filter_fname,
        extLaw=extinction.Gordon16_RvFALaw(),
        av=[0.0, 10.055, 1.0],
        rv=[2.0, 6.0, 1.0],
        fA=[0.0, 1.0, 0.25],
        av_prior_model={"name": "flat"},
        rv_prior_model={"name": "flat"},
        fA_prior_model={"name": "flat"},
        add_spectral_properties_kwargs=add_spectral_properties_kwargs,
    )

    # compare the new to the cached version
    compare_hdf5(seds_fname_cache, seds_fname)
Esempio n. 13
0
def test_add_stellar_priors_to_spectral_grid():

    # download the needed files
    gspec_fname = download_rename("beast_example_phat_spec_grid.hd5")

    # download cached version of spectral grid with priors
    priors_fname_cache = download_rename("beast_example_phat_spec_w_priors.grid.hd5")

    ###############
    # generate the spectral grid with stellar priors from the code

    gspec_fname = "/tmp/beast_example_phat_spec_grid.hd5"
    specgrid = grid.FileSpectralGrid(gspec_fname, backend="memory")

    priors_fname = "/tmp/beast_example_phat_spec_w_priors.grid.hd5"
    priors_fname, g = add_stellar_priors("test", specgrid, priors_fname=priors_fname)

    # compare the new to the cached version
    compare_hdf5(priors_fname_cache, priors_fname)
Esempio n. 14
0
    def test_toothpick_noisemodel(self):
        """
        Generate the nosiemodel (aka observationmodel) using a cached version of
        the artifical star test results (ASTs) and compare the result to a cached
        version.
        """

        # get the modelsedgrid on which to generate the noisemodel
        modelsedgrid = SEDGrid(self.seds_fname_cache)

        # generate the AST noise model
        noise_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name
        noisemodel.make_toothpick_noise_model(
            noise_fname,
            self.asts_fname_cache,
            modelsedgrid,
            absflux_a_matrix=self.settings.absflux_a_matrix,
        )

        # compare the new to the cached version
        compare_hdf5(self.noise_fname_cache, noise_fname)
Esempio n. 15
0
    def test_add_stellar_priors_to_spectral_grid(self):
        """
        Add the stellar priors to the a cached spectral grid and compare
        it to the cached version.
        """
        specgrid = SpectralGrid(self.spec_fname_cache, backend="memory")

        priors_fname = tempfile.NamedTemporaryFile(suffix=".hd5").name
        infoname = tempfile.NamedTemporaryFile(suffix=".asdf").name
        priors_fname, g = add_stellar_priors(
            "test",
            specgrid,
            priors_fname=priors_fname,
            age_prior_model=self.settings.age_prior_model,
            mass_prior_model=self.settings.mass_prior_model,
            met_prior_model=self.settings.met_prior_model,
            distance_prior_model=self.settings.distance_prior_model,
            info_fname=infoname,
        )

        # compare the new to the cached version
        compare_hdf5(self.priors_fname_cache, priors_fname)
Esempio n. 16
0
    def test_create_obsmodel_with_subgrid(self):
        """
        Test create_obsmodel.py, assuming two subgrids
        """
        print("running test_create_obsmodel_with_subgrid")

        # run create_obsmodel
        create_obsmodel.create_obsmodel(
            self.settings_sg,
            use_sd=False,
            nsubs=self.settings_sg.n_subgrid,
            nprocs=1,
            use_rate=False,
        )

        # check that files match
        compare_hdf5(
            self.noise_sub0_fname_cache,
            "beast_example_phat_subgrids/beast_example_phat_subgrids_noisemodel.gridsub0.hd5",
        )
        compare_hdf5(
            self.noise_sub1_fname_cache,
            "beast_example_phat_subgrids/beast_example_phat_subgrids_noisemodel.gridsub1.hd5",
        )