def test_nonnegativeleastsquares(): n = 100 X = np.eye(n) beta = np.random.rand(n) y = np.dot(X, beta) my_nnls = opt.NonNegativeLeastSquares() my_nnls.fit(X, y) npt.assert_equal(my_nnls.coef_, beta) npt.assert_equal(my_nnls.predict(X), y)
def __init__(self, gtab, sphere=None, response=[0.0015, 0.0005, 0.0005], solver='ElasticNet', l1_ratio=0.5, alpha=0.001, isotropic=None): """ Initialize a Sparse Fascicle Model Parameters ---------- gtab : GradientTable class instance sphere : Sphere class instance, optional A sphere on which coefficients will be estimated. Default: symmetric sphere with 362 points (from :mod:`dipy.data`). response : (3,) array-like, optional The eigenvalues of a canonical tensor to be used as the response function of single-fascicle signals. Default:[0.0015, 0.0005, 0.0005] solver : string, or initialized linear model object. This will determine the algorithm used to solve the set of linear equations underlying this model. If it is a string it needs to be one of the following: {'ElasticNet', 'NNLS'}. Otherwise, it can be an object that inherits from `dipy.optimize.SKLearnLinearSolver` or an object with a similar interface from Scikit Learn: `sklearn.linear_model.ElasticNet`, `sklearn.linear_model.Lasso` or `sklearn.linear_model.Ridge` and other objects that inherit from `sklearn.base.RegressorMixin`. Default: 'ElasticNet'. l1_ratio : float, optional Sets the balance betwee L1 and L2 regularization in ElasticNet [Zou2005]_. Default: 0.5 alpha : float, optional Sets the balance between least-squares error and L1/L2 regularization in ElasticNet [Zou2005]_. Default: 0.001 isotropic : IsotropicModel class instance This is a class that implements the function that calculates the value of the isotropic signal. This is a value of the signal that is independent of direction, and therefore removed from both sides of the SFM equation. The default is an instance of IsotropicModel, but other functions can be inherited from IsotropicModel to implement other fits to the aspects of the data that depend on b-value, but not on direction. Notes ----- This is an implementation of the SFM, described in [Rokem2015]_. .. [Rokem2014] Ariel Rokem, Jason D. Yeatman, Franco Pestilli, Kendrick N. Kay, Aviv Mezer, Stefan van der Walt, Brian A. Wandell (2014). Evaluating the accuracy of diffusion MRI models in white matter. PLoS ONE 10(4): e0123272. doi:10.1371/journal.pone.0123272 .. [Zou2005] Zou H, Hastie T (2005). Regularization and variable selection via the elastic net. J R Stat Soc B:301-320 """ ReconstModel.__init__(self, gtab) if sphere is None: sphere = dpd.get_sphere() self.sphere = sphere self.response = np.asarray(response) if isotropic is None: isotropic = IsotropicModel self.isotropic = isotropic if solver == 'ElasticNet': self.solver = lm.ElasticNet(l1_ratio=l1_ratio, alpha=alpha, positive=True, warm_start=True) elif solver == 'NNLS' or solver == 'nnls': self.solver = opt.NonNegativeLeastSquares() elif (isinstance(solver, opt.SKLearnLinearSolver) or has_sklearn and isinstance(solver, sklearn.base.RegressorMixin)): self.solver = solver else: e_s = "The `solver` key-word argument needs to be: " e_s += "'ElasticNet', 'NNLS', or a " e_s += "`dipy.optimize.SKLearnLinearSolver` object" raise ValueError(e_s)