Пример #1
0
    def __init__(self,
                 f_mod_interp,
                 bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
                 lines_pos, lines_width,
                 var_C, var_L, alpha_C, alpha_L,
                 redshiftGridGP,
                 use_interpolators=True,
                 lambdaRef=4.5e3,
                 g_AB=1.0):

        DL = approx_DL()
        self.bands = np.arange(bandCoefAmplitudes.shape[0])
        if isinstance(f_mod_interp, int):
            self.mean_fct = None
            self.nt = f_mod_interp
        else:
            self.mean_fct = Photoz_linear_sed_basis(f_mod_interp)
            self.nt = f_mod_interp.shape[0]
        # self.mean_fct = Photoz_mean_function(
        #    alpha, bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
        #    g_AB=g_AB, lambdaRef=lambdaRef, DL_z=DL)
        self.kernel = Photoz_kernel(
            bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
            lines_pos, lines_width, var_C, var_L, alpha_C, alpha_L,
            g_AB=g_AB, DL_z=DL, redshiftGrid=redshiftGridGP,
            use_interpolators=use_interpolators)
        self.redshiftGridGP = redshiftGridGP
Пример #2
0
    def __init__(self,
                 bandCoefAmplitudes,
                 bandCoefPositions,
                 bandCoefWidths,
                 lines_pos,
                 lines_width,
                 var_C,
                 var_L,
                 alpha_T,
                 alpha_C,
                 alpha_L,
                 redshiftGridGP,
                 use_interpolators=True,
                 lambdaRef=4.5e3,
                 g_AB=1.0):

        DL = approx_DL()
        self.bands = np.arange(bandCoefAmplitudes.shape[0])
        self.kernel = Photoz_SN_kernel(bandCoefAmplitudes,
                                       bandCoefPositions,
                                       bandCoefWidths,
                                       lines_pos,
                                       lines_width,
                                       var_C,
                                       var_L,
                                       alpha_T,
                                       alpha_C,
                                       alpha_L,
                                       g_AB=g_AB,
                                       DL_z=DL,
                                       redshiftGrid=redshiftGridGP,
                                       use_interpolators=use_interpolators)
        self.redshiftGridGP = redshiftGridGP
Пример #3
0
    def __init__(self,
                 f_mod_interp,
                 bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
                 lines_pos, lines_width,
                 var_C, var_L, alpha_C, alpha_L,
                 redshiftGridGP,
                 use_interpolators=True,
                 lambdaRef=4.5e3,
                 g_AB=1.0):

        DL = approx_DL()
        self.bands = np.arange(bandCoefAmplitudes.shape[0])
        if isinstance(f_mod_interp, int):
            self.mean_fct = None
            self.nt = f_mod_interp
        else:
            self.mean_fct = Photoz_linear_sed_basis(f_mod_interp)
            self.nt = f_mod_interp.shape[0]
        # self.mean_fct = Photoz_mean_function(
        #    alpha, bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
        #    g_AB=g_AB, lambdaRef=lambdaRef, DL_z=DL)
        self.kernel = Photoz_kernel(
            bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
            lines_pos, lines_width, var_C, var_L, alpha_C, alpha_L,
            g_AB=g_AB, DL_z=DL, redshiftGrid=redshiftGridGP,
            use_interpolators=use_interpolators)
        self.redshiftGridGP = redshiftGridGP
Пример #4
0
 def __init__(self,
              alpha,
              fcoefs_amp,
              fcoefs_mu,
              fcoefs_sig,
              g_AB=1.0,
              lambdaRef=4.5e3,
              DL_z=None,
              name='photoz_mf'):
     """ Constructor."""
     # If luminosity_distance function not provided, use approximation
     if DL_z is None:
         self.DL_z = approx_DL()
     else:
         self.DL_z = DL_z
     self.g_AB = g_AB
     assert lambdaRef > 1e2 and lambdaRef < 1e5
     self.lambdaRef = lambdaRef
     self.fourpi = 4 * np.pi
     self.sqrthalfpi = np.sqrt(np.pi / 2)
     self.alpha = alpha
     assert fcoefs_amp.shape[0] == fcoefs_mu.shape[0] and\
         fcoefs_amp.shape[0] == fcoefs_sig.shape[0]
     self.fcoefs_amp = np.array(fcoefs_amp)
     self.fcoefs_mu = np.array(fcoefs_mu)
     self.fcoefs_sig = np.array(fcoefs_sig)
     self.numCoefs = fcoefs_amp.shape[1]
     self.norms = np.sqrt(2*np.pi)\
         * np.sum(self.fcoefs_amp * self.fcoefs_sig / self.fcoefs_mu, axis=1)
     self.fcoefs_amp *= self.fcoefs_mu
