예제 #1
0
    def __init__(self, filename, w0, w1, nw):
        self.filename = filename
        self.w0 = w0
        self.w1 = w1
        self.nw = nw
        self.numSplinePoints = 13
        self.processingTools = ProcessingTools()
        self.readSpectrumFile = ReadSpectrumFile(filename, w0, w1, nw)
        self.preProcess = PreProcessSpectrum(w0, w1, nw)

        self.spectrum = self.readSpectrumFile.file_extension()
예제 #2
0
 def __init__(self, snFile, galFile, w0, w1, nw):
     self.w0 = w0
     self.w1 = w1
     self.nw = nw
     self.numSplinePoints = 13
     self.processingTools = ProcessingTools()
     self.snReadSpectrumFile = ReadSpectrumFile(snFile, w0, w1, nw)
     self.galReadSpectrumFile = ReadSpectrumFile(galFile, w0, w1, nw)
     self.snSpectrum = self.snReadSpectrumFile.file_extension()
     self.snWave, self.snFluxes, self.ncols, self.ages, self.ttype, self.splineInfo = self.snSpectrum
     self.galSpectrum = self.galReadSpectrumFile.file_extension()
     self.preProcess = PreProcessSpectrum(w0, w1, nw)
예제 #3
0
    def augment_data(self, flux, stdDevMean=0.05, stdDevStdDev=0.05):
        minIndex, maxIndex = ProcessingTools().min_max_index(flux, outerVal=0.5)
        noise = np.zeros(self.nw)
        stdDev = abs(np.random.normal(stdDevMean, stdDevStdDev)) # randomised standard deviation
        noise[minIndex:maxIndex] = np.random.normal(0, stdDev, maxIndex - minIndex)
        # # Add white noise to regions outside minIndex to maxIndex
        # noise[0:minIndex] = np.random.uniform(0.0, 1.0, minIndex)
        # noise[maxIndex:] = np.random.uniform(0.0, 1.0, self.nw-maxIndex)

        augmentedFlux = flux + noise
        augmentedFlux = normalise_spectrum(augmentedFlux)
        augmentedFlux = zero_non_overlap_part(augmentedFlux, minIndex, maxIndex, outerVal=0.5)

        return augmentedFlux
예제 #4
0
class CombineSnAndHost(object):
    def __init__(self, snInfo, galInfo, w0, w1, nw):
        self.snInfo = snInfo
        self.galInfo = galInfo
        self.processingTools = ProcessingTools()
        self.numSplinePoints = 13
        self.preProcess = PreProcessSpectrum(w0, w1, nw)

    def overlapped_spectra(self):
        snWave, snFlux, snMinIndex, snMaxIndex = self.snInfo
        galWave, galFlux, galMinIndex, galMaxIndex = self.galInfo

        minIndex = max(snMinIndex, galMinIndex)
        maxIndex = min(snMaxIndex, galMaxIndex)

        snFlux = zero_non_overlap_part(snFlux, minIndex, maxIndex)
        galFlux = zero_non_overlap_part(galFlux, minIndex, maxIndex)

        return snWave, snFlux, galWave, galFlux, minIndex, maxIndex

    def sn_plus_gal(self, snCoeff, galCoeff):
        snWave, snFlux, galWave, galFlux, minIndex, maxIndex = self.overlapped_spectra(
        )

        combinedFlux = (snCoeff * snFlux) + (galCoeff * galFlux)

        return snWave, combinedFlux, minIndex, maxIndex

    def template_data(self, snCoeff, galCoeff, z):
        wave, flux, minIndex, maxIndex = self.sn_plus_gal(snCoeff, galCoeff)
        wave, flux = self.processingTools.redshift_spectrum(wave, flux, z)
        flux = zero_non_overlap_part(flux, minIndex, maxIndex, outerVal=0)
        binnedWave, binnedFlux, minIndex, maxIndex = self.preProcess.log_wavelength(
            wave[minIndex:maxIndex + 1], flux[minIndex:maxIndex + 1])
        newFlux, continuum = self.preProcess.continuum_removal(
            binnedWave, binnedFlux, self.numSplinePoints, minIndex, maxIndex)
        meanZero = self.preProcess.mean_zero(newFlux, minIndex, maxIndex)
        apodized = self.preProcess.apodize(meanZero, minIndex, maxIndex)
        fluxNorm = normalise_spectrum(apodized)
        fluxNorm = zero_non_overlap_part(fluxNorm,
                                         minIndex,
                                         maxIndex,
                                         outerVal=0.5)
        # Could  median filter here, but trying without it now

        return binnedWave, fluxNorm, (minIndex, maxIndex)
