def set_material_properties(self, **kwargs): err_msg = "The following argument is required but was not set or passed: " absorption_cross_section = common.dictionary_key_helper(dictionary=kwargs, key="absorption_cross_section", exception_msg=err_msg + "absorption_cross_section") scattering_cross_section = common.dictionary_key_helper(dictionary=kwargs, key="scattering_cross_section", exception_msg=err_msg + "scattering_cross_section") if self.material_object is None: raise RuntimeError("The material has not been set (or reset). Please set it by calling" " 'set_material()' to set the material details of the sample.") self.material_object.set_material_properties(abs_cross_sect=absorption_cross_section, scattering_cross_sect=scattering_cross_section)
def __init__(self, kwargs): # By using kwargs we get a better error than "init takes n arguments" height = common.dictionary_key_helper(dictionary=kwargs, key="height", exception_msg=property_err_string.format("height")) radius = common.dictionary_key_helper(dictionary=kwargs, key="radius", exception_msg=property_err_string.format("radius")) _Cylinder._validate_constructor_inputs(height=height, radius=radius) SampleDetails.validate_constructor_inputs({"height": height, "radius": radius}) self.height = float(height) self.radius = float(radius) self.shape_type = "cylinder"
def set_material(self, **kwargs): chemical_formula = common.dictionary_key_helper(dictionary=kwargs, key="chemical_formula", exception_msg="The following argument is required but was not" " passed: chemical_formula") number_density = common.dictionary_key_helper(dictionary=kwargs, key="number_density", throws=False) if self.material_object is not None: self.print_sample_details() raise RuntimeError("The material has already been set to the above details. If the properties" " have not been set they can be modified with 'set_material_properties()'. Otherwise" " to change the material call 'reset_sample_material()'") self.material_object = _Material(chemical_formula=chemical_formula, number_density=number_density)
def test_dictionary_key_helper(self): good_key_name = "key_exists" bad_key_name = "key_does_not_exist" test_dictionary = {good_key_name: 123} e_msg = "test message" with self.assertRaises(KeyError): common.dictionary_key_helper(dictionary=test_dictionary, key=bad_key_name) with assertRaisesRegex(self, KeyError, e_msg): common.dictionary_key_helper(dictionary=test_dictionary, key=bad_key_name, exception_msg=e_msg) self.assertEqual(common.dictionary_key_helper(dictionary=test_dictionary, key=good_key_name), 123)
def set_sample_details(self, **kwargs): kwarg_name = "sample" sample_details_obj = common.dictionary_key_helper( dictionary=kwargs, key=kwarg_name, exception_msg="The argument containing sample details was not found. Please" " set the following argument: " + kwarg_name) self._sample_details = sample_details_obj
def __init__(self, kwargs): # By using kwargs we get a better error than "init takes n arguments" thickness = common.dictionary_key_helper(dictionary=kwargs, key="thickness", exception_msg=property_err_string.format("thickness")) width = common.dictionary_key_helper(dictionary=kwargs, key="width", exception_msg=property_err_string.format("width")) height = common.dictionary_key_helper(dictionary=kwargs, key="height", exception_msg=property_err_string.format("height")) angle = common.dictionary_key_helper(dictionary=kwargs, key="angle", exception_msg=property_err_string.format("angle")) SampleDetails.validate_constructor_inputs({"thickness": thickness, "width": width, "height": height, "angle": angle}) self.thickness = float(thickness) self.width = float(width) self.height = float(height) self.angle = float(angle) self.shape_type = "slab"
def set_material(self, **kwargs): chemical_formula = common.dictionary_key_helper( dictionary=kwargs, key="chemical_formula", exception_msg="The following argument is required but was not" " passed: chemical_formula") number_density = common.dictionary_key_helper(dictionary=kwargs, key="number_density", throws=False) if self.material_object is not None: self.print_sample_details() raise RuntimeError( "The material has already been set to the above details. If the properties" " have not been set they can be modified with 'set_material_properties()'. Otherwise" " to change the material call 'reset_sample_material()'") self.material_object = _Material(chemical_formula=chemical_formula, number_density=number_density)
def __init__(self, kwargs): # By using kwargs we get a better error than "init takes n arguments" height = common.dictionary_key_helper( dictionary=kwargs, key="height", exception_msg=property_err_string.format("height")) radius = common.dictionary_key_helper( dictionary=kwargs, key="radius", exception_msg=property_err_string.format("radius")) _Cylinder._validate_constructor_inputs(height=height, radius=radius) SampleDetails.validate_constructor_inputs({ "height": height, "radius": radius }) self.height = float(height) self.radius = float(radius) self.shape_type = "cylinder"
def set_sample_details(self, **kwargs): self._switch_mode_specific_inst_settings(kwargs.get("mode")) kwarg_name = "sample" sample_details_obj = common.dictionary_key_helper( dictionary=kwargs, key=kwarg_name, exception_msg= "The argument containing sample details was not found. Please" " set the following argument: " + kwarg_name) self._sample_details = sample_details_obj
def test_dictionary_key_helper(self): good_key_name = "key_exists" bad_key_name = "key_does_not_exist" test_dictionary = {good_key_name: 123} e_msg = "test message" with self.assertRaises(KeyError): common.dictionary_key_helper(dictionary=test_dictionary, key=bad_key_name) with self.assertRaisesRegex(KeyError, e_msg): common.dictionary_key_helper(dictionary=test_dictionary, key=bad_key_name, exception_msg=e_msg) self.assertEqual( common.dictionary_key_helper(dictionary=test_dictionary, key=good_key_name), 123)
def test_dictionary_key_helper_handles_mixed_case(self): mixed_case_name = "tEsT_KeY" lower_case_name = mixed_case_name.lower() expected_val = 456 mixed_case_dict = {mixed_case_name: expected_val} # Check by default it doesn't try to account for key with self.assertRaises(KeyError): common.dictionary_key_helper(dictionary=mixed_case_dict, key=lower_case_name) # Next check if we have the flag set to False it still throws with self.assertRaises(KeyError): common.dictionary_key_helper(dictionary=mixed_case_dict, key=lower_case_name, case_insensitive=False) # Check we actually get the key when we do ask for case insensitive checks try: val = common.dictionary_key_helper(dictionary=mixed_case_dict, key=lower_case_name, case_insensitive=True) self.assertEqual(val, expected_val) except KeyError: self.fail("dictionary_key_helper did not perform case insensitive lookup")
def __init__(self, kwargs, sample_height, sample_radius, sample_center): # By using kwargs we get a better error than "init takes n arguments" container_radius = common.dictionary_key_helper(dictionary=kwargs, key="radius", exception_msg=property_err_string.format("radius")) SampleDetails.validate_constructor_inputs({"container_radius": container_radius}) self.container_radius = float(container_radius) self.shape_type = "HollowCylinder" self.sample_radius = sample_radius self.sample_height = sample_height self.sample_center = sample_center
def test_dictionary_key_helper_handles_mixed_case(self): mixed_case_name = "tEsT_KeY" lower_case_name = mixed_case_name.lower() expected_val = 456 mixed_case_dict = {mixed_case_name: expected_val} # Check by default it doesn't try to account for key with self.assertRaises(KeyError): common.dictionary_key_helper(dictionary=mixed_case_dict, key=lower_case_name) # Next check if we have the flag set to False it still throws with self.assertRaises(KeyError): common.dictionary_key_helper(dictionary=mixed_case_dict, key=lower_case_name, case_insensitive=False) # Check we actually get the key when we do ask for case insensitive checks try: val = common.dictionary_key_helper(dictionary=mixed_case_dict, key=lower_case_name, case_insensitive=True) self.assertEqual(val, expected_val) except KeyError: self.fail("dictionary_key_helper did not perform case insensitive lookup")
def set_container(self, **kwargs): chemical_formula = common.dictionary_key_helper(dictionary=kwargs, key="chemical_formula", exception_msg="The following argument is required but was not" " passed: chemical_formula") number_density = common.dictionary_key_helper(dictionary=kwargs, key="number_density", throws=False) number_density_effective = common.dictionary_key_helper(dictionary=kwargs, key="number_density_effective", throws=False) packing_fraction = common.dictionary_key_helper(dictionary=kwargs, key="packing_fraction", throws=False) if self.container_material_object is not None: self.print_container_details() raise RuntimeError("The container material has already been set to the above details. " "To change the material call 'reset_sample_material()'") self.container_material_object = _Material(chemical_formula=chemical_formula, number_density=number_density, number_density_effective=number_density_effective, packing_fraction=packing_fraction) if self._shape_type.capitalize() == "Cylinder": self._container_shape = _HollowCylinder(kwargs, sample_height=self.height(), sample_radius=self.radius(), sample_center=self.center()) elif self._shape_type.capitalize() == "Slab": self._container_shape = _FlatPlateHolder(kwargs, sample_width=self.width(), sample_height=self.height(), sample_thickness=self.thickness(), sample_angle=self.angle(), sample_center=self.center())
def __init__(self, **kwargs): self._shape_type = common.dictionary_key_helper(dictionary=kwargs, key="shape", throws=False) if self._shape_type is None: self._shape_type = "cylinder" warning = "Failed to supply parameter \"shape\" to SampleDetails - defaulting to \"cylinder\"" print("WARNING: {}".format(warning)) # Show warning in script window logger.warning(warning) # Show warning in Mantid logging area center = common.dictionary_key_helper(dictionary=kwargs, key="center", exception_msg=property_err_string.format("center")) SampleDetails._validate_center(center) self._center = [float(i) for i in center] # List of X, Y, Z position if self._shape_type == "cylinder": self._shape = _Cylinder(kwargs) elif self._shape_type == "slab": self._shape = _Slab(kwargs) else: raise KeyError("Shape type \"" + self._shape_type + "\" not supported: current supported shape types are " "\"cylinder\" and \"slab\"") self.material_object = None
def __init__(self, **kwargs): self._shape_type = common.dictionary_key_helper(dictionary=kwargs, key="shape", throws=False) if self._shape_type is None: self._shape_type = "cylinder" warning = "Failed to supply parameter \"shape\" to SampleDetails - defaulting to \"cylinder\"" print("WARNING: {}".format(warning)) # Show warning in script window logger.warning(warning) # Show warning in Mantid logging area center = common.dictionary_key_helper(dictionary=kwargs, key="center", exception_msg=property_err_string.format("center")) SampleDetails._validate_center(center) self._center = [float(i) for i in center] # List of X, Y, Z position if self._shape_type == "cylinder": self._shape = _Cylinder(kwargs) elif self._shape_type == "slab": self._shape = _Slab(kwargs) else: raise KeyError("Shape type \"" + self._shape_type + "\" not supported: current supported shape types are " "\"cylinder\" and \"slab\"") self.material_object = None
def create_vanadium_sample_details_obj(config_dict): """ Creates a SampleDetails object based on a vanadium sample which is found in the advanced config of an instrument. :param config_dict: The advanced config dictionary of the instrument for the vanadium sample :return: A sample details object which holds properties used in sample corrections """ height_key = "cylinder_sample_height" radius_key = "cylinder_sample_radius" pos_key = "cylinder_position" formula_key = "chemical_formula" number_density_key = "number_density" e_msg = "The following key was not found in the advanced configuration for sample correction:\n" height = common.dictionary_key_helper(dictionary=config_dict, key=height_key, exception_msg=e_msg + height_key) radius = common.dictionary_key_helper(dictionary=config_dict, key=radius_key, exception_msg=e_msg + radius_key) pos = common.dictionary_key_helper(dictionary=config_dict, key=pos_key, exception_msg=e_msg + pos_key) formula = common.dictionary_key_helper(dictionary=config_dict, key=formula_key, exception_msg=e_msg + formula_key) number_density = common.dictionary_key_helper(dictionary=config_dict, key=number_density_key, throws=False) vanadium_sample_details = sample_details.SampleDetails(height=height, radius=radius, center=pos, shape="cylinder") vanadium_sample_details.set_material(chemical_formula=formula, number_density=number_density) return vanadium_sample_details
def __init__(self, kwargs): # By using kwargs we get a better error than "init takes n arguments" thickness = common.dictionary_key_helper(dictionary=kwargs, key="thickness", exception_msg=property_err_string.format("thickness")) width = common.dictionary_key_helper(dictionary=kwargs, key="width", exception_msg=property_err_string.format("width")) height = common.dictionary_key_helper(dictionary=kwargs, key="height", exception_msg=property_err_string.format("height")) angle = common.dictionary_key_helper(dictionary=kwargs, key="angle", exception_msg=property_err_string.format("angle")) SampleDetails.validate_constructor_inputs({"thickness": thickness, "width": width, "height": height, "angle": angle}) self.thickness = float(thickness) self.width = float(width) self.height = float(height) self.angle = float(angle) self.shape_type = "slab" center = common.dictionary_key_helper(dictionary=kwargs, key="center", exception_msg=property_err_string.format("center")) SampleDetails.validate_center(center) self.center = [float(i) for i in center] # List of X, Y, Z position
def create_vanadium_sample_details_obj(config_dict): """ Creates a SampleDetails object based on a vanadium sample which is found in the advanced config of an instrument. :param config_dict: The advanced config dictionary of the instrument for the vanadium sample :return: A sample details object which holds properties used in sample corrections """ height_key = "cylinder_sample_height" radius_key = "cylinder_sample_radius" pos_key = "cylinder_position" formula_key = "chemical_formula" number_density_key = "number_density" e_msg = "The following key was not found in the advanced configuration for sample correction:\n" height = common.dictionary_key_helper(dictionary=config_dict, key=height_key, exception_msg=e_msg + height_key) radius = common.dictionary_key_helper(dictionary=config_dict, key=radius_key, exception_msg=e_msg + radius_key) pos = common.dictionary_key_helper(dictionary=config_dict, key=pos_key, exception_msg=e_msg + pos_key) formula = common.dictionary_key_helper(dictionary=config_dict, key=formula_key, exception_msg=e_msg + formula_key) number_density = common.dictionary_key_helper(dictionary=config_dict, key=number_density_key, throws=False) vanadium_sample_details = sample_details.SampleDetails(height=height, radius=radius, center=pos, shape="cylinder") vanadium_sample_details.set_material(chemical_formula=formula, number_density=number_density) return vanadium_sample_details
def __init__(self, **kwargs): self._shape_type = common.dictionary_key_helper(dictionary=kwargs, key="shape", throws=False) if self._shape_type is None: self._shape_type = "cylinder" warning = "Failed to supply parameter \"shape\" to SampleDetails - defaulting to \"cylinder\"" print("WARNING: {}".format(warning)) # Show warning in script window logger.warning(warning) # Show warning in Mantid logging area if self._shape_type == "cylinder": self._shape = _Cylinder(kwargs) elif self._shape_type == "slab": self._shape = _Slab(kwargs) else: raise KeyError("Shape type \"" + self._shape_type + "\" not supported: current supported shape types are " "\"cylinder\" and \"slab\"") self.material_object = None self.container_material_object = None self._container_shape = None