lib/transient.py
def fit_transients(self, fit_function, parameters, fit_from=0, fit_to=0, method='curve_fit', ext_plot=None,
                       print_results=True, recursive_optimization=False, colorlist=None, saveDir=None):
        """
            Fit given model to a series of Transients.
        :param fit_function:
            Model which will be fitted to the data
        :param parameters: list, (list of lists - no longer supported)
            Initial parameters for the given function
        :param fit_from: int
            Minimum  from which to perform fit.
        :param fit_to: int
            Maximum data point (x axis) from which to perform fit.
        :param method: function
            Fitting method used: supports 'curve_fit'
        :param recursive_optimization: bool
            If true, it uses the optimized fit from previous cycle to initialize the next fitting
        :param ext_plot: bool
            if true, plots the results in a matplotlib figure
        :param print_results: bool
            if true prints fitting results in console while being obtained.
        :return all_popt: dict
            dictionary with transient label as key and fit optimized parameters as values
        :return all_pcov: dict

        """
        if ext_plot is None:
            fig = plt.figure('Fit of transients')
            plt.clf()
            ax = fig.add_subplot(111)
            ax.set_xlabel('Time [ps]', fontsize=18)
            ax.set_ylabel('Differential Reflectivity', fontsize=18)
            ax.set_title(self.series_name, fontsize=26)
            ax.tick_params(axis='x', labelsize=12)
            ax.tick_params(axis='y', labelsize=12)
        if colorlist is None:
            colorlist_length = len(self.transients)
            colorlist = plt.cm.rainbow(np.linspace(0, 1, colorlist_length))
        else:
            ax = ext_plot

        color = iter(colorlist)

        all_popt = {}  # dict type output
        all_pcov = {}

        key_parameter_values = []
        fit_parameters_data = {}  # dict of Data type output
        try:
            if len(parameters[0]) > 1:
                pars = parameters[0]
        except TypeError:
            pars = parameters
        for i, fit_parameter in enumerate(pars):
            fit_parameters_data['par{}'.format(i)] = []

        last_popt = parameters
        for i, transient in enumerate(self.transients):
            xdata = transient.time[fit_from:-fit_to]
            ydata = transient.trace[fit_from:-fit_to]
            key_parameter_values.append(transient.key_parameter_value)
            key_parameter = transient.key_parameter
            label = '{0} {1}'.format(transient.key_parameter_value, transient.get_unit(transient.key_parameter))

            try:
                if len(parameters[0]) > 1:
                    guess = parameters[i]
            except TypeError:
                if recursive_optimization:
                    guess = last_popt
                else:
                    guess = parameters

            all_popt[label] = []
            all_pcov[label] = []

            if method == 'curve_fit':
                try:
                    popt, pcov = curve_fit(fit_function, xdata, ydata, p0=guess)
                    if recursive_optimization:
                        last_popt = popt
                    if print_results:
                        print('{0}: popt: {1}'.format(label, popt))

                    col = next(color)
                    ax.plot(xdata, fit_function(xdata, *popt), '--', c=col)
                    ax.plot(xdata, ydata, c=col, label=label, alpha=0.5)

                    all_popt[label] = popt
                    all_pcov[label] = pcov

                    for i, item in enumerate(popt):
                        fit_parameters_data['par{}'.format(i)].append(item)

                except RuntimeError:
                    print('no fit parameters found for transient: {}'.format(label))
            elif method == 'fmin':
                print('fmin not yet implemented')  # todo: add support for fmin

        for key, value in fit_parameters_data.items():
            fit_parameters_data[key] = Data(key_parameter_values, value, key_parameter, key)

        if ext_plot:
            pass
            # plt.show()

        if saveDir is not None:
            pass
        return all_popt, all_pcov, fit_parameters_data

            Fit given model to a series of Transients.
        :param fit_function:
            Model which will be fitted to the data
        :param parameters: list, (list of lists - no longer supported)
            Initial parameters for the given function
        :param fit_from: int
            Minimum  from which to perform fit.
        :param fit_to: int
            Maximum data point (x axis) from which to perform fit.
        :param method: function
            Fitting method used: supports 'curve_fit'
        :param recursive_optimization: bool
            If true, it uses the optimized fit from previous cycle to initialize the next fitting
        :param ext_plot: bool
            if true, plots the results in a matplotlib figure
        :param print_results: bool
            if true prints fitting results in console while being obtained.
        :return all_popt: dict
            dictionary with transient label as key and fit optimized parameters as values
        :return all_pcov: dict

        
