Skip to content

Commit

Permalink
Refactor joint algorithm
Browse files Browse the repository at this point in the history
Refactored joint algorithm to fit new approach of using
profiles table as inputs to the AnalysisReduction algorithm.
  • Loading branch information
GuiMacielPereira committed Sep 12, 2024
1 parent 61da79d commit d4e116c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 59 deletions.
25 changes: 3 additions & 22 deletions src/mvesuvio/analysis_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,11 @@

from mvesuvio.util.analysis_helpers import loadConstants, numericalThirdDerivative

from dataclasses import dataclass


np.set_printoptions(suppress=True, precision=4, linewidth=200)


@dataclass(frozen=False)
class NeutronComptonProfile:
label: str
mass: float

intensity: float
width: float
center: float

intensity_bounds: list[float, float]
width_bounds: list[float, float]
center_bounds: list[float, float]

mean_intensity: float = None
mean_width: float = None
mean_center: float = None


class AnalysisRoutine(PythonAlgorithm):

def summary(self):
Expand Down Expand Up @@ -923,13 +904,13 @@ def calcMSCorrectionSampleProperties(self, meanWidths, meanIntensityRatios):
# If Backscattering mode and H is present in the sample, add H to MS properties
if self._mode_running == "BACKWARD":
if self._h_ratio > 0: # If H is present, ratio is a number
masses = np.append(masses, 1.0079)
meanWidths = np.append(meanWidths, 5.0)

HIntensity = self._h_ratio * meanIntensityRatios[np.argmin(masses)]
meanIntensityRatios = np.append(meanIntensityRatios, HIntensity)
meanIntensityRatios /= np.sum(meanIntensityRatios)

masses = np.append(masses, 1.0079)
meanWidths = np.append(meanWidths, 5.0)

MSProperties = np.zeros(3 * len(masses))
MSProperties[::3] = masses
MSProperties[1::3] = meanIntensityRatios
Expand Down
74 changes: 39 additions & 35 deletions src/mvesuvio/analysis_routines.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# from .analysis_reduction import iterativeFitForDataReduction
from mantid.api import AnalysisDataService
from mantid.simpleapi import CreateEmptyTableWorkspace
from mantid.simpleapi import CreateEmptyTableWorkspace, mtd
from mantid.api import AlgorithmFactory, AlgorithmManager
import numpy as np

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

def _create_analysis_object_from_current_interface(IC, running_tests=False, overwrite_profiles_table=None):
def _create_analysis_object_from_current_interface(IC, running_tests=False):
ws = loadRawAndEmptyWsFromUserPath(
userWsRawPath=IC.userWsRawPath,
userWsEmptyPath=IC.userWsEmptyPath,
Expand All @@ -26,20 +26,17 @@ def _create_analysis_object_from_current_interface(IC, running_tests=False, over
maskTOFRange=IC.maskTOFRange
)

if overwrite_profiles_table:
profiles_table = overwrite_profiles_table
else:
profiles = []
for mass, intensity, width, center, intensity_bound, width_bound, center_bound in zip(
IC.masses, IC.initPars[::3], IC.initPars[1::3], IC.initPars[2::3],
IC.bounds[::3], IC.bounds[1::3], IC.bounds[2::3]
):
profiles.append(NeutronComptonProfile(
label=str(mass), mass=mass, intensity=intensity, width=width, center=center,
intensity_bounds=list(intensity_bound), width_bounds=list(width_bound), center_bounds=list(center_bound)
))

profiles_table = create_profiles_table(cropedWs.name()+"_initial_parameters", profiles)
profiles = []
for mass, intensity, width, center, intensity_bound, width_bound, center_bound in zip(
IC.masses, IC.initPars[::3], IC.initPars[1::3], IC.initPars[2::3],
IC.bounds[::3], IC.bounds[1::3], IC.bounds[2::3]
):
profiles.append(NeutronComptonProfile(
label=str(mass), mass=mass, intensity=intensity, width=width, center=center,
intensity_bounds=list(intensity_bound), width_bounds=list(width_bound), center_bounds=list(center_bound)
))

profiles_table = create_profiles_table(cropedWs.name()+"_initial_parameters", profiles)

kwargs = {
"InputWorkspace": cropedWs,
Expand Down Expand Up @@ -120,7 +117,31 @@ def runJointBackAndForwardProcedure(bckwdIC, fwdIC, clearWS=True):
if clearWS:
AnalysisDataService.clear()

return runJoint(bckwdIC, fwdIC)
back_alg= _create_analysis_object_from_current_interface(bckwdIC)
front_alg= _create_analysis_object_from_current_interface(fwdIC)

return run_joint_algs(back_alg, front_alg)


def run_joint_algs(back_alg, front_alg):

back_alg.execute()

incoming_means_table = mtd[back_alg.getPropertyValue("OutputMeansTable")]
h_ratio = back_alg.getProperty("HRatioToLowestMass").value

assert incoming_means_table is not None, "Means table from backward routine not correctly accessed."
assert h_ratio is not None, "H ratio from backward routine not correctly accesssed."

receiving_profiles_table = front_alg.getProperty("InputProfiles").value

fix_profile_parameters(incoming_means_table, receiving_profiles_table, h_ratio)

# Even if the name is the same, need to trigger update
front_alg.setProperty("InputProfiles", receiving_profiles_table.name())

front_alg.execute()
return


def runPreProcToEstHRatio(bckwdIC, fwdIC):
Expand Down Expand Up @@ -177,23 +198,6 @@ def createTableWSHRatios():
return table


def runJoint(bckwdIC, fwdIC):

back_alg= _create_analysis_object_from_current_interface(bckwdIC)
front_alg= _create_analysis_object_from_current_interface(fwdIC)

back_alg.execute()
incoming_means_table = back_alg.getProperty("OutputMeansTable").value
receiving_profiles_table = front_alg.getProperty("InputProfiles").value

fixed_profiles_table = fix_profiles(incoming_means_table, receiving_profiles_table)

front_alg.setProperty("InputProfiles", fixed_profiles_table)

front_alg.execute()
return


def isHPresent(masses) -> bool:
Hmask = np.abs(masses - 1) / 1 < 0.1 # H mass whithin 10% of 1 au

Expand Down
6 changes: 4 additions & 2 deletions src/mvesuvio/util/analysis_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from mantid.simpleapi import Load, Rebin, Scale, SumSpectra, Minus, CropWorkspace, \
CloneWorkspace, MaskDetectors, CreateWorkspace, CreateEmptyTableWorkspace
CloneWorkspace, MaskDetectors, CreateWorkspace, CreateEmptyTableWorkspace, \
mtd, RenameWorkspace
import numpy as np
import numbers

Expand Down Expand Up @@ -204,7 +205,8 @@ def fix_profile_parameters(incoming_means_table, receiving_profiles_table, h_rat
p['width_bounds'] = str([p['width'] , p['width']])

result_profiles_table = _convert_dict_to_table(profiles_dict)
return result_profiles_table
RenameWorkspace(result_profiles_table, receiving_profiles_table.name())
return mtd[result_profiles_table.name()]


def _convert_table_to_dict(table):
Expand Down

0 comments on commit d4e116c

Please sign in to comment.