Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: test for Grid equality #152

Merged
merged 2 commits into from
Apr 23, 2020
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
73 changes: 72 additions & 1 deletion powersimdata/input/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from powersimdata.input.usa_tamu_model import TAMU
from powersimdata.input.mat_reader import REISEMATReader
from powersimdata.input.grid_fields \
import Branch, Bus, DCLine, GenCost, Plant, Storage, Sub
import AbstractGridField, Branch, Bus, DCLine, GenCost, Plant, Storage, Sub


class Grid(object):
Expand Down Expand Up @@ -117,6 +117,77 @@ def __getitem__(self, field_name):
except KeyError as e:
print(e)

def __eq__(self, other):
"""Used when 'self == other' is evaluated.
:param object other: other object to be compared against.
:return: (*bool*).
"""
def _univ_eq(ref, test):
"""Check for {boolean, dataframe, or column data} equality.
:param object ref: one object to be tested (order does not matter).
:param object test: another object to be tested.
:raises AssertionError: if no equality can be confirmed.
"""
try:
test_eq = ref == test
if isinstance(test_eq, (bool, dict)):
assert test_eq
else:
assert test_eq.all().all()
except ValueError:
assert set(ref.columns) == set(test.columns)
for col in ref.columns:
assert (ref[col] == test[col]).all()

if not isinstance(other, Grid):
err_msg = 'Unable to compare Grid & %s' % type(other).__name__
raise NotImplementedError(err_msg)
assert self.fields.keys() == other.fields.keys()
assert self.transform.keys() == other.transform.keys()
# Check all AbstractGridField attributes
try:
for k, v in self.fields.items():
if isinstance(v, GenCost):
# Comparing 'after' will fail if one Grid was linearized
self_data = self.fields[k].data['before']
other_data = other.fields[k].data['before']
_univ_eq(self_data, other_data)
elif isinstance(v, Storage):
self_storage_num = len(self.fields[k].data['gencost'])
other_storage_num = len(other.fields[k].data['gencost'])
if self_storage_num == 0:
assert other_storage_num == 0
continue
# These are dicts, so we need to go one level deeper
self_keys = self.fields[k].data.keys()
other_keys = other.fields[k].data.keys()
assert self_keys == other_keys
for subkey in self_keys:
self_data = self.fields[k].data[subkey]
other_data = other.fields[k].data[subkey]
_univ_eq(self_data, other_data)
elif isinstance(v, Bus):
# MOST changes BUS_TYPE for buses with DC Lines attached
self_df = self.fields[k].data.drop('type', axis=1)
other_df = other.fields[k].data.drop('type', axis=1)
_univ_eq(self_df, other_df)
elif isinstance(v, Plant):
# REISE does some modifications to Plant data
excluded_cols = ['status', 'Pmin', 'ramp_10', 'ramp_30']
self_df = self.fields[k].data.drop(excluded_cols, axis=1)
other_df = other.fields[k].data.drop(excluded_cols, axis=1)
_univ_eq(self_df, other_df)
elif isinstance(v, AbstractGridField):
_univ_eq(self.fields[k].data, other.fields[k].data)
else:
_univ_eq(self.fields[k], other.fields[k])
# Check the transform attributes
for k, v in self.transform.items():
_univ_eq(self.transform[k], other.transform[k])
except:
return False
return True


def get_type2color():
"""Defines generator type to generator color mapping. Used for plotting.
Expand Down
2 changes: 1 addition & 1 deletion powersimdata/input/mat_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def column_name_provider():
'name', 'interconnect_sub_id', 'lat', 'lon', 'interconnect']
col_name_bus = [
'bus_id', 'type', 'Pd', 'Qd', 'Gs', 'Bs', 'zone_id', 'Vm', 'Va',
'loss_zone', 'baseKV', 'Vmax', 'Vmin', 'lam_P', 'lam_Q', 'mu_Vmax',
'baseKV', 'loss_zone', 'Vmax', 'Vmin', 'lam_P', 'lam_Q', 'mu_Vmax',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mislabelled these 2 column names in the bus data frame? Good catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't matter for any of our purposes, but this popped up when I was trying to compare bus dataframes between a USA TAMU model and a MATReader model.

'mu_Vmin']
col_name_bus2sub = ['sub_id', 'interconnect']
col_name_branch = [
Expand Down