-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.py
648 lines (525 loc) · 19.4 KB
/
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
# Copyright 2016-2018 Fabian Hofmann (FIAS), Jonas Hoersch (KIT, IAI) and
# Fabian Gotzens (FZJ, IEK-STE)
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Utility functions for checking data completeness and supporting other functions
"""
import multiprocessing
import os
from ast import literal_eval as liteval
import country_converter as coco
import numpy as np
import pandas as pd
import pycountry as pyc
import requests
import six
from numpy import atleast_1d
from tqdm import tqdm
from .core import _data_in, _package_data, get_config, get_obj_if_Acc, logger
cc = coco.CountryConverter()
def lookup(df, keys=None, by="Country, Fueltype", exclude=None, unit="MW"):
"""
Returns a lookup table of the dataframe df with rounded numbers.
Use different lookups as "Country", "Fueltype" for the different lookups.
Parameters
----------
df : pandas.Dataframe or list of pandas.Dataframe's
powerplant databases to be analysed. If multiple dataframes are passed
the lookup table will display them in a MulitIndex
by : string out of 'Country, Fueltype', 'Country' or 'Fueltype'
Define the type of lookup table you want to obtain.
keys : list of strings
labels of the different datasets, only necessary if multiple dataframes
passed
exclude: list
list of fueltype to exclude from the analysis
"""
df = get_obj_if_Acc(df)
if unit == "GW":
scaling = 1000.0
elif unit == "MW":
scaling = 1.0
else:
raise (ValueError("unit has to be MW or GW"))
def lookup_single(df, by=by, exclude=exclude):
df = read_csv_if_string(df)
if isinstance(by, str):
by = by.replace(" ", "").split(",")
if exclude is not None:
df = df[~df.Fueltype.isin(exclude)]
return df.groupby(by).Capacity.sum()
if isinstance(df, list):
if keys is None:
keys = [get_name(d) for d in df]
dfs = pd.concat([lookup_single(a) for a in df], axis=1, keys=keys, sort=False)
dfs = dfs.fillna(0.0)
return (dfs / scaling).round(3)
else:
return (lookup_single(df) / scaling).fillna(0.0).round(3)
def get_raw_file(name, update=False, config=None, skip_retrieve=False):
if config is None:
config = get_config()
df_config = config[name]
path = _data_in(df_config["fn"])
if (not os.path.exists(path) or update) and not skip_retrieve:
url = df_config["url"]
logger.info(f"Retrieving data from {url}")
r = requests.get(url)
with open(path, "wb") as outfile:
outfile.write(r.content)
return path
def config_filter(df, config):
"""
Convenience function to filter data source according to the config.yaml
file. Individual query filters are applied if argument 'name' is given.
Parameters
----------
df : pd.DataFrame
Data to be filtered
name : str, default None
Name of the data source to identify query in the config.yaml file
config : dict, default None
Configuration overrides varying from the config.yaml file
"""
df = get_obj_if_Acc(df)
name = df.powerplant.get_name()
assert name is not None, "No name given for data source"
countries = config["target_countries"] # noqa
fueltypes = config["target_fueltypes"] # noqa
cols = config["target_columns"]
target_query = "Country in @countries and Fueltype in @fueltypes"
main_query = config.get("main_query", "")
# individual filter from config.yaml
queries = {}
for source in config["matching_sources"]:
if isinstance(source, dict):
queries.update(source)
else:
queries[source] = ""
ds_query = queries.get(name, "")
query = " and ".join([q for q in [target_query, main_query, ds_query] if q])
df = correct_manually(df, name, config=config)
return df.reindex(columns=cols).query(query).reset_index(drop=True)
def correct_manually(df, name, config=None):
"""
Update powerplant data based on stored corrections in
powerplantmatching/data/in/manual_corrections.csv. Specify the name
of the data by the second argument.
Parameters
----------
df : pandas.DataFrame
Powerplant data
name : str
Name of the data source, should be in columns of manual_corrections.csv
"""
if config is None:
config = get_config()
corrections_fn = _package_data("manual_corrections.csv")
corrections = pd.read_csv(corrections_fn)
corrections = (
corrections.query("Source == @name")
.drop(columns="Source")
.set_index("projectID")
)
if corrections.empty:
return df
df = df.set_index("projectID").copy()
df.update(corrections)
return df.reset_index()
def set_uncommon_fueltypes_to_other(df, fillna_other=True, config=None, **kwargs):
"""
Replace uncommon fueltype specifications as by 'Other'. This helps to
compare datasources with Capacity statistics given by
powerplantmatching.data.Capacity_stats().
Parameters
----------
df : pd.DataFrame
DataFrame to replace 'Fueltype' argument
fillna_other : Boolean, default True
Whether to replace NaN values in 'Fueltype' with 'Other'
fueltypes : list
list of replaced fueltypes, defaults to
['Bioenergy', 'Geothermal', 'Mixed fuel types', 'Electro-mechanical',
'Hydrogen Storage']
"""
config = get_config() if config is None else config
df = get_obj_if_Acc(df)
default = [
"Bioenergy",
"Geothermal",
"Mixed fuel types",
"Electro-mechanical",
"Hydrogen Storage",
]
fueltypes = kwargs.get("fueltypes", default)
df.loc[df.Fueltype.isin(fueltypes), "Fueltype"] = "Other"
if fillna_other:
df = df.fillna({"Fueltype": "Other"})
return df
def read_csv_if_string(df):
"""
Convenience function to import powerplant data source if a string is given.
"""
from . import data
if isinstance(data, six.string_types):
df = getattr(data, df)()
return df
def to_categorical_columns(df):
"""
Helper function to set datatype of columns 'Fueltype', 'Country', 'Set',
'File', 'Technology' to categorical.
"""
cols = ["Fueltype", "Country", "Set", "File"]
cats = {
"Fueltype": get_config()["target_fueltypes"],
"Country": get_config()["target_countries"],
"Set": get_config()["target_sets"],
}
return df.assign(**{c: df[c].astype("category") for c in cols}).assign(
**{c: lambda df: df[c].cat.set_categories(v) for c, v in cats.items()}
)
def set_column_name(df, name):
"""
Helper function to associate dataframe with a name. This is done with the
columns-axis name, as pd.DataFrame do not have a name attribute.
"""
df.columns.name = name
return df
def get_name(df):
"""
Helper function to associate dataframe with a name. This is done with the
columns-axis name, as pd.DataFrame do not have a name attribute.
"""
return df.columns.name
def to_list_if_other(obj):
"""
Convenience function to ensure list-like output
"""
if not isinstance(obj, list):
return [obj]
else:
return obj
def to_dict_if_string(s):
"""
Convenience function to ensure dict-like output
"""
if isinstance(s, str):
return {s: None}
else:
return s
def projectID_to_dict(df):
"""
Convenience function to convert string of dict to dict type
"""
if df.columns.nlevels > 1:
return df.assign(
projectID=(
df.projectID.stack().dropna().apply(lambda ds: liteval(ds)).unstack()
)
)
else:
return df.assign(projectID=df.projectID.apply(lambda x: liteval(x)))
def select_by_projectID(df, projectID, dataset_name=None):
"""
Convenience function to select data by its projectID
"""
df = get_obj_if_Acc(df)
if isinstance(df.projectID.iloc[0], str):
return df.query("projectID == @projectID")
else:
return df[df["projectID"].apply(lambda x: projectID in sum(x.values(), []))]
def update_saved_matches_for_(name):
"""
Update your saved matched for a single source. This is very helpful if you
modified/updated a data source and do not want to run the whole matching
again.
Example
-------
Assume data source 'ESE' changed a little:
>>> pm.utils.update_saved_matches_for_('ESE')
... <Wait for the update> ...
>>> pm.collection.matched_data(update=True)
Now the matched_data is updated with the modified version of ESE.
"""
from .collection import collect
from .matching import compare_two_datasets
df = collect(name, use_saved_aggregation=False)
dfs = [ds for ds in get_config()["matching_sources"] if ds != name]
for to_match in dfs:
compare_two_datasets([collect(to_match), df], [to_match, name])
def fun(f, q_in, q_out):
"""
Helper function for multiprocessing in classes/functions
"""
while True:
i, x = q_in.get()
if i is None:
break
q_out.put((i, f(x)))
def parmap(f, arg_list, config=None):
"""
Parallel mapping function. Use this function to parallelly map function
f onto arguments in arg_list. The maximum number of parallel threads is
taken from config.yaml:parallel_duke_processes.
Parameters
---------
f : function
python function with one argument
arg_list : list
list of arguments mapped to f
"""
if config is None:
config = get_config()
if config["parallel_duke_processes"]:
nprocs = min(multiprocessing.cpu_count(), config["process_limit"])
logger.info(f"Run process with {nprocs} parallel threads.")
q_in = multiprocessing.Queue(1)
q_out = multiprocessing.Queue()
proc = [
multiprocessing.Process(target=fun, args=(f, q_in, q_out))
for _ in range(nprocs)
]
for p in proc:
p.daemon = True
p.start()
sent = [q_in.put((i, x)) for i, x in enumerate(arg_list)]
[q_in.put((None, None)) for _ in range(nprocs)]
res = [q_out.get() for _ in range(len(sent))]
[p.join() for p in proc]
return [x for i, x in sorted(res)]
else:
return list(map(f, arg_list))
country_map = pd.read_csv(_package_data("country_codes.csv")).replace(
{"name": {"Czechia": "Czech Republic"}}
)
def country_alpha2(country):
"""
Convenience function for converting country name into alpha 2 codes
"""
if not isinstance(country, str):
return ""
try:
return pyc.countries.get(name=country).alpha_2
except (KeyError, AttributeError):
return ""
def convert_alpha2_to_country(df):
df = get_obj_if_Acc(df)
# codes that are not conform to ISO 3166-1 alpha2.
dic = {"EL": "GR", "UK": "GB"}
return convert_to_short_name(df.assign(Country=df.Country.replace(dic)))
def convert_to_short_name(df):
df = get_obj_if_Acc(df)
countries = df.Country.dropna().unique()
kwargs = dict(to="name_short", not_found=None)
short_name = dict(zip(countries, atleast_1d(cc.convert(countries, **kwargs))))
return df.assign(Country=df.Country.replace(short_name))
def convert_country_to_alpha2(df):
df = get_obj_if_Acc(df)
countries = df.Country.dropna().unique()
kwargs = dict(to="iso2", not_found=None)
iso2 = dict(zip(countries, atleast_1d(cc.convert(countries, **kwargs))))
return df.assign(Country=df.Country.replace(iso2).where(lambda ds: ds != "nan"))
def breakdown_matches(df):
"""
Function to inspect grouped and matched entries of a matched
dataframe. Breaks down to all ingoing data on detailed level.
Parameters
----------
df : pd.DataFrame
Matched data with not empty projectID-column. Keys of projectID must
be specified in powerplantmatching.data.data_config
"""
df = get_obj_if_Acc(df)
from . import data
assert "projectID" in df
if isinstance(df.projectID.iloc[0], list):
sources = [df.powerplant.get_name()]
single_source_b = True
else:
sources = df.projectID.apply(list).explode().unique()
single_source_b = False
sources = pd.concat(
[getattr(data, s)().set_index("projectID") for s in sources], sort=False
)
if df.index.nlevels > 1:
stackedIDs = df["projectID"].stack().apply(pd.Series).stack().dropna()
elif single_source_b:
stackedIDs = df["projectID"].apply(pd.Series).stack()
else:
stackedIDs = (
df["projectID"].apply(pd.Series).stack().apply(pd.Series).stack().dropna()
)
return (
sources.reindex(stackedIDs)
.set_axis(
stackedIDs.to_frame("projectID")
.set_index("projectID", append=True)
.droplevel(-2)
.index,
inplace=False,
)
.rename_axis(index=["id", "source", "projectID"])
)
def restore_blocks(df, mode=2, config=None):
"""
Restore blocks of powerplants from a matched dataframe.
This function breaks down all matches. For each match separately it selects
blocks from only one input data source.
For this selection the following modi are available:
1. Select the source with most number of blocks in the match
2. Select the source with the highest reliability score
Parameters
----------
df : pd.DataFrame
Matched data with not empty projectID-column. Keys of projectID must
be specified in powerplantmatching.data.data_config
"""
from .data import OPSD
df = get_obj_if_Acc(df)
assert "projectID" in df
config = get_config() if config is None else config
bd = breakdown_matches(df)
if mode == 1:
block_map = (
bd.reset_index(["source"])["source"]
.groupby(level="id")
.agg(lambda x: pd.Series(x).mode()[0])
)
blocks_i = pd.MultiIndex.from_frame(block_map.reset_index())
res = (
bd.reset_index("projectID")
.loc[blocks_i]
.set_index("projectID", append=True)
)
elif mode == 2:
sources = df.projectID.apply(list).explode().unique()
rel_scores = pd.Series(
{s: config[s]["reliability_score"] for s in sources}
).sort_values(ascending=False)
res = pd.DataFrame().rename_axis(index="id")
for s in rel_scores.index:
subset = bd.reindex(index=[s], level="source")
subset_i = subset.index.unique("id").difference(res.index.unique("id"))
res = pd.concat([res, subset.reindex(index=subset_i, level="id")])
else:
raise ValueError(f"Given `mode` must be either 1 or 2 but is: {mode}")
res = res.sort_index(level="id").reset_index(level=[0, 1])
# Now append Block information from OPSD German list:
df_blocks = (OPSD(rawDE_withBlocks=True).rename(columns={"name_bnetza": "Name"}))[
"Name"
]
res.update(df_blocks)
return res
def parse_Geoposition(
location, zipcode="", country="", use_saved_locations=False, saved_only=False
):
"""
Nominatim request for the Geoposition of a specific location in a country.
Returns a tuples with (latitude, longitude, country) if the request was
successful, returns np.nan otherwise.
ToDo: There exist further online sources for lat/long data which could be
used, if this one fails, e.g.
- Google Geocoding API
- Yahoo! Placefinder
- https://askgeo.com (??)
Parameters
----------
location : string
description of the location, can be city, area etc.
country : string
name of the country which will be used as a bounding area
use_saved_postion : Boolean, default False
Whether to firstly compare with cached results in
powerplantmatching/data/parsed_locations.csv
"""
import geopy.exc
from geopy.geocoders import GoogleV3 # ArcGIS Yandex Nominatim
if location is None or location == float:
return np.nan
alpha2 = country_alpha2(country)
try:
gdata = GoogleV3(api_key=get_config()["google_api_key"], timeout=10).geocode(
query=location,
components={"country": alpha2, "postal_code": str(zipcode)},
exactly_one=True,
)
except geopy.exc.GeocoderQueryError as e:
logger.warn(e)
gdata = None
if gdata is not None:
return pd.Series(
{
"Name": location,
"Country": country,
"lat": gdata.latitude,
"lon": gdata.longitude,
}
)
def fill_geoposition(
df,
use_saved_locations=True,
saved_only=True,
config=None,
):
"""
Fill missing 'lat' and 'lon' values. Uses geoparsing with the value given
in 'Name', limits the search through value in 'Country'.
df must contain 'Name', 'lat', 'lon' and 'Country' as columns.
Parameters
----------
df : pandas.DataFrame
DataFrame of power plants
use_saved_position : Boolean, default True
Whether to firstly compare with cached results in
powerplantmatching/data/parsed_locations.csv
saved_only: Boolean, default True
Whether to only add geo-positions which are stored at
`pm.core._package_data("parsed_locations.csv")`
"""
df = get_obj_if_Acc(df)
fn = _package_data("parsed_locations.csv")
if config is None:
config = get_config()
if not saved_only and config["google_api_key"] is None:
logger.warning(
"Geoparsing not possible as no google api key was "
"found, please add the key to your config.yaml if you "
"want to enable it."
)
saved_only = True
if use_saved_locations:
logger.info(f"Adding stored geo-position from {fn}")
locs = pd.read_csv(fn, index_col=[0, 1])
locs = locs[~locs.index.duplicated()]
df = df.where(
df[["lat", "lon"]].notnull().all(1),
df.drop(columns=["lat", "lon"]).join(locs, on=["Name", "Country"]),
)
if saved_only:
return df
logger.info("Parse geo-positions for missing `lat` and `lon` values")
missing = df[df.lat.isnull()].copy()
if "postalcode" not in missing.columns:
missing["postalcode"] = ""
cols = ["Name", "Country", "lat", "lon"]
geodata = pd.DataFrame(index=missing.index, columns=cols)
for i in tqdm(missing.index):
geodata.loc[i, :] = parse_Geoposition(
location=missing.at[i, "Name"],
zipcode=missing.at[i, "postalcode"],
country=missing.at[i, "Country"],
)
geodata.drop_duplicates(subset=["Name", "Country"]).set_index(
["Name", "Country"]
).to_csv(fn, mode="a", header=False)
df.loc[geodata.index, ["lat", "lon"]] = geodata
return df