Skip to content

Commit

Permalink
Merge pull request #152 from intvenlab/grid_equality
Browse files Browse the repository at this point in the history
feat: test for Grid equality
  • Loading branch information
danielolsen authored Apr 23, 2020
2 parents 55aee65 + 32d8c63 commit 8c6c893
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
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',
'mu_Vmin']
col_name_bus2sub = ['sub_id', 'interconnect']
col_name_branch = [
Expand Down

0 comments on commit 8c6c893

Please sign in to comment.