Fit given model to a series of Transients.
Fit given model to a series of Transients .
def fit_transients(self, fit_function, parameters, fit_from=0, fit_to=0, method='curve_fit', ext_plot=None,
                       print_results=True, recursive_optimization=False, colorlist=None, saveDir=None):
        if ext_plot is None:
            fig = plt.figure('Fit of transients')
            plt.clf()
            ax = fig.add_subplot(111)
            ax.set_xlabel('Time [ps]', fontsize=18)
            ax.set_ylabel('Differential Reflectivity', fontsize=18)
            ax.set_title(self.series_name, fontsize=26)
            ax.tick_params(axis='x', labelsize=12)
            ax.tick_params(axis='y', labelsize=12)
        if colorlist is None:
            colorlist_length = len(self.transients)
            colorlist = plt.cm.rainbow(np.linspace(0, 1, colorlist_length))
        else:
            ax = ext_plot
        color = iter(colorlist)
        all_popt = {}  
        all_pcov = {}
        key_parameter_values = []
        fit_parameters_data = {}  
        try:
            if len(parameters[0]) > 1:
                pars = parameters[0]
        except TypeError:
            pars = parameters
        for i, fit_parameter in enumerate(pars):
            fit_parameters_data['par{}'.format(i)] = []
        last_popt = parameters
        for i, transient in enumerate(self.transients):
            xdata = transient.time[fit_from:-fit_to]
            ydata = transient.trace[fit_from:-fit_to]
            key_parameter_values.append(transient.key_parameter_value)
            key_parameter = transient.key_parameter
            label = '{0} {1}'.format(transient.key_parameter_value, transient.get_unit(transient.key_parameter))
            try:
                if len(parameters[0]) > 1:
                    guess = parameters[i]
            except TypeError:
                if recursive_optimization:
                    guess = last_popt
                else:
                    guess = parameters
            all_popt[label] = []
            all_pcov[label] = []
            if method == 'curve_fit':
                try:
                    popt, pcov = curve_fit(fit_function, xdata, ydata, p0=guess)
                    if recursive_optimization:
                        last_popt = popt
                    if print_results:
                        print('{0}: popt: {1}'.format(label, popt))
                    col = next(color)
                    ax.plot(xdata, fit_function(xdata, *popt), '--', c=col)
                    ax.plot(xdata, ydata, c=col, label=label, alpha=0.5)
                    all_popt[label] = popt
                    all_pcov[label] = pcov
                    for i, item in enumerate(popt):
                        fit_parameters_data['par{}'.format(i)].append(item)
                except RuntimeError:
                    print('no fit parameters found for transient: {}'.format(label))
            elif method == 'fmin':
                print('fmin not yet implemented')  
        for key, value in fit_parameters_data.items():
            fit_parameters_data[key] = Data(key_parameter_values, value, key_parameter, key)
        if ext_plot:
            pass
        if saveDir is not None:
            pass
        return all_popt, all_pcov, fit_parameters_data
