Skip to content
Draft
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
85 changes: 41 additions & 44 deletions pandapower/convert_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ def _restore_index_names(net):
"""Restores dataframes index names stored as dictionary. With newer pp to_json() this
information is stored to the dataframe its self.
"""
if "index_names" in net.keys():
if "index_names" in net:
if not isinstance(net["index_names"], dict):
raise ValueError("To restore the index names of the dataframes, a dict including this "
f"information is expected, not {type(net['index_names'])}")
for key, index_name in net["index_names"].items():
if key in net.keys():
if key in net:
net[key].index.name = index_name
del net["index_names"]

Expand All @@ -147,14 +147,14 @@ def correct_dtypes(net, error):
failed = {}
for key, table in empty_net.items():
if isinstance(table, pd.DataFrame):
if key in net.keys() and isinstance(net[key], pd.DataFrame):
if key in net and isinstance(net[key], pd.DataFrame):
cols = table.columns.intersection(net[key].columns)
diff_cols = cols[~(table.dtypes.loc[cols] == net[key].dtypes.loc[cols])]
for col in diff_cols:
try:
net[key][col] = net[key][col].astype(table[col].dtype)
except ValueError:
if key not in failed.keys():
if key not in failed:
failed[key] = [col]
else:
failed[key].append(col)
Expand Down Expand Up @@ -185,19 +185,19 @@ def _convert_trafo_controller_parameter_names(net):
controller = net.controller.at[ctrl_idx, "object"]
if issubclass(type(controller), TrafoController):

if "tid" in controller.__dict__.keys():
if "tid" in controller.__dict__:
controller.__dict__["element_index"] = controller.__dict__.pop("tid")
elif "transformer_index" in controller.__dict__.keys():
elif "transformer_index" in controller.__dict__:
controller.__dict__["element_index"] = controller.__dict__.pop("transformer_index")

if "trafotable" in controller.__dict__.keys():
if "trafotable" in controller.__dict__:
controller.__dict__["element"] = controller.__dict__.pop("trafotable")
if "trafotype" in controller.__dict__.keys():
if "trafotype" in controller.__dict__:
del controller.__dict__["trafotype"]
elif "trafotype" in controller.__dict__.keys():
elif "trafotype" in controller.__dict__:
controller.__dict__["element"] = controller.__dict__.pop("trafotype")

if "controlled_bus" in controller.__dict__.keys():
if "controlled_bus" in controller.__dict__:
controller.__dict__["trafobus"] = controller.__dict__.pop("controlled_bus")


Expand Down Expand Up @@ -266,10 +266,8 @@ def _add_nominal_power(net):

def _add_missing_tables(net):
net_new = create_empty_network()
for key in net_new.keys():
if key.startswith("_empty_res"):
net[key] = net_new[key]
elif key not in net.keys():
for key in net_new:
if key.startswith("_empty_res") or key not in net:
net[key] = net_new[key]


Expand Down Expand Up @@ -351,7 +349,7 @@ def _rename_columns(net, elements_to_deserialize):
if "controller" in net:
net["controller"] = net["controller"].rename(columns={"controller": "object"})

if _check_elements_to_deserialize('res_line_3ph', elements_to_deserialize):
if 'res_line_3ph' in net and _check_elements_to_deserialize('res_line_3ph', elements_to_deserialize):
if "p_a_l_mw" in net.res_line_3ph:
net['res_line_3ph'] = net['res_line_3ph'].rename(columns={
'p_a_l_mw': 'pl_a_mw',
Expand All @@ -362,7 +360,7 @@ def _rename_columns(net, elements_to_deserialize):
'q_c_l_mvar': 'ql_c_mvar',
})

if _check_elements_to_deserialize('res_trafo_3ph', elements_to_deserialize):
if 'res_trafo_3ph' in net and _check_elements_to_deserialize('res_trafo_3ph', elements_to_deserialize):
if "p_a_l_mw" in net.res_trafo_3ph:
net['res_trafo_3ph'] = net['res_trafo_3ph'].rename(columns={
'p_a_l_mw': 'pl_a_mw',
Expand All @@ -373,20 +371,19 @@ def _rename_columns(net, elements_to_deserialize):
'q_c_l_mvar': 'ql_c_mvar',
})

