def bake(zgrid):
    # get SED list
    listOfSedsFile = "lsst.seds"
    sedLib = sedFilter.createSedDict(listOfSedsFile, "../data/sed/")
    sedList = sorted(sedLib.keys())
    nSED = len(sedLib)

    # get LSST filters
    listOfFiltersFile = "lsst.filters"
    filterLib = sedFilter.createFilterDict(listOfFiltersFile,
                                           "../data/bandpass/")
    filterList = sedFilter.orderFiltersByLamEff(filterLib)
    nFilter = len(filterLib)

    # instantiate photometric calculations
    pcalcs = {}
    for sedname, sed in sedLib.items():
        pcalcs[sedname] = phot.PhotCalcs(sed, filterLib)

    nz = len(zgrid)

    # record array to return
    n_rows = nSED*nz
    dtype = np.dtype([('sedname', str, 300), ('redshift', np.float),
                      ('ug', np.float), ('gr', np.float), ('ri', np.float),
                      ('iz', np.float), ('zy', np.float), ('time', np.float)])
    dummy = ('aaaa', 1.0, 1, 1, 1, 1, 1, 1.0)
    records = np.array([dummy]*n_rows, dtype=dtype)

    i = 0
    # loop over redshifts
    for z in zgrid:
        # each SED in lib
        for sedname in sedList:
            # time calculation of *all* colors for this redshift+SED
            start_time = time.time()
            colors = []
            for ifilt in range(nFilter-1):
                mag = pcalcs[sedname].computeColor(filterList[ifilt],
                                                   filterList[ifilt+1], z)
                colors.append(mag)
            end_time = time.time()
            rec = np.array([(sedname, z, colors[0], colors[1], colors[2],
                             colors[3], colors[4], end_time-start_time)],
                           dtype=records.dtype)
            records[i] = rec
            i += 1

    return records
Пример #2
0
def get_sed_colors(sedDict, filterDict):
    """Calculate the colors for all the SEDs in sedDict given the filters in filterDict, return as pandas
       data frame
    
       @param sedDict       dictionary of SEDs
       @param filterDict    dictionary of filters
    """
    
    ncolors = len(filterDict) - 1
    nseds = len(sedDict)

    # sort based upon effective wavelength
    filter_order = sedFilter.orderFiltersByLamEff(filterDict)
    
    # get names of colors
    color_names = []
    for i in range(ncolors):
        color_names.append(str(filter_order[i]) + "-" + str(filter_order[i+1]) )
    
    # calculate SED colors
    sed_colors = np.zeros((nseds, ncolors))
    sed_names = []
    i=0
    tot_time = 0.
    for sedname, sed in sedDict.items():
    
        print "Calculating colors for SED:", sedname
        sed_names.append(sedname)
        p = phot.PhotCalcs(sed, filterDict)
  
        start_time = time.time()
        for j in range(ncolors):
        
            sed_colors[i,j] = p.computeColor(filter_order[j], filter_order[j+1], 0.)
        end_time = time.time()
        print "Took", end_time - start_time, "to compute", ncolors, "colors"
        tot_time += (end_time - start_time)
        i+=1
    print "Total time to compute colors for SEDs =", tot_time
    
    # convert to dataframe and return
    return pd.DataFrame(sed_colors, columns=color_names, index=sed_names)
