def test_dictionary_parses_correctly(self): expected_value = "bar" second_value = "foo" # Write in two ranges to check it determines the correct one yaml_handle = self.get_temp_file_handle() yaml_handle.write("100-200:\n") yaml_handle.write(" test_item: '" + expected_value + "'\n") yaml_handle.write("201-:\n") yaml_handle.write(" test_item: '" + second_value + "'\n") # Close handle so the test can access it yaml_handle.close() # Check a random value in the mid point returned_dict = yaml_parser.get_run_dictionary(run_number_string="150", file_path=yaml_handle.name) self.assertEqual(returned_dict["test_item"], expected_value) # Check lower bound is respected returned_dict = yaml_parser.get_run_dictionary(run_number_string="100", file_path=yaml_handle.name) self.assertEqual(returned_dict["test_item"], expected_value, "Lower bound not respected") # Check upper bound is respected returned_dict = yaml_parser.get_run_dictionary(run_number_string="200", file_path=yaml_handle.name) self.assertEqual(returned_dict["test_item"], expected_value, "Upper bound not respected") # Check we can handle a range returned_dict = yaml_parser.get_run_dictionary(run_number_string="120-130", file_path=yaml_handle.name) self.assertEqual(returned_dict["test_item"], expected_value, "Range returned incorrect value") # Check the the second dictionary works with unbounded ranges returned_dict = yaml_parser.get_run_dictionary(run_number_string="205", file_path=yaml_handle.name) self.assertEqual(returned_dict["test_item"], second_value)
def test_blank_file_gives_sane_err(self): file_handle = self.get_temp_file_handle() # Write nothing and close file_path = file_handle.name file_handle.close() with assertRaisesRegex(self, ValueError, "YAML files appears to be empty at"): yaml_parser.get_run_dictionary(run_number_string=1, file_path=file_path)
def test_yaml_sanity_check_picks_up_two_unbounded(self): # Check we can detect two unbounded ranges file_handle = self.get_temp_file_handle() file_handle.write("10-:\n") file_handle.write("20-:\n") file_path = file_handle.name file_handle.close() with assertRaisesRegex(self, ValueError, "Seen multiple unbounded keys in mapping file"): yaml_parser.get_run_dictionary(run_number_string="11", file_path=file_path)
def test_yaml_sanity_detects_val_larger_than_unbound(self): # If we have a value that is larger the the unbounded range can we detect this file_handle = self.get_temp_file_handle() file_handle.write("30-:\n") file_handle.write("35:\n") file_path = file_handle.name file_handle.close() with assertRaisesRegex(self, ValueError, "Found a run range in calibration mapping overlaps an unbounded run " "range"): yaml_parser.get_run_dictionary(run_number_string="32", file_path=file_path)
def test_file_not_found_gives_sane_err(self): # Create a file then delete it so we know it cannot exist at that path file_handle = tempfile.NamedTemporaryFile(delete=False) file_path = file_handle.name file_handle.close() os.remove(file_path) if os.path.exists(file_path): self.fail("File exists after deleting cannot continue this test") # Check the error message is there with assertRaisesRegex(self, ValueError, "Config file not found at path"): yaml_parser.get_run_dictionary(run_number_string="1", file_path=file_path)
def get_cal_mapping_dict(run_number_string, cal_mapping_path): # Get the python dictionary from the YAML mapping run_number = common.get_first_run_number( run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary( run_number_string=run_number, file_path=cal_mapping_path) return cal_mapping_dict
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") run_number = common.get_first_run_number( run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary( run_number_string=run_number, file_path=mock_inst.cal_mapping_path) grouping_filename = mock_inst.grouping_file_name empty_runs = common.cal_map_dictionary_key_helper( dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper( dictionary=cal_mapping_dict, key="vanadium_run_numbers") output_obj = run_details.create_run_details_object( run_number_string=run_number_string, inst_settings=mock_inst, is_vanadium_run=True, grouping_file_name=grouping_filename, empty_run_number=empty_runs, vanadium_string=vanadium_runs) 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_run_details_splined_name_list_is_used(self): expected_vanadium_runs = "11-12" splined_name_list = ["bar", "bang", "baz"] run_number_string = "10" mock_inst = self.setup_mock_inst_settings( yaml_file_path="ISISPowderRunDetailsTest.yaml") run_number = common.get_first_run_number( run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary( run_number_string=run_number, file_path=mock_inst.cal_mapping_path) grouping_filename = mock_inst.grouping_file_name empty_runs = common.cal_map_dictionary_key_helper( dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper( dictionary=cal_mapping_dict, key="vanadium_run_numbers") output_obj = run_details.create_run_details_object( run_number_string, inst_settings=mock_inst, is_vanadium_run=False, splined_name_list=splined_name_list, grouping_file_name=grouping_filename, empty_run_number=empty_runs, vanadium_string=vanadium_runs) 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 += ".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)
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") run_number = common.get_first_run_number(run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary(run_number_string=run_number, file_path=mock_inst.cal_mapping_path) grouping_filename = mock_inst.grouping_file_name empty_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="vanadium_run_numbers") output_obj = run_details.create_run_details_object(run_number_string=run_number_string, inst_settings=mock_inst, is_vanadium_run=False, grouping_file_name=grouping_filename, empty_run_number=empty_runs, vanadium_string=vanadium_runs) 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)) expected_file_ext = mock_inst.file_extension expected_file_ext = expected_file_ext if expected_file_ext.startswith('.') else '.' + expected_file_ext self.assertEqual(output_obj.file_extension, expected_file_ext) 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_generate_out_file_paths(self): cal_dir = self._create_temp_dir() out_dir = self._create_temp_dir() mock_inst = self._setup_mock_inst(suffix="-suf", yaml_file_path="ISISPowderRunDetailsTest.yaml", calibration_dir=cal_dir, output_dir=out_dir) run_number = 15 run_number2 = common.get_first_run_number(run_number_string=run_number) cal_mapping_dict = yaml_parser.get_run_dictionary(run_number_string=run_number2, file_path=mock_inst.cal_mapping_path) grouping_filename = _gen_random_string() empty_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="vanadium_run_numbers") run_details_obj = run_details.create_run_details_object(run_number_string=run_number, inst_settings=mock_inst._inst_settings, is_vanadium_run=False, grouping_file_name=grouping_filename, empty_run_number=empty_runs, vanadium_string=vanadium_runs) output_paths = mock_inst._generate_out_file_paths(run_details=run_details_obj) expected_nxs_filename = os.path.join(out_dir, "16_4", "ISISPowderAbstractInstrumentTest", "MOCK15-suf.nxs") self.assertEquals(output_paths["nxs_filename"], expected_nxs_filename)
def test_run_number_not_found_gives_sane_err(self): expected_val = "yamlParserTest" file_handle = self.get_temp_file_handle() file_handle.write("10-20:\n") file_handle.write(" test_key: '" + expected_val + "'\n") file_handle.write("21-:\n") file_handle.write(" test_key: '" + expected_val + "'\n") file_path = file_handle.name file_handle.close() # Test a value in the middle of 1-10 with assertRaisesRegex(self, ValueError, "Run number 5 not recognised in cycle mapping file"): yaml_parser.get_run_dictionary(run_number_string="5", file_path=file_path) # Check on edge of invalid numbers with assertRaisesRegex(self, ValueError, "Run number 9 not recognised in cycle mapping file"): yaml_parser.get_run_dictionary(run_number_string=9, file_path=file_path) # What about a range of numbers with assertRaisesRegex(self, ValueError, "Run number 2 not recognised in cycle mapping file"): yaml_parser.get_run_dictionary(run_number_string="2-8", file_path=file_path) # Check valid number still works returned_dict = yaml_parser.get_run_dictionary(run_number_string="10", file_path=file_path) self.assertEqual(returned_dict["test_key"], expected_val)
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") run_number = common.get_first_run_number(run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary(run_number_string=run_number, file_path=mock_inst.cal_mapping_path) grouping_filename = mock_inst.grouping_file_name empty_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="vanadium_run_numbers") output_obj = run_details.create_run_details_object(run_number_string=run_number_string, inst_settings=mock_inst, is_vanadium_run=True, grouping_file_name=grouping_filename, empty_run_number=empty_runs, vanadium_string=vanadium_runs) 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 _setup_for_generate_out_file_paths(self, nxs_template="", tof_xye_template="", suffix=None, file_ext=None, dat_files_dir=""): cal_dir = self._create_temp_dir() out_dir = self._create_temp_dir() mock_inst = self._setup_mock_inst( suffix=suffix, file_ext=file_ext, yaml_file_path="ISISPowderRunDetailsTest.yaml", calibration_dir=cal_dir, output_dir=out_dir, nxs_filename=nxs_template, tof_xye_filename=tof_xye_template, dat_files_directory=dat_files_dir) run_number = 15 run_number2 = common.get_first_run_number(run_number_string=run_number) cal_mapping_dict = yaml_parser.get_run_dictionary( run_number_string=run_number2, file_path=mock_inst.cal_mapping_path) grouping_filename = _gen_random_string() empty_runs = common.cal_map_dictionary_key_helper( dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper( dictionary=cal_mapping_dict, key="vanadium_run_numbers") run_details_obj = run_details.create_run_details_object( run_number_string=run_number, inst_settings=mock_inst._inst_settings, is_vanadium_run=False, grouping_file_name=grouping_filename, empty_inst_run_number=empty_runs, vanadium_string=vanadium_runs) return mock_inst, run_details_obj, out_dir
def test_run_details_splined_name_list_is_used(self): expected_vanadium_runs = "11-12" splined_name_list = ["bar", "bang", "baz"] run_number_string = "10" mock_inst = self.setup_mock_inst_settings(yaml_file_path="ISISPowderRunDetailsTest.yaml") run_number = common.get_first_run_number(run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary(run_number_string=run_number, file_path=mock_inst.cal_mapping_path) grouping_filename = mock_inst.grouping_file_name empty_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="empty_run_numbers") vanadium_runs = common.cal_map_dictionary_key_helper(dictionary=cal_mapping_dict, key="vanadium_run_numbers") output_obj = run_details.create_run_details_object(run_number_string, inst_settings=mock_inst, is_vanadium_run=False, splined_name_list=splined_name_list, grouping_file_name=grouping_filename, empty_run_number=empty_runs, vanadium_string=vanadium_runs) 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 += ".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)
def get_cal_mapping_dict(run_number_string, cal_mapping_path): # Get the python dictionary from the YAML mapping run_number = common.get_first_run_number(run_number_string=run_number_string) cal_mapping_dict = yaml_parser.get_run_dictionary(run_number_string=run_number, file_path=cal_mapping_path) return cal_mapping_dict