Пример #5
0
 def __init__(self, numTypes, maglim=None):
     self.numTypes = numTypes
     self.params = OrderedDict({})
     self.paramranges = OrderedDict({})
     for i in range(numTypes - 1):
         self.params['pt' + str(i + 1)] = 0.5
         self.paramranges['pt' + str(i + 1)] = [0.2, 0.9]
     if maglim is not None:
         self.maglim = maglim
         self.DL = approx_DL()
     else:
         self.maglim = None
     self.lumFct = powerLawLuminosityFct()  # p(lum | z)
     # p(z, t)
     self.nofz = ComovingVolumeEfect()
     self.children = [self.lumFct] + [self.nofz]
Пример #6
0
    def __init__(self,
                 bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
                 lines_pos, lines_width,
                 var_C, var_L, alpha_T, alpha_C, alpha_L,
                 redshiftGridGP,
                 use_interpolators=True,
                 lambdaRef=4.5e3,
                 g_AB=1.0):

        DL = approx_DL()
        self.bands = np.arange(bandCoefAmplitudes.shape[0])
        self.kernel = Photoz_SN_kernel(
            bandCoefAmplitudes, bandCoefPositions, bandCoefWidths,
            lines_pos, lines_width, var_C, var_L, alpha_T, alpha_C, alpha_L,
            g_AB=g_AB, DL_z=DL, redshiftGrid=redshiftGridGP,
            use_interpolators=use_interpolators)
        self.redshiftGridGP = redshiftGridGP
Пример #7
0
    def __init__(self,
                 tabulatedWavelength,
                 tabulatedSpectrum,
                 photometricBands,
                 redshiftGrid=None,
                 order=15):
        self.DL = approx_DL()
        self.photometricBands = photometricBands
        self.numBands = len(photometricBands)
        self.sed_interp = interp1d(tabulatedWavelength,
                                   tabulatedSpectrum,
                                   bounds_error=False,
                                   fill_value="extrapolate")
        if redshiftGrid is None:
            self.redshiftGrid = np.logspace(np.log10(1e-2), np.log10(2.0), 350)
        else:
            self.redshiftGrid = redshiftGrid

        self.fbcoefs = {}
        self.fbinterps = {}
        self.logfbinterps = {}
        self.order = order
        self.fmodgrid = np.zeros(
            (self.redshiftGrid.size, len(photometricBands)))
        self.bandNames = []
        for ib, filt in enumerate(photometricBands):
            self.bandNames.append(filt.bandName)
            for iz in range(self.redshiftGrid.size):
                opz = (self.redshiftGrid[iz] + 1)
                xf_z = filt.wavelengthGrid / opz
                yf_z = filt.tabulatedResponse
                ysed = self.sed_interp(xf_z)
                facz = opz**2. / (4 * np.pi *
                                  self.DL(self.redshiftGrid[iz])**2.)
                ysedext = facz * ysed
                self.fmodgrid[iz, ib] =\
                    np.trapz(ysedext * yf_z, x=xf_z) / filt.norm
            self.fbinterps[filt.bandName] = UnivariateSpline(self.redshiftGrid,
                                                             self.fmodgrid[:,
                                                                           ib],
                                                             s=0)
            self.fbcoefs[filt.bandName] = np.polyfit(
                self.redshiftGrid, np.log(self.fmodgrid[:, ib]),
                self.order - 1)
            self.logfbinterps[filt.bandName] =\
                np.poly1d(self.fbcoefs[filt.bandName])
