# Path: test/conftest.py
# def assert_no_expr_in_log(flog, expr='error'):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
# 
#     if matched_lines:
#         print(matched_lines)
#         assert len(matched_lines) == 0
# 
# def assert_expr_in_log(flog, expr):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
#     assert len(matched_lines)


import os
import sys
import pytest
import abkhazia.features as features
import abkhazia.acoustic as acoustic
import abkhazia.utils as utils
from .conftest import assert_no_expr_in_log, assert_expr_in_log

# There was a bug with more than 9 jobs (when more than 9 available
# cores/nodes)
@pytest.mark.parametrize('njobs', [4])  # , 11])
def test_acoustic_njobs(corpus, features, njobs, tmpdir, lang_args):
    output_dir = str(tmpdir.mkdir('am-mono'))
    flog = os.path.join(output_dir, 'am-mono.log')
    log = utils.logger.get_log(flog)
    am = acoustic.Monophone(corpus, features, output_dir, lang_args, log)

    am.njobs = njobs
    am.options['total-gaussians'].value = 10
    am.options['num-iterations'].value = 2
    am.options['realign-iterations'].value = [1]

    try:
        am.compute()
    except RuntimeError as err:
        # dump the log to stdout
        sys.stdout.write('####################\n')
        sys.stdout.write(open(flog, 'r').read())
        # log_inner = os.path.join(
        #     output_dir, 'recipe/exp/mono/log/align.0.14.log')
        # sys.stdout.write(open(log_inner, 'r').read())
        sys.stdout.write('####################\n')
        raise err

    acoustic.check_acoustic_model(output_dir)
    assert acoustic.model_type(output_dir) == 'mono'
    assert str(am.options['num-iterations']) == '2'

# Path: test/conftest.py
# def assert_no_expr_in_log(flog, expr='error'):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
# 
#     if matched_lines:
#         print(matched_lines)
#         assert len(matched_lines) == 0
# 
# def assert_expr_in_log(flog, expr):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
#     assert len(matched_lines)


# Path: test/test_acoustic.py
import os
import sys
import pytest
import abkhazia.features as features
import abkhazia.acoustic as acoustic
import abkhazia.utils as utils
from .conftest import assert_no_expr_in_log, assert_expr_in_log


# There was a bug with more than 9 jobs (when more than 9 available
# cores/nodes)
@pytest.mark.parametrize('njobs', [4])  # , 11])
def test_acoustic_njobs(corpus, features, njobs, tmpdir, lang_args):
    output_dir = str(tmpdir.mkdir('am-mono'))
    flog = os.path.join(output_dir, 'am-mono.log')
    log = utils.logger.get_log(flog)
    am = acoustic.Monophone(corpus, features, output_dir, lang_args, log)

    am.njobs = njobs
    am.options['total-gaussians'].value = 10
    am.options['num-iterations'].value = 2
    am.options['realign-iterations'].value = [1]

    try:
        am.compute()
    except RuntimeError as err:
        # dump the log to stdout
        sys.stdout.write('####################\n')
        sys.stdout.write(open(flog, 'r').read())
        # log_inner = os.path.join(
        #     output_dir, 'recipe/exp/mono/log/align.0.14.log')
        # sys.stdout.write(open(log_inner, 'r').read())
        sys.stdout.write('####################\n')
        raise err

    acoustic.check_acoustic_model(output_dir)
    assert acoustic.model_type(output_dir) == 'mono'
    assert str(am.options['num-iterations']) == '2'

    assert_expr_in_log(flog, ' --num-iters 2 ')