Skip to content

Commit

Permalink
add extra system test
Browse files Browse the repository at this point in the history
  • Loading branch information
MialLewis committed Dec 20, 2023
1 parent 07d6a6d commit 15071b2
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 4 deletions.
120 changes: 120 additions & 0 deletions EVSVesuvio/system_tests/old_new_comparison_inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import numpy as np


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

runs = "43066-43076" # 77K # The numbers of the runs to be analysed
empty_runs = (
"41876-41923" # 77K # The numbers of the empty runs to be subtracted
)
spectra = "3-134" # Spectra to be analysed
mode = "DoubleDifference"
subEmptyFromRaw = True # Flag to control wether empty ws gets subtracted from raw
scaleEmpty = 1 # None or scaling factor
scaleRaw = 1


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

runs = "43066-43076" # 100K # The numbers of the runs to be analysed
empty_runs = (
"43868-43911" # 100K # The numbers of the empty runs to be subtracted
)
spectra = "144-182" # Spectra to be analysed
mode = "SingleDifference"
subEmptyFromRaw = False # Flag to control wether empty ws gets subtracted from raw
scaleEmpty = 1 # None or scaling factor
scaleRaw = 1


class GeneralInitialConditions:
transmission_guess = 0.8537 # Experimental value from VesuvioTransmission
multiple_scattering_order, number_of_events = 2, 1.0e5
vertical_width, horizontal_width, thickness = 0.1, 0.1, 0.001 # Expressed in meters


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

HToMassIdxRatio = 19.0620008206 # Set to zero or None when H is not present
massIdx = 0
masses = np.array([12, 16, 27])
initPars = np.array([1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0])
bounds = np.array(
[
[0, np.nan],
[8, 16],
[-3, 1],
[0, np.nan],
[8, 16],
[-3, 1],
[0, np.nan],
[11, 14],
[-3, 1],
]
)
constraints = ()
noOfMSIterations = 3 # 4
firstSpec = 3 # 3
lastSpec = 134 # 134
maskedSpecAllNo = np.array([18, 34, 42, 43, 59, 60, 62, 118, 119, 133])
MSCorrectionFlag = True
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"

masses = np.array([1.0079, 12, 16, 27])
initPars = np.array([1, 4.7, 0, 1, 12.71, 0.0, 1, 8.76, 0.0, 1, 13.897, 0.0])
bounds = np.array(
[
[0, np.nan],
[3, 6],
[-3, 1],
[0, np.nan],
[12.71, 12.71],
[-3, 1],
[0, np.nan],
[8.76, 8.76],
[-3, 1],
[0, np.nan],
[13.897, 13.897],
[-3, 1],
]
)
constraints = ()
noOfMSIterations = 3 # 4
firstSpec = 144 # 144
lastSpec = 182 # 182
MSCorrectionFlag = True
GammaCorrectionFlag = True
maskedSpecAllNo = np.array([173, 174, 179])
tofBinning = "110,1,430" # Binning of ToF spectra
maskTOFRange = None
runHistData = True
normVoigt = False


class YSpaceFitInitialConditions:
anything = True


class UserScriptControls:
runRoutine = True
procedure = "FORWARD" # Options: None, "BACKWARD", "FORWARD", "JOINT"
fitInYSpace = None # Options: None, "BACKWARD", "FORWARD", "JOINT"


class BootstrapInitialConditions:
runBootstrap = False
Binary file not shown.
2 changes: 1 addition & 1 deletion EVSVesuvio/system_tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _run_analysis(cls):
yFitIC.symmetrisationFlag = True

bootRes, noneRes = runScript(
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC, True
)

# TODO: Figure out why doing the two tests simultaneously fails the testing
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion EVSVesuvio/system_tests/test_jackknife.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _run_analysis(cls):
userCtr = UserScriptControls

bootRes, noneRes = runScript(
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC, True
)

