-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_utils.py
782 lines (708 loc) · 33.9 KB
/
my_utils.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import datetime as dt
import itertools
import os
import string
from enum import Enum
import numpy as np
import pandas
from order_cap import order_cap
from order_gen import order_gen
"""
Functions and other utilities available in my_utils.py:
TECH: Enum class containing all technologies (having a wrapper for techs is useful for when tech names change, and enables autocompletion)
tech_names: dictionary mapping the techs to their pretty names
scen_names: dictionary mapping the scenarios to their pretty names
color_dict: dictionary mapping the techs to their colors
EPODreg_to_country: dictionary mapping the EPODreg to the country name
country_to_reg: function that takes a dictionary with reg keys (e.g. VRE_pot), and a country label, then uses EPODreg_to_country to return a dictionary with only the keys that correspond to that country
label_axes: function that walks through subplots and labels each of them with a letter
write_inc: function that takes a dictionary and writes it to a .inc file
write_inc_from_df_columns: function that takes a dataframe and writes it to a .inc file
append_to_file: function that adds 'to_add' to a new line at the bottom of originalfile
add_in_dict: function that adds a value to a dictionary, and sums it if the key already exists
crawl_resource_usage: function that prints memory and CPU usage every 5 minutes
print_red: function that prints in red
print_green: function that prints in green
print_cyan: function that prints in cyan
print_yellow: function that prints in yellow
print_blue: function that prints in blue
print_magenta: function that prints in magenta
fast_rolling_average: function that takes a list or dataframe and returns a rolling average of it
completion_sound: function that plays a sound (e.g. when the script is done)
select_pickle: function that lets the user select a pickle file to load
shorten_year: function that shortens the year in a scenario name (e.g. 2015 -> '15)
prettify_scenario_name: function that prettifies a scenario name (e.g. set1_4opt -> Set 1 (4 opt.))
load_from_file: Read the data from the specified filepath. Supports both compressed and uncompressed files.
save_to_file: Save the data to the specified filepath. Supports both compressed and uncompressed files.
"""
os.system('color')
class TECH(str, Enum):
ELECTROLYZER = 'efuel'
NUCLEAR = 'U'
LIGNITE = 'B'
COAL = 'H'
BIOMASS = 'W'
LIGNITE_CCS = 'BCCS'
COAL_CCS = 'HCCS'
GAS_CCS = 'GCCS'
LIGNITE_BIOMASS_CCS = 'BWCCS'
BIOMASS_CCS = 'BECCS'
COAL_BIOMASS_CCS = 'HWCCS'
COAL_CCS_FLEXIBLE = 'HCCS_flex'
LIGNITE_BIOMASS_CCS_FLEXIBLE = 'HWCCS_flex'
BIOGAS_CCS = 'WGCCS'
GAS_BIOGAS_CCS = 'GWGCCS'
GAS = 'G'
BIOGAS = 'WG'
BIOGAS_PEAK = 'WG_peak'
GAS_PEAK = 'G_peak'
FUEL_CELL = 'FC'
HYDRO = 'RO'
HYDRO_IMPORT = 'RO_imp'
WIND_OFFSHORE_1 = 'WOFF1'
WIND_OFFSHORE_2 = 'WOFF2'
WIND_OFFSHORE_3 = 'WOFF3'
WIND_OFFSHORE_4 = 'WOFF4'
WIND_OFFSHORE_5 = 'WOFF5'
WIND_ONSHORE_12 = 'WON12'
WIND_ONSHORE_11 = 'WON11'
WIND_ONSHORE_10 = 'WON10'
WIND_ONSHORE_9 = 'WON9'
WIND_ONSHORE_8 = 'WON8'
WIND_ONSHORE_7 = 'WON7'
WIND_ONSHORE_6 = 'WON6'
WIND_ONSHORE_5 = 'WONA5'
WIND_ONSHORE_4 = 'WONA4'
WIND_ONSHORE_3 = 'WONA3'
WIND_ONSHORE_2 = 'WONA2'
WIND_ONSHORE_1 = 'WONA1'
SOLAR_OPT = 'PV_cSiOPT'
SOLAR_TRACKING = 'PV_cSiTWO'
PV_A1 = 'PVPA1'
PV_A2 = 'PVPA2'
PV_A3 = 'PVPA3'
PV_A4 = 'PVPA4'
PV_A5 = 'PVPA5'
PV_R1 = 'PVR1'
PV_R2 = 'PVR2'
PV_R3 = 'PVR3'
PV_R4 = 'PVR4'
PV_R5 = 'PVR5'
BATTERY = 'bat'
BATTERY_CAP = 'bat_cap'
FLYWHEEL = 'flywheel'
SYNCHRONOUS_CONDENSER = 'sync_cond'
H2_STORAGE = 'H2store'
ELECTRIC_BOILER = 'EB'
HEAT_PUMP = 'HP'
thermals = [
"B",
"H",
"G",
"G_peak",
"W",
"U",
"BCCS",
"HCCS",
"GCCS",
"BECCS",
"WG",
"WG_peak",
"WGCCS",
"B_CHP",
"H_CHP",
"W_CHP",
"G_CHP",
"WG_CHP",
"WA_CHP"
]
order_map_cap = {j: i for i, j in enumerate(order_cap)}
order_map_gen = {j: i for i, j in enumerate(order_gen)}
tech_names = {'RO': 'Hydro', 'RR': 'Run-of-river', 'U': 'Nuclear', "b": "Lignite ST",
'CHP_wa': 'Waste CHP', 'CHP_bio': 'Woodchip CHP', "G_CHP": "N. Gas CHP", "W_CHP": "Biomass CHP",
"WA_CHP": "Waste CHP", "B_CHP": "Lignite CCS", "H_CHP": "Coal CHP", "WG_CHP": "Biogas CHP",
'GWGCCS': 'Gas-mix CCS', "BCCS": "Lignite CCS", "HCCS": "Coal CCS", "GCCS": "N. Gas CCS",
"BWCCS": "Lignite-biomass mix CCS", "BECCS": "Biomass CCS", "HWCCS": "Coal-biomass mix CCS",
"WGCCS": "Biogas CCS", "Bat. storage": "Bat. storage",
"bat_discharge": "Battery (dis)charge", 'WOFF': 'Offshore wind', 'WON': 'Onshore wind',
'WG': 'Biogas CCGT', 'WG_peak': 'Biogas GT', 'wind_offshore': 'Offshore wind',
"flywheel": "Flywheel", "bat": "Bat. storage", "sync_cond": "Sync. Cond.",
'wind_onshore': 'Onshore wind', 'PV_cSiOPT': 'Solar PV', 'EB': 'EB', 'HP': 'HP', 'HOB_WG': 'Biogas HOB',
'HOB_bio': 'Woodchip HOB', 'solarheat': 'Solar heating', "curtailment": "Curtailment",
'Load': 'Load', 'bat_PS': "Battery (PS)", 'bat_cap_PS': "Battery cap (PS)", 'bat_cap': "Bat. power",
"electrolyser": "Electrolyser", "H": "Coal ST", "W": "Biomass ST",
"G": "N. Gas CCGT", "G_peak": "N. Gas GT", "PV": "Solar PV", "FC": "Fuel cell",
"H2store": "H2 storage", "PtH":"Power-to-Heat", "thermals":"Thermal power", "Hydro":"Hydro power",
"biogas": "Biogas", "Export south": "Exp. south", "Export north": "Exp. north", "System cost": "System cost",
}
scen_names = {"_pre": "Base case", "_leanOR": "Lean OR", "_OR": "OR", "_OR_fixed": "OR", "_OR_inertia": "OR + Inertia",
"_OR+inertia_fixed": "OR + Inertia", "_inertia": "Inertia", "_inertia_2x": "2x Inertia",
"_inertia_noSyn": "Inertia (noSyn)", "_OR_inertia_3xCost": "OR + Inertia (3x)",
"_inertia_3xCost": "Inertia (3x)", "_inertia_noSyn_3xCost": "Inertia (noSyn) (3x)", "noFC": "No FC",
"fullFC": "Full FC", "OR": "FR", "inertia": "Inertia", "lowFlex":"LowFlex", "highFlex":"HighFlex"}
color_dict = {'B_CHP': "#23343A", 'Base': '#2b2d42', 'Bat. In': "#8d5eb7", 'Bat. Out': "#8d5eb7",
'bat': "#714b92", 'bat_PS': "xkcd:deep lavender", 'bat_cap': "#8d5eb7",
'bat_cap_PS': "xkcd:deep lavender", 'bat_discharge': "xkcd:amber", 'BECCS': "#5b9aa0",
'Bio thermals': "#2a9d8f", 'b': "#172226", 'CHP': "#5BB0F6",
'CHP_WG': (0, 176 / 255, 80 / 255), 'CHP_WG_L': 'xkcd:mid green', 'CHP_bio': 'xkcd:tree green',
'curtailment': "xkcd:slate", 'EB': "#E85D04", 'electrolyser': "#68032e", 'FC': "#c65082",
'Fossil thermals': "#2b2d42", 'G': "#5B90F6", 'G_CHP': "#5BB0F6", 'G_peak': "#7209b7",
'GWGCCS': 'xkcd:dark peach', 'H': "#172226", 'H2store': "#ad054d",
'HOB_WG': (128 / 255, 128 / 255, 0), 'HOB_bio': 'green', 'H_CHP': "#172618", 'HP': "#F48C06",
'Hydro': 'xkcd:ocean blue', 'Load': 'Black', 'offset': "white", "Other thermals": "Peru",
'Peak': "crimson", 'PV': 'xkcd:mustard', 'PtH': "#59A5B1", 'RO': 'xkcd:ocean blue', 'RR': 'xkcd:ocean blue',
'solarheat': (204 / 255, 51 / 255, 0), 'sync_cond': 'xkcd:aqua', 'Thermals': "#5BB0F6", 'U': 'xkcd:grape',
'WA_CHP': 'xkcd:deep lavender', 'W': "#014421", 'WOFF': '#DADADA', 'WON': '#B9B9B9',
'Wind': '#B9B9B9', 'wind_offshore': '#DADADA', 'wind_onshore': '#B9B9B9', 'WG': '#a4be20',
'WG_CHP': "#83981a", 'WG_peak': '#d00000', 'W_CHP': "#016421"}
color_dict_paper3 = {'wind_onshore': '#B9B9B9', 'wind_offshore': '#DADADA', 'Wind': '#B9B9B9', 'RO': 'xkcd:ocean blue',
'U': 'xkcd:grape', 'Nuclear': 'xkcd:grape', 'GWGCCS': 'xkcd:dark peach', 'WA_CHP': 'xkcd:deep lavender',
'CHP_bio': 'xkcd:tree green', 'WG': '#a4be20', 'WG_peak': '#d00000', "WG_CHP": "#83981a",
'PV_cSiOPT': 'xkcd:mustard', 'CHP_WG_L': 'xkcd:mid green', 'RR': 'xkcd:ocean blue',
'HP': "#59A5B1", 'EB': "#ACD2D8", "Fossil thermals": "#2b2d42", "Bio thermals": "#2a9d8f",
'CHP_WG': (0, 176 / 255, 80 / 255), "Solar PV": 'xkcd:mustard', 'Hydro': 'xkcd:ocean blue',
'HOB_WG': (128 / 255, 128 / 255, 0), 'solarheat': (204 / 255, 51 / 255, 0), 'HOB_bio': 'green',
'Load': 'Black', "bat_discharge": "xkcd:amber", 'bat': "#714b92", 'Battery': "#714b92",
'bat_cap': "#8d5eb7", 'Bat. In': "#8d5eb7", 'Bat. Out': "#8d5eb7", 'bat_PS': "xkcd:deep lavender",
'bat_cap_PS': "xkcd:deep lavender", "sync_cond": 'xkcd:aqua', "curtailment": "xkcd:slate",
'WOFF': '#DADADA', 'WON': '#B9B9B9', "H": "#172226", "H_CHP": "#172618", "b": "#172226", "B_CHP": "#23343A",
"W": "#014421", "W_CHP": "#016421", "G": "#5B90F6", "G_peak": "#7209b7", "G_CHP": "#5BB0F6",
"PV": "#FDC12A", "FC": "#c65082", "H2store": "#ad054d", "electrolyser": "#68032e", "BECCS": "#5b9aa0",
"Base":"#2b2d42", "Peak": "#5B90F6", "PtH": "#e85d04", "CHP": "#5BB0F6", "Thermals": "#5BB0F6",
"offset": "white"}
# Additional entries for clarity and consistency
color_dict["Battery"] = color_dict["bat"]
color_dict["Bat. storage"] = color_dict["bat"]
color_dict["Bat."] = color_dict["bat"]
color_dict["Bat. power"] = color_dict["bat_cap"]
color_dict["Solar PV"] = color_dict["PV"]
color_dict["PV_cSiOPT"] = color_dict["PV"]
color_dict["Hydro power"] = color_dict["Hydro"]
color_dict["Baseload"] = color_dict["Base"]
color_dict["Nulear"] = color_dict["U"]
# If a value from tech_names is in color_dict, add the value from tech_names to color_dict with the same color
for key, value in tech_names.items():
if key in color_dict:
color_dict[value] = color_dict[key]
EPODreg_to_country = { # dictionary for going between EPODreg to country
'AT': 'Austria', 'BE': 'Belgium', 'BO': 'Bosnia', 'BG': 'Bulgaria', 'CR': 'Croatia', 'CY': 'Cyprus',
'CZ': 'Czech_Republic', 'DK1': 'Denmark', 'DK2': 'Denmark', 'EE': 'Estonia', 'FI': 'Finland', 'FR1': 'France',
'FR2': 'France', 'FR3': 'France', 'FR4': 'France', 'FR5': 'France', 'DE1': 'Germany', 'DE2': 'Germany',
'DE3': 'Germany', 'DE4': 'Germany', 'DE5': 'Germany', 'GR': 'Greece', 'HU': 'Hungary', 'IS': 'Iceland',
'IE': 'Ireland', 'IT1': 'Italy', 'IT2': 'Italy', 'IT3': 'Italy', 'LV': 'Latvia', 'LT': 'Lithuania',
'LU': 'Luxembourg', 'MC': 'Macedonia', 'MT': 'Malta', 'NL': 'Netherlands', 'NO_S': 'Norway', 'NO_N': 'Norway',
'NO1': 'Norway', 'NO2': 'Norway', 'NO3': 'Norway', 'PO1': 'Poland', 'PO2': 'Poland', 'PO3': 'Poland',
'PT': 'Portugal', 'RO': 'Romania', 'SK': 'Slovakia', 'SI': 'Slovenia', 'ES_N': 'Spain', 'ES_S': 'Spain',
'ES1': 'Spain', 'ES2': 'Spain', 'ES3': 'Spain', 'ES4': 'Spain', 'SE_N': 'Sweden', 'SE_S': 'Sweden', 'SE1': 'Sweden',
'SE2': 'Sweden', 'SE3': 'Sweden', 'SE4': 'Sweden', 'CH': 'Switzerland', 'UK1': 'UK', 'UK2': 'UK', 'UK3': 'UK'
}
EPODs = list(EPODreg_to_country.keys())
scenario_shortening = {"iberia": "IB", "brit": "BR", "nordic": "NE",
"CO2price": "CO2p", "fullFC": "FC", }
regions_corrected = {"brit": "Brit", "nordic": "Nordic+", "iberia": "Iberia"}
year_names = {2020: "ref. 2020", 2025: "near-term", 2030: "mid-term", 2040: "long-term"}
year_names_twolines = {2020: "ref.\nyear", 2025: "near-\nterm", 2030: "mid-\nterm", 2040: "long-\nterm"}
def country_to_reg(dictionary, country):
"""
Parameters
----------
dictionary
country
Returns
-------
takes a dictionary with reg keys (e.g. VRE_pot), and a country label, then uses EPODreg_to_country to return a dictionary with only
the keys that correspond to that country
"""
return {reg: dictionary[reg] for reg in dictionary if country in EPODreg_to_country[reg]}
def label_axes(fig, labels=None, loc=None, **kwargs):
"""
Walks through axes and labels each.
kwargs are collected and passed to `annotate`
Parameters
----------
fig : Figure
Figure object to work on
labels : iterable or None
iterable of strings to use to label the axes.
If None, lower case letters are used.
loc : len=2 tuple of floats
Where to put the label in axes-fraction units
"""
if labels is None:
labels = string.ascii_lowercase
# re-use labels rather than stop labeling
labels = itertools.cycle(labels)
if loc is None:
loc = (.9, .9)
for ax, lab in zip(fig.axes, labels):
ax.annotate(lab, xy=loc,
xycoords='axes fraction',
**kwargs)
def write_inc(path, filename, data: dict, flip=True, fliplast=False, comment=False):
"""
Parameters
----------
path
filename
data
flip
Returns
-------
nothing, but creates path/filename.inc containing a variable with 2 or 3 sets, e.g. tech + reg (+ opt. timestep)
"""
with open(path + filename, "w") as writer:
if comment:
writer.write(f"* ---\n")
if type(comment) == list:
for c in comment: writer.write(f"* {c}\n")
else:
writer.write(f"* ---\n* {comment}\n* ---")
writer.write(f"* ---\n")
if type(data) not in [list, dict]:
print("! Wrong data type given to write_inc()")
return
elif type(data)==list:
for i, val in enumerate(data):
writer.write(f"{'h' + str(i + 1).zfill(4)} {val}\n")
for key1, val1 in data.items():
if type(val1) == dict: # key1: {..}
for key2, val2 in val1.items():
if type(val2) == dict: # key1: {key2: {}}
for key3, val3 in val2.items():
if type(val3) == dict: # key1: {key2: {key3: {}}}
for key4, val4 in val3.items():
if flip:
if fliplast:
writer.write(f"{key2:4} . {key1:6} . {key4:6} . {key3:6} {val4}\n")
else:
writer.write(f"{key2:4} . {key1:6} . {key3:6} . {key4:6} {val4}\n")
else:
writer.write(f"{key1:4} . {key2:6} . {key3:6} . {key4:6} {val4}\n")
elif type(val3) == list: # key1: {key2: {key3: [values]}}
for i, val4 in enumerate(val3):
writer.write(f"{key1:4} . {key2:4} . {key3:4} . {'h' + str(i + 1).zfill(4)} {val4}\n")
else: # key1: {key2: {key3:val3}}
if flip:
writer.write(f"{key2:4} . {key1:6} . {key3:4} {val3}\n")
else:
writer.write(f"{key1:4} . {key2:6} . {key3:4} {val3}\n")
elif type(val2) == list: # key1: {key2: [values]}
for i, val3 in enumerate(val2):
writer.write(f"{key1:4} . {key2:4} . {'h' + str(i + 1).zfill(4)} {val3}\n")
else: # key1: {key2:val2}
if flip:
writer.write(f"{key2:4} . {key1:6} {val2}\n")
else:
writer.write(f"{key1:4} . {key2:6} {val2}\n")
elif type(val1) == list: # key1: [values]
for i, val2 in enumerate(val1):
writer.write(f"{key1:4} . {'h' + str(i + 1).zfill(4)} {val2}\n")
if val1 == "":
writer.write(f"{'h' + str(i + 1).zfill(4)} {val2}\n")
else: # key1: val1
writer.write(f"{key1:6} {val1}\n")
return None
def write_inc_from_df_columns(path, filename, df: pandas.DataFrame, comment=False, first_day=1):
"""
Parameters
----------
path
filename
df, where index layers are the parameter sets, and the only column is the values
comment, text to be put in the top of the inc file (commented out with *..)
Returns
-------
nothing, but creates path/filename.inc
"""
try:
os.mkdir(path)
except:
None
if "timestep" in df.index.names and first_day > 1:
if df.index.get_level_values(level="timestep")[0] in ["d001a", "h0001"]:
import re
def shift_time_index(df, start_hour):
# Identify the index level containing the desired values
target_level = None
for level, name in enumerate(df.index.names):
if any(re.match(r'(h|d)\d+', value) for value in df.index.get_level_values(name)):
target_level = level
break
if target_level is None:
print("No matching index level found.")
return df
# Modify the index level with the given start_hour
def update_time_index(value):
prefix, num = value[0], int(value[1:])
new_num = (num + start_hour - 1) % len(df) + 1
return f'{prefix}{new_num:04d}'
new_index_level = df.index.get_level_values(target_level).map(update_time_index)
new_index = df.index.set_levels(new_index_level, level=target_level)
# Set the modified index back to the DataFrame
df.index = new_index
shift_time_index(df, (first_day-1)*24)
else:
print("First day is not 1, but the first timestep index is not 'd001a' or 'h0001'.")
with open(path + filename, "w") as writer:
if comment:
writer.write(f"* ---\n")
if type(comment) == list:
for c in comment: writer.write(f"* {c}\n")
else:
writer.write(f"* ---\n* {comment}\n* ---")
writer.write(f"* ---\n")
#dim = len(df.columns)
for index, value in df.iterrows():
if type(index) == tuple:
line = " . ".join(index) + f" {value[0]}\n"
else:
value = [str(i) for i in value[:-1]] + [value[-1]]
line = " . ".join(value[:-1]) + f" {value[-1]}\n"
writer.write(line)
return None
def append_to_file(filename, scenario, time_to_solve):
"adds 'to_add' to a new line at the bottom of originalfile"
to_add = f"{dt.datetime.now().strftime('%D - %H:%M:%S')} : {scenario:<40} : " \
f"{time_to_solve} min\n"
with open(filename + ".txt", 'a') as f2:
f2.write(to_add)
def add_in_dict(d, key, val, group_vre=False, tech_position=0):
is_touple = type(key) == tuple
if is_touple:
tech = key[tech_position]
else:
tech = key
if group_vre: # group WONA1, WONA2, ... to "WON"
if "WON" in tech:
tech = "WON"
elif "PV" in tech:
tech = "PV"
if is_touple:
key = (tech, key[1])
else:
key = tech
if key in d:
d[key] += val
else:
d[key] = val
def crawl_resource_usage(timer=5):
import time
import psutil
from termcolor import colored
print(f"Resource usage crawler started. Will print memory and CPU usage (%) every {timer} minutes.")
while True:
color = "red" if psutil.virtual_memory().percent > 80 or psutil.cpu_percent(2) > 80 else "white"
print(colored(
f"~~ Resource crawler, {dt.datetime.now().strftime('%H:%M:%S')} ~~, RAM: {psutil.virtual_memory().percent} %, CPU: {psutil.cpu_percent(2)} %",
color))
time.sleep(timer * 60)
def print_red(to_print, *argv, replace_this_line=False, **kwargs):
from termcolor import colored
if type(to_print) != str:
to_print = str(to_print)
if len(argv) > 0:
for arg in argv:
to_print += "\n"+str(arg)
if replace_this_line:
end = '\r'
else:
end = kwargs.pop('end', '\n')
print(colored(to_print, "red"), end=end, **kwargs)
def print_green(to_print, *argv, replace_this_line=False, **kwargs):
from termcolor import colored
if type(to_print) != str:
to_print = str(to_print)
if len(argv) > 0:
for arg in argv:
to_print += " "+str(arg)
if replace_this_line:
end = '\r'
else:
end = kwargs.pop('end', '\n')
print(colored(to_print, "green"), end=end, **kwargs)
def print_cyan(to_print, *argv, replace_this_line=False, **kwargs):
from termcolor import colored
if type(to_print) != str:
to_print = str(to_print)
if len(argv) > 0:
for arg in argv:
to_print += " "+str(arg)
if replace_this_line:
end = '\r'
else:
end = kwargs.pop('end', '\n')
print(colored(to_print, "cyan"), end=end, **kwargs)
def print_yellow(to_print, *argv, replace_this_line=False, **kwargs):
from termcolor import colored
if type(to_print) != str:
to_print = str(to_print)
if len(argv) > 0:
for arg in argv:
to_print += " "+str(arg)
if replace_this_line:
end = '\r'
else:
end = kwargs.pop('end', '\n')
print(colored(to_print, "yellow"), end=end, **kwargs)
def print_blue(to_print, *argv, replace_this_line=False, **kwargs):
from termcolor import colored
if type(to_print) != str:
to_print = str(to_print)
if len(argv) > 0:
for arg in argv:
to_print += " "+str(arg)
if replace_this_line:
end = '\r'
else:
end = kwargs.pop('end', '\n')
print(colored(to_print, "blue"), end=end, **kwargs)
def print_magenta(to_print, *argv, replace_this_line=False, **kwargs):
from termcolor import colored
if type(to_print) != str:
to_print = str(to_print)
if len(argv) > 0:
for arg in argv:
to_print += " "+str(arg)
if replace_this_line:
end = '\r'
else:
end = kwargs.pop('end', '\n')
print(colored(to_print, "magenta"), end=end, **kwargs)
def fast_rolling_average(my_list, window_size, wraparound=True, **kwargs):
import pandas as pd
if window_size == 0:
if type(my_list) in [list, np.ndarray]:
return pd.DataFrame(my_list)
return my_list
elif type(window_size) == float:
window_size = round(window_size)
if type(my_list) == np.ndarray:
my_list = list(my_list)
if wraparound:
wraparound = int(window_size/2+2)
final_index = slice(wraparound,-wraparound)
else: final_index = slice(None)
if type(my_list) == list:
df = pd.DataFrame(my_list)
else:
df = my_list
if wraparound: df = pd.concat([df.iloc[-wraparound:], df, df.iloc[:wraparound]])
return df.rolling(window_size, **kwargs).mean().fillna(method="bfill").iloc[final_index]
def completion_sound():
from winsound import Beep
notes = [(440, 300), (494, 300), (523, 300), (587, 300), (659, 300)] # Pairs of (frequency, duration)
for note, duration in notes:
Beep(note, duration)
def select_pickle(predetermined_choice=False, pickle_folder="PickleJar\\"):
import glob
pickle_files = glob.glob(os.path.join(pickle_folder, "data_results_*"))
if not pickle_files:
print_red("No data_results_* files found in PickleJar folder.")
return None
if predetermined_choice==True:
predetermined_choice = 1
pickle_files.sort(key=os.path.getmtime, reverse=True)
print_blue(f"Found {len(pickle_files)} data_results_* files.")
print_blue(f"Most recent file: {pickle_files[0]}")
if predetermined_choice == 1 or len(pickle_files) == 1 or predetermined_choice == "most_recent":
# Either use defaults or no appropriate pickle files were found, so just use the most recent file
# most_recent_file = max(pickle_files, key=lambda x: os.path.getctime(pickle_folder + x))
return pickle_files[0]
elif type(predetermined_choice) == int:
user_input = str(predetermined_choice)
elif predetermined_choice == "combine":
user_input = '5'
else:
print_yellow("Select the pickle file to load:")
print_yellow("1. Most recent file")
print_yellow("2. Largest file")
print_yellow("3. Pick among the 10 most recent files")
print_yellow("4. Enter the filename manually")
print_yellow("5. A combination of the most recent allyears and allopt pickle files")
user_input = input("Please enter the option number: ")
if user_input == '1':
# Most recent file
return pickle_files[0] # The list is already sorted by modification time
elif user_input == '2':
# Largest file
pickle_files.sort(key=os.path.getsize, reverse=True)
return pickle_files[0]
elif user_input == '3':
# Pick among the 10 most recent files
print_yellow("Pick among the 10 most recent files:")
for i, f in enumerate(pickle_files[:10]):
print(f"{i + 1}. " + f.split("\\")[-1])
user_input = input("Please enter the option number: ")
try:
return pickle_files[int(user_input) - 1]
except:
print_red("Invalid input. Falling back to the most recent file.")
return pickle_files[0]
elif user_input == '4':
# Enter the filename manually
user_input = input("Please enter the filename: ")
if user_input in pickle_files or "PickleJar\\" + user_input in pickle_files or "PickleJar\\" + user_input + ".pickle" in pickle_files or "PickleJar\\" + user_input + ".blosc" in pickle_files:
if "PickleJar\\" not in user_input:
user_input = "PickleJar\\" + user_input
return user_input
else:
print_red("The file was not found in the directory. Falling back to the most recent file.")
return pickle_files[0]
elif user_input == '5':
# A combination of the most recent allyears and allopt pickle files
allyears_pickle = max([f for f in pickle_files if "allyears" in f], key=os.path.getmtime)
sets_pickle = max([f for f in pickle_files if "allopt" in f], key=os.path.getmtime)
trueref_sets_pickle = max([f for f in pickle_files if "trueref_all" in f], key=os.path.getmtime)
random_pickle = max([f for f in pickle_files if "random" in f and "addme" not in f], key=os.path.getmtime)
return [allyears_pickle, sets_pickle, trueref_sets_pickle, random_pickle]
def shorten_year(scenario):
import re
# define a function to be used in re.sub
def replacer(match):
return "'" + match.group()[-2:]
# use re.sub to replace all occurrences of 4-digit years
return re.sub(r'(19|20)\d{2}', replacer, scenario)
def prettify_scenario_name(name,return_single_as_year=False):
#print_yellow(f"Prettifying scenario name: {name}")
if "set1" in name:
#print_yellow("Set 1 scenario detected")
# turn set1_4opt into Set 1 (4 opt.)
nr = name.split("_")[1].replace("opt", "")
alt = " alt."*('alt' in name)
even = ", eq. w."*('even' in name)
if "even" in name: nr = 4
return f"2 HP + {nr}{alt} opt." + even # 2 opt., 2 HP
if "HP" in name and "opt" in name:
parts = name.split("_")
opt = parts[1][0]
extra = f" ({parts[-1]})" if len(parts) == 3 and parts[-1]!="mean" else ""
if "2012" in extra:
return f"{name[0]} HP + {opt} opt. (2012)"
elif "evenweights" in extra:
extra = ", eq. w."
return f"{name[0]} HP + {opt} opt.{extra}"
if "allyears" in name:
return "All years"
if "allopt" in name:
# turn allopt2_final into All opt. (2 yr), and allopt2_final_a into All opt. (2 yr) a
nr = name.split("_")[0].replace("allopt", "")
if len(name.split("_")) == 3:
abc = name.split("_")[2]
abc = f" ({abc})"
else:
abc = ""
return f"{nr} opt.{abc}"
if "iter2_3" in name:
return "Set (1 opt.)"
elif "iter3_16start" in name:
return "Set (2 opt.)"
if "singleyear" in name:
# turn 'singleyear_1989to1990_1h' into "'89-'90" using regex
if return_single_as_year: return shorten_year(name)
return "Single year"
# remove 'base' and 'extreme' and split into a list
parts = name.replace('base', '').replace('extreme', ' ').split()
if "v2" in name or "_5_" in name:
return f'Alt. set ({parts[0]} opt.)'
elif "even" in name:
return f'6 yr, eq. weights'
# join the parts with appropriate labels
return f'Set ({parts[0]} opt.)'
def load_from_file(filepath):
"""
Read the data from the specified filepath. The following file formats are supported: .pickle, .csv, .blosc.
If no file extension is specified, the function will first look for a .blosc file, then a .pickle file. This is recommended as it lends more flexibility to the function.
"""
import os
# if the file is a pickle file, load it as a pickle file
expected_filetypes = [".pickle", ".csv", ".blosc"]
if not any([filepath.endswith(ft) for ft in expected_filetypes]):
filepath += ".blosc"
#if the file is not found, look for a .pickle file instead
if not os.path.isfile(filepath):
filepath = filepath.replace(".blosc", ".pickle")
if filepath.endswith(".pickle"):
import pickle
with open(filepath, "rb") as f:
return pickle.load(f)
elif filepath.endswith(".csv"):
import pandas as pd
return pd.read_csv(filepath)
elif filepath.endswith(".blosc"):
try: import blosc2 as blosc
except ImportError: import blosc
import pickle
blosc.set_nthreads(4)
with open(filepath, "rb") as f:
data = blosc.decompress(f.read())
return pickle.loads(data)
def save_to_file(data, filepath, clever=5, nthreads=4, max_compression=True, **kwargs):
"""
Save the data to the specified filepath. The following file formats are supported:
1 .pickle (NOT RECOMMENDED): A standard, uncompressed way of storing python objects in binary files. Fast to save and load, but takes up a lot of space. If you want to prioritize speed, consider using .blosc with max_compression=False instead.
2 .csv: Available only to pandas objects with a .to_csv() method.
3 .blosc (default): A compressed binary file format. Might take another second to load/save a ~1GB file, but instead it takes up only a fraction of the space of a pickle file.
If no file extension is specified, it defaults to .blosc. This is recommended as it lends more flexibility to the function.
Parameters:
-----------
data: the data to be saved
filepath: the path to the file to be saved. If no file extension is specified, it defaults to .blosc
clevel: the compression level to use. 0 is fastest, 9 is slowest. 5 is the default and recommended value as no big gains are made by going higher.
nthreads: the number of threads to use for compression. Large diminishing returns after 4 threads.
max_compression: if False, use LZ4 compression instead of ZSTD. LZ4 is a lot faster, but ZSTD is a lot more efficient.
**kwargs: additional arguments to be passed to blosc.compress2()
Notes on speed:
---------------
With max_compression=False, about 90% of the time to save a file is spent on serializing the data which must be done regardless of compression or not.
With max_compression=True, the time spent compressing the data can become a significant portion of the total time to save a file. Still, the time spent is not enough to make you bored.
"""
# if the file is a pickle file, save it as a pickle file
supported_filetypes = [".pickle", ".csv", ".blosc"]
if not any([filepath.endswith(ft) for ft in supported_filetypes]):
# check if the blosc2 module is available
legacy_blosc = False
try:
try: import blosc2 as blosc
except ImportError:
import blosc
legacy_blosc = True
filepath += ".blosc" #default to blosc
except:
print_red("Blosc2 module not found. Please install it using 'pip install blosc2' or use a different file extension.")
filepath += ".pickle"
if filepath.endswith(".pickle"):
import pickle
with open(filepath, "wb") as f:
pickle.dump(data, f, protocol=pickle.DEFAULT_PROTOCOL) # HIGHEST_PROTOCOL is faster but only available in Python 3.8+
if max_compression:
print_red("Max compression not supported for pickle files. Save as .blosc instead.")
elif filepath.endswith(".csv"):
import pandas as pd
data.to_csv(filepath)
elif filepath.endswith(".blosc"):
import pickle
bytes_data = pickle.dumps(data, protocol=pickle.DEFAULT_PROTOCOL)
if len(bytes_data) > 2_147_483_631: # ValueError: bytesobj cannot be larger than 2147483631 bytes (2 GB)
print_red(f"Data is too large to be saved to a .blosc file. Saving as .pickle instead.")
with open(filepath.replace(".blosc", ".pickle"), "wb") as f:
pickle.dump(data, f, protocol=pickle.DEFAULT_PROTOCOL)
return None
if legacy_blosc:
import blosc
else:
import blosc2 as blosc
if not max_compression:
codec_to_use = blosc.Codec.LZ4
else:
codec_to_use = blosc.Codec.ZSTD
blosc.set_nthreads(nthreads)
with open(filepath, "wb") as f:
if legacy_blosc:
f.write(blosc.compress(bytes_data, cname="lz4", clevel=9, **kwargs))
else:
f.write(blosc.compress2(bytes_data, codec=codec_to_use, clevel=clever, filter=blosc.Filter.SHUFFLE, **kwargs))
else:
print_red(f"File extension not recognized: {filepath}")
return None
#print_green(f"Data saved to {filepath}")
return None