Пример #1
0
def sed_from_galacticus_mags(galacticus_mags, redshift, h=0.71, omega_m=0.265):
    """
    galacticus_mags is a numpy array such that
    galacticus_mags[i][j] is the magnitude of the jth star in the ith bandpass,
    where the bandpasses are ordered in ascending order of minimum wavelength.

    Will return a numpy array of SED names and a numpy array of magNorms.
    """

    if not _LSST_IS_AVAILABLE:
        raise RuntimeError("You cannot use sed_from_galacticus_mags\n"
                           "You do not have *lsst* installed and setup")

    if not hasattr(sed_from_galacticus_mags, '_sed_color_tree'):
        catsim_dir \
            = os.path.join(getPackageDir('sims_GCRCatSimInterface'), 'data')
        color_grid_file = os.path.join(catsim_dir, 'CatSimMagGrid.txt')

        if not os.path.exists(color_grid_file):
            msg = '\n%s does not exist\n' % color_grid_file
            msg += 'Go into the directory %s ' % catsim_dir
            msg += 'and run the script get_sed_mags.py'
            raise RuntimeError(msg)

        dtype_list = [('name', str, 200)]
        for ii in range(30):
            dtype_list.append(('mag%d' % ii, float))
        dtype_list.append(('magNorm', float))
        dtype = np.dtype(dtype_list)
        sed_data = np.genfromtxt(color_grid_file, dtype=dtype)
        sed_colors = np.array([sed_data['mag%d' % (ii+1)] - sed_data['mag%d' % ii]
                               for ii in range(29)])
        sed_from_galacticus_mags._sed_colors = sed_colors.transpose()
        sed_from_galacticus_mags._sed_names = sed_data['name']
        sed_from_galacticus_mags._mag_norm = sed_data['magNorm']
        sed_from_galacticus_mags._sed_mags = np.array([sed_data['mag%d' % ii]
                                                       for ii in range(30)]).transpose()

    cosmology = CosmologyObject(H0=100.0*h, Om0=omega_m)
    distance_modulus = cosmology.distanceModulus(redshift=redshift)
    assert len(distance_modulus) == len(galacticus_mags[0])

    galacticus_colors = np.array([galacticus_mags[ii+1]-galacticus_mags[ii]
                                  for ii in range(29)]).transpose()

    mag_dex = np.zeros(len(galacticus_colors), dtype=int)
    for i_star in range(len(galacticus_colors)):
        dd = np.sum((galacticus_colors[i_star]
                     -sed_from_galacticus_mags._sed_colors)**2, axis=1)
        mag_dex[i_star] = np.argmin(dd)

    output_names = sed_from_galacticus_mags._sed_names[mag_dex]

    chosen_mags = sed_from_galacticus_mags._sed_mags[mag_dex]
    galacticus_mags_t = galacticus_mags.transpose()
    d_mag = (galacticus_mags_t - chosen_mags).sum(axis=1)/30.0
    output_mag_norm = sed_from_galacticus_mags._mag_norm[mag_dex] + d_mag + distance_modulus
    assert len(output_mag_norm) == len(output_names)

    return output_names, output_mag_norm
