from scona.make_corr_matrices import create_residuals_df, \
    get_non_numeric_cols, create_corrmat
from scona.stats_functions import residuals
import pytest
import pandas as pd
import numpy as np


@pytest.fixture
def subject_array():
    return np.array([np.arange(x, x+4) for x in [7, 4, 1]])


@pytest.fixture
def subject_data():
    columns = ['noggin_left', 'noggin_right', 'day of week', 'haircut']
    data = subject_array()
    return pd.DataFrame(data, columns=columns)


@pytest.fixture
def subject_residuals():
    columns = ['noggin_left', 'noggin_right', 'day of week', 'haircut']
    data = np.array([[1, 0, -1], [0, -5, 5], [1, 2, 3], [300, 200, 100]]).T
    return pd.DataFrame(data, columns=columns)


def test_non_numeric_cols():
    df = subject_residuals()
    assert get_non_numeric_cols(df).size == 0
    df['hats'] = 'stetson'
    assert get_non_numeric_cols(df) == np.array(['hats'])


def test_create_residuals_df_covars_plural():
    names, covars = ['noggin_left', 'noggin_right'], ['day of week', 'haircut']
    array_resids = [residuals(subject_array()[:, 2:].T,
                    subject_array()[:, i]) for i in [0, 1]]
    np.testing.assert_almost_equal(
        np.array(create_residuals_df(subject_data(), names, covars)[names]),
        np.array(array_resids).T)


def test_create_residuals_df_covars_singular():
    names, covars = ['noggin_left', 'noggin_right'], ['day of week']
    array_resids = [residuals(subject_array()[:, 2:3].T,
                    subject_array()[:, i]) for i in [0, 1]]
    np.testing.assert_almost_equal(
        np.array(create_residuals_df(subject_data(), names, covars)[names]),
        np.array(array_resids).T)


def test_create_residuals_df_covars_none():
    names, covars = ['noggin_left', 'noggin_right'], []
    array_resids = [residuals(subject_array()[:, 2:2].T, subject_array()[:, i])
                    for i in [0, 1]]
    np.testing.assert_almost_equal(
        np.array(create_residuals_df(subject_data(), names, covars)[names]),
        np.array(array_resids).T)


def test_create_corrmat_pearson():
    df_res = subject_residuals()
    names = ['noggin_left', 'noggin_right']
    np.testing.assert_almost_equal(
        np.array(create_corrmat(df_res, names)),
        np.array([[1, -0.5], [-0.5, 1]]))


def test_create_corrmat_spearman():
    df_res = subject_residuals()
    names = ['noggin_left', 'noggin_right']
    np.testing.assert_almost_equal(
        np.array(create_corrmat(df_res, names, method='spearman')),
        np.array([[1, -0.5], [-0.5, 1]]))

@@ -325,16 +325,29 @@
 ct_data(
+subject_array
 ):%0A    c
@@ -437,18 +437,16 @@
 ct_array
-()
 %0A    ret
@@ -751,16 +751,33 @@
 ic_cols(
+subject_residuals
 ):%0A    d
@@ -793,26 +793,24 @@
 ct_residuals
-()
 %0A    assert 
@@ -974,16 +974,43 @@
 _plural(
+subject_array, subject_data
 ):%0A    n
@@ -1123,26 +1123,24 @@
 ubject_array
-()
 %5B:, 2:%5D.T,%0A 
@@ -1163,34 +1163,32 @@
    subject_array
-()
 %5B:, i%5D) for i in
@@ -1273,34 +1273,32 @@
 _df(subject_data
-()
 , names, covars)
@@ -1388,16 +1388,43 @@
 ingular(
+subject_array, subject_data
 ):%0A    n
@@ -1526,26 +1526,24 @@
 ubject_array
-()
 %5B:, 2:3%5D.T,%0A
@@ -1571,26 +1571,24 @@
 ubject_array
-()
 %5B:, i%5D) for 
@@ -1677,34 +1677,32 @@
 _df(subject_data
-()
 , names, covars)
@@ -1788,16 +1788,43 @@
 rs_none(
+subject_array, subject_data
 ):%0A    n
@@ -1913,26 +1913,24 @@
 ubject_array
-()
 %5B:, 2:2%5D.T, 
@@ -1942,18 +1942,16 @@
 ct_array
-()
 %5B:, i%5D)%0A
@@ -2072,18 +2072,16 @@
 ect_data
-()
 , names,
@@ -2166,16 +2166,33 @@
 pearson(
+subject_residuals
 ):%0A    d
@@ -2208,34 +2208,32 @@
 ubject_residuals
-()
 %0A    names = %5B'n
@@ -2423,16 +2423,33 @@
 pearman(
+subject_residuals
 ):%0A    d
@@ -2473,18 +2473,16 @@
 esiduals
-()
 %0A    nam

remove calls to pytest fixtures