Skip to content

Commit abec532

Browse files
committed
formatting
1 parent ac0ab2f commit abec532

20 files changed

+1417
-261
lines changed

demos/aggregator_extension.ipynb

Lines changed: 43 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -68,53 +68,30 @@
6868
"from enbios import Experiment\n",
6969
"\n",
7070
"exp_config = {\n",
71-
" \"adapters\": [\n",
72-
" {\n",
73-
" \"adapter_name\": \"assignment-adapter\",\n",
74-
" \"methods\": {\n",
75-
" \"co2\": \"kg\"\n",
76-
" }\n",
77-
" }\n",
78-
" ],\n",
71+
" \"adapters\": [{\"adapter_name\": \"assignment-adapter\", \"methods\": {\"co2\": \"kg\"}}],\n",
7972
" \"hierarchy\": {\n",
8073
" \"name\": \"root\",\n",
8174
" \"aggregator\": \"sum\",\n",
8275
" \"children\": [\n",
83-
" {\n",
84-
" \"name\": \"n1\",\n",
85-
" \"adapter\": \"assign\",\n",
86-
" \"config\": {\n",
87-
" \"outputs\": [{\"unit\": \"kg\"}]\n",
88-
" }\n",
89-
" },\n",
90-
" {\n",
91-
" \"name\": \"n2\",\n",
92-
" \"adapter\": \"assign\",\n",
93-
" \"config\": {\n",
94-
" \"outputs\": [{\"unit\": \"l\"}]\n",
95-
" }\n",
96-
" }\n",
97-
" ]\n",
76+
" {\"name\": \"n1\", \"adapter\": \"assign\", \"config\": {\"outputs\": [{\"unit\": \"kg\"}]}},\n",
77+
" {\"name\": \"n2\", \"adapter\": \"assign\", \"config\": {\"outputs\": [{\"unit\": \"l\"}]}},\n",
78+
" ],\n",
9879
" },\n",
9980
" \"scenarios\": [\n",
10081
" {\n",
10182
" \"name\": \"scenario1\",\n",
10283
" \"nodes\": {\n",
10384
" \"n1\": {\n",
10485
" \"outputs\": [{\"magnitude\": 100}],\n",
105-
" \"impacts\": {\n",
106-
" \"co2\": {\"unit\": \"kg\", \"magnitude\": 100}\n",
107-
" }\n",
86+
" \"impacts\": {\"co2\": {\"unit\": \"kg\", \"magnitude\": 100}},\n",
10887
" },\n",
10988
" \"n2\": {\n",
11089
" \"outputs\": [{\"magnitude\": 100}],\n",
111-
" \"impacts\": {\n",
112-
" \"co2\": {\"unit\": \"kg\", \"magnitude\": 100}\n",
113-
" }\n",
114-
" }\n",
115-
" }\n",
90+
" \"impacts\": {\"co2\": {\"unit\": \"kg\", \"magnitude\": 100}},\n",
91+
" },\n",
92+
" },\n",
11693
" }\n",
117-
" ]\n",
94+
" ],\n",
11895
"}\n",
11996
"\n",
12097
"Experiment(exp_config).run()"
@@ -167,10 +144,9 @@
167144
"source": [
168145
"path_string = module_path.as_posix()\n",
169146
"\n",
170-
"exp_config[\"aggregators\"] = [{\n",
171-
" \"aggregator_name\": \"sum-threshold\",\n",
172-
" \"module_path\": path_string\n",
173-
"}]\n",
147+
"exp_config[\"aggregators\"] = [\n",
148+
" {\"aggregator_name\": \"sum-threshold\", \"module_path\": path_string}\n",
149+
"]\n",
174150
"\n",
175151
"exp = Experiment(exp_config)\n",
176152
"exp.run()"
@@ -223,10 +199,7 @@
223199
"source": [
224200
"exp_config[\"hierarchy\"][\"aggregator\"] = \"threshold\"\n",
225201
"exp_config[\"hierarchy\"][\"config\"] = {\n",
226-
" \"method_thresholds\": [{\n",
227-
" \"method\": \"co2\",\n",
228-
" \"threshold\": 300\n",
229-
" }]\n",
202+
" \"method_thresholds\": [{\"method\": \"co2\", \"threshold\": 300}]\n",
230203
"}\n",
231204
"\n",
232205
"exp = Experiment(exp_config)\n",
@@ -422,7 +395,7 @@
422395
},
423396
"cell_type": "code",
424397
"source": [
425-
"#this is the whole module content:\n",
398+
"# this is the whole module content:\n",
426399
"\n",
427400
"from typing import Any\n",
428401
"\n",
@@ -442,15 +415,16 @@
442415
"\n",
443416
"\n",
444417
"class ThresholdAggregator(SumAggregator):\n",
445-
"\n",
446418
" def __init__(self):\n",
447419
" super().__init__()\n",
448420
" self.node_thresholds: dict[str, NodeThresholdConfig] = {}\n",
449421
" self.threshold_results: dict[str, dict[str, bool]] = {}\n",
450422
"\n",
451423
" def validate_node(self, node_name: str, node_config: Any):\n",
452424
" if node_config:\n",
453-
" self.node_thresholds[node_name] = NodeThresholdConfig.model_validate(node_config)\n",
425+
" self.node_thresholds[node_name] = NodeThresholdConfig.model_validate(\n",
426+
" node_config\n",
427+
" )\n",
454428
"\n",
455429
" def name(self) -> str:\n",
456430
" return \"sum-threshold-aggregator\"\n",
@@ -459,8 +433,7 @@
459433
" return \"threshold\"\n",
460434
"\n",
461435
" def aggregate_node_result(\n",
462-
" self, node: BasicTreeNode[ScenarioResultNodeData],\n",
463-
" scenario_name: str\n",
436+
" self, node: BasicTreeNode[ScenarioResultNodeData], scenario_name: str\n",
464437
" ) -> dict[str, ResultValue]:\n",
465438
" sum_ = super().aggregate_node_result(node, scenario_name)\n",
466439
" if node.name in self.node_thresholds:\n",
@@ -469,15 +442,17 @@
469442
" for method_threshold in node_thresholds.method_thresholds:\n",
470443
" if method_threshold.method in sum_:\n",
471444
" method = method_threshold.method\n",
472-
" self.threshold_results[node.name][method] = sum_[method].magnitude >= method_threshold.threshold\n",
445+
" self.threshold_results[node.name][method] = (\n",
446+
" sum_[method].magnitude >= method_threshold.threshold\n",
447+
" )\n",
473448
" return sum_\n",
474449
"\n",
475450
" def result_extras(self, node_name: str, scenario_name: str) -> dict[str, Any]:\n",
476451
" results = self.threshold_results.get(node_name, {})\n",
477452
" if results:\n",
478453
" return {\"threshold_results\": results}\n",
479454
" else:\n",
480-
" return {}\n"
455+
" return {}"
481456
],
482457
"id": "703f28d7cf71e479",
483458
"outputs": [],
@@ -509,23 +484,21 @@
509484
},
510485
"cell_type": "code",
511486
"source": [
512-
"exp_config[\"scenarios\"].append({\n",
513-
" \"name\": \"scenario2\",\n",
514-
" \"nodes\": {\n",
515-
" \"n1\": {\n",
516-
" \"outputs\": [{\"magnitude\": 190}],\n",
517-
" \"impacts\": {\n",
518-
" \"co2\": {\"unit\": \"kg\", \"magnitude\": 200}\n",
519-
" }\n",
487+
"exp_config[\"scenarios\"].append(\n",
488+
" {\n",
489+
" \"name\": \"scenario2\",\n",
490+
" \"nodes\": {\n",
491+
" \"n1\": {\n",
492+
" \"outputs\": [{\"magnitude\": 190}],\n",
493+
" \"impacts\": {\"co2\": {\"unit\": \"kg\", \"magnitude\": 200}},\n",
494+
" },\n",
495+
" \"n2\": {\n",
496+
" \"outputs\": [{\"magnitude\": 300}],\n",
497+
" \"impacts\": {\"co2\": {\"unit\": \"kg\", \"magnitude\": 500}},\n",
498+
" },\n",
520499
" },\n",
521-
" \"n2\": {\n",
522-
" \"outputs\": [{\"magnitude\": 300}],\n",
523-
" \"impacts\": {\n",
524-
" \"co2\": {\"unit\": \"kg\", \"magnitude\": 500}\n",
525-
" }\n",
526-
" }\n",
527500
" }\n",
528-
"})"
501+
")"
529502
],
530503
"id": "9aebd3bec28988ab",
531504
"outputs": [],
@@ -544,7 +517,9 @@
544517
"exp = Experiment(exp_config)\n",
545518
"exp.run()\n",
546519
"\n",
547-
"exp.results_to_csv(\"temp.csv\", flat_hierarchy=True,include_output=False, include_method_units=False)\n",
520+
"exp.results_to_csv(\n",
521+
" \"temp.csv\", flat_hierarchy=True, include_output=False, include_method_units=False\n",
522+
")\n",
548523
"\n",
549524
"pd.read_csv(\"temp.csv\").fillna(\"\")"
550525
],
@@ -688,22 +663,21 @@
688663
"source": [
689664
"exp_config[\"aggregators\"][0] = {\n",
690665
" \"aggregator_name\": \"scenario-sum-threshold\",\n",
691-
" \"module_path\": Path() / \"data/threshold_aggregator_scenarios.py\"\n",
666+
" \"module_path\": Path() / \"data/threshold_aggregator_scenarios.py\",\n",
692667
"}\n",
693668
"\n",
694669
"exp_config[\"hierarchy\"][\"aggregator\"] = \"scenario-threshold\"\n",
695670
"\n",
696671
"exp_config[\"scenarios\"][1][\"nodes\"][\"root\"] = {\n",
697-
" \"method_thresholds\": [{\n",
698-
" \"method\": \"co2\",\n",
699-
" \"threshold\": 1000\n",
700-
" }]\n",
672+
" \"method_thresholds\": [{\"method\": \"co2\", \"threshold\": 1000}]\n",
701673
"}\n",
702674
"\n",
703675
"exp = Experiment(exp_config)\n",
704676
"exp.run()\n",
705677
"\n",
706-
"exp.results_to_csv(\"temp.csv\", flat_hierarchy=True, include_output=False, include_method_units=False)\n",
678+
"exp.results_to_csv(\n",
679+
" \"temp.csv\", flat_hierarchy=True, include_output=False, include_method_units=False\n",
680+
")\n",
707681
"pd.read_csv(\"temp.csv\").fillna(\"\")"
708682
],
709683
"id": "20e917a75d19160c",