Пример #2
0
class CosmologyMixin(object):
    """
    This class is designed to operate as a mixin for InstanceCatalog classes.
    It provides a member variable self.cosmology which is an instantiation
    of the CosmologyObject class.  self.cosmology defaults to the
    Milliennium Simulation cosmology.  This mixin also provides a method
    self.setCosmology() which will allow the user to customize self.cosmology
    and a getter for the column cosmologicalDistanceModulus that reflects the
    effect of the luminosity distance on a galaxy's component magnitudes.

    NOTE: one should only include this mixin in catalogs whose magNorm is
    normalized to an absolute magnitude of some sort (i.e. the magnitude if
    the galaxy was at redshift=0).  The magNorms for galaxies stored on the
    University of Washington LSST database do not fit this criterion.
    magNorms on the University of Washington LSST database include the
    effects of cosmological distance modulus.
    """

    cosmology = CosmologyObject()

    def setCosmology(self, H0=73.0, Om0=0.25, Ok0=None, w0=None, wa=None):
        """
        This method customizes the member variable self.cosmology by re-instantiating
        the CosmologyObject class

        param [in] H0 is the Hubble parameter today in km/s/Mpc

        param [in] Om0 is the density paramter (fraction of critical) associated with matter today

        param [in] Ode0 is the density paratmer associated with dark energy today

        param [in] w0 is the w0 parameter associated with the equation of state of dark energy
        w = w0 + wa z/(1+z)

        param [in] wa is the wa parameter usesd to set the equation of state of dark energy
        w = w0 + wa z/(1+z)
        """
        self.cosmology = CosmologyObject(H0=H0, Om0=Om0, Ok0=Ok0, w0=w0, wa=wa)

    @cached
    def get_cosmologicalDistanceModulus(self):
        """
        getter for cosmologicalDistanceModulus (the effect of the luminosity
        distance on a galaxy's component magnitudes)
        """
        redshift = self.column_by_name("redshift")

        if len(redshift) == 0:
            #newer versions of astropy do not appreciate being passed an
            #empty numpy array of redshifts; avoid nasty exceptions by
            #just returning an empty numpy array if we got an empty numpy array
            return numpy.array([])

        return self.cosmology.distanceModulus(redshift)
Пример #3
0
class CosmologyMixin(object):
    """
    This class is designed to operate as a mixin for InstanceCatalog classes.
    It provides a member variable self.cosmology which is an instantiation
    of the CosmologyObject class.  self.cosmology defaults to the
    Milliennium Simulation cosmology.  This mixin also provides a method
    self.setCosmology() which will allow the user to customize self.cosmology
    and a getter for the column cosmologicalDistanceModulus that reflects the
    effect of the luminosity distance on a galaxy's component magnitudes.

    NOTE: one should only include this mixin in catalogs whose magNorm is
    normalized to an absolute magnitude of some sort (i.e. the magnitude if
    the galaxy was at redshift=0).  The magNorms for galaxies stored on the
    University of Washington LSSTCATSIM database do not fit this criterion.
    magNorms on the University of Washington LSSTCATSIM database include the
    effects of cosmological distance modulus.
    """

    cosmology = CosmologyObject()

    def setCosmology(self, H0=73.0, Om0=0.25, Ok0=None, w0=None, wa=None):
        """
        This method customizes the member variable self.cosmology by re-instantiating
        the CosmologyObject class

        param [in] H0 is the Hubble parameter today in km/s/Mpc

        param [in] Om0 is the density paramter (fraction of critical) associated with matter today

        param [in] Ode0 is the density paratmer associated with dark energy today

        param [in] w0 is the w0 parameter associated with the equation of state of dark energy
        w = w0 + wa z/(1+z)

        param [in] wa is the wa parameter usesd to set the equation of state of dark energy
        w = w0 + wa z/(1+z)
        """
        self.cosmology = CosmologyObject(H0=H0, Om0=Om0, Ok0=Ok0, w0=w0, wa=wa)

    @cached
    def get_cosmologicalDistanceModulus(self):
        """
        getter for cosmologicalDistanceModulus (the effect of the luminosity
        distance on a galaxy's component magnitudes)
        """
        redshift = self.column_by_name("redshift")

        if len(redshift) == 0:
            #newer versions of astropy do not appreciate being passed an
            #empty numpy array of redshifts; avoid nasty exceptions by
            #just returning an empty numpy array if we got an empty numpy array
            return numpy.array([])

        return self.cosmology.distanceModulus(redshift)
