Skip to content

Commit

Permalink
Save global fit and minos plots
Browse files Browse the repository at this point in the history
Save some plots by default that were not being saved
  • Loading branch information
GuiMacielPereira committed Dec 13, 2024
1 parent 88f8a01 commit 6cfcba8
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/mvesuvio/analysis_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ def saveMinuitPlot(yFitIC, wsMinuitFit, mObj):
fig, ax = plt.subplots(subplot_kw={"projection": "mantid"})
ax.errorbar(wsMinuitFit, "k.", wkspIndex=0, label="Weighted Avg")
ax.errorbar(wsMinuitFit, "r-", wkspIndex=1, label=leg)
ax.plot(wsMinuitFit, "b.", wkspIndex=2, label="Residuals")
ax.set_xlabel("YSpace")
ax.set_ylabel("Counts")
ax.set_title("Minuit Fit")
Expand Down Expand Up @@ -933,12 +934,12 @@ def runMinos(mObj, yFitIC, constrFunc, wsName):
minosManErr = list(np.zeros(np.array(minosAutoErr).shape))

if yFitIC.show_plots:
plotAutoMinos(mObj, wsName)
plotAutoMinos(mObj, wsName, yFitIC)

else: # Case with positivity constraint on function, use manual implementation
# Changes values of minuit obj m, do not use m below this point
merrors, fig = runAndPlotManualMinos(
mObj, constrFunc, bestFitVals, bestFitErrs, yFitIC.show_plots
mObj, constrFunc, bestFitVals, bestFitErrs, yFitIC.show_plots, yFitIC, wsName
)

# Same as above, but the other way around
Expand All @@ -948,13 +949,12 @@ def runMinos(mObj, yFitIC, constrFunc, wsName):
minosAutoErr = list(np.zeros(np.array(minosManErr).shape))

if yFitIC.show_plots:
fig.canvas.manager.set_window_title(wsName + "_Manual_Implementation_MINOS")
fig.show()

return parameters, values, errors, minosAutoErr, minosManErr


def runAndPlotManualMinos(minuitObj, constrFunc, bestFitVals, bestFitErrs, showPlots):
def runAndPlotManualMinos(minuitObj, constrFunc, bestFitVals, bestFitErrs, showPlots, yFitIC, wsName):
"""
Runs brute implementation of minos algorithm and
plots the profile for each parameter along the way.
Expand All @@ -975,7 +975,7 @@ def runAndPlotManualMinos(minuitObj, constrFunc, bestFitVals, bestFitErrs, showP
figsize=figsize,
subplot_kw={"projection": "mantid"},
) # subplot_kw={'projection':'mantid'}
# fig.canvas.manager.set_window_title("Plot of Manual Implementation MINOS")
fig.canvas.manager.set_window_title(wsName + "_minos")

merrors = {}
for p, ax in zip(minuitObj.parameters, axs.flat):
Expand All @@ -992,6 +992,8 @@ def runAndPlotManualMinos(minuitObj, constrFunc, bestFitVals, bestFitErrs, showP
# ALl axes share same legend, so set figure legend to first axis
handle, label = axs[0, 0].get_legend_handles_labels()
fig.legend(handle, label, loc="lower right")
savePath = yFitIC.figSavePath / fig.canvas.manager.get_window_title()
plt.savefig(savePath, bbox_inches="tight")
return merrors, fig


Expand Down Expand Up @@ -1104,7 +1106,7 @@ def errsFromMinosCurve(varSpace, varVal, fValsScipy, fValsMin, dChi2=1):
return lerr, uerr


def plotAutoMinos(minuitObj, wsName):
def plotAutoMinos(minuitObj, wsName, yFitIC):
# Set format of subplots
height = 2
width = int(np.ceil(len(minuitObj.parameters) / 2))
Expand All @@ -1117,7 +1119,7 @@ def plotAutoMinos(minuitObj, wsName):
figsize=figsize,
subplot_kw={"projection": "mantid"},
)
fig.canvas.manager.set_window_title(wsName + "_Plot_Automatic_MINOS")
fig.canvas.manager.set_window_title(wsName + "_autominos")

for p, ax in zip(minuitObj.parameters, axs.flat):
loc, fvals, status = minuitObj.mnprofile(p, bound=2)
Expand All @@ -1137,6 +1139,8 @@ def plotAutoMinos(minuitObj, wsName):
# ALl axes share same legend, so set figure legend to first axis
handle, label = axs[0, 0].get_legend_handles_labels()
fig.legend(handle, label, loc="lower right")
savePath = yFitIC.figSavePath / fig.canvas.manager.get_window_title()
plt.savefig(savePath, bbox_inches="tight")
fig.show()


Expand Down Expand Up @@ -1476,7 +1480,7 @@ def constr(*pars):
print("\n")

if yFitIC.show_plots:
plotGlobalFit(dataX, dataY, dataE, m, totCost, wsYSpace.name())
plotGlobalFit(dataX, dataY, dataE, m, totCost, wsYSpace.name(), yFitIC)

return np.array(m.values), np.array(
m.errors
Expand Down Expand Up @@ -1847,7 +1851,7 @@ def minuitInitialParameters(defaultPars, sharedPars, nSpec):
return initPars


def plotGlobalFit(dataX, dataY, dataE, mObj, totCost, wsName):
def plotGlobalFit(dataX, dataY, dataE, mObj, totCost, wsName, yFitIC):
if len(dataY) > 10:
print("\nToo many axes to show in figure, skipping the plot ...\n")
return
Expand All @@ -1860,7 +1864,7 @@ def plotGlobalFit(dataX, dataY, dataE, mObj, totCost, wsName):
tight_layout=True,
subplot_kw={"projection": "mantid"},
)
fig.canvas.manager.set_window_title(wsName + "_Plot_of_Global_Fit")
fig.canvas.manager.set_window_title(wsName + "_fitglobal")

# Data used in Global Fit
for i, (x, y, yerr, ax) in enumerate(zip(dataX, dataY, dataE, axs.flat)):
Expand All @@ -1882,5 +1886,7 @@ def plotGlobalFit(dataX, dataY, dataE, mObj, totCost, wsName):

ax.fill_between(x, yfit, label="\n".join(leg), alpha=0.4)
ax.legend()
savePath = yFitIC.figSavePath / fig.canvas.manager.get_window_title()
plt.savefig(savePath, bbox_inches="tight")
fig.show()
return

0 comments on commit 6cfcba8

Please sign in to comment.