def fit_transients ( self , fit_function , parameters , fit_from = 0 , fit_to = 0 , method = 'curve_fit' , ext_plot = None , print_results = True , recursive_optimization = False , colorlist = None , saveDir = None ) : if ext_plot is None : fig = plt . figure ( 'Fit of transients' ) plt . clf ( ) ax = fig . add_subplot ( 111 ) ax . set_xlabel ( 'Time [ps]' , fontsize = 18 ) ax . set_ylabel ( 'Differential Reflectivity' , fontsize = 18 ) ax . set_title ( self . series_name , fontsize = 26 ) ax . tick_params ( axis = 'x' , labelsize = 12 ) ax . tick_params ( axis = 'y' , labelsize = 12 ) if colorlist is None : colorlist_length = len ( self . transients ) colorlist = plt . cm . rainbow ( np . linspace ( 0 , 1 , colorlist_length ) ) else : ax = ext_plot color = iter ( colorlist ) all_popt = { } all_pcov = { } key_parameter_values = [ ] fit_parameters_data = { } try : if len ( parameters [ 0 ] ) > 1 : pars = parameters [ 0 ] except TypeError : pars = parameters for i , fit_parameter in enumerate ( pars ) : fit_parameters_data [ 'par{}' . format ( i ) ] = [ ] last_popt = parameters for i , transient in enumerate ( self . transients ) : xdata = transient . time [ fit_from : - fit_to ] ydata = transient . trace [ fit_from : - fit_to ] key_parameter_values . append ( transient . key_parameter_value ) key_parameter = transient . key_parameter label = '{0} {1}' . format ( transient . key_parameter_value , transient . get_unit ( transient . key_parameter ) ) try : if len ( parameters [ 0 ] ) > 1 : guess = parameters [ i ] except TypeError : if recursive_optimization : guess = last_popt else : guess = parameters all_popt [ label ] = [ ] all_pcov [ label ] = [ ] if method == 'curve_fit' : try : popt , pcov = curve_fit ( fit_function , xdata , ydata , p0 = guess ) if recursive_optimization : last_popt = popt if print_results : print ( '{0}: popt: {1}' . format ( label , popt ) ) col = next ( color ) ax . plot ( xdata , fit_function ( xdata , * popt ) , '--' , c = col ) ax . plot ( xdata , ydata , c = col , label = label , alpha = 0.5 ) all_popt [ label ] = popt all_pcov [ label ] = pcov for i , item in enumerate ( popt ) : fit_parameters_data [ 'par{}' . format ( i ) ] . append ( item ) except RuntimeError : print ( 'no fit parameters found for transient: {}' . format ( label ) ) elif method == 'fmin' : print ( 'fmin not yet implemented' ) for key , value in fit_parameters_data . items ( ) : fit_parameters_data [ key ] = Data ( key_parameter_values , value , key_parameter , key ) if ext_plot : pass if saveDir is not None : pass return all_popt , all_pcov , fit_parameters_data
Fit given model to a series of Transients.
Fit given model to a series of Transients .
"""
            Fit given model to a series of Transients.
        :param fit_function:
            Model which will be fitted to the data
        :param parameters: list, (list of lists - no longer supported)
            Initial parameters for the given function
        :param fit_from: int
            Minimum  from which to perform fit.
        :param fit_to: int
            Maximum data point (x axis) from which to perform fit.
        :param method: function
            Fitting method used: supports 'curve_fit'
        :param recursive_optimization: bool
            If true, it uses the optimized fit from previous cycle to initialize the next fitting
        :param ext_plot: bool
            if true, plots the results in a matplotlib figure
        :param print_results: bool
            if true prints fitting results in console while being obtained.
        :return all_popt: dict
            dictionary with transient label as key and fit optimized parameters as values
        :return all_pcov: dict

        """ # dict type output # dict of Data type output # todo: add support for fmin # plt.show()
dict
dictionary with transient label as key and fit optimized parameters as values
dict dictionary with transient label as key and fit optimized parameters as values
all_popt
None
all_pcov
self
fit_function
Model which will be fitted to the data
Model which will be fitted to the data
parameters
list, (list of lists - no longer supported)
Initial parameters for the given function
list ( list of lists - no longer supported ) Initial parameters for the given function
fit_from
int
Minimum  from which to perform fit.
int Minimum from which to perform fit .
fit_to
int
Maximum data point (x axis) from which to perform fit.
int Maximum data point ( x axis ) from which to perform fit .
method
function
Fitting method used: supports 'curve_fit'
function Fitting method used : supports ' curve_fit '
ext_plot
bool
if true, plots the results in a matplotlib figure
bool if true plots the results in a matplotlib figure
print_results
bool
if true prints fitting results in console while being obtained.
bool if true prints fitting results in console while being obtained .
recursive_optimization
bool
If true, it uses the optimized fit from previous cycle to initialize the next fitting
bool If true it uses the optimized fit from previous cycle to initialize the next fitting
colorlist
saveDir