diff --git a/data.cff b/data.cff new file mode 100644 index 0000000..77aac9d Binary files /dev/null and b/data.cff differ diff --git a/generate_plots_MKGF.py b/generate_plots_MKGF.py new file mode 100644 index 0000000..db99bdc --- /dev/null +++ b/generate_plots_MKGF.py @@ -0,0 +1,374 @@ +import numpy as np +import pandas as pd +import itertools +from main.astrodatagui.db.StarsDB import StarDB +import matplotlib.ticker as tck +from matplotlib.pyplot import MaxNLocator +import matplotlib.pyplot as plt +from datetime import datetime +from errno import EEXIST +from os import makedirs, path +import shutil + +def normalizePhase(phase, phaseMin = None, phaseMax = None): + if(phaseMin is None): + phaseMin = np.abs(np.min(phase)) + if(phaseMax is None): + phaseMax = np.abs(np.max(phase)) + return (phase + phaseMin) / (phaseMin + phaseMax) * (2) + +def mkdir_p(mypath): + '''Creates a directory. equivalent to using mkdir -p on the command line''' + + try: + makedirs(mypath) + except OSError as exc: # Python >2.5 + if exc.errno == EEXIST and path.isdir(mypath): + pass + else: raise + +fileName = "data.cff" +data = pd.read_pickle(fileName) + +binList = [10, 20, 30] +spType = ["M", "K", "G", "F"] + +useKepler = True +useK2 = True +useTESS = True + +showSourceFilter = np.full(len(data), False) +if(useKepler): + showKepler = data["Source"] == "Kepler" + showSourceFilter |= showKepler +if(useK2): + showK2 = data["Source"] == "K2" + showSourceFilter |= showK2 +if(useTESS): + showTESS = data["Source"] == "TESS" + showSourceFilter |= showTESS + +current = datetime.now() +date = f"{current.year}-{current.month}-{current.day}" +time = f"{current.hour}-{current.minute}-{current.second}" +folderPath = f"../{date}/{time}/" +#folderPath = f"G:/Meine Ablage/Masterthesis/{date}/" +mkdir_p(folderPath) + +for comboLength in range(1, len(spType) + 1): + for combo in itertools.combinations(spType, comboLength): + finalData = pd.DataFrame() + if("M" in combo): + Mfilter = data["SpType"].str.startswith("M") + Mfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Mfilter]], ignore_index=True) + if("K" in combo): + Kfilter = data["SpType"].str.startswith("K") + Kfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Kfilter]], ignore_index=True) + if("G" in combo): + Gfilter = data["SpType"].str.startswith("G") + Gfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Gfilter]], ignore_index=True) + if("F" in combo): + Ffilter = data["SpType"].str.startswith("F") + Ffilter &= showSourceFilter + finalData = pd.concat([finalData, data[Ffilter]], ignore_index=True) + + + + numStars = len(set(finalData["StarName"])) + + pdcsapbinningData = [] + locFolder = f"{folderPath}/{''.join(combo)}/" + mkdir_p(f"{locFolder}/") + csvFile = open(f"{locFolder}/{''.join(combo)}.csv", "a") + csvFile.write("Star Name,Spectral Type,Source,File,Flare Time,Flare Peak,Period,Normalized Phase of Peak,Peak in Period") + csvFile.write("\n") + for ind, row in finalData.reset_index().iterrows(): + PDCSAPminOrigPhase = row["pdcsapFoldedFitPhaseStarEnd"][0] + PDCSAPmaxOrigPhase = row["pdcsapFoldedFitPhaseStarEnd"][1] + if(len(row["pdcsapFoldedPeaksPhasePair"]) > 0): + pdcsapVals = pd.DataFrame(row["pdcsapFoldedPeaksPhasePair"]) + for td, peak, pv in zip(pdcsapVals["Phase"], pdcsapVals["Peak"], row["pdcsapPeaks"]): + normPhase = normalizePhase(td.value, np.abs(PDCSAPminOrigPhase), np.abs(PDCSAPmaxOrigPhase)) + csvFile.write(f"{row['StarName']},{row['SpType']},{row['Source']},{row['FilePath']},{pv['FlarePeakTime']},{pv['FlarePeak']},{row['pdcsapPeriod']},{normPhase},{peak['FlarePeak']}") + csvFile.write("\n") + pdcsapbinningData.append({"SpType": row["SpType"][0], + "PDCSAPNormPhase": normPhase, + "Peak": peak["FlarePeak"]}) + if(peak["FlarePeak"] > 100): + print(row["StarName"], "has over 100 peak") + + csvFile.close() + pdcsapbinningData = pd.DataFrame(pdcsapbinningData) + PDCSAPdataList = [] + PDCSAPlabelList = [] + PDCSAPcolorList = [] + PDCSAPdataList2dhistPhase = [] + PDCSAPdataList2dhistPeak = [] + PDCSAPlabelList2dhist = [] + PDCSAPcolorList2dhist = [] + if("M" in combo): + Mfilter = pdcsapbinningData["SpType"] == "M" + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[Mfilter]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[Mfilter]["Peak"]) + PDCSAPlabelList2dhist.append("M Stars") + PDCSAPcolorList2dhist.append("red") + + PDCSAPdataList.append(pdcsapbinningData[Mfilter]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("M Stars") + PDCSAPcolorList.append("red") + if("K" in combo): + Kfilter = pdcsapbinningData["SpType"] == "K" + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[Kfilter]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[Kfilter]["Peak"]) + PDCSAPlabelList2dhist.append("K Stars") + PDCSAPcolorList2dhist.append("orange") + + PDCSAPdataList.append(pdcsapbinningData[Kfilter]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("K Stars") + PDCSAPcolorList.append("orange") + if("G" in combo): + Gfilter = pdcsapbinningData["SpType"] == "G" + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[Gfilter]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[Gfilter]["Peak"]) + PDCSAPlabelList2dhist.append("G Stars") + PDCSAPcolorList2dhist.append("yellow") + + PDCSAPdataList.append(pdcsapbinningData[Gfilter]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("G Stars") + PDCSAPcolorList.append("yellow") + if("F" in combo): + Ffilter = pdcsapbinningData["SpType"] == "F" + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[Ffilter]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[Ffilter]["Peak"]) + PDCSAPlabelList2dhist.append("F Stars") + PDCSAPcolorList2dhist.append("greenyellow") + + PDCSAPdataList.append(pdcsapbinningData[Ffilter]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("F Stars") + PDCSAPcolorList.append("greenyellow") + + xData = pd.DataFrame() + yData = pd.DataFrame() + for aX, aY in zip(PDCSAPdataList2dhistPhase, PDCSAPdataList2dhistPeak): + xData = pd.concat([xData, aX], ignore_index=True) + yData = pd.concat([yData, aY], ignore_index=True) + + xData = np.asarray(xData.values)[:,0] + yData = np.asarray(yData.values)[:,0] + + for bins in binList: + figHisto, ((axHisto)) = plt.subplots(nrows=1, ncols=1) + y, binEdges, _ = axHisto.hist(PDCSAPdataList, bins, + label=PDCSAPlabelList, + color=PDCSAPcolorList, + stacked=True, + range=[0, 2]) + bincenters = 0.5*(binEdges[1:]+binEdges[:-1]) + if(isinstance(y[0], np.ndarray)): + y = y[-1] + n_i = y + m_i = bincenters * np.pi + N = np.sum(n_i) + mean = np.sum(n_i * m_i)/N + stdDev = np.sqrt(np.sum(((n_i - mean)**2)) / (N-1)) + menStd = np.sqrt(y) + axHisto.bar(bincenters[y > 0], y[y > 0], width=0, color='r', yerr=stdDev) + axHisto.set_ylim(0, max(y) + stdDev) + axHisto.set_ylabel("Num. flares") + axHisto.set_xlabel("Phase") + axHisto.set_title(f"Flare count per phase of {', '.join(combo)} type stars with {bins} bins ({numStars} stars)") + axHisto.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + axHisto.xaxis.set_major_locator(MaxNLocator(5)) + axHisto.legend() + + axHistoPhase = axHisto.twinx() + secAxisXdata = np.linspace(0, 2, num=10000) + secAxisYdata = np.cos(secAxisXdata*np.pi) + 1 + axHistoPhase.plot(secAxisXdata, secAxisYdata) + axHistoPhase.set_ylim(0, 7) + + plt.savefig(f"{locFolder}/{''.join(combo)}-Flarecount-{bins}_Bins.png") + plt.close() + + for maxY in [1.05, 1.1, 1.2, 1.5, 2, 2.5, 3, 5, max(yData)]: + figFlarepeakHist, ((axFlarepeakHist)) = plt.subplots(nrows=1, ncols=1) + axFlarepeakHist.set_ylabel("Flare peak") + axFlarepeakHist.set_xlabel("Phase") + axFlarepeakHist.set_title(f"Flare peak per phase histogram of {', '.join(combo)} type stars with {bins} bins ({numStars} stars)") + H, xedges, yedges = np.histogram2d(xData, yData, bins=bins, range=[[0, 2], [0.95, maxY]]) + cmax = 11 + H_clipped = np.clip(H, None, cmax) + im = axFlarepeakHist.imshow(H_clipped.T, origin='lower', interpolation='nearest', + extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], + aspect='auto', cmap='viridis') + figFlarepeakHist.colorbar(im, label='Counts', ax=axFlarepeakHist) + axFlarepeakHist.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + axFlarepeakHist.xaxis.set_major_locator(MaxNLocator(5)) + plt.savefig(f"{locFolder}/{''.join(combo)}-Flarepeaks-{bins}_Bins_maxY-{maxY}.png") + plt.close() + + for maxY in [1.05, 1.1, 1.2, 1.5, 2, 2.5, 3, 5, max(yData)]: + figFlarePeaks, ((axFlarePeaks)) = plt.subplots(nrows=1, ncols=1) + axFlarePeaks.set_title(f"Flare peaks per phase of {', '.join(combo)} type stars ({numStars} stars)") + if("M" in combo): + axFlarePeaks.scatter(pdcsapbinningData[Mfilter]["PDCSAPNormPhase"], + pdcsapbinningData[Mfilter]["Peak"], + label="M Stars", color="red") + if("K" in combo): + axFlarePeaks.scatter(pdcsapbinningData[Kfilter]["PDCSAPNormPhase"], + pdcsapbinningData[Kfilter]["Peak"], + label="K Stars", color="orange") + if("G" in combo): + axFlarePeaks.scatter(pdcsapbinningData[Gfilter]["PDCSAPNormPhase"], + pdcsapbinningData[Gfilter]["Peak"], + label="G Stars", color="yellow") + if("F" in combo): + axFlarePeaks.scatter(pdcsapbinningData[Ffilter]["PDCSAPNormPhase"], + pdcsapbinningData[Ffilter]["Peak"], + label="F Stars", color="greenyellow") + axFlarePeaks.set_xlim(0, 2) + axFlarePeaks.set_ylim(0.95, maxY) + axFlarePeaks.set_ylabel("Flare peak") + axFlarePeaks.set_xlabel("Phase") + axFlarePeaks.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + axFlarePeaks.xaxis.set_major_locator(MaxNLocator(5)) + axFlarePeaks.legend() + + plt.savefig(f"{locFolder}/{''.join(combo)}-Flarepeaks_maxY-{maxY}.png") + plt.close() + +for sT, color in zip(["M", "K", "G", "F"], ["red", "orange", "yellow", "greenyellow"]): + spTypes = [f"{sT}0", f"{sT}1", f"{sT}2", f"{sT}3", f"{sT}4", f"{sT}5", f"{sT}6", f"{sT}7", f"{sT}8", f"{sT}9"] + for spTyp in spTypes: + finalData = pd.DataFrame() + + Mfilter = data["SpType"].str.startswith(spTyp) + numStars = len(set(data[Mfilter]["StarName"])) + Mfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Mfilter]], ignore_index=True) + + pdcsapbinningData = [] + locFolder = f"{folderPath}/{spTyp}/" + mkdir_p(f"{locFolder}/") + csvFile = open(f"{locFolder}/{spTyp}.csv", "a") + csvFile.write("Star Name,Spectral Type,Source,File,Flare Time,Flare Peak,Period,Normalized Phase of Peak,Peak in Period") + csvFile.write("\n") + for ind, row in finalData.reset_index().iterrows(): + PDCSAPminOrigPhase = row["pdcsapFoldedFitPhaseStarEnd"][0] + PDCSAPmaxOrigPhase = row["pdcsapFoldedFitPhaseStarEnd"][1] + if(len(row["pdcsapFoldedPeaksPhasePair"]) > 0): + pdcsapVals = pd.DataFrame(row["pdcsapFoldedPeaksPhasePair"]) + for td, peak, pv in zip(pdcsapVals["Phase"], pdcsapVals["Peak"], row["pdcsapPeaks"]): + normPhase = normalizePhase(td.value, np.abs(PDCSAPminOrigPhase), np.abs(PDCSAPmaxOrigPhase)) + csvFile.write(f"{row['StarName']},{row['SpType']},{row['Source']},{row['FilePath']},{pv['FlarePeakTime']},{pv['FlarePeak']},{row['pdcsapPeriod']},{normPhase},{peak['FlarePeak']}") + csvFile.write("\n") + pdcsapbinningData.append({"SpType": f'{row["SpType"][0]}{row["SpType"][1]}', + "PDCSAPNormPhase": normPhase, + "Peak": peak["FlarePeak"]}) + if(peak["FlarePeak"] > 100): + print(row["StarName"], "has over 100 peak") + csvFile.close() + pdcsapbinningData = pd.DataFrame(pdcsapbinningData) + PDCSAPdataList = [] + PDCSAPlabelList = [] + PDCSAPcolorList = [] + PDCSAPdataList2dhistPhase = [] + PDCSAPdataList2dhistPeak = [] + PDCSAPlabelList2dhist = [] + PDCSAPcolorList2dhist = [] + try: + SpTypefilter = pdcsapbinningData["SpType"] == spTyp + except: + shutil.rmtree(locFolder) + continue + + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[SpTypefilter]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[SpTypefilter]["Peak"]) + PDCSAPlabelList2dhist.append(f"{spTyp} Stars") + PDCSAPcolorList2dhist.append(color) + + xData = pd.DataFrame() + yData = pd.DataFrame() + for aX, aY in zip(PDCSAPdataList2dhistPhase, PDCSAPdataList2dhistPeak): + xData = pd.concat([xData, aX], ignore_index=True) + yData = pd.concat([yData, aY], ignore_index=True) + + xData = np.asarray(xData.values)[:,0] + yData = np.asarray(yData.values)[:,0] + PDCSAPdataList.append(pdcsapbinningData[SpTypefilter]["PDCSAPNormPhase"]) + + PDCSAPlabelList.append(f"{spTyp} Stars") + PDCSAPcolorList.append(color) + + for bins in binList: + figHisto, ((axHisto)) = plt.subplots(nrows=1, ncols=1) + y, binEdges, _ = axHisto.hist(PDCSAPdataList, bins, + label=PDCSAPlabelList, + color=PDCSAPcolorList, + stacked=True, + range=[0, 2]) + bincenters = 0.5*(binEdges[1:]+binEdges[:-1]) + if(isinstance(y[0], np.ndarray)): + y = y[-1] + n_i = y + m_i = bincenters * np.pi + N = np.sum(n_i) + mean = np.sum(n_i * m_i)/N + stdDev = np.sqrt(np.sum(((n_i - mean)**2)) / (N-1)) + menStd = np.sqrt(y) + axHisto.bar(bincenters[y > 0], y[y > 0], width=0, color='r', yerr=stdDev) + if(~np.isnan(stdDev) & ~np.isinf(stdDev)): + axHisto.set_ylim(0, max(y[y > 0 & ~np.isnan(y) & ~np.isinf(y)] + stdDev)) + axHisto.set_ylabel("Num. flares") + axHisto.set_xlabel("Phase") + axHisto.set_title(f"Flare count in phase of {spTyp} type stars with {bins} bins ({numStars} stars)") + axHisto.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + axHisto.xaxis.set_major_locator(MaxNLocator(5)) + axHisto.legend() + + axHistoPhase = axHisto.twinx() + secAxisXdata = np.linspace(0, 2, num=10000) + secAxisYdata = np.cos(secAxisXdata*np.pi) + 1 + axHistoPhase.plot(secAxisXdata, secAxisYdata) + axHistoPhase.set_ylim(0, 7) + + plt.savefig(f"{locFolder}/{''.join(spTyp)}-Flarecount-{bins}_Bins.png") + plt.close() + + for maxY in [1.05, 1.1, 1.2, 1.5, 2, 2.5, 3, 5, max(yData)]: + figFlarepeakHist, ((axFlarepeakHist)) = plt.subplots(nrows=1, ncols=1) + axFlarepeakHist.set_ylabel("Flare peak") + axFlarepeakHist.set_xlabel("Phase") + H, xedges, yedges = np.histogram2d(xData, yData, bins=bins, range=[[0, 2], [min(yData), maxY]]) + cmax = 11 + H_clipped = np.clip(H, None, cmax) + im = axFlarepeakHist.imshow(H_clipped.T, origin='lower', interpolation='nearest', + extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], + aspect='auto', cmap='viridis') + figFlarepeakHist.colorbar(im, label='Counts', ax=axFlarepeakHist) + axFlarepeakHist.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + axFlarepeakHist.xaxis.set_major_locator(MaxNLocator(5)) + axFlarepeakHist.set_title(f"Flare peak per phase histogram of {spTyp} type stars with {bins} bins ({numStars} stars)") + plt.savefig(f"{locFolder}/{''.join(spTyp)}-Flarepeaks-{bins}_Bins_maxY-{maxY}.png") + plt.close() + + for maxY in [1.05, 1.1, 1.2, 1.5, 2, 2.5, 3, 5, max(yData)]: + figFlarePeaks, ((axFlarePeaks)) = plt.subplots(nrows=1, ncols=1) + axFlarePeaks.scatter(pdcsapbinningData[SpTypefilter]["PDCSAPNormPhase"], + pdcsapbinningData[SpTypefilter]["Peak"], + label=f"{spTyp} Stars", color=color) + axFlarePeaks.set_xlim(0, 2) + axFlarePeaks.set_ylim(0.95, maxY) + axFlarePeaks.set_ylabel("Flare peak") + axFlarePeaks.set_xlabel("Phase") + axFlarePeaks.set_title(f"Flare peaks per phase of {spTyp} type stars ({numStars} stars)") + axFlarePeaks.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + axFlarePeaks.xaxis.set_major_locator(MaxNLocator(5)) + axFlarePeaks.legend() + + plt.savefig(f"{locFolder}/{''.join(spTyp)}-Flarepeaks_maxY-{maxY}.png") + plt.close() + diff --git a/main/astrodatagui/CalcAllFlaresThread.py b/main/astrodatagui/CalcAllFlaresThread.py index 0e81170..1378b25 100644 --- a/main/astrodatagui/CalcAllFlaresThread.py +++ b/main/astrodatagui/CalcAllFlaresThread.py @@ -44,7 +44,7 @@ def getFlareCount(filesDict): pdcsapminPhasesBounds, pdcsapmaxPhasesBounds = getPhaseRangesNearPeak((pdcsapMinima, pdcsapMaxima), pdcsapPhase, returnPhaseValue=True) pdcsapFoldedPeaks = [] pdcsapFoldedPeaksPhasePair = [] - for peak in sapPeaks: + for peak in pdcsapPeaks: cycle, foldedIndex = convertStarndardIndexToFoldedIndex(pdcsapFoldedLC, peak["StandardIndex"]) pdcsapFoldedPeaks.append({"Cycle: ": cycle, "Index": foldedIndex}) pdcsapFoldedPeaksPhasePair.append({"Phase": pdcsapFoldedLC.phase[pdcsapFoldedLC.cycle == cycle][foldedIndex], "Peak": peak}) diff --git a/main/astrodatagui/FlareSummaryPlotGUI.py b/main/astrodatagui/FlareSummaryPlotGUI.py index 97e82d7..9f9ab67 100644 --- a/main/astrodatagui/FlareSummaryPlotGUI.py +++ b/main/astrodatagui/FlareSummaryPlotGUI.py @@ -1,6 +1,6 @@ from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QGridLayout, - QPushButton, QCheckBox, QLabel) + QPushButton, QCheckBox, QLabel, QLineEdit) from matplotlib.figure import Figure from matplotlib.backends.backend_qtagg import ( FigureCanvas, NavigationToolbar2QT as NavigationToolbar) @@ -41,6 +41,13 @@ def sumArrayLengthsNorm(series): sumRes += len(s) return sumRes / len(series) +def normalizePhase(phase, phaseMin = None, phaseMax = None): + if(phaseMin is None): + phaseMin = np.abs(np.min(phase)) + if(phaseMax is None): + phaseMax = np.abs(np.max(phase)) + return (phase + phaseMin) / (phaseMin + phaseMax) * (2) + class FlareSummaryPlotGUI(QWidget): def __init__(self, starFLareDictList): @@ -87,6 +94,11 @@ class FlareSummaryPlotGUI(QWidget): self.btShowFlaresInMinimaMaximaPerMinimaMaxima = QPushButton("Show num Flares Minima/Maxima normalized") self.btShowFlaresInMinimaMaximaPerMinimaMaxima.clicked.connect(self.btShowFlaresInMinimaMaximaPerMinimaMaximaClicked) + self.btShowFlaresBinnedOnPhase = QPushButton("Show flares binned") + self.btShowFlaresBinnedOnPhase.clicked.connect(self.btShowFlaresBinnedOnPhaseClicked) + self.textNumBins = QLineEdit() + self.textNumBins.setText("10") + self.cbKepler = QCheckBox("Kepler") self.cbKepler.setChecked(True) self.cbK2 = QCheckBox("K2") @@ -95,7 +107,7 @@ class FlareSummaryPlotGUI(QWidget): self.cbTESS.setChecked(True) self.cbSpTypeL = QCheckBox("L") - self.cbSpTypeL.setChecked(True) + self.cbSpTypeL.setChecked(False) self.cbSpTypeM = QCheckBox("M") self.cbSpTypeM.setChecked(True) self.cbSpTypeK = QCheckBox("K") @@ -105,7 +117,7 @@ class FlareSummaryPlotGUI(QWidget): self.cbSpTypeF = QCheckBox("F") self.cbSpTypeF.setChecked(True) self.cbSpTypeUnknown = QCheckBox("Unknown") - self.cbSpTypeUnknown.setChecked(True) + self.cbSpTypeUnknown.setChecked(False) self.cbShowSAP = QCheckBox("SAP") self.cbShowSAP.setChecked(True) @@ -121,6 +133,8 @@ class FlareSummaryPlotGUI(QWidget): self.buttonGridLayout.addWidget(self.btNumMinimaMaximaNorm, 0, 6) self.buttonGridLayout.addWidget(self.btShowFlaresInMinimaMaxima, 0, 7) self.buttonGridLayout.addWidget(self.btShowFlaresInMinimaMaximaPerMinimaMaxima, 0, 8) + self.buttonGridLayout.addWidget(self.btShowFlaresBinnedOnPhase, 0, 9) + self.buttonGridLayout.addWidget(self.textNumBins, 0, 10) self.buttonGridLayout.addWidget(QLabel("Sources: "), 1, 0) self.buttonGridLayout.addWidget(self.cbKepler, 1, 1) @@ -128,12 +142,12 @@ class FlareSummaryPlotGUI(QWidget): self.buttonGridLayout.addWidget(self.cbTESS, 1, 3) self.buttonGridLayout.addWidget(QLabel("Sp Types: "), 2, 0) - self.buttonGridLayout.addWidget(self.cbSpTypeL, 2, 1) - self.buttonGridLayout.addWidget(self.cbSpTypeM, 2, 2) - self.buttonGridLayout.addWidget(self.cbSpTypeK, 2, 3) - self.buttonGridLayout.addWidget(self.cbSpTypeG, 2, 4) - self.buttonGridLayout.addWidget(self.cbSpTypeF, 2, 5) - self.buttonGridLayout.addWidget(self.cbSpTypeUnknown, 2, 6) + #self.buttonGridLayout.addWidget(self.cbSpTypeL, 2, 1) + self.buttonGridLayout.addWidget(self.cbSpTypeM, 2, 1) + self.buttonGridLayout.addWidget(self.cbSpTypeK, 2, 2) + self.buttonGridLayout.addWidget(self.cbSpTypeG, 2, 3) + self.buttonGridLayout.addWidget(self.cbSpTypeF, 2, 4) + #self.buttonGridLayout.addWidget(self.cbSpTypeUnknown, 2, 6) self.buttonGridLayout.addWidget(self.cbShowSAP, 3, 0) self.buttonGridLayout.addWidget(self.cbShowPDCSAP, 3, 1) @@ -1295,3 +1309,268 @@ class FlareSummaryPlotGUI(QWidget): print("Total Minima: ", PDCSAPtotalMin) print("Total Maxima: ", PDCSAPtotalMax) + + def btShowFlaresBinnedOnPhaseClicked(self): + data = self.starFLareDictList + showSourceFilter = np.full(len(data), False) + try: + nBins = int(self.textNumBins.text()) + except Exception as e: + print("Falling back to 10 Bins") + print(e) + nBins = 10 + if(self.cbKepler.isChecked()): + showKepler = data["Source"] == "Kepler" + showSourceFilter |= showKepler + if(self.cbK2.isChecked()): + showK2 = data["Source"] == "K2" + showSourceFilter |= showK2 + if(self.cbTESS.isChecked()): + showTESS = data["Source"] == "TESS" + showSourceFilter |= showTESS + + finalData = pd.DataFrame() + #if(self.cbSpTypeL.isChecked()): + # Lfilter = data["SpType"].str.startswith("L") + # Lfilter &= showSourceFilter + # finalData = pd.concat([finalData, data[Lfilter]], ignore_index=True) + if(self.cbSpTypeM.isChecked()): + Mfilter = data["SpType"].str.startswith("M") + Mfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Mfilter]], ignore_index=True) + if(self.cbSpTypeK.isChecked()): + Kfilter = data["SpType"].str.startswith("K") + Kfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Kfilter]], ignore_index=True) + if(self.cbSpTypeG.isChecked()): + Gfilter = data["SpType"].str.startswith("G") + Gfilter &= showSourceFilter + finalData = pd.concat([finalData, data[Gfilter]], ignore_index=True) + if(self.cbSpTypeF.isChecked()): + Ffilter = data["SpType"].str.startswith("F") + Ffilter &= showSourceFilter + finalData = pd.concat([finalData, data[Ffilter]], ignore_index=True) + #if(self.cbSpTypeUnknown.isChecked()): + # Unknownfilter = data["SpType"].str.startswith("-") + # Unknownfilter &= showSourceFilter + # finalData = pd.concat([finalData, data[Unknownfilter]], ignore_index=True) + + sapbinningData = [] + pdcsapbinningData = [] + for ind, row in finalData.reset_index().iterrows(): + SAPminOrigPhase = row["sapFoldedFitPhaseStarEnd"][0] + SAPmaxOrigPhase = row["sapFoldedFitPhaseStarEnd"][1] + + PDCSAPminOrigPhase = row["pdcsapFoldedFitPhaseStarEnd"][0] + PDCSAPmaxOrigPhase = row["pdcsapFoldedFitPhaseStarEnd"][1] + + sapValsList = [] + pdcsapValsList = [] + if(len(row["sapFoldedPeaksPhasePair"]) > 0): + sapVals = pd.DataFrame(row["sapFoldedPeaksPhasePair"]) + for td, peak in zip(sapVals["Phase"], sapVals["Peak"]): + sapbinningData.append({"SpType": row["SpType"][0], + "SAPNormPhase": normalizePhase(td.value, np.abs(SAPminOrigPhase), np.abs(SAPmaxOrigPhase)), + "Peak": peak["FlarePeak"]}) + + if(len(row["pdcsapFoldedPeaksPhasePair"]) > 0): + pdcsapVals = pd.DataFrame(row["pdcsapFoldedPeaksPhasePair"]) + for td, peak in zip(pdcsapVals["Phase"], pdcsapVals["Peak"]): + pdcsapbinningData.append({"SpType": row["SpType"][0], + "PDCSAPNormPhase": normalizePhase(td.value, np.abs(PDCSAPminOrigPhase), np.abs(PDCSAPmaxOrigPhase)), + "Peak": peak["FlarePeak"]}) + if(peak["FlarePeak"] > 100): + print(row["StarName"], "has over 100 peak") + + sapbinningData = pd.DataFrame(sapbinningData) + SAPdataList = [] + SAPlabelList = [] + SAPcolorList = [] + pdcsapbinningData = pd.DataFrame(pdcsapbinningData) + PDCSAPdataList = [] + PDCSAPlabelList = [] + PDCSAPcolorList = [] + #if(self.cbSpTypeL.isChecked()): + # SAPdataList.append(sapbinningData[sapbinningData["SpType"] == "L"]["SAPNormPhase"]) + # SAPlabelList.append("L Stars") + # SAPcolorList.append("brown") + # PDCSAPdataList.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "L"]["PDCSAPNormPhase"]) + # PDCSAPlabelList.append("L Stars") + # PDCSAPcolorList.append("brown") + if(self.cbSpTypeM.isChecked()): + SAPdataList.append(sapbinningData[sapbinningData["SpType"] == "M"]["SAPNormPhase"]) + SAPlabelList.append("M Stars") + SAPcolorList.append("red") + PDCSAPdataList.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "M"]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("M Stars") + PDCSAPcolorList.append("red") + if(self.cbSpTypeK.isChecked()): + SAPdataList.append(sapbinningData[sapbinningData["SpType"] == "K"]["SAPNormPhase"]) + SAPlabelList.append("K Stars") + SAPcolorList.append("orange") + PDCSAPdataList.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "K"]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("K Stars") + PDCSAPcolorList.append("orange") + if(self.cbSpTypeG.isChecked()): + SAPdataList.append(sapbinningData[sapbinningData["SpType"] == "G"]["SAPNormPhase"]) + SAPlabelList.append("G Stars") + SAPcolorList.append("yellow") + PDCSAPdataList.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "G"]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("G Stars") + PDCSAPcolorList.append("yellow") + if(self.cbSpTypeF.isChecked()): + SAPdataList.append(sapbinningData[sapbinningData["SpType"] == "F"]["SAPNormPhase"]) + SAPlabelList.append("F Stars") + SAPcolorList.append("greenyellow") + PDCSAPdataList.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "F"]["PDCSAPNormPhase"]) + PDCSAPlabelList.append("F Stars") + PDCSAPcolorList.append("greenyellow") + #if(self.cbSpTypeUnknown.isChecked()): + # SAPdataList.append(sapbinningData[sapbinningData["SpType"] == "-"]["SAPNormPhase"]) + # SAPlabelList.append("Unknown Stars") + # SAPcolorList.append("gray") + # PDCSAPdataList.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "-"]["PDCSAPNormPhase"]) + # PDCSAPlabelList.append("Unknown Stars") + # PDCSAPcolorList.append("gray") + self.figureAxis.clear() + import matplotlib.ticker as tck + from matplotlib.pyplot import MaxNLocator + + import matplotlib.pyplot as plt + #fig, ((ax1, ax3), (ax5, ax7)) = plt.subplots(nrows=2, ncols=2) + fig1, ((ax1)) = plt.subplots(nrows=1, ncols=1) + + ax1.clear() + ax1.set_title("PDCSAP Flarerate in phase") + y, binEdges, _ = ax1.hist(PDCSAPdataList, nBins, label=PDCSAPlabelList, color=PDCSAPcolorList, stacked=True) + bincenters = 0.5*(binEdges[1:]+binEdges[:-1]) + if(isinstance(y[0], np.ndarray)): + y = y[-1] + print("n_i") + n_i = y + print(n_i) + print("----------------------") + print("m_i") + m_i = bincenters * np.pi + print(m_i) + print("----------------------") + print("N") + N = np.sum(n_i) + print(N) + print("----------------------") + + mean = np.sum(n_i * m_i)/N + print(np.sum(n_i * m_i)) + print("----------------------") + print(mean) + print("----------------------") + stdDev = np.sqrt(np.sum(((n_i - mean)**2)) / (N-1)) + print(stdDev) + menStd = np.sqrt(y) + #width = 0.05 + print(bincenters) + print(type(y)) + ax1.bar(bincenters, y, width=0, color='r', yerr=stdDev) + ax1.set_ylabel("Num. flares") + ax1.set_xlabel("Phase") + ax1.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + ax1.xaxis.set_major_locator(MaxNLocator(5)) + ax1.legend() + + ax2 = ax1.twinx() + secAxisXdata = np.linspace(0, 2, num=10000) + secAxisYdata = np.cos(secAxisXdata*np.pi) + 1 + ax2.plot(secAxisXdata, secAxisYdata) + ax2.set_ylim(0, 5) + + fig3, ((ax3)) = plt.subplots(nrows=1, ncols=1) + ax3.clear() + ax3.set_title("PDCSAP Flare peak in phase") + if(self.cbSpTypeM.isChecked()): + Mfilter = pdcsapbinningData["SpType"] == "M" + ax3.scatter(pdcsapbinningData[Mfilter]["PDCSAPNormPhase"], + pdcsapbinningData[Mfilter]["Peak"], + label="M Stars", color="red") + if(self.cbSpTypeK.isChecked()): + ax3.scatter(pdcsapbinningData[pdcsapbinningData["SpType"] == "K"]["PDCSAPNormPhase"], + pdcsapbinningData[pdcsapbinningData["SpType"] == "K"]["Peak"], + label="K Stars", color="orange") + if(self.cbSpTypeG.isChecked()): + ax3.scatter(pdcsapbinningData[pdcsapbinningData["SpType"] == "G"]["PDCSAPNormPhase"], + pdcsapbinningData[pdcsapbinningData["SpType"] == "G"]["Peak"], + label="G Stars", color="yellow") + if(self.cbSpTypeF.isChecked()): + ax3.scatter(pdcsapbinningData[pdcsapbinningData["SpType"] == "F"]["PDCSAPNormPhase"], + pdcsapbinningData[pdcsapbinningData["SpType"] == "F"]["Peak"], + label="F Stars", color="greenyellow") + #if(self.cbSpTypeUnknown.isChecked()): + # ax3.scatter(pdcsapbinningData[pdcsapbinningData["SpType"] == "-"]["PDCSAPNormPhase"], + # pdcsapbinningData[pdcsapbinningData["SpType"] == "-"]["Peak"], + # label="Unknown Stars", color="gray") + + ax3.set_ylabel("Flare peak") + ax3.set_xlabel("Phase") + ax3.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + ax3.xaxis.set_major_locator(MaxNLocator(5)) + ax3.legend() + + fig5, ((ax5)) = plt.subplots(nrows=1, ncols=1) + ax5.clear() + ax5.set_title("PDCSAP Flare peak in phase") + PDCSAPdataList2dhistPhase = [] + PDCSAPdataList2dhistPeak = [] + PDCSAPlabelList2dhist = [] + PDCSAPcolorList2dhist = [] + if(self.cbSpTypeM.isChecked()): + Mfilter = pdcsapbinningData["SpType"] == "M" + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[Mfilter]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[Mfilter]["Peak"]) + PDCSAPlabelList2dhist.append("M Stars") + PDCSAPcolorList2dhist.append("red") + if(self.cbSpTypeK.isChecked()): + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "K"]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "K"]["Peak"]) + PDCSAPlabelList2dhist.append("K Stars") + PDCSAPcolorList2dhist.append("orange") + if(self.cbSpTypeG.isChecked()): + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "G"]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "G"]["Peak"]) + PDCSAPlabelList2dhist.append("G Stars") + PDCSAPcolorList2dhist.append("yellow") + if(self.cbSpTypeF.isChecked()): + PDCSAPdataList2dhistPhase.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "F"]["PDCSAPNormPhase"]) + PDCSAPdataList2dhistPeak.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "F"]["Peak"]) + PDCSAPlabelList2dhist.append("F Stars") + PDCSAPcolorList2dhist.append("greenyellow") + #if(self.cbSpTypeUnknown.isChecked()): + # PDCSAPdataList2dhistPhase.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "-"]["PDCSAPNormPhase"]) + # PDCSAPdataList2dhistPeak.append(pdcsapbinningData[pdcsapbinningData["SpType"] == "-"]["Peak"]) + # PDCSAPlabelList2dhist.append("Unknown Stars") + # PDCSAPcolorList2dhist.append("gray") + + xData = pd.DataFrame() + yData = pd.DataFrame() + for aX, aY in zip(PDCSAPdataList2dhistPhase, PDCSAPdataList2dhistPeak): + xData = pd.concat([xData, aX], ignore_index=True) + yData = pd.concat([yData, aY], ignore_index=True) + + print(np.shape(np.asarray(xData.values)[:,0])) + xData = np.asarray(xData.values)[:,0] + yData = np.asarray(yData.values)[:,0] + ax5.set_ylabel("Flare peak") + ax5.set_xlabel("Phase") + #h = ax5.hist2d(x=xData, y=yData, bins=[nBins, nBins], cmin=0, cmax=10)#, range=[[0, 2], [1, 4.5]]) + #fig5.colorbar(h[3], ax=ax5) + H, xedges, yedges = np.histogram2d(xData, yData, bins=nBins) + cmax = 11 + H_clipped = np.clip(H, None, cmax) + im = ax5.imshow(H_clipped.T, origin='lower', interpolation='nearest', + extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], + aspect='auto', cmap='viridis') + fig5.colorbar(im, label='Counts', ax=ax5) + ax5.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$')) + ax5.xaxis.set_major_locator(MaxNLocator(5)) + #ax5.legend() + + #fig.tight_layout() + plt.show() \ No newline at end of file diff --git a/main/astrodatagui/db/StarsDB.py b/main/astrodatagui/db/StarsDB.py index 6dbb851..ffcb327 100644 --- a/main/astrodatagui/db/StarsDB.py +++ b/main/astrodatagui/db/StarsDB.py @@ -163,14 +163,32 @@ class StarDB(): else: raise Exception(f"Fit type {fitType} not supported, must be one of {supportedFitTypes}") + def updateStarInfoSpType(self, mainName, spType): + self.dbCursor.execute(f"""UPDATE starInfo + SET spType = '{spType}' + WHERE mainName = '{mainName}'""") + self.connection.commit() + def getAllStars(self): res = self.dbCursor.execute("""SELECT DISTINCT mainName FROM stars ORDER BY mainName""") resList = [] for s in res.fetchall(): resList.append(s[0]) + print(f"Loading {len(resList)} stars") return resList - + + def getAllStarsWithSpType(self, spType: str): + res = self.dbCursor.execute(f"""SELECT DISTINCT mainName + FROM stars INNER JOIN starInfo USING(mainName) + WHERE spType LIKE '{spType}%' + ORDER BY mainName""") + resList = [] + for s in res.fetchall(): + resList.append(s[0]) + print(f"Loading {len(resList)} stars") + return resList + def getStarSequences(self, mainName): res = self.dbCursor.execute(f"""SELECT sourceName, sequence FROM stars WHERE mainName = \"{mainName}\" diff --git a/main/astrodatagui/ui/NewStarDialog.py b/main/astrodatagui/ui/NewStarDialog.py index 01f6bcc..96f3cf7 100644 --- a/main/astrodatagui/ui/NewStarDialog.py +++ b/main/astrodatagui/ui/NewStarDialog.py @@ -1,3 +1,4 @@ +from types import NoneType from PyQt5 import QtWidgets from PyQt5.QtWidgets import QDialog, QListWidgetItem from PyQt5 import uic @@ -8,6 +9,62 @@ from astropy.table import vstack from ...astrodatadownloader.astrodatadownloader import (ObservationSource, getStarObservations, downloadStarProducts) +from astroquery.simbad import Simbad +import multiprocessing +import concurrent.futures +from astropy.table import Table + +def createEmptyObsTable(): + table = Table() + table['obsID'] = [] + table['obs_collection'] = [] + table['dataproduct_type'] = [] + table['obs_id'] = [] + table['description'] = [] + table['type'] = [] + table['dataURI'] = [] + table['productType'] = [] + table['productGroupDescription'] = [] + table['productSubGroupDescription'] = [] + table['productDocumentationURL'] = [] + table['project'] = [] + table['prvversion'] = [] + table['proposal_id'] = [] + table['productFilename'] = [] + table['size'] = [] + table['parent_obsid'] = [] + table['dataRights'] = [] + table['calib_level'] = [] + table['filters'] = [] + table['sequence_number'] = [] + table['starName'] = [] + return table + +def getStarObsParallel(starName, keplerKadences, k2Kadences, sources): + simbad = Simbad() + s = starName.strip() + altNames = simbad.query_objectids(s) + if(type(altNames) == NoneType): + altNames = [s] + else: + altNames = list(altNames["ID"]) + print("Trying for " + ", ".join(altNames)) + obs = createEmptyObsTable() + for name in altNames: + try: + obs = getStarObservations(name, keplerKadences, k2Kadences, sources) + if(len(obs) == 0): + continue; + + obs['starName'] = name + break + except: + print(f"Could not find data for {name}") + return obs + +def getStarObsParallelWrapper(args): + return getStarObsParallel(*args) + class NewStarDialog(QDialog): def __init__(self): super().__init__() @@ -27,6 +84,8 @@ class NewStarDialog(QDialog): QtWidgets.QMessageBox.Ok) def btFetchData_clicked(self): + from astroquery.simbad import Simbad + simbad = Simbad() star = self.leStarIdentifier.text() if not star: self.showErrorMessage("No star identifier", @@ -51,23 +110,43 @@ class NewStarDialog(QDialog): k2Kadences = [self.cbK2ShortCadence.isChecked(), self.cbK2LongCadence.isChecked()] - allObs = [] + #allObs = [] stars = star.split(";") + starsRet = [] self.listPreview.clear() - for s in stars: - s = s.strip() - obs = getStarObservations(s, keplerKadences, k2Kadences, sources) - if(len(obs) == 0): - self.showErrorMessage("No observations found", - "No observationnal data has been found with the current filters") - return - allObs.append(obs) - for o in obs: - self.listPreview.addItem(QListWidgetItem(f"{s} - {o['obs_collection']} - {o['sequence_number']}")) + #for s in stars: + # s = s.strip() + # altNames = simbad.query_objectids(s) + # if(type(altNames) == NoneType): + # altNames = [s] + # else: + # altNames = list(altNames["ID"]) + # print("Trying for " + ", ".join(altNames)) + # for name in altNames: + # try: + # obs = getStarObservations(name, keplerKadences, k2Kadences, sources) + # if(len(obs) == 0): + # self.showErrorMessage("No observations found", + # "No observationnal data has been found with the current filters") + # return + # allObs.append(obs) +# +# for o in obs: +# self.listPreview.addItem(QListWidgetItem(f"{name} - {o['obs_collection']} - {o['sequence_number']}")) +# starsRet.append(name) +# break +# except: +# print(f"Could not find data for {name}") + cpuCount = multiprocessing.cpu_count() + executor = concurrent.futures.ThreadPoolExecutor(500) + args = ((starName, keplerKadences, k2Kadences, sources) for starName in stars) + allObs = list(executor.map(getStarObsParallelWrapper, args)) obs = vstack(allObs) - + for o in obs: + self.listPreview.addItem(QListWidgetItem(f"{o['starName']} - {o['obs_collection']} - {o['sequence_number']}")) self.currentObservations = obs - self.starIdentifier = star + self.starIdentifier = ";".join(starsRet) + self.leStarIdentifier.setText(self.starIdentifier) def btOk_clicked(self): if self.currentObservations: diff --git a/main/astrodatagui/ui/NewStarDialog.ui b/main/astrodatagui/ui/NewStarDialog.ui index 6a355fb..22aaafa 100644 --- a/main/astrodatagui/ui/NewStarDialog.ui +++ b/main/astrodatagui/ui/NewStarDialog.ui @@ -78,6 +78,9 @@ 16777215 + + 999999 + diff --git a/stars.db b/stars.db index 202f19d..a5be937 100644 Binary files a/stars.db and b/stars.db differ