예제 #5
0
class PreProcessing(object):
    """ Pre-processes spectra for cross correlation """
    def __init__(self, filename, w0, w1, nw):
        self.filename = filename
        self.w0 = w0
        self.w1 = w1
        self.nw = nw
        self.numSplinePoints = 13
        self.processingTools = ProcessingTools()
        self.readSpectrumFile = ReadSpectrumFile(filename, w0, w1, nw)
        self.preProcess = PreProcessSpectrum(w0, w1, nw)

        self.spectrum = self.readSpectrumFile.file_extension()

    def galaxy_template(self, z):
        self.wave, self.flux = self.spectrum
        wave, flux = self.readSpectrumFile.two_col_input_spectrum(
            self.wave, self.flux, z)
        binnedwave, binnedflux, minindex, maxindex = self.preProcess.log_wavelength(
            wave, flux)
        newflux, continuum = self.preProcess.continuum_removal(
            binnedwave, binnedflux, self.numSplinePoints, minindex, maxindex)
        meanzero = self.preProcess.mean_zero(binnedwave, newflux, minindex,
                                             maxindex)
        apodized = self.preProcess.apodize(binnedwave, meanzero, minindex,
                                           maxindex)

        plt.figure('Galaxy_SB1')
        plt.plot(wave, flux, label='original')
        plt.plot(binnedwave, binnedflux, label='binned')
        plt.plot(binnedwave, continuum, label='continuum')
        plt.plot(binnedwave, newflux, label='continuumSubtracted')
        plt.plot(binnedwave, meanzero, label='meanzero')
        plt.plot(binnedwave, apodized, label='apodized')
        plt.legend()
        #plt.show()

        return binnedwave, apodized, minindex, maxindex

    def two_column_data(self, z, smooth, minWave, maxWave):
        self.wave, self.flux = self.spectrum
        self.flux = limit_wavelength_range(self.wave, self.flux, minWave,
                                           maxWave)
        self.wDensity = (self.w1 -
                         self.w0) / self.nw  # Average wavelength spacing
        wavelengthDensity = (max(self.wave) - min(self.wave)) / len(self.wave)

        filterSize = int(
            self.wDensity / wavelengthDensity * smooth / 2) * 2 + 1
        preFiltered = medfilt(self.flux, kernel_size=filterSize)
        wave, flux = self.readSpectrumFile.two_col_input_spectrum(
            self.wave, preFiltered, z)
        binnedwave, binnedflux, minindex, maxindex = self.preProcess.log_wavelength(
            wave, flux)
        newflux, continuum = self.preProcess.continuum_removal(
            binnedwave, binnedflux, self.numSplinePoints, minindex, maxindex)
        meanzero = self.preProcess.mean_zero(binnedwave, newflux, minindex,
                                             maxindex)
        apodized = self.preProcess.apodize(binnedwave, meanzero, minindex,
                                           maxindex)

        #filterSize = smooth * 2 + 1
        medianFiltered = medfilt(apodized, kernel_size=1)  #filterSize)

        # from scipy.interpolate import interp1d
        #
        # plt.plot(self.flux)
        #
        # spline = interp1d(binnedwave[minindex:maxindex], binnedflux[minindex:maxindex], kind='cubic')
        # waveSpline = np.linspace(binnedwave[minindex],binnedwave[maxindex-1],num=self.numSplinePoints)
        # print(spline)
        # print('###')
        # print(spline(binnedwave[minindex:maxindex]))
        # plt.figure('1')
        # plt.plot(waveSpline, spline(waveSpline), '--', label='spline')
        #
        # print(wave)
        # print(binnedwave)
        # print(binnedflux)
        # print(len(binnedwave))
        # plt.plot(wave,flux)
        # plt.figure('2')
        # plt.plot(binnedwave, binnedflux, label='binned')
        # plt.plot(binnedwave, newflux, label='continuumSubtract1')
        # plt.plot(binnedwave, continuum, label='polyfit1')
        # print(len(binnedwave))
        # print((min(binnedwave), max(binnedwave)))
        # print(len(newflux))
        #
        # #newflux2, poly2 = self.preProcess.continuum_removal(binnedwave, binnedflux, 6, minindex, maxindex)
        # #plt.plot(binnedwave, newflux2, label='continuumSubtract2')
        # #plt.plot(binnedwave, poly2, label='polyfit2')
        # plt.plot(binnedwave, apodized, label='taper')
        # plt.legend()
        # plt.figure('filtered')
        # plt.plot(medianFiltered)
        # plt.figure('3')
        # plt.plot(medfilt(apodized,kernel_size=3))
        # plt.show()

        return binnedwave, medianFiltered, minindex, maxindex

    def snid_template_data(self, ageIdx, z):
        """lnw templates """
        wave, fluxes, ncols, ages, ttype, splineInfo = self.spectrum
        wave, flux = self.processingTools.redshift_spectrum(
            wave, fluxes[ageIdx], z)
        binnedwave, binnedflux, minindex, maxindex = self.preProcess.log_wavelength(
            wave, flux)
        medianFiltered = medfilt(binnedflux, kernel_size=1)

        return binnedwave, medianFiltered, ncols, ages, ttype, minindex, maxindex
