from dqc_wrapper.utils.utils_stats import *
from dqc_wrapper.unit_tests.conftest import dirs
import logging


def test_get_plausible_ranges():
    df = pd.read_csv(dirs.stats_csv)  # min 10 max 24
    res, msg = get_plausible_ranges(df, 7, 28, None, None, 'degC', logging.getLogger())
    assert res == 'ok'
    assert 'variable is within the expected ranges' in msg


def test_update_stats_row():
    df = pd.read_csv(dirs.stats_csv)
    hist = np.histogram_bin_edges(np.random.random(500), bins=30, range=(0, 100))
    df_stats = update_stats_row('sample20200101.nc_c', df, 'all', 123, 456, 789, hist)
    assert df_stats.loc['sample20200101.nc_c']['Min'] == 123
    df_stats = update_stats_row('new_row', df, 'all', 123, 456, 789, hist)
    assert 'new_row' in df_stats.loc[:, 'File_dt']
    assert df_stats.loc['new_row']['Max'] == 456


def test_get_min_max():
    minmax = {'temperature': [-100, 100], 'precipitation': [0, 200]}
    res = get_min_max(minmax)
    assert res == (-100, 200)


def test_get_basic_stats():
    hist = np.histogram_bin_edges(np.random.random(100), bins=5, range=(0, 100))
    res = get_basic_stats([30, 60, 90], hist)
    assert res == [30, 15, 40, 25]


def test_stats_from_hist():
    hist = np.histogram_bin_edges(np.random.random(100), bins=5, range=(0, 100))
    res = stats_from_hist(hist, [30, 60, 90])
    assert (res == np.array([15, 30, 40])).all()


""" Class valid_range """


def prep_stats():
    data = pd.read_csv(dirs.stats_csv)
    data.iloc[1] = data.values[0]
    data = data.drop(['File_dt', 'dataType'], axis=1)
    vr = valid_range(data=data, dr_gap=0.2, r_zero=0.01)
    return vr


def test_set_parameters():
    vr = prep_stats()
    vr.set_parameters()
    assert vr.N > 0
    assert type(vr.q_left) == list
    assert type(vr.q_right) == list


def test_quantiles_from_hist():
    vr = prep_stats()
    vr.set_parameters()
    res = vr.quantiles_from_hist(vr.q_left)
    assert len(res) == 2
    assert np.isfinite(res).all()


def test_outlier_gaps():
    vr = valid_range(file_path=Path(dirs.hist_csv).parent, file_name=Path(dirs.hist_csv).name)
    res = vr.outlier_gaps()
    assert res


def test_find_gaps():
    vr = valid_range(file_path=Path(dirs.hist_csv).parent, file_name=Path(dirs.hist_csv).name)
    res = vr.find_gaps()
    assert res[0] < res[3]
    assert res[1] < res[2]


"""Class multiparametric valid range"""


def test_set_vr():
    data = pd.read_csv(dirs.stats_csv)
    data.iloc[1] = data.values[0]
    data = data.drop(['File_dt', 'dataType'], axis=1)
    mvr = multiparametric_valid_range()
    res = mvr.set_vr(data)
    assert type(res) == valid_range


def test_get():
    mvr = multiparametric_valid_range()
    r, o_r, err, others = mvr.get(file=dirs.hist_csv)
    assert not err


def test_get_extra():
    mvr = multiparametric_valid_range()
    mvr.get(file=dirs.hist_csv)  # to set nx
    res = mvr.get_extra([-100, 100])
    assert res


def test_min_None():
    assert min_None(None, 6) == 6
    assert min_None(5, 6) == 5


def test_max_None():
    assert max_None(None, 5) == 5
    assert max_None(5, 6) == 6

