Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix behavior of contingency table ignored rows #67

Merged
merged 4 commits into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Release notes
0.3
===

0.3.2
-----

- Bug fix: missing import in ``test_gala.py``. This was caused by rebasing
commits from post-0.3 onto 0.3.


0.3.1
-----

Expand Down
43 changes: 5 additions & 38 deletions gala/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,6 @@ def sparse_csr_row_max(csr_mat):
return ret


def bin_values(a, bins=255):
"""Return an array with its values discretised to the given number of bins.

Parameters
----------
a : np.ndarray, arbitrary shape
The input array.
bins : int, optional
The number of bins in which to put the data. default: 255.

Returns
-------
b : np.ndarray, same shape as a
The output array, such that values in bin X are replaced by mean(X).
"""
if len(np.unique(a)) < 2*bins:
return a.copy()
b = np.zeros_like(a)
m, M = a.min(), a.max()
r = M - m # the range of the data
step = r / bins
lows = np.arange(m, M, step)
highs = np.arange(m+step, M+step, step)
for low, high in zip(lows, highs):
locations = np.flatnonzero((low <= a) * (a < high))
if len(locations) > 0:
values = a.ravel()[locations]
b.ravel()[locations] = values.mean()
return b


def pixel_wise_boundary_precision_recall(pred, gt):
"""Evaluate voxel prediction accuracy against a ground truth.

Expand Down Expand Up @@ -362,16 +331,14 @@ def contingency_table(seg, gt, ignore_seg=[0], ignore_gt=[0], norm=True):
"""
segr = seg.ravel()
gtr = gt.ravel()
ij = np.vstack((segr, gtr))
selector = np.ones(segr.shape, np.bool)
ignored = np.zeros(segr.shape, np.bool)
data = np.ones(len(gtr))
for i in ignore_seg:
selector[segr == i] = 0
ignored[segr == i] = True
for j in ignore_gt:
selector[gtr == j] = 0
ij = ij[:, selector]
data = data[selector]
cont = sparse.coo_matrix((data, ij)).tocsc()
ignored[gtr == j] = True
data[ignored] = 0
cont = sparse.coo_matrix((data, (segr, gtr))).tocsc()
if norm:
cont /= float(cont.sum())
return cont
Expand Down
15 changes: 15 additions & 0 deletions tests/test_evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np
from numpy.testing import assert_equal
from gala import evaluate as ev

def test_contingency_table():
seg = np.array([0, 1, 1, 1, 2, 2, 2, 3])
gt = np.array([1, 1, 1, 2, 2, 2, 2, 0])
ct = ev.contingency_table(seg, gt, ignore_seg=[], ignore_gt=[])
ct0 = ev.contingency_table(seg, gt, ignore_seg=[0], ignore_gt=[0])
ctd = ct.todense()
assert_equal(ctd, np.array([[0. , 0.125, 0. ],
[0. , 0.25 , 0.125],
[0. , 0. , 0.375],
[0.125, 0. , 0. ]]))
assert ct.shape == ct0.shape