-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.py
485 lines (311 loc) · 11.6 KB
/
validate.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import datetime
import pathlib
import re
import gurobipy as gp
import numpy as np
import pandas as pd
import shapely
import chart
def is_market_node(value, *, required=True):
if value is None:
return not required
return bool(re.search("^[A-Z]{2}[0-9a-zA-Z]{2}$", value))
def is_market_node_list(value, *, required=True):
if value is None:
return not required
if not isinstance(value, list):
return False
return all(is_market_node(x) for x in value)
def is_bool(value, *, required=True):
if value is None:
return not required
return isinstance(value, bool)
def is_market_node_dict(value, *, required=True):
if value is None:
return not required
if not isinstance(value, (dict, gp.tupledict)):
return False
return all(is_market_node(x) for x in value.keys())
def is_breakdown_level(value, *, required=True):
if value is None:
return not required
return is_integer(value, min_value=0, max_value=2)
def is_chart(value, *, required=True):
if value is None:
return not required
return isinstance(value, chart.Chart)
def is_color(value, *, required=True):
if value is None:
return not required
return bool(re.search("^#([A-F0-9]{6}|[A-F0-9]{8})$", value))
def is_color_format(value, *, required=True):
if value is None:
return not required
return value in ["hex", "rgb", "rgba"]
def is_color_name(value, *, required=True):
if value is None:
return not required
# Don't use utils.read_csv because it might create a circular import
colors = ["slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", "indigo", "violet", "purple", "fuchsia", "pink", "rose"]
return value in colors
def is_color_value(value, *, required=True):
if value is None:
return not required
return value in [50, 100, 200, 300, 400, 500, 600, 700, 800, 900]
def is_config(value, *, required=True):
if value is None:
return not required
if not isinstance(value, dict):
return False
if not is_string(value.get("name")):
return False
if not is_scenario(value.get("scenario")):
return False
if not is_country_code_list(value.get("country_codes"), code_type="nuts2"):
return False
if len(value.get("country_codes")) == 0:
return False
if not is_dict(value.get("climate_years")):
return False
if not is_integer(value["climate_years"].get("start")):
return False
if not is_integer(value["climate_years"].get("end")):
return False
if value["climate_years"]["start"] > value["climate_years"]["end"]:
return False
if not is_resolution(value.get("resolution")):
return False
if len(value.get("technologies").get("ires")) == 0:
return False
if len(value.get("technologies").get("storage")) == 0:
return False
if not value.get("optimization"):
return False
if not is_integer(value["optimization"].get("method"), min_value=-1, max_value=6):
return False
if not is_integer(value["optimization"].get("thread_count"), min_value=1):
return False
return True
def is_country_code(value, *, required=True, code_type):
if value is None:
return not required
if code_type == "nuts2":
return bool(re.search("^[A-Z]{2}$", value))
if code_type == "alpha3":
return bool(re.search("^[A-Z]{3}$", value))
return False
def is_country_code_list(value, *, required=True, code_type):
if value is None:
return not required
if not is_list_like(value):
return False
return all(is_country_code(code, code_type=code_type) for code in value)
def is_country_code_type(value, *, required=True):
if value is None:
return not required
return value == "nuts2" or value == "alpha3"
def is_country_obj(value, *, required=True):
if value is None:
return not required
if not isinstance(value, dict):
return False
return bool(value["name"] and value["market_nodes"])
def is_country_obj_list(value, *, required=True):
if value is None:
return not required
if not is_list_like(value) or len(value) == 0:
return False
return all(is_country_obj(x) for x in value)
def is_dataframe(value, *, required=True, column_validator=None):
if value is None:
return not required
if not isinstance(value, pd.DataFrame):
return False
if column_validator:
return all(column_validator(column_name) for column_name in value.columns)
return True
def is_dataframe_dict(value, *, required=True):
if value is None:
return not required
if not isinstance(value, dict):
return False
return all(is_dataframe(value[x]) for x in value)
def is_date(value, *, required=True):
if value is None:
return not required
return isinstance(value, datetime.date)
def is_datetime(value, *, required=True):
if value is None:
return not required
return isinstance(value, datetime.datetime)
def is_datetime_index(value, *, required=True):
if value is None:
return not required
return isinstance(value, pd.core.indexes.datetimes.DatetimeIndex)
def is_dict(value, *, required=True):
if value is None:
return not required
return isinstance(value, dict)
def is_dict_or_list(value, *, required=True):
if value is None:
return not required
return isinstance(value, (list, dict))
def is_directory_path(value, *, required=True, existing=None):
if value is None:
return not required
if not isinstance(value, pathlib.Path):
return False
if existing is False:
return not value.exists()
if existing is True:
return value.is_dir()
return True
def is_aggregation_level(value, *, required=True):
if value is None:
return not required
return value in ["all", "country"]
def is_filepath(value, *, required=True, suffix=None, existing=None):
if value is None:
return not required
if not isinstance(value, pathlib.Path):
return False
if suffix and value.suffix != suffix:
return False
if existing is False:
return not value.exists()
if existing is True:
return value.is_file()
return True
def is_filepath_list(value, *, required=True, suffix=None):
if value is None:
return not required
if not is_list_like(value):
return False
return all(is_filepath(filepath, suffix=suffix) for filepath in value)
def is_float(value, *, required=True, min_value=None, max_value=None):
if value is None:
return not required
if not isinstance(value, (float, np.float64)):
return False
if min_value is not None and value < min_value:
return False
if max_value is not None and value > max_value:
return False
return True
def is_func(value, *, required=True):
if value is None:
return not required
return callable(value)
def is_gurobi_variable(value, *, required=True):
if value is None:
return not required
return isinstance(value, (gp.Var, gp.LinExpr, gp.QuadExpr))
def is_gurobi_variable_tupledict(value, *, required=True):
if value is None:
return not required
if not isinstance(value, gp.tupledict):
return False
return all(is_gurobi_variable(x) for x in value.values())
def is_interconnection_tuple(value, *, required=True):
if value is None:
return not required
if not isinstance(value, tuple) or len(value) != 2:
return False
return is_market_node(value[0]) and (is_market_node(value[1]) or bool(re.search("^(gross|net)_(ex|im)port_limit$", value[1])))
def is_interconnection_type(value, *, required=True):
if value is None:
return not required
return value in ["hvac", "hvdc", "limits"]
def is_interconnection_direction(value, *, required=True):
if value is None:
return not required
return value in ["import", "export"]
def is_integer(value, *, required=True, min_value=None, max_value=None):
if value is None:
return not required
if not isinstance(value, (int, np.int64)):
return False
if min_value is not None and value < min_value:
return False
if max_value is not None and value > max_value:
return False
return True
def is_list_like(value, *, required=True):
if value is None:
return not required
return pd.api.types.is_list_like(value)
def is_model(value, *, required=True):
if value is None:
return not required
return isinstance(value, gp.Model)
def is_number(value, *, required=True, min_value=None, max_value=None):
if value is None:
return not required
return is_float(value, min_value=min_value, max_value=max_value) or is_integer(value, min_value=min_value, max_value=max_value)
def is_point(value, *, required=True):
if value is None:
return not required
return isinstance(value, shapely.geometry.point.Point)
def is_resolution(value, *, required=True):
if value is None:
return not required
if not is_string(value):
return False
try:
pd.tseries.frequencies.to_offset(value)
return True
except ValueError:
return False
def is_scenario(value, *, required=True):
if value is None:
return not required
return value in [directory.name for directory in pathlib.Path("./input/scenarios").iterdir() if directory.is_dir()]
def is_sensitivity_config(value, *, required=True):
if value is None:
return not required
if not isinstance(value, dict):
return False
return value["analysis_type"] in ["curtailment", "climate_years", "technology_scenario", "hydrogen_demand", "extra_hydrogen_costs", "dispatchable_generation", "hydropower_capacity", "interconnection_capacity", "interconnection_efficiency", "min_self_sufficiency", "max_self_sufficiency", "barrier_convergence_tolerance"]
def is_series(value, *, required=True):
if value is None:
return not required
return isinstance(value, pd.core.series.Series)
def is_string(value, *, required=True, min_length=0):
if value is None:
return not required
if not isinstance(value, str):
return False
return len(value) >= min_length
def is_technology(value, *, required=True):
if value is None:
return not required
if value in ["pv", "onshore", "offshore"]:
return True
if value in ["nuclear", "h2_ccgt", "h2_gas_turbine"]:
return True
if value in ["run_of_river", "reservoir", "pumped_storage_open", "pumped_storage_closed"]:
return True
if value in ["lion"]:
return True
if value in ["pem"]:
return True
return False
def is_technology_list(value, *, required=True):
if value is None:
return not required
if not is_list_like(value) or len(value) == 0:
return False
return all(is_technology(x) for x in value)
def is_technology_scenario(value, *, required=True):
if value is None:
return not required
return value in ["conservative", "moderate", "advanced"]
def is_technology_type(value, *, required=True):
if value is None:
return not required
return value in ["ires", "dispatchable", "hydropower", "storage", "electrolysis"]
def is_url(value, *, required=True):
if value is None:
return not required
url_regex = r'^(ftp|https?):\/\/[^ "]+\.\w{2,}'
return bool(re.search(url_regex, value))