from __future__ import division, absolute_import, print_function

import warnings

import numpy as np

try:
    import scipy.stats as stats
except ImportError:
    pass

from .common import Benchmark

class Anderson_KSamp(Benchmark):
    def setup(self, *args):
        self.rand = [np.random.normal(loc=i, size=1000) for i in range(3)]

    def time_anderson_ksamp(self):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            stats.anderson_ksamp(self.rand)


class CorrelationFunctions(Benchmark):
    param_names = ['alternative']
    params = [
        ['two-sided', 'less', 'greater']
    ]

    def setup(self, mode):
        a = np.random.rand(2,2) * 10
        self.a = a

    def time_fisher_exact(self, alternative):   
        oddsratio, pvalue = stats.fisher_exact(self.a, alternative=alternative)


class InferentialStats(Benchmark):
    def setup(self):
        np.random.seed(12345678)
        self.a = stats.norm.rvs(loc=5, scale=10, size=500)
        self.b = stats.norm.rvs(loc=8, scale=10, size=20)
        self.c = stats.norm.rvs(loc=8, scale=20, size=20)

    def time_ttest_ind_same_var(self):
        # test different sized sample with variances
        stats.ttest_ind(self.a, self.b)
        stats.ttest_ind(self.a, self.b, equal_var=False)

    def time_ttest_ind_diff_var(self):
        # test different sized sample with different variances
        stats.ttest_ind(self.a, self.c)
        stats.ttest_ind(self.a, self.c, equal_var=False)


class Distribution(Benchmark):
    param_names = ['distribution', 'properties']    
    params = [
        ['cauchy', 'gamma', 'beta'],
        ['pdf', 'cdf', 'rvs', 'fit']
    ]
    
    def setup(self, distribution, properties):
        np.random.seed(12345678)
        self.x = np.random.rand(100)

    def time_distribution(self, distribution, properties):
        if distribution == 'gamma':
            if properties == 'pdf':
                stats.gamma.pdf(self.x, a=5, loc=4, scale=10)
            elif properties == 'cdf':
                stats.gamma.cdf(self.x, a=5, loc=4, scale=10)
            elif properties == 'rvs':
                stats.gamma.rvs(size=1000, a=5, loc=4, scale=10)
            elif properties == 'fit':
                stats.gamma.fit(self.x, loc=4, scale=10)
        elif distribution == 'cauchy':
            if properties == 'pdf':
                stats.cauchy.pdf(self.x, loc=4, scale=10)
            elif properties == 'cdf':
                stats.cauchy.cdf(self.x, loc=4, scale=10)
            elif properties == 'rvs':
                stats.cauchy.rvs(size=1000, loc=4, scale=10)
            elif properties == 'fit':
                stats.cauchy.fit(self.x, loc=4, scale=10)
        elif distribution == 'beta':
            if properties == 'pdf':
                stats.beta.pdf(self.x, a=5, b=3, loc=4, scale=10)
            elif properties == 'cdf':
                stats.beta.cdf(self.x, a=5, b=3, loc=4, scale=10)
            elif properties == 'rvs':
                stats.beta.rvs(size=1000, a=5, b=3, loc=4, scale=10)
            elif properties == 'fit':
                stats.beta.fit(self.x, loc=4, scale=10)

    # Retain old benchmark results (remove this if changing the benchmark)
    time_distribution.version = "fb22ae5386501008d945783921fe44aef3f82c1dafc40cddfaccaeec38b792b0"


class DescriptiveStats(Benchmark):
    param_names = ['n_levels']
    params = [
        [10, 1000]
    ]

    def setup(self, n_levels):
        np.random.seed(12345678)
        self.levels = np.random.randint(n_levels, size=(1000, 10))

    def time_mode(self, n_levels):
        stats.mode(self.levels, axis=0)