예제 #6
0
 def __init__(self, snInfo, galInfo, w0, w1, nw):
     self.snInfo = snInfo
     self.galInfo = galInfo
     self.processingTools = ProcessingTools()
     self.numSplinePoints = 13
     self.preProcess = PreProcessSpectrum(w0, w1, nw)
예제 #7
0
class CombineSnAndHost(object):
    def __init__(self, snFile, galFile, w0, w1, nw):
        self.w0 = w0
        self.w1 = w1
        self.nw = nw
        self.numSplinePoints = 13
        self.processingTools = ProcessingTools()
        self.snReadSpectrumFile = ReadSpectrumFile(snFile, w0, w1, nw)
        self.galReadSpectrumFile = ReadSpectrumFile(galFile, w0, w1, nw)
        self.snSpectrum = self.snReadSpectrumFile.file_extension()
        self.snWave, self.snFluxes, self.ncols, self.ages, self.ttype, self.splineInfo = self.snSpectrum
        self.galSpectrum = self.galReadSpectrumFile.file_extension()
        self.preProcess = PreProcessSpectrum(w0, w1, nw)

    def snid_sn_template_data(self, ageIdx):
        # Undo continuum in the following step in preprocessing.py
        wave, flux = self.snReadSpectrumFile.snid_template_undo_processing(self.snWave, self.snFluxes[ageIdx], self.splineInfo, ageIdx)

        binnedWave, binnedFlux, minIndex, maxIndex = self.preProcess.log_wavelength(wave, flux)
        binnedFluxNorm = normalise_spectrum(binnedFlux)

        return binnedWave, binnedFluxNorm, minIndex, maxIndex

    def gal_template_data(self):
        wave, flux = self.galSpectrum

        # Limit bounds from w0 to w1 and normalise flux
        wave, flux = self.galReadSpectrumFile.two_col_input_spectrum(wave, flux, z=0)
        binnedWave, binnedFlux, minIndex, maxIndex = self.preProcess.log_wavelength(wave, flux)
        binnedFluxNorm = normalise_spectrum(binnedFlux)

        return binnedWave, binnedFluxNorm, minIndex, maxIndex

    def overlapped_spectra(self, snAgeIdx):
        snWave, snFlux, snMinIndex, snMaxIndex = self.snid_sn_template_data(snAgeIdx)
        galWave, galFlux, galMinIndex, galMaxIndex = self.gal_template_data()

        minIndex = max(snMinIndex, galMinIndex)
        maxIndex = min(snMaxIndex, galMaxIndex)

        snFlux = zero_non_overlap_part(snFlux, minIndex, maxIndex)
        galFlux = zero_non_overlap_part(galFlux, minIndex, maxIndex)

        return snWave, snFlux, galWave, galFlux, minIndex, maxIndex

    def sn_plus_gal(self, snCoeff, galCoeff, snAgeIdx):
        snWave, snFlux, galWave, galFlux, minIndex, maxIndex = self.overlapped_spectra(snAgeIdx)

        combinedFlux = (snCoeff * snFlux) + (galCoeff * galFlux)

        return snWave, combinedFlux, minIndex, maxIndex

    def training_template_data(self, snAgeIdx, snCoeff, galCoeff, z):
        wave, flux, minIndex, maxIndex = self.sn_plus_gal(snCoeff, galCoeff, snAgeIdx)
        wave, flux = self.processingTools.redshift_spectrum(wave, flux, z)
        binnedWave, binnedFlux, minIndex, maxIndex = self.preProcess.log_wavelength(wave, flux)
        newFlux, continuum = self.preProcess.continuum_removal(binnedWave, binnedFlux, self.numSplinePoints, minIndex, maxIndex)
        meanZero = self.preProcess.mean_zero(binnedWave, newFlux, minIndex, maxIndex)
        apodized = self.preProcess.apodize(binnedWave, meanZero, minIndex, maxIndex)
        # fluxNorm = self._normalise_spectrum(apodized) # This happens in create_arrays anyway
        # Could  median filter here, but trying without it now

        return binnedWave, apodized, minIndex, maxIndex, self.ncols, self.ages, self.ttype