class StateAdjustment(StateBase): calculate_transmission = TypedParameter(StateCalculateTransmission, validator_sub_state) normalize_to_monitor = TypedParameter(StateNormalizeToMonitor, validator_sub_state) wavelength_and_pixel_adjustment = TypedParameter(StateWavelengthAndPixelAdjustment, validator_sub_state) wide_angle_correction = BoolParameter() show_transmission = BoolParameter() def __init__(self): super(StateAdjustment, self).__init__() self.wide_angle_correction = False self.show_transmission = False def validate(self): is_invalid = {} # Calculate transmission if not self.calculate_transmission: is_invalid.update({"StateAdjustment": "The StateCalculateTransmission object is missing."}) if self.calculate_transmission: try: self.calculate_transmission.validate() except ValueError as e: is_invalid.update({"StateAdjustment": "The sub-CalculateTransmission state is invalid," " see here {0}".format(str(e))}) # Normalize to monitor if not self.normalize_to_monitor: is_invalid.update({"StateAdjustment": "The StateNormalizeToMonitor object is missing."}) if self.normalize_to_monitor: try: self.normalize_to_monitor.validate() except ValueError as e: is_invalid.update({"StateAdjustment": "The sub-NormalizeToMonitor state is invalid," " see here {0}".format(str(e))}) # Wavelength and pixel adjustment if not self.wavelength_and_pixel_adjustment: is_invalid.update({"StateAdjustment": "The StateWavelengthAndPixelAdjustment object is missing."}) if self.wavelength_and_pixel_adjustment: try: self.wavelength_and_pixel_adjustment.validate() except ValueError as e: is_invalid.update({"StateAdjustment": "The sub-WavelengthAndPixelAdjustment state is invalid," " see here {0}".format(str(e))}) if is_invalid: raise ValueError("StateAdjustment: The provided inputs are illegal. " "Please see: {0}".format(json.dumps(is_invalid)))
class ComplexState(StateBase): float_parameter = FloatParameter() positive_float_with_none_parameter = PositiveFloatWithNoneParameter() sub_state_1 = TypedParameter(SimpleState, validator_sub_state) dict_parameter = DictParameter() def __init__(self): super(ComplexState, self).__init__() self.float_parameter = 23. self.positive_float_with_none_parameter = 234. self.sub_state_1 = SimpleState() self.dict_parameter = {"A": SimpleState(), "B": SimpleState()} def validate(self): pass
class SimpleState(StateBase): string_parameter = StringParameter() bool_parameter = BoolParameter() float_parameter = FloatParameter() positive_float_parameter = PositiveFloatParameter() positive_integer_parameter = PositiveIntegerParameter() dict_parameter = DictParameter() float_with_none_parameter = FloatWithNoneParameter() positive_float_with_none_parameter = PositiveFloatWithNoneParameter() float_list_parameter = FloatListParameter() string_list_parameter = StringListParameter() positive_integer_list_parameter = PositiveIntegerListParameter() class_type_parameter = ClassTypeParameter(TestType) class_type_list_parameter = ClassTypeListParameter(TestType) sub_state_very_simple = TypedParameter(VerySimpleState, validator_sub_state) def __init__(self): super(SimpleState, self).__init__() self.string_parameter = "String_in_SimpleState" self.bool_parameter = False # We explicitly leave out the float_parameter self.positive_float_parameter = 1. self.positive_integer_parameter = 6 self.dict_parameter = {"1": 123, "2": "test"} self.float_with_none_parameter = 325. # We expliclty leave out the positive_float_with_none_parameter self.float_list_parameter = [123., 234.] self.string_list_parameter = ["test1", "test2"] self.positive_integer_list_parameter = [1, 2, 3] self.class_type_parameter = TestType.TypeA self.class_type_list_parameter = [TestType.TypeA, TestType.TypeB] self.sub_state_very_simple = VerySimpleState() def validate(self): pass
class State(StateBase): data = TypedParameter(StateData, validator_sub_state) move = TypedParameter(StateMove, validator_sub_state) reduction = TypedParameter(StateReductionMode, validator_sub_state) slice = TypedParameter(StateSliceEvent, validator_sub_state) mask = TypedParameter(StateMask, validator_sub_state) wavelength = TypedParameter(StateWavelength, validator_sub_state) save = TypedParameter(StateSave, validator_sub_state) scale = TypedParameter(StateScale, validator_sub_state) adjustment = TypedParameter(StateAdjustment, validator_sub_state) convert_to_q = TypedParameter(StateConvertToQ, validator_sub_state) compatibility = TypedParameter(StateCompatibility, validator_sub_state) def __init__(self): super(State, self).__init__() def validate(self): is_invalid = dict() # Make sure that the substates are contained if not self.data: is_invalid.update("State: The state object needs to include a StateData object.") if not self.move: is_invalid.update("State: The state object needs to include a StateMove object.") if not self.reduction: is_invalid.update("State: The state object needs to include a StateReduction object.") if not self.slice: is_invalid.update("State: The state object needs to include a StateSliceEvent object.") if not self.mask: is_invalid.update("State: The state object needs to include a StateMask object.") if not self.wavelength: is_invalid.update("State: The state object needs to include a StateWavelength object.") if not self.save: is_invalid.update("State: The state object needs to include a StateSave object.") if not self.scale: is_invalid.update("State: The state object needs to include a StateScale object.") if not self.adjustment: is_invalid.update("State: The state object needs to include a StateAdjustment object.") if not self.convert_to_q: is_invalid.update("State: The state object needs to include a StateConvertToQ object.") # We don't enforce a compatibility mode, we just create one if it does not exist if not self.compatibility: if self.data: self.compatibility = get_compatibility_builder(self.data).build() if is_invalid: raise ValueError("State: There is an issue with your in put. See: {0}".format(json.dumps(is_invalid))) # Check the attributes themselves is_invalid = {} for descriptor_name, descriptor_object in inspect.getmembers(type(self)): if inspect.isdatadescriptor(descriptor_object) and isinstance(descriptor_object, TypedParameter): try: attr = getattr(self, descriptor_name) attr.validate() except ValueError as err: is_invalid.update({descriptor_name: pickle.dumps(str(err))}) if is_invalid: raise ValueError("State: There is an issue with your in put. See: {0}".format(json.dumps(is_invalid)))