Skip to content

Commit

Permalink
small code updates (using pyupgrade)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause committed Apr 17, 2024
1 parent 8f3e071 commit 7fc3e0e
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 64 deletions.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# pyam documentation build configuration file, created by
# sphinx-quickstart on Tue Feb 9 09:59:03 2016.
Expand Down
2 changes: 1 addition & 1 deletion pyam/_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _op_data(df, name, method, axis, fillna=None, args=(), ignore_units=False, *
# separate pint quantities into numerical value and unit (as index)
if ignore_units is False:
_value = pd.DataFrame(
[[i.magnitude, "{:~}".format(i.units)] for i in result.values],
[[i.magnitude, f"{i.units:~}"] for i in result.values],
columns=["value", "unit"],
index=result.index,
).set_index("unit", append=True)
Expand Down
2 changes: 1 addition & 1 deletion pyam/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _get_standard_colors( # noqa: C901
cmap = colormap
colormap = cm.get_cmap(colormap)
if colormap is None:
raise ValueError("Colormap {0} is not recognized".format(cmap))
raise ValueError(f"Colormap {cmap} is not recognized")

Check warning on line 27 in pyam/_style.py

View check run for this annotation

Codecov / codecov/patch

pyam/_style.py#L27

Added line #L27 was not covered by tests
colors = [colormap(num) for num in np.linspace(0, 1, num=num_colors)]
elif color is not None:
if colormap is not None:
Expand Down
4 changes: 2 additions & 2 deletions pyam/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _aggregate_recursive(df, variable, recursive):
# iterate over variables (bottom-up) and aggregate all components up to `variable`
for d in reversed(range(find_depth(variable), max(find_depth(_df.variable)))):
components = compress(_df.variable, find_depth(_df.variable, level=d + 1))
var_list = set([reduce_hierarchy(v, -1) for v in components])
var_list = {reduce_hierarchy(v, -1) for v in components}

# a temporary dataframe allows to distinguish between full data and new data
_data_agg = _aggregate(_df, variable=var_list)
Expand Down Expand Up @@ -175,7 +175,7 @@ def _aggregate_time(df, variable, column, value, components, method="sum"):
"""Internal implementation for aggregating data over subannual time"""
# default `components` to all entries in `column` other than `value`
if components is None:
components = list(set(df.data.subannual.unique()) - set([value]))
components = list(set(df.data.subannual.unique()) - {value})

# compute aggregate over time
filter_args = dict(variable=variable)
Expand Down
10 changes: 5 additions & 5 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
logger = logging.getLogger(__name__)


class IamDataFrame(object):
class IamDataFrame:
"""Scenario timeseries data and meta indicators
The class provides a number of diagnostic features (including validation of
Expand Down Expand Up @@ -1781,7 +1781,7 @@ def _variable_components(self, variable, level=0):
include `'foo|bar|baz'`, which is a sub-sub-category. If `level=None`,
all variables below `variable` in the hierarchy are returned."""
var_list = pd.Series(self.variable)
return var_list[pattern_match(var_list, "{}|*".format(variable), level=level)]
return var_list[pattern_match(var_list, f"{variable}|*", level=level)]

def _get_cols(self, cols):
"""Return a list of columns of `self.data`"""
Expand Down Expand Up @@ -2445,7 +2445,7 @@ def to_excel(

# write meta table unless `include_meta=False`
if include_meta and len(self.meta.columns):
meta_rename = dict([(i, i.capitalize()) for i in self.index.names])
meta_rename = {i: i.capitalize() for i in self.index.names}
write_sheet(
excel_writer,
"meta" if include_meta is True else include_meta,
Expand Down Expand Up @@ -2501,7 +2501,7 @@ def to_datapackage(self, path):

# cast tables to datapackage
package = Package()
package.infer("{}/*.csv".format(tmp))
package.infer(f"{tmp}/*.csv")
if not package.valid:
logger.warning("The exported datapackage is not valid")
package.save(path)
Expand Down Expand Up @@ -2530,7 +2530,7 @@ def load_meta(self, path, sheet_name="meta", ignore_conflict=False, **kwargs):
meta = read_pandas(path, sheet_name=sheet_name, **kwargs)

# cast index-column headers to lower-case, check that required index exists
meta = meta.rename(columns=dict([(i.capitalize(), i) for i in META_IDX]))
meta = meta.rename(columns={i.capitalize(): i for i in META_IDX})
if missing_cols := [c for c in self.index.names if c not in meta.columns]:
raise ValueError(
f"Missing index columns for meta indicators: {missing_cols}"
Expand Down
8 changes: 3 additions & 5 deletions pyam/figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ def sankey(df, mapping):
_df = pd.DataFrame.from_dict(
mapping, orient="index", columns=["source", "target"]
).merge(df._data, how="left", left_index=True, right_on="variable")
label_mapping = dict(
[
(label, i)
label_mapping = {

Check warning on line 54 in pyam/figures.py

View check run for this annotation

Codecov / codecov/patch

pyam/figures.py#L54

Added line #L54 was not covered by tests
label: i
for i, label in enumerate(set(pd.concat([_df["source"], _df["target"]])))
]
)
}
_df.replace(label_mapping, inplace=True)
region = get_index_levels(_df, "region")[0]
unit = get_index_levels(_df, "unit")[0]
Expand Down
16 changes: 8 additions & 8 deletions pyam/iiasa.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def set_config(*args, **kwargs):

def _read_config(file):
"""Read username and password for IIASA API connection from file"""
with open(file, "r") as stream:
with open(file) as stream:
creds = yaml.safe_load(stream)

return ManagerAuth(**creds, url=str(settings.manager_url))
Expand Down Expand Up @@ -139,7 +139,7 @@ def __call__(self):
return {"Authorization": "Bearer " + self.access_token}


class Connection(object):
class Connection:
"""A class to facilitate querying an IIASA Scenario Explorer database API
Parameters
Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(self, name=None, creds=None, auth_url=_AUTH_URL):
logger.info("You are connected as an anonymous user")

@property
@lru_cache()
@lru_cache
def _connection_map(self):
# TODO: application-list will be reimplemented in conjunction with ixmp-server
r = self.auth.client.get("legacy/applications", headers=self.auth())
Expand All @@ -194,7 +194,7 @@ def _connection_map(self):
name = x["name"]
if env is not None:
if env in aliases:
logger.warning("Duplicate instance alias {}".format(env))
logger.warning(f"Duplicate instance alias {env}")

Check warning on line 197 in pyam/iiasa.py

View check run for this annotation

Codecov / codecov/patch

pyam/iiasa.py#L197

Added line #L197 was not covered by tests
conn_map[name] = name
first_duplicate = conn_map.pop(env)
conn_map[first_duplicate] = first_duplicate
Expand All @@ -206,7 +206,7 @@ def _connection_map(self):
return conn_map

@property
@lru_cache()
@lru_cache
def valid_connections(self):
"""Return available resources (database API connections)"""
logger.warning(
Expand Down Expand Up @@ -358,7 +358,7 @@ def properties(self, default_only=True, **kwargs):
cols = audit_cols + other_cols

_df = self._query_index(default_only, meta=True, cols=cols, **kwargs)
audit_mapping = dict([(i, i.replace("_", "ate_")) for i in audit_cols])
audit_mapping = {i: i.replace("_", "ate_") for i in audit_cols}

return _df.set_index(META_IDX).rename(columns=audit_mapping)

Expand All @@ -370,7 +370,7 @@ def scenarios(self):
"""List all scenarios in the connected resource"""
return pd.Series(self._query_index()["scenario"].unique(), name="scenario")

@lru_cache()
@lru_cache
def variables(self):
"""List all variables in the connected resource"""
url = "/".join([self._base_url, "ts"])
Expand All @@ -379,7 +379,7 @@ def variables(self):
df = pd.read_json(StringIO(r.text), orient="records")
return pd.Series(df["variable"].unique(), name="variable")

@lru_cache()
@lru_cache
def regions(self, include_synonyms=False):
"""List all regions in the connected resource
Expand Down
18 changes: 9 additions & 9 deletions pyam/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def pie(
if not isinstance(df, pd.DataFrame):
df = df.as_pandas()

for col in set(SORT_IDX) - set([category]):
for col in set(SORT_IDX) - {category}:
if len(df[col].unique()) > 1:
msg = (
"Can not plot multiple {}s in a pie plot with value={} and category={}"
Expand Down Expand Up @@ -436,7 +436,7 @@ def stack( # noqa: C901
if not isinstance(df, pd.DataFrame):
df = df.as_pandas()

for col in set(SORT_IDX) - set([x, stack]):
for col in set(SORT_IDX) - {x, stack}:
if len(df[col].unique()) > 1:
msg = "Can not plot multiple {}s in stack_plot with x={}, stack={}"
raise ValueError(msg.format(col, x, stack))
Expand Down Expand Up @@ -539,7 +539,7 @@ def as_series(index, name):
for var in ["model", "scenario", "region", "variable"]:
values = df[var].unique()
if len(values) == 1:
_title.append("{}: {}".format(var, values[0]))
_title.append(f"{var}: {values[0]}")
if title and _title:
title = " ".join(_title) if title is True else title
ax.set_title(title)
Expand Down Expand Up @@ -605,7 +605,7 @@ def bar( # noqa: C901
if not isinstance(df, pd.DataFrame):
df = df.as_pandas()

for col in set(SORT_IDX) - set([x, bars]):
for col in set(SORT_IDX) - {x, bars}:
if len(df[col].unique()) > 1:
msg = "Can not plot multiple {}s in bar plot with x={}, bars={}"
raise ValueError(msg.format(col, x, bars))
Expand Down Expand Up @@ -658,7 +658,7 @@ def bar( # noqa: C901
for var in ["model", "scenario", "region", "variable"]:
values = df[var].unique()
if len(values) == 1:
_title.append("{}: {}".format(var, values[0]))
_title.append(f"{var}: {values[0]}")
if title and _title:
title = " ".join(_title) if title is True else title
ax.set_title(title)
Expand Down Expand Up @@ -1008,9 +1008,9 @@ def line( # noqa: C901
# pivot data if asked for explicit variable name
variables = df["variable"].unique()
if x in variables or y in variables:
keep_vars = set([x, y]) & set(variables)
keep_vars = {x, y} & set(variables)
df = df[df["variable"].isin(keep_vars)]
idx = list(set(df.columns) - set(["value"]))
idx = list(set(df.columns) - {"value"})
df = (
df.reset_index()
.set_index(idx)
Expand Down Expand Up @@ -1038,7 +1038,7 @@ def line( # noqa: C901
# prepare a dict for ordering, reshape data for use in line_plot
idx_cols = list(df.columns.drop(y))
if not isinstance(order, dict):
order = dict([(i, None) for i in order or idx_cols])
order = {i: None for i in order or idx_cols}
df = reshape_mpl(df, x, y, idx_cols, **order)

# determine the columns that should go into the legend
Expand Down Expand Up @@ -1158,7 +1158,7 @@ def line( # noqa: C901

# build unique legend handles and labels
if legend is not False:
handles, labels = [np.array(i) for i in ax.get_legend_handles_labels()]
handles, labels = (np.array(i) for i in ax.get_legend_handles_labels())
if label is not None: # label given explicitly via kwarg
_add_legend(ax, handles, labels, legend)
else:
Expand Down
4 changes: 2 additions & 2 deletions pyam/run_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ def _get_path(self, key, fyaml, fname):
msg = (
"YAML key '{}' in {}: {} is not a valid relative " + "or absolute path"
)
raise IOError(msg.format(key, fyaml, fname))
raise OSError(msg.format(key, fyaml, fname))

Check warning on line 116 in pyam/run_control.py

View check run for this annotation

Codecov / codecov/patch

pyam/run_control.py#L116

Added line #L116 was not covered by tests
return _fname

def _load_yaml(self, obj):
if hasattr(obj, "read"): # it's a file
obj = obj.read()
if is_str(obj) and not os.path.exists(obj):
raise IOError("File {} does not exist".format(obj))
raise OSError(f"File {obj} does not exist")
if is_str(obj) and os.path.exists(obj):
fname = obj
with open(fname) as f:
Expand Down
22 changes: 11 additions & 11 deletions pyam/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pyam.utils import META_IDX, is_list_like


class Statistics(object):
class Statistics:
"""This class generates descriptive statistics of timeseries data
Parameters
Expand Down Expand Up @@ -46,9 +46,9 @@ def __init__( # noqa: C901
self.groupby = groupby
self.idx_depth = 2
elif groupby is not None:
raise ValueError("arg `{}` not valid `groupby`".format(groupby))
raise ValueError(f"arg `{groupby}` not valid `groupby`")

Check warning on line 49 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L49

Added line #L49 was not covered by tests
if self.col is not None and self.col not in df.meta.columns:
raise ValueError("column `{}` not in `df.meta`".format(self.col))
raise ValueError(f"column `{self.col}` not in `df.meta`")

Check warning on line 51 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L51

Added line #L51 was not covered by tests

# if neither groupby nor filters is given, use filters to describe all
# and assume that rows are used
Expand Down Expand Up @@ -79,11 +79,11 @@ def __init__( # noqa: C901
and is_str(idx[0])
or not is_str(idx[1])
):
raise ValueError("`{}` is not a valid index".format(idx))
raise ValueError(f"`{idx}` is not a valid index")

Check warning on line 82 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L82

Added line #L82 was not covered by tests
self._add_to_index(idx[0], idx[1])
# check that filters in tuple are valid
if not isinstance(_filter, dict):
raise ValueError("`{}` is not a valid filter".format(_filter))
raise ValueError(f"`{_filter}` is not a valid filter")

Check warning on line 86 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L86

Added line #L86 was not covered by tests
elif not (set(_filter) - set(META_IDX)).issubset(df.meta):
raise ValueError(
"column `{}` not in `df.meta`".format(
Expand All @@ -98,7 +98,7 @@ def __init__( # noqa: C901
self.percentiles = list(percentiles)
self._describe_cols = (
["count", "mean", "std", "min"]
+ ["{:.0%}".format(i) for i in self.percentiles]
+ [f"{i:.0%}" for i in self.percentiles]
+ ["max"]
)

Expand All @@ -112,10 +112,10 @@ def _add_to_index(self, idx, sub_idx=None):
raise ValueError(msg.format(idx, "(idx0, idx1)"))
if self.idx_depth == 1 and sub_idx is not None:
raise ValueError(
"index depth set to 1, found `({}, {})`".format(idx, sub_idx)
f"index depth set to 1, found `({idx}, {sub_idx})`"
)
if self.idx_depth == 2 and sub_idx is None:
raise ValueError("index depth set to 2, found `({})`".format(idx))
raise ValueError(f"index depth set to 2, found `({idx})`")

Check warning on line 118 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L118

Added line #L118 was not covered by tests

# append to lists for sorting index
if idx not in self._idx:
Expand Down Expand Up @@ -292,7 +292,7 @@ def format_rows(
count = max(
[i for i in row.loc[(slice(None), slice(None), "count")] if not np.isnan(i)]
)
ret.loc[("count", "")] = ("{:.0f}".format(count)) if count > 1 else ""
ret.loc[("count", "")] = (f"{count:.0f}") if count > 1 else ""

# set upper and lower for the range
upper, lower = ("max", "min") if fullrange is True else ("75%", "25%")
Expand All @@ -308,10 +308,10 @@ def format_rows(
x[center], x[upper], x[lower]
)
elif _count == 1:
s = "{f}".format(f=custom_format).format(x["50%"])
s = f"{custom_format}".format(x["50%"])

Check warning on line 311 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L311

Added line #L311 was not covered by tests
# add count of this section as `[]` if different from count_max
if 0 < _count < count:
s += " [{:.0f}]".format(_count)
s += f" [{_count:.0f}]"

Check warning on line 314 in pyam/statistics.py

View check run for this annotation

Codecov / codecov/patch

pyam/statistics.py#L314

Added line #L314 was not covered by tests
ret.loc[i] = s

return ret
2 changes: 1 addition & 1 deletion pyam/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test(x):
return level <= x if x is not None else False

else:
raise ValueError("Unknown level type: `{}`".format(level))
raise ValueError(f"Unknown level type: `{level}`")

return list(map(test, n_pipes))

Expand Down
2 changes: 1 addition & 1 deletion pyam/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def cross_threshold(
Whether to cast the returned values to integer (years)
"""
direction = [direction] if is_str(direction) else list(direction)
if not set(direction).issubset(set(["from above", "from below"])):
if not set(direction).issubset({"from above", "from below"}):
raise ValueError(f"Invalid direction: {direction}")

# get the values and time-domain index
Expand Down
2 changes: 1 addition & 1 deletion pyam/unfccc.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _compile_variable(i, variable):
"""Translate UNFCCC columns into an IAMC-style variable"""
if i["variable"]:
raise ValueError("Conflict in variable mapping.")
return variable.format(**dict((c, i[c]) for c in NAME_COLS))
return variable.format(**{c: i[c] for c in NAME_COLS})

Check warning on line 139 in pyam/unfccc.py

View check run for this annotation

Codecov / codecov/patch

pyam/unfccc.py#L139

Added line #L139 was not covered by tests


def _compile_unit(i):
Expand Down
2 changes: 1 addition & 1 deletion pyam/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def convert_unit(
registry = registry or iam_units.registry

# Make versions without -equiv
_current, _to = [i.replace("-equiv", "") for i in [current, to]]
_current, _to = (i.replace("-equiv", "") for i in [current, to])
# Pair of (magnitude, unit)
qty = [ret._data.loc[where].values, _current]

Expand Down
2 changes: 1 addition & 1 deletion pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
range(0, 702),
[i for i in string.ascii_uppercase]
+ [
"{}{}".format(i, j)
f"{i}{j}"
for i, j in itertools.product(
string.ascii_uppercase, string.ascii_uppercase
)
Expand Down

0 comments on commit 7fc3e0e

Please sign in to comment.