Skip to content

Commit

Permalink
Remove unused functions and improve testing of evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
jni committed Sep 21, 2016
1 parent 40d9aa3 commit cb70694
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 73 deletions.
73 changes: 0 additions & 73 deletions gala/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,79 +42,6 @@ def nzcol(mat, row_idx):
return mat[row_idx].nonzero()[1]


def sparse_min(mat, axis=None):
"""Compute the minimum value in a sparse matrix (optionally over an axis).
This function mimics the numpy.min() API for sparse.CSC or CSR matrices.
Parameters
----------
mat : a scipy.sparse csc or csr matrix
The matrix for which to compute the min.
axis : int in {0, 1}, optional
Compute the minimum over each column (`axis=0`) or over each row
(`axis=1`). By default, compute over entire matrix.
Returns
-------
mn : mat.dtype (if `axis=None`) or np.ndarray of shape (mat.shape[1-axis],)
The minimum value in the array or along an axis.
"""
mn = - sparse_max(-mat, axis)
return mn


def sparse_max(mat, axis=None):
"""Compute the maximum value in a sparse matrix (optionally over an axis).
This function mimics the numpy.max() API for sparse.CSC or CSR matrices.
Parameters
----------
mat : a scipy.sparse csc or csr matrix
The matrix for which to compute the max.
axis : int in {0, 1}, optional
Compute the maximum over each column (`axis=0`) or over each row
(`axis=1`). By default, compute over entire matrix.
Returns
-------
mx : mat.dtype (if `axis=None`) or np.ndarray of shape (mat.shape[1-axis],)
The maximum value in the array or along an axis.
"""
if type(mat) == sparse.csr_matrix:
mat = mat.tocsc()
if axis is None:
mx = np.max(mat.data)
elif axis == 0:
mx = sparse_csr_row_max(mat.T)
elif axis == 1:
mx = sparse_csr_row_max(mat.tocsr())
else:
raise ValueError("Invalid axis %i for matrix (2 dimensional)." % axis)
return mx


def sparse_csr_row_max(csr_mat):
"""Compute maximum over each row of a CSR format sparse matrix.
Parameters
----------
csr_mat : scipy.sparse.csr_matrix
The input matrix.
Returns
-------
mx : np.ndarray of shape `(mat.shape[0],)`
The maximum along every row.
"""
ret = np.zeros(csr_mat.shape[0])
row_diff = np.diff(csr_mat.indptr)
ret[row_diff != 0] = np.maximum.reduceat(csr_mat.data,
csr_mat.indptr[:-1][row_diff > 0])
return ret


def pixel_wise_boundary_precision_recall(pred, gt):
"""Evaluate voxel prediction accuracy against a ground truth.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ def test_contingency_table():
[0. , 0. , 0.375],
[0.125, 0. , 0. ]]))
assert ct.shape == ct0.shape


def test_vi():
seg = np.array([1, 2, 3, 4])
gt = np.array([1, 1, 8, 8])
assert_equal(ev.vi(seg, gt), 1)

0 comments on commit cb70694

Please sign in to comment.