Search code examples
pythonmodelingastropy

Extracting parameters from astropy.modeling Gaussian2D


I have managed to use astropy.modeling to model a 2D Gaussian over my image and the parameters it has produced to fit the image seem reasonable. However, I need to run the 2D Gaussian over thousands of images because we are interested in examining the mean x and y of the model and also the x and y standard deviations over our images. The model output looks like this:

m2
<Gaussian2D(amplitude=0.0009846091239480168, x_mean=30.826676737477573, y_mean=31.004045976953222, x_stddev=2.5046722491074536, y_stddev=3.163048479350727, theta=-0.0070295894129793896)>

I can also tell you this:

type(m2)
<class 'astropy.modeling.functional_models.Gaussian2D'>
Name: Gaussian2D
Inputs: (u'x', u'y')
Outputs: (u'z',)
Fittable parameters: ('amplitude', 'x_mean', 'y_mean', 'x_stddev', 'y_stddev', 'theta')

What I need is a method to extract the parameters of the model, namely:

x_mean
y_mean
x_stddev
y_stddev

I am not familiar with this form output so I am really stuck on how to extract the parameters.


Solution

  • The models have attributes you can access:

    from astropy.modeling import models
    g2d = models.Gaussian2D(1,2,3,4,5)
    g2d.amplitude.value  # 1.0
    g2d.x_mean.value     # 2.0
    g2d.y_mean.value     # 3.0
    g2d.x_stddev.value   # 4.0
    g2d.y_stddev.value   # 5.0
    

    You need to extract these values after you fitted the model but you can access them in the same way: .<name>.value.

    You can also extract them in one go but then you need to keep track which parameter is in which position:

    g2d.parameters    # array([ 1.,  2.,  3.,  4.,  5.,  0.])
    # Amplitude
    g2d.parameters[0] # 1.0
    # x-mean
    g2d.parameters[1] # 2.0
    # ...