Skip to content

Commit

Permalink
Merge pull request pygridgen#47 from ricaenriquez/to_from_spec
Browse files Browse the repository at this point in the history
Add `to|from_spec` methods to Focus and Gridgen classes
  • Loading branch information
phobson authored Nov 14, 2018
2 parents d38598d + 8e1cf58 commit 540f21e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
37 changes: 37 additions & 0 deletions pygridgen/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ def __call__(self, x, y):
x, y = focuspoint(x, y)
return x, y

def to_spec(self):
""" Export to the defining properties of the focus to a JSON-like
structure
"""
output_spec = []
for focuspoint in self._focuspoints:
output_spec.append(focuspoint.to_dict())
return output_spec

@classmethod
def from_spec(cls, foci):
""" Create a new focus object from a JSON-like structure
"""
f = cls()
for focuspoint in foci:
f.add_focus(**focuspoint)
return f


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

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

def to_spec(self):
""" Export the grid-defining parameters into a JSON-like structure """
output_dict = {'xbry': self.xbry, 'ybry': self.ybry,
'beta': self.beta, 'shape': self.shape,
'focus': self.focus.to_spec() if self.focus else None,
'ul_idx': self.ul_idx, 'proj': self.proj,
'nnodes': self.nnodes, 'precision': self.precision,
'nppe': self.nppe, 'newton': self.newton,
'thin': self.thin, 'checksimplepoly': self.checksimplepoly}

return output_dict

@classmethod
def from_spec(cls, attributes):
""" Create a new grid from a JSON-like data structure """
focus_spec = attributes.pop('focus', None)
focus = Focus.from_spec(focus_spec) if focus_spec else None
return cls(focus=focus, **attributes)


def rho_to_vert(xr, yr, pm, pn, ang): # pragma: no cover
""" Possibly converts centroids to nodes """
Expand Down
19 changes: 14 additions & 5 deletions pygridgen/tests/test_focus.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,17 @@ def test_full_focus_called(full_focus, xy):
nptest.assert_array_almost_equal(yf, known_focused_y, decimal=3)


def test_focuspoint_to_dict():
fp = pygridgen.grid._FocusPoint(0.25, 'x', 4, 0.5)
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())

assert f1.to_spec() == f2.to_spec()
20 changes: 20 additions & 0 deletions pygridgen/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,23 @@ def test_mask_poylgon(grid_and_knowns):
])
grid.mask_polygon(island)
nptest.assert_array_almost_equal(grid.mask_rho, known_mask_rho)


@pytest.mark.parametrize('use_focus', [True, False])
def test_gridgen_to_from_spec(use_focus):
x = numpy.array([0.50, 2.00, 2.00, 3.50, 3.50, 2.00, 2.00, 0.50, 0.50])
y = numpy.array([0.50, 0.50, 1.75, 1.75, 2.25, 2.25, 3.50, 3.50, 0.50])
beta = numpy.array([1, 1, -1, 1, 1, -1, 1, 1, 0])

focus = None
if use_focus:
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 540f21e

Please sign in to comment.