Ejemplo n.º 1
0
def test_sparse_nnls():
    # Set up the regression:
    beta = np.random.rand(10)
    X = np.random.randn(1000, 10)
    y = np.dot(X, beta)
    beta_hat = sparse_nnls(y, X)
    beta_hat_sparse = sparse_nnls(y, sps.csr_matrix(X))
    # We should be able to get back the right answer for this simple case
    npt.assert_array_almost_equal(beta, beta_hat, decimal=1)
    npt.assert_array_almost_equal(beta, beta_hat_sparse, decimal=1)
Ejemplo n.º 2
0
def test_sparse_nnls():
    # Set up the regression:
    beta = np.random.rand(10)
    X = np.random.randn(1000, 10)
    y = np.dot(X, beta)
    beta_hat = sparse_nnls(y, X)
    beta_hat_sparse = sparse_nnls(y, sps.csr_matrix(X))
    # We should be able to get back the right answer for this simple case
    npt.assert_array_almost_equal(beta, beta_hat, decimal=1)
    npt.assert_array_almost_equal(beta, beta_hat_sparse, decimal=1)
Ejemplo n.º 3
0
Archivo: life.py Proyecto: mutou7/dipy
    def fit(self, data, streamline, affine=None, evals=[0.001, 0, 0], sphere=None):
        """
        Fit the LiFE FiberModel for data and a set of streamlines associated
        with this data

        Parameters
        ----------
        data : 4D array
           Diffusion-weighted data

        streamline : list
           A bunch of streamlines

        affine: 4 by 4 array (optional)
           The affine to go from the streamline coordinates to the data
           coordinates. Defaults to use `np.eye(4)`

        evals : list (optional)
           The eigenvalues of the tensor response function used in constructing
           the model signal. Default: [0.001, 0, 0]

        sphere: `dipy.core.Sphere` instance, or False
            Whether to approximate (and cache) the signal on a discrete
            sphere. This may confer a significant speed-up in setting up the
            problem, but is not as accurate. If `False`, we use the exact
            gradients along the streamlines to calculate the matrix, instead of
            an approximation.

        Returns
        -------
        FiberFit class instance
        """
        if affine is None:
            affine = np.eye(4)
        life_matrix, vox_coords = self.setup(streamline, affine, evals=evals, sphere=sphere)
        to_fit, weighted_signal, b0_signal, relative_signal, mean_sig, vox_data = self._signals(data, vox_coords)
        beta = opt.sparse_nnls(to_fit, life_matrix)
        return FiberFit(
            self,
            life_matrix,
            vox_coords,
            to_fit,
            beta,
            weighted_signal,
            b0_signal,
            relative_signal,
            mean_sig,
            vox_data,
            streamline,
            affine,
            evals,
        )
Ejemplo n.º 4
0
    def fit(self,
            data,
            streamline,
            affine=None,
            evals=[0.001, 0, 0],
            sphere=None):
        """
        Fit the LiFE FiberModel for data and a set of streamlines associated
        with this data

        Parameters
        ----------
        data : 4D array
           Diffusion-weighted data

        streamline : list
           A bunch of streamlines

        affine: 4 by 4 array (optional)
           The affine to go from the streamline coordinates to the data
           coordinates. Defaults to use `np.eye(4)`

        evals : list (optional)
           The eigenvalues of the tensor response function used in constructing
           the model signal. Default: [0.001, 0, 0]

        sphere: `dipy.core.Sphere` instance, or False
            Whether to approximate (and cache) the signal on a discrete
            sphere. This may confer a significant speed-up in setting up the
            problem, but is not as accurate. If `False`, we use the exact
            gradients along the streamlines to calculate the matrix, instead of
            an approximation.

        Returns
        -------
        FiberFit class instance
        """
        if affine is None:
            affine = np.eye(4)
        life_matrix, vox_coords = \
            self.setup(streamline, affine, evals=evals, sphere=sphere)
        (to_fit, weighted_signal, b0_signal, relative_signal, mean_sig,
         vox_data) = self._signals(data, vox_coords)
        beta = opt.sparse_nnls(to_fit, life_matrix)
        return FiberFit(self, life_matrix, vox_coords, to_fit, beta,
                        weighted_signal, b0_signal, relative_signal, mean_sig,
                        vox_data, streamline, affine, evals)
