|
68 | 68 | "from enbios import Experiment\n",
|
69 | 69 | "\n",
|
70 | 70 | "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", |
79 | 72 | " \"hierarchy\": {\n",
|
80 | 73 | " \"name\": \"root\",\n",
|
81 | 74 | " \"aggregator\": \"sum\",\n",
|
82 | 75 | " \"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", |
98 | 79 | " },\n",
|
99 | 80 | " \"scenarios\": [\n",
|
100 | 81 | " {\n",
|
101 | 82 | " \"name\": \"scenario1\",\n",
|
102 | 83 | " \"nodes\": {\n",
|
103 | 84 | " \"n1\": {\n",
|
104 | 85 | " \"outputs\": [{\"magnitude\": 100}],\n",
|
105 |
| - " \"impacts\": {\n", |
106 |
| - " \"co2\": {\"unit\": \"kg\", \"magnitude\": 100}\n", |
107 |
| - " }\n", |
| 86 | + " \"impacts\": {\"co2\": {\"unit\": \"kg\", \"magnitude\": 100}},\n", |
108 | 87 | " },\n",
|
109 | 88 | " \"n2\": {\n",
|
110 | 89 | " \"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", |
116 | 93 | " }\n",
|
117 |
| - " ]\n", |
| 94 | + " ],\n", |
118 | 95 | "}\n",
|
119 | 96 | "\n",
|
120 | 97 | "Experiment(exp_config).run()"
|
|
167 | 144 | "source": [
|
168 | 145 | "path_string = module_path.as_posix()\n",
|
169 | 146 | "\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", |
174 | 150 | "\n",
|
175 | 151 | "exp = Experiment(exp_config)\n",
|
176 | 152 | "exp.run()"
|
|
223 | 199 | "source": [
|
224 | 200 | "exp_config[\"hierarchy\"][\"aggregator\"] = \"threshold\"\n",
|
225 | 201 | "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", |
230 | 203 | "}\n",
|
231 | 204 | "\n",
|
232 | 205 | "exp = Experiment(exp_config)\n",
|
|
422 | 395 | },
|
423 | 396 | "cell_type": "code",
|
424 | 397 | "source": [
|
425 |
| - "#this is the whole module content:\n", |
| 398 | + "# this is the whole module content:\n", |
426 | 399 | "\n",
|
427 | 400 | "from typing import Any\n",
|
428 | 401 | "\n",
|
|
442 | 415 | "\n",
|
443 | 416 | "\n",
|
444 | 417 | "class ThresholdAggregator(SumAggregator):\n",
|
445 |
| - "\n", |
446 | 418 | " def __init__(self):\n",
|
447 | 419 | " super().__init__()\n",
|
448 | 420 | " self.node_thresholds: dict[str, NodeThresholdConfig] = {}\n",
|
449 | 421 | " self.threshold_results: dict[str, dict[str, bool]] = {}\n",
|
450 | 422 | "\n",
|
451 | 423 | " def validate_node(self, node_name: str, node_config: Any):\n",
|
452 | 424 | " 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", |
454 | 428 | "\n",
|
455 | 429 | " def name(self) -> str:\n",
|
456 | 430 | " return \"sum-threshold-aggregator\"\n",
|
|
459 | 433 | " return \"threshold\"\n",
|
460 | 434 | "\n",
|
461 | 435 | " 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", |
464 | 437 | " ) -> dict[str, ResultValue]:\n",
|
465 | 438 | " sum_ = super().aggregate_node_result(node, scenario_name)\n",
|
466 | 439 | " if node.name in self.node_thresholds:\n",
|
|
469 | 442 | " for method_threshold in node_thresholds.method_thresholds:\n",
|
470 | 443 | " if method_threshold.method in sum_:\n",
|
471 | 444 | " 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", |
473 | 448 | " return sum_\n",
|
474 | 449 | "\n",
|
475 | 450 | " def result_extras(self, node_name: str, scenario_name: str) -> dict[str, Any]:\n",
|
476 | 451 | " results = self.threshold_results.get(node_name, {})\n",
|
477 | 452 | " if results:\n",
|
478 | 453 | " return {\"threshold_results\": results}\n",
|
479 | 454 | " else:\n",
|
480 |
| - " return {}\n" |
| 455 | + " return {}" |
481 | 456 | ],
|
482 | 457 | "id": "703f28d7cf71e479",
|
483 | 458 | "outputs": [],
|
|
509 | 484 | },
|
510 | 485 | "cell_type": "code",
|
511 | 486 | "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", |
520 | 499 | " },\n",
|
521 |
| - " \"n2\": {\n", |
522 |
| - " \"outputs\": [{\"magnitude\": 300}],\n", |
523 |
| - " \"impacts\": {\n", |
524 |
| - " \"co2\": {\"unit\": \"kg\", \"magnitude\": 500}\n", |
525 |
| - " }\n", |
526 |
| - " }\n", |
527 | 500 | " }\n",
|
528 |
| - "})" |
| 501 | + ")" |
529 | 502 | ],
|
530 | 503 | "id": "9aebd3bec28988ab",
|
531 | 504 | "outputs": [],
|
|
544 | 517 | "exp = Experiment(exp_config)\n",
|
545 | 518 | "exp.run()\n",
|
546 | 519 | "\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", |
548 | 523 | "\n",
|
549 | 524 | "pd.read_csv(\"temp.csv\").fillna(\"\")"
|
550 | 525 | ],
|
|
688 | 663 | "source": [
|
689 | 664 | "exp_config[\"aggregators\"][0] = {\n",
|
690 | 665 | " \"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", |
692 | 667 | "}\n",
|
693 | 668 | "\n",
|
694 | 669 | "exp_config[\"hierarchy\"][\"aggregator\"] = \"scenario-threshold\"\n",
|
695 | 670 | "\n",
|
696 | 671 | "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", |
701 | 673 | "}\n",
|
702 | 674 | "\n",
|
703 | 675 | "exp = Experiment(exp_config)\n",
|
704 | 676 | "exp.run()\n",
|
705 | 677 | "\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", |
707 | 681 | "pd.read_csv(\"temp.csv\").fillna(\"\")"
|
708 | 682 | ],
|
709 | 683 | "id": "20e917a75d19160c",
|
|
0 commit comments