From df2ea14fa48a5599f311c1e9c10567de114fc993 Mon Sep 17 00:00:00 2001 From: Kevin Knosala Date: Thu, 25 Jan 2024 12:37:07 +0100 Subject: [PATCH] Restructure WebtoolDict --- hisim/postprocessing/postprocessing_main.py | 21 ++-- hisim/postprocessing/webtool_entries.py | 117 +++++++++++--------- 2 files changed, 72 insertions(+), 66 deletions(-) diff --git a/hisim/postprocessing/postprocessing_main.py b/hisim/postprocessing/postprocessing_main.py index 1db212d89..f9b05c577 100644 --- a/hisim/postprocessing/postprocessing_main.py +++ b/hisim/postprocessing/postprocessing_main.py @@ -31,7 +31,7 @@ from hisim.postprocessing.report_image_entries import ReportImageEntry, SystemChartEntry from hisim.sim_repository_singleton import SingletonSimRepository, SingletonDictKeyEnum from hisim.json_generator import JsonConfigurationGenerator -from hisim.postprocessing.webtool_entries import WebtoolEntries, get_components_for_webtool +from hisim.postprocessing.webtool_entries import WebtoolDict from obsolete import loadprofilegenerator_connector @@ -945,21 +945,16 @@ def write_results_for_webtool_to_json_file(self, ppdt: PostProcessingDataTransfe simulation_parameters=ppdt.simulation_parameters, ) - # Create components - components = get_components_for_webtool( + # Consolidate results into structured dataclass for webtool + webtool_results_dataclass = WebtoolDict( components=ppdt.wrapped_components, + kpis=dict_with_important_kpi, computed_opex=opex_compute_return, computed_capex=capex_compute_return, ) - # Initialize webtool kpi entries dataclass - webtool_kpi_dataclass = WebtoolEntries( - components=components, - kpis=dict_with_important_kpi, - ) - - # Save dict as json file in results folder - json_file = webtool_kpi_dataclass.to_json(indent=4) + # Save dataclass as json file in results folder + json_file = webtool_results_dataclass.to_json(indent=4) with open( os.path.join(ppdt.simulation_parameters.result_directory, "results_for_webtool.json"), "w", @@ -969,8 +964,8 @@ def write_results_for_webtool_to_json_file(self, ppdt: PostProcessingDataTransfe else: raise ValueError( - "Some PostProcessingOptions are not set." - "Please check if PostProcessingOptions.COMPUTE_AND_WRITE_KPIS_TO_REPORT, PostProcessingOptions.COMPUTE_CAPEX," + "Some PostProcessingOptions are not set. Please check if" + "PostProcessingOptions.COMPUTE_AND_WRITE_KPIS_TO_REPORT, PostProcessingOptions.COMPUTE_CAPEX and" "PostProcessingOptions.COMPUTE_OPEX are set in your system setup." ) diff --git a/hisim/postprocessing/webtool_entries.py b/hisim/postprocessing/webtool_entries.py index 9773ed2bb..7757afeb1 100644 --- a/hisim/postprocessing/webtool_entries.py +++ b/hisim/postprocessing/webtool_entries.py @@ -8,56 +8,67 @@ @dataclass -class WebtoolEntries(JSONWizard): - - """Class for storing important kpis for hisim webtool.""" - - components: List - kpis: Dict[str, Any] = field(default_factory=dict) - - -def get_components_for_webtool(components: List[ComponentWrapper], computed_opex: List, computed_capex: List) -> List: - """Create list of components with result values.""" - this_components_dict = {} - - # Get component names and initialize dictionary. - for component in components: - this_name = component.my_component.my_display_config.pretty_name or component.my_component.component_name - this_component_class = component.my_component.get_classname() - this_components_dict[component.my_component.component_name] = { - "Name": this_name, - "Class": this_component_class, - "Sizing": {}, - "Energy": {}, - "Emissions": {}, - "Economics": {}, - } - - categories_opex = ["Economics", "Emissions", "Energy"] - categories_capex = ["Economics", "Emissions", "Economics"] - - # Get OPEX and CAPEX - for computed_values, categories in zip([computed_opex, computed_capex], [categories_opex, categories_capex]): - if not all(isinstance(_, str) for _ in computed_values[0]): - # First row is header. - raise ValueError("Expected header in first row.") - for computed_values_row in computed_values[1:]: - if not isinstance(computed_values_row[0], str): - # Fist column is component name. - raise ValueError("Expected component name in first column.") - if "total" in computed_values_row[0].lower(): - # Skip rows with total values. - continue - for idx_column, computed_values_item in enumerate(computed_values_row[1:]): - # Get component name - this_component = computed_values_row[0] - # Get value key and reformat unit into brackets - computed_values_key = computed_values[0][idx_column + 1] - computed_values_key = computed_values_key.replace(" in ", " [") - computed_values_key = computed_values_key + "]" - # Save to dict - this_components_dict[this_component][categories[idx_column]].update( - {computed_values_key: computed_values_item} - ) - - return list(this_components_dict.values()) +class WebtoolDict: + + """Class for storing results for hisim webtool.""" + + def __init__( + self, + components: List[ComponentWrapper], + kpis: Dict, + computed_opex: List, + computed_capex: List, + ): + self.components = {} + self.kpis = kpis # KPIs are used for result validation. Do not change this. + + # Add results one by one. + self.init_structure(components) + self.add_opex_capex(computed_opex, computed_capex) + self.add_sizing(components) + + def init_structure(self, components): + """Initialize results dict for webtool with component names and categories.""" + for component in components: + this_name = component.my_component.component_name + this_pretty_name = component.my_component.my_display_config.pretty_name + this_component_class = component.my_component.get_classname() + self.components[this_name] = { + "prettyName": this_pretty_name, + "class": this_component_class, + "sizing": {}, + "energy": {}, + "emissions": {}, + "economics": {}, + } + + def add_opex_capex(self, computed_opex, computed_capex): + """Add results from the results of `opex_calculation()` and `capex_calculation()` to webtool dict.""" + categories_opex = ["economics", "emissions", "energy"] + categories_capex = ["economics", "emissions", "economics"] + # Get OPEX and CAPEX + for computed_values, categories in zip([computed_opex, computed_capex], [categories_opex, categories_capex]): + if not all(isinstance(_, str) for _ in computed_values[0]): + # First row is header. + raise ValueError("Expected header in first row.") + for computed_values_row in computed_values[1:]: + if not isinstance(computed_values_row[0], str): + # Fist column is component name. + raise ValueError("Expected component name in first column.") + if "total" in computed_values_row[0].lower(): + # Skip rows with total values. + continue + for idx_column, computed_values_item in enumerate(computed_values_row[1:]): + # Get component name + this_component = computed_values_row[0] + # Get value key and reformat unit into brackets + computed_values_key = computed_values[0][idx_column + 1] + computed_values_key = computed_values_key.replace(" in ", " [") + computed_values_key = computed_values_key + "]" + # Save to dict + self.components[this_component][categories[idx_column]].update( + {computed_values_key: computed_values_item} + ) + + def add_sizing(self, components): + print("HI.")