Skip to content

Commit

Permalink
Move serialization of focus to Gridgen.to_spec() and create a focus o…
Browse files Browse the repository at this point in the history
…bejtect in Gridgen.from_spec(). simplify focus tests
  • Loading branch information
ricaenriquez committed Nov 14, 2018
1 parent b32b083 commit faf6864
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
11 changes: 8 additions & 3 deletions pygridgen/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,16 +986,21 @@ def generate_grid(self):
super(Gridgen, self).__init__(x, y)

def to_spec(self):
focus = Focus.from_spec(self.focus.to_spec())
output_dict = {'xbry': self.xbry, 'ybry': self.ybry,
'beta': self.beta, 'shape': self.shape,
'focus': focus}
'focus': self.focus.to_spec(),
'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):
return cls(**attributes)
focus_spec = attributes.pop('focus', None)
focus = Focus.from_spec(focus_spec)
return cls(**attributes, focus=focus)


def rho_to_vert(xr, yr, pm, pn, ang): # pragma: no cover
Expand Down
17 changes: 2 additions & 15 deletions pygridgen/tests/test_focus.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,7 @@ def test_full_focus_called(full_focus, xy):

nptest.assert_array_almost_equal(xf, known_focused_x, decimal=3)
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)
Expand All @@ -171,10 +164,4 @@ def test_focus_to_from_spec():

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
assert f1.to_spec() == f2.to_spec()

0 comments on commit faf6864

Please sign in to comment.