Exemplo n.º 1
0
def _iterate(simulation, variables, expected, guess, bounds, biased_parameter, model, input_values, logger, len_parameter=3):
    biased_parameters = np.zeros((len(input_values), len_parameter))
    for i, input_ in enumerate(input_values):
        model.setVariable(biased_parameter, input_)
        logger.debug(">>>> Input for %s: %s" % (biased_parameter, input_))
        logger.debug("Fit variables %s: " % model.get_Symbols())
        res = Residuum(model, 'ratio')
        residuals = FitWrapper(res.getResiduals())
        Fit = FitModel()
        resultls = Fit._least_squares(residuals, guess, simulation, bounds)
        biased_parameters[i] = np.array(resultls.x) - np.array(expected)
    return biased_parameters
Exemplo n.º 2
0
def test_main():
    from get_ssa import get_ssa
    zenith = 53.1836240528
    AMass = 1.66450160404
    rel_h = 0.665
    pressure = 950
    AM = 5
    ssa = get_ssa(rel_h, AM)
    x = np.linspace(200, 800, 100)  # config
    variables = ['alpha', 'beta', 'g_dsa', 'g_dsr']  # config
    expected_values = [2.5, 0.06, 0.6, 0.5]
    print('Expected: %s' % expected_values)
    guess = [1.0, 0.01, 0.5, 0.8]  # config
    bounds = [(-0.2, 4), (0., 3), (0., 2.), (0., 2.)]  # config

    # Theano
    irr_symbol = IrradianceModel_sym(x, zenith, AMass, pressure, ssa, variables)
    getIrrRatio = irr_symbol.getcompiledModel('ratio')
    y_theano = getIrrRatio(*expected_values)
    res = Residuum(irr_symbol, 'ratio')
    residuum = FitWrapper(res.getResiduum())
    residuals = FitWrapper(res.getResiduals())
    derivative = FitWrapper(res.getDerivative())
    Fit = FitModel()
    result = Fit._minimize(residuum, guess, y_theano, bounds, jacobian=derivative)
    print("Got %s" % result.x)
    resultls = Fit._least_squares(residuals, guess, y_theano, bounds)
    print("Got %s" % resultls.x)


    # Python
    IrradianceObject = IrradianceModel_python(AMass, rel_h, ssa, zenith, pressure)
    y_python = IrradianceObject.irradiance_ratio(x, 2.5, 0.06,0.0, 0.6, 0.5)

    gmod = Model(IrradianceObject.irradiance_ratio, independent_vars=['x'], param_names=variables)
    gmod.set_param_hint('alpha', value=guess[0], min=bounds[0][0], max=bounds[0][1])
    gmod.set_param_hint('beta',  value=guess[1], min=bounds[1][0], max=bounds[1][1])
    gmod.set_param_hint('g_dsa', value=guess[2], min=bounds[2][0], max=bounds[2][1])
    gmod.set_param_hint('g_dsr', value=guess[3], min=bounds[3][0], max=bounds[3][1])

    result_lmfit = gmod.fit(y_python, x=x)
    print(result_lmfit.fit_report())

    plt.plot(x, y_theano)
    x_new = np.linspace(300, 900,150)
    irr_symbol.set_wavelengthAOI(x_new)
    getIrrRatio = irr_symbol.getcompiledModel('ratio')
    y_new = getIrrRatio(*expected_values)
    plt.plot(x_new, y_new, '+', label='different wavelengths')
    plt.legend()
    plt.show()
Exemplo n.º 3
0
class LeastSquaresFit:

    def __init__(self, model, config, param_dict, logger):
        self.config = config
        self.model = model
        self.res = Residuum(model)
        self.callable = FitWrapper(self.res.getResiduals())
        self.symbols = model.get_Symbols()
        self.param_dict = param_dict
        self.logger = logger

    def fit(self):
        self.logger.info("Symbols: %s" % self.symbols)
        self.logger.info('Method %s' % self.config['method'])
        bounds = tuple(map(list, zip(*self.config['limits'])))  # [(min,max),(min1,max1)..] -> ([min,min1,..], [max,max1..])
        self.result = least_squares(self.callable, self.config['initial_values'], args=(self.param_dict['spectra_range'],), bounds=bounds)
        self.param_dict['variables'] = dict()
        for idx, symbol in enumerate(self.symbols):
            self.param_dict['variables'][symbol] = dict()
            self.param_dict['variables'][symbol]['stderr'] = None
            self.param_dict['variables'][symbol]['value'] = self.result.x[idx]
        self._calc_fitted_spectra()
        self._calc_residuals()
        return Result(self.result, self.fitted_spectra, self.residuals, self.param_dict['wave_range']), self.param_dict

    def _calc_fitted_spectra(self):
        f = self.model.get_compiled()
        self.fitted_spectra = f(*self.result.x)

    def _calc_residuals(self):
        #TODO
        self.residuals = self.param_dict['spectra_range'] - self.fitted_spectra
Exemplo n.º 4
0
 def __init__(self, model, config, param_dict, logger):
     self.config = config
     self.model = model
     self.res = Residuum(model)
     self.callable = FitWrapper(self.res.getResiduals())
     self.symbols = model.get_Symbols()
     self.param_dict = param_dict
     self.logger = logger
