示例#1
0
def getdata():
    data = fetch_sdss_specgals()
    m_max = 17.7
    # redshift and magnitude cuts
    data = data[data['z'] > 0.08]
    data = data[data['z'] < 0.12]
    data = data[data['petroMag_r'] < m_max]
    # RA/DEC cuts
    RAmin, RAmax = 140, 220
    DECmin, DECmax = 5, 45
    data = data[data['ra'] < RAmax]
    data = data[data['ra'] > RAmin]
    data = data[data['dec'] < DECmax]
    data = data[data['dec'] > DECmin]
    ur = data['modelMag_u'] - data['modelMag_r']
    flag_red = (ur > 2.22)
    flag_blue = ~flag_red
    data_red = data[flag_red]
    data_blue = data[flag_blue]

    return [data, data_red, data_blue]
from sklearn.ensemble import GradientBoostingRegressor
from astroML.datasets import fetch_sdss_specgals
from astroML.decorators import pickle_results

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

#------------------------------------------------------------
# Fetch and prepare the data
data = fetch_sdss_specgals()

# put magnitudes in a matrix
mag = np.vstack([data['modelMag_%s' % f] for f in 'ugriz']).T
z = data['z']

# train on ~60,000 points
mag_train = mag[::10]
z_train = z[::10]

# test on ~6,000 distinct points
mag_test = mag[1::100]
z_test = z[1::100]


#------------------------------------------------------------
示例#3
0
"""
SDSS Spectroscopic Galaxy Sample
--------------------------------
This figure shows photometric colors of the SDSS spectroscopic galaxy
sample.
"""
# Author: Jake VanderPlas <*****@*****.**>
# License: BSD
#   The figure is an example from astroML: see http://astroML.github.com
import numpy as np
from matplotlib import pyplot as plt

from astroML.datasets import fetch_sdss_specgals

data = fetch_sdss_specgals()

#------------------------------------------------------------
# plot the RA/DEC in an area-preserving projection

RA = data['ra']
DEC = data['dec']

# convert coordinates to degrees
RA -= 180
RA *= np.pi / 180
DEC *= np.pi / 180

ax = plt.axes(projection='mollweide')

ax = plt.axes()
ax.grid()
示例#4
0
def main():
    ''' Your goal is to recalculate the luminosity function for the red and
    blue galaxies presented in the lower right of Figure 4.10 using
    reproducible methods for histogram binning. This activity divides the
    script for making these plots into more modular format. The above functions
    are only suggestions for how to divide the script, feel free to modularize
    the script in a different fashion.

    Figure 4.10 shows how the probability density of galaxies as function of
    redshift and absolute magnitude varies for two different galaxy populations
    selected by color. See the left panels of Figure 4.10 showing the
    probability density function p(M, z) for the two populations as a function
    of absolute magnitude M and redshift z. p(M, z) = Phi(M) * rho(z) where Phi
    is the normalized luminosity function, and rho is the sold angle averaged
    number density of galaxies. The normalization of rho in the figure is
    confusing, the top-right plot shows that the number density of redder
    galaxies, u-r > 2.22, is more dense than blue galaxies at smaller
    redshifts.  The luminosity function plotted in the bottom right show that
    there are fewer bright blue galaxies, u-r < 2.22, than bright red galaxies,
    u-r > 2.22.

    Read through the script
    http://www.astroml.org/book_figures/chapter4/fig_lyndenbell_gals.html
    to understand the steps the authors take to calculate the luminosity and
    volume number density functions for red and blue galaxies from the SDSS
    archive.

    *** Truncate the sample to 1/10 the size as commented in line 48 and 49 ***
    *** Else computation time exorbitant ***

    The author create redshift and absolute magnitude bins to compute the
    luminosity function. Find where these bins are initialized, and change the
    binning method to either Freedman-Diaconis or Scott. Use the most
    appropriate binning strategy for the data.

    Plot the results on the same scale and with the same limits as the code in
    the example script.

    Does the histogram change how you interpret the results of this data? Are
    these binning methods sensitive to outliers?

    Notes
    -----
    -> The Freedman-Diaconis and Scott binning methods can imported as
    functions in the following way:
        from astroML.density_estimation import scotts_bin_width as scott_bin
        from astroML.density_estimation import freedman_bin_width as free_bin

    '''

    from astroML.datasets import fetch_sdss_specgals

    data = fetch_sdss_specgals()

    z_min = 0.08
    z_max = 0.12
    m_max = 17.7

    data_red, data_blue = cut_data(data, z_min, z_max, m_max)
    data_red = data_red[::10]
    data_blue = data_blue[::10]
    data_samples = (data_red, data_blue)

    mu_z, z_mu = derive_dist_mod_tables((0.01, 1.5), 100)

    # Initialize data lists to contain blue and red data
    Mbins_list = []
    zbins_list = []
    dist_M_list = []
    err_M_list = []
    dist_z_list = []
    err_z_list = []

    archive_files = ['lumfunc_red.npz', 'lumfunc_blue.npz']

    # calculate luminosity function for blue and red galaxies
    for i in xrange(2):
        m = data_samples[i]['petroMag_r']
        z = data_samples[i]['z']
        M = m - mu_z(z)

        zbins, dist_z, err_z, Mbins, dist_M, err_M = \
                compute_luminosity_function(z, m, M, m_max, archive_files[i],
                    Nbootstraps=20, bin_type='freedman', z_mu=z_mu)

        Mbins_list.append(Mbins)
        zbins_list.append(zbins)
        dist_M_list.append(dist_M)
        err_M_list.append(err_M)
        dist_z_list.append(dist_z)
        err_z_list.append(err_z)

    # Plot the volume number density and the luminosity function
    titles = (r'u-r > 2.22', r'u-r < 2.22')
    markers = ['o', '^']

    plot_num_density(zbins_list,
                     dist_z_list,
                     err_z_list,
                     titles=titles,
                     markers=markers)

    plot_luminosity_function(Mbins_list,
                             dist_M_list,
                             err_M_list,
                             titles=titles,
                             markers=markers)
