<commit_before># -*- coding: utf-8 -*-

import numpy as np
from numpy import testing

# dynfunconn
from dyfunconn.ts import (aaft,
                          fdr,
                          phase_rand,
                          surrogate_analysis,
                          entropy_reduction_rate,
                          symoblic_transfer_entropy,
                          embed_delay,
                          sample_entropy,
                          ordinal_pattern_similarity,
                          permutation_entropy,
                          entropy,
                          rr_order_patterns,
                          wald,
                          markov_matrix, transition_rate, occupancy_time,
                          teager_kaiser_energy,
                          dcorr,
                          fisher_score,
                          fisher_z_plv,
                          fisher_z,
                          complexity_index,
                          fnn,
                          cv,
                          icc_31)


ts = None


def setup_module(module):
    global ts

    ts = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts = ts[0:1, 0:128].ravel()


def test_surrogate_analysis():
    rng = np.random.RandomState(0)

    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_vals, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, estimator_func=None, ts1_no_surr=False, rng=rng)

    expected_result = np.load("data/test_ts_surrogates.npy").squeeze()
    np.testing.assert_array_equal(surrogates, expected_result)

    expected_result = np.load("data/test_ts_surrogates_p_val.npy").squeeze()
    assert p_vals == expected_result

    np.save("/tmp/out.npy", surr_vals)

    expected_result = np.load("data/test_ts_surrogates_corr.npy").squeeze()
    np.testing.assert_array_almost_equal(surr_vals, expected_result)


def test_surrogate_analysis_fdr():
    rng = np.random.RandomState(0)

    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_val, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, estimator_func=None, ts1_no_surr=False, rng=rng)

    num_ts = 2
    num_pvals = num_ts  * (num_ts -1) / 2.0
    num_pvals = np.int32(num_pvals)
    p_vals = np.ones([num_pvals, 1]) * p_val
    h, crit_p = fdr(p_vals, 0.01, 'pdep')

    expected_result = np.load("data/test_ts_surrogates_fdr_h.npy")
    assert h == expected_result

    expected_result = np.load("data/test_ts_surrogates_fdr_crit_p.npy")
    assert crit_p == expected_result


def test_surrogate_analysis2():
    rng = np.random.RandomState(0)

    data = np.load(
        "../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_val, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, ts1_no_surr=True, rng=rng)

    expected_result = np.load("data/test_ts_surrogates2.npy")
    np.testing.assert_array_equal(surrogates, expected_result)

    expected_result = np.load("data/test_ts_surrogates2_p_val.npy")
    assert p_val == expected_result


def test_surrogate_analysis2_fdr():
    rng = np.random.RandomState(0)

    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_val, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, ts1_no_surr=True, rng=rng)

    num_ts = 2
    num_pvals = num_ts  * (num_ts -1) / 2.0
    num_pvals = np.int32(num_pvals)
    p_vals = np.ones([num_pvals, 1]) * p_val
    h, crit_p = fdr(p_vals, 0.01, 'pdep')

    expected_result = np.load("data/test_ts_surrogates2_fdr_h.npy")
    assert h == expected_result


def test_rr_order_similarity():
    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:128].ravel()
    ts2 = data[1, 0:128].ravel()

    m = 3
    tau = 1
    cstr = rr_order_patterns(ts1, ts2, m, tau)

    expected_result = np.load("data/test_ts_cstr.npy")
    assert cstr == expected_result


def test_pemutation_entropy():
    dim = 3
    tau = 1

    pe, npe = permutation_entropy(ts, dim, tau)

    expected_result = np.load("data/test_ts_permutation_entropy.npy")
    assert pe == expected_result

    expected_result = np.load("data/test_ts_n_permutation_entropy.npy")
    assert npe == expected_result


