|
3 | 3 | read by semeio.fmudesign.DesignMatrix.generate |
4 | 4 | """ |
5 | 5 |
|
| 6 | +import logging |
6 | 7 | from collections import OrderedDict |
7 | 8 | from collections.abc import Mapping |
8 | 9 | from typing import Any |
|
12 | 13 | import pandas as pd |
13 | 14 | import yaml |
14 | 15 |
|
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
15 | 18 |
|
16 | 19 | def excel2dict_design( |
17 | 20 | input_filename: str, sheetnames: Mapping[str, Any] | None = None |
@@ -382,34 +385,34 @@ def _read_defaultvalues(filename: str, sheetname: str) -> OrderedDict[str, Any]: |
382 | 385 | Returns: |
383 | 386 | OrderedDict with defaultvalues (parameter, value) |
384 | 387 | """ |
385 | | - default_dict: OrderedDict[str, Any] = OrderedDict() |
386 | 388 | default_df = pd.read_excel( |
387 | 389 | filename, sheetname, header=0, index_col=0, engine="openpyxl" |
388 | 390 | ) |
| 391 | + |
389 | 392 | default_df.dropna(axis=0, how="all", inplace=True) |
390 | 393 | default_df = default_df.loc[ |
391 | 394 | :, ~default_df.columns.astype(str).str.contains("^Unnamed") |
392 | 395 | ] |
393 | 396 |
|
394 | | - # Strip spaces before and after parameter names, if they are there |
395 | | - # it is probably invisible user errors in Excel. |
396 | | - |
397 | | - default_df.index = pd.Index( |
398 | | - [ |
399 | | - paramname.strip() if isinstance(paramname, str) else paramname |
400 | | - for paramname in default_df.index |
401 | | - ] |
402 | | - ) |
403 | | - for row in default_df.itertuples(): |
404 | | - if str(row[0]) in default_dict: |
405 | | - print( |
406 | | - f"WARNING: The default value '{row[0]}' " |
407 | | - f"is listed twice in the sheet '{sheetname}'. " |
408 | | - "Only the first entry will be used in output file" |
| 397 | + if default_df.empty: |
| 398 | + return OrderedDict() |
| 399 | + |
| 400 | + # Strip leading/trailing spaces from parameter names such that |
| 401 | + # for example " PARAM" and "PARAM" are treated as duplicates. |
| 402 | + default_df.index = default_df.index.str.strip() |
| 403 | + |
| 404 | + # Check for duplicates and warn |
| 405 | + duplicates = default_df.index.duplicated(keep="first") |
| 406 | + if duplicates.any(): |
| 407 | + duplicate_names = default_df.index[duplicates].unique() |
| 408 | + for dup_name in duplicate_names: |
| 409 | + logger.warning( |
| 410 | + f"The default value '{dup_name}' is listed twice in the sheet " |
| 411 | + f"'{sheetname}'. Only the first entry will be used in output file" |
409 | 412 | ) |
410 | | - else: |
411 | | - default_dict[str(row[0])] = row[1] |
412 | | - return default_dict |
| 413 | + |
| 414 | + default_df = default_df[~duplicates] |
| 415 | + return OrderedDict(default_df.iloc[:, 0].to_dict()) |
413 | 416 |
|
414 | 417 |
|
415 | 418 | def _read_dependencies( |
|
0 commit comments