|
| 1 | +import operator |
| 2 | +from functools import reduce |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | + |
| 7 | +def cast_by_name(type_name, value): |
| 8 | + """Cast a string read in from an input file as a data type also given as a string. |
| 9 | + Currently allowed data types: int, float, bool, str |
| 10 | +
|
| 11 | + Args: |
| 12 | + type_name (str): The data type to cast into, as a string. |
| 13 | + value (str): The value, as a string. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + The value in the specified data type |
| 17 | + """ |
| 18 | + |
| 19 | + bool_map = {"true": True, "false": False, "yes": True, "no": False, "1": True, "0": False} |
| 20 | + |
| 21 | + trusted_types = ["int", "float", "bool", "str"] ## others as needed |
| 22 | + if type_name in trusted_types: |
| 23 | + if type_name == "bool": |
| 24 | + return bool_map.get(value.lower()) |
| 25 | + else: |
| 26 | + return __builtins__[type_name](value) |
| 27 | + else: |
| 28 | + msg = f"Specified data type {type_name} invalid, must be one of {trusted_types}" |
| 29 | + raise TypeError(msg) |
| 30 | + |
| 31 | + |
| 32 | +def get_from_dict(dataDict, mapList): |
| 33 | + """Get value from nested dictionary using a list of keys. |
| 34 | +
|
| 35 | + Allows for programmatic calling of items in a nested dict using a variable-length list. |
| 36 | + Instead of dataDict[item1][item2][item3][item4][item5], you can use |
| 37 | + get_from_dict(dataDict, [item1, item2, item3, item4, item5]). |
| 38 | +
|
| 39 | + Args: |
| 40 | + dataDict (dict): The nested dictionary to access. |
| 41 | + mapList (list): List of keys to traverse the nested dictionary. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + The value at the specified nested location in the dictionary. |
| 45 | +
|
| 46 | + Example: |
| 47 | + >>> data = {"a": {"b": {"c": 42}}} |
| 48 | + >>> get_from_dict(data, ["a", "b", "c"]) |
| 49 | + 42 |
| 50 | + """ |
| 51 | + return reduce(operator.getitem, mapList, dataDict) |
| 52 | + |
| 53 | + |
| 54 | +def set_in_dict(dataDict, mapList, value): |
| 55 | + """Set value in nested dictionary using a list of keys. |
| 56 | +
|
| 57 | + Allows for programmatic setting of items in a nested dict using a variable-length list. |
| 58 | + Instead of dataDict[item1][item2][item3][item4][item5] = value, you can use |
| 59 | + set_in_dict(dataDict, [item1, item2, item3, item4, item5], value). |
| 60 | +
|
| 61 | + Args: |
| 62 | + dataDict (dict): The nested dictionary to modify. |
| 63 | + mapList (list): List of keys to traverse the nested dictionary. |
| 64 | + value: The value to set at the specified nested location. |
| 65 | +
|
| 66 | + Example: |
| 67 | + >>> data = {"a": {"b": {}}} |
| 68 | + >>> set_in_dict(data, ["a", "b", "c"], 42) |
| 69 | + >>> data["a"]["b"]["c"] |
| 70 | + 42 |
| 71 | + """ |
| 72 | + get_from_dict(dataDict, mapList[:-1])[mapList[-1]] = value |
| 73 | + |
| 74 | + |
| 75 | +def load_tech_config_cases(case_file): |
| 76 | + """Load extensive lists of values from a spreadsheet to run many different cases. |
| 77 | +
|
| 78 | + Loads tech_config values from a CSV file to run multiple cases with different |
| 79 | + technology configuration values. |
| 80 | +
|
| 81 | + Args: |
| 82 | + case_file (Path): Path to the .csv file where the different tech_config values |
| 83 | + are listed. The CSV must be formatted with "Index 1", "Index 2", etc. |
| 84 | + columns followed by case name columns. Each row should have "technologies" |
| 85 | + as the first index value, followed by tech_name and parameter names. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + pd.DataFrame: DataFrame with the indexes of the tech_config as a MultiIndex |
| 89 | + and the different case names as the column names. |
| 90 | +
|
| 91 | + Note: |
| 92 | + The CSV format should be: |
| 93 | + | "Index 1" |...| "Index <N>" | "Type" | <Case 1 Name> |...| <Case N Name> | |
| 94 | + | "technologies" |...| <param_1_name> | "float" | <Case 1 value> |...| <Case N value> | |
| 95 | + | "technologies" |...| <param_2_name> | "str" | <Case 1 value> |...| <Case N value> | |
| 96 | +
|
| 97 | + If some parameters are nested deeper than others, make as many Index columns for the deepest- |
| 98 | + nested parameters and leave any unused Indexes blank. |
| 99 | +
|
| 100 | + See example .csv in h2integrate/tools/test/test_inputs.csv |
| 101 | + """ |
| 102 | + tech_config_cases = pd.read_csv(case_file) |
| 103 | + column_names = tech_config_cases.columns.values |
| 104 | + index_names = list(filter(lambda x: "Index" in x, column_names)) |
| 105 | + index_names.append("Type") |
| 106 | + tech_config_cases = tech_config_cases.set_index(index_names) |
| 107 | + |
| 108 | + return tech_config_cases |
| 109 | + |
| 110 | + |
| 111 | +def modify_tech_config(h2i_model, tech_config_case): |
| 112 | + """Modify particular tech_config values on an existing H2I model before it is run. |
| 113 | +
|
| 114 | + Args: |
| 115 | + h2i_model: H2IntegrateModel that has been set up but not run. |
| 116 | + tech_config_case (pd.Series): Series that was indexed from tech_config_cases |
| 117 | + DataFrame containing the parameter values to modify. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + H2IntegrateModel: The H2IntegrateModel with modified tech_config values. |
| 121 | + """ |
| 122 | + for index_tup, value in tech_config_case.items(): |
| 123 | + index_list = list(index_tup) |
| 124 | + data_type = index_list[-1] |
| 125 | + index_list = index_list[:-1] |
| 126 | + # Remove nans from blank index fields |
| 127 | + while type(index_list[-1]) is not str: |
| 128 | + index_list = index_list[:-1] |
| 129 | + set_in_dict(h2i_model.technology_config, index_list, cast_by_name(data_type, value)) |
| 130 | + |
| 131 | + return h2i_model |
0 commit comments