# Path: modality/diptest.py
# def cum_distr(data, w=None):
#     if w is None:
#         w = np.ones(len(data))*1./len(data)
#     eps = 1e-10
#     data_ord = np.argsort(data)
#     data_sort = data[data_ord]
#     w_sort = w[data_ord]
#     data_sort, indices = unique(data_sort, return_index=True, eps=eps, is_sorted=True)
#     if len(indices) < len(data_ord):
#         w_unique = np.zeros(len(indices))
#         for i in range(len(indices)-1):
#             w_unique[i] = np.sum(w_sort[indices[i]:indices[i+1]])
#         w_unique[-1] = np.sum(w_sort[indices[-1]:])
#         w_sort = w_unique
#     wcum = np.cumsum(w_sort)
#     wcum /= wcum[-1]
# 
#     N = len(data_sort)
#     x = np.empty(2*N)
#     x[2*np.arange(N)] = data_sort
#     x[2*np.arange(N)+1] = data_sort
#     y = np.empty(2*N)
#     y[0] = 0
#     y[2*np.arange(N)+1] = wcum
#     y[2*np.arange(N-1)+2] = wcum[:-1]
#     return x, y
# 
# def dip_from_cdf(xF, yF, plotting=False, verbose=False, eps=1e-12):
#     dip, _ = dip_and_closest_unimodal_from_cdf(xF, yF, plotting, verbose, eps)
#     return dip
# 
# def dip_pval_tabinterpol(dip, N):
#     '''
#     dip     -   dip value computed from dip_from_cdf
#     N       -   number of observations
#     '''
#     if qDiptab_df is None:
#         raise DataError("Tabulated p-values not available, {} missing. "
#                         "See installation instructions.".format(qDiptab_file))
# 
#     if np.isnan(N) or N < 10:
#         return np.nan
# 
#     diptable = np.array(qDiptab_df)
#     ps = np.array(qDiptab_df.columns).astype(float)
#     Ns = np.array(qDiptab_df.index)
# 
#     if N >= Ns[-1]:
#         dip = transform_dip_to_other_nbr_pts(dip, N, Ns[-1]-0.1)
#         N = Ns[-1]-0.1
# 
#     iNlow = np.nonzero(Ns < N)[0][-1]
#     qN = (N-Ns[iNlow])/(Ns[iNlow+1]-Ns[iNlow])
#     dip_sqrtN = np.sqrt(N)*dip
#     dip_interpol_sqrtN = (
#         np.sqrt(Ns[iNlow])*diptable[iNlow, :] + qN*(
#             np.sqrt(Ns[iNlow+1])*diptable[iNlow+1, :]-np.sqrt(Ns[iNlow])*diptable[iNlow, :]))
# 
#     if not (dip_interpol_sqrtN < dip_sqrtN).any():
#         return 1
# 
#     iplow = np.nonzero(dip_interpol_sqrtN < dip_sqrtN)[0][-1]
#     if iplow == len(dip_interpol_sqrtN) - 1:
#         return 0
# 
#     qp = (dip_sqrtN-dip_interpol_sqrtN[iplow])/(dip_interpol_sqrtN[iplow+1]-dip_interpol_sqrtN[iplow])
#     p_interpol = ps[iplow] + qp*(ps[iplow+1]-ps[iplow])
# 
#     return 1 - p_interpol


    import cPickle as pickle
    import pickle
import os
import matplotlib.pyplot as plt
import numpy as np
        import time
from scipy.special import beta as betafun
from scipy.optimize import brentq
from .diptest import cum_distr, dip_from_cdf, dip_pval_tabinterpol
from .KernelDensityDerivative import KernelDensityDerivative
        from beeswarm import beeswarm
'''
    Alternative calibration of dip test (no resampling) based on
        M.-Y. Cheng and P. Hall. 1999: Calibrating the excess mass and
        dip tests of modality. Journal of the Royal Statistical Society:
        Series B (Statistical Methodology), 60(3): 579–589, 1998.
'''
from __future__ import unicode_literals
from __future__ import print_function

try:
except ImportError:




def calibrated_dip_test(data, N_bootstrap=1000):
    xF, yF = cum_distr(data)
    dip = dip_from_cdf(xF, yF)
    n_eval = 512
    f_hat = KernelDensityDerivative(data, 0)
    f_bis_hat = KernelDensityDerivative(data, 2)
    x = np.linspace(np.min(data), np.max(data), n_eval)
    f_hat_eval = f_hat.evaluate(x)
    ind_x0_hat = np.argmax(f_hat_eval)
    d_hat = np.abs(f_bis_hat.evaluate(x[ind_x0_hat]))/f_hat_eval[ind_x0_hat]**3
    ref_distr = select_calibration_distribution(d_hat)
    ref_dips = np.zeros(N_bootstrap)
    for i in range(N_bootstrap):
        samp = ref_distr.sample(len(data))
        xF, yF = cum_distr(samp)