cls._jackBackSamples = bootRes["bckwdScat"].bootSamples
Expand Down
229 changes: 229 additions & 0 deletions EVSVesuvio/system_tests/test_old_new_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
from EVSVesuvio.vesuvio_analysis.run_script import runScript
import unittest
import numpy as np
import numpy.testing as nptest
from pathlib import Path
from EVSVesuvio.scripts import handle_config
from EVSVesuvio.system_tests.old_new_comparison_inputs import (
LoadVesuvioBackParameters,
LoadVesuvioFrontParameters,
BackwardInitialConditions,
ForwardInitialConditions,
YSpaceFitInitialConditions,
BootstrapInitialConditions,
UserScriptControls,
)


ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))


class AnalysisRunner:
_benchmarkResults = None
_currentResults = None

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

@classmethod
def get_current_result(cls):
if not AnalysisRunner._currentResults:
cls._run()
return AnalysisRunner._currentResults

@classmethod
def _run(cls):
scattRes, yfitRes = runScript(
UserScriptControls(),
"test_expr_comp",
LoadVesuvioBackParameters(ipFilesPath),
LoadVesuvioFrontParameters(ipFilesPath),
BackwardInitialConditions(ipFilesPath),
ForwardInitialConditions(ipFilesPath),
YSpaceFitInitialConditions(),
BootstrapInitialConditions(),
True,
)

wsFinal, forwardScatteringResults = scattRes

# Test the results
np.set_printoptions(suppress=True, precision=8, linewidth=150)

currentResults = forwardScatteringResults
AnalysisRunner._currentResults = currentResults

@classmethod
def _load_benchmark_results(cls):
testPath = Path(__file__).absolute().parent
benchmarkResults = np.load(
str(testPath / "stored_spec_144-182_iter_3_GC_MS.npz")
)
AnalysisRunner._benchmarkResults = benchmarkResults


def displayMaskAllIter(mask, rtol, string):
print("\nNo of different " + string + f", rtol={rtol}:")
for i, mask_i in enumerate(mask):
noDiff = np.sum(mask_i)
maskSize = mask_i.size
print(
f"iter {i}: ",
noDiff,
" out of ",
maskSize,
f"ie {100*noDiff/maskSize:.1f} %",
)