def main(argv):
    
    save_stem = 'new_lsst' # files will be saved to filenames beginning `save_stem`
    perf_lim = 3           # performance limit: min number of colors that should reach LSST sys err
    color_file = "../tmp/brown_colors_lsst.txt"  # File to contain colors or to read colors from 
    listOfFilters = 'LSST.filters'               # Filter set to use                           
    corr_type = 'cubic'    # type of covariance function to use in GP
    theta0 = 0.2           # parameters for GP covariance function
                 
    try:
        opts, args = getopt.getopt(argv,"hs:p:c:f:g:")
    except getopt.GetoptError as err: # if include option that's not there
        usage(2)
      
    for opt, arg in opts:
        if opt == '-h':
            usage(0)
        elif opt in ("-s"):
            save_stem = arg
        elif opt in ("-p"):
            perf_lim = int(arg)
        elif opt in ("-c"):
            color_file = arg
        elif opt in ("-f"):
            listOfFilters = arg
        elif opt in ("-g"):
            corr_type = arg.split(',')[0]
            theta0 = float(arg.split(',')[1])
            
    print '\n Command line arguments:'
    print ' Saving to files ... ', save_stem
    print ' Reading/saving colors from/to file', color_file
    print ' Using', listOfFilters ,'filter set'
    print ' At least', perf_lim ,'colors must meet LSST sys err to be `good`'
    print ' Covariance function will be', corr_type ,'with parameter', theta0
    print ''


    ### Read SEDs into a dictionary
    listOfSeds = 'brown_masked.seds'                             
    pathToSEDs = '../sed_data'
    sedDict = sedFilter.createSedDict(listOfSeds, pathToSEDs)
    nSED = len(sedDict)
    print "Number of SEDs =", nSED


    ### Filter set to calculate colors
    pathToFilters = '../filter_data/'
    filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters)
    filterList = sedFilter.orderFiltersByLamEff(filterDict)
    nFilters = len(filterList)
    print "Number of filters =", nFilters


    ### Wavelength grid to do PCA on
    minWavelen = 1000.
    maxWavelen = 12000.
    nWavelen = 10000

                 
    ### Do PCA and train GP
    ncomp = nSED
    nfit = -1
    pcaGP = sedMapper.PcaGaussianProc(sedDict, filterDict, color_file, ncomp, 
                                      minWavelen, maxWavelen, nWavelen, nfit,
                                      corr_type, theta0)
    colors = pcaGP._colors
    spectra = pcaGP._spectra
    waveLen = pcaGP._waveLen
    meanSpectrum = pcaGP.meanSpec
    projected_all = pcaGP.eigenvalue_coeffs
    print "... done\n"


    ### Leave out each SED in turn
    delta_mag = np.zeros((nSED,nFilters))
    perf = []
    for i, (sedname, spec) in enumerate(sedDict.items()):
    
        print "\nOn SED", i+1 ,"of", nSED
    

        ### Retrain GP with SED removed
        nc = nSED-1
        pcaGP.reTrainGP(nc, i)
    
    
        ### Reconstruct SED
        sed_rec = pcaGP.generateSpectrum(colors[i,:])
        
    
        ### Calculate colors of reconstructed SED
        pcalcs = phot.PhotCalcs(sed_rec, filterDict)
        cnt = 0
        isBad = False

        for j in range(nFilters-1):
            cs = pcalcs.computeColor(filterList[j], filterList[j+1])
        
            delta_mag[i,j] = cs-colors[i,j]
            if (j<6):
                print "(", cs, colors[i,j], delta_mag[i,j],")"
            if (abs(delta_mag[i,j])<0.005):
                cnt+=1
            if (abs(delta_mag[i,j])>0.05):
                isBad = True
        print ""


        ### Get array version of SED back
        wl, spec_rec = sed_rec.getSedData(lamMin=minWavelen, lamMax=maxWavelen, nLam=nWavelen)

    
        ### Plot
        fig = plt.figure(figsize=(10,10))
        ax = fig.add_subplot(111)
        ax.plot(waveLen, spectra[i,:], color='blue', label='true')
        ax.plot(wl, spec_rec, color='red', linestyle='dashed', label='estimated')
        ax.plot(waveLen, meanSpectrum, color='black', linestyle='dotted', label='mean')
        ax.set_xlabel('wavelength (angstroms)', fontsize=24)
        ax.set_ylabel('flux', fontsize=24)
        handles, labels = ax.get_legend_handles_labels()
        ax.legend(loc='lower right', prop={'size':12})
        ax.set_title(sedname, fontsize=24) 
        
        annotate =  "Mean $\Delta$ color = {0:.5f} \n".format(np.mean(delta_mag[i,:]))
        annotate += "Stdn $\Delta$ color = {0:.5f} ".format(np.std(delta_mag[i,:]))
        y1, y2 = ax.get_ylim()
        ax.text(9000, 0.9*y2, annotate, fontsize=12)
        plt.savefig(save_stem + '_' + 'bad_' + sedname + '.png')
        #plt.show(block=True)
        
        
        ### Performance check
        print cnt,"colors within LSST systematic error"
        perf.append(cnt)
    

    perf = np.asarray(perf)

    ### Save results
    np.savetxt(save_stem + '_deltamag.txt', delta_mag)

     
    ### Plot eigenvalue 1 vs eigenvalue 2
    fig = plt.figure(figsize=(10,10))
    ax = fig.add_subplot(111)    
    ax.plot(projected_all[:, 0], projected_all[:, 1], linestyle='none', marker='o', color='blue', label='good')
    ax.plot(projected_all[np.where(perf<perf_lim), 0], projected_all[np.where(perf<perf_lim), 1],
            linestyle='none', marker='o', color='red', label='bad')
    ax.set_xlabel('eigenvalue 1', fontsize=24)
    ax.set_ylabel('eigenvalue 2', fontsize=24)
    handles, labels = ax.get_legend_handles_labels()
    ax.legend(handles[:4], labels[:4], loc='lower right', prop={'size':12})


    ### Histogram of number of colors per SED better than LSST systematic error
    fig = plt.figure(figsize=(10,10))
    ax = fig.add_subplot(111)
    ax.hist(perf,20, normed=False, histtype='stepfilled')
    ax.set_xlabel('number of colors better than sys error', fontsize=24)
    plt.savefig(save_stem + '_' + 'perf.png')
    plt.show(block=True)
    

    ### Histogram of delta-mags
    for j in range(nFilters-1):
        fig = plt.figure(figsize=(10,10))
        ax = fig.add_subplot(111)
    
        dmag_finite = delta_mag[np.where(abs(delta_mag[:,j])<50),j].T
    
        ax.hist(dmag_finite, 20, normed=False, histtype='stepfilled')
        ax.set_xlabel('$\Delta$color$_{' + str(j) + "}$", fontsize=24)
        plt.savefig(save_stem + '_color' + str(j) + '.png')

 
    plt.show(block=True)
