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)
|
finalData = pd.concat([finalData, data[nameFilter]], ignore_index=True)
|
||||||
|
|
||||||
pdcsapbinningData = []
|
pdcsapbinningData = []
|
||||||
pdcsapbinningDataSpotModDiffPeriod = []
|
|
||||||
foldedFits = []
|
foldedFits = []
|
||||||
foldedPeriodFits = []
|
|
||||||
locFolder = f"{folderPath}/stars/{starNameR}/"
|
locFolder = f"{folderPath}/stars/{starNameR}/"
|
||||||
mkdir_p(f"{locFolder}/")
|
mkdir_p(f"{locFolder}/")
|
||||||
csvFile = open(f"{locFolder}/{starNameR}.csv", "a")
|
csvFile = open(f"{locFolder}/{starNameR}.csv", "a")
|
||||||
@@ -208,19 +206,9 @@ def plotStar(data, showSourceFilter, folderPath, starName):
|
|||||||
if(peak["FlarePeak"] > 100):
|
if(peak["FlarePeak"] > 100):
|
||||||
print(row["StarName"], "has over 100 peak")
|
print(row["StarName"], "has over 100 peak")
|
||||||
foldedFits.append([normalizePhase(row["pdcsapFoldedFitPhase"]), row["pdcsapFoldedFit"]])
|
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()
|
csvFile.close()
|
||||||
pdcsapbinningData = pd.DataFrame(pdcsapbinningData)
|
pdcsapbinningData = pd.DataFrame(pdcsapbinningData)
|
||||||
pdcsapbinningDataSpotModDiffPeriod = pd.DataFrame(pdcsapbinningDataSpotModDiffPeriod) if len(pdcsapbinningDataSpotModDiffPeriod) > 0 else None
|
|
||||||
PDCSAPdataList = []
|
PDCSAPdataList = []
|
||||||
PDCSAPlabelList = []
|
PDCSAPlabelList = []
|
||||||
PDCSAPcolorList = []
|
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",
|
pdcsapbinningData, None, f"{starName}", color, f"Flare peaks per phase of {starName}", f"{locFolder}/{starNameR}-Flarepeaks_maxY-@maxY.png",
|
||||||
foldedFits)
|
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):
|
if(pdcsapbinningDataSpotModDiffPeriod is not None):
|
||||||
PDCSAPdataListPeriod = []
|
PDCSAPdataListPeriod = []
|
||||||
PDCSAPlabelList = []
|
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",
|
pdcsapbinningDataSpotModDiffPeriod, None, f"{starName}", color, f"Flare peaks per phase of {starName}", f"{locFolder}/{starNameR}-Flarepeaks_maxY-@maxY_Period.png",
|
||||||
foldedPeriodFits)
|
foldedPeriodFits)
|
||||||
|
|
||||||
|
|
||||||
def plotCombo(data, showSourceFilter, folderPath, combo):
|
def plotCombo(data, showSourceFilter, folderPath, combo):
|
||||||
for maxFlarePeak in [1.01, 1.05, 1.1, 1.25, 1.5]:
|
for maxFlarePeak in [1.01, 1.05, 1.1, 1.25, 1.5]:
|
||||||
# max Flare Peak cut
|
# 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",
|
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")
|
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]
|
binList = [10, 20, 30]
|
||||||
spType = ["M", "K", "G", "F"]
|
spType = ["M", "K", "G", "F"]
|
||||||
|
periodsCutList = [0.5, 1, 1.5, 2, 5, 10, 15, 20]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
fileName = "datav5.1.cff"
|
fileName = "datav5.1.cff"
|
||||||
fullData = pd.read_pickle(fileName)
|
fullData = pd.read_pickle(fileName)
|
||||||
starDB: StarDB = StarDB.getInstance("stars.db")
|
starDB: StarDB = StarDB.getInstance("stars.db")
|
||||||
|
allStars = starDB.getAllStars()
|
||||||
|
|
||||||
useKepler = True
|
useKepler = True
|
||||||
useK2 = True
|
useK2 = True
|
||||||
@@ -769,8 +795,7 @@ if __name__ == "__main__":
|
|||||||
time = f"{current.hour}-{current.minute}-{current.second}"
|
time = f"{current.hour}-{current.minute}-{current.second}"
|
||||||
|
|
||||||
cpuCount = multiprocessing.cpu_count()
|
cpuCount = multiprocessing.cpu_count()
|
||||||
executor = concurrent.futures.ProcessPoolExecutor(cpuCount)
|
pool = multiprocessing.Pool(processes=cpuCount)
|
||||||
#executor = concurrent.futures.ThreadPoolExecutor(cpuCount)
|
|
||||||
|
|
||||||
foldedFitTypes = ["sine", "poly"]
|
foldedFitTypes = ["sine", "poly"]
|
||||||
for foldedFitTypesLength in range(1, len(foldedFitTypes)+1):
|
for foldedFitTypesLength in range(1, len(foldedFitTypes)+1):
|
||||||
@@ -778,12 +803,16 @@ if __name__ == "__main__":
|
|||||||
folderPath = f"../{date}-{'-'.join(foldedFitTypeCombo)}/"
|
folderPath = f"../{date}-{'-'.join(foldedFitTypeCombo)}/"
|
||||||
mkdir_p(folderPath)
|
mkdir_p(folderPath)
|
||||||
foldedFitTypeComboFilter = np.full(len(fullData), False)
|
foldedFitTypeComboFilter = np.full(len(fullData), False)
|
||||||
|
foldedFitTypeComboFilterPeriod = np.full(len(fullData), False)
|
||||||
if("sine" in foldedFitTypeCombo):
|
if("sine" in foldedFitTypeCombo):
|
||||||
foldedFitTypeComboFilter |= fullData["FitType"] == "sine"
|
foldedFitTypeComboFilter |= fullData["FitType"] == "sine"
|
||||||
|
foldedFitTypeComboFilterPeriod |= fullData["periodFitType"] == "sine"
|
||||||
if("poly" in foldedFitTypeCombo):
|
if("poly" in foldedFitTypeCombo):
|
||||||
foldedFitTypeComboFilter |= fullData["FitType"] == "poly"
|
foldedFitTypeComboFilter |= fullData["FitType"] == "poly"
|
||||||
|
foldedFitTypeComboFilterPeriod |= fullData["periodFitType"] == "poly"
|
||||||
|
|
||||||
data = fullData[(foldedFitTypeComboFilter) & (fullData["isValidFold"])]
|
data = fullData[(foldedFitTypeComboFilter) & (fullData["isValidFold"])]
|
||||||
|
dataPeriod = fullData[(foldedFitTypeComboFilterPeriod) & (fullData["isValidFold"])]
|
||||||
|
|
||||||
showSourceFilter = np.full(len(data), False)
|
showSourceFilter = np.full(len(data), False)
|
||||||
if(useKepler):
|
if(useKepler):
|
||||||
@@ -807,12 +836,14 @@ if __name__ == "__main__":
|
|||||||
"PeriodWithinStd": allValuesWithin3Std(periods)})
|
"PeriodWithinStd": allValuesWithin3Std(periods)})
|
||||||
|
|
||||||
validStarPeriodMap = pd.DataFrame(validStarPeriodMap)
|
validStarPeriodMap = pd.DataFrame(validStarPeriodMap)
|
||||||
periodsCutList = [0.5, 1, 1.5, 2, 5, 10, 15, 20]
|
|
||||||
|
|
||||||
data = pd.merge(data, validStarPeriodMap, on="StarName")
|
data = pd.merge(data, validStarPeriodMap, on="StarName")
|
||||||
|
|
||||||
starPlotFunc = partial(plotStar, data, showSourceFilter, folderPath)
|
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 = []
|
combos = []
|
||||||
for comboLength in range(1, len(spType) + 1):
|
for comboLength in range(1, len(spType) + 1):
|
||||||
@@ -820,4 +851,4 @@ if __name__ == "__main__":
|
|||||||
combos.append(combo)
|
combos.append(combo)
|
||||||
|
|
||||||
plotComboFunc = partial(plotCombo, data, showSourceFilter, folderPath)
|
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