class TestFitParameters(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.benchmarkResults = AnalysisRunner.get_benchmark_result()
cls.currentResults = AnalysisRunner.get_current_result()

def setUp(self):
oriPars = self.benchmarkResults["all_spec_best_par_chi_nit"]
self.orispec = oriPars[:, :, 0]
self.orichi2 = oriPars[:, :, -2]
self.orinit = oriPars[:, :, -1]
self.orimainPars = oriPars[:, :, 1:-2]
self.oriintensities = self.orimainPars[:, :, 0::3]
self.oriwidths = self.orimainPars[:, :, 1::3]
self.oricenters = self.orimainPars[:, :, 2::3]

optPars = self.currentResults.all_spec_best_par_chi_nit
self.optspec = optPars[:, :, 0]
self.optchi2 = optPars[:, :, -2]
self.optnit = optPars[:, :, -1]
self.optmainPars = optPars[:, :, 1:-2]
self.optintensities = self.optmainPars[:, :, 0::3]
self.optwidths = self.optmainPars[:, :, 1::3]
self.optcenters = self.optmainPars[:, :, 2::3]

self.rtol = 0.0001
self.equal_nan = True

def test_mainPars(self):
totalMask = np.isclose(
self.orimainPars, self.optmainPars, rtol=self.rtol, equal_nan=self.equal_nan
)
totalDiffMask = ~totalMask
displayMaskAllIter(totalDiffMask, self.rtol, "parameters")
# nptest.assert_allclose(self.orimainPars, self.optmainPars, self.rtol)

def test_chi2(self):
totalMask = np.isclose(
self.orichi2, self.optchi2, rtol=self.rtol, equal_nan=self.equal_nan
)
totalDiffMask = ~totalMask
displayMaskAllIter(totalDiffMask, self.rtol, "chi2")
nptest.assert_allclose(self.orichi2, self.optchi2, self.rtol)

def test_nit(self):
totalMask = np.isclose(
self.orinit, self.optnit, rtol=self.rtol, equal_nan=self.equal_nan
)
totalDiffMask = ~totalMask
displayMaskAllIter(totalDiffMask, self.rtol, "nit")
# nptest.assert_allclose(self.orinit, self.optnit, self.rtol)

def test_intensities(self):
totalMask = np.isclose(
self.oriintensities,
self.optintensities,
rtol=self.rtol,
equal_nan=self.equal_nan,
)
totalDiffMask = ~totalMask
displayMaskAllIter(totalDiffMask, self.rtol, "intensities")
# nptest.assert_allclose(self.oriintensities, self.optintensities, self.rtol)


class TestNcp(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.benchmarkResults = AnalysisRunner.get_benchmark_result()
cls.currentResults = AnalysisRunner.get_current_result()

def setUp(self):
self.orincp = self.benchmarkResults["all_tot_ncp"][:, :, :-1]

self.optncp = self.currentResults.all_tot_ncp

self.rtol = 0.0001
self.equal_nan = True

def test_ncp(self):
correctNansOri = np.where(
(self.orincp == 0) & np.isnan(self.optncp), np.nan, self.orincp
)

totalMask = np.isclose(
correctNansOri, self.optncp, rtol=self.rtol, equal_nan=self.equal_nan
)
totalDiffMask = ~totalMask
displayMaskAllIter(totalDiffMask, self.rtol, "ncp")
# nptest.assert_allclose(self.orincp, self.optncp, self.rtol)


class TestMeanWidths(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.benchmarkResults = AnalysisRunner.get_benchmark_result()
cls.currentResults = AnalysisRunner.get_current_result()

def setUp(self):
self.orimeanwidths = self.benchmarkResults["all_mean_widths"]
self.optmeanwidths = self.currentResults.all_mean_widths
self.rtol = 0.0001

def test_widths(self):
nptest.assert_allclose(self.orimeanwidths, self.optmeanwidths, self.rtol)


class TestMeanIntensities(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.benchmarkResults = AnalysisRunner.get_benchmark_result()
cls.currentResults = AnalysisRunner.get_current_result()

def setUp(self):
self.orimeanintensities = self.benchmarkResults["all_mean_intensities"]
self.optmeanintensities = self.currentResults.all_mean_intensities
self.rtol = 0.001

def test_intensities(self):
nptest.assert_allclose(
self.orimeanintensities, self.optmeanintensities, self.rtol
)


class TestFitWorkspaces(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.benchmarkResults = AnalysisRunner.get_benchmark_result()
cls.currentResults = AnalysisRunner.get_current_result()

def setUp(self):
self.oriws = self.benchmarkResults["all_fit_workspaces"]
self.optws = self.currentResults.all_fit_workspaces

self.decimal = 8
self.rtol = 0.0001
self.equal_nan = True

def test_ws(self):
totalMask = np.isclose(
self.oriws, self.optws, rtol=self.rtol, equal_nan=self.equal_nan
)
totalDiffMask = ~totalMask
displayMaskAllIter(totalDiffMask, self.rtol, "workspaces to be fitted")
# nptest.assert_allclose(self.oriws, self.optws, self.rtol)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion EVSVesuvio/system_tests/test_yspace_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _run(cls):
yFitIC.fitModel = "SINGLE_GAUSSIAN"

scattRes, yfitRes = runScript(
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC, True
)
cls._currentResults = yfitRes

Expand Down
2 changes: 1 addition & 1 deletion EVSVesuvio/system_tests/test_yspace_fit_GC.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _run(cls):
yFitIC.fitModel = "GC_C4_C6"

scattRes, yfitRes = runScript(
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC
userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC, True
)
cls._currentResults = yfitRes

Expand Down

0 comments on commit 15071b2

Please sign in to comment.