"""Compare E2V and ITL filters


"""

import sedFilter
import matplotlib.pyplot as plt


### Read in ITL filters
listOfFiltersFile = "LSST_ITL.filters"
pathToFile = "../filter_data/"
itl = sedFilter.createFilterDict(listOfFiltersFile, pathToFile)
itl_list = sedFilter.orderFiltersByLamEff(itl)

### Read in E2V filters
listOfFiltersFile = "LSST_E2V.filters"
pathToFile = "../filter_data/"
e2v = sedFilter.createFilterDict(listOfFiltersFile, pathToFile)
e2v_list = sedFilter.orderFiltersByLamEff(e2v)


### start figure
fig = plt.figure(figsize=(20,20))

ifilt = 1
for itlname,e2vname in zip(itl_list,e2v_list):

    wavelengths, transmissions = itl[itlname].getFilterData()
    wavelengths_e2v, transmissions_e2v = e2v[e2vname].getFilterData()
    
def get_sed_colors(sedDict, filterDict, ipivot=-1, doPrinting=True):
    """Calculate the colors for all the SEDs in sedDict given the filters in filterDict, return as pandas
       data frame
    
       @param sedDict       dictionary of SEDs
       @param filterDict    dictionary of filters
       @param ipivot        index of filter to reference ALL colors to (if -1 just does usual)
       @param doPrinting 
    """
    
    nfilters = len(filterDict)
    ncolors = nfilters - 1
    nseds = len(sedDict)
    
    if ipivot>ncolors:
        raise ValueError("Error! pivot filter outside range")

    # sort based upon effective wavelength
    filter_order = sedFilter.orderFiltersByLamEff(filterDict)
    
    # get names of colors
    color_names = []
    for i in range(ncolors):
        color_names.append(str(filter_order[i]) + "-" + str(filter_order[i+1]) )
    
    # calculate SED colors
    sed_colors = np.zeros((nseds, ncolors))
    sed_names = []
    i=0
    tot_time = 0.
    for sedname, sed in sedDict.items():
    
        if doPrinting:
            print "Calculating colors for SED:", sedname
        sed_names.append(sedname)
        p = phot.PhotCalcs(sed, filterDict)
  
        start_time = time.time()
        
        if (ipivot>=0):
            # all colors in reference to a pivot filter, e.g. u-r, g-r, r-i, r-z, r-y
            #ii = 0
            colors = []
            for j in range(nfilters):
                if (j<ipivot):
                    colors.append(p.computeColor(filter_order[j], filter_order[ipivot], 0.))
                    #sed_colors[i,ii] = p.computeColor(filter_order[j], filter_order[ipivot], 0.)
                    if (i<1):
                        #print ii, 
                        print "Doing", filter_order[j] ,"-", filter_order[ipivot], 
                        print p.computeColor(filter_order[j], filter_order[ipivot], 0.)
                    #ii=+1
                elif (j>ipivot):
                    #sed_colors[i,ii] = p.computeColor(filter_order[ipivot], filter_order[j], 0.)
                    colors.append(p.computeColor(filter_order[ipivot], filter_order[j], 0.))
                    if (i<1):
                        #print ii, 
                        print "Doing", filter_order[ipivot] ,"-", filter_order[j],
                        print p.computeColor(filter_order[ipivot], filter_order[j], 0.)
                    #ii=+1
                # note that nothing is done when filter index j = ipivot    
            if (i<1):
                print colors, len(colors)
            for j in range(ncolors):
                sed_colors[i,j] = colors[j]  
        else:
            # traditional color definition: e.g. u-g, g-r, r-i etc
            for j in range(ncolors):
                sed_colors[i,j] = p.computeColor(filter_order[j], filter_order[j+1], 0.)
            
        end_time = time.time()
        
        if doPrinting:
            print "Took", end_time - start_time, "to compute", ncolors, "colors"
        
        tot_time += (end_time - start_time)
        
        i+=1
    if doPrinting:
        print "Total time to compute colors for SEDs =", tot_time
    
    # convert to dataframe and return
    return pd.DataFrame(sed_colors, columns=color_names, index=sed_names)