Пример #4
0
    def testDistanceModulus(self):
        """
        Test the calculation of the distance modulus out to a certain redshift
        """
        H0 = 73.0

        universe = CosmologyObject()
        for Om0 in np.arange(start=0.15, stop=0.56, step=0.2):
            for Ok0 in np.arange(start=-0.1, stop=0.11, step=0.2):
                for w0 in np.arange(start=-1.1, stop=-0.85, step=0.2):
                    for wa in np.arange(start=-0.1, stop=0.11, step=0.2):

                        universe = CosmologyObject(H0=H0,
                                                   Om0=Om0,
                                                   Ok0=Ok0,
                                                   w0=w0,
                                                   wa=wa)

                        sqrtkCurvature = \
                        np.sqrt(np.abs(universe.OmegaCurvature()))*universe.H()/self.speedOfLight

                        Og0 = universe.OmegaPhotons()
                        Onu0 = universe.OmegaNeutrinos()
                        Ode0 = universe.OmegaDarkEnergy()

                        for zz in np.arange(start=0.1, stop=4.2, step=2.0):
                            modulusControl = universe.distanceModulus(
                                redshift=zz)

                            comovingDistance = \
                            self.speedOfLight*scipy.integrate.quad(comovingDistanceIntegrand, 0.0, zz,
                                                                   args=(H0, Om0, Ode0, Og0,
                                                                         Onu0, w0, wa))[0]

                            if universe.OmegaCurvature() < 0.0:
                                nn = sqrtkCurvature * comovingDistance
                                nn = np.sin(nn)
                                luminosityDistance = (1.0 +
                                                      zz) * nn / sqrtkCurvature
                            elif universe.OmegaCurvature() > 0.0:
                                nn = sqrtkCurvature * comovingDistance
                                nn = np.sinh(nn)
                                luminosityDistance = (1.0 +
                                                      zz) * nn / sqrtkCurvature
                            else:
                                luminosityDistance = (1.0 +
                                                      zz) * comovingDistance

                            modulusTest = 5.0 * np.log10(
                                luminosityDistance) + 25.0
                            self.assertAlmostEqual(
                                modulusControl / modulusTest, 1.0, 4)
Пример #5
0
 def testDistanceModulusAtZero(self):
     """
     Test to make sure that the distance modulus is set to zero if the distance modulus method
     returns a negative number
     """
     universe = CosmologyObject()
     ztest = [0.0, 1.0, 2.0, 0.0, 3.0]
     mm = universe.distanceModulus(redshift=ztest)
     self.assertEqual(mm[0], 0.0)
     self.assertEqual(mm[3], 0.0)
     self.assertEqual(mm[1], 5.0*np.log10(universe.luminosityDistance(ztest[1])) + 25.0)
     self.assertEqual(mm[2], 5.0*np.log10(universe.luminosityDistance(ztest[2])) + 25.0)
     self.assertEqual(mm[4], 5.0*np.log10(universe.luminosityDistance(ztest[4])) + 25.0)
def get_m_i(abs_mag_i, redshift):
    """
    Take numpy arrays of absolute i-band magnitude and
    cosmological redshift.  Return a numpy array of
    observed i-band magnitudes
    """
    z_grid, k_grid = create_k_corr_grid()
    k_corr = np.interp(redshift, z_grid, k_grid)

    dc2_cosmo = CosmologyObject(H0=71.0, Om0=0.265)
    distance_modulus = dc2_cosmo.distanceModulus(redshift=redshift)
    obs_mag_i = abs_mag_i + distance_modulus + k_corr
    return obs_mag_i
Пример #7
0
def get_m_i(abs_mag_i, redshift):
    """
    (Edited from original because of error)
    Take numpy arrays of absolute i-band magnitude and
    cosmological redshift.  
    
    Returns
    -------
    array-like
        observed i-band magnitudes
    """
    z_grid, k_grid = create_k_corr_grid(redshift)
    k_corr = np.interp(redshift, z_grid, k_grid)

    dc2_cosmo = CosmologyObject(H0=71.0, Om0=0.265)
    distance_modulus = dc2_cosmo.distanceModulus(redshift=redshift)
    obs_mag_i = abs_mag_i + distance_modulus + k_corr
    return obs_mag_i
