# Buggy code
def grid_to_graph(
    n_x, n_y, n_z=1, mask=None, return_as=sparse.coo_matrix, dtype=np.int
):
    """Graph of the pixel-to-pixel connections

    Edges exist if 2 voxels are connected.

    Parameters
    ===========
    n_x: int
        Dimension in x axis
    n_y: int
        Dimension in y axis
    n_z: int, optional, default 1
        Dimension in z axis
    mask : ndarray of booleans, optional
        An optional mask of the image, to consider only part of the
        pixels.
    return_as: np.ndarray or a sparse matrix class, optional
        The class to use to build the returned adjacency matrix.
    dtype: dtype, optional, default int
        The data of the returned sparse matrix. By default it is int

    Notes
    ===========
    For sklearn versions 0.14.1 and prior, return_as=np.ndarray was handled
    by returning a dense np.matrix instance.  Going forward, np.ndarray
    returns an np.ndarray, as expected.

    For compatibility, user code relying on this method should wrap its
    calls in ``np.asarray`` to avoid type issues.
    """
    return _to_graph(n_x, n_y, n_z, mask=mask, return_as=return_as, dtype=dtype)


# Traceback (IndexError)
======================================================================
ERROR: sklearn.tests.test_common.test_regressors_train('RANSACRegressor', <class 'sklearn.linear_model.ransac.RANSACRegr
essor'>, array([[-0.44836249, -0.47282444, -1.20608008, ..., -0.75500806,
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\Python27\lib\site-packages\sklearn\tests\test_common.py", line 850, in check_regressors_train
assert_raises(ValueError, regressor.fit, X, y[:-1])
File "C:\Python27\lib\unittest\case.py", line 473, in assertRaises
callableObj(*args, **kwargs)
File "C:\Python27\lib\site-packages\sklearn\linear_model\ransac.py", line 261, in fit
y_subset = y[subset_idxs]
IndexError: index 199 is out of bounds for axis 0 with size 199

# Fixed code
def grid_to_graph(
    n_x, n_y, n_z=1, mask=None, return_as=sparse.coo_matrix, dtype=np.int
):
    """Graph of the pixel-to-pixel connections

    Edges exist if 2 voxels are connected.

    Parameters
    ===========
    n_x: int
        Dimension in x axis
    n_y: int
        Dimension in y axis
    n_z: int, optional, default 1
        Dimension in z axis
    mask : ndarray of booleans, optional
        An optional mask of the image, to consider only part of the
        pixels.
    return_as: np.ndarray or a sparse matrix class, optional
        The class to use to build the returned adjacency matrix.
    dtype: dtype, optional, default int
        The data of the returned sparse matrix. By default it is int

    Notes
    =====
    For sklearn versions 0.14.1 and prior, return_as=np.ndarray was handled
    by returning a dense np.matrix instance.  Going forward, np.ndarray
    returns an np.ndarray, as expected.

    For compatibility, user code relying on this method should wrap its
    calls in ``np.asarray`` to avoid type issues.
    """
    return _to_graph(n_x, n_y, n_z, mask=mask, return_as=return_as, dtype=dtype)