Skip to content

Commit

Permalink
Fix system analysis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiMacielPereira committed Nov 8, 2024
1 parent 056987f commit 32ffc6b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 128 deletions.
17 changes: 8 additions & 9 deletions src/mvesuvio/main/run_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def run(self):
wsInMtd = [ws in mtd for ws in self.ws_to_fit_y_space] # Bool list
if (len(wsInMtd) > 0) and all(wsInMtd):
self.runAnalysisFitting()
return
return self.analysis_result, self.fitting_result

checkUserClearWS(self.yes_to_all) # Check if user is OK with cleaning all workspaces

Expand All @@ -85,13 +85,14 @@ def run(self):
end_time = time.time()
print("\nRunning time: ", end_time - start_time, " seconds")

return self.analysis_result, self.fitting_result # Return results used only in tests
# Return results used only in tests
return self.analysis_result, self.fitting_result


def runAnalysisFitting(self):
for wsName, i_cls in zip(self.ws_to_fit_y_space, self.classes_to_fit_y_space):
resYFit = fitInYSpaceProcedure(self.yFitIC, i_cls, wsName)
self.fitting_result = resYFit
self.fitting_result = fitInYSpaceProcedure(self.yFitIC, i_cls, wsName)
return


def runAnalysisRoutine(self):
Expand All @@ -111,13 +112,11 @@ def runAnalysisRoutine(self):
), "When H is not present, HToMassIdxRatio has to be set to None"

if routine_type == "BACKWARD":
res = runIndependentIterativeProcedure(self.bckwdIC, running_tests=self.running_tests)
self.analysis_result = runIndependentIterativeProcedure(self.bckwdIC, running_tests=self.running_tests)
if routine_type == "FORWARD":
res = runIndependentIterativeProcedure(self.fwdIC, running_tests=self.running_tests)
self.analysis_result = runIndependentIterativeProcedure(self.fwdIC, running_tests=self.running_tests)
if routine_type == "JOINT":
res = runJointBackAndForwardProcedure(self.bckwdIC, self.fwdIC)

self.analysis_result = res
self.analysis_result = runJointBackAndForwardProcedure(self.bckwdIC, self.fwdIC)
return


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@


class LoadVesuvioBackParameters:
def __init__(self, ipFilesPath):
self.ipfile = ipFilesPath / "ip2019.par"
ipfile = "ip2019.par"

runs = "43066-43076" # 77K # The numbers of the runs to be analysed
empty_runs = (
Expand All @@ -17,8 +16,7 @@ def __init__(self, ipFilesPath):


class LoadVesuvioFrontParameters:
def __init__(self, ipFilesPath):
self.ipfile = ipFilesPath / "ip2018_3.par"
ipfile = "ip2018_3.par"

runs = "43066-43076" # 100K # The numbers of the runs to be analysed
empty_runs = (
Expand All @@ -38,8 +36,7 @@ class GeneralInitialConditions:


class BackwardInitialConditions(GeneralInitialConditions):
def __init__(self, ipFilesPath):
self.InstrParsPath = ipFilesPath / "ip2018_3.par"
instrParsFile = "ip2018_3.par"

HToMassIdxRatio = 19.0620008206 # Set to zero or None when H is not present
massIdx = 0
Expand Down Expand Up @@ -67,13 +64,10 @@ def __init__(self, ipFilesPath):
GammaCorrectionFlag = False
tofBinning = "275.,1.,420" # Binning of ToF spectra
maskTOFRange = None
runHistData = True
normVoigt = False


class ForwardInitialConditions(GeneralInitialConditions):
def __init__(self, ipFilesPath):
self.InstrParsPath = ipFilesPath / "ip2018_3.par"
instrParsFile = "ip2018_3.par"

HToMassIdxRatio = 0 # New way to ignore ratio

Expand Down Expand Up @@ -104,8 +98,6 @@ def __init__(self, ipFilesPath):
maskedSpecAllNo = np.array([173, 174, 179])
tofBinning = "110,1,430" # Binning of ToF spectra
maskTOFRange = None
runHistData = True
normVoigt = False


class YSpaceFitInitialConditions:
Expand Down
17 changes: 17 additions & 0 deletions tests/data/analysis/inputs/yspace_gauss_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from tests.data.analysis.inputs.analysis_test import (
LoadVesuvioBackParameters,
LoadVesuvioFrontParameters,
GeneralInitialConditions,
BackwardInitialConditions,
ForwardInitialConditions,
YSpaceFitInitialConditions,
UserScriptControls
)

ForwardInitialConditions.noOfMSIterations = 1
ForwardInitialConditions.firstSpec = 164
ForwardInitialConditions.lastSpec = 175
YSpaceFitInitialConditions.fitModel = "SINGLE_GAUSSIAN"
UserScriptControls.runRoutine = True
UserScriptControls.procedure = "FORWARD"
UserScriptControls.fitInYSpace = "FORWARD"
18 changes: 18 additions & 0 deletions tests/data/analysis/inputs/yspace_gc_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tests.data.analysis.inputs.analysis_test import (
LoadVesuvioBackParameters,
LoadVesuvioFrontParameters,
GeneralInitialConditions,
BackwardInitialConditions,
ForwardInitialConditions,
YSpaceFitInitialConditions,
UserScriptControls
)

ForwardInitialConditions.noOfMSIterations = 1
ForwardInitialConditions.firstSpec = 164
ForwardInitialConditions.lastSpec = 175
YSpaceFitInitialConditions.fitModel = "GC_C4_C6"
YSpaceFitInitialConditions.symmetrisationFlag = False
UserScriptControls.runRoutine = True
UserScriptControls.procedure = "FORWARD"
UserScriptControls.fitInYSpace = "FORWARD"
42 changes: 12 additions & 30 deletions tests/system/analysis/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,9 @@
import numpy as np
import numpy.testing as nptest
from pathlib import Path
from mvesuvio.run_routine import runRoutine
from mvesuvio.main.run_routine import Runner
from mvesuvio.util import handle_config
from tests.data.analysis.inputs.sample_test import (
LoadVesuvioBackParameters,
LoadVesuvioFrontParameters,
BackwardInitialConditions,
ForwardInitialConditions,
YSpaceFitInitialConditions,
UserScriptControls,
)
import mvesuvio
mvesuvio.set_config(
ip_folder=str(Path(handle_config.VESUVIO_PACKAGE_PATH).joinpath("config", "ip_files")),
inputs_file=str(Path(__file__).absolute().parent.parent.parent / "data" / "analysis" / "inputs" / "sample_test.py")
)
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))