Exemplo n.º 5
0
class Minimize:

    def __init__(self, model, config, param_dict, logger):
        self.model = model
        self.param_dict = param_dict
        self.config = config
        del self.config['independent']['x']
        for key, value in self.config['independent'].items():
            logger.debug("Set %s for %s " % (value, key))
            self.model.setVariable(key, value)
        self.res = Residuum(model)
        self.callable = FitWrapper(self.res.getResiduum())
        self.symbols = model.get_Symbols()
        self.logger = logger
        if config['jac_flag']:
            logger.info("Using jacobian")
            self.jacobian = FitWrapper(self.res.getDerivative())
        else:
            assert config['jac_flag'] == False
            self.jacobian = False

    def fit(self):
        self.logger.info("Symbols: %s" % self.symbols)
        self.logger.info('Method %s' % self.config['method'])
        self.result = minimize(self.callable, self.config['initial_values'], args=(self.param_dict['spectra_range']), jac=self.jacobian,
                               method=self.config['method'], bounds=self.config['limits'])
        self.param_dict['variables'] = dict()
        for idx, symbol in enumerate(self.symbols):
            self.param_dict['variables'][symbol.name] = dict()
            self.param_dict['variables'][symbol.name]['stderr'] = None
            self.param_dict['variables'][symbol.name]['value'] = self.result.x[idx]
        self._calc_fitted_spectra()
        self._calc_residuals()
        return Result(self.result, self.fitted_spectra, self.residuals, self.param_dict['wave_range']), self.param_dict

    def _calc_fitted_spectra(self):
        f = self.model.get_compiled()
        self.fitted_spectra = f(*self.result.x)

    def _calc_residuals(self):
        #TODO
        self.residuals = self.param_dict['spectra_range'] - self.fitted_spectra
Exemplo n.º 6
0
def example_gaussian():
    #Config output
    x = np.linspace(-20, 20, 100)
    a = 3.4
    b = 2.4
    c = 5
    bounds = [(3, 4), (2, 3), (4.2, 5.2)]
    guess = [a,b,c]
    variables = ['a', 'b', 'c']
    y = gaussian(x, guess) + np.random.normal(0, 0.1, len(x))

    #Auswertung
    model= GaussModel(x, variables)
    res = Residuum(model, 'gauss')
    function_to_fit = FitWrapper(res.getResiduum())
    test = FitModel(function_to_fit)
    result = test.fit(guess, y, bounds)

    y_fitted = gaussian(x, result.x)

    plt.plot(x,y, 'b')
    plt.plot(x, y_fitted, 'r')
Exemplo n.º 7
0
 def __init__(self, model, config, param_dict, logger):
     self.model = model
     self.res = Residuum(model, 'ratio')
     self.callable = FitWrapper(self.res.getResiduum())
     self.symbols = model.get_Symbols()
     self.param_dict = param_dict
     self.config = config
     self.logger = logger
     if config['jac_flag']:
         logger.info("Using jacobian")
         self.jacobian = FitWrapper(self.res.getDerivative())
     else:
         assert config['jac_flag'] == False
         self.jacobian = False
Exemplo n.º 8
0
class Minimize:

    def __init__(self, model, config, param_dict, logger):
        self.model = model
        self.res = Residuum(model, 'ratio')
        self.callable = FitWrapper(self.res.getResiduum())
        self.symbols = model.get_Symbols()
        self.param_dict = param_dict
        self.config = config
        self.logger = logger
        if config['jac_flag']:
            logger.info("Using jacobian")
            self.jacobian = FitWrapper(self.res.getDerivative())
        else:
            assert config['jac_flag'] == False
            self.jacobian = False

    def fit(self):
        self.logger.info('Method %s' % self.config['method'])
        self.result = minimize(self.callable, self.config['initial_values'], args=(self.param_dict['spectra_range']), jac=self.jacobian,
                               method=self.config['method'], bounds=self.config['limits'])
        for idx, symbol in enumerate(self.symbols):
            self.param_dict[symbol] = dict()
            self.param_dict[symbol]['stderr'] = None
            self.param_dict[symbol]['value'] = self.result.x[idx]
        self._calc_fitted_spectra()
        self._calc_residuals()
        return Result(self.result, self.fitted_spectra, self.residuals, self.param_dict['wave_range']), self.param_dict

    def _calc_fitted_spectra(self):
        f = self.model.getcompiledModel('ratio')
        self.fitted_spectra = f(*self.result.x)

    def _calc_residuals(self):
        #TODO
        self.residuals = self.param_dict['spectra_range'] - self.fitted_spectra
Exemplo n.º 9
0
 def __init__(self, model, config, param_dict, logger):
     self.model = model
     self.param_dict = param_dict
     self.config = config
     del self.config['independent']['x']
     for key, value in self.config['independent'].items():
         logger.debug("Set %s for %s " % (value, key))
         self.model.setVariable(key, value)
     self.res = Residuum(model)
     self.callable = FitWrapper(self.res.getResiduum())
     self.symbols = model.get_Symbols()
     self.logger = logger
     if config['jac_flag']:
         logger.info("Using jacobian")
         self.jacobian = FitWrapper(self.res.getDerivative())
     else:
         assert config['jac_flag'] == False
         self.jacobian = False