Skip to content

Commit

Permalink
Shorten logging when reading meta indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Aug 24, 2023
1 parent f75a362 commit 7901ad1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
55 changes: 25 additions & 30 deletions pyam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
append_index_col,
)
from pyam.time import swap_time_for_year, swap_year_for_time
from pyam.logging import raise_data_error, deprecation_warning
from pyam.logging import raise_data_error, deprecation_warning, format_log_message
from pyam.validation import _exclude_on_fail

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -160,7 +160,6 @@ def _init(self, data, meta=None, index=DEFAULT_META_INDEX, **kwargs):

# read from file
if isinstance(data, Path):
data = Path(data) # casting str or LocalPath to Path
if not data.is_file():
raise FileNotFoundError(f"No such file: '{data}'")
logger.info(f"Reading file {data}")
Expand Down Expand Up @@ -2488,9 +2487,7 @@ def to_datapackage(self, path):
# return the package (needs to reloaded because `tmp` was deleted)
return Package(path)

def load_meta(
self, path, sheet_name="meta", ignore_conflict=False, *args, **kwargs
):
def load_meta(self, path, sheet_name="meta", ignore_conflict=False, **kwargs):
"""Load 'meta' indicators from file
Parameters
Expand All @@ -2510,39 +2507,37 @@ def load_meta(
path = path if isinstance(path, pd.ExcelFile) else Path(path)
meta = read_pandas(path, sheet_name=sheet_name, **kwargs)

# cast model-scenario column headers to lower-case (if necessary)
# 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]))

# check that required index columns exist
missing_cols = [c for c in self.index.names if c not in meta.columns]
if missing_cols:
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}"
)

# set index, filter to relevant scenarios from imported file
n = len(meta)
# skip import of meta indicators if no rows in meta
if not len(meta.index):
logger.warning(f"No scenarios found in sheet {sheet_name}")
return

# set index, check consistency between existing index and meta
meta.set_index(self.index.names, inplace=True)
meta = meta.loc[self.meta.index.intersection(meta.index)]

# skip import of meta indicators if np
if not n:
logger.info(f"No scenarios found in sheet {sheet_name}")
return
missing = self.index.difference(meta.index)
invalid = meta.index.difference(self.index)

msg = "Reading meta indicators"
# indicate if not all scenarios are included in the meta file
if len(meta) < len(self.meta):
i = len(self.meta)
msg += f" for {len(meta)} out of {i} scenario{s(i)}"

# indicate if more scenarios exist in meta file than in self
invalid = n - len(meta)
if invalid:
msg += f", ignoring {invalid} scenario{s(invalid)} from file"
logger.warning(msg)
else:
logger.info(msg)
if not missing.empty:
logger.warning(
format_log_message(
"No meta indicators for the following scenarios", missing
)
)
if not invalid.empty:
logger.warning(

Check warning on line 2535 in pyam/core.py

View check run for this annotation

Codecov / codecov/patch

pyam/core.py#L2535

Added line #L2535 was not covered by tests
format_log_message(
"Ignoring meta indicators for the following scenarios", invalid
)
)
meta = meta.loc[self.meta.index.intersection(meta.index)]

Check warning on line 2540 in pyam/core.py

View check run for this annotation

Codecov / codecov/patch

pyam/core.py#L2540

Added line #L2540 was not covered by tests

# in pyam < 2.0, an "exclude" columns was part of the `meta` attribute
# this section ensures compatibility with xlsx files created with pyam < 2.0
Expand Down
10 changes: 7 additions & 3 deletions pyam/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ def deprecation_warning(msg, item="This method", stacklevel=3):


def raise_data_error(msg, data):
"""Utils function to format error message from data formatting"""
"""Format error message with (head of) data table and raise"""
raise ValueError(format_log_message(msg, data))


def format_log_message(msg, data):
"""Utils function to format message with (head of) data table"""
if isinstance(data, pd.MultiIndex):
data = data.to_frame(index=False)
data = data.drop_duplicates()
msg = f"{msg}:\n{data.head()}" + ("\n..." if len(data) > 5 else "")
raise ValueError(msg)
return f"{msg}:\n{data.head()}" + ("\n..." if len(data) > 5 else "")

0 comments on commit 7901ad1

Please sign in to comment.