Exemplo n.º 1
0
    def default_opt_arg(self):
        """Provide default optional arguments by the user.

        Should be given as a dictionary when overridden.
        """
        return {
            opt: default_arg_from_bounds(bnd)
            for (opt, bnd) in self.default_opt_arg_bounds().items()
        }
Exemplo n.º 2
0
def _init_guess(bounds, current, default, typ, para_name):
    """Proper determination of initial guess."""
    if typ == "default":
        if bounds[0] < default < bounds[1]:
            return default
        return default_arg_from_bounds(bounds)
    if typ == "current":
        return current
    raise ValueError("CovModel.fit: unknown init_guess: '{}'".format(typ))
Exemplo n.º 3
0
def _pre_init_guess(model, init_guess, mean_x=1.0, mean_y=1.0):
    # init guess should be a dict
    if not isinstance(init_guess, dict):
        init_guess = {"default": init_guess}
    # "default" init guess is the respective default value
    default_guess = init_guess.pop("default", "default")
    if default_guess not in ["default", "current"]:
        raise ValueError(f"fit_variogram: unknown def. guess: {default_guess}")
    default = default_guess == "default"
    # check invalid names for given init guesses
    invalid_para = set(init_guess) - set(model.iso_arg + ["anis"])
    if invalid_para:
        raise ValueError(f"fit_variogram: unknown init guess: {invalid_para}")
    bnd = model.arg_bounds
    # default length scale is mean of given bin centers (respecting "rescale")
    init_guess.setdefault(
        "len_scale", mean_x * model.rescale if default else model.len_scale
    )
    # init guess for variance and nugget is mean of given variogram
    for par in ["var", "nugget"]:
        init_guess.setdefault(par, mean_y if default else getattr(model, par))
    # anis setting
    init_guess.setdefault(
        "anis", default_arg_from_bounds(bnd["anis"]) if default else model.anis
    )
    # correctly handle given values for anis (need a list of values)
    init_guess["anis"] = list(set_anis(model.dim, init_guess["anis"]))
    # set optional arguments
    for opt in model.opt_arg:
        init_guess.setdefault(
            opt,
            default_arg_from_bounds(bnd[opt])
            if default
            else getattr(model, opt),
        )
    # convert all init guesses to float (except "anis")
    for arg in model.iso_arg:
        init_guess[arg] = float(init_guess[arg])
    return init_guess
Exemplo n.º 4
0
def _init_curve_fit_para(model, para, init_guess, constrain_sill, sill, anis):
    """Create initial guess and bounds for fitting."""
    low_bounds = []
    top_bounds = []
    init_guess_list = []
    for par in DEFAULT_PARA:
        if para[par]:
            low_bounds.append(model.arg_bounds[par][0])
            if par == "var" and constrain_sill:  # var <= sill in this case
                top_bounds.append(sill)
            else:
                top_bounds.append(model.arg_bounds[par][1])
            init_guess_list.append(
                _init_guess(
                    bounds=[low_bounds[-1], top_bounds[-1]],
                    current=getattr(model, par),
                    default=1.0,
                    typ=init_guess,
                    para_name=par,
                ))
    for opt in model.opt_arg:
        if para[opt]:
            low_bounds.append(model.arg_bounds[opt][0])
            top_bounds.append(model.arg_bounds[opt][1])
            init_guess_list.append(
                _init_guess(
                    bounds=[low_bounds[-1], top_bounds[-1]],
                    current=getattr(model, opt),
                    default=model.default_opt_arg()[opt],
                    typ=init_guess,
                    para_name=opt,
                ))
    if anis:
        low_bounds += [model.anis_bounds[0]] * (model.dim - 1)
        top_bounds += [model.anis_bounds[1]] * (model.dim - 1)
        if init_guess == "default":
            def_arg = default_arg_from_bounds(model.anis_bounds)
            init_guess_list += [def_arg] * (model.dim - 1)
        elif init_guess == "current":
            init_guess_list += list(model.anis)
        else:
            raise ValueError(
                "CovModel.fit: unknown init_guess: '{}'".format(init_guess))

    return (low_bounds, top_bounds), init_guess_list
Exemplo n.º 5
0
def _init_guess(bounds, default):
    """Proper determination of initial guess."""
    if bounds[0] < default < bounds[1]:
        return default
    return default_arg_from_bounds(bounds)