Exemplo n.º 1
0
    def __init__(self,
                 c_regr=OLS(),
                 st_regr=OLS(),
                 c_fit_kwargs={},
                 st_fit_kwargs={},
                 c_constraints=[ConstraintNonneg()],
                 st_constraints=[ConstraintNonneg()],
                 max_iter=50,
                 err_fcn=mse,
                 tol_increase=0.0,
                 tol_n_increase=10,
                 tol_err_change=None,
                 tol_n_above_min=10):
        """
        Multivariate Curve Resolution - Alternating Regression
        """

        self.max_iter = max_iter

        self.tol_increase = tol_increase
        self.tol_n_increase = tol_n_increase
        self.tol_err_change = tol_err_change
        self.tol_n_above_min = tol_n_above_min

        self.err_fcn = err_fcn
        self.err = None

        self.c_constraints = c_constraints
        self.st_constraints = st_constraints

        self.c_regressor = self._check_regr(c_regr)
        self.st_regressor = self._check_regr(st_regr)
        self.c_fit_kwargs = c_fit_kwargs
        self.st_fit_kwargs = st_fit_kwargs

        self.C_ = None
        self.ST_ = None

        self.C_opt_ = None
        self.ST_opt_ = None
        self.n_iter_opt = None

        self.n_iter = None
        self.n_increase = None
        self.n_above_min = None

        self.exit_max_iter_reached = False
        self.exit_tol_increase = False
        self.exit_tol_n_increase = False
        self.exit_tol_err_change = False
        self.exit_tol_n_above_min = False

        # Saving every C or S^T matrix at each iteration
        # Could create huge memory usage
        self._saveall_st = False
        self._saveall_c = False
        self._saved_st = []
        self._saved_c = []
Exemplo n.º 2
0
def test_ols_negative_x():
    """ Test nnls """
    A = np.array([[1,1,0],[1,0,1],[0,0,1]])
    x = np.array([1,-2,3])
    b = np.dot(A,x)

    regr = OLS()

    # b is 1D
    regr.fit(A,b)
    assert_allclose(x, regr.X_)
Exemplo n.º 3
0
Arquivo: mcr.py Projeto: rkern/pyMCR
 def _check_regr(self, mth):
     """
         Check regressor method. If accetible strings, instantiate and return
         object. If instantiated class, make sure it has a fit attribute.
     """
     if isinstance(mth, str):
         if mth.upper() == 'OLS':
             return OLS()
         elif mth.upper() == 'NNLS':
             return NNLS()
         else:
             raise ValueError('{} is unknown. Use NNLS or OLS.'.format(mth))
     elif hasattr(mth, 'fit'):
         return mth
     else:
         raise ValueError(
             'Input class {} does not have a \'fit\' method'.format(mth))
Exemplo n.º 4
0
if 'ConstraintCumsumNonneg' in st_l:
    st_constraints.append(ConstraintCumsumNonneg())
if 'ConstraintNorm' in st_l:
    st_constraints.append(ConstraintNorm())
# print(c_constraints,st_constraints)
logger = logging.getLogger('pymcr')
logger.setLevel(logging.DEBUG)
stdout_handler = logging.StreamHandler(stream=sys.stdout)
stdout_format = logging.Formatter('%(message)s')
stdout_handler.setFormatter(stdout_format)
logger.addHandler(stdout_handler)

mcrar = McrAR(
    max_iter=max_iter,
    st_regr=NNLS(),
    c_regr=OLS(),
    c_constraints=[],
    st_constraints=st_constraints)
print('calculating MCR-ALS')
mcrar.fit(D, ST=ST_guess, verbose=True)

np.savetxt('out/wavelengths.txt', x)
np.savetxt('out/C-opt.txt', mcrar.C_opt_)
if dct['smooth spectra'] == 'True':
    args = (int(dct['smoothing window length']), str(dct['smoothing window']))
    S = np.apply_along_axis(smooth, 1, mcrar.ST_opt_, *args).T
    np.savetxt('out/S-opt.txt', S)
else:
    S = mcrar.ST_opt_.T
    np.savetxt('out/S-opt.txt', S)