KEVIVI
KEVIVI

Reputation: 57

Script interface for the Fit image Palette introduced in GMS 2.3?

The Fit Image Palette is quite nice and powerful. Is there a script interface that we can access it directly?

Upvotes: 0

Views: 154

Answers (1)

BmyGuest
BmyGuest

Reputation: 2949

There is a script interface, and the example script below will get you started. However, the script interface is not officially supported. It might therefore be buggy or likely to change in future GMS versions.

For GMS 2.3 the following script works:

// create the input image:
Image input := NewImage("formula test", 2, 100)
input = 500.5 - icol*11.1 + icol*icol*0.11

// add some random noise:
input += (random()-0.5)*sqrt(abs(input))

// create image with error data (not required)
Image errors := input.ImageClone()
errors = tert(input > 1, sqrt(input), 1)

// setup fit:
Image pars := NewImage("pars", 2, 3)
Image parsToFit := NewImage("pars to fit", 2, 3)
pars = 10;          // starting values
parsToFit = 1;
Number chiSqr = 1e6
Number conv_cond = 0.00001

Result("\n starting pars = {")
Number xSize = pars.ImageGetDimensionSize(0)
Number i = 0
for (i = 0; i < xSize; i++)
{
    Result(GetPixel(pars, i, 0))
    if (i < (xSize-1)) Result(", ")
}
Result("}")

// fit:
String formulaStr = "p0 + p1*x + p2*x**2"
Number ok = FitFormula(formulaStr, input, errors, pars, parsToFit, chiSqr, conv_cond)

Result("\n results  pars = {")
for (i = 0; i < xSize; i++)
{
    Result(GetPixel(pars, i, 0))
    if (i < (xSize-1)) Result(", ")
}
Result("}")
Result(", chiSqr ="+ chiSqr)

// plot results of fit:
Image plot := PlotFormula(formulaStr, input, pars)

// compare the plot and original data:
Image compare := NewImage("Compare Fit", 2, 100, 3)
compare[icol, 0] = input        // original data
compare[icol, 1] = plot         // fit function
compare[icol, 2] = input - plot // residuals

ImageDocument linePlotDoc = CreateImageDocument("Test Fitting")
ImageDisplay linePlotDsp = linePlotDoc.ImageDocumentAddImageDisplay(compare, 3)
linePlotDoc.ImageDocumentShow()

Upvotes: 0

Related Questions