Пример #8
0
 def testDistanceModulusAtZero(self):
     """
     Test to make sure that the distance modulus is set to zero if the distance modulus method
     returns a negative number
     """
     universe = CosmologyObject()
     ztest = [0.0, 1.0, 2.0, 0.0, 3.0]
     mm = universe.distanceModulus(redshift=ztest)
     self.assertEqual(mm[0], 0.0)
     self.assertEqual(mm[3], 0.0)
     self.assertEqual(
         mm[1],
         5.0 * np.log10(universe.luminosityDistance(ztest[1])) + 25.0)
     self.assertEqual(
         mm[2],
         5.0 * np.log10(universe.luminosityDistance(ztest[2])) + 25.0)
     self.assertEqual(
         mm[4],
         5.0 * np.log10(universe.luminosityDistance(ztest[4])) + 25.0)
    def testCatalogDistanceModulus(self):
        """
        Does cosmologicalDistanceModulus get properly applied
        """
        dbObj = myTestGals(database=self.dbName)
        cosmoCat = cosmologicalGalaxyCatalog(dbObj)
        controlCat = absoluteGalaxyCatalog(dbObj)
        cosmoIter = cosmoCat.iter_catalog(chunk_size=self.dbSize)
        controlIter = controlCat.iter_catalog(chunk_size=self.dbSize)

        cosmology = CosmologyObject()

        for (cosmoRow, controlRow) in zip(cosmoIter, controlIter):
            modulus = cosmology.distanceModulus(controlRow[25])
            self.assertEqual(cosmoRow[0], controlRow[0])
            self.assertEqual(cosmoRow[25], controlRow[25])
            self.assertEqual(cosmoRow[26], modulus)
            for i in range(1,25):
                self.assertAlmostEqual(cosmoRow[i], controlRow[i] + modulus, 6)
Пример #10
0
    def testCatalogDistanceModulus(self):
        """
        Does cosmologicalDistanceModulus get properly applied
        """
        dbObj = myTestGals(database=self.dbName)
        cosmoCat = cosmologicalGalaxyCatalog(dbObj, obs_metadata=self.obs)
        controlCat = absoluteGalaxyCatalog(dbObj, obs_metadata=self.obs)
        cosmoIter = cosmoCat.iter_catalog(chunk_size=self.dbSize)
        controlIter = controlCat.iter_catalog(chunk_size=self.dbSize)

        cosmology = CosmologyObject()

        for (cosmoRow, controlRow) in zip(cosmoIter, controlIter):
            modulus = cosmology.distanceModulus(controlRow[25])
            self.assertEqual(cosmoRow[0], controlRow[0])
            self.assertEqual(cosmoRow[25], controlRow[25])
            self.assertEqual(cosmoRow[26], modulus)
            for i in range(1, 25):
                self.assertAlmostEqual(cosmoRow[i], controlRow[i] + modulus, 6)
Пример #11
0
    def testDistanceModulus(self):
        """
        Test the calculation of the distance modulus out to a certain redshift
        """
        H0 = 73.0

        universe = CosmologyObject()
        for Om0 in np.arange(start=0.15, stop=0.56, step=0.2):
            for Ok0 in np.arange(start=-0.1, stop=0.11, step=0.2):
                for w0 in np.arange(start=-1.1, stop=-0.85, step=0.2):
                    for wa in np.arange(start=-0.1, stop=0.11, step=0.2):

                        universe = CosmologyObject(H0=H0, Om0=Om0, Ok0=Ok0, w0=w0, wa=wa)

                        sqrtkCurvature = \
                        np.sqrt(np.abs(universe.OmegaCurvature()))*universe.H()/self.speedOfLight

                        Og0 = universe.OmegaPhotons()
                        Onu0 = universe.OmegaNeutrinos()
                        Ode0 = universe.OmegaDarkEnergy()

                        for zz in np.arange(start=0.1, stop=4.2, step=2.0):
                            modulusControl = universe.distanceModulus(redshift=zz)

                            comovingDistance = \
                            self.speedOfLight*scipy.integrate.quad(comovingDistanceIntegrand, 0.0, zz,
                                                                   args=(H0, Om0, Ode0, Og0,
                                                                         Onu0, w0, wa))[0]

                            if universe.OmegaCurvature() < 0.0:
                                nn = sqrtkCurvature*comovingDistance
                                nn = np.sin(nn)
                                luminosityDistance = (1.0+zz)*nn/sqrtkCurvature
                            elif universe.OmegaCurvature() > 0.0:
                                nn = sqrtkCurvature*comovingDistance
                                nn = np.sinh(nn)
                                luminosityDistance = (1.0+zz)*nn/sqrtkCurvature
                            else:
                                luminosityDistance = (1.0+zz)*comovingDistance

                            modulusTest = 5.0*np.log10(luminosityDistance) + 25.0
                            self.assertAlmostEqual(modulusControl/modulusTest, 1.0, 4)