# Path: modality/diptest.py
# def cum_distr(data, w=None):
#     if w is None:
#         w = np.ones(len(data))*1./len(data)
#     eps = 1e-10
#     data_ord = np.argsort(data)
#     data_sort = data[data_ord]
#     w_sort = w[data_ord]
#     data_sort, indices = unique(data_sort, return_index=True, eps=eps, is_sorted=True)
#     if len(indices) < len(data_ord):
#         w_unique = np.zeros(len(indices))
#         for i in range(len(indices)-1):
#             w_unique[i] = np.sum(w_sort[indices[i]:indices[i+1]])
#         w_unique[-1] = np.sum(w_sort[indices[-1]:])
#         w_sort = w_unique
#     wcum = np.cumsum(w_sort)
#     wcum /= wcum[-1]
# 
#     N = len(data_sort)
#     x = np.empty(2*N)
#     x[2*np.arange(N)] = data_sort
#     x[2*np.arange(N)+1] = data_sort
#     y = np.empty(2*N)
#     y[0] = 0
#     y[2*np.arange(N)+1] = wcum
#     y[2*np.arange(N-1)+2] = wcum[:-1]
#     return x, y
# 
# def dip_from_cdf(xF, yF, plotting=False, verbose=False, eps=1e-12):
#     dip, _ = dip_and_closest_unimodal_from_cdf(xF, yF, plotting, verbose, eps)
#     return dip
# 
# def dip_pval_tabinterpol(dip, N):
#     '''
#     dip     -   dip value computed from dip_from_cdf
#     N       -   number of observations
#     '''
#     if qDiptab_df is None:
#         raise DataError("Tabulated p-values not available, {} missing. "
#                         "See installation instructions.".format(qDiptab_file))
# 
#     if np.isnan(N) or N < 10:
#         return np.nan
# 
#     diptable = np.array(qDiptab_df)
#     ps = np.array(qDiptab_df.columns).astype(float)
#     Ns = np.array(qDiptab_df.index)
# 
#     if N >= Ns[-1]:
#         dip = transform_dip_to_other_nbr_pts(dip, N, Ns[-1]-0.1)
#         N = Ns[-1]-0.1
# 
#     iNlow = np.nonzero(Ns < N)[0][-1]
#     qN = (N-Ns[iNlow])/(Ns[iNlow+1]-Ns[iNlow])
#     dip_sqrtN = np.sqrt(N)*dip
#     dip_interpol_sqrtN = (
#         np.sqrt(Ns[iNlow])*diptable[iNlow, :] + qN*(
#             np.sqrt(Ns[iNlow+1])*diptable[iNlow+1, :]-np.sqrt(Ns[iNlow])*diptable[iNlow, :]))
# 
#     if not (dip_interpol_sqrtN < dip_sqrtN).any():
#         return 1
# 
#     iplow = np.nonzero(dip_interpol_sqrtN < dip_sqrtN)[0][-1]
#     if iplow == len(dip_interpol_sqrtN) - 1:
#         return 0
# 
#     qp = (dip_sqrtN-dip_interpol_sqrtN[iplow])/(dip_interpol_sqrtN[iplow+1]-dip_interpol_sqrtN[iplow])
#     p_interpol = ps[iplow] + qp*(ps[iplow+1]-ps[iplow])
# 
#     return 1 - p_interpol


# Path: modality/diptest_alternative_calibration.py
    import cPickle as pickle
    import pickle
import os
import matplotlib.pyplot as plt
import numpy as np
        import time
from scipy.special import beta as betafun
from scipy.optimize import brentq
from .diptest import cum_distr, dip_from_cdf, dip_pval_tabinterpol
from .KernelDensityDerivative import KernelDensityDerivative
        from beeswarm import beeswarm

'''
    Alternative calibration of dip test (no resampling) based on
        M.-Y. Cheng and P. Hall. 1999: Calibrating the excess mass and
        dip tests of modality. Journal of the Royal Statistical Society:
        Series B (Statistical Methodology), 60(3): 579–589, 1998.
'''
from __future__ import unicode_literals
from __future__ import print_function

try:
except ImportError:




def calibrated_dip_test(data, N_bootstrap=1000):
    xF, yF = cum_distr(data)
    dip = dip_from_cdf(xF, yF)
    n_eval = 512
    f_hat = KernelDensityDerivative(data, 0)
    f_bis_hat = KernelDensityDerivative(data, 2)
    x = np.linspace(np.min(data), np.max(data), n_eval)
    f_hat_eval = f_hat.evaluate(x)
    ind_x0_hat = np.argmax(f_hat_eval)
    d_hat = np.abs(f_bis_hat.evaluate(x[ind_x0_hat]))/f_hat_eval[ind_x0_hat]**3
    ref_distr = select_calibration_distribution(d_hat)
    ref_dips = np.zeros(N_bootstrap)
    for i in range(N_bootstrap):
        samp = ref_distr.sample(len(data))
        xF, yF = cum_distr(samp)

        ref_dips[i] = dip_from_cdf(xF, yF)