Example #1
0
def bolEstm(obj, sig, width):
    '''
    peakFinder.bolEstm(obj, sig, width)
    =============================
    Performs a SN estimation at each wavelength following the max-likelihood
    approach of Bolton et al. (2012).  

    Parameters:
        obj: The SDSS object/spectra on which applied the subtraction
        sig: Width of the gaussian kernel
        width: Width of the convolutional window
    Returns:
        SN: The SN at each wavelength as an array. Beginning and end are filled
        with null values due to the convolution.
    '''
    NormGauss = gauss(np.linspace(-width * 0.5, width * 0.5, width), 0.0, 1.0,
                      sig**2.0)
    NormGauss = NormGauss / np.sum(NormGauss)
    Cj1 = np.array([
        np.sum(
            kernel(j + 0.5 * width, width, NormGauss, len(obj.wave)) *
            obj.reduced_flux * obj.ivar)
        for j in range(int(len(obj.wave) - width))
    ])
    Cj2 = np.array([
        np.sum(obj.ivar *
               kernel(j + 0.5 * width, width, NormGauss, len(obj.wave))**2.0)
        for j in range(int(len(obj.wave) - width))
    ])
    SN = np.zeros(len(obj.wave))
    SN[int(width * 0.5):int(width * 0.5 + len(Cj1))] = Cj1 / np.sqrt(Cj2)
    return SN
Example #2
0
def bolEstm(obj, sig, width):
    '''
    peakFinder.bolEstm(obj, sig, width)
    =============================
    Performs a SN estimation at each wavelength following the max-likelihood
    approach of Bolton et al. (2012).  

    Parameters:
        obj: The SDSS object/spectra on which applied the subtraction
        sig: Width of the gaussian kernel
        width: Width of the convolutional window
    Returns:
        SN: The SN at each wavelength as an array. Beginning and end are filled
        with null values due to the convolution.
    '''
    NormGauss = gauss(np.linspace(-width * 0.5, width * 0.5, width), 0.0, 1.0,
                      sig ** 2.0)
    NormGauss = NormGauss / np.sum(NormGauss)
    Cj1 = np.array([np.sum(kernel(j + 0.5 * width, width, NormGauss,
                                  len(obj.wave)) * obj.reduced_flux * obj.ivar)
                    for j in range(int(len(obj.wave) - width))])
    Cj2 = np.array([np.sum(obj.ivar * kernel(j + 0.5 * width, width, NormGauss,
                                             len(obj.wave)) ** 2.0)
                    for j in range(int(len(obj.wave) - width))])
    SN = np.zeros(len(obj.wave))
    SN[int(width * 0.5): int(width * 0.5 + len(Cj1))] = Cj1 / np.sqrt(Cj2)
    return SN
Example #3
0
def qsoContfit(obj, peak, searchLyA, sig, window_width=40):
    '''
    peakFinder.qsoContfit(obj, peak, searchLyA, window_width)
    =============================
    A function to fit the QSO continuum near a peak with a 3rd order polynomial 
    and subtract it. Add the new SN of the peak in its class.

    Parameters:
        obj: The SDSS object/spectra on which applied the subtraction
        peak: The inquired peak
        sig: Sigma of gaussian to perform SN max-likelihood 
        searchLyA: True if we search for background LAE, False for backgroung ELGs
        window_width: Half-width (Angstroms) on which the polynomial is fitted
    Returns:
        accept: True if the SN is still high after subtraction. False otherwise.
    '''
    x0 = peak.wavelength
    window = np.linspace(obj.wave2bin(x0) - window_width,
                         obj.wave2bin(x0) + window_width,
                         2 * window_width + 1,
                         dtype=np.int16)
    median_local = np.median(obj.reduced_flux[window])

    fit_QSO = np.poly1d(np.polyfit(x=obj.wave[window],y=obj.reduced_flux[window],deg=3, \
        w=(np.abs(obj.reduced_flux[window]-median_local)<5)*np.sqrt(obj.ivar[window])) )

    new_flux = obj.reduced_flux[window] - fit_QSO(obj.wave[window])

    NormGauss = gauss(
        np.linspace(-window_width * 0.5, window_width * 0.5, window_width),
        0.0, 1.0, sig**2.0)
    NormGauss = NormGauss / np.sum(NormGauss)
    cj1_new = np.sum(
        new_flux *
        kernel(int(len(window) / 2), window_width, NormGauss, len(new_flux)) *
        obj.ivar[window])
    cj2_new = np.sum(
        obj.ivar[window] *
        kernel(int(len(window) / 2), window_width, NormGauss, len(window))**2)
    SN_fitted = cj1_new / np.sqrt(cj2_new)

    if searchLyA and SN_fitted < 6:
        return False  #Reject
    elif searchLyA and SN_fitted > 6:
        peak.reduced_sn = SN_fitted
        obj.reduced_flux_QSO[window] = new_flux
    elif searchLyA == False and SN_fitted < 6:
        return False  # Reject
    elif searchLyA == False and SN_fitted > 6:
        peak.reduced_sn = SN_fitted
        obj.reduced_flux_QSO[window] = new_flux
    return True  # Accept
Example #4
0
def bolEstm(obj, sig, width):
    NormGauss = gauss(np.linspace(-width * 0.5, width * 0.5, width), 0.0, 1.0,
                      sig ** 2.0)
    NormGauss = NormGauss / np.sum(NormGauss)
    Cj1 = np.array([np.sum(kernel(j + 0.5 * width, width, NormGauss,
                                  len(obj.wave)) * obj.reduced_flux * obj.ivar)
                    for j in range(int(len(obj.wave) - width))])
    Cj2 = np.array([np.sum(obj.ivar * kernel(j + 0.5 * width, width, NormGauss,
                                             len(obj.wave)) ** 2.0)
                    for j in range(int(len(obj.wave) - width))])
    SN = np.zeros(len(obj.wave))
    SN[int(width * 0.5): int(width * 0.5 + len(Cj1))] = Cj1 / np.sqrt(Cj2)
    return SN