Пример #12
0
def test_sne(grid_name, x1_vals, c0_vals,
             z_vals, abs_mag_vals, t_vals,
             dict_key, out_dict):

    bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
    cosmo = CosmologyObject()

    n_samples = len(z_vals)

    mag_truth = np.zeros((6,n_samples), dtype=float)
    mag_interp = np.zeros((6,n_samples), dtype=float)

    mag_grid_dict = {}
    with h5py.File(grid_name, 'r') as in_file:
        param_mins = in_file['param_mins'].value
        d_params = in_file['d_params'].value
        t_grid = in_file['t_grid'].value
        for name in in_file.keys():
            if name == 'param_mins':
                continue
            if name == 'd_params':
                continue
            if name == 't_grid':
                continue
            mag_grid_dict[name] = in_file[name].value

    t_start = time.time()
    for ii in range(n_samples):
        x1 = x1_vals[ii]
        c0 = c0_vals[ii]
        z = z_vals[ii]
        abs_mag = abs_mag_vals[ii]
        t = t_vals[ii]

        sn = sncosmo.Model(source='salt2-extended')
        sn.set(x1=x1,c=c0,z=z)
        sn.source.set_peakmag(abs_mag+cosmo.distanceModulus(z),
                              band='bessellb', magsys='ab')

        flambda = 10.0*sn.flux(time=t, wave=10.0*bp_dict['g'].wavelen)
        ss = Sed(flambda=flambda, wavelen=bp_dict['g'].wavelen)
        mag_truth[:,ii] = bp_dict.magListForSed(ss)

        i_x1 = np.round((x1-param_mins[0])/d_params[0]).astype(int)
        i_c0 = np.round((c0-param_mins[1])/d_params[1]).astype(int)
        i_z = np.round((z-param_mins[2])/d_params[2]).astype(int)
        d_mag = abs_mag-param_mins[3]

        tag = i_x1+i_c0*100+i_z*10000
        mag_grid = mag_grid_dict['%d' % tag]
        interp_mags = np.zeros(6, dtype=float)
        for i_bp in range(6):
            mm = np.interp(t, t_grid, mag_grid[i_bp])+d_mag
            mag_interp[i_bp,ii] = mm


        if ii>0 and ii%100 == 0:
            duration = (time.time()-t_start)/3600.0
            pred = n_samples*duration/ii
            print('%d in %e hrs; predict %e' % (ii,duration,pred))

    out_dict[dict_key] = (mag_interp, mag_truth)
    query = 'SELECT magnorm_agn, redshift, varParamStr FROM '
    query += 'galaxy WHERE varParamStr IS NOT NULL '
    query += 'AND dec BETWEEN -2.5 AND 2.5 '
    query += 'AND (ra<2.5 OR ra>357.5)'

    dtype = np.dtype([('magnorm', float), ('redshift', float),
                      ('varParamStr', str, 400)])

    data_iter = db.get_arbitrary_chunk_iterator(query,
                                                dtype=dtype,
                                                chunk_size=10000)

    with open('data/dc1_agn_params.txt', 'w') as out_file:
        out_file.write('# z m_i M_i tau sfu sfg sfr sfi sfz sfy\n')
        for chunk in data_iter:
            DM = cosmo.distanceModulus(redshift=chunk['redshift'])
            k_corr = np.interp(chunk['redshift'], z_grid, k_grid)

            for i_row, agn in enumerate(chunk):
                ss = Sed(wavelen=base_sed.wavelen, flambda=base_sed.flambda)
                fnorm = getImsimFluxNorm(ss, agn['magnorm'])
                ss.multiplyFluxNorm(fnorm)
                ss.redshiftSED(agn['redshift'], dimming=True)
                mag = ss.calcMag(bp)
                abs_m_i = mag - DM[i_row] - k_corr[i_row]
                params = json.loads(agn['varParamStr'])['pars']
                out_file.write(
                    '%e %e %e %e %e %e %e %e %e %e\n' %
                    (agn['redshift'], mag, abs_m_i, params['agn_tau'],
                     params['agn_sfu'], params['agn_sfg'], params['agn_sfr'],
                     params['agn_sfi'], params['agn_sfz'], params['agn_sfy']))
    valid = np.where(np.logical_and(dc2_data['bhmass']!=0.0,
                                    dc2_data['accretion_rate']!=0.0))

    dc2_data = dc2_data[valid]

    mass_cut = np.where(dc2_data['bhmass']>=10.0**7)
    dc2_data = dc2_data[mass_cut]

    dc2_log_edd_rat = log_Eddington_ratio(dc2_data['bhmass'],
                                          dc2_data['accretion_rate'])

    dc2_abs_mag_i = M_i_from_L_Mass(dc2_log_edd_rat, np.log10(dc2_data['bhmass']))

    dc2_cosmo = CosmologyObject(H0=71.0, Om0=0.265)

    DM = dc2_cosmo.distanceModulus(redshift=dc2_data['redshift'])

    bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
    bp_i = bp_dict['i']
    sed_dir = os.path.join(getPackageDir('sims_sed_library'),
                           'agnSED')
    sed_name = os.path.join(sed_dir, 'agn.spec.gz')
    if not os.path.exists(sed_name):
        raise RuntimeError('\n\n%s\n\nndoes not exist\n\n' % sed_name)
    base_sed = Sed()
    base_sed.readSED_flambda(sed_name)
    z_grid = np.arange(0.0, dc2_data['redshift'].max(), 0.01)
    k_grid = np.zeros(len(z_grid),dtype=float)

    for i_z, zz in enumerate(z_grid):
        ss = Sed(flambda=base_sed.flambda, wavelen=base_sed.wavelen)
