From fad0c77e04853368524d8b98d37ee2cb3101a8e1 Mon Sep 17 00:00:00 2001
From: GuiMacielPereira <gui.maciel-pereira@stfc.ac.uk>
Date: Mon, 16 Sep 2024 16:37:35 +0100
Subject: [PATCH] Fix analysis fitting

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.
---
 src/mvesuvio/analysis_fitting.py            | 28 +++++++++++++++------
 src/mvesuvio/analysis_routines.py           |  2 +-
 src/mvesuvio/run_routine.py                 |  6 ++---
 tests/system/analysis/test_yspace_fit.py    |  4 +--
 tests/system/analysis/test_yspace_fit_GC.py |  4 +--
 5 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/mvesuvio/analysis_fitting.py b/src/mvesuvio/analysis_fitting.py
index c8ea26e1..17a6c954 100644
--- a/src/mvesuvio/analysis_fitting.py
+++ b/src/mvesuvio/analysis_fitting.py
@@ -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)
 
@@ -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
diff --git a/src/mvesuvio/analysis_routines.py b/src/mvesuvio/analysis_routines.py
index 4bb74a8b..f8b5f55e 100644
--- a/src/mvesuvio/analysis_routines.py
+++ b/src/mvesuvio/analysis_routines.py
@@ -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
 
diff --git a/src/mvesuvio/run_routine.py b/src/mvesuvio/run_routine.py
index 29545374..9b4809b8 100644
--- a/src/mvesuvio/run_routine.py
+++ b/src/mvesuvio/run_routine.py
@@ -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
@@ -69,7 +69,7 @@ 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
@@ -77,7 +77,7 @@ def 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
 
diff --git a/tests/system/analysis/test_yspace_fit.py b/tests/system/analysis/test_yspace_fit.py
index 2bbf9529..bd709433 100644
--- a/tests/system/analysis/test_yspace_fit.py
+++ b/tests/system/analysis/test_yspace_fit.py
@@ -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
diff --git a/tests/system/analysis/test_yspace_fit_GC.py b/tests/system/analysis/test_yspace_fit_GC.py
index 71a9178e..88d5d612 100644
--- a/tests/system/analysis/test_yspace_fit_GC.py
+++ b/tests/system/analysis/test_yspace_fit_GC.py
@@ -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