# Path: pymicro/xray/fitting.py
# def fit(y, x=None, expression=None, nb_params=None, init=None):
#     '''Static method to perform curve fitting directly.
# 
#       *Parameters*
# 
#       **y**: the data to match (a 1d numpy array)
# 
#       **x**: the corresponding x coordinates (optional, None by default)
# 
#       **expression**: can be either a string to select a predefined
#       function or alternatively a user defined function with the signature
#       f(x, p) (in this case you must specify the length of the parameters
#       array p via setting nb_params).
# 
#       **nb_params**: the number of parameters of the user defined fitting
#       function (only needed when a custom fitting function is provided,
#       None by default)
# 
#       **init**: a sequence (the length must be equal to the number of
#       parameters of the fitting function) used to initialise the fitting
#       function.
# 
#       For instance, to fit some (x,y) data with a gaussian function, simply use:
#        ::
# 
#          F = fit(y, x, expression='Gaussian')
# 
#        Alternatively you may specify you own function directly defined with Python, like:
#        ::
# 
#          def myf(x, p):
#            return p[0]*x + p[1]
# 
#          F = fit(y, x, expression=myf, nb_params=2)
#     '''
#     if expression == 'Gaussian':
#         F = Gaussian()
#     elif expression == 'Lorentzian':
#         F = Lorentzian()
#     elif expression == 'Cosine':
#         F = Cosine()
#     elif expression == 'Voigt':
#         F = Voigt()
#     else:
#         F = FitFunction()
#         if not nb_params:
#             print('please specify the number of parameters for your fit function, aborting fit...')
#             return None
#         if not init:
#             init = np.ones(nb_params)
#         if not len(init) == nb_params:
#             print(
#                 'there are more parameters in the fit function than specified in the initialization sequence, aborting initialization...')
#             init = np.ones(nb_params)
#         for i in range(nb_params):
#             F.add_parameter(init[i], 'p%d' % i)
#         F.expression = expression
#     F.fit(y, x)
#     return F


import numpy as np
import os
from matplotlib import pyplot as plt
from pymicro.xray.fitting import fit
from matplotlib import image
#!/usr/bin/env python

'''Basic curve fitting example with a cosine function. The data is
also fitted with a custom function slightly different than the default
Cosine, which lead to the same result.'''
x = np.linspace(-3.0, 3.0, 31)
# generate some noisy data
np.random.seed(13)
y = np.cos(4 * x / np.pi - 0.5) + 0.05 * np.random.randn(len(x))


# custom function
def C(x, p):
    return np.sin(np.pi * (x - p[0].value) / (2 * p[1].value)) * p[2].value


# perform fitting
F = fit(y, x, expression=C, nb_params=3)

# Path: pymicro/xray/fitting.py
# def fit(y, x=None, expression=None, nb_params=None, init=None):
#     '''Static method to perform curve fitting directly.
# 
#       *Parameters*
# 
#       **y**: the data to match (a 1d numpy array)
# 
#       **x**: the corresponding x coordinates (optional, None by default)
# 
#       **expression**: can be either a string to select a predefined
#       function or alternatively a user defined function with the signature
#       f(x, p) (in this case you must specify the length of the parameters
#       array p via setting nb_params).
# 
#       **nb_params**: the number of parameters of the user defined fitting
#       function (only needed when a custom fitting function is provided,
#       None by default)
# 
#       **init**: a sequence (the length must be equal to the number of
#       parameters of the fitting function) used to initialise the fitting
#       function.
# 
#       For instance, to fit some (x,y) data with a gaussian function, simply use:
#        ::
# 
#          F = fit(y, x, expression='Gaussian')
# 
#        Alternatively you may specify you own function directly defined with Python, like:
#        ::
# 
#          def myf(x, p):
#            return p[0]*x + p[1]
# 
#          F = fit(y, x, expression=myf, nb_params=2)
#     '''
#     if expression == 'Gaussian':
#         F = Gaussian()
#     elif expression == 'Lorentzian':
#         F = Lorentzian()
#     elif expression == 'Cosine':
#         F = Cosine()
#     elif expression == 'Voigt':
#         F = Voigt()
#     else:
#         F = FitFunction()
#         if not nb_params:
#             print('please specify the number of parameters for your fit function, aborting fit...')
#             return None
#         if not init:
#             init = np.ones(nb_params)
#         if not len(init) == nb_params:
#             print(
#                 'there are more parameters in the fit function than specified in the initialization sequence, aborting initialization...')
#             init = np.ones(nb_params)
#         for i in range(nb_params):
#             F.add_parameter(init[i], 'p%d' % i)
#         F.expression = expression
#     F.fit(y, x)
#     return F


# Path: examples/plotting/cos_fitting.py
import numpy as np
import os
from matplotlib import pyplot as plt
from pymicro.xray.fitting import fit
from matplotlib import image

#!/usr/bin/env python

'''Basic curve fitting example with a cosine function. The data is
also fitted with a custom function slightly different than the default
Cosine, which lead to the same result.'''
x = np.linspace(-3.0, 3.0, 31)
# generate some noisy data
np.random.seed(13)
y = np.cos(4 * x / np.pi - 0.5) + 0.05 * np.random.randn(len(x))


# custom function
def C(x, p):
    return np.sin(np.pi * (x - p[0].value) / (2 * p[1].value)) * p[2].value


# perform fitting
F = fit(y, x, expression=C, nb_params=3)

C = fit(y, x, expression='Cosine')