def get_sed_array(sedDict, minWavelen=2999., maxWavelen=12000., nWavelen=10000, 
                  filterDict=None, color_file=None):
    """Return array of SEDs on same wavelength grid (optionally along with colors as defined by filterDict)
    
       If computing colors, first orders filters by effective wavelength, then a color is:
       color_{i, i+1} = mag_filter_i - mag_filter_i+1
    
       @param sedDict       dictionary of SEDs
       @param minWavelen    minimum wavelength of wavelength grid
       @param maxWavelen    maximum wavelength of wavelength grid
       @param nWavelen      number of points in wavelength grid
       @param filterDict    dictionary of filters
       @param color_file    file to save SED colors to or read colors from (if exists)
       
    """
    
    doColors = True
    if (filterDict==None or color_file==None):
        doColors = False
    
    
    isFileExist = False
    if doColors:
    
        # sort based upon effective wavelength
        filter_order = sedFilter.orderFiltersByLamEff(filterDict)
    
    
        # check if file exists and need to calculate colors
        isFileExist = os.path.isfile(color_file)
        if (isFileExist):
            print "\nColors already computed,",
        else:
            print "\nComputing colors,",
    print "placing SEDs in array ..." 

    # loop over each SED
    nSED = len(sedDict)
    spectra = []
    colors = []
    for ised, (sedname, spec) in enumerate(sedDict.items()):
    
        
        print "On SED", ised+1 ,"of", nSED, sedname
    
        # re-grid SEDs onto same wavelengths
        waveLen, fl = spec.getSedData(lamMin=minWavelen, lamMax=maxWavelen, nLam=nWavelen)
        
        
        # normalise so they sum to 1
        norm = np.sum(fl)
        spectra.append(fl/norm)
        
        
        if doColors: 
            # calculate or read colors
            cs = []
            if (isFileExist):
        
                # reading colors
                colors_in_file = np.loadtxt(color_file)
                cs = colors_in_file[ised,:]
    
            else:
        
                # calculating colors
                spec = sedFilter.SED(waveLen, fl)#/norm)
                pcalcs = phot.PhotCalcs(spec, filterDict)
    
                # in each filter
                for i in range(len(filterDict)-1):
                    color = pcalcs.computeColor(filter_order[i], filter_order[i+1])
                    if (color == float('inf')):
                        color = 99.
                    cs.append(color)
        
            # store colors for this SED
            colors.append(cs)
        
    
    # conver to np arrays for ease
    spectra = np.array(spectra)
    colors = np.array(colors)
    
    
    # if had to calculate, save colors to file to re-use
    if (not isFileExist and doColors):
        print "Saving colors to file for future use"
        np.savetxt(color_file, colors)

    if doColors:
        return waveLen, spectra, colors
    else:
        return waveLen, spectra
import numpy as np
import cphotometry as cphot
import sedFilter
import cosmo
import photErrorModel as pem

# Single SED for testing
fname = os.path.join(os.path.dirname(__file__), '../sed_data/El_B2004a.sed')
seddata = np.loadtxt(fname)
TEST_SED = sedFilter.SED(seddata[:,0], seddata[:,1])

