Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions matpowercaseframes/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
"map": ["IFNUM", "BRANCHIDX"],
"lims": ["IFNUM", "LOWER", "UPPER"],
},
"gencost": ["MODEL", "STARTUP", "SHUTDOWN", "NCOST", "COST"],
"dclinecost": ["MODEL", "STARTUP", "SHUTDOWN", "NCOST", "COST"],
"gencost": ["MODEL", "STARTUP", "SHUTDOWN", "NCOST"],
"dclinecost": ["MODEL", "STARTUP", "SHUTDOWN", "NCOST"],
"bus_name": ["BUS_NAME"],
"branch_name": ["BRANCH_NAME"],
"gen_name": ["GEN_NAME"],
Expand Down
37 changes: 30 additions & 7 deletions matpowercaseframes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import copy
import os
import warnings

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -209,9 +210,8 @@ def _get_path(path):
if os.path.isfile(path_added_matpower_m):
return path_added_matpower_m

# Create detailed error message
error_msg = f"Could not find file or directory '{path}'."
raise FileNotFoundError(error_msg)
message = f"Can't find data at {os.path.abspath(path)}"
raise FileNotFoundError(message)

def _read_matpower(self, filepath, allow_any_keys=False):
"""
Expand Down Expand Up @@ -433,17 +433,40 @@ def _get_dataframe(self, attribute, data, n_cols=None, columns_template=None):
)

columns = columns_template[:n_cols]

# special case for gencost and dclinecost
if n_cols > len(columns):
if attribute not in ("gencost", "dclinecost"):
msg = (
f"Number of columns in {attribute} ({n_cols}) is greater"
f" than the expected number."
)
raise IndexError(msg)
columns = columns[:-1] + [
"{}_{}".format(columns[-1], i)
for i in range(n_cols - len(columns), -1, -1)
]
NCOST = n_cols - len(columns)
# Warning if mixed models exist
gencost_models = data[:, 0]
first_row_model = int(gencost_models[0]) # TODO: support mixed models
unique_models = np.unique(gencost_models).astype(int)

if len(unique_models) > 1:
warnings.warn(
f"Mixed cost models detected in {attribute}: "
f"{unique_models.tolist()}. "
f"Using model type {first_row_model} from first row. "
"Mixed models are not fully supported.",
UserWarning,
stacklevel=2,
)

if first_row_model == 1: # PW_LINEAR
ncost_cols = [
f"{prefix}{i}"
for i in range(1, (NCOST // 2) + 1)
for prefix in ("X", "Y")
]
columns = columns + ncost_cols
else: # POLYNOMIAL
columns = columns + [f"C{i}" for i in range(NCOST - 1, -1, -1)]

return pd.DataFrame(data, columns=columns)

Expand Down
8 changes: 6 additions & 2 deletions matpowercaseframes/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
def int_else_float_except_string(s):
try:
f = float(s.replace(",", "."))
i = int(f)
return i if i == f else f
if f.is_integer():
try:
return int(f)
except (OverflowError, ValueError):
return f
return f
except ValueError:
return s
2 changes: 1 addition & 1 deletion notebooks/compare_load.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down
Loading