Ejemplo n.º 1
0
More can be added as our programs evolve

There are a few parameters you should set beforehand.
plot_name is where the plot showing all composite spectra together will be saved.
title is the header on your plot
name_array is the list of legend labels
xmin and xmax define the boundary of the plot.
Plots actually defines which plots get shown
"""
queries = int(sys.argv[1])

d = {}
for n in range(queries):
    if len(sys.argv) == queries + 3:
        d["composite{0}".format(n + 1)] = composite.main(
            sys.argv[n + 2], sys.argv[queries + 2])
    if len(sys.argv) == queries + 4:
        d["composite{0}".format(n + 1)] = composite.main(
            sys.argv[n + 2], sys.argv[queries + 2], sys.argv[queries + 3])
    if len(sys.argv) == queries + 5:
        d["composite{0}".format(n + 1)] = composite.main(
            sys.argv[n + 2], sys.argv[queries + 2], sys.argv[queries + 3],
            sys.argv[queries + 4])
    else:
        d["composite{0}".format(n + 1)] = composite.main(sys.argv[n + 2])

#Read whatever you sasved the table as, iterates over however many composites you used.
#This is how you have to address things if you want to iterate over queries.
#The n+1 makes the first item composite1, not composite0.
for n in range(queries):
    #d["data{0}".format(n+1)]         = Table.read(d["composite{0}".format(n+1)].savedname, format='ascii')
Ejemplo n.º 2
0
plot_name is where the plot showing both composite spectra together will be saved.
wmin and wmax define the boundary of the plot.
"""

plot_name = '2_composite_comparison'
wmin = 4000
wmax = 7000

#This part works just fine
#composite_full = composite.main("SELECT * FROM Supernovae")
"""
Here we set the queries that get used to find the spectra for compositing.
We only want to select spectra that have data for both redshift and phase, so both of them need to be in the query.
But you can change the values to whatever you want.
"""
composite1 = composite.main("SELECT * FROM Supernovae WHERE Redshift >.01 AND Phase BETWEEN -3 AND 3")
composite2 = composite.main("SELECT * FROM Supernovae WHERE Redshift >.01 AND Phase BETWEEN 3 AND 7")

#This makes, shows, and saves a quick comparison plot...we can probably get rid of this when plotting.main works.
lowindex = np.where(composite1.wavelength == composite.find_nearest(composite1.wavelength, wmin))
highindex = np.where(composite1.wavelength == composite.find_nearest(composite1.wavelength, wmax))
plt.plot(composite1.wavelength[lowindex[0]:highindex[0]], composite1.flux[lowindex[0]:highindex[0]])
plt.plot(composite2.wavelength[lowindex[0]:highindex[0]], composite2.flux[lowindex[0]:highindex[0]])
plt.savefig('../plots/' + plot_name + '.png')
plt.show()

#Read whatever you sasved the table as
Data = Table.read(composite1.savedname, format='ascii')

#Checking to see how the table reads..right now it has a header that might be screwing things up.
print Data
Ejemplo n.º 3
0
import composite
import Plotting
from astropy.table import Table
import matplotlib.pyplot as plt
import numpy as np
import sys

composites = []
composites.append(
    composite.main(
        "SELECT * FROM Supernovae WHERE Morphology=5 AND PHASE BETWEEN .78 AND .80"
    ))
composites.append(
    composite.main(
        "SELECT * FROM Supernovae WHERE Morphology=5 AND PHASE BETWEEN .80 AND .82"
    ))
composites.append(
    composite.main(
        "SELECT * FROM Supernovae WHERE Morphology=5 AND PHASE BETWEEN .82 AND .85"
    ))

labels = ['Phase: .78 to .82', 'Phase: .82 to .85', 'Phase: .85 to .89']

scales = composite.find_scales(composites, composites[0].flux,
                               composites[0].ivar)

wmin = 0
wmax = 100000
for comp in composites:
    SN = comp
    if (SN.minwave > wmin):
Ejemplo n.º 4
0
def make_composite(query_strings,
                   boot=False,
                   nboots=100,
                   medmean=1,
                   selection='max_coverage',
                   gini_balance=False,
                   verbose=True,
                   make_corr=True,
                   av_corr=True,
                   multi_epoch=True,
                   combine=True,
                   low_av_test=None,
                   measure_vs=False,
                   get_og_arr=False,
                   shape_param=None,
                   db_file=None):
    """ This is the main fuunction for constructing composite spectra from spectra stored in kaepora.
        Args:
            query_strings: A list of SQL query strings

        Keyword Args:
            boot: If 'b', estimate error via bootstrap resampling. If 'nb', create
                composite spectrum without bootstrapping (faster).
            medmean: If 1, do an inverse variance weighted average as a function 
                of wavelength. If 2, do a median.
            selection: If multi_epoch is False, this string defines the selection
                criteria for choosing a single spectrum from a SN. Options are:
                'maximum_coverage'(default): largest wavelength range
                'maximum_coverage_choose_uv': largest wavelength range but prioritize
                    hst and swift spectra
                'choose_bluest': smallest minimum wavelength
                'max_snr': highest signal to noise
                'accurate_phase': closest to middle of the phase bin. TODO: implement
                    this without parsing the query (currently requires uncommenting
                    code)
                'max_coverage_splice': allows multiple spectra from the same SN as 
                    long as overlap is < 500 A
            gini_balance: If True, finds region of the composite spectrum dominated 
                by high SNR spectra, then deweights these spectra to have the the 
                median weight in that wavelength range. TODO: improve this 
                algorithm (currently produced representative composites, but
                deweighting straight to median might not be ideal). If False, ivar
                spectra are left as is. 
            verbose: If True, print metadata of each spectrum object in SN_Array.
            multi_epoch: If True, include all spectra for a given SN that satisify 
                the query. If False, choose one 1 spectrum per SN based on the 
                selection keyword.
            combine: If True, combines spectra from the same SN with an inverse weighted
                average prior to construction fo the composite spectrum. This should be 
                True if doing an inverse variance weighted average.
            low_av_test: If True, does not correct SNe with A_V < low_av_test
            measure_vs: If True, will measure and print the Si II 6355 line velocity and 
                error of each composite spectrum
            og_arr: If True, this function will also return the original spectra in addition 
                to the new combined spectra of individual SNe.

        Returns:
            composites: Each element is a list of spectrum objects containing the composite spectrum.
            sn_arrays: Each element is a list of the spectrum objects used to create the composite spectrum.
                This includes pre-combined spectra but not necessarily every original spectrum.
            (optional) og_sn_arrays: Each element is a list of the original spectrum objects used to make 
                the composite spectra (only if og_arr=True). 
            boot_sn_arrays: Each element is a list spectrum objects containing composite spectra generated 
                from the bootstrap resampling process.
    """
    composites = []
    sn_arrays = []
    og_sn_arrays = []
    boot_sn_arrays = []
    store_boots = True
    num_queries = len(query_strings)
    for n in range(num_queries):
        if get_og_arr:
            comp, arr, og_arr, boots = composite.main(
                query_strings[n],
                boot=boot,
                nboots=nboots,
                medmean=medmean,
                shape_param=shape_param,
                selection=selection,
                gini_balance=gini_balance,
                combine=combine,
                db_file=db_file,
                make_corr=make_corr,
                av_corr=av_corr,
                verbose=verbose,
                multi_epoch=multi_epoch,
                low_av_test=low_av_test,
                get_og_arr=get_og_arr)
            og_sn_arrays.append(og_arr)
        else:
            comp, arr, boots = composite.main(query_strings[n],
                                              boot=boot,
                                              nboots=nboots,
                                              medmean=medmean,
                                              shape_param=shape_param,
                                              selection=selection,
                                              gini_balance=gini_balance,
                                              combine=combine,
                                              db_file=db_file,
                                              make_corr=make_corr,
                                              av_corr=av_corr,
                                              verbose=verbose,
                                              multi_epoch=multi_epoch,
                                              low_av_test=low_av_test,
                                              get_og_arr=get_og_arr)
        if store_boots:
            boot_sn_arrays.append(boots)
        composites.append(comp)
        sn_arrays.append(arr)

    if None not in composites:
        composite.optimize_scales(composites, composites[0], True)
        composites = normalize_comps(composites)
    if measure_vs:
        for comp in composites:
            dm15 = np.round(np.nanmean(comp.dm15_array[comp.x1:comp.x2]), 2)
            # r = sa.measure_si_ratio(comp.wavelength[comp.x1:comp.x2], comp.flux[comp.x1:comp.x2], vexp = .001, dm15=dm15)
            v_strong, si_min_wave = sa.measure_velocity(
                comp.wavelength[comp.x1:comp.x2], comp.flux[comp.x1:comp.x2],
                5900., 6300.)
            print 'v = ', v_strong

        for boot in boot_sn_arrays:
            vs = []
            for b in boot:
                v_strong, si_min_wave = sa.measure_velocity(
                    b.wavelength[b.x1:b.x2], b.flux[b.x1:b.x2], 5900., 6300.)
                vs.append(v_strong)
            v_err = np.nanstd(vs)
            print 'v_err = ', v_err

    if get_og_arr:
        return composites, sn_arrays, og_sn_arrays, boot_sn_arrays
    else:
        return composites, sn_arrays, boot_sn_arrays