# List of filters for testing
listOfFilters = 'LSST.filters'
pathToFilters = '../filter_data'
TEST_FILTERDICT = sedFilter.createFilterDict(listOfFilters, pathToFilters)
FILTERLIST = sedFilter.orderFiltersByLamEff(TEST_FILTERDICT)

# List of SEDs for testing
SEDLIST = []
sedfiles = ['../sed_data/El_B2004a.sed','../sed_data/Sbc_B2004a.sed','../sed_data/Scd_B2004a.sed',
           '../sed_data/Im_B2004a.sed']
for name in sedfiles:
    seddata = np.loadtxt(name)
    SEDLIST.append(sedFilter.SED(seddata[:,0], seddata[:,1]))

# Cosmological model
COSMOMODEL = cosmo.cosmologyCalculator()


class TestPhotCalcs(unittest.TestCase):
Пример #8
0
def main(argv):

    save_stem = 'new_lsst'  # files will be saved to filenames beginning `save_stem`
    perf_lim = 3  # performance limit: min number of colors that should reach LSST sys err
    color_file = "../tmp/brown_colors_lsst.txt"  # File to contain colors or to read colors from
    listOfFilters = 'LSST.filters'  # Filter set to use
    corr_type = 'cubic'  # type of covariance function to use in GP
    theta0 = 0.2  # parameters for GP covariance function

    try:
        opts, args = getopt.getopt(argv, "hs:p:c:f:g:")
    except getopt.GetoptError as err:  # if include option that's not there
        usage(2)

    for opt, arg in opts:
        if opt == '-h':
            usage(0)
        elif opt in ("-s"):
            save_stem = arg
        elif opt in ("-p"):
            perf_lim = int(arg)
        elif opt in ("-c"):
            color_file = arg
        elif opt in ("-f"):
            listOfFilters = arg
        elif opt in ("-g"):
            corr_type = arg.split(',')[0]
            theta0 = float(arg.split(',')[1])

    print '\n Command line arguments:'
    print ' Saving to files ... ', save_stem
    print ' Reading/saving colors from/to file', color_file
    print ' Using', listOfFilters, 'filter set'
    print ' At least', perf_lim, 'colors must meet LSST sys err to be `good`'
    print ' Covariance function will be', corr_type, 'with parameter', theta0
    print ''

    ### Read SEDs into a dictionary
    listOfSeds = 'brown_masked.seds'
    pathToSEDs = '../sed_data'
    sedDict = sedFilter.createSedDict(listOfSeds, pathToSEDs)
    nSED = len(sedDict)
    print "Number of SEDs =", nSED

    ### Filter set to calculate colors
    pathToFilters = '../filter_data/'
    filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters)
    filterList = sedFilter.orderFiltersByLamEff(filterDict)
    nFilters = len(filterList)
    print "Number of filters =", nFilters

    ### Wavelength grid to do PCA on
    minWavelen = 1000.
    maxWavelen = 12000.
    nWavelen = 10000

    ### Do PCA and train GP
    ncomp = nSED
    nfit = -1
    pcaGP = sedMapper.PcaGaussianProc(sedDict, filterDict, color_file, ncomp,
                                      minWavelen, maxWavelen, nWavelen, nfit,
                                      corr_type, theta0)
    colors = pcaGP._colors
    spectra = pcaGP._spectra
    waveLen = pcaGP._waveLen
    meanSpectrum = pcaGP.meanSpec
    projected_all = pcaGP.eigenvalue_coeffs
    print "... done\n"

    ### Leave out each SED in turn
    delta_mag = np.zeros((nSED, nFilters))
    perf = []
    for i, (sedname, spec) in enumerate(sedDict.items()):

        print "\nOn SED", i + 1, "of", nSED

        ### Retrain GP with SED removed
        nc = nSED - 1
        pcaGP.reTrainGP(nc, i)

        ### Reconstruct SED
        sed_rec = pcaGP.generateSpectrum(colors[i, :])

        ### Calculate colors of reconstructed SED
        pcalcs = phot.PhotCalcs(sed_rec, filterDict)
        cnt = 0
        isBad = False

        for j in range(nFilters - 1):
            cs = pcalcs.computeColor(filterList[j], filterList[j + 1])

            delta_mag[i, j] = cs - colors[i, j]
            if (j < 6):
                print "(", cs, colors[i, j], delta_mag[i, j], ")"
            if (abs(delta_mag[i, j]) < 0.005):
                cnt += 1
            if (abs(delta_mag[i, j]) > 0.05):
                isBad = True
        print ""

        ### Get array version of SED back
        wl, spec_rec = sed_rec.getSedData(lamMin=minWavelen,
                                          lamMax=maxWavelen,
                                          nLam=nWavelen)

        ### Plot
        fig = plt.figure(figsize=(10, 10))
        ax = fig.add_subplot(111)
        ax.plot(waveLen, spectra[i, :], color='blue', label='true')
        ax.plot(wl,
                spec_rec,
                color='red',
                linestyle='dashed',
                label='estimated')
        ax.plot(waveLen,
                meanSpectrum,
                color='black',
                linestyle='dotted',
                label='mean')
        ax.set_xlabel('wavelength (angstroms)', fontsize=24)
        ax.set_ylabel('flux', fontsize=24)
        handles, labels = ax.get_legend_handles_labels()
        ax.legend(loc='lower right', prop={'size': 12})
        ax.set_title(sedname, fontsize=24)

        annotate = "Mean $\Delta$ color = {0:.5f} \n".format(
            np.mean(delta_mag[i, :]))
        annotate += "Stdn $\Delta$ color = {0:.5f} ".format(
            np.std(delta_mag[i, :]))
        y1, y2 = ax.get_ylim()
        ax.text(9000, 0.9 * y2, annotate, fontsize=12)
        plt.savefig(save_stem + '_' + 'bad_' + sedname + '.png')
        #plt.show(block=True)

        ### Performance check
        print cnt, "colors within LSST systematic error"
        perf.append(cnt)

    perf = np.asarray(perf)

    ### Save results
    np.savetxt(save_stem + '_deltamag.txt', delta_mag)

    ### Plot eigenvalue 1 vs eigenvalue 2
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)
    ax.plot(projected_all[:, 0],
            projected_all[:, 1],
            linestyle='none',
            marker='o',
            color='blue',
            label='good')
    ax.plot(projected_all[np.where(perf < perf_lim), 0],
            projected_all[np.where(perf < perf_lim), 1],
            linestyle='none',
            marker='o',
            color='red',
            label='bad')
    ax.set_xlabel('eigenvalue 1', fontsize=24)
    ax.set_ylabel('eigenvalue 2', fontsize=24)
    handles, labels = ax.get_legend_handles_labels()
    ax.legend(handles[:4], labels[:4], loc='lower right', prop={'size': 12})

    ### Histogram of number of colors per SED better than LSST systematic error
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)
    ax.hist(perf, 20, normed=False, histtype='stepfilled')
    ax.set_xlabel('number of colors better than sys error', fontsize=24)
    plt.savefig(save_stem + '_' + 'perf.png')
    plt.show(block=True)

    ### Histogram of delta-mags
    for j in range(nFilters - 1):
        fig = plt.figure(figsize=(10, 10))
        ax = fig.add_subplot(111)

        dmag_finite = delta_mag[np.where(abs(delta_mag[:, j]) < 50), j].T

        ax.hist(dmag_finite, 20, normed=False, histtype='stepfilled')
        ax.set_xlabel('$\Delta$color$_{' + str(j) + "}$", fontsize=24)
        plt.savefig(save_stem + '_color' + str(j) + '.png')

    plt.show(block=True)