Пример #15
0
true_redshift_list =  catalog_qties['redshift_true'][first_disk]
full_redshift_list = catalog_qties['redshift'][first_disk]

# hack to use the redshift at the snapshot of the galaxy
true_redshift_list = []
for step in catalog_qties['step'][first_disk]:
    true_redshift_list.append(step_to_z[step])
true_redshift_list = np.array(true_redshift_list)
full_redshift_list = true_redshift_list


from lsst.sims.photUtils import CosmologyObject
cosmo = CosmologyObject(H0=71.0, Om0=0.265)

dm = cosmo.distanceModulus(true_redshift_list)

fudge = 2.5*np.log10(1.0+true_redshift_list)

u_control = -2.5*np.log10(u_control) + dm - fudge
g_control = -2.5*np.log10(g_control) + dm - fudge
r_control = -2.5*np.log10(r_control) + dm - fudge
i_control = -2.5*np.log10(i_control) + dm - fudge
z_control = -2.5*np.log10(z_control) + dm - fudge
y_control = -2.5*np.log10(y_control) + dm - fudge

u_dustless = -2.5*np.log10(u_dustless) + dm - fudge
g_dustless = -2.5*np.log10(g_dustless) + dm - fudge
r_dustless = -2.5*np.log10(r_dustless) + dm - fudge
i_dustless = -2.5*np.log10(i_dustless) + dm - fudge
z_dustless = -2.5*np.log10(z_dustless) + dm - fudge
Пример #16
0
t_start = time.time()
models_generated = 0
tags_used = set()
max_tag = 2**32
with h5py.File('data/sne_interp_models.h5', 'w') as out_file:
    out_file.create_dataset('t_grid', data=t_grid)
    out_file.create_dataset('param_mins',
                            data=np.array([x1_min, c0_min, z_min, abs_mag]))
    out_file.create_dataset('d_params',
                            data=np.array([d_x1, d_c0, d_z]))
    out_file.create_dataset('param_names',
            data=np.array(['x1','c0', 'z', 'abs_mag']).astype(np.string_))

    for z in z_grid:
        i_z = np.round((z-z_min)/d_z).astype(int)
        dm = cosmo.distanceModulus(z)
        assert i_z>=0
        for c0 in c0_grid:
            i_c0 = np.round((c0-c0_min)/d_c0).astype(int)
            assert i_c0>=0
            for x1 in x1_grid:
                i_x1 = np.round((x1-x1_min)/d_x1).astype(int)
                assert i_x1>=0
                tag = i_x1+i_c0*100+i_z*10000
                assert tag not in tags_used
                assert tag<max_tag
                tags_used.add(tag)
                mag_grid = np.zeros((6, len(t_grid)), dtype=float)

                sn = sncosmo.Model(source='salt2-extended')
                sn.set(x1=x1,c=c0,z=z)