|
| 1 | +import jsonschema |
| 2 | +import traceback |
| 3 | +from multi_vector_simulator.utils.data_parser import MAP_EPA_MVS |
| 4 | + |
| 5 | + |
| 6 | +SA_SCHEMA = { |
| 7 | + "type": "object", |
| 8 | + "required": [ |
| 9 | + "variable_parameter_name", |
| 10 | + "variable_parameter_range", |
| 11 | + "variable_parameter_ref_val", |
| 12 | + "output_parameter_names", |
| 13 | + ], |
| 14 | + "properties": { |
| 15 | + "variable_parameter_name": { |
| 16 | + "oneOf": [{"type": "array", "items": {"type": "string"}}] |
| 17 | + }, |
| 18 | + "variable_parameter_range": {"type": "array", "items": {"type": "number"}}, |
| 19 | + "variable_parameter_ref_val": {"type": "number"}, |
| 20 | + "output_parameter_names": {"type": "array", "items": {"type": "string"}}, |
| 21 | + }, |
| 22 | + "additionalProperties": False, |
| 23 | +} |
| 24 | + |
| 25 | +SENSITIVITY_ANALYSIS_SETTINGS = "sensitivity_analysis_settings" |
| 26 | + |
| 27 | + |
| 28 | +class SensitivityAnalysis: |
| 29 | + __raw_input = None |
| 30 | + variable_parameter_name = None |
| 31 | + variable_parameter_range = None |
| 32 | + variable_parameter_ref_val = None |
| 33 | + output_parameter_names = None |
| 34 | + validation_error = "" |
| 35 | + |
| 36 | + def __init__(self, dict_settings): |
| 37 | + sa_settings = dict_settings.get(SENSITIVITY_ANALYSIS_SETTINGS, None) |
| 38 | + self.__raw_input = sa_settings |
| 39 | + if sa_settings is not None: |
| 40 | + try: |
| 41 | + jsonschema.validate(sa_settings, SA_SCHEMA) |
| 42 | + schema_is_valid = True |
| 43 | + except jsonschema.exceptions.ValidationError as e: |
| 44 | + schema_is_valid = False |
| 45 | + self.validation_error = "{}".format(traceback.format_exc()) |
| 46 | + |
| 47 | + if schema_is_valid is True: |
| 48 | + self.variable_parameter_name = sa_settings.get( |
| 49 | + "variable_parameter_name", None |
| 50 | + ) |
| 51 | + self.format_parameter_name() |
| 52 | + |
| 53 | + self.variable_parameter_range = sa_settings.get( |
| 54 | + "variable_parameter_range", None |
| 55 | + ) |
| 56 | + self.output_parameter_names = sa_settings.get( |
| 57 | + "output_parameter_names", None |
| 58 | + ) |
| 59 | + self.variable_parameter_ref_val = sa_settings.get( |
| 60 | + "variable_parameter_ref_val", None |
| 61 | + ) |
| 62 | + else: |
| 63 | + self.validation_error = ( |
| 64 | + f"The key {SENSITIVITY_ANALYSIS_SETTINGS} is missing in the input json" |
| 65 | + ) |
| 66 | + |
| 67 | + def format_parameter_name(self): |
| 68 | + if self.variable_parameter_name is not None: |
| 69 | + self.variable_parameter_name = tuple( |
| 70 | + [MAP_EPA_MVS.get(key, key) for key in self.variable_parameter_name] |
| 71 | + ) |
| 72 | + |
| 73 | + def is_valid(self): |
| 74 | + if ( |
| 75 | + self.variable_parameter_name is not None |
| 76 | + and self.variable_parameter_range is not None |
| 77 | + and self.output_parameter_names is not None |
| 78 | + and self.variable_parameter_ref_val |
| 79 | + ): |
| 80 | + answer = True |
| 81 | + if self.variable_parameter_ref_val not in self.variable_parameter_range: |
| 82 | + answer = False |
| 83 | + self.validation_error = ( |
| 84 | + f"The value ({self.variable_parameter_ref_val}) of the variable parameter" |
| 85 | + f" {'.'.join(self.variable_parameter_name)} for the reference scenario of" |
| 86 | + " the sensitivity analysis is not within the variable range provided" |
| 87 | + f": [{', '.join([str(p) for p in self.variable_parameter_range]) }]" |
| 88 | + ) |
| 89 | + else: |
| 90 | + answer = False |
| 91 | + return answer |
| 92 | + |
| 93 | + def __str__(self): |
| 94 | + return str(self.__raw_input) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + import json |
| 99 | + |
| 100 | + with open("test_sa.json", "r") as jf: |
| 101 | + input_dict = json.load(jf) |
| 102 | + |
| 103 | + # with open("AFG_epa_format.json", "w") as jf: |
| 104 | + # json.dump(input_dict, jf, indent=4) |
| 105 | + |
| 106 | + # input_dict[SENSITIVITY_ANALYSIS_SETTINGS] = { |
| 107 | + # "variable_parameter_name": ["energy_busses"], |
| 108 | + # "variable_parameter_range": [1, 2, 3.2, 3.5], |
| 109 | + # "variable_parameter_ref_val": 3, |
| 110 | + # "output_parameter_names": [ |
| 111 | + # "specific_emissions_per_electricity_equivalent", |
| 112 | + # "total_feedinElectricity", |
| 113 | + # "total_internal_generation", |
| 114 | + # "peak_flow", |
| 115 | + # ], |
| 116 | + # } |
| 117 | + sa = SensitivityAnalysis(input_dict) |
| 118 | + print(sa.is_valid(), sa.validation_error) |
| 119 | + import ipdb |
| 120 | + |
| 121 | + ipdb.set_trace() |
| 122 | + # input_dict[SENSITIVITY_ANALYSIS_SETTINGS] = { |
| 123 | + # "variable_parameter_name": [ |
| 124 | + # "energy_providers", |
| 125 | + # "Grid_DSO", |
| 126 | + # "energy_price", |
| 127 | + # "value", |
| 128 | + # ], |
| 129 | + # "variable_parameter_range": [1, 2, 3, 3.5], |
| 130 | + # "variable_parameter_ref_val": 3, |
| 131 | + # "output_parameter_names": [ |
| 132 | + # "specific_emissions_per_electricity_equivalent", |
| 133 | + # "total_feedinElectricity", |
| 134 | + # "total_internal_generation", |
| 135 | + # "peak_flow", |
| 136 | + # ], |
| 137 | + # # } |
| 138 | + # sa = SensitivityAnalysis(input_dict) |
| 139 | + # print(sa.is_valid(), sa.validation_error) |
| 140 | + # sa = SensitivityAnalysis({SENSITIVITY_ANALYSIS_SETTINGS: { |
| 141 | + # "variable_parameter_name": "", |
| 142 | + # "variable_parameter_range": "", |
| 143 | + # "variable_parameter_ref_val": "", |
| 144 | + # "output_parameter_names": "", |
| 145 | + # }}) |
0 commit comments