Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import (absolute_import, division, print_function)
import mantid.api
import tempfile
import os
import random
import string
import unittest
import warnings
from isis_powder.routines import RunDetails
class ISISPowderInstrumentRunDetailsTest(unittest.TestCase):
def setup_mock_inst_settings(self, yaml_file_path):
calibration_dir = tempfile.mkdtemp()
self._folders_to_remove = [calibration_dir]
test_configuration_path = mantid.api.FileFinder.getFullPath(yaml_file_path)
if not test_configuration_path or len(test_configuration_path) <= 0:
self.fail("Could not find the unit test input file called: " + str(yaml_file_path))
mock_inst = MockInstSettings(cal_file_path=test_configuration_path, calibration_dir=calibration_dir)
return mock_inst
def tearDown(self):
for folder in self._folders_to_remove:
try:
os.rmdir(folder)
except OSError:
warnings.warn("Could not remove the folder at the following path:\n" + str(folder))
def test_create_run_details_object(self):
# These attributes are based on a flat YAML file at the specified path
expected_label = "16_4"
expected_vanadium_runs = "11-12"
expected_empty_runs = "13-14"
expected_offset_file_name = "offset_file_name"
run_number_string = "17-18"
mock_inst = self.setup_mock_inst_settings(yaml_file_path="ISISPowderRunDetailsTest.yaml")
output_obj = RunDetails.create_run_details_object(run_number_string=run_number_string, inst_settings=mock_inst,
is_vanadium_run=False)
self.assertEqual(output_obj.empty_runs, expected_empty_runs)
self.assertEqual(output_obj.grouping_file_path,
os.path.join(mock_inst.calibration_dir, mock_inst.grouping_file_name))
self.assertEqual(output_obj.label, expected_label)
self.assertEqual(output_obj.offset_file_path,
os.path.join(mock_inst.calibration_dir, expected_label, expected_offset_file_name))
self.assertEqual(output_obj.output_run_string, run_number_string)
self.assertEqual(output_obj.run_number, 17)
self.assertEqual(output_obj.vanadium_run_numbers, expected_vanadium_runs)
def test_create_run_details_object_when_van_cal(self):
# When we are running the vanadium calibration we expected the run number to take the vanadium
# number instead
run_number_string = "17-18"
expected_vanadium_runs = "11-12"
mock_inst = self.setup_mock_inst_settings(yaml_file_path="ISISPowderRunDetailsTest.yaml")
output_obj = RunDetails.create_run_details_object(run_number_string=run_number_string, inst_settings=mock_inst,
is_vanadium_run=True)
self.assertEqual(expected_vanadium_runs, output_obj.run_number)
self.assertEqual(output_obj.vanadium_run_numbers, output_obj.run_number)
self.assertEqual(expected_vanadium_runs, output_obj.output_run_string)
def test_callable_params_are_used(self):
# These attributes are based on a custom YAML file at the specified path
expected_label = "16_4"
expected_vanadium_runs = "11-12"
expected_empty_runs = "13-14"
expected_grouping_file_name = "grouping_file"
run_number_string = "17-18"
mock_inst = self.setup_mock_inst_settings(yaml_file_path="ISISPowderRunDetailsTestCallable.yaml")
# Get the YAML file as a dict first
wrapped_funcs = RunDetails.WrappedFunctionsRunDetails
yaml_callable = RunDetails.RunDetailsFuncWrapper(function=wrapped_funcs.get_cal_mapping_dict,
run_number_string=run_number_string, inst_settings=mock_inst)
empty_callable = yaml_callable.add_to_func_chain(user_function=wrapped_funcs.cal_dictionary_key_helper,
key="custom_empty_run_numbers")
vanadium_callable = yaml_callable.add_to_func_chain(user_function=wrapped_funcs.cal_dictionary_key_helper,
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
key="custom_vanadium_run_numbers")
grouping_callable = RunDetails.RunDetailsFuncWrapper(function=lambda: expected_grouping_file_name)
output_obj = RunDetails.create_run_details_object(run_number_string=run_number_string, inst_settings=mock_inst,
is_vanadium_run=True, empty_run_call=empty_callable,
vanadium_run_call=vanadium_callable,
grouping_file_name_call=grouping_callable)
self.assertEqual(output_obj.label, expected_label)
self.assertEqual(output_obj.empty_runs, expected_empty_runs)
self.assertEqual(output_obj.grouping_file_path,
os.path.join(mock_inst.calibration_dir, expected_grouping_file_name))
self.assertEqual(output_obj.vanadium_run_numbers, expected_vanadium_runs)
self.assertEqual(output_obj.run_number, expected_vanadium_runs)
def test_run_details_splined_name_list_is_used(self):
expected_vanadium_runs = "11-12"
expected_offset_file_name = "offset_file_name"
splined_name_list = ["bar", "bang", "baz"]
mock_inst = self.setup_mock_inst_settings(yaml_file_path="ISISPowderRunDetailsTest.yaml")
output_obj = RunDetails.create_run_details_object(run_number_string=10, inst_settings=mock_inst,
is_vanadium_run=False, splined_name_list=splined_name_list)
expected_splined_out_str = ''.join('_' + val for val in splined_name_list)
expected_output_name = "VanSplined_" + expected_vanadium_runs + expected_splined_out_str
expected_output_name += '_' + expected_offset_file_name + ".nxs"
expected_path = os.path.join(mock_inst.calibration_dir, output_obj.label, expected_output_name)
self.assertEqual(expected_path, output_obj.splined_vanadium_file_path)
class MockInstSettings(object):
def __init__(self, cal_file_path, calibration_dir):
self.calibration_dir = calibration_dir
self.cal_mapping_path = cal_file_path
self.grouping_file_name = MockInstSettings.gen_random_string()
@staticmethod
def gen_random_string():
return ''.join(random.choice(string.ascii_lowercase) for _ in range(10))
if __name__ == "__main__":
unittest.main()