'Function to minimize.
Parameters
fps : list
the fitted parameters - result of an one iteration of the
fitting algorithm
args : dict
tuple of measured and input coordinates
args is always passed as a tuple from optimize.leastsq'

def objective_function(self, fps, *args):

    lstsqargs = list(args)
    fitted = []
    fitparams = list(fps)
    numjp = len(self.initvals)
    jointfitparams = fitparams[:numjp]
    del fitparams[:numjp]
    for model in self.models:
        joint_params = self.jointparams[model]
        margs = lstsqargs[:(model.n_inputs + 1)]
        del lstsqargs[:(model.n_inputs + 1)]
        numfp = (len(model._parameters) - len(joint_params))
        mfparams = fitparams[:numfp]
        del fitparams[:numfp]
        mparams = []
        param_metrics = model._param_metrics
        for param_name in model.param_names:
            if (param_name in joint_params):
                index = joint_params.index(param_name)
                mparams.extend([jointfitparams[index]])
            else:
                slice_ = param_metrics[param_name][u'slice']
                plen = (slice_.stop - slice_.start)
                mparams.extend(mfparams[:plen])
                del mfparams[:plen]
        modelfit = model.evaluate(margs[:(-1)], *mparams)
        fitted.extend((modelfit - margs[(-1)]))
    return np.ravel(fitted)
