From 54976b1d25e24f9bbf097c8fc1dc3ecf43f39413 Mon Sep 17 00:00:00 2001 From: Richard Waite Date: Wed, 18 Sep 2024 09:28:31 +0100 Subject: [PATCH 1/3] Replace simpleapi with child alg calls --- .../algorithms/FindGoniometerFromUB.py | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py b/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py index 6ab1c0d14ac5..b68609a08015 100644 --- a/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py +++ b/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py @@ -6,16 +6,6 @@ # SPDX - License - Identifier: GPL - 3.0 + from mantid import config from mantid.api import DataProcessorAlgorithm, AlgorithmFactory, Progress, FileFinder, MultipleFileProperty, ITableWorkspaceProperty -from mantid.simpleapi import ( - CreateEmptyTableWorkspace, - CreateSampleWorkspace, - LoadLog, - LoadNexusLogs, - LoadIsawUB, - SetUB, - SaveIsawUB, - DeleteWorkspace, -) from mantid.kernel import Direction, FloatBoundedValidator, IntListValidator, StringMandatoryValidator, V3D import numpy as np from os import path @@ -200,22 +190,21 @@ def loadUBFiles(self, ubFiles, omegaHand, phiHand, omegaLogName, phiLogName): # these rotation matrices are defined in right-handed coordinate system (i.e. omegaHand = 1 etc.) _, fname = path.split(ubFiles[irun]) dataPath = FileFinder.findRuns(fname.split(".mat")[0])[0] - tmpWS = CreateSampleWorkspace(StoreInADS=False) + tmpWS = self.exec_child_alg("CreateSampleWorkspace") if dataPath[-4:] == ".raw": # assume log is kept separately in a .log file with same path - LoadLog(Workspace=tmpWS, Filename="".join(dataPath[:-4] + ".log")) + self.exec_child_alg("CreateSampleWorkspace", Workspace=tmpWS, Filename="".join(dataPath[:-4] + ".log")) elif dataPath[-4:] == ".nxs": # logs are kept with data in nexus file - LoadNexusLogs(Workspace=tmpWS, Filename=dataPath) + self.exec_child_alg("LoadNexusLogs", Workspace=tmpWS, Filename=dataPath) # read omega and phi (in RH coords) omega[irun] = omegaHand * tmpWS.getRun().getLogData(omegaLogName).value[0] phiRef[irun] = phiHand * tmpWS.getRun().getLogData(phiLogName).value[0] # load UB - LoadIsawUB(InputWorkspace=tmpWS, Filename=ubFiles[irun], CheckUMatrix=True) + self.exec_child_alg("LoadIsawUB", InputWorkspace=tmpWS, Filename=ubFiles[irun], CheckUMatrix=True) tmpUB = tmpWS.sample().getOrientedLattice().getUB() # permute axes to use IPNS convention (as in saved .mat file) matUB += [tmpUB[[2, 0, 1], :]] - DeleteWorkspace(tmpWS) return matUB, omega, phiRef def findConsistentUB( @@ -224,7 +213,7 @@ def findConsistentUB( # calculate the rotation matrix that maps the UB of the first run onto subsequent UBs # get save directory saveDir = config["defaultsave.directory"] - tmpWS = CreateSampleWorkspace() + tmpWS = self.exec_child_alg("CreateSampleWorkspace") for irun in range(1, len(omega)): chi, phi, u = self.getGonioAngles(matUB[irun], zeroUB, omega[irun]) # phi relative to first run in RH/LH convention of user @@ -252,8 +241,8 @@ def findConsistentUB( _, nameUB = path.split(ubFiles[irun]) newUBPath = path.join(saveDir, nameUB[:-4] + "_consistent" + nameUB[-4:]) # set as UB (converting back to non-IPNS convention) - SetUB(tmpWS, UB=matUB[irun][[1, 2, 0], :]) - SaveIsawUB(tmpWS, newUBPath) + self.exec_child_alg("SetUB", Workspace=tmpWS, UB=matUB[irun][[1, 2, 0], :]) + self.exec_child_alg("SaveIsawUB", InputWorkspace=tmpWS, Filename=newUBPath) # populate row of table phi2print = phiHand * (phi + phiRef[0]) phi2print = phi2print + np.ceil(-phi2print / 360) * 360 @@ -265,13 +254,12 @@ def findConsistentUB( "Check the goniometer angles and handedness supplied " "and the accuracy of reference UB.".format(ubFiles[irun]) ) - DeleteWorkspace(tmpWS) def createGoniometerTable(self): """ :return: Empty table workspace with columns Run, Chi, Phi and GonioAxis (unit vector) """ - gonioTable = CreateEmptyTableWorkspace(StoreInADS=False) + gonioTable = self.exec_child_alg(" CreateEmptyTableWorkspace") # Add some columns, Recognized types are: int,float,double,bool,str,V3D,long64 gonioTable.addColumn(type="str", name="Run") gonioTable.addColumn(type="float", name="Chi") @@ -318,6 +306,15 @@ def getGonioAngles(self, aUB, zeroUB, omega): phi = phi + np.ceil(-phi / 360) * 360 return chi, phi, u + def exec_child_alg(self, alg_name: str, **kwargs): + alg = self.createChildAlgorithm(alg_name, enableLogging=False) + alg.setAlwaysStoreInADS(False) + alg.initialize() + alg.setProperties(kwargs) + alg.execute() + out_props = tuple(alg.getProperty(prop).value for prop in alg.outputProperties()) + return out_props[0] if len(out_props) == 1 else out_props + # register algorithm with mantid AlgorithmFactory.subscribe(FindGoniometerFromUB) From 6162aa9f5b7046e1d1067e11da041ed613c12418 Mon Sep 17 00:00:00 2001 From: Richard Waite Date: Wed, 18 Sep 2024 09:33:44 +0100 Subject: [PATCH 2/3] Add plugins.algorithms to import --- .../PythonInterface/plugins/algorithms/FindGlobalBMatrix.py | 2 +- scripts/Diffraction/single_crystal/base_sx.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Framework/PythonInterface/plugins/algorithms/FindGlobalBMatrix.py b/Framework/PythonInterface/plugins/algorithms/FindGlobalBMatrix.py index 0b82023ca421..8b9e60119d7f 100644 --- a/Framework/PythonInterface/plugins/algorithms/FindGlobalBMatrix.py +++ b/Framework/PythonInterface/plugins/algorithms/FindGlobalBMatrix.py @@ -9,7 +9,7 @@ from mantid.kernel import Direction, FloatBoundedValidator, StringArrayProperty import numpy as np from scipy.optimize import leastsq -from FindGoniometerFromUB import getSignMaxAbsValInCol +from plugins.algorithms.FindGoniometerFromUB import getSignMaxAbsValInCol _MIN_NUM_PEAKS = 6 # minimum required to use FindUBUsingLatticeParameters assuming all linearly indep. _MIN_NUM_INDEXED_PEAKS = 3 # minimum indexed peaks required for CalculateUMatrix diff --git a/scripts/Diffraction/single_crystal/base_sx.py b/scripts/Diffraction/single_crystal/base_sx.py index 9c39be5f9a96..cc06d2bfd293 100644 --- a/scripts/Diffraction/single_crystal/base_sx.py +++ b/scripts/Diffraction/single_crystal/base_sx.py @@ -10,7 +10,7 @@ import mantid.simpleapi as mantid from mantid.api import FunctionFactory, AnalysisDataService as ADS, IMDEventWorkspace, IPeaksWorkspace, IPeak from mantid.kernel import logger, SpecialCoordinateSystem -from FindGoniometerFromUB import getSignMaxAbsValInCol +from plugins.algorithms.FindGoniometerFromUB import getSignMaxAbsValInCol from mantid.geometry import CrystalStructure, SpaceGroupFactory, ReflectionGenerator, ReflectionConditionFilter, PeakShape from os import path from json import loads as json_loads From c4cf0e45e5ec148a1473cd4fa503355b5819669e Mon Sep 17 00:00:00 2001 From: Richard Waite Date: Wed, 18 Sep 2024 10:00:45 +0100 Subject: [PATCH 3/3] Fix typos in child alg names Thanks @jclarkeSTFC --- .../plugins/algorithms/FindGoniometerFromUB.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py b/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py index b68609a08015..4fa2dabb2298 100644 --- a/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py +++ b/Framework/PythonInterface/plugins/algorithms/FindGoniometerFromUB.py @@ -193,7 +193,7 @@ def loadUBFiles(self, ubFiles, omegaHand, phiHand, omegaLogName, phiLogName): tmpWS = self.exec_child_alg("CreateSampleWorkspace") if dataPath[-4:] == ".raw": # assume log is kept separately in a .log file with same path - self.exec_child_alg("CreateSampleWorkspace", Workspace=tmpWS, Filename="".join(dataPath[:-4] + ".log")) + self.exec_child_alg("LoadLog", Workspace=tmpWS, Filename="".join(dataPath[:-4] + ".log")) elif dataPath[-4:] == ".nxs": # logs are kept with data in nexus file self.exec_child_alg("LoadNexusLogs", Workspace=tmpWS, Filename=dataPath) @@ -259,7 +259,7 @@ def createGoniometerTable(self): """ :return: Empty table workspace with columns Run, Chi, Phi and GonioAxis (unit vector) """ - gonioTable = self.exec_child_alg(" CreateEmptyTableWorkspace") + gonioTable = self.exec_child_alg("CreateEmptyTableWorkspace") # Add some columns, Recognized types are: int,float,double,bool,str,V3D,long64 gonioTable.addColumn(type="str", name="Run") gonioTable.addColumn(type="float", name="Chi")