"""Compare E2V and ITL filters


"""

import sedFilter
import matplotlib.pyplot as plt

### Read in ITL filters
listOfFiltersFile = "LSST_ITL.filters"
pathToFile = "../filter_data/"
itl = sedFilter.createFilterDict(listOfFiltersFile, pathToFile)
itl_list = sedFilter.orderFiltersByLamEff(itl)

### Read in E2V filters
listOfFiltersFile = "LSST_E2V.filters"
pathToFile = "../filter_data/"
e2v = sedFilter.createFilterDict(listOfFiltersFile, pathToFile)
e2v_list = sedFilter.orderFiltersByLamEff(e2v)

### start figure
fig = plt.figure(figsize=(20, 20))

ifilt = 1
for itlname, e2vname in zip(itl_list, e2v_list):

    wavelengths, transmissions = itl[itlname].getFilterData()
    wavelengths_e2v, transmissions_e2v = e2v[e2vname].getFilterData()

    # plot
    ax = fig.add_subplot(2, 3, ifilt)
Пример #10
0
def get_sed_colors(sedDict, filterDict, ipivot=-1, doPrinting=True):
    """Calculate the colors for all the SEDs in sedDict given the filters in filterDict, return as pandas
       data frame
    
       @param sedDict       dictionary of SEDs
       @param filterDict    dictionary of filters
       @param ipivot        index of filter to reference ALL colors to (if -1 just does usual)
       @param doPrinting 
    """

    nfilters = len(filterDict)
    ncolors = nfilters - 1
    nseds = len(sedDict)

    if ipivot > ncolors:
        raise ValueError("Error! pivot filter outside range")

    # sort based upon effective wavelength
    filter_order = sedFilter.orderFiltersByLamEff(filterDict)

    # get names of colors
    color_names = []
    for i in range(ncolors):
        color_names.append(
            str(filter_order[i]) + "-" + str(filter_order[i + 1]))

    # calculate SED colors
    sed_colors = np.zeros((nseds, ncolors))
    sed_names = []
    i = 0
    tot_time = 0.
    for sedname, sed in sedDict.items():

        if doPrinting:
            print "Calculating colors for SED:", sedname
        sed_names.append(sedname)
        p = phot.PhotCalcs(sed, filterDict)

        start_time = time.time()

        if (ipivot >= 0):
            # all colors in reference to a pivot filter, e.g. u-r, g-r, r-i, r-z, r-y
            #ii = 0
            colors = []
            for j in range(nfilters):
                if (j < ipivot):
                    colors.append(
                        p.computeColor(filter_order[j], filter_order[ipivot],
                                       0.))
                    #sed_colors[i,ii] = p.computeColor(filter_order[j], filter_order[ipivot], 0.)
                    if (i < 1):
                        #print ii,
                        print "Doing", filter_order[j], "-", filter_order[
                            ipivot],
                        print p.computeColor(filter_order[j],
                                             filter_order[ipivot], 0.)
                    #ii=+1
                elif (j > ipivot):
                    #sed_colors[i,ii] = p.computeColor(filter_order[ipivot], filter_order[j], 0.)
                    colors.append(
                        p.computeColor(filter_order[ipivot], filter_order[j],
                                       0.))
                    if (i < 1):
                        #print ii,
                        print "Doing", filter_order[ipivot], "-", filter_order[
                            j],
                        print p.computeColor(filter_order[ipivot],
                                             filter_order[j], 0.)
                    #ii=+1
                # note that nothing is done when filter index j = ipivot
            if (i < 1):
                print colors, len(colors)
            for j in range(ncolors):
                sed_colors[i, j] = colors[j]
        else:
            # traditional color definition: e.g. u-g, g-r, r-i etc
            for j in range(ncolors):
                sed_colors[i, j] = p.computeColor(filter_order[j],
                                                  filter_order[j + 1], 0.)

        end_time = time.time()

        if doPrinting:
            print "Took", end_time - start_time, "to compute", ncolors, "colors"

        tot_time += (end_time - start_time)

        i += 1
    if doPrinting:
        print "Total time to compute colors for SEDs =", tot_time

    # convert to dataframe and return
    return pd.DataFrame(sed_colors, columns=color_names, index=sed_names)
