Skip to content

Instantly share code, notes, and snippets.

@jdavies-st
Last active October 29, 2019 21:37
Show Gist options
  • Save jdavies-st/17d25cb11646309f41a6f3162550655c to your computer and use it in GitHub Desktop.
Save jdavies-st/17d25cb11646309f41a6f3162550655c to your computer and use it in GitHub Desktop.
serialize astropy.modeling instance to ASDF
>>> from astropy.modeling import models, fitting
>>> import asdf
>>> import numpy as np
>>> x = np.random.random(100)
>>> y = np.random.random(100)
>>>
>>> mymodel = models.Polynomial1D(degree=3)
>>> fitter = fitting.LevMarLSQFitter()
>>> myfit = fitter(mymodel,x,y)
WARNING: Model is linear in parameters; consider using linear fitting methods. [astropy.modeling.fitting]
>>>
>>> myfit
<Polynomial1D(3, c0=0.55412012, c1=-1.00790666, c2=1.53229478, c3=-0.54992893)>
>>> tree = dict(model=myfit)
>>> af = asdf.AsdfFile(tree=tree)
>>> af.tree['model']
<Polynomial1D(3, c0=0.55412012, c1=-1.00790666, c2=1.53229478, c3=-0.54992893)>
>>> af.write_to('mymodel.asdf')
>>> new = asdf.open('mymodel.asdf').tree['model']
>>> new
<Polynomial1D(3, c0=0.55412012, c1=-1.00790666, c2=1.53229478, c3=-0.54992893)>
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment