Пример #1
0
    def onCalibnationFileOpen(self):
        filePath = QFileDialog.getOpenFileName(
            self, 'Open calibration file',
            './Spectroscopy_Example/20181023/combine/')[0]
        hdr, data = openFitData(filePath)
        self.selfImageCanvas.figure.clear()
        self.selfImageAx = self.selfImageCanvas.figure.add_subplot(111)
        zimshow(self.selfImageAx, data)
        self.selfImageCanvas.draw()

        self.imageWidth = int(data.shape[1])
        self.selfData = data
Пример #2
0
    def initUI(self):
        self.hbox = QHBoxLayout()

        self.fig = plt.Figure()
        self.canvas = FigureCanvas(self.fig)
        self.ax = self.fig.add_subplot(111)

        self.canvas.mpl_connect("button_press_event", self.on_press)
        self.canvas.mpl_connect("motion_notify_event", self.on_move)
        self.canvas.mpl_connect("button_release_event", self.on_release)

        self.hbox.addWidget(self.canvas)
        self.setLayout(self.hbox)

        if (self.filename !=''):
            self.data = fits.open(Path(self.filename))[0].data
            zimshow(self.ax, self.data)
        self.canvas.draw()
Пример #3
0
    def initUI(self):
        self.gridLayout = QGridLayout()
        self.setLayout(self.gridLayout)
        self.fig = plt.Figure()
        self.canvas = FigureCanvas(self.fig)
        self.ax = self.fig.add_subplot(111)

        self.gridLayout.addWidget(self.canvas, 0, 0, 1, -1)
        self.yesBtn = QPushButton('&Yes', self)
        self.noBtn = QPushButton('&No', self)
        self.gridLayout.addWidget(self.yesBtn, 1, 0)
        self.gridLayout.addWidget(self.noBtn, 1, 1)
        self.yesBtn.clicked.connect(self.onYes)
        self.noBtn.clicked.connect(self.onNo)
        if self.cropInfo.filename == '' : return
        else:
            self.data = fits.open(Path(self.cropInfo.filename))[0].data[self.cropInfo.y0:self.cropInfo.y1,
               self.cropInfo.x0:self.cropInfo.x1]
            zimshow(self.ax, self.data)
            self.canvas.draw()
Пример #4
0
 def setFileName(self, fileName):
     self.filename = fileName
     self.data = fits.open(Path(self.filename))[0].data
     zimshow(self.ax, self.data)
     self.canvas.draw()