Пример #11
0
def get_sed_array(sedDict,
                  minWavelen=2999.,
                  maxWavelen=12000.,
                  nWavelen=10000,
                  filterDict=None,
                  color_file=None):
    """Return array of SEDs on same wavelength grid (optionally along with colors as defined by filterDict)
    
       If computing colors, first orders filters by effective wavelength, then a color is:
       color_{i, i+1} = mag_filter_i - mag_filter_i+1
    
       @param sedDict       dictionary of SEDs
       @param minWavelen    minimum wavelength of wavelength grid
       @param maxWavelen    maximum wavelength of wavelength grid
       @param nWavelen      number of points in wavelength grid
       @param filterDict    dictionary of filters
       @param color_file    file to save SED colors to or read colors from (if exists)
       
    """

    doColors = True
    if (filterDict == None or color_file == None):
        doColors = False

    isFileExist = False
    if doColors:

        # sort based upon effective wavelength
        filter_order = sedFilter.orderFiltersByLamEff(filterDict)

        # check if file exists and need to calculate colors
        isFileExist = os.path.isfile(color_file)
        if (isFileExist):
            print "\nColors already computed,",
        else:
            print "\nComputing colors,",
    print "placing SEDs in array ..."

    # loop over each SED
    nSED = len(sedDict)
    spectra = []
    colors = []
    for ised, (sedname, spec) in enumerate(sedDict.items()):

        print "On SED", ised + 1, "of", nSED, sedname

        # re-grid SEDs onto same wavelengths
        waveLen, fl = spec.getSedData(lamMin=minWavelen,
                                      lamMax=maxWavelen,
                                      nLam=nWavelen)

        # normalise so they sum to 1
        norm = np.sum(fl)
        spectra.append(fl / norm)

        if doColors:
            # calculate or read colors
            cs = []
            if (isFileExist):

                # reading colors
                colors_in_file = np.loadtxt(color_file)
                cs = colors_in_file[ised, :]

            else:

                # calculating colors
                spec = sedFilter.SED(waveLen, fl)  #/norm)
                pcalcs = phot.PhotCalcs(spec, filterDict)

                # in each filter
                for i in range(len(filterDict) - 1):
                    color = pcalcs.computeColor(filter_order[i],
                                                filter_order[i + 1])
                    if (color == float('inf')):
                        color = 99.
                    cs.append(color)

            # store colors for this SED
            colors.append(cs)

    # conver to np arrays for ease
    spectra = np.array(spectra)
    colors = np.array(colors)

    # if had to calculate, save colors to file to re-use
    if (not isFileExist and doColors):
        print "Saving colors to file for future use"
        np.savetxt(color_file, colors)

    if doColors:
        return waveLen, spectra, colors
    else:
        return waveLen, spectra
        return False