demos/data/threshold_aggregator.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ class NodeThresholdConfig(BaseModel):
1616

1717

1818
class ThresholdAggregator(SumAggregator):
19-
2019
def __init__(self):
2120
super().__init__()
2221
self.node_thresholds: dict[str, NodeThresholdConfig] = {}
2322
self.threshold_results: dict[str, dict[str, bool]] = {}
2423

2524
def validate_node(self, node_name: str, node_config: Any):
2625
if node_config:
27-
self.node_thresholds[node_name] = NodeThresholdConfig.model_validate(node_config)
26+
self.node_thresholds[node_name] = NodeThresholdConfig.model_validate(
27+
node_config
28+
)
2829

2930
def name(self) -> str:
3031
return "sum-threshold-aggregator"
@@ -33,8 +34,7 @@ def node_indicator(self) -> str:
3334
return "threshold"
3435

3536
def aggregate_node_result(
36-
self, node: BasicTreeNode[ScenarioResultNodeData],
37-
scenario_name: str
37+
self, node: BasicTreeNode[ScenarioResultNodeData], scenario_name: str
3838
) -> dict[str, ResultValue]:
3939
sum_ = super().aggregate_node_result(node, scenario_name)
4040
if node.name in self.node_thresholds:
@@ -43,11 +43,12 @@ def aggregate_node_result(
4343
for method_threshold in node_thresholds.method_thresholds:
4444
if method_threshold.method in sum_:
4545
method = method_threshold.method
46-
self.threshold_results[node.name][method] = sum_[method].magnitude >= method_threshold.threshold
46+
self.threshold_results[node.name][method] = (
47+
sum_[method].magnitude >= method_threshold.threshold
48+
)
4749
return sum_
4850

49-
def result_extras(self, node_name: str,
50-
scenario_name: str) -> dict[str, Any]:
51+
def result_extras(self, node_name: str, scenario_name: str) -> dict[str, Any]:
5152
results = self.threshold_results.get(node_name, {})
5253
if results:
5354
return {"threshold_results": results}

demos/data/threshold_aggregator_scenarios.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class NodeThresholdConfig(BaseModel):
1616

1717

1818
class ThresholdAggregator(SumAggregator):
19-
2019
def __init__(self):
2120
super().__init__()
2221
# node -> threshold
@@ -28,7 +27,9 @@ def __init__(self):
2827

2928
def validate_node(self, node_name: str, node_config: Any):
3029
if node_config:
31-
self.node_thresholds[node_name] = NodeThresholdConfig.model_validate(node_config)
30+
self.node_thresholds[node_name] = NodeThresholdConfig.model_validate(
31+
node_config
32+
)
3233

3334
def name(self) -> str:
3435
return "sum-scenario-threshold-aggregator"
@@ -37,14 +38,14 @@ def node_indicator(self) -> str:
3738
return "scenario-threshold"
3839

3940
def validate_scenario_node(
40-
self, node_name: str, scenario_name: str, scenario_node_data: Any
41+
self, node_name: str, scenario_name: str, scenario_node_data: Any
4142
):
42-
self.scenario_configs.setdefault(scenario_name, {})[node_name] = NodeThresholdConfig.model_validate(
43-
scenario_node_data)
43+
self.scenario_configs.setdefault(scenario_name, {})[
44+
node_name
45+
] = NodeThresholdConfig.model_validate(scenario_node_data)
4446

4547
def aggregate_node_result(
46-
self, node: BasicTreeNode[ScenarioResultNodeData],
47-
scenario_name: str
48+
self, node: BasicTreeNode[ScenarioResultNodeData], scenario_name: str
4849
) -> dict[str, ResultValue]:
4950
sum_ = super().aggregate_node_result(node, scenario_name)
5051

@@ -53,8 +54,9 @@ def threshold_checks(threshold_config: NodeThresholdConfig):
5354
for method_threshold in threshold_config.method_thresholds:
5455
if method_threshold.method in sum_:
5556
method = method_threshold.method
56-
self.threshold_results[node.name][method] = sum_[
57-
method].magnitude >= method_threshold.threshold
57+
self.threshold_results[node.name][method] = (
58+
sum_[method].magnitude >= method_threshold.threshold
59+
)
5860

5961
if scenario_name in self.scenario_configs:
6062
if node.name in self.scenario_configs[scenario_name]:

demos/plot_results.ipynb

Lines changed: 16 additions & 18 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)