"""Test suite for the ``Fiji_metrics`` module"""

from os import environ
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import pytest

from bangmetric.isbi12 import pixel_error, rand_error, warp_error

EPSILON = 1e-4


def test_no_environment_variable():

    backup = environ["FIJI_EXE_PATH"]
    environ["FIJI_EXE_PATH"] = "/no/path"

    with pytest.raises(ValueError):
        # -- creating a fake prediction
        y_pred = np.eye(32).astype('f')
        y_pred = gaussian_filter(y_pred, 2.)
        y_pred -= y_pred.min()
        y_pred /= y_pred.max()
        y_pred = 1. - y_pred

        # -- creating a fake ground truth
        y_true = 1 - np.eye(32).astype(np.uint8)

        pixel_error(y_true, y_pred)

    environ["FIJI_EXE_PATH"] = backup

@pytest.mark.skipif("environ.get('FIJI_EXE_PATH') is None")
def test_simple_segmentation_with_provided_path():

    # -- creating a fake y_prediction
    y_pred = np.eye(32).astype('f')
    y_pred = gaussian_filter(y_pred, 2.)
    y_pred -= y_pred.min()
    y_pred /= y_pred.max()
    y_pred = 1. - y_pred

    # -- creating a fake ground truth
    y_true = 1 - np.eye(32).astype(np.uint8)

    pe = pixel_error(y_true, y_pred)
    re = rand_error(y_true, y_pred)
    we = warp_error(y_true, y_pred)

    assert np.abs(pe - 0.010183) < EPSILON
    assert np.abs(re - 0.020385) < EPSILON
    assert np.abs(we - 0.0) < EPSILON

@@ -65,17 +65,90 @@
  environ
+, path%0Afrom scipy import misc%0Afrom matplotlib.pyplot import matshow, show
 %0A
-
 import n
@@ -288,16 +288,101 @@
 rp_error
+%0Afrom bangmetric.isbi12 import warp2d%0A%0AMYPATH = path.abspath(path.dirname(__file__)) 
 %0A%0AEPSILO
@@ -931,16 +931,17 @@
 backup%0A%0A
+%0A
 @pytest.
@@ -1511,32 +1511,32 @@
 0385) %3C EPSILON%0A
-
     assert np.ab
@@ -1545,20 +1545,457 @@
 we - 0.0) %3C EPSILON%0A
+%0A%0Adef test_simple_warp2d():%0A%0A    # -- read in y_pred and y_true from PNG images%0A    y_pred = misc.imread(path.join(MYPATH, 'y_pred128.png'), flatten=True)%0A    y_true = misc.imread(path.join(MYPATH, 'y_true128.png'), flatten=True)%0A    gt = misc.imread(path.join(MYPATH, 'y_warp128.png'), flatten=True) %3E 0%0A%0A    # -- compute the %22warped annotations%22%0A    gv = warp2d(y_true, y_pred)%0A    print gv%0A%0A    assert (abs(gt - gv) %3C EPSILON).all()%0A%0A

add warp2d wrapper from Fiji