0

For example I have data:

x    y1    y2    y1
---------------------
1    5     8    -
2    4     -    4
3    7     7    10
4    9     4    12
5    10    -    20
6    15    -    21

Where x is x axis and y1, y2, y3 are three different data sets, which are fitted together.

For the sake of simplicity, heres reduced version of fitting code:

def gauss_dataset(params, i, x):
    """calc gaussian from params for data set i
    using simple, hardwired naming convention"""
    x = params['x_%i' % (i+1)].value
    y = params['x_%i' % (i+1)].value
    return x + y

def objective(params, x, data):
    """ calculate total residual for fits to several data sets held
    in a 2-D array, and modeled by Gaussian functions"""
    ndata, nx = data.shape
    resid = 0.0*data[:]
    # make residual per data set
    for i in range(ndata):
        resid[i, :] = data[i, :] - gauss_dataset(params, i, x)
    # now flatten this to a 1D array, as minimize() needs
    return resid.flatten()

x  = np.linspace(0, 50, 50)
data = []

...

# Rearange data
for col in range(0, data_sets):
    for row in range (0, size_rows):
        data[col][row] = intens[row][col+1]

# create 5 sets of parameters, one per data set
fit_params = Parameters()
for iy, y in enumerate(data):
    fit_params.add( 'x_%i' % (iy+1))
    fit_params.add( 'y_%i' % (iy+1))

# run the global fit to all the data sets
minimize(objective, fit_params, args=(x, data))

in minimize(objective, fit_params, args=(x, data)): data is data[y][z]: y - data set, z - data in that data set. and x is x axis.

How do I modify my python script lmfit minimize to ignore missing data points or rewrite my script, so each data has its own x axis?:

x1   y1    x2   y2   x3   y3
-----------------------------
1    5     1    8    2    4-
2    4     3    7    3    10
3    7     4    4    4    12
4    9               5    20
5    10              6    21
6    15

also I cannot use multiple minimize fits (script is actually more complicated than shown above), so first = minimize(objective, fit_params, args=(x1, y1)), second = minimize(objective, fit_params, args=(x2, y2)) is not valid answer.

Dancia
  • 762
  • 1
  • 18
  • 32

1 Answers1

0

I don't see how you read in your data, and so can't tell how the missing values are represented. If you use something like np.nan to represent a missing value, then you could mask out the points from the residual calculation using np.isnan and np.where. That could happen either before or after you flatten() the result.

M Newville
  • 7,486
  • 2
  • 16
  • 29