#### Real code starts here

print "\n\n\n ***Warning*** --- this program takes a few minutes to run --- \n\n"

# filter set of choice is the LSST filters
listOfFilters = 'LSST.filters'
pathToFilters = '../filter_data'

# create a dictionary: keyword is filter name, value is a Filter object
filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters)

# return the filter names
filterList = sedFilter.orderFiltersByLamEff(filterDict)
print 'Filter list = ', filterList
nFilter = len(filterList)

# load in elliptical SED (1st col: wavelength in A, 2nd col: fluxes in wl units)
seddata = np.loadtxt('../sed_data/El_B2004a.sed')

# turn into SED object
sed = sedFilter.SED(seddata[:, 0], seddata[:, 1])

# instantiate photometry calculations

# need cosmological model
h = 1.
omegamat = 0.3
omegaDE = 0.7
import sedFilter
import photometry as phot
import matplotlib.pyplot as plt


# filter set of choice
listOfFilters = 'LSST.filters'
pathToFilters = '../filter_data'


# creates a dictionary: keyword is filter name, value is a Filter object
filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters)


# return the filter names
filterList = sedFilter.orderFiltersByLamEff(filterDict)
print 'Filter list = ', filterList
nFilter = len(filterList)


# redshift grid to calculate colors at
zmin = 0.
zmax = 2.6
nz = 100
dz = (zmax-zmin)/(nz-1.)


# load in CWW templates
sedList = ['../sed_data/El_B2004a.sed','../sed_data/Sbc_B2004a.sed','../sed_data/Scd_B2004a.sed',
           '../sed_data/Im_B2004a.sed']
Пример #14
0
import numpy as np
import cphotometry as cphot
import sedFilter
import cosmo
import photErrorModel as pem

# Single SED for testing
fname = os.path.join(os.path.dirname(__file__), '../sed_data/El_B2004a.sed')
seddata = np.loadtxt(fname)
TEST_SED = sedFilter.SED(seddata[:, 0], seddata[:, 1])

# List of filters for testing
listOfFilters = 'LSST.filters'
pathToFilters = '../filter_data'
TEST_FILTERDICT = sedFilter.createFilterDict(listOfFilters, pathToFilters)
FILTERLIST = sedFilter.orderFiltersByLamEff(TEST_FILTERDICT)

# List of SEDs for testing
SEDLIST = []
sedfiles = [
    '../sed_data/El_B2004a.sed', '../sed_data/Sbc_B2004a.sed',
    '../sed_data/Scd_B2004a.sed', '../sed_data/Im_B2004a.sed'
]
for name in sedfiles:
    seddata = np.loadtxt(name)
    SEDLIST.append(sedFilter.SED(seddata[:, 0], seddata[:, 1]))

# Cosmological model
COSMOMODEL = cosmo.cosmologyCalculator()