Skip to content

Commit

Permalink
fix: PEP8 tweaks, slight testing refactor for succintness
Browse files Browse the repository at this point in the history
  • Loading branch information
danielolsen committed Oct 31, 2019
1 parent 14bf3ec commit 5cf62e7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
13 changes: 8 additions & 5 deletions postreise/analyze/mwmiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from powersimdata.input.grid import Grid


def haversine(point1, point2):
"""Given two lat/long pairs, return distance in miles.
Expand Down Expand Up @@ -29,6 +30,7 @@ def haversine(point1, point2):

return d


def calculate_mw_miles(scenario):
"""Given a Scenario object, calculate the number of upgraded lines and
transformers, and the total upgrade quantity (in MW and MW-miles).
Expand All @@ -43,6 +45,7 @@ def calculate_mw_miles(scenario):
upgrades = _calculate_mw_miles(original_grid, ct)
return upgrades


def _calculate_mw_miles(original_grid, ct):
"""Given a base grid and a change table, calculate the number of upgraded
lines and transformers, and the total upgrade quantity (in MW & MW-miles).
Expand All @@ -57,20 +60,20 @@ def _calculate_mw_miles(original_grid, ct):
"""

upgrade_categories = (
'mw_miles', 'transformer_mw','num_lines', 'num_transformers')
'mw_miles', 'transformer_mw', 'num_lines', 'num_transformers')
upgrades = {u: 0 for u in upgrade_categories}

base_branch = original_grid.branch
upgraded_branches = ct['branch']['branch_id']
for b, v in upgraded_branches.items():
# 'upgraded' capacity is v-1 because a scale of 1 = an upgrade of 0
upgraded_capacity = base_branch.loc[b,'rateA'] * (v-1)
device_type = base_branch.loc[b,'branch_device_type']
upgraded_capacity = base_branch.loc[b, 'rateA'] * (v-1)
device_type = base_branch.loc[b, 'branch_device_type']
if device_type == 'Line':
from_coords = (
base_branch.loc[b,'from_lat'], base_branch.loc[b,'from_lon'])
base_branch.loc[b, 'from_lat'], base_branch.loc[b, 'from_lon'])
to_coords = (
base_branch.loc[b,'to_lat'], base_branch.loc[b,'to_lon'])
base_branch.loc[b, 'to_lat'], base_branch.loc[b, 'to_lon'])
addtl_mw_miles = (
upgraded_capacity * haversine(from_coords, to_coords))
upgrades['mw_miles'] += addtl_mw_miles
Expand Down
46 changes: 20 additions & 26 deletions tests/test_mwmiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,42 @@
mock_branch = {
'branch_id': [11, 12, 13, 14, 15],
'rateA': [10, 20, 30, 40, 50],
'from_lat': [47.61, 47.61, 47.61, 47.61, 47.61,],
'from_lon': [-122.33, -122.33, -122.33, -122.33, -122.33,],
'from_lat': [47.61, 47.61, 47.61, 47.61, 47.61],
'from_lon': [-122.33, -122.33, -122.33, -122.33, -122.33],
'to_lat': [37.78, 47.66, 47.61, 47.61, 47.61],
'to_lon': [-122.42, -117.43, -122.33, -122.33, -122.33],
'branch_device_type': 2 * ['Line'] + 3 * ['Transformer'],
}

expected_keys = set((
'mw_miles', 'transformer_mw', 'num_lines', 'num_transformers'))
expected_keys = {
'mw_miles', 'transformer_mw', 'num_lines', 'num_transformers'}


class TestCalculateMWMiles(unittest.TestCase):
def setUp(self):
self.grid = MockGrid(grid_attrs={'branch': mock_branch})

def _check_return_structure(self, mw_miles):
"""Check for a dict with proper keys and floats for values.
def _check_expected_values(self, mw_miles, expected_mw_miles):
"""Check for proper structure and that values match expected.
:param dict mw_miles: dict of upgrade metrics.
:param dict expected_mw_miles: dict of expected upgrade metrics.
"""
self.assertIsInstance(mw_miles, dict, 'dict not returned')
self.assertEqual(expected_keys, mw_miles.keys(),
msg='Dict keys not as expected')
self.assertEqual(
expected_keys, mw_miles.keys(), msg='Dict keys not as expected')
for v in mw_miles.values():
self.assertIsInstance(v, (float, int))
for k in expected_mw_miles.keys():
err_msg = 'Did not get expected value for ' + str(k)
self.assertAlmostEqual(
mw_miles[k], expected_mw_miles[k], msg=err_msg)

def test_calculate_mw_miles_no_scale(self):
mock_ct = {'branch': {'branch_id': {}}}
expected_mw_miles = {k: 0 for k in expected_keys}
mw_miles = _calculate_mw_miles(self.grid, mock_ct)
self._check_return_structure(mw_miles)
for k in expected_mw_miles.keys():
self.assertAlmostEqual(mw_miles[k], expected_mw_miles[k],
msg=('Did not get expected value for ' + str(k)))
self._check_expected_values(mw_miles, expected_mw_miles)

def test_calculate_mw_miles_one_line_scaled(self):
mock_ct = {'branch': {'branch_id': {11: 2}}}
Expand All @@ -55,10 +57,7 @@ def test_calculate_mw_miles_one_line_scaled(self):
'num_transformers': 0,
}
mw_miles = _calculate_mw_miles(self.grid, mock_ct)
self._check_return_structure(mw_miles)
for k in expected_mw_miles.keys():
self.assertAlmostEqual(mw_miles[k], expected_mw_miles[k],
msg=('Did not get expected value for ' + str(k)))
self._check_expected_values(mw_miles, expected_mw_miles)

def test_calculate_mw_miles_one_transformer_scaled(self):
mock_ct = {'branch': {'branch_id': {13: 2.5}}}
Expand All @@ -69,10 +68,7 @@ def test_calculate_mw_miles_one_transformer_scaled(self):
'num_transformers': 1,
}
mw_miles = _calculate_mw_miles(self.grid, mock_ct)
self._check_return_structure(mw_miles)
for k in expected_mw_miles.keys():
self.assertAlmostEqual(mw_miles[k], expected_mw_miles[k],
msg=('Did not get expected value for ' + str(k)))
self._check_expected_values(mw_miles, expected_mw_miles)

def test_calculate_mw_miles_many_scaled(self):
mock_ct = {'branch': {'branch_id': {
Expand All @@ -84,10 +80,8 @@ def test_calculate_mw_miles_many_scaled(self):
'num_transformers': 3,
}
mw_miles = _calculate_mw_miles(self.grid, mock_ct)
self._check_return_structure(mw_miles)
for k in expected_mw_miles.keys():
self.assertAlmostEqual(mw_miles[k], expected_mw_miles[k],
msg=('Did not get expected value for ' + str(k)))
self._check_expected_values(mw_miles, expected_mw_miles)


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 5cf62e7

Please sign in to comment.