def assess_sample_stability(end_cutoff=3): ip = get_ipython() rowavg = ip.user_ns['_rowavg'] tab = [['Sample name', 'Distance', 'Slope of autocorrelation function', 'Stability']] plt.figure() for sn in sorted(rowavg): for dist in sorted(rowavg[sn]): rowavg_rescaled = rowavg[sn][dist] / rowavg[sn][dist].mean() rowavg_std = rowavg_rescaled.std() * np.ones_like(rowavg_rescaled) try: A, B, stat = nonlinear_leastsquares(np.arange(len(rowavg_rescaled)), rowavg_rescaled, rowavg_std, lambda x, a, b: a * x + b, [0, 0]) problematic = (A.val > A.err * 3) except TypeError: A = 'N/A' problematic = 2 tab.append([sn, '%.2f' % dist, A, ["\u2713", "\u2718\u2718\u2718\u2718\u2718", '\u274e'][problematic]]) plt.errorbar(np.arange(len(rowavg_rescaled)), rowavg_rescaled, label=sn + ' %.2f mm' % dist) # ,diags_std[1:]) plt.xlabel('Separation in time (FSN units)') plt.ylabel('Average discrepancy between curves') plt.legend(loc='best') tab = ipy_table.IpyTable(tab) tab.apply_theme('basic') display(tab) plt.show()
def fit(self, function, parameters_init, xname='x', yname='y', dyname='dy', **kwargs): """Non-linear least-squares fit to the dataset. Inputs: ------- `function`: a callable, corresponding to the function to be fitted. Should have the following signature:: >>> function(x, par1, par2, par3, ...) where ``par1``, ``par2``, etc. are the values of the parameters to be fitted. `parameters_init`: a sequence (tuple or list) of the initial values for the parameters to be fitted. Their ordering should be the same as that of the arguments of `function` other keyword arguments are given to `nonlinear_leastsquares()` without any modification. Outputs: -------- `par1_fit`, `par2_fit`, etc.: the best fitting values of the parameters. `statdict`: a dictionary with various status data, such as `R2`, `DoF`, `Chi2_reduced`, `Covariance`, `Correlation_coeffs` etc. For a full list, see the help of `sastool.misc.easylsq.nlsq_fit()` `func_value`: the value of the function at the best fitting parameters, represented as an instance of the same class as this curve. Notes: ------ The fitting itself is done via sastool.misc.easylsq.nonlinear_leastsquares() """ obj = self.sanitize(fieldname=yname).sanitize(fieldname=xname) if not hasattr(obj, dyname): dy = np.ones(len(obj), np.double) * np.nan else: dy = getattr(obj, dyname) ret = nonlinear_leastsquares(getattr(obj, xname), getattr( obj, yname), dy, function, parameters_init, **kwargs) funcvalue = type(self)(getattr(obj, xname), ret[-1]['func_value']) return ret + tuple([funcvalue])