-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_helpers.py
More file actions
358 lines (302 loc) · 10.8 KB
/
_helpers.py
File metadata and controls
358 lines (302 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# SPDX-FileCopyrightText: Contributors to technology-data <https://github.com/pypsa/technology-data>
#
# SPDX-License-Identifier: GPL-3.0-only
# coding: utf-8
import logging
import os
import re
import sys
from pathlib import Path
import numpy as np
import pandas as pd
class Dict(dict):
"""
Dict is a subclass of dict, which allows you to get AND SET items in the
dict using the attribute syntax!
Stripped down from addict https://github.com/mewwts/addict/ used in from pypsa.descriptor import Dict.
"""
def __setattr__(self, name, value):
"""
Setattr is called when the syntax a.b = 2 is used to set a value.
"""
if hasattr(Dict, name):
raise AttributeError(f"'Dict' object attribute '{name}' is read-only")
self[name] = value
def __getattr__(self, item):
try:
return self.__getitem__(item)
except KeyError as e:
raise AttributeError(e.args[0])
def __delattr__(self, name):
"""
Is invoked when del some_addict.b is called.
"""
del self[name]
_re_pattern = re.compile("[a-zA-Z_][a-zA-Z0-9_]*")
def __dir__(self):
"""
Return a list of object attributes.
This includes key names of any dict entries, filtered to the
subset of valid attribute names (e.g. alphanumeric strings
beginning with a letter or underscore). Also includes
attributes of parent dict class.
"""
dict_keys = []
for k in self.keys():
if isinstance(k, str):
m = self._re_pattern.match(k)
if m:
dict_keys.append(m.string)
obj_attrs = list(dir(Dict))
return dict_keys + obj_attrs
def get_relative_fn(fn):
if isinstance(fn, str):
fn = Path(fn).resolve()
return fn.relative_to(os.path.commonpath([fn, os.getcwd()]))
def mock_snakemake(
rulename,
root_dir=None,
configfiles=None,
submodule_dir="workflow/submodules/pypsa-eur",
**wildcards,
):
"""
This function is expected to be executed from the 'scripts'-directory of '
the snakemake project. It returns a snakemake.script.Snakemake object,
based on the Snakefile.
If a rule has wildcards, you have to specify them in **wildcards.
Parameters
----------
rulename: str
name of the rule for which the snakemake object should be generated
root_dir: str/path-like
path to the root directory of the snakemake project
configfiles: list, str
list of configfiles to be used to update the config
submodule_dir: str, Path
in case PyPSA-Eur is used as a submodule, submodule_dir is
the path of pypsa-eur relative to the project directory.
**wildcards:
keyword arguments fixing the wildcards. Only necessary if wildcards are
needed.
"""
import os
import snakemake as sm
from snakemake.api import Workflow
from snakemake.common import SNAKEFILE_CHOICES
from snakemake.script import Snakemake
from snakemake.settings.types import (
ConfigSettings,
DAGSettings,
ResourceSettings,
StorageSettings,
WorkflowSettings,
)
script_dir = Path(__file__).parent.resolve()
if root_dir is None:
root_dir = script_dir.parent
else:
root_dir = Path(root_dir).resolve()
user_in_script_dir = Path.cwd().resolve() == script_dir
if str(submodule_dir) in __file__:
# the submodule_dir path is only need to locate the project dir
os.chdir(Path(__file__[: __file__.find(str(submodule_dir))]))
elif user_in_script_dir:
os.chdir(root_dir)
elif Path.cwd().resolve() != root_dir:
raise RuntimeError(
"mock_snakemake has to be run from the repository root"
f" {root_dir} or scripts directory {script_dir}"
)
try:
for p in SNAKEFILE_CHOICES:
if os.path.exists(p):
snakefile = p
break
if configfiles is None:
configfiles = []
elif isinstance(configfiles, str):
configfiles = [configfiles]
resource_settings = ResourceSettings()
config_settings = ConfigSettings(configfiles=map(Path, configfiles))
workflow_settings = WorkflowSettings()
storage_settings = StorageSettings()
dag_settings = DAGSettings(rerun_triggers=[])
workflow = Workflow(
config_settings,
resource_settings,
workflow_settings,
storage_settings,
dag_settings,
storage_provider_settings=dict(),
)
workflow.include(snakefile)
if configfiles:
for f in configfiles:
if not os.path.exists(f):
raise FileNotFoundError(f"Config file {f} does not exist.")
workflow.configfile(f)
workflow.global_resources = {}
rule = workflow.get_rule(rulename)
dag = sm.dag.DAG(workflow, rules=[rule])
wc = Dict(wildcards)
job = sm.jobs.Job(rule, dag, wc)
def make_accessable(*ios):
for io in ios:
for i, _ in enumerate(io):
io[i] = os.path.abspath(io[i])
make_accessable(job.input, job.output, job.log)
snakemake = Snakemake(
job.input,
job.output,
job.params,
job.wildcards,
job.threads,
job.resources,
job.log,
job.dag.workflow.config,
job.rule.name,
None,
)
# create log and output dir if not existent
for path in list(snakemake.log) + list(snakemake.output):
Path(path).parent.mkdir(parents=True, exist_ok=True)
finally:
if user_in_script_dir:
os.chdir(script_dir)
return snakemake
def prepare_inflation_rate(fn: str, currency_to_use: str = "eur") -> pd.Series:
"""
The function reads-in annual the inflation rates from Eurostat
https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/dataflow/ESTAT/prc_hicp_aind/1.0?references=descendants&detail=referencepartial&format=sdmx_2.1_generic&compressed=true
Parameters
----------
fn: str
file name for the Eurostat inflation rates
currency_to_use: str
currency to select for the inflation rate
Returns
-------
pandas.Series
inflation rates series
"""
if currency_to_use.casefold() == "usd":
row_to_use = "United States"
else:
row_to_use = "European Economic Area (EEA18-1995, EEA28-2004, EEA30-2007, EEA31-2013, EEA30-2020)"
df = (
pd.read_excel(
fn,
sheet_name="Sheet 1",
index_col=0,
na_values=[":", "d"],
header=8,
engine="calamine",
)
.loc[row_to_use]
.dropna()
)
df.index = df.index.astype(int)
df = df.astype(float)
df /= 100.0
return df
def adjust_for_inflation(
inflation_rate: pd.Series,
costs: pd.DataFrame,
techs: pd.Series,
eur_year: int,
col_name: str,
usa_costs_flag: bool = False,
) -> pd.DataFrame:
"""
The function adjust the investment costs for the specified techs for inflation.
Parameters
----------
inflation_rate : pandas.Series
inflation rates for several years
costs : pd.DataFrame
existing cost dataframe
techs : pd.Series
technologies
eur_year : int,
reference year for which the costs are provided and based on which the inflation adjustment is done
col_name : str
column name to which to apply the inflation rate adjustment
usa_costs_flag: bool
flag for US specific costs
Returns
-------
pandas.Dataframe
inflation updated cost dataframe
"""
def get_factor(inflation_rate_df, ref_year, eur_year_val):
if (pd.isna(ref_year)) or (ref_year < 1900):
return np.nan
if ref_year == eur_year_val:
return 1
mean = inflation_rate_df.mean()
if ref_year < eur_year_val:
new_index = np.arange(ref_year + 1, eur_year_val + 1)
df = 1 + inflation_rate_df.reindex(new_index).fillna(mean)
return df.cumprod().loc[eur_year_val]
else:
new_index = np.arange(eur_year_val + 1, ref_year + 1)
df = 1 + inflation_rate_df.reindex(new_index).fillna(mean)
return 1 / df.cumprod().loc[ref_year]
inflation = costs.currency_year.apply(
lambda x: get_factor(inflation_rate, x, eur_year)
)
paras = ["investment", "VOM", "fuel"]
if usa_costs_flag:
filter_i = costs.technology.isin(techs) & costs.parameter.isin(paras)
else:
filter_i = costs.index.get_level_values(0).isin(
techs
) & costs.index.get_level_values(1).isin(paras)
costs.loc[filter_i, col_name] = costs.loc[filter_i, col_name].mul(
inflation.loc[filter_i], axis=0
)
return costs
def configure_logging(snakemake, skip_handlers=False):
"""
Configure the basic behaviour for the logging module.
Note: Must only be called once from the __main__ section of a script.
The setup includes printing log messages to STDERR and to a log file defined
by either (in priority order): snakemake.log.python, snakemake.log[0] or "logs/{rulename}.log".
Additional keywords from logging.basicConfig are accepted via the snakemake configuration
file under snakemake.config.logging.
Parameters
----------
snakemake : snakemake object
Your snakemake object containing a snakemake.config and snakemake.log.
skip_handlers : True | False (default)
Do (not) skip the default handlers created for redirecting output to STDERR and file.
"""
kwargs = snakemake.config.get("logging", dict()).copy()
kwargs.setdefault("level", "INFO")
if skip_handlers is False:
fallback_path = Path(__file__).parent.joinpath(
"..", "logs", f"{snakemake.rule}.log"
)
logfile = snakemake.log.get(
"python", snakemake.log[0] if snakemake.log else fallback_path
)
kwargs.update(
{
"handlers": [
# Prefer the 'python' log, otherwise take the first log for each
# Snakemake rule
logging.FileHandler(logfile),
logging.StreamHandler(),
]
}
)
logging.basicConfig(**kwargs)
# Setup a function to handle uncaught exceptions and include them with their stacktrace into logfiles
def handle_exception(exc_type, exc_value, exc_traceback):
# Log the exception
logger = logging.getLogger()
logger.error(
"Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)
)
sys.excepthook = handle_exception