def test_ordinal_pattern_similariy():
    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:128].ravel()
    ts2 = data[1, 0:128].ravel()

    dim = 3
    tau = 1
    diss, ts, distr = ordinal_pattern_similarity(ts1, ts2, dim, tau)

    expected_result = np.load("data/test_ts_dissimilarity.npy")
    assert diss == expected_result

    expected_result = np.load("data/test_ts_ordinal_patterns.npy")
    np.testing.assert_array_equal(ts, expected_result)

    expected_result = np.load("data/test_ts_patterns_distribution.npy")
    np.testing.assert_array_equal(distr, expected_result)


def test_ts_embedded_ts():
    dim = 3
    tau = 1
    embedded_ts = embed_delay(ts, dim, tau)

    expected_result = np.load("data/test_ts_embedded_ts.npy")
    np.testing.assert_array_equal(embedded_ts, expected_result)


def test_phase_rand():
    global ts
    num_surr = 1
    rng = np.random.RandomState(0)

    rp = phase_rand(ts, num_surr, rng)

    expected_result = np.load("data/test_ts_phase_rand.npy")
    np.testing.assert_array_equal(rp, expected_result)


def test_aaft():
    global ts
    num_surr = 1
    rng = np.random.RandomState(0)

    s = aaft(ts, num_surr, rng)

    expected_result = np.load("data/test_ts_afft.npy")
    np.testing.assert_array_equal(s, expected_result)


def test_fdr():
    num_pvals = 2 * (2 - 1) / 2.0
    num_pvals = np.int32(num_pvals)

    pvals = np.ones((1, num_pvals)) * 0.0025
    q = 0.01
    method = "pdep"

    h, crit_p = fdr(pvals, q, method)

    h = h.ravel()[0]
    crit_p = crit_p.ravel()[0]

    assert h == True<commit_msg>Update nosetests to pass. Convert to float32/nan_to_num and use assert_all_close(x, y, rtol=1e-10, atol=0.0) where needed<commit_after># -*- coding: utf-8 -*-

import numpy as np
from numpy import testing

# dynfunconn
from dyfunconn.ts import (aaft,
                          fdr,
                          phase_rand,
                          surrogate_analysis,
                          entropy_reduction_rate,
                          symoblic_transfer_entropy,
                          embed_delay,
                          sample_entropy,
                          ordinal_pattern_similarity,
                          permutation_entropy,
                          entropy,
                          rr_order_patterns,
                          wald,
                          markov_matrix, transition_rate, occupancy_time,
                          teager_kaiser_energy,
                          dcorr,
                          fisher_score,
                          fisher_z_plv,
                          fisher_z,
                          complexity_index,
                          fnn,
                          cv,
                          icc_31)


ts = None


def setup_module(module):
    global ts

    ts = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts = ts[0:1, 0:128].ravel()


def test_surrogate_analysis():
    rng = np.random.RandomState(0)

    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_vals, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, estimator_func=None, ts1_no_surr=False, rng=rng)

    expected_result = np.load("data/test_ts_surrogates.npy").squeeze()
    np.testing.assert_array_equal(surrogates, expected_result)

    expected_result = np.load("data/test_ts_surrogates_p_val.npy").squeeze()
    assert p_vals == expected_result

    np.save("/tmp/out.npy", surr_vals)

    expected_result = np.load("data/test_ts_surrogates_corr.npy").squeeze()
    np.testing.assert_array_almost_equal(surr_vals, expected_result)


def test_surrogate_analysis_fdr():
    rng = np.random.RandomState(0)

    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_val, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, estimator_func=None, ts1_no_surr=False, rng=rng)

    num_ts = 2
    num_pvals = num_ts  * (num_ts -1) / 2.0
    num_pvals = np.int32(num_pvals)
    p_vals = np.ones([num_pvals, 1]) * p_val
    h, crit_p = fdr(p_vals, 0.01, 'pdep')

    expected_result = np.load("data/test_ts_surrogates_fdr_h.npy")
    assert h == expected_result

    expected_result = np.load("data/test_ts_surrogates_fdr_crit_p.npy")
    assert crit_p == expected_result


