<commit_before>from __future__ import division, unicode_literals

from itertools import permutations, product
from random import Random

import numpy as np
import pytest

try:
    from unittest import mock
except ImportError:
    import mock

from catpy.client import CoordinateTransformer, StackOrientation

COUNT = 20
SEED = 1

DIMS = 'xyz'
DIRECTIONS = 'stack_to_project', 'project_to_stack'
EXAMPLE_COORD = 10

ZOOM_LEVELS = (-2, 0, 1)


@pytest.fixture
def default_res():
    return {
        'x': 1.0,
        'y': 2.0,
        'z': 3.0
    }


@pytest.fixture
def default_trans():
    return {
        'x': 10.0,
        'y': 20.0,
        'z': 30.0
    }


default_orientation = 'XY'


@pytest.fixture
def coordinate_generator():
    """Return a generator which returns `count` randomly-generated coordinates in the range [-1000, 1000], in a mixture
    of floats and ints"""
    def wrapped(count=COUNT, seed=SEED):
        twister = Random(seed)

        def rand(is_int=False):
            n = (twister.random() - 0.5) * 1000
            return n if not is_int else int(n)

        for _ in range(count):
            is_int = twister.random() > 0.5
            yield {dim: rand(is_int) for dim in DIMS}

    return wrapped


@pytest.fixture
def catmaid_mock(default_res, default_trans):
    catmaid = mock.Mock()
    stack_info = {
        'resolution': default_res,
        'translation': default_trans,
        'orientation': default_orientation
    }
    catmaid.configure_mock(**{'get.return_value': stack_info})
    return catmaid


@pytest.fixture
def default_coord_transformer(default_res, default_trans):
    return CoordinateTransformer(default_res, default_trans)


@pytest.mark.parametrize('res_fixture', [default_res, lambda: None])
@pytest.mark.parametrize('trans_fixture', [default_trans, lambda: None])
def test_instantiate(res_fixture, trans_fixture):
    assert CoordinateTransformer(res_fixture(), trans_fixture())


def test_from_catmaid(default_coord_transformer, catmaid_mock):
    assert CoordinateTransformer.from_catmaid(catmaid_mock, None) == default_coord_transformer


@pytest.mark.parametrize('dim', DIMS)
def test_project_to_stack_coord(dim, default_coord_transformer, default_res, default_trans):
    project_coord = EXAMPLE_COORD

    expected_response = (project_coord - default_trans[dim]) / default_res[dim]
    response = default_coord_transformer.project_to_stack_coord(dim, project_coord)

    assert response[1] == expected_response


@pytest.mark.parametrize('dim', DIMS)
def test_stack_to_project_coord(dim, default_coord_transformer, default_res, default_trans):
    stack_coord = EXAMPLE_COORD<commit_msg>fix tests for pytest 4<commit_after>from __future__ import division, unicode_literals

from itertools import permutations, product
from random import Random

import numpy as np
import pytest

try:
    from unittest import mock
except ImportError:
    import mock

from catpy.client import CoordinateTransformer, StackOrientation

COUNT = 20
SEED = 1

DIMS = 'xyz'
DIRECTIONS = 'stack_to_project', 'project_to_stack'
EXAMPLE_COORD = 10

ZOOM_LEVELS = (-2, 0, 1)


@pytest.fixture
def default_res():
    return {
        'x': 1.0,
        'y': 2.0,
        'z': 3.0
    }


@pytest.fixture
def default_trans():
    return {
        'x': 10.0,
        'y': 20.0,
        'z': 30.0
    }


default_orientation = 'XY'


@pytest.fixture
def coordinate_generator():
    """Return a generator which returns `count` randomly-generated coordinates in the range [-1000, 1000], in a mixture
    of floats and ints"""
    def wrapped(count=COUNT, seed=SEED):
        twister = Random(seed)

        def rand(is_int=False):
            n = (twister.random() - 0.5) * 1000
            return n if not is_int else int(n)

        for _ in range(count):
            is_int = twister.random() > 0.5
            yield {dim: rand(is_int) for dim in DIMS}

    return wrapped


@pytest.fixture
def catmaid_mock(default_res, default_trans):
    catmaid = mock.Mock()
    stack_info = {
        'resolution': default_res,
        'translation': default_trans,
        'orientation': default_orientation
    }
    catmaid.configure_mock(**{'get.return_value': stack_info})
    return catmaid


@pytest.fixture
def default_coord_transformer(default_res, default_trans):
    return CoordinateTransformer(default_res, default_trans)


@pytest.mark.parametrize('has_res', [True, False])
@pytest.mark.parametrize('has_trans', [True, False])
def test_instantiate(default_res, default_trans, has_res, has_trans):
    if not has_res:
        default_res = None
    if not has_trans:
        default_trans = None
    assert CoordinateTransformer(default_res, default_trans)


def test_from_catmaid(default_coord_transformer, catmaid_mock):
    assert CoordinateTransformer.from_catmaid(catmaid_mock, None) == default_coord_transformer


@pytest.mark.parametrize('dim', DIMS)
def test_project_to_stack_coord(dim, default_coord_transformer, default_res, default_trans):
    project_coord = EXAMPLE_COORD

    expected_response = (project_coord - default_trans[dim]) / default_res[dim]
    response = default_coord_transformer.project_to_stack_coord(dim, project_coord)

    assert response[1] == expected_response


@pytest.mark.parametrize('dim', DIMS)
def test_stack_to_project_coord(dim, default_coord_transformer, default_res, default_trans):
    stack_coord = EXAMPLE_COORD
{'fraction_non_alphanumeric': 0.09850027772634697, 'fraction_numerical': 0.014997222736530272, 'mean_word_length': 3.88868778280543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7142840', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1756, 'n_words': 413}