# Path: gala/evaluate.py
# def nzcol(mat, row_idx):
# def pixel_wise_boundary_precision_recall(pred, gt):
# def wiggle_room_precision_recall(pred, boundary, margin=2, connectivity=1):
# def get_stratified_sample(ar, n):
# def edit_distance(aseg, gt, size_threshold=1000, sp=None):
# def raw_edit_distance(aseg, gt, size_threshold=1000):
# def contingency_table(seg, gt, *, ignore_seg=(), ignore_gt=(), norm=True):
# def assignment_table(seg_or_ctable, gt=None, *, dtype=np.bool_):
# def _mindiff(arr):
#     def __init__(self, arg1, shape=None, dtype=None, copy=False,
#                  max_num_rows=None, max_nonzero=None,
#                  expansion_factor=2):
#     def data(self):
#     def data(self, value):
#     def indices(self):
#     def indices(self, value):
#     def indptr(self):
#     def indptr(self, value):
#     def __setitem__(self, index, value):
#     def _append_row_at(self, index, value):
#     def _zero_row(self, index):
#     def _double_indptr(self):
#     def _double_data_and_indices(self):
# def merge_contingency_table(a, b, ignore_seg=[0], ignore_gt=[0]):
# def xlogx(x, out=None, in_place=False):
# def special_points_evaluate(eval_fct, coords, flatten=True, coord_format=True):
#     def special_eval_fct(x, y, *args, **kwargs):
# def make_synaptic_functions(fn, fcts):
# def make_synaptic_vi(fn):
# def vi(x, y=None, weights=np.ones(2), ignore_x=[0], ignore_y=[0]):
# def split_vi(x, y=None, ignore_x=[0], ignore_y=[0]):
# def vi_pairwise_matrix(segs, split=False):
#         def dmerge(x, y): return split_vi(x, y)[0]
#         def dsplit(x, y): return split_vi(x, y)[1]
# def split_vi_threshold(tup):
# def vi_by_threshold(ucm, gt, ignore_seg=[], ignore_gt=[], npoints=None,
#                                                             nprocessors=None):
# def rand_by_threshold(ucm, gt, npoints=None):
# def adapted_rand_error(seg, gt, all_stats=False):
# def calc_entropy(split_vals, count):
# def split_vi_mem(x, y):
# def divide_rows(matrix, column, in_place=False):
# def divide_columns(matrix, row, in_place=False):
# def vi_tables(x, y=None, ignore_x=[0], ignore_y=[0]):
# def sorted_vi_components(s1, s2, ignore1=[0], ignore2=[0], compress=False):
# def split_components(idx, cont, num_elems=4, axis=0):
# def rand_values(cont_table):
# def rand_index(x, y=None):
# def adj_rand_index(x, y=None):
# def fm_index(x, y=None):
# def reduce_vi(fn_pattern='testing/%i/flat-single-channel-tr%i-%i-%.2f.lzf.h5',
#         iterable=[(ts, tr, ts) for ts, tr in it.permutations(range(8), 2)],
#         thresholds=np.arange(0, 1.01, 0.01)):
# def sem(ar, axis=None):
# def vi_statistics(vi_table):
# class csrRowExpandableCSR(sparse.csr_matrix):


import numpy as np
from numpy.testing import assert_equal, assert_almost_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


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

# Path: gala/evaluate.py
# def nzcol(mat, row_idx):
# def pixel_wise_boundary_precision_recall(pred, gt):
# def wiggle_room_precision_recall(pred, boundary, margin=2, connectivity=1):
# def get_stratified_sample(ar, n):
# def edit_distance(aseg, gt, size_threshold=1000, sp=None):
# def raw_edit_distance(aseg, gt, size_threshold=1000):
# def contingency_table(seg, gt, *, ignore_seg=(), ignore_gt=(), norm=True):
# def assignment_table(seg_or_ctable, gt=None, *, dtype=np.bool_):
# def _mindiff(arr):
#     def __init__(self, arg1, shape=None, dtype=None, copy=False,
#                  max_num_rows=None, max_nonzero=None,
#                  expansion_factor=2):
#     def data(self):
#     def data(self, value):
#     def indices(self):
#     def indices(self, value):
#     def indptr(self):
#     def indptr(self, value):
#     def __setitem__(self, index, value):
#     def _append_row_at(self, index, value):
#     def _zero_row(self, index):
#     def _double_indptr(self):
#     def _double_data_and_indices(self):
# def merge_contingency_table(a, b, ignore_seg=[0], ignore_gt=[0]):
# def xlogx(x, out=None, in_place=False):
# def special_points_evaluate(eval_fct, coords, flatten=True, coord_format=True):
#     def special_eval_fct(x, y, *args, **kwargs):
# def make_synaptic_functions(fn, fcts):
# def make_synaptic_vi(fn):
# def vi(x, y=None, weights=np.ones(2), ignore_x=[0], ignore_y=[0]):
# def split_vi(x, y=None, ignore_x=[0], ignore_y=[0]):
# def vi_pairwise_matrix(segs, split=False):
#         def dmerge(x, y): return split_vi(x, y)[0]
#         def dsplit(x, y): return split_vi(x, y)[1]
# def split_vi_threshold(tup):
# def vi_by_threshold(ucm, gt, ignore_seg=[], ignore_gt=[], npoints=None,
#                                                             nprocessors=None):
# def rand_by_threshold(ucm, gt, npoints=None):
# def adapted_rand_error(seg, gt, all_stats=False):
# def calc_entropy(split_vals, count):
# def split_vi_mem(x, y):
# def divide_rows(matrix, column, in_place=False):
# def divide_columns(matrix, row, in_place=False):
# def vi_tables(x, y=None, ignore_x=[0], ignore_y=[0]):
# def sorted_vi_components(s1, s2, ignore1=[0], ignore2=[0], compress=False):
# def split_components(idx, cont, num_elems=4, axis=0):
# def rand_values(cont_table):
# def rand_index(x, y=None):
# def adj_rand_index(x, y=None):
# def fm_index(x, y=None):
# def reduce_vi(fn_pattern='testing/%i/flat-single-channel-tr%i-%i-%.2f.lzf.h5',
#         iterable=[(ts, tr, ts) for ts, tr in it.permutations(range(8), 2)],
#         thresholds=np.arange(0, 1.01, 0.01)):
# def sem(ar, axis=None):
# def vi_statistics(vi_table):
# class csrRowExpandableCSR(sparse.csr_matrix):


# Path: tests/test_evaluate.py
import numpy as np
from numpy.testing import assert_equal, assert_almost_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


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

    assert_equal(ev.vi(seg, gt), 1)