Skip to content

Commit

Permalink
Add .to_spec() and .from_spec() functions for Focus and Gridgen class…
Browse files Browse the repository at this point in the history
…es. Additonal tests are added
  • Loading branch information
ricaenriquez committed Nov 13, 2018
1 parent d38598d commit bbf0c62
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pygridgen/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def __call__(self, x, y):
x, y = focuspoint(x, y)
return x, y

def to_spec(self):
output_spec = []
for focuspoint in self._focuspoints:
output_spec.append(focuspoint.to_dict())
return output_spec

def from_spec(foci):
f = Focus()
for focuspoint in foci:
f.add_focus(**focuspoint)
return f

class CGrid(object):
"""
Expand Down Expand Up @@ -972,6 +983,17 @@ def generate_grid(self):

super(Gridgen, self).__init__(x, y)

def to_spec(self):
output_dict = {'xbry':self.xbry, 'ybry':self.ybry,
'beta':self.beta, 'shape':self.shape,
'focus':self.focus}

return output_dict

def from_spec(attributes):
# print(attributes)
g = Gridgen(**attributes)
return g

def rho_to_vert(xr, yr, pm, pn, ang): # pragma: no cover
""" Possibly converts centroids to nodes """
Expand Down
21 changes: 21 additions & 0 deletions pygridgen/tests/test_focus.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,24 @@ def test_focuspoint_to_dict():
fp2 = pygridgen.grid._FocusPoint(**fp.to_dict())
assert fp.to_dict() == {'pos': 0.25, 'axis': 'x', 'factor': 4, 'extent': 0.5}
assert fp.to_dict() == fp2.to_dict()

def test_focus_to_from_spec():
fpx = pygridgen.grid._FocusPoint(0.25, 'x', 4, 0.5)
fpy = pygridgen.grid._FocusPoint(0.33, 'y', 0.1, 0.2)
f1 = pygridgen.grid.Focus(fpx, fpy)

## Check that the spec dictionary returns desired results
assert f1.to_spec() == [
{'pos': 0.25, 'axis': 'x', 'factor': 4, 'extent': 0.5},
{'pos': 0.33, 'axis': 'y', 'factor': 0.1, 'extent': 0.2}
]

f2 = pygridgen.grid.Focus.from_spec(f1.to_spec())

## Check that the spec dictionary returns desired results of the orignal
## focus and the focus generated from .to_spec()
## First check the keys are in order and then check the values
for dict1, dict2 in zip(f1.to_spec(), f2.to_spec()):
numpy.testing.assert_equal(dict1.keys(), dict2.keys())
for value1, value2 in zip(dict1.values(), dict2.values()):
assert value1==value2
12 changes: 12 additions & 0 deletions pygridgen/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,15 @@ def test_mask_poylgon(grid_and_knowns):
])
grid.mask_polygon(island)
nptest.assert_array_almost_equal(grid.mask_rho, known_mask_rho)

def test_gridgen_to_from_spec():
focus = pygridgen.Focus()
focus.add_focus(0.50, 'y', factor=5, extent=0.25)
focus.add_focus(0.50, 'x', factor=5, extent=0.25)

grid1 = pygridgen.Gridgen(x, y, beta, shape=(20, 10), focus=focus)
grid2 = pygridgen.grid.Gridgen.from_spec((grid1.to_spec()))

# testing - using almost equal due to rounding issues with floats
numpy.testing.assert_array_almost_equal(grid1.x, grid2.x)
numpy.testing.assert_array_almost_equal(grid1.y, grid2.y)

0 comments on commit bbf0c62

Please sign in to comment.