Esempio n. 1
0
def fit(problem, method=FIT_DEFAULT_ID, verbose=False, **options):
    """
    Simplified fit interface.

    Given a fit problem, the name of a fitter and the fitter options,
    it will run the fit and return the best value and standard error of
    the parameters.  If *verbose* is true, then the console monitor will
    be enabled, showing progress through the fit and showing the parameter
    standard error at the end of the fit, otherwise it is completely
    silent.

    Returns an *OptimizeResult* object containing "x" and "dx".  The
    dream fitter also includes the "state" object, allowing for more
    detailed uncertainty analysis.  Optimizer information such as the
    stopping condition and the number of function evaluations are not
    yet included.

    To run in parallel (with multiprocessing and dream)::

        from bumps.mapper import MPMapper
        mapper = MPMapper.start_mapper(problem, None, cpu=0) #cpu=0 for all CPUs
        result = fit(problem, method="dream", mapper=mapper)

    """
    from scipy.optimize import OptimizeResult

    #verbose = True
    if method not in FIT_AVAILABLE_IDS:
        raise ValueError("unknown method %r not one of %s"
                         % (method, ", ".join(sorted(FIT_ACTIVE_IDS))))
    for fitclass in FITTERS:
        if fitclass.id == method:
            break
    monitors = None if verbose else []  # default is step monitor
    driver = FitDriver(
        fitclass=fitclass, problem=problem, monitors=monitors,
        **options)
    driver.clip() # make sure fit starts within domain
    x0 = problem.getp()
    x, fx = driver.fit()
    problem.setp(x)
    dx = driver.stderr()
    if verbose:
        print("final chisq", problem.chisq_str())
        driver.show_err()
    result = OptimizeResult(
        x=x, dx=driver.stderr(),
        fun=fx,
        success=True, status=0, message="successful termination",
        #nit=0, # number of iterations
        #nfev=0, # number of function evaluations
        #njev, nhev # jacobian and hessian evaluations
        #maxcv=0, # max constraint violation
        )
    if hasattr(driver.fitter, 'state'):
        result.state = driver.fitter.state
    return result
Esempio n. 2
0
    def bumps_fit(self,
                  method='dream',
                  pop=15,
                  samples=1e5,
                  burn=100,
                  steps=0,
                  thin=1,
                  alpha=0,
                  outliers='none',
                  trim=False,
                  monitors=[],
                  problem=None,
                  **options):
        # create a fitter similar to the bumps.fitters.fit function but with option for GUI monitoring
        from scipy.optimize import OptimizeResult
        from bumps.fitters import FitDriver, FIT_AVAILABLE_IDS, FITTERS, FIT_ACTIVE_IDS
        options['pop'] = pop
        options['samples'] = samples
        options['burn'] = burn
        options['steps'] = steps
        options['thin'] = thin
        options['alpha'] = alpha
        options['outliers'] = outliers
        options['trim'] = trim

        if problem is None:
            problem = self.bumps_problem()

        # verbose = True
        if method not in FIT_AVAILABLE_IDS:
            raise ValueError("unknown method %r not one of %s" %
                             (method, ", ".join(sorted(FIT_ACTIVE_IDS))))
        for fitclass in FITTERS:
            if fitclass.id == method:
                break
        driver = FitDriver(fitclass=fitclass,
                           problem=problem,
                           monitors=monitors,
                           **options)
        driver.clip()  # make sure fit starts within domain
        x0 = problem.getp()
        x, fx = driver.fit()
        problem.setp(x)
        dx = driver.stderr()
        result = OptimizeResult(x=x,
                                dx=driver.stderr(),
                                fun=fx,
                                cov=driver.cov(),
                                success=True,
                                status=0,
                                message="successful termination")
        if hasattr(driver.fitter, 'state'):
            result.state = driver.fitter.state
        return result