Ejemplo n.º 5
0
Archivo: life.py Proyecto: mvgolub/dipy
    def fit(self, data, streamline, affine, evals=[0.001, 0, 0], sphere=None):
        """
        Fit the LiFE FiberModel for data and a set of streamlines associated
        with this data

        Parameters
        ----------
        data : 4D array
            Diffusion-weighted data
        streamline : list
            A bunch of streamlines
        affine : array_like (4, 4)
            The mapping from voxel coordinates to streamline points.
            The voxel_to_rasmm matrix, typically from a NIFTI file.
        evals : list (optional)
            The eigenvalues of the tensor response function used in
            constructing the model signal. Default: [0.001, 0, 0]
        sphere: `dipy.core.Sphere` instance, or False
            Whether to approximate (and cache) the signal on a discrete
            sphere. This may confer a significant speed-up in setting up the
            problem, but is not as accurate. If `False`, we use the exact
            gradients along the streamlines to calculate the matrix, instead of
            an approximation.

        Returns
        -------
        FiberFit class instance
        """
        if affine is None:
            affine = np.eye(4)
        sl_len = np.array([len(s) for s in streamline])
        if np.any(sl_len < 2):
            raise ValueError(
                "Input contains streamlines with only one node."
                " The LiFE model cannot be fit with these streamlines included."
            )
        life_matrix, vox_coords = \
            self.setup(streamline, affine, evals=evals, sphere=sphere)
        (to_fit, weighted_signal, b0_signal, relative_signal, mean_sig,
         vox_data) = self._signals(data, vox_coords)
        beta = opt.sparse_nnls(to_fit, life_matrix)
        return FiberFit(self, life_matrix, vox_coords, to_fit, beta,
                        weighted_signal, b0_signal, relative_signal, mean_sig,
                        vox_data, streamline, affine, evals)
Ejemplo n.º 6
0
    def fit(self,
            data,
            streamline,
            affine,
            evals=[0.001, 0, 0],
            sphere=None,
            processes=1,
            verbose=False):
        """
        Fit the LiFE FiberModel for data and a set of streamlines associated
        with this data

        Parameters
        ----------
        data : 4D array
            Diffusion-weighted data
        streamline : list
            A bunch of streamlines
        affine : array_like (4, 4)
            The mapping from voxel coordinates to streamline points.
            The voxel_to_rasmm matrix, typically from a NIFTI file.
        evals : list (optional)
            The eigenvalues of the tensor response function used in
            constructing the model signal. Default: [0.001, 0, 0]
        sphere: `dipy.core.Sphere` instance, or False
            Whether to approximate (and cache) the signal on a discrete
            sphere. This may confer a significant speed-up in setting up the
            problem, but is not as accurate. If `False`, we use the exact
            gradients along the streamlines to calculate the matrix, instead of
            an approximation.

        Returns
        -------
        FiberFit class instance
        """
        if affine is None:
            affine = np.eye(4)
        life_matrix, vox_coords = \
            self.setup(streamline, affine, evals=evals, sphere=sphere, processes=processes, verbose=verbose)
        duration1 = time()
        (to_fit, weighted_signal, b0_signal, relative_signal, mean_sig,
         vox_data) = self._signals(data, vox_coords)
        if verbose:
            print("Signals computed in " + str(time() - duration1) + "s")
        duration2 = time()
        beta = opt.sparse_nnls(to_fit, life_matrix)
        if verbose:
            print("beta matrix computed in " + str(time() - duration2) + "s")
        """
        nanvals=0
        for i in range(np.shape(relative_signal)[0]):
            for j in range(np.shape(relative_signal)[1]):
                if math.isnan(relative_signal[i,j]):
                    nanvals += 1
                    
        print(str(nanvals) + " out of " + str(np.size(relative_signal)))
        
        nanvals = 0
        for i in range(np.shape(mean_sig)[0]):
            if math.isnan(mean_sig[i]):
                    nanvals += 1
        
        print(str(nanvals) + " out of " + str(np.size(mean_sig)))
        """

        return FiberFit(self, life_matrix, vox_coords, to_fit, beta,
                        weighted_signal, b0_signal, relative_signal, mean_sig,
                        vox_data, streamline, affine, evals)