From 24979f97cdffd0452b51f8fe8fbec2826117fe1c Mon Sep 17 00:00:00 2001 From: SGCMarkus Date: Mon, 29 Apr 2024 08:59:29 +0200 Subject: [PATCH] tmp --- main/flaredetector/flaredetector.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/main/flaredetector/flaredetector.py b/main/flaredetector/flaredetector.py index 51ea6b4..22a1ee2 100644 --- a/main/flaredetector/flaredetector.py +++ b/main/flaredetector/flaredetector.py @@ -16,10 +16,10 @@ def moveFluxForFit(curPeakIndex, flux): flarePoints = np.array(np.linspace(minInd, maxInd, num=maxInd-minInd+1), dtype=int) flareFitDataPoints = flarePoints - curPeakIndex Peak = flux[curPeakIndex] - 1 - return flarePoints, flareFitDataPoints, Peak, maxInd - curPeakIndex + return flarePoints, flareFitDataPoints, Peak, curPeakIndex - minInd def isValidFlarePoint(flareData, newPeakIndex): - threshold = 0.005 + threshold = 0.003 if((flareData[newPeakIndex-1] > threshold and flareData[newPeakIndex+1] > threshold) or (flareData[newPeakIndex+1] > threshold and flareData[newPeakIndex+2] > threshold)): return True @@ -27,9 +27,21 @@ def isValidFlarePoint(flareData, newPeakIndex): def calculateFitForFlare(lightcurve, peakIndex): flarePoints, flareFitDataPoints, Peak, newPeakIndex = moveFluxForFit(peakIndex, lightcurve.flux) - if(not isValidFlarePoint(lightcurve.flux[flarePoints]-1, newPeakIndex)): + flareData = lightcurve.flux[flarePoints]-1 + PeakMinus1 = flareData[newPeakIndex-1] + PeakMinus2 = flareData[newPeakIndex-2] + if(not isValidFlarePoint(flareData, newPeakIndex)): raise Exception("Flare not valid: not enough datapoints above mean") - popt, pcov = curve_fit(lambda x, l, w: GaussExpo(x, Peak, l, w), flareFitDataPoints, lightcurve.flux[flarePoints]-1) + if(PeakMinus1 > 0.01 and PeakMinus1 < PeakMinus2): + if(Peak > 0.025): + print(lightcurve.time[peakIndex], lightcurve.flux[peakIndex]) + print(lightcurve.flux[peakIndex], lightcurve.flux[peakIndex-1], lightcurve.flux[peakIndex-2]) + raise Exception(f"Flare not valid: peak-2 > peak-1, {PeakMinus2} > {PeakMinus1}") + # TODO: allow peak-1 to be around/lower than mean too if theres enough points after + popt, pcov = curve_fit(lambda x, l, w: GaussExpo(x, Peak, l, w), flareFitDataPoints, flareData) + pcovDiag = np.diag(pcov) + if(pcovDiag[0] > 1 and pcovDiag[1] > 1): + raise Exception("Flare not valid: could not properly fit") fit = GaussExpo(flareFitDataPoints, Peak, *popt) flareFitDataPoints = flareFitDataPoints + peakIndex return Peak+1, flareFitDataPoints, fit+1 @@ -48,7 +60,8 @@ def calculateFlareFitsForLightcurve(lightcurve, num=100, distance=1, height=0.05 "FlareFitTime": lightcurve.time[flareFitDataPoints], "StandardIndex": flareFitDataPoints}) except Exception as error: - print("Flare fit exception: ", error) + pass + #print("Flare fit exception: ", error) return peaks, fits