class AnalysisRunner:
Expand All @@ -26,29 +13,24 @@ class AnalysisRunner:

@classmethod
def get_benchmark_result(cls):
if not AnalysisRunner._benchmarkResults:
if not cls._benchmarkResults:
cls._load_benchmark_results()
return AnalysisRunner._benchmarkResults
return cls._benchmarkResults

@classmethod
def get_current_result(cls):
if not AnalysisRunner._currentResults:
if not cls._currentResults:
mvesuvio.set_config(
ip_folder=str(Path(handle_config.VESUVIO_PACKAGE_PATH).joinpath("config", "ip_files")),
inputs_file=str(Path(__file__).absolute().parent.parent.parent / "data" / "analysis" / "inputs" / "analysis_test.py")
)
cls._run()
return AnalysisRunner._currentResults
return cls._currentResults

@classmethod
def _run(cls):
scattRes, yfitRes = runRoutine(
UserScriptControls(),
LoadVesuvioBackParameters(ipFilesPath),
LoadVesuvioFrontParameters(ipFilesPath),
BackwardInitialConditions(ipFilesPath),
ForwardInitialConditions(ipFilesPath),
YSpaceFitInitialConditions(),
True,
running_tests=True
)
AnalysisRunner._currentResults = scattRes
scattRes, yfitRes = Runner(True, True).run()
cls._currentResults = scattRes
return

@classmethod
Expand All @@ -57,7 +39,7 @@ def _load_benchmark_results(cls):
benchmarkResults = np.load(
str(benchmarkPath / "stored_spec_144-182_iter_3_GC_MS.npz")
)
AnalysisRunner._benchmarkResults = benchmarkResults
cls._benchmarkResults = benchmarkResults


class TestFitParameters(unittest.TestCase):
Expand Down
47 changes: 9 additions & 38 deletions tests/system/analysis/test_yspace_fit.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
from mvesuvio.run_routine import runRoutine
from mvesuvio.main.run_routine import Runner
from mantid.simpleapi import Load
from mantid.api import AnalysisDataService
from pathlib import Path
import numpy as np
import unittest
import numpy.testing as nptest
from mvesuvio.util import handle_config
from tests.data.analysis.inputs.sample_test import (
LoadVesuvioBackParameters,
LoadVesuvioFrontParameters,
BackwardInitialConditions,
ForwardInitialConditions,
YSpaceFitInitialConditions,
)
import mvesuvio
mvesuvio.set_config(
ip_folder=str(Path(handle_config.VESUVIO_PACKAGE_PATH).joinpath("config", "ip_files")),
inputs_file=str(Path(__file__).absolute().parent.parent.parent / "data" / "analysis" / "inputs" / "sample_test.py")
)
np.set_printoptions(suppress=True, precision=8, linewidth=150)


class UserScriptControls:
runRoutine = True
procedure = "FORWARD"
fitInYSpace = "FORWARD"


userCtr = UserScriptControls
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
wsBackIC = LoadVesuvioBackParameters(ipFilesPath)
wsFrontIC = LoadVesuvioFrontParameters(ipFilesPath)
bckwdIC = BackwardInitialConditions(ipFilesPath)
fwdIC = ForwardInitialConditions(ipFilesPath)
yFitIC = YSpaceFitInitialConditions()
scriptName = handle_config.get_script_name()

