Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
znicholls committed Mar 29, 2020
1 parent 1f749f2 commit cd2840e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
41 changes: 26 additions & 15 deletions src/silicone/multiple_infillers/infill_composite_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def infill_composite_values(df, composite_dic=None):
Key: The variable names of the composite. Value: The variable names of the
constituents, which may include wildcards ('*'). Optionally, these may be
dictionaries of the names to factors, which we multiply the numbers by before
summing them. Defaults to a list of PFC, HFC, F-Gases, CO2 and Kyoto gases.
summing them. Defaults to a list of PFC, HFC, F-Gases, CO2 and Kyoto gases.
Returns
-------
:obj:`pyam.IamDataFrame`
:obj:`pyam.IamDataFrame` containing the composite values.
"""
if not composite_dic:
composite_dic = {
Expand All @@ -41,39 +46,45 @@ def infill_composite_values(df, composite_dic=None):
"Emissions|F-Gases",
],
}

# Do a simple test to ensure we have any relevant data. This does not prevent
# problems from missing subsets of data.
to_delete = []

# Disable core logging to prevent warnings about empty filters
logging.getLogger("pyam.core").setLevel(logging.CRITICAL)
for composite in composite_dic.keys():
if df.filter(variable=composite_dic.get(composite)).data.empty:
logger.warning("No data found for {}".format(composite_dic.get(composite)))
for composite, composite_variables in composite_dic.items():
if df.filter(variable=composite_variables).data.empty:
logger.warning("No data found for {}".format(composite_variables))
to_delete.append(composite)

logging.getLogger("pyam.core").setLevel(logging.WARNING)
[composite_dic.pop(x) for x in to_delete]
for x in to_delete:
composite_dic.pop(x)

# If the composite variables already exist, remove them first
df = df.filter(variable=composite_dic.keys(), keep=False)
for composite in composite_dic.keys():
if type(composite_dic.get(composite)) is dict:
for composite, composite_variables in composite_dic.items():
if isinstance(composite_variables, dict):
temp_df = df.filter(
variable=[composite] + list(composite_dic.get(composite).keys())
variable=[composite] + list(composite_variables.keys())
)
for factor in composite_dic.get(composite).keys():
for contributor, factor in composite_variables.items():
temp_df.data["value"].loc[
temp_df.data["variable"] == factor
] = temp_df.data["value"].loc[
temp_df.data["variable"] == factor
] * composite_dic.get(composite).get(factor)
temp_df.data["variable"] == contributor
] *= factor

composite_df = _construct_consistent_values(
composite, composite_dic.get(composite), temp_df
composite, composite_variables, temp_df
)

else:
composite_df = _construct_consistent_values(
composite, composite_dic.get(composite), df
composite, composite_variables, df
)
try:
to_return.append(composite_df)
except NameError:
to_return = composite_df

return to_return
30 changes: 15 additions & 15 deletions src/silicone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,25 +454,25 @@ def _adjust_time_style_to_match(in_df, target_df):

def _construct_consistent_values(aggregate_name, components, db_to_generate):
"""
Calculates the sum of the components and creates an IamDataFrame with this
value under variable type `aggregate_name`.
Calculates the sum of the components and creates an IamDataFrame with this
value under variable type `aggregate_name`.
Parameters
----------
aggregate_name : str
The name of the aggregate variable.
Parameters
----------
aggregate_name : str
The name of the aggregate variable.
components : [str]
List of the names of the variables to be summed.
components : [str]
List of the names of the variables to be summed.
db_to_generate : :obj:`pyam.IamDataFrame`
Input data from which to construct consistent values.
db_to_generate : :obj:`pyam.IamDataFrame`
Input data from which to construct consistent values.
Return
------
:obj:`pyam.IamDataFrame`
Consistently calculated aggregate data.
"""
Returns
-------
:obj:`pyam.IamDataFrame`
Consistently calculated aggregate data.
"""
assert (
aggregate_name not in db_to_generate.variables().values
), "We already have a variable of this name"
Expand Down

0 comments on commit cd2840e

Please sign in to comment.