|
| 1 | +import json |
| 2 | +from projects.dtos import convert_to_dto |
| 3 | + |
| 4 | + |
| 5 | +# Helper method to clean dict data from empty values |
| 6 | +def remove_empty_elements(d): |
| 7 | + def empty(x): |
| 8 | + return x is None or x == {} or x == [] |
| 9 | + |
| 10 | + if not isinstance(d, (dict, list)): |
| 11 | + return d |
| 12 | + elif isinstance(d, list): |
| 13 | + return [v for v in (remove_empty_elements(v) for v in d) if not empty(v)] |
| 14 | + else: |
| 15 | + return { |
| 16 | + k: v |
| 17 | + for k, v in ((k, remove_empty_elements(v)) for k, v in d.items()) |
| 18 | + if not empty(v) |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | +# Helper to convert Scenario data to MVS importable json |
| 23 | +def format_scenario_for_mvs(scenario_to_convert): |
| 24 | + mvs_request_dto = convert_to_dto(scenario_to_convert) |
| 25 | + dumped_data = json.loads( |
| 26 | + json.dumps(mvs_request_dto.__dict__, default=lambda o: o.__dict__) |
| 27 | + ) |
| 28 | + |
| 29 | + # format the constraints in MVS format directly, thus avoiding the need to maintain MVS-EPA |
| 30 | + # parser in multi-vector-simulator package |
| 31 | + constraint_dict = {} |
| 32 | + for constraint in dumped_data["constraints"]: |
| 33 | + constraint_dict[constraint["label"]] = constraint["value"] |
| 34 | + dumped_data["constraints"] = constraint_dict |
| 35 | + |
| 36 | + # Remove None values |
| 37 | + return remove_empty_elements(dumped_data) |
| 38 | + |
| 39 | + |
| 40 | +def sensitivity_analysis_payload( |
| 41 | + variable_parameter_name="", |
| 42 | + variable_parameter_range="", |
| 43 | + variable_parameter_ref_val="", |
| 44 | + output_parameter_names=None, |
| 45 | +): |
| 46 | + """format the parameters required to request a sensitivity analysis in a specific JSON""" |
| 47 | + if output_parameter_names is None: |
| 48 | + output_parameter_names = [] |
| 49 | + return { |
| 50 | + "sensitivity_analysis_settings": { |
| 51 | + "variable_parameter_name": variable_parameter_name, |
| 52 | + "variable_parameter_range": variable_parameter_range, |
| 53 | + "variable_parameter_ref_val": variable_parameter_ref_val, |
| 54 | + "output_parameter_names": output_parameter_names, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +SA_RESPONSE_SCHEMA = { |
| 60 | + "type": "object", |
| 61 | + "required": ["server_info", "mvs_version", "id", "status", "results"], |
| 62 | + "properties": { |
| 63 | + "server_info": {"type": "string"}, |
| 64 | + "mvs_version": {"type": "string"}, |
| 65 | + "id": {"type": "string"}, |
| 66 | + "status": {"type": "string"}, |
| 67 | + "results": { |
| 68 | + "type": "object", |
| 69 | + "required": ["reference_simulation_id", "sensitivity_analysis_steps"], |
| 70 | + "properties": { |
| 71 | + "reference_simulation_id": {"type": "string"}, |
| 72 | + "sensitivity_analysis_steps": { |
| 73 | + "type": "array", |
| 74 | + "items": {"type": "object"}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + "additionalProperties": False, |
| 78 | + }, |
| 79 | + "ref_sim_id": {"type": "string"}, |
| 80 | + "sensitivity_analysis_ids": {"type": "array", "items": {"type": "string"}}, |
| 81 | + }, |
| 82 | + "additionalProperties": False, |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +# Used to proof the json objects stored as text in the db |
| 87 | +SA_OUPUT_NAMES_SCHEMA = {"type": "array", "items": {"type": "string"}} |
| 88 | + |
| 89 | + |
| 90 | +def sa_output_values_schema_generator(output_names): |
| 91 | + return { |
| 92 | + "type": "object", |
| 93 | + "required": output_names, |
| 94 | + "properties": { |
| 95 | + output_name: { |
| 96 | + "type": "object", |
| 97 | + "required": ["value", "path"], |
| 98 | + "properties": { |
| 99 | + "value": { |
| 100 | + "oneOf": [ |
| 101 | + {"type": "null"}, |
| 102 | + { |
| 103 | + "type": "array", |
| 104 | + "items": { |
| 105 | + "anyOf": [{"type": "number"}, {"type": "null"}] |
| 106 | + }, |
| 107 | + }, |
| 108 | + ] |
| 109 | + }, |
| 110 | + "path": { |
| 111 | + "oneOf": [ |
| 112 | + {"type": "string"}, |
| 113 | + {"type": "array", "items": {"type": "string"}}, |
| 114 | + ] |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + for output_name in output_names |
| 119 | + }, |
| 120 | + "additionalProperties": False, |
| 121 | + } |
0 commit comments