Skip to content

Commit

Permalink
Clean comments and functions
Browse files Browse the repository at this point in the history
Cleaned some unused comments from previous changes to the code.
Made tests use routine object directly without having to go through
the AlgorithmManager, this makes sure we can access results from a
public method.
  • Loading branch information
GuiMacielPereira committed Sep 6, 2024
1 parent 17aa715 commit 1078313
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 106 deletions.
99 changes: 31 additions & 68 deletions src/mvesuvio/analysis_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class NeutronComptonProfile:
width: float
center: float

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

mean_intensity: float = None
mean_width: float = None
Expand Down Expand Up @@ -95,6 +95,12 @@ def PyInit(self):
self.declareProperty("ResultsPath", "", doc="Directory to store results, to be deleted later")
self.declareProperty("FiguresPath", "", doc="Directory to store figures, to be deleted later")

# Outputs
self.declareProperty(TableWorkspaceProperty(name="OutputMeansTable",
defaultValue="",
direction=Direction.Output),
doc="TableWorkspace containing final means of intensity and widths.")


def PyExec(self):
self._setup()
Expand All @@ -119,7 +125,6 @@ def _setup(self):
self._h_ratio = self.getProperty("HRatioToLowestMass").value
self._constraints = () #self.getProperty("Constraints").value

self._profiles = {}
self._profiles_table = self.getProperty("InputProfiles").value

# Variables changing during fit
Expand Down Expand Up @@ -150,19 +155,6 @@ def calculate_h_ratio(self):
return sorted_intensities[0] / sorted_intensities[1]


# @property
# def profiles(self):
# return self._profiles


# @profiles.setter
# def profiles(self, incoming_profiles):
# assert(isinstance(incoming_profiles, dict))
# common_keys = self._profiles.keys() & incoming_profiles.keys()
# common_keys_profiles = {k: incoming_profiles[k] for k in common_keys}
# self._profiles = {**self._profiles, **common_keys_profiles}


def _update_workspace_data(self):

self._dataX = self._workspace_being_fit.extractX()
Expand Down Expand Up @@ -206,31 +198,6 @@ def _initialize_table_fit_parameters(self):
return table


@property
def mean_widths(self):
return self._mean_widths


@mean_widths.setter
def mean_widths(self, value):
self._mean_widths = value
# for i, p in enumerate(self._profiles.values()):
# p.mean_width = self._mean_widths[i]
return


@property
def mean_intensity_ratios(self):
return self._mean_intensity_ratios

@mean_intensity_ratios.setter
def mean_intensity_ratios(self, value):
self._mean_intensity_ratios = value
# for i, p in enumerate(self.profiles.values()):
# p.mean_intensity = self._mean_intensity_ratios[i]
return


def _create_emtpy_ncp_workspace(self, suffix):
return CreateWorkspace(
DataX=self._dataX,
Expand Down Expand Up @@ -433,7 +400,7 @@ def _save_plots(self):
ax.legend()

fileName = self._workspace_being_fit.name() + "_profiles_sum.pdf"
savePath = self._save_figures_path + fileName
savePath = self._save_figures_path + '/' + fileName
plt.savefig(savePath, bbox_inches="tight")
plt.close(fig)
return
Expand Down Expand Up @@ -471,24 +438,24 @@ def _set_means_and_std(self):
len(meanWidths) == self._profiles_table.rowCount()
), "Number of mean widths must match number of profiles!"

self.mean_widths = meanWidths # Use setter
self._mean_widths = meanWidths
self._std_widths = stdWidths
self.mean_intensity_ratios = meanIntensityRatios # Use setter
self._mean_intensity_ratios = meanIntensityRatios
self._std_intensity_ratios = stdIntensityRatios

self.createMeansAndStdTableWS()
self._create_means_table()
return


def createMeansAndStdTableWS(self):
meansTableWS = CreateEmptyTableWorkspace(
OutputWorkspace=self._workspace_being_fit.name() + "_MeanWidthsAndIntensities"
def _create_means_table(self):
table = CreateEmptyTableWorkspace(
OutputWorkspace=self._workspace_being_fit.name() + "_means"
)
meansTableWS.addColumn(type="str", name="Mass")
meansTableWS.addColumn(type="float", name="Mean Widths")
meansTableWS.addColumn(type="float", name="Std Widths")
meansTableWS.addColumn(type="float", name="Mean Intensities")
meansTableWS.addColumn(type="float", name="Std Intensities")
table.addColumn(type="str", name="label")
table.addColumn(type="float", name="mean_width")
table.addColumn(type="float", name="std_width")
table.addColumn(type="float", name="mean_intensity")
table.addColumn(type="float", name="std_intensity")

print("\nCreated Table with means and std:")
print("\nMass Mean \u00B1 Std Widths Mean \u00B1 Std Intensities\n")
Expand All @@ -499,10 +466,12 @@ def createMeansAndStdTableWS(self):
self._mean_intensity_ratios,
self._std_intensity_ratios,
):
meansTableWS.addRow([label, mean_width, std_width, mean_intensity, std_intensity])
table.addRow([label, mean_width, std_width, mean_intensity, std_intensity])
print(f"{label:5s} {mean_width:10.5f} \u00B1 {std_width:7.5f} \
{mean_intensity:10.5f} \u00B1 {std_intensity:7.5f}\n")
return