import numpy as np
import scipy as sp

import scipy.interpolate as spi
import matplotlib.pyplot as plt


from astroML.datasets import fetch_sdss_specgals
from astroML.datasets import fetch_sdss_spectrum

from sklearn.manifold import Isomap

d_galaxies= fetch_sdss_specgals()

Ngals = 1000

k = 20
n = 2

d_galaxies = d_galaxies[0:Ngals]

#print d_galaxies
#print d_galaxies.shape 

mjd_galaxies = d_galaxies['mjd']
plate_galaxies = d_galaxies['plate']
fiberID_galaxies = d_galaxies['fiberID']

h_alpha_flux_galaxies = d_galaxies['h_alpha_flux']
extinction_r_galaxies = d_galaxies['extinction_r']
velDisp_galaxies = d_galaxies['velDisp']
def main():

    ''' Your goal is to recalculate the luminosity function for the red and
    blue galaxies presented in the lower right of Figure 4.10 using
    reproducible methods for histogram binning. This activity divides the
    script for making these plots into more modular format. The above functions
    are only suggestions for how to divide the script, feel free to modularize
    the script in a different fashion.

    Figure 4.10 shows how the probability density of galaxies as function of
    redshift and absolute magnitude varies for two different galaxy populations
    selected by color. See the left panels of Figure 4.10 showing the
    probability density function p(M, z) for the two populations as a function
    of absolute magnitude M and redshift z. p(M, z) = Phi(M) * rho(z) where Phi
    is the normalized luminosity function, and rho is the sold angle averaged
    number density of galaxies. The normalization of rho in the figure is
    confusing, the top-right plot shows that the number density of redder
    galaxies, u-r > 2.22, is more dense than blue galaxies at smaller
    redshifts.  The luminosity function plotted in the bottom right show that
    there are fewer bright blue galaxies, u-r < 2.22, than bright red galaxies,
    u-r > 2.22.

    Read through the script
    http://www.astroml.org/book_figures/chapter4/fig_lyndenbell_gals.html
    to understand the steps the authors take to calculate the luminosity and
    volume number density functions for red and blue galaxies from the SDSS
    archive.

    *** Truncate the sample to 1/10 the size as commented in line 48 and 49 ***
    *** Else computation time exorbitant ***

    The author create redshift and absolute magnitude bins to compute the
    luminosity function. Find where these bins are initialized, and change the
    binning method to either Freedman-Diaconis or Scott. Use the most
    appropriate binning strategy for the data.

    Plot the results on the same scale and with the same limits as the code in
    the example script.

    Does the histogram change how you interpret the results of this data? Are
    these binning methods sensitive to outliers?

    Notes
    -----
    -> The Freedman-Diaconis and Scott binning methods can imported as
    functions in the following way:
        from astroML.density_estimation import scotts_bin_width as scott_bin
        from astroML.density_estimation import freedman_bin_width as free_bin

    '''

    from astroML.datasets import fetch_sdss_specgals

    data = fetch_sdss_specgals()

    z_min = 0.08
    z_max = 0.12
    m_max = 17.7

    data_red, data_blue = cut_data(data, z_min, z_max, m_max)
    data_red = data_red[::10]
    data_blue = data_blue[::10]
    data_samples = (data_red, data_blue)

    mu_z, z_mu = derive_dist_mod_tables((0.01, 1.5), 100)

    # Initialize data lists to contain blue and red data
    Mbins_list = []
    zbins_list = []
    dist_M_list = []
    err_M_list = []
    dist_z_list = []
    err_z_list = []

    archive_files = ['lumfunc_red.npz', 'lumfunc_blue.npz']

    # calculate luminosity function for blue and red galaxies
    for i in xrange(2):
        m = data_samples[i]['petroMag_r']
        z = data_samples[i]['z']
        M = m - mu_z(z)

        zbins, dist_z, err_z, Mbins, dist_M, err_M = \
                compute_luminosity_function(z, m, M, m_max, archive_files[i],
                    Nbootstraps=20, bin_type='freedman', z_mu=z_mu)

        Mbins_list.append(Mbins)
        zbins_list.append(zbins)
        dist_M_list.append(dist_M)
        err_M_list.append(err_M)
        dist_z_list.append(dist_z)
        err_z_list.append(err_z)

    # Plot the volume number density and the luminosity function
    titles = (r'u-r > 2.22', r'u-r < 2.22')
    markers = ['o', '^']

    plot_num_density(zbins_list, dist_z_list, err_z_list, titles=titles,
            markers=markers)

    plot_luminosity_function(Mbins_list, dist_M_list, err_M_list,
            titles=titles, markers=markers)