if "options" in net:
if "recycle" in net["options"]:
if "Ybus" in net["options"]["recycle"]:
if net["options"]["recycle"]["Ybus"]:
net["options"]["recycle"]["trafo"] = False
del net["options"]["recycle"]["Ybus"]
else:
net["options"]["recycle"]["trafo"] = True
if "ppc" in net["options"]["recycle"]:
if net["options"]["recycle"]["ppc"]:
net["options"]["recycle"]["bus_pq"] = False
del net["options"]["recycle"]["ppc"]
else:
net["options"]["recycle"]["bus_pq"] = True
if "options" in net and "recycle" in net["options"]:
if "Ybus" in net["options"]["recycle"]:
if net["options"]["recycle"]["Ybus"]:
net["options"]["recycle"]["trafo"] = False
del net["options"]["recycle"]["Ybus"]
else:
net["options"]["recycle"]["trafo"] = True
if "ppc" in net["options"]["recycle"]:
if net["options"]["recycle"]["ppc"]:
net["options"]["recycle"]["bus_pq"] = False
del net["options"]["recycle"]["ppc"]
else:
net["options"]["recycle"]["bus_pq"] = True


def _add_missing_columns(net, elements_to_deserialize):
Expand Down Expand Up @@ -495,16 +492,16 @@ def _add_missing_columns(net, elements_to_deserialize):
net.switch['in_ka'] = np.nan

# Update the switch table with 'in_ka'
if _check_elements_to_deserialize('res_switch', elements_to_deserialize) and \
'p_from_mw' not in net.res_switch:
net.res_switch['p_from_mw'] = np.nan
net.res_switch['q_from_mvar'] = np.nan
net.res_switch['p_to_mw'] = np.nan
net.res_switch['q_to_mvar'] = np.nan
if ('res_switch' in net and _check_elements_to_deserialize('res_switch', elements_to_deserialize) and
'p_from_mw' not in net.res_switch):
net.res_switch['p_from_mw'] = np.nan
net.res_switch['q_from_mvar'] = np.nan
net.res_switch['p_to_mw'] = np.nan
net.res_switch['q_to_mvar'] = np.nan

# Update the switch table with 'in_ka'
if _check_elements_to_deserialize('res_switch_est', elements_to_deserialize) and \
'p_from_mw' not in net.res_switch_est:
if ('res_switch_est' in net and _check_elements_to_deserialize('res_switch_est', elements_to_deserialize) and
'p_from_mw' not in net.res_switch_est):
net.res_switch_est['p_from_mw'] = np.nan
net.res_switch_est['q_from_mvar'] = np.nan
net.res_switch_est['p_to_mw'] = np.nan
Expand Down Expand Up @@ -538,8 +535,8 @@ def _add_missing_columns(net, elements_to_deserialize):
"slack_weight" not in net.xward:
net.xward['slack_weight'] = 0.0

if _check_elements_to_deserialize('res_line_3ph', elements_to_deserialize) and \
"p_c_from_mw" not in net.res_line_3ph:
if ('res_line_3ph' in net and _check_elements_to_deserialize('res_line_3ph', elements_to_deserialize) and
"p_c_from_mw" not in net.res_line_3ph):
net.res_line_3ph['p_c_from_mw'] = np.nan
net.res_line_3ph['loading_a_percent'] = np.nan
net.res_line_3ph['loading_b_percent'] = np.nan
Expand All @@ -548,8 +545,8 @@ def _add_missing_columns(net, elements_to_deserialize):

