zen-garden 2.8.4__py3-none-any.whl → 2.8.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zen_garden/postprocess/results/cache.py +52 -0
- zen_garden/postprocess/results/results.py +2 -2
- zen_garden/postprocess/results/solution_loader.py +7 -9
- zen_garden/utils.py +4 -3
- {zen_garden-2.8.4.dist-info → zen_garden-2.8.6.dist-info}/METADATA +1 -1
- {zen_garden-2.8.4.dist-info → zen_garden-2.8.6.dist-info}/RECORD +9 -8
- {zen_garden-2.8.4.dist-info → zen_garden-2.8.6.dist-info}/WHEEL +0 -0
- {zen_garden-2.8.4.dist-info → zen_garden-2.8.6.dist-info}/entry_points.txt +0 -0
- {zen_garden-2.8.4.dist-info → zen_garden-2.8.6.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ConditionalCache:
|
|
5
|
+
"""
|
|
6
|
+
Decorator to conditionally cache method results based on an instance flag.
|
|
7
|
+
If the flag is True, the method results are cached using functools.cache.
|
|
8
|
+
Otherwise, the method is called normally without caching.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
```
|
|
12
|
+
class MyClass:
|
|
13
|
+
def __init__(self, enable_cache: bool):
|
|
14
|
+
self.enable_cache = enable_cache
|
|
15
|
+
|
|
16
|
+
@ConditionalCache('enable_cache')
|
|
17
|
+
def my_method(self, ...):
|
|
18
|
+
# method implementation
|
|
19
|
+
pass
|
|
20
|
+
```
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, flag_name: str):
|
|
24
|
+
self.flag_name = flag_name
|
|
25
|
+
|
|
26
|
+
def __call__(self, func):
|
|
27
|
+
self.func = func
|
|
28
|
+
functools.update_wrapper(self, func)
|
|
29
|
+
return self
|
|
30
|
+
|
|
31
|
+
def __get__(self, instance, owner):
|
|
32
|
+
if instance is None:
|
|
33
|
+
return self
|
|
34
|
+
|
|
35
|
+
# Check if caching is enabled for this instance and get the bound method
|
|
36
|
+
enable_cache = getattr(instance, self.flag_name, False)
|
|
37
|
+
bound_func = self.func.__get__(instance, owner)
|
|
38
|
+
|
|
39
|
+
# If caching is disabled, return the original bound method
|
|
40
|
+
if not enable_cache:
|
|
41
|
+
return bound_func
|
|
42
|
+
|
|
43
|
+
# Check if the cached version already exists
|
|
44
|
+
cache_attr = f"__cached_{self.func.__name__}"
|
|
45
|
+
cached_func = getattr(instance, cache_attr, None)
|
|
46
|
+
if cached_func is not None:
|
|
47
|
+
return cached_func
|
|
48
|
+
|
|
49
|
+
# Create and store the cached version of the method
|
|
50
|
+
cached_func = functools.cache(bound_func)
|
|
51
|
+
setattr(instance, cache_attr, cached_func)
|
|
52
|
+
return cached_func
|
|
@@ -28,7 +28,7 @@ class Results:
|
|
|
28
28
|
"""
|
|
29
29
|
The Results class is used to extract and process the results of a model run.
|
|
30
30
|
"""
|
|
31
|
-
def __init__(self, path: str):
|
|
31
|
+
def __init__(self, path: str, enable_cache: bool = True):
|
|
32
32
|
"""
|
|
33
33
|
Initializes the Results class.
|
|
34
34
|
|
|
@@ -36,7 +36,7 @@ class Results:
|
|
|
36
36
|
"""
|
|
37
37
|
assert os.path.exists(path), f"The output folder {Path(path).absolute()} does not exist."
|
|
38
38
|
assert len(os.listdir(path)) > 0, f"The output folder {Path(path).absolute()} is empty."
|
|
39
|
-
self.solution_loader = SolutionLoader(path)
|
|
39
|
+
self.solution_loader = SolutionLoader(path, enable_cache=enable_cache)
|
|
40
40
|
self.has_scenarios = len(self.solution_loader.scenarios) > 1
|
|
41
41
|
first_scenario = next(iter(self.solution_loader.scenarios.values()))
|
|
42
42
|
self.name = Path(first_scenario.analysis.dataset).name
|
|
@@ -14,9 +14,9 @@ import logging
|
|
|
14
14
|
|
|
15
15
|
from typing import Optional, Any,Literal
|
|
16
16
|
from enum import Enum
|
|
17
|
-
from functools import cache
|
|
18
17
|
from zen_garden.default_config import Analysis, System, Solver
|
|
19
18
|
from zen_garden.utils import slice_df_by_index
|
|
19
|
+
from .cache import ConditionalCache
|
|
20
20
|
|
|
21
21
|
class ComponentType(Enum):
|
|
22
22
|
parameter: str = "parameter"
|
|
@@ -304,11 +304,12 @@ class SolutionLoader():
|
|
|
304
304
|
Implementation of a SolutionLoader.
|
|
305
305
|
"""
|
|
306
306
|
|
|
307
|
-
def __init__(self, path: str) -> None:
|
|
307
|
+
def __init__(self, path: str, enable_cache: bool = True) -> None:
|
|
308
308
|
self.path = path
|
|
309
309
|
assert len(os.listdir(path)) > 0, f"Path {path} is empty."
|
|
310
310
|
self._scenarios: dict[str, Scenario] = self._read_scenarios()
|
|
311
311
|
self._series_cache: dict[str, "pd.Series[Any]"] = {}
|
|
312
|
+
self.enable_cache = enable_cache
|
|
312
313
|
|
|
313
314
|
@property
|
|
314
315
|
def scenarios(self) -> dict[str, Scenario]:
|
|
@@ -395,7 +396,7 @@ class SolutionLoader():
|
|
|
395
396
|
series.index.names = new_index_names
|
|
396
397
|
return series
|
|
397
398
|
|
|
398
|
-
@
|
|
399
|
+
@ConditionalCache("enable_cache")
|
|
399
400
|
def get_component_data(
|
|
400
401
|
self,
|
|
401
402
|
scenario: Scenario,
|
|
@@ -492,7 +493,7 @@ class SolutionLoader():
|
|
|
492
493
|
|
|
493
494
|
return ans
|
|
494
495
|
|
|
495
|
-
@
|
|
496
|
+
@ConditionalCache("enable_cache")
|
|
496
497
|
def get_timestep_duration(
|
|
497
498
|
self, scenario: Scenario, component: Component
|
|
498
499
|
) -> "pd.Series[Any]":
|
|
@@ -523,9 +524,7 @@ class SolutionLoader():
|
|
|
523
524
|
|
|
524
525
|
return time_step_duration
|
|
525
526
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
@cache
|
|
527
|
+
@ConditionalCache("enable_cache")
|
|
529
528
|
def get_timesteps(
|
|
530
529
|
self, scenario: Scenario, component: Component, year: int
|
|
531
530
|
) -> "pd.Series[Any]":
|
|
@@ -556,7 +555,7 @@ class SolutionLoader():
|
|
|
556
555
|
|
|
557
556
|
return ans
|
|
558
557
|
|
|
559
|
-
@
|
|
558
|
+
@ConditionalCache("enable_cache")
|
|
560
559
|
def get_timesteps_of_years(
|
|
561
560
|
self, scenario: Scenario, ts_type: TimestepType, years: tuple
|
|
562
561
|
) -> "pd.DataFrame | pd.Series[Any]":
|
|
@@ -785,7 +784,6 @@ def get_has_units(h5_file: h5py.File,component_name: str,version: str) -> bool:
|
|
|
785
784
|
raise ValueError(f"Value {has_units} for has_units not supported.")
|
|
786
785
|
return has_units
|
|
787
786
|
|
|
788
|
-
@cache
|
|
789
787
|
def get_df_from_path(path: str, component_name: str, version: str, data_type: Literal["dataframe","units"] = "dataframe",index: Optional[tuple[str]] = None) -> "pd.Series[Any]":
|
|
790
788
|
"""
|
|
791
789
|
Helper-function that returns a Pandas series given the path of a file and the
|
zen_garden/utils.py
CHANGED
|
@@ -21,6 +21,7 @@ import requests
|
|
|
21
21
|
from importlib.metadata import metadata
|
|
22
22
|
import zipfile
|
|
23
23
|
import io
|
|
24
|
+
import pandas as pd
|
|
24
25
|
|
|
25
26
|
def setup_logger(level=logging.INFO):
|
|
26
27
|
""" set up logger
|
|
@@ -350,10 +351,10 @@ def slice_df_by_index(df,index_tuple) -> dict:
|
|
|
350
351
|
if key in df.index.names:
|
|
351
352
|
if isinstance(index[key], list):
|
|
352
353
|
df = df.loc[df.index.get_level_values(key).isin(index[key])]
|
|
353
|
-
|
|
354
|
-
if index[key] not in df.index.get_level_values(key):
|
|
355
|
-
df = pd.DataFrame(columns=df.columns) # return empty dataframe if value not in index
|
|
354
|
+
elif index[key] in df.index.get_level_values(key):
|
|
356
355
|
df = df.xs(index[key], level=key, drop_level=False)
|
|
356
|
+
else:
|
|
357
|
+
df = pd.DataFrame(columns=df.columns) # return empty dataframe if value not in index
|
|
357
358
|
return df
|
|
358
359
|
|
|
359
360
|
def get_label_position(obj,label:int):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: zen_garden
|
|
3
|
-
Version: 2.8.
|
|
3
|
+
Version: 2.8.6
|
|
4
4
|
Summary: ZEN-garden is an optimization model of energy systems and value chains.
|
|
5
5
|
Author: Alissa Ganter, Johannes Burger, Francesco De Marco, Lukas Kunz, Lukas Schmidt-Engelbertz, Christoph Funke, Paolo Gabrielli, Giovanni Sansavini
|
|
6
6
|
Author-email: Jacob Mannhardt <zen-garden@ethz.ch>
|
|
@@ -3,7 +3,7 @@ zen_garden/__main__.py,sha256=Zrz6zr81gXN3_NLPwWz3a-8HiiRgqGx_OzQHarBcxtQ,2508
|
|
|
3
3
|
zen_garden/default_config.py,sha256=ozz2LBSGSaMH8alEAzh-6ziM9IE9zXM9PfRD3nulAIs,8663
|
|
4
4
|
zen_garden/optimization_setup.py,sha256=Rv_IciR_Y9GF-OaPDDAvTQ2huTuAt94jrWpcDJCQgWQ,42176
|
|
5
5
|
zen_garden/runner.py,sha256=gLjHh3DZbgUAiz5lsnDiSwAZT5RFw52-Pnr1K_nPaMM,7207
|
|
6
|
-
zen_garden/utils.py,sha256=
|
|
6
|
+
zen_garden/utils.py,sha256=GFR-LBIkMA7pDQ46J07mr0ujqyIfNPlI0UVnvBDfHRw,60529
|
|
7
7
|
zen_garden/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
8
|
zen_garden/cli/zen_example_cli.py,sha256=hCYlAxM5BdphMzXA5LyHmTN3UiYIhMlG7G9sxYi0G_s,1411
|
|
9
9
|
zen_garden/cli/zen_garden_cli.py,sha256=ikiJZoIP4_D-eoNuo4m2f2Z4ydU0sHLDUpCEWn93nnU,6505
|
|
@@ -26,15 +26,16 @@ zen_garden/postprocess/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
|
|
|
26
26
|
zen_garden/postprocess/comparisons.py,sha256=uyEtV0Q8_YPzJi4b12DWiqYU-kKJz6I3Li-I7_6RURY,13239
|
|
27
27
|
zen_garden/postprocess/postprocess.py,sha256=0_1iy028iZmvf2egXUFO2ZS13HmkacMt7A5eTI0iCE4,29644
|
|
28
28
|
zen_garden/postprocess/results/__init__.py,sha256=363lzC0uRJ5F42cOEhD9jxMPCioTt8WRt0qwZG0IizY,49
|
|
29
|
-
zen_garden/postprocess/results/
|
|
30
|
-
zen_garden/postprocess/results/
|
|
29
|
+
zen_garden/postprocess/results/cache.py,sha256=t-1P7k9EVkSxR1oB1NIodux3JbfhBhxxwgRytcQ8vjQ,1592
|
|
30
|
+
zen_garden/postprocess/results/results.py,sha256=S21h0Unf4l3a8WD0UFvn8IeMgDxKitZRaiX7iLvJnL0,41012
|
|
31
|
+
zen_garden/postprocess/results/solution_loader.py,sha256=n3n_n-6hmTjgW6LGSQo8gyI45xp8kn5n1r0Tx5IxCNw,31427
|
|
31
32
|
zen_garden/preprocess/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
32
33
|
zen_garden/preprocess/extract_input_data.py,sha256=PWKdel7s488SELseyAaoFkwXpzJU4MZg0pW0wO2ZHoI,51523
|
|
33
34
|
zen_garden/preprocess/parameter_change_log.py,sha256=WNhLYTyuaFkUl_e4QH36W1chpqg00m7zg__PawPogAY,387
|
|
34
35
|
zen_garden/preprocess/time_series_aggregation.py,sha256=v8xRRk4I9WZ2yi7V3I_Ersj2RV1T2WIKslx8ohkMfWI,32112
|
|
35
36
|
zen_garden/preprocess/unit_handling.py,sha256=y78oxoPVqcA3y9aEiyr0MU5zITf2BPNGVKOfyMhhCvY,63040
|
|
36
|
-
zen_garden-2.8.
|
|
37
|
-
zen_garden-2.8.
|
|
38
|
-
zen_garden-2.8.
|
|
39
|
-
zen_garden-2.8.
|
|
40
|
-
zen_garden-2.8.
|
|
37
|
+
zen_garden-2.8.6.dist-info/entry_points.txt,sha256=FkXXB0MgvdZG_p_zPifgXc0G2GSPJi0NncG3hZ8TXkU,232
|
|
38
|
+
zen_garden-2.8.6.dist-info/licenses/LICENSE.txt,sha256=_kEtxPe9gWOwMzdiy8nLzgABiPdMvUS0kaSCOIrEA_E,1101
|
|
39
|
+
zen_garden-2.8.6.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
40
|
+
zen_garden-2.8.6.dist-info/METADATA,sha256=ukkRuihxJjio9vja2mjtSF96nf-9qRKjIjSvAmzEQxE,5648
|
|
41
|
+
zen_garden-2.8.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|