Ejemplo n.º 1
0
    def filter_frames(self, data):
        t1 = time.time()
        data = data[0]
        axis = self.axis
        positions = self.positions
        print positions
        weights = data[self.peakindex]
        widths = np.ones_like(positions)*self.parameters["width_guess"]
        p = []
        p.extend(weights)
        p.extend(widths)
        curvetype = self.getFitFunction(str(self.parameters['peak_shape']))
        print "HI"
        [x1, infodict1] = ral_nlls.solve(
                    p, self._resid, self.dfunc,
                    params=(curvetype, data, axis, positions),
                                      options = {
                                          'print_level': 3,
                                          'maxit': 100,
                                          'model': 1,
                                          'nlls_method': 4,
                                          'stop_g_absolute': 1e-7,
                                          'stop_g_relative': 1e-7,
                                          'relative_tr_radius': 0, 
                                          'initial_radius_scale': 1.0,
                                          'maximum_radius': 1e8,
                                          'eta_successful':1e-8,
                                          'eta_success_but_reduce': 1e-8,
                                          'eta_very_successful':0.9,
                                          'eta_too_successful': 2.0,
                                          'radius_increase': 2.0, 
                                          'radius_reduce': 0.5,
                                          'radius_reduce_max': 1/16,
                                          'tr_update_strategy': 2,
                                          'hybrid_switch': 1e-0,
                                          'scale': 0,
                                          'scale_max':1e11,
                                          'scale_min':1e-11,
                                          'more_sorensen_maxits': 500,
                                          'more_sorensen_shift': 1e-13,
                                          'more_sorensen_tol': 1e-3,
                                          'hybrid_tol': 2.0,
                                          'hybrid_switch_its': 1
                                      })

        logging.debug("done one")
        params = x1[0]
        if np.isnan(params).any():
            logging.debug('Nans were detected here')
            params = np.zeros(len(params))

        weights, widths, areas = self.getAreas(curvetype,
                                               axis, positions, params)
        residuals = self._resid(params, curvetype, data, axis, positions)
        # all fitting routines will output the same format.
        # nchannels long, with 3 elements. Each can be a subarray.
        t2 = time.time()
        logging.debug("Simple fit iteration took: %s ms", str((t2-t1)*1e3))
        return [weights, widths, areas, residuals]
Ejemplo n.º 2
0
    def filter_frames(self, data):
        t1 = time.time()
        data = data[0]
        axis = self.axis
        positions = self.positions
        print positions
        weights = data[self.peakindex]
        widths = np.ones_like(positions)*self.parameters["width_guess"]
        p = []
        p.extend(weights)
        p.extend(widths)
        curvetype = self.getFitFunction(str(self.parameters['peak_shape']))
        print "HI"
        [x1, infodict1] = ral_nlls.solve(
                    p, self._resid, self.dfunc,
                    params=(curvetype, data, axis, positions),
                                      options = {
                                          'print_level': 3,
                                          'maxit': 100,
                                          'model': 1,
                                          'nlls_method': 4,
                                          'stop_g_absolute': 1e-7,
                                          'stop_g_relative': 1e-7,
                                          'relative_tr_radius': 0, 
                                          'initial_radius_scale': 1.0,
                                          'maximum_radius': 1e8,
                                          'eta_successful':1e-8,
                                          'eta_success_but_reduce': 1e-8,
                                          'eta_very_successful':0.9,
                                          'eta_too_successful': 2.0,
                                          'radius_increase': 2.0, 
                                          'radius_reduce': 0.5,
                                          'radius_reduce_max': 1/16,
                                          'tr_update_strategy': 2,
                                          'hybrid_switch': 1e-0,
                                          'scale': 0,
                                          'scale_max':1e11,
                                          'scale_min':1e-11,
                                          'more_sorensen_maxits': 500,
                                          'more_sorensen_shift': 1e-13,
                                          'more_sorensen_tol': 1e-3,
                                          'hybrid_tol': 2.0,
                                          'hybrid_switch_its': 1
                                      })

        logging.debug("done one")
        params = x1[0]
        if np.isnan(params).any():
            logging.debug('Nans were detected here')
            params = np.zeros(len(params))

        weights, widths, areas = self.getAreas(curvetype,
                                               axis, positions, params)
        residuals = self._resid(params, curvetype, data, axis, positions)
        # all fitting routines will output the same format.
        # nchannels long, with 3 elements. Each can be a subarray.
        t2 = time.time()
        logging.debug("Simple fit iteration took: %s ms", str((t2-t1)*1e3))
        return [weights, widths, areas, residuals]
 def fit(self):
     """
     Run problem with RALFit.
     """
     self._popt = ral_nlls.solve(self.initial_params,
                                 self.cost_func.eval_r,
                                 self.jacobian.eval,
                                 options=self._options)[0]
     self._status = 0 if self._popt is not None else 1
Ejemplo n.º 4
0
    def fit(self):
        """
        Run problem with RALFit.
        """
        self.success = False
        self._popt = ral_nlls.solve(self.initial_params,
                                    self._prediction_error,
                                    self._jac,
                                    options=self._options)[0]

        self.success = (self._popt is not None)
Ejemplo n.º 5
0
# Where H_i = [ 0                     t_i x_1 e^(x_2 t_i)    ]
#             [ t_i x_1 e^(x_2 t_i)   x_1 t_i^2 e^(x_2 t_i)  ]
def Hr(x, r, t, y):
    x1 = x[0]
    x2 = x[1]

    Hr = numpy.zeros((2, 2))
    Hr[0, 0] = 0.0  # H_11
    v = t * numpy.exp(x2 * t)
    Hr[1, 0] = numpy.dot(r, v)  # H_21
    Hr[1, 1] = numpy.dot(r, (t * x1) * v)  # H_22

    return Hr


# Data to be fitted
t = numpy.array([1.0, 2.0, 4.0, 5.0, 8.0])
y = numpy.array([3.0, 4.0, 6.0, 11.0, 20.0])

# Starting guess
x0 = numpy.array([2.5, 0.25])

# Call fitting routine
(x, inform) = ral_nlls.solve(x0, r, J, Hr=Hr, params=(t, y))

# Print result
print("Found a local optimum at x = [ {0:.8f}  {1:.8f} ]".format(x[0], x[1]))
print("RALFit converged in {:d} iterations".format(inform["iter"]))
print("The cost function is {:f} at the minimum, and ||J^Tr||={:e}".format(
    inform["obj"], inform["norm_g"]))