Ejemplo n.º 5
0
    boot = sys.argv[1]
    if boot == 'nb':
        boot = False
    else:
        boot = True

    query_strings = sys.argv[2:]

    num_queries = len(query_strings)

    for n in range(num_queries):
        c, sn_arr, boots = composite.main(query_strings[n],
                                          boot,
                                          nboots=100,
                                          medmean=1,
                                          gini_balance=True,
                                          make_corr=False,
                                          multi_epoch=True,
                                          combine=True)
        # c, sn_arr, boots = composite.main(query_strings[n], boot, medmean=1, gini_balance = False, multi_epoch=True, combine=False)
        # composites.append(c)
        # SN_Arrays.append(sn_arr)
        # if store_boots:
        #   boot_sn_arrays.append(boots)

        #use this for good composites
        # c, sn_arr, boots = composite.main(query_strings[n], boot, medmean=1, gini_balance = True, verbose=True, multi_epoch=True, combine=True)
        # composites.append(c)
        # SN_Arrays.append(sn_arr)
        # if store_boots:
        #   boot_sn_arrays.append(boots)
Ejemplo n.º 6
0
def main(queries, plot_name, plots, labels):
    num = int(queries[1])
    #Now the file name of the plot is labeled by time of creation, but you can personalize it if you want.
    #Or rename it once it's been saved.
    #plot_name = str(queries) + '_composite_comparison, ' + (time.strftime("%H,%M,%S"))
    wmin = 3000
    wmax = 10000

    d = {}
    for n in range(num):
        if len(queries) == num + 3:
            d["composite{0}".format(n + 1)] = composite.main(
                queries[n + 2], queries[num + 2])
        if len(queries) == num + 4:
            d["composite{0}".format(n + 1)] = composite.main(
                queries[n + 2], queries[num + 2], queries[num + 3])
        if len(queries) == num + 5:
            d["composite{0}".format(n + 1)] = composite.main(
                queries[n + 2], queries[num + 2], queries[num + 3],
                queries[num + 4])
        else:
            d["composite{0}".format(n + 1)] = composite.main(queries[n + 2])

    #Read whatever you sasved the table as, iterates over however many composites you used.
    #This is how you have to address things if you want to iterate over queries.
    #The n+1 makes the first item composite1, not composite0.
    for n in range(num):
        d["data{0}".format(n + 1)] = Table.read(
            d["composite{0}".format(n + 1)].savedname, format='ascii')
        d["wavelengths{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Wavelength"]])
        d["fluxes{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Flux"]])
        d["variances{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Variance"]])
        d["ages{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Age"]])
        d["dm15s{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Dm_15"]])
        d["vels{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Velocity"]])
        d["reds{0}".format(n + 1)] = np.array(
            [d["data{0}".format(n + 1)]["Redshift"]])

    xmin = 0
    xmax = 100000
    for n in range(num):
        wave = d["wavelengths{0}".format(n + 1)][0]
        flux = d["fluxes{0}".format(n + 1)][0]
        good = np.where(flux != 0)
        lo_wave = wave[good][0]
        hi_wave = wave[good][len(wave[good]) - 1]
        print lo_wave, hi_wave
        if (lo_wave > xmin):
            xmin = lo_wave
        if (hi_wave < xmax):
            xmax = hi_wave

    # From now on, list the data you want to plot as [ Xdata, Ydata, Xdata_2, Ydata_2]
    #This chunk will create an array that's the right length for however many queries you used.
    plot_array = []
    name_array = []
    residual_array = []
    variance_array = []
    age_array = []
    dm15_array = []
    vel_array = []
    red_array = []
    for n in range(num):
        plot_array.append(d["wavelengths{0}".format(n + 1)])
        plot_array.append(d["fluxes{0}".format(n + 1)])
        residual_array.append(d["wavelengths{0}".format(n + 1)][0])
        residual_list = np.array([d["fluxes{0}".format(n + 1)] - d["fluxes1"]])
        residual_array.append(residual_list[0][0])
        variance_array.append(d["wavelengths{0}".format(n + 1)][0])
        variance_array.append(d["variances{0}".format(n + 1)][0])
        age_array.append(d["wavelengths{0}".format(n + 1)][0])
        age_array.append(d["ages{0}".format(n + 1)][0])
        dm15_array.append(d["wavelengths{0}".format(n + 1)][0])
        dm15_array.append(d["dm15s{0}".format(n + 1)][0])
        vel_array.append(d["wavelengths{0}".format(n + 1)][0])
        vel_array.append(d["vels{0}".format(n + 1)][0])
        red_array.append(d["wavelengths{0}".format(n + 1)][0])
        red_array.append(d["reds{0}".format(n + 1)][0])
        name_array.append(labels[n])
        name_array.append(" ")

    #print variance_array # there were some problems with dimensionality, fixed now.

    ##################
    #If you want to use custom names for your composites,
    #fill out and uncomment this next line
    #name_array = ["composite1name", " ", "composite2name", " ", etc]
    ##################

    Relative_Flux = plot_array  #plots all given composites
    Variance = variance_array  # Check it out! Variances plot now.
    Residuals = residual_array  # Check it out! Residuals plot now.
    Spectra_Bin = []
    Age = age_array
    Delta = dm15_array
    Redshift = red_array
    ## If you want custom names, uncomment and use line 83, for consistency.
    ##Otherwise it'll default to just labeling composites in order.
    Names = name_array
    Show_Data = [
        Relative_Flux, Variance, Residuals, Spectra_Bin, Age, Delta, Redshift
    ]

    ## Available Plots:  Relative Flux, Residuals, Spectra/Bin, Age, Delta, Redshift, Multiple Spectrum, Stacked Spectrum
    ##                   0              1          2            3    4      5         6,                 7
    # the plots you want to create

    # All of these worked for me. Test with your own queries. (Sam, 4/16)
    # Choose the plot range and plot type!
    Plots = plots

    image_title = "../plots/" + plot_name + ".png"
    title = plot_name
    Plotting.main(Show_Data=Show_Data,
                  Plots=Plots,
                  image_title=image_title,
                  title=title,
                  Names=Names,
                  xmin=xmin,
                  xmax=xmax)