generate_plots: fix multithreaded plot generation
This commit is contained in:
+50
-19
@@ -184,9 +184,7 @@ def plotStar(data, showSourceFilter, folderPath, starName):
|
||||
finalData = pd.concat([finalData, data[nameFilter]], ignore_index=True)
|
||||
|
||||
pdcsapbinningData = []
|
||||
pdcsapbinningDataSpotModDiffPeriod = []
|
||||
foldedFits = []
|
||||
foldedPeriodFits = []
|
||||
locFolder = f"{folderPath}/stars/{starNameR}/"
|
||||
mkdir_p(f"{locFolder}/")
|
||||
csvFile = open(f"{locFolder}/{starNameR}.csv", "a")
|
||||
@@ -208,19 +206,9 @@ def plotStar(data, showSourceFilter, folderPath, starName):
|
||||
if(peak["FlarePeak"] > 100):
|
||||
print(row["StarName"], "has over 100 peak")
|
||||
foldedFits.append([normalizePhase(row["pdcsapFoldedFitPhase"]), row["pdcsapFoldedFit"]])
|
||||
if(row["pdcsapPeriodFoldedPhase"] is not None):
|
||||
periodPdcsapVals = pd.DataFrame(row["pdcsapPeriodFoldedPeaksPhasePair"])
|
||||
for td, peak in zip(periodPdcsapVals["Phase"], periodPdcsapVals["Peak"]):
|
||||
normPhasePeriod = normalizePhase(td.value, np.abs(row["pdcsapPeriodFoldedFitPhaseStarEnd"][0]), np.abs(row["pdcsapPeriodFoldedFitPhaseStarEnd"][1]))
|
||||
pdcsapbinningDataSpotModDiffPeriod.append({"SpType": f'{row["SpType"][0:2] if len(row["SpType"]) > 1 else row["SpType"][0]}',
|
||||
"PDCSAPNormPhasePeriod": normPhasePeriod,
|
||||
"PeakPeriod": peak["FlarePeak"]})
|
||||
if(peak["FlarePeak"] > 100):
|
||||
print(row["StarName"], "has over 100 peak")
|
||||
foldedPeriodFits.append([normalizePhase(row["pdcsapPeriodFoldedFitPhase"]), row["pdcsapPeriodFoldedFit"]])
|
||||
|
||||
csvFile.close()
|
||||
pdcsapbinningData = pd.DataFrame(pdcsapbinningData)
|
||||
pdcsapbinningDataSpotModDiffPeriod = pd.DataFrame(pdcsapbinningDataSpotModDiffPeriod) if len(pdcsapbinningDataSpotModDiffPeriod) > 0 else None
|
||||
PDCSAPdataList = []
|
||||
PDCSAPlabelList = []
|
||||
PDCSAPcolorList = []
|
||||
@@ -251,6 +239,42 @@ def plotStar(data, showSourceFilter, folderPath, starName):
|
||||
pdcsapbinningData, None, f"{starName}", color, f"Flare peaks per phase of {starName}", f"{locFolder}/{starNameR}-Flarepeaks_maxY-@maxY.png",
|
||||
foldedFits)
|
||||
|
||||
def plotStarPeriod(data, showSourceFilter, folderPath, starName):
|
||||
finalData = pd.DataFrame()
|
||||
starNameR = starName.replace('*', '_star_')
|
||||
nameFilter = data["StarName"] == starName
|
||||
nameFilter &= showSourceFilter
|
||||
finalData = pd.concat([finalData, data[nameFilter]], ignore_index=True)
|
||||
|
||||
pdcsapbinningData = []
|
||||
pdcsapbinningDataSpotModDiffPeriod = []
|
||||
foldedFits = []
|
||||
foldedPeriodFits = []
|
||||
locFolder = f"{folderPath}/stars/{starNameR}/"
|
||||
mkdir_p(f"{locFolder}/")
|
||||
csvFile = open(f"{locFolder}/{starNameR}_Period.csv", "a")
|
||||
csvFile.write("Star Name,Spectral Type,Source,File,Flare Time,Flare Peak,Period,Spot Modulation,Normalized Phase of Peak,Peak in Period")
|
||||
csvFile.write("\n")
|
||||
for ind, row in finalData.reset_index().iterrows():
|
||||
PDCSAPminOrigPhase = row["pdcsapPeriodFoldedFitPhaseStarEnd"][0]
|
||||
PDCSAPmaxOrigPhase = row["pdcsapPeriodFoldedFitPhaseStarEnd"][1]
|
||||
|
||||
if(len(row["pdcsapPeriodFoldedPeaksPhasePair"]) > 0):
|
||||
pdcsapVals = pd.DataFrame(row["pdcsapPeriodFoldedPeaksPhasePair"])
|
||||
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']},{row['pdcsapSpotModulation']},{normPhase},{peak['FlarePeak']}")
|
||||
csvFile.write("\n")
|
||||
pdcsapbinningData.append({"SpType": f'{row["SpType"][0:2] if len(row["SpType"]) > 1 else row["SpType"][0]}',
|
||||
"PDCSAPNormPhasePeriod": normPhase,
|
||||
"PeakPeriod": peak["FlarePeak"]})
|
||||
if(peak["FlarePeak"] > 100):
|
||||
print(row["StarName"], "has over 100 peak")
|
||||
foldedFits.append([normalizePhase(row["pdcsapFoldedFitPhase"]), row["pdcsapFoldedFit"]])
|
||||
|
||||
csvFile.close()
|
||||
pdcsapbinningDataSpotModDiffPeriod = pd.DataFrame(pdcsapbinningDataSpotModDiffPeriod) if len(pdcsapbinningDataSpotModDiffPeriod) > 0 else None
|
||||
|
||||
if(pdcsapbinningDataSpotModDiffPeriod is not None):
|
||||
PDCSAPdataListPeriod = []
|
||||
PDCSAPlabelList = []
|
||||
@@ -284,6 +308,7 @@ def plotStar(data, showSourceFilter, folderPath, starName):
|
||||
pdcsapbinningDataSpotModDiffPeriod, None, f"{starName}", color, f"Flare peaks per phase of {starName}", f"{locFolder}/{starNameR}-Flarepeaks_maxY-@maxY_Period.png",
|
||||
foldedPeriodFits)
|
||||
|
||||
|
||||
def plotCombo(data, showSourceFilter, folderPath, combo):
|
||||
for maxFlarePeak in [1.01, 1.05, 1.1, 1.25, 1.5]:
|
||||
# max Flare Peak cut
|
||||
@@ -751,14 +776,15 @@ def plotCombo(data, showSourceFilter, folderPath, combo):
|
||||
xDataO, yDataO, f"Flare peak per phase histogram of {', '.join(combo)} type stars with @bins bins (Rot. Period over {periodCut} days)", f"{locFolder}/{''.join(combo)}-Flarepeaks-Period_o_{periodCut}-@bins_Bins_maxY-@maxY.png",
|
||||
pdcsapbinningDataO, plotFiltersO, PDCSAPlabelList, PDCSAPcolorList, f"Flare peaks per phase of {', '.join(combo)} type stars (Rot. Period over {periodCut} days)", f"{locFolder}/{''.join(combo)}-Flarepeaks-Period_o_{periodCut}-maxY_@maxY.png")
|
||||
|
||||
|
||||
binList = [10, 20, 30]
|
||||
spType = ["M", "K", "G", "F"]
|
||||
periodsCutList = [0.5, 1, 1.5, 2, 5, 10, 15, 20]
|
||||
|
||||
if __name__ == "__main__":
|
||||
fileName = "datav5.1.cff"
|
||||
fullData = pd.read_pickle(fileName)
|
||||
starDB: StarDB = StarDB.getInstance("stars.db")
|
||||
allStars = starDB.getAllStars()
|
||||
|
||||
useKepler = True
|
||||
useK2 = True
|
||||
@@ -769,8 +795,7 @@ if __name__ == "__main__":
|
||||
time = f"{current.hour}-{current.minute}-{current.second}"
|
||||
|
||||
cpuCount = multiprocessing.cpu_count()
|
||||
executor = concurrent.futures.ProcessPoolExecutor(cpuCount)
|
||||
#executor = concurrent.futures.ThreadPoolExecutor(cpuCount)
|
||||
pool = multiprocessing.Pool(processes=cpuCount)
|
||||
|
||||
foldedFitTypes = ["sine", "poly"]
|
||||
for foldedFitTypesLength in range(1, len(foldedFitTypes)+1):
|
||||
@@ -778,12 +803,16 @@ if __name__ == "__main__":
|
||||
folderPath = f"../{date}-{'-'.join(foldedFitTypeCombo)}/"
|
||||
mkdir_p(folderPath)
|
||||
foldedFitTypeComboFilter = np.full(len(fullData), False)
|
||||
foldedFitTypeComboFilterPeriod = np.full(len(fullData), False)
|
||||
if("sine" in foldedFitTypeCombo):
|
||||
foldedFitTypeComboFilter |= fullData["FitType"] == "sine"
|
||||
foldedFitTypeComboFilterPeriod |= fullData["periodFitType"] == "sine"
|
||||
if("poly" in foldedFitTypeCombo):
|
||||
foldedFitTypeComboFilter |= fullData["FitType"] == "poly"
|
||||
foldedFitTypeComboFilterPeriod |= fullData["periodFitType"] == "poly"
|
||||
|
||||
data = fullData[(foldedFitTypeComboFilter) & (fullData["isValidFold"])]
|
||||
dataPeriod = fullData[(foldedFitTypeComboFilterPeriod) & (fullData["isValidFold"])]
|
||||
|
||||
showSourceFilter = np.full(len(data), False)
|
||||
if(useKepler):
|
||||
@@ -807,12 +836,14 @@ if __name__ == "__main__":
|
||||
"PeriodWithinStd": allValuesWithin3Std(periods)})
|
||||
|
||||
validStarPeriodMap = pd.DataFrame(validStarPeriodMap)
|
||||
periodsCutList = [0.5, 1, 1.5, 2, 5, 10, 15, 20]
|
||||
|
||||
data = pd.merge(data, validStarPeriodMap, on="StarName")
|
||||
|
||||
starPlotFunc = partial(plotStar, data, showSourceFilter, folderPath)
|
||||
list(executor.map(starPlotFunc, starDB.getAllStars())) # wrap in list, to force evaluation
|
||||
list(pool.map(starPlotFunc, allStars)) # wrap in list, to force evaluation
|
||||
|
||||
starPlotPeriodFunc = partial(plotStarPeriod, dataPeriod, showSourceFilter, folderPath)
|
||||
list(pool.map(starPlotPeriodFunc, allStars)) # wrap in list, to force evaluation
|
||||
|
||||
combos = []
|
||||
for comboLength in range(1, len(spType) + 1):
|
||||
@@ -820,4 +851,4 @@ if __name__ == "__main__":
|
||||
combos.append(combo)
|
||||
|
||||
plotComboFunc = partial(plotCombo, data, showSourceFilter, folderPath)
|
||||
list(executor.map(plotComboFunc, combos))
|
||||
list(pool.map(plotComboFunc, combos)) # wrap in list, to force evaluation
|
||||
|
||||
Reference in New Issue
Block a user