Esempio n. 1
0
    def set_xrange(self, *args):
        """set the range for the monte carlo simulation"""

        if not args:
            self.__settings[lit.XRANGE] = ()
        else:
            new_range = (args[0], args[1]) if len(args) > 1 else args
            utils.validate_xrange(new_range)
            self.__settings[lit.XRANGE] = new_range

        self.__evaluator.values.clear()
Esempio n. 2
0
def fit_to_xy_dataset(dataset: dts.XYDataSet, model, **kwargs) -> XYFitResult:
    """Perform a fit on an XYDataSet object"""

    fit_model = fut.prepare_fit_model(model)

    if fit_model.name == lit.POLY:
        # By default, the degree of a polynomial fit model is 3, because if it were 2, the
        # quadratic fit model would've been chosen. The number of parameters is the degree
        # of the fit model plus one. (e.g. a degree-1, or linear fit, has 2 params)
        new_constraints = FitParamConstraints(
            kwargs.get("degrees", 3) + 1, False, False)
        fit_model = FitModelInfo(fit_model.name, fit_model.func,
                                 new_constraints)

    param_info, fit_model = fut.prepare_param_info(fit_model, **kwargs)

    xrange = kwargs.get("xrange", None)
    if xrange and utils.validate_xrange(xrange):
        x_to_fit = dataset.xdata[(xrange[0] <= dataset.xdata)
                                 & (dataset.xdata < xrange[1])]
        y_to_fit = dataset.ydata[(xrange[0] <= dataset.xdata)
                                 & (dataset.xdata < xrange[1])]
    else:
        x_to_fit = dataset.xdata
        y_to_fit = dataset.ydata

    yerr = y_to_fit.errors if any(err > 0 for err in y_to_fit.errors) else None

    if fit_model.name in [lit.POLY, lit.LIN, lit.QUAD]:
        raw_res = __polynomial_fit(x_to_fit, y_to_fit,
                                   fit_model.param_constraints.length - 1,
                                   yerr)
    else:
        raw_res = __curve_fit(fit_model.func, x_to_fit, y_to_fit,
                              param_info.parguess, yerr)

    # wrap the parameters in MeasuredValue objects
    def wrap_param_in_measurements():
        par_res = zip(raw_res.popt, raw_res.perr, param_info.parunits,
                      param_info.parnames)
        for param, err, unit, name in par_res:
            yield dt.MeasuredValue(param, err, unit=unit, name=name)

    params = list(wrap_param_in_measurements())

    pcorr = utils.cov2corr(raw_res.pcov)
    __correlate_fit_params(params, raw_res.pcov)

    # wrap the result function with the params
    result_func = __combine_fit_func_and_fit_params(fit_model.func, params)

    return XYFitResult(dataset=dataset,
                       model=fit_model,
                       res_func=result_func,
                       res_params=params,
                       pcorr=pcorr,
                       xrange=xrange)
Esempio n. 3
0
    def __init__(self, *args, **kwargs):
        """Constructor for XYObjectOnPlot"""

        xrange = kwargs.pop("xrange", ())
        if xrange:
            utils.validate_xrange(xrange)
        self._xrange = xrange

        xname = kwargs.pop("xname", "")
        if not isinstance(xname, str):
            raise TypeError("The xname provided is not a string!")
        self._xname = xname

        yname = kwargs.pop("yname", "")
        if not isinstance(yname, str):
            raise TypeError("The yname provided is not a string!")
        self._yname = yname

        xunit = kwargs.pop("xunit", "")
        if not isinstance(xunit, str):
            raise TypeError("The xunit provided is not a string!")
        self._xunit = xunit

        yunit = kwargs.pop("yunit", "")
        if not isinstance(yname, str):
            raise TypeError("The yunit provided is not a string!")
        self._yunit = yunit

        # save the plot kwargs
        self.plot_kwargs = {
            k: v
            for k, v in kwargs.items() if k in PLOT_VALID_KWARGS
        }
        self.err_kwargs = {
            k: v
            for k, v in kwargs.items() if k in ERRORBAR_VALID_KWARGS
        }

        super().__init__(*args, **kwargs)
Esempio n. 4
0
 def xrange(self, new_range: tuple):
     if new_range:
         utils.validate_xrange(new_range)
     self._xrange = new_range
     self._ydata = None  # clear y data since it would need to be re-calculated
Esempio n. 5
0
 def xrange(self, new_range: tuple):
     if new_range:
         utils.validate_xrange(new_range)
     self._xrange = new_range