Skip to content

Commit

Permalink
Merge pull request #38014 from mantidproject/temporary_fix_for_import…
Browse files Browse the repository at this point in the history
…_error_isis_sx_class

Fix for import error in ISIS SX reduction classes
  • Loading branch information
SilkeSchomann committed Sep 18, 2024
2 parents 2722e15 + c4cf0e4 commit 20bfc15
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("LoadLog", 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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion scripts/Diffraction/single_crystal/base_sx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 20bfc15

Please sign in to comment.