Пример #8
0
 def __init__(self,
              fcoefs_amp,
              fcoefs_mu,
              fcoefs_sig,
              lines_mu,
              lines_sig,
              var_C,
              var_L,
              alpha_C,
              alpha_L,
              g_AB=1.0,
              DL_z=None,
              redshiftGrid=None,
              use_interpolators=True):
     """ Constructor."""
     self.use_interpolators = use_interpolators
     if DL_z is None:
         self.DL_z = approx_DL()
     else:
         self.DL_z = DL_z
     self.g_AB = g_AB
     self.fourpi = 4 * np.pi
     self.lines_mu = np.array(lines_mu)
     self.lines_sig = np.array(lines_sig)
     self.numLines = self.lines_mu.size
     assert fcoefs_amp.shape[0] == fcoefs_mu.shape[0] and\
         fcoefs_amp.shape[0] == fcoefs_sig.shape[0]
     self.fcoefs_amp = fcoefs_amp
     self.fcoefs_mu = fcoefs_mu
     self.fcoefs_sig = fcoefs_sig
     self.numCoefs = fcoefs_amp.shape[1]
     self.numBands = fcoefs_amp.shape[0]
     self.norms = np.sqrt(2*np.pi)\
         * np.sum(self.fcoefs_amp * self.fcoefs_sig, axis=1)
     # Initialize parameters and link them.
     self.var_C = var_C
     self.var_L = var_L
     self.alpha_C = alpha_C
     self.alpha_L = alpha_L
     if redshiftGrid is None:
         self.redshiftGrid = np.linspace(0, 4, num=160)
     else:
         self.redshiftGrid = copy(redshiftGrid)
     self.nz = self.redshiftGrid.size
     self.construct_interpolators()
Пример #9
0
    def __init__(self,
                 tabulatedWavelength,
                 tabulatedSpectrum,
                 photometricBands,
                 redshiftGrid=None,
                 order=15):
        self.DL = approx_DL()
        self.photometricBands = photometricBands
        self.numBands = len(photometricBands)
        self.sed_interp = interp1d(tabulatedWavelength, tabulatedSpectrum)
        if redshiftGrid is None:
            self.redshiftGrid = np.logspace(np.log10(1e-2), np.log10(2.0), 350)
        else:
            self.redshiftGrid = redshiftGrid

        self.fbcoefs = {}
        self.fbinterps = {}
        self.order = order
        for filt in photometricBands:
            fmodgrid = np.zeros((self.redshiftGrid.size, ))
            for iz in range(self.redshiftGrid.size):
                opz = (self.redshiftGrid[iz] + 1)
                xf_z = filt.wavelengthGrid / opz
                yf_z = filt.tabulatedResponse
                ysed = self.sed_interp(xf_z)
                facz = opz**2. / (4 * np.pi *
                                  self.DL(self.redshiftGrid[iz])**2.)
                ysedext = facz * ysed
                fmodgrid[iz] =\
                    np.trapz(ysedext * yf_z, x=xf_z) / filt.norm
            # self.fbinterps[filt.bandName] = UnivariateSpline(
            #    self.redshiftGrid, fmodgrid, s=0)
            self.fbcoefs[filt.bandName] = np.polyfit(self.redshiftGrid,
                                                     np.log(fmodgrid),
                                                     self.order - 1)
            self.fbinterps[filt.bandName] =\
                np.poly1d(self.fbcoefs[filt.bandName])
