# Buggy code
def find_signal_means(b_unique, data_norm, bvals, rho, lb_matrix, w=1e-03):
    r"""Calculates the mean signal for each shell

    Parameters
    ----------
    b_unique : 1d ndarray,
        unique b-values in a vector excluding zero
    data_norm : 1d ndarray,
        normalized diffusion signal
    bvals : 1d ndarray,
        the b-values
    rho : 2d ndarray,
        SH basis matrix for fitting the signal on each shell
    lb_matrix : 2d ndarray,
        Laplace-Beltrami regularization matrix
    w : float,
        weight for the Laplace-Beltrami regularization

    Returns
    -------
    means : 1d ndarray
        the average of the signal for each b-values

    """
    lb = len(b_unique)
    means = np.zeros(lb)
    for u in range(lb):
        ind = bvals == b_unique[u]
        shell = data_norm[ind]
        if np.sum(ind) > 20:
            M = rho[ind, :]

            pseudo_inv = np.dot(np.linalg.inv(np.dot(M.T, M) + w * lb_matrix), M.T)
            coef = np.dot(pseudo_inv, shell)

            means[u] = coef[0] / np.sqrt(4 * np.pi)
        else:
            means[u] = shell.mean()

    return means


# Traceback (AssertionError)
dipy (master)$nosetests dipy/reconst/tests/test_forecast.py
F.....
======================================================================
FAIL: dipy.reconst.tests.test_forecast.test_forecast_positive_constrain
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/arokem/.virtualenvs/dipy/lib/python3.7/site-packages/nose/case.py", line 198, in runTest
self.test(*self.arg)
File "/Users/arokem/.virtualenvs/dipy/lib/python3.7/site-packages/numpy/testing/_private/decorators.py", line 155, in skipper_func
return f(*args, **kwargs)
File "/Users/arokem/source/dipy/dipy/reconst/tests/test_forecast.py", line 53, in test_forecast_positive_constrain
assert_almost_equal(fodf[fodf < 0].sum(), 0, 2)
File "/Users/arokem/.virtualenvs/dipy/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 584, in assert_almost_equal
raise AssertionError(_build_err_msg())
AssertionError:
Arrays are not almost equal to 2 decimals
ACTUAL: -0.015931951384411995
DESIRED: 0

----------------------------------------------------------------------
Ran 6 tests in 0.264s

# Fixed code
def find_signal_means(b_unique, data_norm, bvals, rho, lb_matrix, w=1e-03):
    r"""Calculate the mean signal for each shell.

    Parameters
    ----------
    b_unique : 1d ndarray,
        unique b-values in a vector excluding zero
    data_norm : 1d ndarray,
        normalized diffusion signal
    bvals : 1d ndarray,
        the b-values
    rho : 2d ndarray,
        SH basis matrix for fitting the signal on each shell
    lb_matrix : 2d ndarray,
        Laplace-Beltrami regularization matrix
    w : float,
        weight for the Laplace-Beltrami regularization

    Returns
    -------
    means : 1d ndarray
        the average of the signal for each b-values

    """
    lb = len(b_unique)
    means = np.zeros(lb)
    for u in range(lb):
        ind = bvals == b_unique[u]
        shell = data_norm[ind]
        if np.sum(ind) > 20:
            M = rho[ind, :]

            pseudo_inv = np.dot(np.linalg.inv(np.dot(M.T, M) + w * lb_matrix), M.T)
            coef = np.dot(pseudo_inv, shell)

            means[u] = coef[0] / np.sqrt(4 * np.pi)
        else:
            means[u] = shell.mean()

    return means