# pythonpath modification to make hytra available 
# for import without requiring it to be installed
import os

SCRIPTS_PATH = 'scripts'
assert os.path.exists(SCRIPTS_PATH), 'Scripts path not found (Maybe you forgot to run the test from the root directory?)'
os.chdir(SCRIPTS_PATH)

import sys
sys.path.insert(0, os.path.abspath('..'))
from subprocess import check_call
import vigra
import numpy as np

def test_mergerResolvingTestDataset():
    check_call(["python",
                "../hytra/configtemplates/create_config.py",
                "--in",
                "../tests/mergerResolvingTestDataset/config_template.ini",
                "--out",
                "../tests/mergerResolvingTestDataset/test_config.ini",
                "embryonicDir",
                ".."
                ])
    check_call(["python", "pipeline.py", "--config", "../tests/mergerResolvingTestDataset/test_config.ini"])

    for f in range(4):
        frame = vigra.impex.readImage('../tests/mergerResolvingTestDataset/ctc_RES/mask00{}.tif'.format(f))
        assert(np.all(np.unique(frame) == [0,1,2]))

    with open('../tests/mergerResolvingTestDataset/ctc_RES/res_track.txt', 'r') as resultFile:
        assert(resultFile.read() == '1 0 3 0\n2 0 3 0\n')

def test_mergerResolvingTestDataset_withoutTracklets():
    check_call(["python",
                "../hytra/configtemplates/create_config.py",
                "--in",
                "../tests/mergerResolvingTestDataset/config_template.ini",
                "--out",
                "../tests/mergerResolvingTestDataset/test_config.ini",
                "embryonicDir",
                ".."
                ])
    check_call(["python", "pipeline.py", "--config", "../tests/mergerResolvingTestDataset/test_config.ini", "--without-tracklets"])

    for f in range(4):
        frame = vigra.impex.readImage('../tests/mergerResolvingTestDataset/ctc_RES/mask00{}.tif'.format(f))
        assert(np.all(np.unique(frame) == [0,1,2]))

    with open('../tests/mergerResolvingTestDataset/ctc_RES/res_track.txt', 'r') as resultFile:
        assert(resultFile.read() == '1 0 3 0\n2 0 3 0\n')

def test_divisionTestDataset():
    check_call(["python",
                "../hytra/configtemplates/create_config.py",
                "--in",
                "../tests/divisionTestDataset/config_template.ini",
                "--out",
                "../tests/divisionTestDataset/test_config.ini",
                "embryonicDir",
                ".."
                ])
    check_call(["python", "pipeline.py", "--config", "../tests/divisionTestDataset/test_config.ini"])

    expectedIds = [[0, 1], [0, 1], [0, 2, 3], [0, 2, 3]]
    for f in range(4):
        frame = vigra.impex.readImage('../tests/divisionTestDataset/ctc_RES/mask00{}.tif'.format(f))
        assert(np.all(np.unique(frame) == expectedIds[f]))

    with open('../tests/divisionTestDataset/ctc_RES/res_track.txt', 'r') as resultFile:
        assert(resultFile.read() == '1 0 1 0\n2 2 3 1\n3 2 3 1\n')

def test_divisionTestDataset_withoutTracklets():
    check_call(["python",
                "../hytra/configtemplates/create_config.py",
                "--in",
                "../tests/divisionTestDataset/config_template.ini",
                "--out",
                "../tests/divisionTestDataset/test_config.ini",
                "embryonicDir",
                ".."
                ])
    check_call(["python", "pipeline.py", "--config", "../tests/divisionTestDataset/test_config.ini", "--without-tracklets"])

    expectedIds = [[0, 1], [0, 1], [0, 2, 3], [0, 2, 3]]
    for f in range(4):
        frame = vigra.impex.readImage('../tests/divisionTestDataset/ctc_RES/mask00{}.tif'.format(f))
        assert(np.all(np.unique(frame) == expectedIds[f]))

    with open('../tests/divisionTestDataset/ctc_RES/res_track.txt', 'r') as resultFile:
        assert(resultFile.read() == '1 0 1 0\n2 2 3 1\n3 2 3 1\n')

@@ -3881,28 +3881,963 @@
 0 1 0%5Cn2 2 3 1%5Cn3 2 3 1%5Cn')%0A
+%0Adef test_noscripts_mergerResolvingTestDataset():%0A    check_call(%5B%22python%22,%0A                %22../hytra/configtemplates/create_config.py%22,%0A                %22--in%22,%0A                %22../tests/mergerResolvingTestDataset/config_template.ini%22,%0A                %22--out%22,%0A                %22../tests/mergerResolvingTestDataset/test_config.ini%22,%0A                %22embryonicDir%22,%0A                %22..%22%0A                %5D)%0A    check_call(%5B%22python%22, %22pipelineNoScripts.py%22, %22--config%22, %22../tests/mergerResolvingTestDataset/test_config.ini%22%5D)%0A%0A    for f in range(4):%0A        frame = vigra.impex.readImage('../tests/mergerResolvingTestDataset/ctc_RES/mask00%7B%7D.tif'.format(f))%0A        assert(np.all(np.unique(frame) == %5B0,1,2%5D))%0A%0A    with open('../tests/mergerResolvingTestDataset/ctc_RES/res_track.txt', 'r') as resultFile:%0A        assert(resultFile.read() == '1 0 3 0%5Cn2 0 3 0%5Cn')%0A%0Aif __name__ == %22__main__%22:%0A    test_noscripts_mergerResolvingTestDataset()

Add a test for the pipeline without scripts