Пример #5
0
 def setCropInfo(self, cropInfo):
     self.cropInfo = cropInfo
     self.data = fits.open(Path(self.cropInfo.filename))[0].data[self.cropInfo.y0:self.cropInfo.y1,
            self.cropInfo.x0:self.cropInfo.x1]
     zimshow(self.ax, self.data)
     self.canvas.draw()
 def showImage(self):
     data = self.flux[self.cropInfo.y0:self.cropInfo.y1,
                      self.cropInfo.x0:self.cropInfo.x1]
     zimshow(self.mainImageAx, data, normalize=self.norm)
     self.mainImageAx.figure.canvas.draw()
    def apertureTrace(self, fitMethod=None):
        if fitMethod is not None: self.apertureFitMethod = fitMethod
        apertureAx = self.apertureAx
        fitAx = self.fitAx
        residualAx = self.residualAx
        flux = self.flux
        apertureAx.clear()
        fitAx.clear()
        residualAx.clear()

        xFlux = np.arange(len(flux[0]))
        xxFlux = np.arange(0, len(flux[0]), 0.01)
        yFlux = np.arange(len(flux))
        xyFlux = np.arange(0, len(flux), 0.01)
        skysubtractedFlux = self.skysubtractedFlux
        apertures = self.apertures
        apertureCoeffs = self.apertureCoeffs
        fitMethod = self.apertureFitMethod
        norm = znorm(skysubtractedFlux)
        fitDeg = 3
        sigmaATFitMask = 2
        itersATFitMask = 2
        itersATFit = 3
        # APTrace with chebyshev(or polynomial)
        # Extract itersATFit iteratively as mask the sigma( Maybe not useful after iter>3

        if (fitMethod == 'chebyshev'):
            fitter = np.polynomial.chebyshev.chebfit
            valFunc = np.polynomial.chebyshev.chebval
        elif (fitMethod == 'polynomial'):
            fitter = np.polynomial.polynomial.polyfit
            valFunc = np.polynomial.polynomial.polyval

        # first
        fitted = fitter(xFlux, apertures, fitDeg)
        xxFlux = np.arange(0, len(flux[0]), 0.001)
        fitVal = valFunc(xxFlux, fitted)

        # Extract abnormals by sigmaClip and refit
        for iATFit in range(itersATFit):
            clip_mask = sigma_clip(apertures - valFunc(xFlux, fitted),
                                   sigma=sigmaATFitMask,
                                   maxiters=itersATFitMask).mask
            fitted = fitter(xFlux[~clip_mask], apertures[~clip_mask], fitDeg)
            xxFlux = np.arange(0, len(flux[0]), 0.001)
            xfitVal = valFunc(xxFlux, fitted)
            fitVal = valFunc(xFlux, fitted)

        self.fitVal = fitVal

        zimshow(apertureAx, skysubtractedFlux, normalize=norm)

        apertureAx.plot(xFlux[~clip_mask], apertures[~clip_mask], 'k,')
        apertureAx.plot(xFlux[clip_mask], apertures[clip_mask], 'rx')

        zimshow(fitAx, skysubtractedFlux, normalize=norm)
        fitAx.plot(xFlux[~clip_mask], apertures[~clip_mask], 'k+', ms=5)
        fitAx.plot(xFlux[clip_mask], apertures[clip_mask], 'rx')
        fitAx.plot(xxFlux, xfitVal, 'b--')

        residualAx.plot(xFlux[~clip_mask],
                        apertures[~clip_mask] - fitVal[~clip_mask],
                        'k+',
                        ms=5)
        residualAx.plot(xFlux[clip_mask],
                        apertures[clip_mask] - fitVal[clip_mask], 'rx')
        residualAx.axhline(0, color='b', linestyle='--')
        residualAx.set_ylim(-0.5, 0.5)

        apertureAx.set_xlim(0, len(flux[0]))
        fitAx.set_xlim(0, len(flux[0]))
        residualAx.set_xlim(0, len(flux[0]))
    def findAperturePoints(self):
        flux = self.flux
        fig = self.fig
        FWHM = self.FWHM
        skyFit = self.skyFit
        objFit = self.objFit

        xFlux = np.arange(len(flux[0]))
        xxFlux = np.arange(0, len(flux[0]), 0.01)
        yFlux = np.arange(len(flux))
        xyFlux = np.arange(0, len(flux), 0.01)
        skysubtractedFlux = []
        skyFlux = []
        apertureCoeffs = []
        apertures = []

        self.imgAx = fig.add_subplot(321)
        self.subImgAx = fig.add_subplot(323, sharex=self.imgAx)
        self.skyImgAx = fig.add_subplot(325, sharex=self.imgAx)
        self.apertureAx = fig.add_subplot(322, sharey=self.imgAx)
        self.fitAx = fig.add_subplot(324)
        self.residualAx = fig.add_subplot(326)

        zimshow(self.imgAx, self.flux, normalize=self.norm)
        zimshow(self.apertureAx, self.flux, normalize=self.norm)

        for xPix in range(len(flux[0])):
            fluxNow = flux[:, xPix]
            # find initial guess for mean value in image gaussian mean
            # Peak을 prominence가 가장 큰걸 기준으로 찾아서 sky중 가장 큰 값이 peak이 아닐 경우를 대비.

            peaks, _ = signal.find_peaks(fluxNow)
            prominences = signal.peak_prominences(fluxNow, peaks)[0]
            peakPixGuess = peaks[np.argmax(prominences)]

            #imgAx.plot(xPix, peakPixGuess, 'r+', ms=5, label='initial guess on gauss peak')
            # find initial guess for linear sky
            # extract gaussian and linear fit
            # peakPix 주변 FWHM 2배 만큼의 픽셀을 뺀 후에 linearfit

            objMin = peakPixGuess - FWHM * 2 if peakPixGuess - FWHM * 2 > 0 else 0
            objMax = peakPixGuess + FWHM * 2 if peakPixGuess + FWHM * 2 < len(
                fluxNow) else len(fluxNow)

            yObj = np.arange(objMin, objMax)
            ySky = np.delete(yFlux, yObj)
            fluxSky = np.delete(fluxNow, yObj)
            #guessAx.plot(yFlux, fluxNow, 'r-')
            interceptGuess = fluxSky[0]
            slopeGuess = (fluxSky[-1] - fluxSky[0]) / len(fluxSky)
            try:
                poptSky, pcov = curve_fit(skyFit, ySky, fluxSky,
                                          [slopeGuess, interceptGuess])
            except:
                poptSky = [0, 0]
            slopeGuess = poptSky[0]
            interceptGuess = poptSky[1]
            # 더 빨리? ->
            #SlopeGuess = np.median(fluxNow[-1:-11]) - np.median(fluxNow[0:10])
            #interceptGuess = np.median(fluxNow[0:10])

            #guessAx.plot(xyFlux, skyFit(xyFlux, *poptSky), 'y--')

            # gaussGuess plot after extraction of sky
            gaussGuessFlux = fluxNow - skyFit(yFlux, *poptSky)

            try:
                poptGauss, pcov = curve_fit(
                    gaussian,
                    yFlux,
                    gaussGuessFlux, [
                        gaussGuessFlux[peakPixGuess], peakPixGuess,
                        FWHM * gaussian_fwhm_to_sigma
                    ],
                    bounds=((0, peakPixGuess - FWHM, 0),
                            (gaussGuessFlux[peakPixGuess] * 2,
                             peakPixGuess + FWHM, FWHM)))
            except:
                poptGauss = np.array(
                    [0, peakPixGuess, FWHM * gaussian_fwhm_to_sigma])

            #guessAx.plot(xyFlux, gaussian(xyFlux, *poptGauss), 'y--')
            #guessAx.plot(xyFlux, skyImage(xyFlux, *poptSky, *poptGauss), 'b--')

            amplitudeGuess = poptGauss[0]
            meanGuess = poptGauss[1]
            stddevGuess = poptGauss[2]
            # 더 빨리? ->
            #amplitudeGuess = gaussGuessFlux[peakPixGuess]
            #meanGuess = peakPixGuess
            #stddevGuess = FWHM * gaussian_fwhm_to_sigma
            try:
                poptFin, pcov = curve_fit(
                    skyImage,
                    yFlux,
                    fluxNow, [
                        slopeGuess, interceptGuess, amplitudeGuess, meanGuess,
                        stddevGuess
                    ],
                    bounds=((-np.inf, -np.inf, 0, peakPixGuess - FWHM, 0.001),
                            (np.inf, np.inf, 2 * gaussGuessFlux[peakPixGuess],
                             peakPixGuess + FWHM, FWHM)))

            except:
                poptSky, pcov = curve_fit(skyFit, yFlux, fluxNow,
                                          [slopeGuess, interceptGuess])
                poptFin = np.array([
                    poptSky[0], poptSky[1], 0, peakPixGuess,
                    FWHM * gaussian_fwhm_to_sigma
                ])

            #fitAx.plot(yFlux, fluxNow, 'r-')
            #fitAx.plot(xyFlux, skyImage(xyFlux, *poptFin), 'b--')
            # plt.plot(fluxNow)
            # print(popt[3])
            #imgAx.plot(xPix, poptFin[3], 'k+', ms=3, label='final Aperture')
            self.apertureAx.plot(
                [xPix, xPix],
                [poptFin[3] - poptFin[4] * 3, poptFin[3] + poptFin[4] * 3],
                'k',
                lw=1)
            self.apertureAx.plot(xPix,
                                 poptFin[3],
                                 'b,',
                                 ms=3,
                                 label='final Aperture points')
            skyFluxNow = (poptFin[0] * yFlux + poptFin[1])
            skysubtractedFluxNow = fluxNow - skyFluxNow

            apertureCoeffs.append(poptFin)
            apertures.append(poptFin[3])
            skysubtractedFlux.append(skysubtractedFluxNow)
            skyFlux.append(skyFluxNow)
            self.progressChangeSignal.emit(xPix / len(flux[0]) * 100)
        self.progressChangeSignal.emit(100)

        skysubtractedFlux = np.array(skysubtractedFlux)
        skyFlux = np.array(skyFlux)
        self.apertures = np.array(apertures)
        self.apertureCoeffs = np.array(apertureCoeffs)
        self.skysubtractedFlux = skysubtractedFlux.T
        self.skyFlux = skyFlux.T
        zimshow(self.subImgAx, self.skysubtractedFlux, normalize=self.norm)
        zimshow(self.skyImgAx, self.skyFlux, normalize=self.norm)

        self.imgAx.set_xlim(0, len(flux[0]))
        self.imgAx.set_ylim(0, len(flux))
        self.apertureAx.set_xlim(0, len(flux[0]))
        self.apertureAx.set_ylim(0, len(flux))