Пример #10
0
def getDataFromFile(params,
                    firstLine,
                    lastLine,
                    prefix="",
                    ftype="catalog",
                    getXY=True,
                    CV=False):
    """
    Returns an iterator to parse an input catalog file.
    Returns the fluxes, redshifts, etc, and also GP inputs if getXY=True.
    """

    if ftype == "gpparams":

        with open(params[prefix + 'paramFile']) as f:
            for line in itertools.islice(f, firstLine, lastLine):
                data = np.fromstring(line, dtype=float, sep=' ')
                B = int(data[0])
                z = data[1]
                ell = data[2]
                bands = data[3:3 + B]
                flatarray = data[3 + B:]
                X = np.zeros((B, 3))
                for off, iband in enumerate(bands):
                    X[off, 0] = iband
                    X[off, 1] = z
                    X[off, 2] = ell

                yield z, ell, bands, X, B, flatarray

    if ftype == "catalog":

        DL = approx_DL()
        bandIndices, bandNames, bandColumns, bandVarColumns, redshiftColumn,\
            refBandColumn = readColumnPositions(params, prefix=prefix)
        bandCoefAmplitudes, bandCoefPositions, bandCoefWidths, norms\
            = readBandCoefficients(params)
        refBandNorm = norms[params['bandNames'].index(params[prefix +
                                                             'referenceBand'])]

        if CV:
            bandIndicesCV, bandNamesCV, bandColumnsCV,\
                bandVarColumnsCV, redshiftColumnCV =\
                readColumnPositions(params, prefix=prefix+'CV_', refFlux=False)

        with open(params[prefix + 'catFile']) as f:
            for line in itertools.islice(f, firstLine, lastLine):

                data = np.array(line.split(' '), dtype=float)
                refFlux = data[refBandColumn]
                normedRefFlux = refFlux * refBandNorm
                if redshiftColumn >= 0:
                    z = data[redshiftColumn]
                else:
                    z = -1

                # drop bad values and find how many bands are valid
                mask = np.isfinite(data[bandColumns])
                mask &= np.isfinite(data[bandVarColumns])
                mask &= data[bandColumns] > 0.0
                mask &= data[bandVarColumns] > 0.0
                bandsUsed = np.where(mask)[0]
                numBandsUsed = mask.sum()

                if z > -1:
                    ell = normedRefFlux * 4 * np.pi \
                        * params['fluxLuminosityNorm'] * DL(z)**2 * (1+z)

                if (refFlux <= 0) or (not np.isfinite(refFlux))\
                        or (z < 0) or (numBandsUsed <= 1):
                    print("Skipping galaxy: refflux=", refFlux, "z=", z,
                          "numBandsUsed=", numBandsUsed)
                    continue  # not valid data - skip to next valid object

                fluxes = data[bandColumns[mask]]
                fluxesVar = data[bandVarColumns[mask]] +\
                    (params['training_extraFracFluxError'] * fluxes)**2

                if CV:
                    maskCV = np.isfinite(data[bandColumnsCV])
                    maskCV &= np.isfinite(data[bandVarColumnsCV])
                    maskCV &= data[bandColumnsCV] > 0.0
                    maskCV &= data[bandVarColumnsCV] > 0.0
                    bandsUsedCV = np.where(maskCV)[0]
                    numBandsUsedCV = maskCV.sum()
                    fluxesCV = data[bandColumnsCV[maskCV]]
                    fluxesCVVar = data[bandVarColumnsCV[maskCV]] +\
                        (params['training_extraFracFluxError'] * fluxesCV)**2

                if not getXY:

                    if CV:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            bandIndicesCV[maskCV], fluxesCV, fluxesCVVar
                    else:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            None, None, None

                if getXY:

                    Y = np.zeros((numBandsUsed, 1))
                    Yvar = np.zeros((numBandsUsed, 1))
                    X = np.ones((numBandsUsed, 3))
                    for off, iband in enumerate(bandIndices[mask]):
                        X[off, 0] = iband
                        X[off, 1] = z
                        X[off, 2] = ell
                        Y[off, 0] = fluxes[off]
                        Yvar[off, 0] = fluxesVar[off]

                    if CV:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            bandIndicesCV[maskCV], fluxesCV, fluxesCVVar,\
                            X, Y, Yvar
                    else:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            None, None, None,\
                            X, Y, Yvar