[((813, 864), 'scipy.stats.fisher_exact', 'stats.fisher_exact', (['self.a'], {'alternative': 'alternative'}), '(self.a, alternative=alternative)\n', (831, 864), True, 'import scipy.stats as stats\n'), ((931, 955), 'numpy.random.seed', 'np.random.seed', (['(12345678)'], {}), '(12345678)\n', (945, 955), True, 'import numpy as np\n'), ((973, 1014), 'scipy.stats.norm.rvs', 'stats.norm.rvs', ([], {'loc': '(5)', 'scale': '(10)', 'size': '(500)'}), '(loc=5, scale=10, size=500)\n', (987, 1014), True, 'import scipy.stats as stats\n'), ((1032, 1072), 'scipy.stats.norm.rvs', 'stats.norm.rvs', ([], {'loc': '(8)', 'scale': '(10)', 'size': '(20)'}), '(loc=8, scale=10, size=20)\n', (1046, 1072), True, 'import scipy.stats as stats\n'), ((1090, 1130), 'scipy.stats.norm.rvs', 'stats.norm.rvs', ([], {'loc': '(8)', 'scale': '(20)', 'size': '(20)'}), '(loc=8, scale=20, size=20)\n', (1104, 1130), True, 'import scipy.stats as stats\n'), ((1232, 1263), 'scipy.stats.ttest_ind', 'stats.ttest_ind', (['self.a', 'self.b'], {}), '(self.a, self.b)\n', (1247, 1263), True, 'import scipy.stats as stats\n'), ((1272, 1320), 'scipy.stats.ttest_ind', 'stats.ttest_ind', (['self.a', 'self.b'], {'equal_var': '(False)'}), '(self.a, self.b, equal_var=False)\n', (1287, 1320), True, 'import scipy.stats as stats\n'), ((1432, 1463), 'scipy.stats.ttest_ind', 'stats.ttest_ind', (['self.a', 'self.c'], {}), '(self.a, self.c)\n', (1447, 1463), True, 'import scipy.stats as stats\n'), ((1472, 1520), 'scipy.stats.ttest_ind', 'stats.ttest_ind', (['self.a', 'self.c'], {'equal_var': '(False)'}), '(self.a, self.c, equal_var=False)\n', (1487, 1520), True, 'import scipy.stats as stats\n'), ((1762, 1786), 'numpy.random.seed', 'np.random.seed', (['(12345678)'], {}), '(12345678)\n', (1776, 1786), True, 'import numpy as np\n'), ((1804, 1823), 'numpy.random.rand', 'np.random.rand', (['(100)'], {}), '(100)\n', (1818, 1823), True, 'import numpy as np\n'), ((3507, 3531), 'numpy.random.seed', 'np.random.seed', (['(12345678)'], {}), '(12345678)\n', (3521, 3531), True, 'import numpy as np\n'), ((3554, 3598), 'numpy.random.randint', 'np.random.randint', (['n_levels'], {'size': '(1000, 10)'}), '(n_levels, size=(1000, 10))\n', (3571, 3598), True, 'import numpy as np\n'), ((3643, 3674), 'scipy.stats.mode', 'stats.mode', (['self.levels'], {'axis': '(0)'}), '(self.levels, axis=0)\n', (3653, 3674), True, 'import scipy.stats as stats\n'), ((283, 317), 'numpy.random.normal', 'np.random.normal', ([], {'loc': 'i', 'size': '(1000)'}), '(loc=i, size=1000)\n', (299, 317), True, 'import numpy as np\n'), ((386, 411), 'warnings.catch_warnings', 'warnings.catch_warnings', ([], {}), '()\n', (409, 411), False, 'import warnings\n'), ((425, 469), 'warnings.simplefilter', 'warnings.simplefilter', (['"""ignore"""', 'UserWarning'], {}), "('ignore', UserWarning)\n", (446, 469), False, 'import warnings\n'), ((482, 513), 'scipy.stats.anderson_ksamp', 'stats.anderson_ksamp', (['self.rand'], {}), '(self.rand)\n', (502, 513), True, 'import scipy.stats as stats\n'), ((691, 711), 'numpy.random.rand', 'np.random.rand', (['(2)', '(2)'], {}), '(2, 2)\n', (705, 711), True, 'import numpy as np\n'), ((1972, 2017), 'scipy.stats.gamma.pdf', 'stats.gamma.pdf', (['self.x'], {'a': '(5)', 'loc': '(4)', 'scale': '(10)'}), '(self.x, a=5, loc=4, scale=10)\n', (1987, 2017), True, 'import scipy.stats as stats\n'), ((2072, 2117), 'scipy.stats.gamma.cdf', 'stats.gamma.cdf', (['self.x'], {'a': '(5)', 'loc': '(4)', 'scale': '(10)'}), '(self.x, a=5, loc=4, scale=10)\n', (2087, 2117), True, 'import scipy.stats as stats\n'), ((2407, 2448), 'scipy.stats.cauchy.pdf', 'stats.cauchy.pdf', (['self.x'], {'loc': '(4)', 'scale': '(10)'}), '(self.x, loc=4, scale=10)\n', (2423, 2448), True, 'import scipy.stats as stats\n'), ((2172, 2220), 'scipy.stats.gamma.rvs', 'stats.gamma.rvs', ([], {'size': '(1000)', 'a': '(5)', 'loc': '(4)', 'scale': '(10)'}), '(size=1000, a=5, loc=4, scale=10)\n', (2187, 2220), True, 'import scipy.stats as stats\n'), ((2503, 2544), 'scipy.stats.cauchy.cdf', 'stats.cauchy.cdf', (['self.x'], {'loc': '(4)', 'scale': '(10)'}), '(self.x, loc=4, scale=10)\n', (2519, 2544), True, 'import scipy.stats as stats\n'), ((2829, 2878), 'scipy.stats.beta.pdf', 'stats.beta.pdf', (['self.x'], {'a': '(5)', 'b': '(3)', 'loc': '(4)', 'scale': '(10)'}), '(self.x, a=5, b=3, loc=4, scale=10)\n', (2843, 2878), True, 'import scipy.stats as stats\n'), ((2275, 2315), 'scipy.stats.gamma.fit', 'stats.gamma.fit', (['self.x'], {'loc': '(4)', 'scale': '(10)'}), '(self.x, loc=4, scale=10)\n', (2290, 2315), True, 'import scipy.stats as stats\n'), ((2599, 2643), 'scipy.stats.cauchy.rvs', 'stats.cauchy.rvs', ([], {'size': '(1000)', 'loc': '(4)', 'scale': '(10)'}), '(size=1000, loc=4, scale=10)\n', (2615, 2643), True, 'import scipy.stats as stats\n'), ((2933, 2982), 'scipy.stats.beta.cdf', 'stats.beta.cdf', (['self.x'], {'a': '(5)', 'b': '(3)', 'loc': '(4)', 'scale': '(10)'}), '(self.x, a=5, b=3, loc=4, scale=10)\n', (2947, 2982), True, 'import scipy.stats as stats\n'), ((2698, 2739), 'scipy.stats.cauchy.fit', 'stats.cauchy.fit', (['self.x'], {'loc': '(4)', 'scale': '(10)'}), '(self.x, loc=4, scale=10)\n', (2714, 2739), True, 'import scipy.stats as stats\n'), ((3037, 3089), 'scipy.stats.beta.rvs', 'stats.beta.rvs', ([], {'size': '(1000)', 'a': '(5)', 'b': '(3)', 'loc': '(4)', 'scale': '(10)'}), '(size=1000, a=5, b=3, loc=4, scale=10)\n', (3051, 3089), True, 'import scipy.stats as stats\n'), ((3144, 3183), 'scipy.stats.beta.fit', 'stats.beta.fit', (['self.x'], {'loc': '(4)', 'scale': '(10)'}), '(self.x, loc=4, scale=10)\n', (3158, 3183), True, 'import scipy.stats as stats\n')]