Skip to content

Commit

Permalink
Fix errors in handling pause, and MD workspaces with no UB (#142)
Browse files Browse the repository at this point in the history
* Fix errors in handling pause, and MD workspaces with no UB

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update sample.py

* Update sample.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* docstring

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AndreiSavici and pre-commit-ci[bot] authored Oct 10, 2024
1 parent 8594abb commit 0831391
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/shiver/models/convert_dgs_to_single_mde.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ def PyExec(self): # pylint: disable=too-many-branches

# do filtering
if units == "TOF" and len(CheckForSampleLogs(Workspace=data, LogNames="pause")) == 0:
data = FilterByLogValue(InputWorkspace=data, LogName="pause", MinimumValue=-1, MaximumValue=0.5)
data = FilterByLogValue(
InputWorkspace=data, LogName="pause", MinimumValue=-1, MaximumValue=0.5, LogBoundary="Left"
)
if units == "TOF" and bad_pulses_threshold > 0:
data = FilterBadPulses(InputWorkspace=data, LowerCutoff=bad_pulses_threshold)

Expand Down
16 changes: 11 additions & 5 deletions src/shiver/models/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ def connect_error_message(self, callback):
def get_lattice_ub(self):
"""return oriented lattice object from mtd - if no name provided initialize lattice"""
if self.name:
if mtd.doesExist(self.name):
self.oriented_lattice = mtd[self.name].getExperimentInfo(0).sample().getOrientedLattice()
return self.oriented_lattice
err_msg = f"Workspace {self.name} does not exist. Lattice from default parameters\n"
logger.error(err_msg)
try:
if mtd.doesExist(self.name):
self.oriented_lattice = mtd[self.name].getExperimentInfo(0).sample().getOrientedLattice()
return self.oriented_lattice
err_msg = f"Workspace {self.name} does not exist. Lattice from default parameters\n"
logger.error(err_msg)
except RuntimeError:
err_msg = (
f"Workspace {self.name} does not contain an OrientedLattice. Lattice from default parameters\n"
)
logger.error(err_msg)
# no valid name-workspace provided, create lattice with initial params
params = {
"a": 1.00000,
Expand Down
11 changes: 11 additions & 0 deletions tests/models/test_generatemde.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
LoadNexusMonitors,
GetEiT0atSNS,
DgsReduction,
AddTimeSeriesLog,
CropWorkspace,
ConvertToMD,
ConvertToMDMinMaxGlobal,
Expand Down Expand Up @@ -60,6 +61,16 @@ def test_convert_dgs_to_single_mde_single_qsample():
assert CompareMDWorkspaces(md1, md2, IgnoreBoxID=True)[0]


def test_convert_dgs_to_single_pause():
"""Test for correctly handling pause"""
raw_data_folder = os.path.join(os.path.dirname(__file__), "../data/raw")

data = LoadEventNexus(os.path.join(raw_data_folder, "HYS_178921.nxs.h5"))
AddTimeSeriesLog(Workspace=data, Name="pause", Time="2017-11-02T01:48:58.543570666", Value=1, Type="int")
md_p = ConvertDGSToSingleMDE(InputWorkspace=data, Ei=25.0, T0=112.0, TimeIndependentBackground="Default")
assert md_p.getNEvents() == 6140 # the original mde has 23682 events


def test_convert_dgs_to_single_mde_single_qlab():
"""Test for QFrame=Q_lab option ConvertDGSToSingleMDE"""

Expand Down
104 changes: 103 additions & 1 deletion tests/models/test_sample_parameters_buttons_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@
import numpy as np
from pytest import approx

from mantid.kernel import amend_config

# pylint: disable=no-name-in-module
from mantid.simpleapi import LoadMD, mtd
from mantid.simpleapi import (
LoadMD,
LoadEventNexus,
mtd,
ConvertToMD,
SetGoniometer,
MaskBTP,
FilterByLogValue,
LoadNexusMonitors,
GetEiT0atSNS,
DgsReduction,
CropWorkspace,
ConvertToMDMinMaxGlobal,
)
from shiver.models.sample import SampleModel
from shiver.models.generate import gather_mde_config_dict

Expand Down Expand Up @@ -119,6 +134,93 @@ def error_callback(msg):
assert v_array == approx(param_v_array)


def test_apply_button_valid_mde_ub():
"""Test for pressing Apply button with mde workspace that does not have ub"""

name = "out_data"

datafile = os.path.join(os.path.dirname(__file__), "../data/raw", "SEQ_124735.nxs.h5")

# Manual data reduction
LoadEventNexus(Filename=datafile, OutputWorkspace="data")
SetGoniometer(Workspace="data", Axis0="phi,0,1,0,1")
MaskBTP(Workspace="data", Pixel="1-7,122-128")
MaskBTP(Workspace="data", Bank="114,115,75,76,38,39")
MaskBTP(Workspace="data", Bank="88", Tube="2-4", Pixel="30-35")
MaskBTP(Workspace="data", Bank="127", Tube="7-8", Pixel="99-128")
MaskBTP(Workspace="data", Bank="99-102")
MaskBTP(Workspace="data", Bank="38-42", Pixel="120-128")
MaskBTP(Workspace="data", Bank="43", Pixel="119-128")
MaskBTP(Workspace="data", Bank="44-48", Pixel="120-128")
MaskBTP(Workspace="data", Bank="74", Tube="8")
MaskBTP(Workspace="data", Bank="96", Tube="8")
MaskBTP(Workspace="data", Bank="130-132", Pixel="113-128")
MaskBTP(Workspace="data", Bank="148", Tube="4")
MaskBTP(Workspace="data", Bank="46", Tube="6-8", Pixel="105-110")

FilterByLogValue(InputWorkspace="data", LogName="pause", MinimumValue=-1, MaximumValue=0.5, OutputWorkspace="data")

LoadNexusMonitors(Filename=datafile, OutputWorkspace="__MonWS")
e_i, t_0 = GetEiT0atSNS(MonitorWorkspace="__MonWS", IncidentEnergyGuess="35")
with amend_config(facility="SNS"):
DgsReduction(
SampleInputWorkspace="data",
SampleInputMonitorWorkspace="__MonWS",
IncidentEnergyGuess=e_i,
UseIncidentEnergyGuess=True,
TimeZeroGuess=t_0,
EnergyTransferRange="-17.5,1,31.5",
SofPhiEIsDistribution=False,
OutputWorkspace="dgs",
)
CropWorkspace(InputWorkspace="dgs", OutputWorkspace="dgs", XMin="-17.5", XMax="31.5")
min_values, max_values = ConvertToMDMinMaxGlobal(
InputWorkspace="dgs", QDimensions="Q3D", dEAnalysisMode="Direct", Q3DFrames="Q"
)

ConvertToMD(
InputWorkspace="dgs",
QDimensions="Q3D",
dEAnalysisMode="Direct",
Q3DFrames="Q_sample",
MinValues=min_values,
MaxValues=max_values,
MaxRecursionDepth=2,
OutputWorkspace=name,
)

# workspace does not have a ub
sample_model = SampleModel(name)

errors = []
params = {}

params["a"] = 4.44000
params["b"] = 4.44000
params["c"] = 4.44000
params["alpha"] = 90.0
params["beta"] = 90.0
params["gamma"] = 90.0
params["u"] = "0.00,-0.00,4.40"
params["v"] = "4.12717,4.12717,-0.000"

def error_callback(msg):
errors.append(msg)

sample_model.connect_error_message(error_callback)
sample_model.set_ub(params)

# check if the oriented lattice is updated
assert len(errors) == 0

assert sample_model.oriented_lattice.a() == params["a"]
assert sample_model.oriented_lattice.a() == params["b"]
assert sample_model.oriented_lattice.b() == params["c"]
assert sample_model.oriented_lattice.alpha() == approx(params["alpha"])
assert sample_model.oriented_lattice.beta() == approx(params["beta"])
assert sample_model.oriented_lattice.gamma() == approx(params["gamma"])


def test_apply_button_invalid():
"""Test for pressing Apply button with invalid input"""

Expand Down

0 comments on commit 0831391

Please sign in to comment.