import numpy as np
import pkg_resources
import pytest

import optimal_nod_combo.optimal_nods_selection as ons


def test_parse_boolgrid():
    testfile = pkg_resources.resource_filename("optimal_nod_combo", "data/boolgrid_test_data.dat")
    # 11111011
    # 01111111
    # 11011101
    # 00000010
    test_grid = np.array([[1, 1, 1, 1, 1, 0, 1, 1],
                          [0, 1, 1, 1, 1, 1, 1, 1],
                          [1, 1, 0, 1, 1, 1, 0, 1],
                          [0, 0, 0, 0, 0, 0, 1, 0]], dtype=bool)
    assert np.array_equal(ons.parse_boolgrid(testfile), test_grid)


@pytest.mark.xfail
@pytest.mark.parametrize("snr", [100, 200])
@pytest.mark.parametrize("chip", [1, 2, 3, 4])
@pytest.mark.parametrize("seed", [8, 103])   # Seeds that pass this configuration.
def test_sampled_snr(snr, chip, seed):
    """Test sampled snr.

    To counteract the random failing the seed is specified to enable constantly sampled values
    to be drawn for each set of parameters.
    The seeds have been manually set to enable the tests to pass in this configuration.
    """
    # limits = {1: [900, 960], 2: [460, 600], 3: [240, 310], 4: [450, 490]}
    np.random.seed(seed)  # Fix the seed
    x = np.random.normal(1.0, 0.1 / snr, 10000)
    # sampled snr within 10% of specified value.
    assert (abs(ons.sampled_snr(x, chip) - snr) / snr) < 0.10

import numpy as np
import pkg_resources
import pytest

import optimal_nod_combo.optimal_nods_selection as ons


def test_parse_boolgrid():
    testfile = pkg_resources.resource_filename("optimal_nod_combo", "data/boolgrid_test_data.dat")
    # 11111011
    # 01111111
    # 11011101
    # 00000010
    test_grid = np.array([[1, 1, 1, 1, 1, 0, 1, 1],
                          [0, 1, 1, 1, 1, 1, 1, 1],
                          [1, 1, 0, 1, 1, 1, 0, 1],
                          [0, 0, 0, 0, 0, 0, 1, 0]], dtype=bool)
    assert np.array_equal(ons.parse_boolgrid(testfile), test_grid)


@pytest.mark.xfail
@pytest.mark.parametrize("snr", [100, 200])
@pytest.mark.parametrize("chip", [1, 2, 3, 4])
@pytest.mark.parametrize("seed", [8, 103])   # Seeds that pass this configuration.
def test_sampled_snr(snr, chip, seed):
    """Test sampled snr.

    To counteract the random failing the seed is specified to enable constantly sampled values
    to be drawn for each set of parameters.
    The seeds have been manually set to enable the tests to pass in this configuration.
    """
    # limits = {1: [900, 960], 2: [460, 600], 3: [240, 310], 4: [450, 490]}
    np.random.seed(seed)  # Fix the seed
    x = np.random.normal(1.0, 1 / snr, 1000)

    # sampled snr within 10% of specified value.
    assert (abs(ons.sampled_snr(x, chip) - snr) / snr) < 0.1

Adjust snr test to make pass. change noise from 0.1/SNR to 1/SNR