def test_surrogate_analysis2():
    rng = np.random.RandomState(0)

    data = np.load(
        "../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_val, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, ts1_no_surr=True, rng=rng)

    expected_result = np.load("data/test_ts_surrogates2.npy")
    np.testing.assert_array_equal(surrogates, expected_result)

    expected_result = np.load("data/test_ts_surrogates2_p_val.npy")
    assert p_val == expected_result


def test_surrogate_analysis2_fdr():
    rng = np.random.RandomState(0)

    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:512].ravel()
    ts2 = data[1, 0:512].ravel()

    p_val, surr_vals, surrogates, r_value = surrogate_analysis(
        ts1, ts2, num_surr=1000, ts1_no_surr=True, rng=rng)

    num_ts = 2
    num_pvals = num_ts  * (num_ts -1) / 2.0
    num_pvals = np.int32(num_pvals)
    p_vals = np.ones([num_pvals, 1]) * p_val
    h, crit_p = fdr(p_vals, 0.01, 'pdep')

    expected_result = np.load("data/test_ts_surrogates2_fdr_h.npy")
    assert h == expected_result


def test_rr_order_similarity():
    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:128].ravel()
    ts2 = data[1, 0:128].ravel()

    m = 3
    tau = 1
    cstr = rr_order_patterns(ts1, ts2, m, tau)

    expected_result = np.load("data/test_ts_cstr.npy")
    assert cstr == expected_result


def test_pemutation_entropy():
    dim = 3
    tau = 1

    pe, npe = permutation_entropy(ts, dim, tau)

    expected_result = np.load("data/test_ts_permutation_entropy.npy")
    assert pe == expected_result

    expected_result = np.load("data/test_ts_n_permutation_entropy.npy")
    assert npe == expected_result


def test_ordinal_pattern_similariy():
    data = np.load("../examples/data/eeg_32chans_10secs.npy")
    ts1 = data[0, 0:128].ravel()
    ts2 = data[1, 0:128].ravel()

    dim = 3
    tau = 1
    diss, ts, distr = ordinal_pattern_similarity(ts1, ts2, dim, tau)

    expected_result = np.load("data/test_ts_dissimilarity.npy")
    assert diss == expected_result

    expected_result = np.load("data/test_ts_ordinal_patterns.npy")
    np.testing.assert_array_equal(ts, expected_result)

    expected_result = np.load("data/test_ts_patterns_distribution.npy")
    np.testing.assert_array_equal(distr, expected_result)


def test_ts_embedded_ts():
    dim = 3
    tau = 1
    embedded_ts = embed_delay(ts, dim, tau)

    expected_result = np.load("data/test_ts_embedded_ts.npy")
    np.testing.assert_array_equal(embedded_ts, expected_result)


def test_phase_rand():
    global ts
    num_surr = 1
    rng = np.random.RandomState(0)

    rp = phase_rand(ts, num_surr, rng)
    rp = np.float32(rp)

    expected_result = np.load("data/test_ts_phase_rand.npy")
    expected_result = np.float32(expected_result)
    np.testing.assert_array_equal(rp, expected_result)


def test_aaft():
    global ts
    num_surr = 1
    rng = np.random.RandomState(0)

    s = aaft(ts, num_surr, rng)

    expected_result = np.load("data/test_ts_afft.npy")
    np.testing.assert_array_equal(s, expected_result)


def test_fdr():
    num_pvals = 2 * (2 - 1) / 2.0
    num_pvals = np.int32(num_pvals)

    pvals = np.ones((1, num_pvals)) * 0.0025
    q = 0.01
    method = "pdep"

    h, crit_p = fdr(pvals, q, method)

    h = h.ravel()[0]
    crit_p = crit_p.ravel()[0]

    assert h == True
{'fraction_non_alphanumeric': 0.1157177225340818, 'fraction_numerical': 0.03295910184442662, 'mean_word_length': 2.5439045183290707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3050333', 'n_tokens_mistral': 4995, 'n_tokens_neox': 4575, 'n_words': 759}