self.setPropertyValue("OutputMeansTable", table.name())
return table


def calculateMeansAndStds(self, widthsIn, intensitiesIn):
Expand Down Expand Up @@ -591,12 +560,6 @@ def _fit_neutron_compton_profiles_to_row(self):
):
bounds.extend([eval(intensity_bounds), eval(width_bounds), eval(center_bounds)])

# for p in self._profiles.values():
# for attr in ['intensity', 'width', 'center']:
# initial_parameters.append(getattr(p, attr))
# for attr in ['intensity_bounds', 'width_bounds', 'center_bounds']:
# bounds.append(getattr(p, attr))

result = optimize.minimize(
self.errorFunction,
initial_parameters,
Expand Down Expand Up @@ -803,7 +766,7 @@ def _get_parsed_constraints(self):

def _get_parsed_constraint_function(self, function_string: str):

profile_order = [key for key in self._profiles.keys()]
profile_order = [label for label in self._profiles_table.column("label")]
attribute_order = ['intensity', 'width', 'center']

words = function_string.split(' ')
Expand Down Expand Up @@ -1072,11 +1035,11 @@ def _set_results(self):
allIterNcp.append(allNCP)

# Extract Mean and Std Widths, Intensities
meansTable = mtd[wsIterName + "_MeanWidthsAndIntensities"]
allMeanWidhts.append(meansTable.column("Mean Widths"))
allStdWidths.append(meansTable.column("Std Widths"))
allMeanIntensities.append(meansTable.column("Mean Intensities"))
allStdIntensities.append(meansTable.column("Std Intensities"))
meansTable = mtd[wsIterName + "_means"]
allMeanWidhts.append(meansTable.column("mean_width"))
allStdWidths.append(meansTable.column("std_width"))
allMeanIntensities.append(meansTable.column("mean_intensity"))
allStdIntensities.append(meansTable.column("std_intensity"))

j += 1
except KeyError:
Expand Down
68 changes: 39 additions & 29 deletions src/mvesuvio/analysis_routines.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# from .analysis_reduction import iterativeFitForDataReduction
from mantid.api import AnalysisDataService
from mantid.simpleapi import CreateEmptyTableWorkspace
from mantid.api import AlgorithmFactory
from mantid.api import AlgorithmFactory, AlgorithmManager
import numpy as np

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

def _create_analysis_object_from_current_interface(IC):
def _create_analysis_object_from_current_interface(IC, running_tests=False):
ws = loadRawAndEmptyWsFromUserPath(
userWsRawPath=IC.userWsRawPath,
userWsEmptyPath=IC.userWsEmptyPath,
Expand Down Expand Up @@ -37,31 +37,38 @@ def _create_analysis_object_from_current_interface(IC):
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)

AlgorithmFactory.subscribe(AnalysisRoutine)
alg = create_algorithm("AnalysisRoutine",
InputWorkspace=cropedWs,
InputProfiles=profiles_table.name(),
InstrumentParametersFile=str(IC.InstrParsPath),
HRatioToLowestMass=IC.HToMassIdxRatio,
NumberOfIterations=int(IC.noOfMSIterations),
InvalidDetectors=IC.maskedSpecAllNo.astype(int).tolist(),
MultipleScatteringCorrection=IC.MSCorrectionFlag,
SampleVerticalWidth=IC.vertical_width,
SampleHorizontalWidth=IC.horizontal_width,
SampleThickness=IC.thickness,
GammaCorrection=IC.GammaCorrectionFlag,
ModeRunning=IC.modeRunning,
TransmissionGuess=IC.transmission_guess,
MultipleScatteringOrder=int(IC.multiple_scattering_order),
NumberOfEvents=int(IC.number_of_events),
# Constraints=IC.constraints,
ResultsPath=str(IC.resultsSavePath),
FiguresPath=str(IC.figSavePath)
)
# alg.add_profiles(*profiles)
profiles_table = create_profiles_table(cropedWs.name()+"_initial_parameters", profiles)

kwargs = {
"InputWorkspace": cropedWs,
"InputProfiles": profiles_table.name(),
"InstrumentParametersFile": str(IC.InstrParsPath),
"HRatioToLowestMass": IC.HToMassIdxRatio,
"NumberOfIterations": int(IC.noOfMSIterations),
"InvalidDetectors": IC.maskedSpecAllNo.astype(int).tolist(),
"MultipleScatteringCorrection": IC.MSCorrectionFlag,
"SampleVerticalWidth": IC.vertical_width,
"SampleHorizontalWidth": IC.horizontal_width,
"SampleThickness": IC.thickness,
"GammaCorrection": IC.GammaCorrectionFlag,
"ModeRunning": IC.modeRunning,
"TransmissionGuess": IC.transmission_guess,
"MultipleScatteringOrder": int(IC.multiple_scattering_order),
"NumberOfEvents": int(IC.number_of_events),
# Constraints"": IC.constraints,
"ResultsPath": str(IC.resultsSavePath),
"FiguresPath": str(IC.figSavePath),
"OutputMeansTable":" Final_Means"
}

if running_tests:
alg = AnalysisRoutine()
else:
AlgorithmFactory.subscribe(AnalysisRoutine)
alg = AlgorithmManager.createUnmanaged("AnalysisRoutine")

alg.initialize()
alg.setProperties(kwargs)
return alg


Expand All @@ -83,6 +90,7 @@ def create_profiles_table(name, profiles: list[NeutronComptonProfile]):
print(str(getattr(p, "width_bounds")))
return table


def set_initial_profiles_from(self, source: 'AnalysisRoutine'):

# Set intensities
Expand Down Expand Up @@ -117,7 +125,7 @@ def _get_lightest_profile(self):
masses = [p.mass for p in self._profiles.values()]
return profiles[np.argmin(masses)]

def runIndependentIterativeProcedure(IC, clearWS=True):
def runIndependentIterativeProcedure(IC, clearWS=True, running_tests=False):
"""
Runs the iterative fitting of NCP, cleaning any previously stored workspaces.
input: Backward or Forward scattering initial conditions object
Expand All @@ -128,8 +136,10 @@ def runIndependentIterativeProcedure(IC, clearWS=True):
if clearWS:
AnalysisDataService.clear()

alg = _create_analysis_object_from_current_interface(IC)
return alg.execute()
alg = _create_analysis_object_from_current_interface(IC, running_tests=running_tests)
alg.execute()
means_table = alg.getPropertyValue("OutputMeansTable")
return alg


def runJointBackAndForwardProcedure(bckwdIC, fwdIC, clearWS=True):
Expand Down
5 changes: 3 additions & 2 deletions src/mvesuvio/run_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def runRoutine(
fwdIC,
yFitIC,
yes_to_all=False,
running_tests=False
):
# Set extra attributes from user attributes
completeICFromInputs(fwdIC, wsFrontIC)
Expand All @@ -46,9 +47,9 @@ def runProcedure():
), "When H is not present, HToMassIdxRatio has to be set to None"

if proc == "BACKWARD":
res = runIndependentIterativeProcedure(bckwdIC)
res = runIndependentIterativeProcedure(bckwdIC, running_tests=running_tests)
if proc == "FORWARD":
res = runIndependentIterativeProcedure(fwdIC)
res = runIndependentIterativeProcedure(fwdIC, running_tests=running_tests)
if proc == "JOINT":
res = runJointBackAndForwardProcedure(bckwdIC, fwdIC)
return res
Expand Down
14 changes: 7 additions & 7 deletions tests/data/analysis/inputs/sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def __init__(self, ipFilesPath):
initPars = np.array([1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0])
bounds = np.array(
[
[0, np.nan],
[0, None],
[8, 16],
[-3, 1],
[0, np.nan],
[0, None],
[8, 16],
[-3, 1],
[0, np.nan],
[0, None],
[11, 14],
[-3, 1],
]
Expand Down Expand Up @@ -81,16 +81,16 @@ def __init__(self, ipFilesPath):
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],
[0, None],
[3, 6],
[-3, 1],
[0, np.nan],
[0, None],
[12.71, 12.71],
[-3, 1],
[0, np.nan],
[0, None],
[8.76, 8.76],
[-3, 1],
[0, np.nan],
[0, None],
[13.897, 13.897],
[-3, 1],
]
Expand Down
1 change: 1 addition & 0 deletions tests/system/analysis/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _run(cls):
ForwardInitialConditions(ipFilesPath),
YSpaceFitInitialConditions(),
True,
running_tests=True
)
AnalysisRunner._currentResults = scattRes
return
Expand Down

0 comments on commit 1078313

Please sign in to comment.