Skip to content

Commit

Permalink
configuration for saving sample logs added when saving histogram,load…
Browse files Browse the repository at this point in the history
… ub from nexus changes and related tests added
  • Loading branch information
mpatrou committed May 7, 2024
1 parent 9feea6b commit 33b4af2
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/shiver/configuration_template.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ display_title = full
#the flag (bool: True/False) indicates the plot scale (logarithmic or not)
logarithmic_intensity = False

[main_tab.sample_logs]
#the flag (bool = True/False) indicates whether the data are saved as sample logs in the MDHistogram workspace
save_instrument = True
save_sample = True
save_logs = True

[global.other]
help_url = https://neutrons.github.io/Shiver/GUI/
2 changes: 0 additions & 2 deletions src/shiver/models/convert_dgs_to_single_mde.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
FileAction,
)
from mantid.kernel import (
config,
Direction,
Property,
StringArrayProperty,
Expand Down Expand Up @@ -247,7 +246,6 @@ def validateInputs(self):
return issues

def PyExec(self): # pylint: disable=too-many-branches
config["default.facility"] = "SNS"
# get properties
data = self.getProperty("InputWorkspace").value
data_m = self.getProperty("InputMonitorWorkspace").value
Expand Down
17 changes: 14 additions & 3 deletions src/shiver/models/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
SpaceGroupFactory,
PointGroupFactory,
)

from shiver.configuration import get_data
from shiver.models.generate import GenerateModel
from shiver.models.polarized import PolarizedModel

Expand Down Expand Up @@ -195,8 +195,19 @@ def rename(self, old_name, new_name):

def save(self, ws_name, filename):
"""Save the workspace to Nexus file."""
save_params = {"SaveInstrument": False, "SaveSample": False, "SaveLogs": False}
SaveMD(ws_name, filename, **save_params)
save_instrument = get_data("main_tab.sample_logs", "save_instrument")
save_sample = get_data("main_tab.sample_logs", "save_sample")
save_logs = get_data("main_tab.sample_logs", "save_logs")

if isinstance(save_instrument, bool) and isinstance(save_sample, bool) and isinstance(save_logs, bool):
save_params = {"SaveInstrument": save_instrument, "SaveSample": save_sample, "SaveLogs": save_logs}
print("here", save_instrument, save_sample, save_logs)
SaveMD(ws_name, filename, **save_params)
else:
if self.error_callback:
err = "Save_logs in configuration file has invalid input. Please update it and try again."
logger.error(err)
self.error_callback(err)

def save_to_ascii(
self,
Expand Down
11 changes: 6 additions & 5 deletions src/shiver/models/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
LoadNexusProcessed,
LoadMD,
)
from mantid.api import AlgorithmManager
from mantid.geometry import OrientedLattice
from mantid.kernel import Logger
from mantidqtinterfaces.DGSPlanner.LoadNexusUB import LoadNexusUB
Expand Down Expand Up @@ -164,11 +165,11 @@ def load_nexus_processed(self, filename):
def is_nexus_processed(self, filename):
"""Return true if the file is NexusProcessed
- can be loaded through LoadNexusProcessed, else false"""
try:
_ = LoadNexusProcessed(filename)
return True
except RuntimeError:
return False

alg = AlgorithmManager.create("Load")
alg.initialize()
alg.setProperty("Filename", filename)
return alg.getProperty("LoaderName").value == "LoadNexusProcessed"

def load_nexus_ub(self, filename):
"""Mantid SetUB with Nexus file"""
Expand Down
59 changes: 47 additions & 12 deletions tests/models/test_generatemde.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
"""Tests for the ConvertDGSToSingleMDE algorithm"""
import os
from pytest import approx, raises
from mantid.simpleapi import ( # pylint: disable=no-name-in-module
from mantid.kernel import amend_config

# Need to import the new algorithms so they are registered with mantid
import shiver.models.convert_dgs_to_single_mde # noqa: F401, E402 pylint: disable=unused-import, wrong-import-order
import shiver.models.generate_dgs_mde # noqa: F401, E402 pylint: disable=unused-import, wrong-import-order

from mantid.simpleapi import ( # pylint: disable=no-name-in-module, ungrouped-imports
ConfigService,
ConvertDGSToSingleMDE,
GenerateDGSMDE,
MergeMD,
Expand Down Expand Up @@ -142,6 +149,34 @@ def test_convert_dgs_to_single_mde_calculate_t0_ei():
assert md1.getExperimentInfo(0).run()["Ei"].value == 25


def test_convert_dgs_to_single_mde_facility():
"""Test for ConvertDGSToSingleMDE facility"""

with amend_config(facility="HFIR"):
facility = ConfigService.getFacility().name()
assert facility == "HFIR"
print("facility", facility)
data_files = [
"HYS_178921.nxs.h5",
"HYS_178922.nxs.h5",
"HYS_178923.nxs.h5",
"HYS_178924.nxs.h5",
"HYS_178925.nxs.h5",
"HYS_178926.nxs.h5",
]

raw_data_folder = os.path.join(os.path.dirname(__file__), "../data/raw")

_ = GenerateDGSMDE(
Filenames=",".join(os.path.join(raw_data_folder, data_file) for data_file in data_files),
Ei=25.0,
T0=112.0,
TimeIndependentBackground="Default",
)
print("facility", facility)
assert ConfigService.getFacility().name() == "HFIR"


def test_convert_dgs_to_single_mde_merged():
"""Test for merging results and compare to existing data ConvertDGSToSingleMDE"""

Expand Down Expand Up @@ -257,17 +292,17 @@ def test_generate_dgs_mde_seq():

LoadNexusMonitors(Filename=datafile, OutputWorkspace="__MonWS")
e_i, t_0 = GetEiT0atSNS(MonitorWorkspace="__MonWS", IncidentEnergyGuess="35")

DgsReduction(
SampleInputWorkspace="data",
SampleInputMonitorWorkspace="__MonWS",
IncidentEnergyGuess=e_i,
UseIncidentEnergyGuess=True,
TimeZeroGuess=t_0,
EnergyTransferRange="-17.5,1,31.5",
SofPhiEIsDistribution=False,
OutputWorkspace="dgs",
)
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"
Expand Down
Loading

0 comments on commit 33b4af2

Please sign in to comment.