Example #5
0
    def _generate_table(self, max_rng):
        """
        Workhorse of GenCoeffs class, compute kernel
        and separate into rng specific coefficients.
        """
        # Calculate combined kernel of rng from 1 to max_rng:
        kers = kernel(max_rng)

        # Separate into kernels of each rng and flatten them:
        self._coeffs = [
            *reversed([
                *map(
                    GenCoeffs.flattened_rim,
                    map(
                        lambda slices: kers[slices],
                        zip(
                            repeat(...),
                            *tee(
                                chain((slice(None, None), ),
                                      map(lambda i: slice(i, -i),
                                          range(1, max_rng)))),
                        ))),
            ])
        ]

        self._g_scalers = [
            *map(
                lambda coeffs: 255.9 / (255 * np.hypot(*coeffs)),
                accumulate(
                    map(
                        lambda coeffs: np.maximum(coeffs, 0).sum(axis=1),
                        self._coeffs,
                    )))
        ]
        return self
def update_nn_weights_derivative_free_old(cloud, cost, lr, N, kernel_a, alpha,
                                          beta, gamma):

    #get flattened weights, nn shape and weight names
    cloudf, nn_shape, weight_names = flatten_weights(cloud, N)

    #compute kernels
    kernels = [[kernel(cloudf[i], cloudf[j], kernel_a) for j in range(N)]
               for i in range(N)]
    gkernels = [[gkernel(cloudf[i], cloudf[j], kernel_a) for j in range(N)]
                for i in range(N)]

    #plt.imshow(kernels,vmin=0,vmax=1)
    #plt.colorbar()

    #compute mean and standart deviation
    cloud_mean = np.mean(cloudf, axis=0)
    cloud_var = get_var(cloudf, cloud_mean)

    #compute gradient flows
    updates = []
    for nn in range(N):
        R = 0
        P = 0
        S = 0

        Q = [
            gkernels[nn][j] * cost[j] + kernels[nn][j] * cost[j] * np.divide(
                (cloudf[j] - cloud_mean), cloud_var) for j in range(N)
        ]
        Q = np.mean(Q, axis=0)

        if alpha > 0:
            R = [[kernels[nn][j] * (cloudf[j] - cloudf[k]) for j in range(N)]
                 for k in range(N)]
            R = [item for sublist in R
                 for item in sublist]  #Flatten list of lists
            R = np.sum(R, axis=0) * float(1 / N**2)

        if beta > 0:
            P = [gkernels[nn][j] for j in range(N)]
            P = np.mean(P, axis=0)

        if gamma > 0:
            S = [
                kernels[nn][j] * np.divide((cloudf[j] - cloud_mean), cloud_var)
                for j in range(N)
            ]
            S = np.mean(S, axis=0)

        updates.append(-lr * (Q + alpha * R + beta * P + gamma * S))

    #update flattened tensors
    for nn in range(N):
        cloudf[nn] = cloudf[nn] + updates[nn]

    #restore NN weight shapes
    new_nn_weights = unflatten_weights(cloudf, nn_shape, weight_names, N)

    return new_nn_weights, cloud_var
Example #7
0
def qsoContfit(obj, peak, searchLyA, sig, window_width = 40):
    '''
    peakFinder.qsoContfit(obj, peak, searchLyA, window_width)
    =============================
    A function to fit the QSO continuum near a peak with a 3rd order polynomial 
    and subtract it. Add the new SN of the peak in its class.

    Parameters:
        obj: The SDSS object/spectra on which applied the subtraction
        peak: The inquired peak
        sig: Sigma of gaussian to perform SN max-likelihood 
        searchLyA: True if we search for background LAE, False for backgroung ELGs
        window_width: Half-width (Angstroms) on which the polynomial is fitted
    Returns:
        accept: True if the SN is still high after subtraction. False otherwise.
    '''
    x0 = peak.wavelength
    window = np.linspace(obj.wave2bin(x0)-window_width,obj.wave2bin(x0)+window_width,2*window_width+1,dtype = np.int16)
    median_local = np.median(obj.reduced_flux[window])

    fit_QSO = np.poly1d(np.polyfit(x=obj.wave[window],y=obj.reduced_flux[window],deg=3, \
        w=(np.abs(obj.reduced_flux[window]-median_local)<5)*np.sqrt(obj.ivar[window])) )
    
    new_flux = obj.reduced_flux[window] - fit_QSO(obj.wave[window])
    
    NormGauss = gauss(np.linspace(-window_width * 0.5, window_width * 0.5, window_width), 0.0, 1.0,sig ** 2.0)
    NormGauss = NormGauss / np.sum(NormGauss)
    cj1_new = np.sum(new_flux*kernel(int(len(window)/2),window_width,NormGauss,len(new_flux))*obj.ivar[window])
    cj2_new = np.sum(obj.ivar[window]*kernel(int(len(window)/2),window_width,NormGauss,len(window))**2)
    SN_fitted = cj1_new/np.sqrt(cj2_new)

    if searchLyA and SN_fitted < 6:
        return False #Reject
    elif searchLyA and SN_fitted > 6:
        peak.reduced_sn = SN_fitted
        obj.reduced_flux_QSO[window]=new_flux
    elif searchLyA == False and SN_fitted < 6:
        return False  # Reject
    elif searchLyA == False and SN_fitted > 6:
        peak.reduced_sn = SN_fitted
        obj.reduced_flux_QSO[window]=new_flux
    return True  # Accept