def _update_trafo_type_parameter_names(net):
for element in ('trafo', 'trafo3w'):
for type in net.std_types[element].keys():
keys = {col: _update_column(col) for col in net.std_types[element][type].keys() if
for type in net.std_types[element]:
keys = {col: _update_column(col) for col in net.std_types[element][type] if
col.startswith("tp") or col.startswith("vsc")}
for old_key, new_key in keys.items():
net.std_types[element][type][new_key] = net.std_types[element][type].pop(old_key)
Expand Down Expand Up @@ -592,7 +589,7 @@ def _set_data_type_of_columns(net):

def _convert_to_mw(net):
replace = [("kw", "mw"), ("kvar", "mvar"), ("kva", "mva")]
for element in net.keys():
for element in net:
if isinstance(net[element], pd.DataFrame):
for old, new in replace:
diff = {column: column.replace(old, new) for column in net[element].columns if
Expand Down Expand Up @@ -659,7 +656,7 @@ def _convert_objects(net, elements_to_deserialize):
"""
_check_elements_to_deserialize('controller', elements_to_deserialize)
if _check_elements_to_deserialize('controller', elements_to_deserialize) and \
"controller" in net.keys():
"controller" in net:
for obj in net["controller"].object.values:
_update_object_attributes(obj)

Expand Down
4 changes: 2 additions & 2 deletions pandapower/converter/pypower/from_ppc.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ def validate_from_ppc(ppc, net, max_diff_values={"bus_vm_pu": 1e-6, "bus_va_degr
raise ValueError(
"net._from_ppc_lookups must contain a lookup (dict of keys 'branch' and 'gen')")

if net.res_bus.shape[0] == 0 and net.bus.shape[0] > 0:
logger.debug("runpp() is performed by validate_from_ppc() since res_bus is empty.")
if "res_bus" not in net or net.res_bus.shape[0] == 0 and net.bus.shape[0] > 0:
logger.debug("runpp() is performed by validate_from_ppc() since res_bus is not created or is empty.")
runpp(net, calculate_voltage_angles=True, trafo_model="pi")

# --- pypower powerflow results -> ppc_res -----------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions pandapower/estimation/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ def _extract_result_ppci_to_pp(net, ppc, ppci):
net["res_bus_est"].loc[bus,"q_mvar"] += Sinj.imag
if element == "shunt":
element_res_est = "res_" + element + "_est"
net[element_res_est]["p_mw"] = Sinj.real
net[element_res_est]["q_mvar"] = Sinj.imag
net[element_res_est]["vm_pu"] = net["res_bus_est"].loc[bus,"vm_pu"].values
if element_res_est in net:
net[element_res_est]["p_mw"] = Sinj.real
net[element_res_est]["q_mvar"] = Sinj.imag
net[element_res_est]["vm_pu"] = net["res_bus_est"].loc[bus,"vm_pu"].values
return net


Expand Down
33 changes: 17 additions & 16 deletions pandapower/estimation/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def add_virtual_meas_from_loadflow(net, v_std_dev=0.01, p_std_dev=0.03, q_std_de
'trafo3w': {'side': ('hv', 'mv', 'lv'),
'meas_type': ('p_mw', 'q_mvar')}}
for bus_ix, bus_res in net.res_bus.iterrows():
for meas_type in bus_meas_types.keys():
for meas_type in bus_meas_types:
meas_value = float(bus_res[bus_meas_types[meas_type]])
if meas_type in ('p', 'q'):
create_measurement(net, meas_type=meas_type, element_type='bus', element=bus_ix,
Expand All @@ -161,8 +161,8 @@ def add_virtual_meas_from_loadflow(net, v_std_dev=0.01, p_std_dev=0.03, q_std_de
remove_shunt_injection_from_meas(net,"shunt")
remove_shunt_injection_from_meas(net,"ward")

for br_type in branch_meas_type.keys():
if not net['res_' + br_type].empty:
for br_type in branch_meas_type:
if f'res_{br_type}' in net and not net['res_' + br_type].empty:
for br_ix, br_res in net['res_' + br_type].iterrows():
for side in branch_meas_type[br_type]['side']:
for meas_type in branch_meas_type[br_type]['meas_type']:
Expand All @@ -173,7 +173,7 @@ def add_virtual_meas_from_loadflow(net, v_std_dev=0.01, p_std_dev=0.03, q_std_de
add_virtual_meas_error(net, v_std_dev=v_std_dev, p_std_dev=p_std_dev, q_std_dev=q_std_dev,
with_random_error=with_random_error)

def remove_shunt_injection_from_meas(net,type):
def remove_shunt_injection_from_meas(net, type):
index = net[type].index.tolist()
bus = net[type]["bus"].tolist()
for k in range(len(index)):
Expand Down Expand Up @@ -203,19 +203,20 @@ def add_virtual_pmu_meas_from_loadflow(net, v_std_dev=0.001, i_std_dev=0.1,
'meas_type': ('i_ka', 'ia_degree', 'p_mw', 'q_mvar')}}

# Added degree result for branches
for br_type in branch_meas_type.keys():
for br_type in branch_meas_type:
for side in branch_meas_type[br_type]['side']:
p, q, vm, va = net["res_" + br_type]["p_%s_mw" % side].values, \
net["res_" + br_type]["q_%s_mvar" % side].values, \
net["res_" + br_type]["vm_%s_pu" % side].values, \
net["res_" + br_type]["va_%s_degree" % side].values
S = p + q * 1j
V = vm * np.exp(np.deg2rad(va) * 1j)
I = np.conj(S / V)
net["res_" + br_type]["ia_%s_degree" % side] = np.rad2deg(np.angle(I))
if "res_" + br_type in net:
p, q, vm, va = net["res_" + br_type]["p_%s_mw" % side].values, \
net["res_" + br_type]["q_%s_mvar" % side].values, \
net["res_" + br_type]["vm_%s_pu" % side].values, \
net["res_" + br_type]["va_%s_degree" % side].values
S = p + q * 1j
V = vm * np.exp(np.deg2rad(va) * 1j)
I = np.conj(S / V)
net["res_" + br_type]["ia_%s_degree" % side] = np.rad2deg(np.angle(I))

for bus_ix, bus_res in net.res_bus.iterrows():
for meas_type in bus_meas_types.keys():
for meas_type in bus_meas_types:
meas_value = float(bus_res[bus_meas_types[meas_type]])
if meas_type in ('p', 'q'):
create_measurement(net, meas_type=meas_type, element_type='bus', element=bus_ix,
Expand All @@ -226,8 +227,8 @@ def add_virtual_pmu_meas_from_loadflow(net, v_std_dev=0.001, i_std_dev=0.1,
remove_shunt_injection_from_meas(net,"shunt")
remove_shunt_injection_from_meas(net,"ward")

for br_type in branch_meas_type.keys():
if not net['res_' + br_type].empty:
for br_type in branch_meas_type:
if 'res_' + br_type in net and not net['res_' + br_type].empty:
for br_ix, br_res in net['res_' + br_type].iterrows():
for side in branch_meas_type[br_type]['side']:
for meas_type in branch_meas_type[br_type]['meas_type']:
Expand Down
13 changes: 7 additions & 6 deletions pandapower/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,24 +354,24 @@ def from_json_string(
empty_dict_like_object=empty_dict_like_object,
ignore_unknown_objects=ignore_unknown_objects)
net_dummy = create_empty_network()
if ('version' not in net.keys()) | (Version(net.version) < Version('2.1.0')):
if ('version' not in net) | (Version(net.version) < Version('2.1.0')):
raise UserWarning('table selection is only possible for nets above version 2.0.1. '
'Convert and save your net first.')
if keep_serialized_elements:
for key in elements_to_deserialize:
net[key] = json.loads(net[key], cls=PPJSONDecoder)
else:
if (('version' not in net.keys()) or (net['version'] != net_dummy.version)) and \
if (('version' not in net) or (net['version'] != net_dummy.version)) and \
not convert:
raise UserWarning(
'The version of your net %s you are trying to load differs from the actual '
'pandapower version %s. Before you can load only distinct tables, convert '
'and save your net first or set convert to True!'
% (net['version'], net_dummy.version))
for key in net.keys():
for key in net:
if key in elements_to_deserialize:
net[key] = json.loads(net[key], cls=PPJSONDecoder)
elif not isinstance(net[key], str):
elif not isinstance(net[key], str) or "res_" in key:
continue
elif 'pandas' in net[key]:
net[key] = net_dummy[key]
Expand Down Expand Up @@ -422,11 +422,12 @@ def from_json_dict(json_dict):
for par, value in json_dict["parameters"]["parameter"].items():
net[par] = value

for key in sorted(json_dict.keys()):
for key in sorted(json_dict):
if key == 'dtypes':
continue
if key in net and isinstance(net[key], pd.DataFrame) and isinstance(json_dict[key], dict) \
or key == "piecewise_linear_cost" or key == "polynomial_cost":
or key == "piecewise_linear_cost" or key == "polynomial_cost" \
or (key.startswith("res_") and key[4:] in net and isinstance(net[key[4:]], pd.DataFrame)):
net[key] = pd.DataFrame.from_dict(json_dict[key], orient="columns")
net[key].set_index(net[key].index.astype(numpy.int64), inplace=True)
else:
Expand Down
5 changes: 3 additions & 2 deletions pandapower/grid_equivalents/auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def drop_assist_elms_by_creating_ext_net(net, elms=None):
target_elm_idx = net[elm].index[net[elm].name.astype(str).str.contains(
"assist_" + elm, na=False, regex=False)]
net[elm] = net[elm].drop(target_elm_idx)
if net["res_" + elm].shape[0]:
if f"res_{elm}" in net and net["res_" + elm].shape[0]:
res_target_elm_idx = net["res_" +
elm].index.intersection(target_elm_idx)
net["res_" + elm] = net["res_" + elm].drop(res_target_elm_idx)
Expand Down Expand Up @@ -526,7 +526,8 @@ def replace_motor_by_load(net, all_external_buses):
q = q_mvar if not np.isnan(net.res_bus.vm_pu[m.bus]) and m.in_service else 0.0
net.res_load.loc[li] = p, q
net.motor = net.motor.drop(motors)
net.res_motor = net.res_motor.drop(motors)
if "res_motor" in net:
net.res_motor = net.res_motor.drop(motors)


if __name__ == "__main__":
Expand Down
Loading
Loading