Skip to content

Commit

Permalink
Fix analysis fitting
Browse files Browse the repository at this point in the history
The fittnig routine was failing due to the naming conventions having
changed. Updated routine so that it finds the correct ncp workspaces.
Updated the system tests.
  • Loading branch information
GuiMacielPereira committed Sep 16, 2024
1 parent c5e0db6 commit fad0c77
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
28 changes: 20 additions & 8 deletions src/mvesuvio/analysis_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@


def fitInYSpaceProcedure(yFitIC, IC, wsTOF):

try:
wsTOF = mtd[wsTOF]
except KeyError:
print(f"Workspace to fit {wsTOF} not found.")
return

ncpForEachMass = extractNCPFromWorkspaces(wsTOF, IC)
wsResSum, wsRes = calculateMantidResolutionFirstMass(IC, yFitIC, wsTOF)

Expand Down Expand Up @@ -42,14 +49,19 @@ def fitInYSpaceProcedure(yFitIC, IC, wsTOF):
def extractNCPFromWorkspaces(wsFinal, ic):
"""Extra function to extract ncps from loaded ws in mantid."""

ncpForEachMass = mtd[wsFinal.name() + "_TOF_Fitted_Profile_0"].extractY()[
np.newaxis, :, :
]
for i in range(1, ic.noOfMasses):
ncpToAppend = mtd[wsFinal.name() + "_TOF_Fitted_Profile_" + str(i)].extractY()[
np.newaxis, :, :
]
ncpForEachMass = np.append(ncpForEachMass, ncpToAppend, axis=0)
for ws_name in mtd.getObjectNames():
if ws_name.startswith(ic.name+'_'+str(ic.noOfMSIterations)) and ws_name.endswith('ncp'):

if 'total' in ws_name:
continue

print(ws_name)
ws = mtd[ws_name]
dataY = ws.extractY()[np.newaxis, :, :]
try:
ncpForEachMass = np.append(ncpForEachMass, dataY, axis=0)
except UnboundLocalError:
ncpForEachMass = dataY

# Ensure shape of ncp matches data
shape = ncpForEachMass.shape
Expand Down
2 changes: 1 addition & 1 deletion src/mvesuvio/analysis_routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from mvesuvio.util.analysis_helpers import fix_profile_parameters, \
loadRawAndEmptyWsFromUserPath, cropAndMaskWorkspace, \
NeutronComptonProfile, calculate_h_ratio, serialize_lambdas
NeutronComptonProfile, calculate_h_ratio
from mvesuvio.analysis_reduction import AnalysisRoutine
from tests.testhelpers.calibration.algorithms import create_algorithm

Expand Down
6 changes: 3 additions & 3 deletions src/mvesuvio/run_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def runProcedure():
ICs = []
for mode, IC in zip(["BACKWARD", "FORWARD"], [bckwdIC, fwdIC]):
if (userCtr.fitInYSpace == mode) | (userCtr.fitInYSpace == "JOINT"):
wsNames.append(buildFinalWSName(mode, IC))
wsNames.append(IC.name + '_' + str(IC.noOfMSIterations))
ICs.append(IC)

# Default workflow for procedure + fit in y space
Expand All @@ -69,15 +69,15 @@ def runProcedure():
wsInMtd
): # When wsName is empty list, loop doesn't run
for wsName, IC in zip(wsNames, ICs):
resYFit = fitInYSpaceProcedure(yFitIC, IC, mtd[wsName])
resYFit = fitInYSpaceProcedure(yFitIC, IC, wsName)
return None, resYFit # To match return below.

checkUserClearWS(yes_to_all) # Check if user is OK with cleaning all workspaces
res = runProcedure()

resYFit = None
for wsName, IC in zip(wsNames, ICs):
resYFit = fitInYSpaceProcedure(yFitIC, IC, mtd[wsName])
resYFit = fitInYSpaceProcedure(yFitIC, IC, wsName)

return res, resYFit # Return results used only in tests

Expand Down
4 changes: 2 additions & 2 deletions tests/system/analysis/test_yspace_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ def _load_workspaces(cls):
AnalysisDataService.clear()
wsFinal = Load(
str(cls._input_data_path / "wsFinal.nxs"),
OutputWorkspace=scriptName + "_FORWARD_1",
OutputWorkspace=scriptName + "_fwd_1",
)
for i in range(len(fwdIC.masses)):
fileName = "wsFinal_ncp_" + str(i) + ".nxs"
Load(
str(cls._input_data_path / fileName),
OutputWorkspace=wsFinal.name() + "_TOF_Fitted_Profile_" + str(i),
OutputWorkspace=wsFinal.name() + "_label" + str(i) +"_ncp",
)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/system/analysis/test_yspace_fit_GC.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def _load_workspaces(cls):
AnalysisDataService.clear()
wsFinal = Load(
str(cls._input_data_path / "wsFinal.nxs"),
OutputWorkspace=scriptName + "_FORWARD_1",
OutputWorkspace=scriptName + "_fwd_1",
)
for i in range(len(fwdIC.masses)):
fileName = "wsFinal_ncp_" + str(i) + ".nxs"
Load(
str(cls._input_data_path / fileName),
OutputWorkspace=wsFinal.name() + "_TOF_Fitted_Profile_" + str(i),
OutputWorkspace=wsFinal.name() + "_label" + str(i) +"_ncp",
)

@classmethod
Expand Down

0 comments on commit fad0c77

Please sign in to comment.