Пример #11
0
def getDataFromFile(params, firstLine, lastLine,
                    prefix="", ftype="catalog", getXY=True, CV=False):
    """
    Returns an iterator to parse an input catalog file.
    Returns the fluxes, redshifts, etc, and also GP inputs if getXY=True.
    """

    if ftype == "gpparams":

        with open(params[prefix+'paramFile']) as f:
            for line in itertools.islice(f, firstLine, lastLine):
                data = np.fromstring(line, dtype=float, sep=' ')
                B = int(data[0])
                z = data[1]
                ell = data[2]
                bands = data[3:3+B]
                flatarray = data[3+B:]
                X = np.zeros((B, 3))
                for off, iband in enumerate(bands):
                    X[off, 0] = iband
                    X[off, 1] = z
                    X[off, 2] = ell

                yield z, ell, bands, X, B, flatarray

    if ftype == "catalog":

        DL = approx_DL()
        bandIndices, bandNames, bandColumns, bandVarColumns, redshiftColumn,\
            refBandColumn = readColumnPositions(params, prefix=prefix)
        bandCoefAmplitudes, bandCoefPositions, bandCoefWidths, norms\
            = readBandCoefficients(params)
        refBandNorm = norms[params['bandNames']
                            .index(params[prefix+'referenceBand'])]

        if CV:
            bandIndicesCV, bandNamesCV, bandColumnsCV,\
                bandVarColumnsCV, redshiftColumnCV =\
                readColumnPositions(params, prefix=prefix+'CV_', refFlux=False)

        with open(params[prefix+'catFile']) as f:
            for line in itertools.islice(f, firstLine, lastLine):

                data = np.array(line.split(' '), dtype=float)
                refFlux = data[refBandColumn]
                normedRefFlux = refFlux * refBandNorm
                if redshiftColumn >= 0:
                    z = data[redshiftColumn]
                else:
                    z = -1

                # drop bad values and find how many bands are valid
                mask = np.isfinite(data[bandColumns])
                mask &= np.isfinite(data[bandVarColumns])
                mask &= data[bandColumns] > 0.0
                mask &= data[bandVarColumns] > 0.0
                bandsUsed = np.where(mask)[0]
                numBandsUsed = mask.sum()

                if z > -1:
                    ell = normedRefFlux * 4 * np.pi \
                        * params['fluxLuminosityNorm'] * DL(z)**2 * (1+z)

                if (refFlux <= 0) or (not np.isfinite(refFlux))\
                        or (z < 0) or (numBandsUsed <= 1):
                    print("Skipping galaxy: refflux=", refFlux,
                          "z=", z, "numBandsUsed=", numBandsUsed)
                    continue  # not valid data - skip to next valid object

                fluxes = data[bandColumns[mask]]
                fluxesVar = data[bandVarColumns[mask]] +\
                    (params['training_extraFracFluxError'] * fluxes)**2

                if CV:
                    maskCV = np.isfinite(data[bandColumnsCV])
                    maskCV &= np.isfinite(data[bandVarColumnsCV])
                    maskCV &= data[bandColumnsCV] > 0.0
                    maskCV &= data[bandVarColumnsCV] > 0.0
                    bandsUsedCV = np.where(maskCV)[0]
                    numBandsUsedCV = maskCV.sum()
                    fluxesCV = data[bandColumnsCV[maskCV]]
                    fluxesCVVar = data[bandVarColumnsCV[maskCV]] +\
                        (params['training_extraFracFluxError'] * fluxesCV)**2

                if not getXY:

                    if CV:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            bandIndicesCV[maskCV], fluxesCV, fluxesCVVar
                    else:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            None, None, None

                if getXY:

                    Y = np.zeros((numBandsUsed, 1))
                    Yvar = np.zeros((numBandsUsed, 1))
                    X = np.ones((numBandsUsed, 3))
                    for off, iband in enumerate(bandIndices[mask]):
                        X[off, 0] = iband
                        X[off, 1] = z
                        X[off, 2] = ell
                        Y[off, 0] = fluxes[off]
                        Yvar[off, 0] = fluxesVar[off]

                    if CV:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            bandIndicesCV[maskCV], fluxesCV, fluxesCVVar,\
                            X, Y, Yvar
                    else:
                        yield z, normedRefFlux,\
                            bandIndices[mask], fluxes, fluxesVar,\
                            None, None, None,\
                            X, Y, Yvar