fwdIC.noOfMSIterations = 1
fwdIC.firstSpec = 164
fwdIC.lastSpec = 175
yFitIC.fitModel = "SINGLE_GAUSSIAN"
np.set_printoptions(suppress=True, precision=8, linewidth=150)


class AnalysisRunner:
Expand All @@ -64,17 +33,22 @@ def get_benchmark_result(cls):
@classmethod
def get_current_result(cls):
if not AnalysisRunner._currentResults:
mvesuvio.set_config(
ip_folder=str(Path(handle_config.VESUVIO_PACKAGE_PATH).joinpath("config", "ip_files")),
inputs_file=str(Path(__file__).absolute().parent.parent.parent / "data" / "analysis" / "inputs" / "yspace_gauss_test.py")
)
cls._run()
return AnalysisRunner._currentResults

@classmethod
def _load_workspaces(cls):
AnalysisDataService.clear()
scriptName = handle_config.get_script_name()
wsFinal = Load(
str(cls._input_data_path / "wsFinal.nxs"),
OutputWorkspace=scriptName + "_fwd_1",
)
for i in range(len(fwdIC.masses)):
for i in range(4):
fileName = "wsFinal_ncp_" + str(i) + ".nxs"
Load(
str(cls._input_data_path / fileName),
Expand All @@ -84,10 +58,7 @@ def _load_workspaces(cls):
@classmethod
def _run(cls):
cls.load_workspaces() # Load data to skip run of routine

scattRes, yfitRes = runRoutine(
userCtr, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, True
)
scattRes, yfitRes = Runner(True, True).run()
cls._currentResults = yfitRes

@classmethod
Expand Down
48 changes: 9 additions & 39 deletions tests/system/analysis/test_yspace_fit_GC.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
from mvesuvio.run_routine import runRoutine
from mvesuvio.main.run_routine import Runner
from mantid.simpleapi import Load
from mantid.api import AnalysisDataService
from pathlib import Path
import numpy as np
import unittest
import numpy.testing as nptest
from mvesuvio.util import handle_config
from tests.data.analysis.inputs.sample_test import (
LoadVesuvioBackParameters,
LoadVesuvioFrontParameters,
BackwardInitialConditions,
ForwardInitialConditions,
YSpaceFitInitialConditions,
)
import mvesuvio
mvesuvio.set_config(
ip_folder=str(Path(handle_config.VESUVIO_PACKAGE_PATH).joinpath("config", "ip_files")),
inputs_file=str(Path(__file__).absolute().parent.parent.parent / "data" / "analysis" / "inputs" / "sample_test.py")
)
np.set_printoptions(suppress=True, precision=8, linewidth=150)


class UserScriptControls:
runRoutine = True
procedure = "FORWARD"
fitInYSpace = "FORWARD"


userCtr = UserScriptControls
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
wsBackIC = LoadVesuvioBackParameters(ipFilesPath)
wsFrontIC = LoadVesuvioFrontParameters(ipFilesPath)
bckwdIC = BackwardInitialConditions(ipFilesPath)
fwdIC = ForwardInitialConditions(ipFilesPath)
yFitIC = YSpaceFitInitialConditions()
scriptName = handle_config.get_script_name()

fwdIC.noOfMSIterations = 1
fwdIC.firstSpec = 164
fwdIC.lastSpec = 175
yFitIC.fitModel = "GC_C4_C6"
yFitIC.symmetrisationFlag = False
np.set_printoptions(suppress=True, precision=8, linewidth=150)


class AnalysisRunner:
Expand All @@ -65,17 +33,22 @@ def get_benchmark_result(cls):
@classmethod
def get_current_result(cls):
if not AnalysisRunner._currentResults:
mvesuvio.set_config(
ip_folder=str(Path(handle_config.VESUVIO_PACKAGE_PATH).joinpath("config", "ip_files")),
inputs_file=str(Path(__file__).absolute().parent.parent.parent / "data" / "analysis" / "inputs" / "yspace_gc_test.py")
)
cls._run()
return AnalysisRunner._currentResults

@classmethod
def _load_workspaces(cls):
AnalysisDataService.clear()
scriptName = handle_config.get_script_name()
wsFinal = Load(
str(cls._input_data_path / "wsFinal.nxs"),
OutputWorkspace=scriptName + "_fwd_1",
)
for i in range(len(fwdIC.masses)):
for i in range(4):
fileName = "wsFinal_ncp_" + str(i) + ".nxs"
Load(
str(cls._input_data_path / fileName),
Expand All @@ -85,10 +58,7 @@ def _load_workspaces(cls):
@classmethod
def _run(cls):
cls.load_workspaces()

scattRes, yfitRes = runRoutine(
userCtr, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, True
)
scattRes, yfitRes = Runner(True, True).run()
cls._currentResults = yfitRes

@classmethod
Expand Down

0 comments on commit 32ffc6b

Please sign in to comment.