# -*- coding: latin-1 -*-
#
# intended to implement a power-law fitting routine as specified in.....
# http://www.santafe.edu/~aaronc/powerlaws/
#
# The MLE for the power-law alpha is very easy to derive given knowledge
# of the lowest value at which a power law holds, but that point is
# difficult to derive and must be acquired iteratively.

"""
numpy/matplotlib version of plfit.py
====================================
A power-law distribution fitter based on code by Aaron Clauset.  It can use
fortran, cython, or numpy-based power-law fitting 'backends'.  Fortran's
fastest.

Requires pylab (matplotlib), which requires numpy

Example very simple use::

    from plfit import plfit

    MyPL = plfit(mydata)
    MyPL.plotpdf(log=True)

"""
from __future__ import print_function

import numpy as np
import time
import pylab
try:
    from . import fplfit
    fortranOK = True
except:
    fortranOK = False
try:
    from . import cplfit
    cyOK = True
except:
    cyOK = False

import numpy.random as npr
from numpy import log,log10,sum,argmin,argmax,exp,min,max
try:
    import scipy.stats
    scipyOK = True
except ImportError:
    scipyOK = False
    print("scipy didn't import.  Can't compute certain basic statistics.")

def alpha_gen(x):
    """ Create a mappable function alpha to apply to each xmin in a list of xmins.
    This is essentially the slow version of fplfit/cplfit, though I bet it could
    be speeded up with a clever use of parellel_map.  Not intended to be used by users.

    Docstring for the generated alpha function::

        Given a sorted data set and a minimum, returns power law MLE fit
        data is passed as a keyword parameter so that it can be vectorized

        If there is only one element, return alpha=0
    """
    def alpha_(xmin,x=x):
        """
        Given a sorted data set and a minimum, returns power law MLE fit
        data is passed as a keyword parameter so that it can be vectorized

        If there is only one element, return alpha=0
        """
        gexmin = x>=xmin
        n = np.count_nonzero(gexmin)
        if n < 2:
            return 0
        x = x[gexmin]
        a = 1 + float(n) / sum(log(x/xmin))
        return a
    return alpha_

def kstest_gen(x,unique=False,finite=False):
    """
    Create a mappable function kstest to apply to each xmin in a list of xmins.

    Parameters
    ----------
    unique : bool
        If set, will filter the input array 'x' to its unique elements.
        Normally, this would be done at an earlier step, so `unique`
        can be disabled for performance improvement
    finite : bool
        Apply the finite-sample correction from Clauset et al 2007...
        Not clear yet which equation this comes from.

    Docstring for the generated kstest function::

        Given a sorted data set and a minimum, returns power law MLE ks-test
        against the data

        data is passed as a keyword parameter so that it can be vectorized

        The returned value is the "D" parameter in the ks test.
    """
    def kstest_(xmin,x=x):
        """
        Given a sorted data set and a minimum, returns power law MLE ks-test
        against the data

        data is passed as a keyword parameter so that it can be vectorized

        The returned value is the "D" parameter in the ks test.
        """
        if unique:
            x = np.unique(x)
        x = x[x>=xmin]
        n = len(x)
        if n == 0: return np.inf
        a = 1+float(n) / sum(log(x/xmin))
        if finite:
            a = a*(n-1.)/n+1./n
        cx = np.arange(n,dtype='float')/float(n)
        cf = 1-(xmin/x)**(a-1)
        ks = max(abs(cf-cx))
        return ks
    return kstest_

def sigma(alpha, n):
    """
    Clauset et al 2007 equation 3.2:
        sigma = (alpha-1)/sqrt(n)
    """
    return (alpha-1.) / n**0.5

class plfit(object):
    """
    A Python implementation of the Matlab code `http://www.santafe.edu/~aaronc/powerlaws/plfit.m`_
    from `http://www.santafe.edu/~aaronc/powerlaws/`_.

    See `A. Clauset, C.R. Shalizi, and M.E.J. Newman, "Power-law distributions
    in empirical data" SIAM Review, 51, 661-703 (2009). (arXiv:0706.1062)
    <http://arxiv.org/abs/0706.1062>`_

    The output "alpha" is defined such that :math:`p(x) \sim (x/xmin)^{-alpha}`
    """

    def __init__(self,x,**kwargs):
        """
        Initializes and fits the power law.  Can pass "quiet" to turn off
        output (except for warnings; "silent" turns off warnings)
        """
        x = np.array(x) # make sure x is an array, otherwise the next step fails
        if (x<0).sum() > 0:
            print("Removed %i negative points" % ((x<0).sum()))
            x = x[x>0]
        self.data = x
        self.plfit(**kwargs)

    def plfit(self, nosmall=True, finite=False, quiet=False, silent=False,
              usefortran=False, usecy=False, xmin=None, verbose=False,
              discrete=None, discrete_approx=True, discrete_n_alpha=1000,
              skip_consistency_check=False):
        """
        A Python implementation of the Matlab code
        http://www.santafe.edu/~aaronc/powerlaws/plfit.m
        from http://www.santafe.edu/~aaronc/powerlaws/

        See A. Clauset, C.R. Shalizi, and M.E.J. Newman, "Power-law distributions
        in empirical data" SIAM Review, 51, 661-703 (2009). (arXiv:0706.1062)
        http://arxiv.org/abs/0706.1062

        There are 3 implementations of xmin estimation.  The fortran version is
        fastest, the C (cython) version is ~10% slower, and the python version
        is ~3x slower than the fortran version.  Also, the cython code suffers
        ~2% numerical error relative to the fortran and python for unknown
        reasons.

        There is also a discrete version implemented in python - it is
        different from the continous version!

        Parameters
        ----------
        discrete : bool or None
            If *discrete* is None, the code will try to determine whether the
            data set is discrete or continous based on the uniqueness of the
            data; if your data set is continuous but you have any non-unique
            data points (e.g., flagged "bad" data), the "automatic"
            determination will fail.  If *discrete* is True or False, the
            discrete or continuous fitter will be used, respectively.
        xmin : float or int
            If you specify xmin, the fitter will only determine alpha assuming
            the given xmin; the rest of the code (and most of the complexity)
            is determining an estimate for xmin and alpha.
        nosmall : bool
            When on, the code rejects low s/n points.  WARNING: This option,
            which is on by default, may result in different answers than the
            original Matlab code and the "powerlaw" python package
        finite : bool
            There is a 'finite-size bias' to the estimator.  The "alpha" the
            code measures is "alpha-hat" s.t. ᾶ = (nα-1)/(n-1), or α = (1 + ᾶ
            (n-1)) / n
        quiet : bool
            If False, delivers messages about what fitter is used and the fit
            results
        verbose : bool
            Deliver descriptive messages about the fit parameters (only if
            `quiet==False`)
        silent : bool
            If True, will print NO messages
        skip_consistency_check : bool
            The code will normally perform a consistency check to make sure the
            alpha value computed by the fitter matches the alpha value computed
            directly in python.  It is possible for numerical differences to
            creep in, usually at the 10^-6 or less level.  If you see an
            exception reporting this type of error, skipping the check can be
            the appropriate next step.

        Returns
        -------
        (xmin, alpha)
        The best-fit xmin and alpha values
        """
        x = self.data
        if any(x < 0):
            raise ValueError("Power law distributions are only valid for "
                             "positive data.  Remove negative values before "
                             "fitting.")
        z = np.sort(x)

        # xmins = the unique values of x that can be used as the threshold for
        # the power law fit
        # argxmins = the index of each of these possible thresholds
        xmins,argxmins = np.unique(z,return_index=True)
        self._nunique = len(xmins)

        if self._nunique == len(x) and discrete is None:
            if verbose:
                print("Using CONTINUOUS fitter because there are no repeated "
                      "values.")
            discrete = False
        elif self._nunique < len(x) and discrete is None:
            if verbose:
                print("Using DISCRETE fitter because there are repeated "
                      "values.")
            discrete = True

        t = time.time()
        if xmin is None:
            if discrete:
                self.discrete_best_alpha(approximate=discrete_approx,
                                         n_alpha=discrete_n_alpha,
                                         verbose=verbose,
                                         finite=finite)
                return self._xmin,self._alpha
            elif usefortran and fortranOK:
                kstest_values,alpha_values = fplfit.plfit(z, 0)
                if not quiet:
                    print(("FORTRAN plfit executed in %f seconds" % (time.time()-t)))
            elif usecy and cyOK:
                kstest_values,alpha_values = cplfit.plfit_loop(z,
                                                               nosmall=False,
                                                               zunique=xmins,
                                                               argunique=argxmins)
                if not quiet:
                    print(("CYTHON plfit executed in %f seconds" % (time.time()-t)))
            else:
                # python (numpy) version
                f_alpha = alpha_gen(z)
                f_kstest = kstest_gen(z)
                alpha_values = np.asarray(list(map(f_alpha,xmins)),
                                          dtype='float')
                kstest_values = np.asarray(list(map(f_kstest,xmins)),
                                           dtype='float')
                if not quiet:
                    print(("PYTHON plfit executed in %f seconds" % (time.time()-t)))

            if not quiet:
                if usefortran and not fortranOK:
                    raise ImportError("fortran fplfit did not load")
                if usecy and not cyOK:
                    raise ImportError("cython cplfit did not load")

            # For each alpha, the number of included data points is
            # total data length - first index of xmin
            # No +1 is needed: xmin is included.
            sigma = (alpha_values-1)/np.sqrt(len(z)-argxmins)
            # I had changed it to this, but I think this is wrong.
            # sigma = (alpha_values-1)/np.sqrt(len(z)-np.arange(len(z)))

            if nosmall:
                # test to make sure the number of data points is high enough
                # to provide a reasonable s/n on the computed alpha
                goodvals = sigma<0.1
                nmax = argmin(goodvals)
                if nmax <= 0:
                    nmax = len(xmins) - 1
                    if not silent:
                        print("Not enough data left after flagging "
                              "low S/N points.  "
                              "Using all data.")
            else:
                # -1 to weed out the very last data point; it cannot be correct
                # (can't have a power law with 1 data point).
                nmax = len(xmins)-1

            best_ks_index = argmin(kstest_values[:nmax])
            xmin = xmins[best_ks_index]

            self._alpha_values = alpha_values
            self._xmin_kstest = kstest_values
            if scipyOK:
                # CHECK THIS
                self._ks_prob_all = np.array([scipy.stats.ksone.sf(D_stat,
                                                                   len(kstest_values)-ii)
                                              for ii,D_stat in
                                              enumerate(kstest_values)])
            self._sigma = sigma

            # sanity check
            n = np.count_nonzero(z>=xmin)
            alpha = 1. + float(n)/sum(log(z[z>=xmin]/xmin))
            try:
                if not skip_consistency_check:
                    np.testing.assert_almost_equal(alpha,
                                                   alpha_values[best_ks_index],
                                                   decimal=4)
            except AssertionError:
                raise AssertionError("The alpha value computed was not self-"
                                     "consistent.  This should not happen.  "
                                     "However, it is possible that this is "
                                     "a numerical uncertainty issue; the "
                                     "values being compared are {0} and {1}."
                                     "If they are close enough, set "
                                     "skip_consistency_check=True."
                                     .format(alpha,
                                             alpha_values[best_ks_index]))

        z = z[z>=xmin]
        n = len(z)
        alpha = 1. + float(n) / sum(log(z/xmin))
        if finite:
            alpha = alpha*(n-1.)/n+1./n
        if n < 50 and not finite and not silent:
            print(('(PLFIT) Warning: finite-size bias may be present. n=%i' % n))

        ks = max(abs( np.arange(n)/float(n) - (1-(xmin/z)**(alpha-1)) ))
        # Parallels Eqn 3.5 in Clauset et al 2009, but zeta(alpha, xmin) =
        # (alpha-1)/xmin.  Really is Eqn B3 in paper.
        L = n*log((alpha-1)/xmin) - alpha*sum(log(z/xmin))
        #requires another map... Larr = arange(len(unique(x))) * log((alpha_values-1)/unique(x)) - alpha_values*sum
        self._likelihood = L
        self._xmin = xmin
        self._xmins = xmins
        self._alpha= alpha
        self._alphaerr = (alpha-1)/np.sqrt(n)

        # this ks statistic may not have the same value as min(dat) because of unique()
        self._ks = ks

        if scipyOK:
            self._ks_prob = scipy.stats.ksone.sf(ks, n)

        self._ngtx = n
        if n == 1:
            if not silent:
                print("Failure: only 1 point kept.  Probably not a power-law distribution.")
            self._alpha = alpha = 0
            self._alphaerr = 0
            self._likelihood = L = 0
            self._ks = 0
            self._ks_prob = 0
            self._xmin = xmin
            return xmin,0
        if np.isnan(L) or np.isnan(xmin) or np.isnan(alpha):
            raise ValueError("plfit failed; returned a nan")

        if not quiet:
            if verbose: print("The lowest value included in the power-law fit, ", end=' ')
            print("xmin: %g" % xmin, end=' ')
            if verbose: print("\nThe number of values above xmin, ", end=' ')
            print("n(>xmin): %i" % n, end=' ')
            if verbose: print("\nThe derived power-law alpha (p(x)~x^-alpha) with MLE-derived error, ", end=' ')
            print("alpha: %g +/- %g  " % (alpha,self._alphaerr), end=' ')
            if verbose: print("\nThe log of the Likelihood (the maximized parameter; you minimized the negative log likelihood), ", end=' ')
            print("Log-Likelihood: %g  " % L, end=' ')
            if verbose: print("\nThe KS-test statistic between the best-fit power-law and the data, ", end=' ')
            print("ks: %g" % (ks), end=' ')
            if scipyOK:
                if verbose: print(" occurs with probability  ", end=' ')
                print("p(ks): %g" % (self._ks_prob))
            else:
                print()

        return xmin,alpha

    def discrete_best_alpha(self, alpharangemults=(0.9,1.1), n_alpha=201,
                            approximate=True, verbose=True, finite=True):
        """
        Use the maximum likelihood to determine the most likely value of alpha

        *alpharangemults* [ 2-tuple ]
            Pair of values indicating multiplicative factors above and below the
            approximate alpha from the MLE alpha to use when determining the
            "exact" alpha (by directly maximizing the likelihood function)
        *n_alpha* [ int ]
            Number of alpha values to use when measuring.  Larger number is more accurate.
        *approximate* [ bool ]
            If False, try to "zoom-in" around the MLE alpha and get the exact
            best alpha value within some range around the approximate best
        *vebose* [ bool ]
        *finite* [ bool ]
            Correction for finite data?
        """

        data = self.data
        self._xmins = xmins = np.unique(data)
        if approximate:
            alpha_of_xmin = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]
        else:
            alpha_approx = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]
            alpharanges = [(0.9*a,1.1*a) for a in alpha_approx]
            alpha_of_xmin = [ most_likely_alpha(data,xmin,alpharange=ar,n_alpha=n_alpha)
                             for xmin,ar in zip(xmins,alpharanges) ]
        ksvalues = np.array([discrete_ksD(data, xmin, alpha)
                                for xmin,alpha in zip(xmins,alpha_of_xmin)
                               ])
        self._alpha_values = np.array(alpha_of_xmin)
        self._xmin_kstest = ksvalues

        ksvalues[np.isnan(ksvalues)] = np.inf

        best_index = argmin(ksvalues)
        self._alpha = best_alpha = alpha_of_xmin[best_index]
        self._xmin = best_xmin = xmins[best_index]
        self._ks = best_ks = ksvalues[best_index]
        self._likelihood = best_likelihood = discrete_likelihood(data, best_xmin, best_alpha)

        if finite:
            self._alpha = self._alpha*(n-1.)/n+1./n

        if verbose:
            print("alpha = %f   xmin = %f   ksD = %f   L = %f   (n<x) = %i  (n>=x) = %i" % (
                    best_alpha, best_xmin, best_ks, best_likelihood,
                    (data<best_xmin).sum(), (data>=best_xmin).sum()))

        self._ngtx = n = (self.data>=self._xmin).sum()
        self._alphaerr = (self._alpha-1.0)/np.sqrt(n)
        if scipyOK:
            self._ks_prob = scipy.stats.ksone.sf(self._ks, n)

        return best_alpha,best_xmin,best_ks,best_likelihood

    def xminvsks(self, **kwargs):
        """
        Plot xmin versus the ks value for derived alpha.  This plot can be used
        as a diagnostic of whether you have derived the 'best' fit: if there are
        multiple local minima, your data set may be well suited to a broken
        powerlaw or a different function.
        """

        pylab.plot(self._xmins,self._xmin_kstest,'.')
        pylab.plot(self._xmin,self._ks,'s')
        #pylab.errorbar([self._ks],self._alpha,yerr=self._alphaerr,fmt='+')

        ax=pylab.gca()
        ax.set_ylabel("KS statistic")
        ax.set_xlabel("min(x)")
        pylab.draw()

        return ax

    def alphavsks(self,autozoom=True,**kwargs):
        """
        Plot alpha versus the ks value for derived alpha.  This plot can be used
        as a diagnostic of whether you have derived the 'best' fit: if there are
        multiple local minima, your data set may be well suited to a broken
        powerlaw or a different function.
        """

        pylab.plot(self._alpha_values, self._xmin_kstest, '.')
        pylab.errorbar(self._alpha, self._ks, xerr=self._alphaerr, fmt='+')

        ax=pylab.gca()
        if autozoom:
            ax.set_ylim(0.8*(self._ks),3*(self._ks))
            ax.set_xlim((self._alpha)-5*self._alphaerr,(self._alpha)+5*self._alphaerr)
        ax.set_ylabel("KS statistic")
        ax.set_xlabel(r'$\alpha$')
        pylab.draw()

        return ax

    def plotcdf(self, x=None, xmin=None, alpha=None, pointcolor='k',
                dolog=True, zoom=True, pointmarker='+', **kwargs):
        """
        Plots CDF and powerlaw
        """
        if x is None: x=self.data
        if xmin is None: xmin=self._xmin
        if alpha is None: alpha=self._alpha

        x=np.sort(x)
        n=len(x)
        xcdf = np.arange(n,0,-1,dtype='float')/float(n)

        q = x[x>=xmin]
        fcdf = (q/xmin)**(1-alpha)
        nc = xcdf[argmax(x>=xmin)]
        fcdf_norm = nc*fcdf

        D_location = argmax(xcdf[x>=xmin]-fcdf_norm)
        pylab.vlines(q[D_location], xcdf[x>=xmin][D_location],
                     fcdf_norm[D_location], color='m', linewidth=2, zorder=2)
        pylab.plot([q[D_location]]*2,
                   [xcdf[x>=xmin][D_location], fcdf_norm[D_location]],
                   color='m',
                   marker='s', zorder=3)

        #plotx = pylab.linspace(q.min(),q.max(),1000)
        #ploty = (plotx/xmin)**(1-alpha) * nc

        if dolog:
            pylab.loglog(x,xcdf,marker=pointmarker,color=pointcolor,**kwargs)
            pylab.loglog(q,fcdf_norm,'r',**kwargs)
        else:
            pylab.semilogx(x,xcdf,marker=pointmarker,color=pointcolor,**kwargs)
            pylab.semilogx(q,fcdf_norm,'r',**kwargs)

        if zoom:
            pylab.axis([xmin, x.max(), xcdf.min(), nc])

    def plotpdf(self, x=None, xmin=None, alpha=None, nbins=50, dolog=True,
                dnds=False, drawstyle='steps-post', histcolor='k', plcolor='r',
                fill=False, dohist=True, **kwargs):
        """
        Plots PDF and powerlaw.

        kwargs is passed to pylab.hist and pylab.plot
        """
        if x is None:
            x=self.data
        if xmin is None:
            xmin=self._xmin
        if alpha is None:
            alpha=self._alpha

        x=np.sort(x)
        #n=len(x)

        pylab.gca().set_xscale('log')
        pylab.gca().set_yscale('log')

        if dnds:
            hb = pylab.histogram(x,bins=np.logspace(log10(min(x)),log10(max(x)),nbins))
            h = hb[0]
            b = hb[1]
            db = hb[1][1:]-hb[1][:-1]
            h = h/db
            if dohist:
                pylab.plot(b[:-1],h,drawstyle=drawstyle,color=histcolor,**kwargs)
            #alpha -= 1
        elif dolog:
            hb = pylab.hist(x, bins=np.logspace(log10(min(x)), log10(max(x)),
                                                nbins), log=True, fill=fill,
                            edgecolor=histcolor, **kwargs)
            alpha -= 1
            h,b=hb[0],hb[1]
            if not dohist:
                for rect in hb[2]:
                    rect.set_visible(False)
        else:
            hb = pylab.hist(x, bins=np.linspace((min(x)), (max(x)), nbins),
                            fill=fill, edgecolor=histcolor, **kwargs)
            h,b=hb[0],hb[1]
            if not dohist:
                for rect in hb[2]:
                    rect.set_visible(False)
        # plotting points are at the center of each bin
        b = (b[1:]+b[:-1])/2.0

        q = x[x>=xmin]
        px = (alpha-1)/xmin * (q/xmin)**(-alpha)

        # Normalize by the median ratio between the histogram and the power-law
        # The normalization is semi-arbitrary; an average is probably just as valid
        plotloc = (b>xmin)*(h>0)
        norm = np.median(h[plotloc] / ((alpha-1)/xmin *
                                       (b[plotloc]/xmin)**(-alpha)))
        px = px*norm

        plotx = pylab.linspace(q.min(),q.max(),1000)
        ploty = (alpha-1)/xmin * (plotx/xmin)**(-alpha) * norm

        #pylab.loglog(q,px,'r',**kwargs)
        pylab.plot(plotx, ploty, color=plcolor, **kwargs)

        axlims = pylab.axis()
        pylab.vlines(xmin, axlims[2], max(px), colors=plcolor,
                     linestyle='dashed')

        if dolog and min(x) <= 0:
            lolim = 0.1
        else:
            lolim = min(x)
        pylab.gca().set_xlim(lolim, max(x))

    def plotppf(self,x=None,xmin=None,alpha=None,dolog=True,**kwargs):
        """
        Plots the power-law-predicted value on the Y-axis against the real
        values along the X-axis.  Can be used as a diagnostic of the fit
        quality.
        """
        if not(xmin): xmin=self._xmin
        if not(alpha): alpha=self._alpha
        if not(x): x=np.sort(self.data[self.data>xmin])
        else: x=np.sort(x[x>xmin])

        # N = M^(-alpha+1)
        # M = N^(1/(-alpha+1))

        m0 = min(x)
        N = (1.0+np.arange(len(x)))[::-1]
        xmodel = m0 * N**(1/(1-alpha)) / max(N)**(1/(1-alpha))

        if dolog:
            pylab.loglog(x,xmodel,'.',**kwargs)
            pylab.gca().set_xlim(min(x),max(x))
            pylab.gca().set_ylim(min(x),max(x))
        else:
            pylab.plot(x,xmodel,'.',**kwargs)
        pylab.plot([min(x),max(x)],[min(x),max(x)],'k--')
        pylab.xlabel("Real Value")
        pylab.ylabel("Power-Law Model Value")

    def test_pl(self, niter=1e3, print_timing=False, **kwargs):
        """
        Monte-Carlo test to determine whether distribution is consistent with a power law

        Runs through niter iterations of a sample size identical to the input sample size.

        Will randomly select values from the data < xmin.  The number of values selected will
        be chosen from a uniform random distribution with p(<xmin) = n(<xmin)/n.

        Once the sample is created, it is fit using above methods, then the best fit is used to
        compute a Kolmogorov-Smirnov statistic.  The KS stat distribution is compared to the
        KS value for the fit to the actual data, and p = fraction of random ks values greater
        than the data ks value is computed.  If p<.1, the data may be inconsistent with a
        powerlaw.  A data set of n(>xmin)>100 is required to distinguish a PL from an exponential,
        and n(>xmin)>~300 is required to distinguish a log-normal distribution from a PL.
        For more details, see figure 4.1 and section

        **WARNING** This can take a very long time to run!  Execution time scales as
        niter * setsize

        Returns
        -------
        (p,ksv):
        p is the p-value (probability) that the data are consistent with a power-law
        ksv is an array of the KS-test values for each monte-carlo experiment

        """
        xmin = self._xmin
        alpha = self._alpha
        niter = int(niter)

        ntail = sum(self.data >= xmin)
        ntot = len(self.data)
        nnot = ntot-ntail              # n(<xmin)
        pnot = nnot/float(ntot)        # p(<xmin)
        nonpldata = self.data[self.data<xmin]
        nrandnot = sum( npr.rand(ntot) < pnot ) # randomly choose how many to sample from <xmin
        nrandtail = ntot - nrandnot         # and the rest will be sampled from the powerlaw

        ksv = []
        if print_timing: deltat = []
        for i in range(niter):
            # first, randomly sample from power law
            # with caveat!
            nonplind = np.floor(npr.rand(nrandnot)*nnot).astype('int')
            fakenonpl = nonpldata[nonplind]
            randarr = npr.rand(nrandtail)
            fakepl = randarr**(1/(1-alpha)) * xmin
            fakedata = np.concatenate([fakenonpl,fakepl])
            if print_timing: t0 = time.time()
            # second, fit to powerlaw
            # (add some silencing kwargs optionally)
            for k,v in {'quiet':True,'silent':True,'nosmall':True}.items():
                if k not in kwargs:
                    kwargs[k] = v
            TEST = plfit(fakedata,**kwargs)
            ksv.append(TEST._ks)
            if print_timing:
                deltat.append( time.time() - t0 )
                print("Iteration %i: %g seconds" % (i, deltat[-1]))

        ksv = np.array(ksv)
        p = (ksv>self._ks).sum() / float(niter)
        self._pval = p
        self._ks_rand = ksv

        print("p(%i) = %0.3f" % (niter,p))
        if print_timing: print("Iteration timing: %g +/- %g" % (np.mean(deltat),np.std(deltat)))

        return p,ksv

    def lognormal(self,doprint=True):
        """
        Use the maximum likelihood estimator for a lognormal distribution to
        produce the best-fit lognormal parameters
        """
        # N = float(self.data.shape[0])
        # mu = log(self.data).sum() / N
        # sigmasquared = ( ( log(self.data) - mu )**2 ).sum() / N
        # self.lognormal_mu = mu
        # self.lognormal_sigma = np.sqrt(sigmasquared)
        # self.lognormal_likelihood = -N/2. * log(np.pi*2) - N/2. * log(sigmasquared) - 1/(2*sigmasquared) * (( self.data - mu )**2).sum()
        # if doprint:
        #     print "Best fit lognormal is exp( -(x-%g)^2 / (2*%g^2)" % (mu,np.sqrt(sigmasquared))
        #     print "Likelihood: %g" % (self.lognormal_likelihood)
        if scipyOK:
            fitpars = scipy.stats.lognorm.fit(self.data)
            self.lognormal_dist = scipy.stats.lognorm(*fitpars)
            self.lognormal_ksD,self.lognormal_ksP = scipy.stats.kstest(self.data,self.lognormal_dist.cdf)
            # nnlf = NEGATIVE log likelihood
            self.lognormal_likelihood = -1*scipy.stats.lognorm.nnlf(fitpars,self.data)

            # Is this the right likelihood ratio?
            # Definition of L from eqn. B3 of Clauset et al 2009:
            # L = log(p(x|alpha))
            # _nnlf from scipy.stats.distributions:
            # -sum(log(self._pdf(x, *args)),axis=0)
            # Assuming the pdf and p(x|alpha) are both non-inverted, it looks
            # like the _nnlf and L have opposite signs, which would explain the
            # likelihood ratio I've used here:
            self.power_lognorm_likelihood = (self._likelihood + self.lognormal_likelihood)
            # a previous version had 2*(above).  That is the correct form if you want the likelihood ratio
            # statistic "D": http://en.wikipedia.org/wiki/Likelihood-ratio_test
            # The above explanation makes sense, since nnlf is the *negative* log likelihood function:
            ## nnlf  -- negative log likelihood function (to minimize)
            #
            # Assuming we want the ratio between the POSITIVE likelihoods, the D statistic is:
            # D = -2 log( L_power / L_lognormal )
            self.likelihood_ratio_D = -2 * (log(self._likelihood/self.lognormal_likelihood))

            if doprint:
                print("Lognormal KS D: %g  p(D): %g" % (self.lognormal_ksD,self.lognormal_ksP), end=' ')
                print("  Likelihood Ratio Statistic (powerlaw/lognormal): %g" % self.likelihood_ratio_D)
                print("At this point, have a look at Clauset et al 2009 Appendix C: determining sigma(likelihood_ratio)")

    def plot_lognormal_pdf(self,**kwargs):
        """
        Plot the fitted lognormal distribution
        """
        if not hasattr(self,'lognormal_dist'):
            return

        normalized_pdf = self.lognormal_dist.pdf(self.data)/self.lognormal_dist.pdf(self.data).max()
        minY,maxY = pylab.gca().get_ylim()
        pylab.plot(self.data,normalized_pdf*maxY,'.',**kwargs)

    def plot_lognormal_cdf(self,**kwargs):
        """
        Plot the fitted lognormal distribution
        """
        if not hasattr(self,'lognormal_dist'):
            return

        x=np.sort(self.data)
        n=len(x)
        xcdf = np.arange(n,0,-1,dtype='float')/float(n)
        lcdf = self.lognormal_dist.sf(x)

        D_location = argmax(xcdf-lcdf)
        pylab.vlines(x[D_location],xcdf[D_location],lcdf[D_location],color='m',linewidth=2)

        pylab.plot(x, lcdf,',',**kwargs)

def plfit_lsq(x,y):
    """
    Returns A and B in y=Ax^B
    http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
    """
    n = len(x)
    btop = n * (log(x)*log(y)).sum() - (log(x)).sum()*(log(y)).sum()
    bbottom = n*(log(x)**2).sum() - (log(x).sum())**2
    b = btop / bbottom
    a = ( log(y).sum() - b * log(x).sum() ) / n

    A = exp(a)
    return A,b

def plexp_cdf(x,xmin=1,alpha=2.5, pl_only=False, exp_only=False):
    """
    CDF(x) for the piecewise distribution exponential x<xmin, powerlaw x>=xmin
    This is the CDF version of the distributions drawn in fig 3.4a of Clauset et al.
    The constant "C" normalizes the PDF
    """

    x = np.array(x)
    C = 1/(-xmin/(1 - alpha) - xmin/alpha + exp(alpha)*xmin/alpha)
    Ppl = lambda X: 1+C*(xmin/(1-alpha)*(X/xmin)**(1-alpha))
    Pexp = lambda X: C*xmin/alpha*exp(alpha)-C*(xmin/alpha)*exp(-alpha*(X/xmin-1))

    if exp_only:
        return Pexp(x)
    elif pl_only:
        return Ppl(x)

    d=Ppl(x)
    d[x<xmin]=Pexp(x)[x<xmin]
    return d

def plexp_pdf(x,xmin=1,alpha=2.5):
    x = np.array(x)
    C = 1/(-xmin/(1 - alpha) - xmin/alpha + exp(alpha)*xmin/alpha)
    Ppl = lambda X: C*(X/xmin)**(-alpha)
    Pexp = lambda X: C*exp(-alpha*(X/xmin-1))
    d=Ppl(x)
    d[x<xmin] = Pexp(x)[x<xmin]
    return d

# WRONG
# def plexp_inv(P,xmin,alpha):
#     """
#     Inverse CDF for a piecewise PDF as defined in eqn. 3.10
#     of Clauset et al.
#     """
#
#     C = 1/(-xmin/(1 - alpha) - xmin/alpha + exp(alpha)*xmin/alpha)
#     Pxm = -C*(xmin/(1-alpha))
#     x = P*0
#     x[P>=Pxm] = xmin*( (P[P>=Pxm]-1) * (1-alpha)/(C*xmin) )**(1/(1-alpha)) # powerlaw
#     x[P<Pxm] = (log( (C*xmin/alpha*exp(alpha)-P[P<Pxm])/(C*xmin/alpha) ) - alpha) * (-xmin/alpha) # exp
#
#     return x

def plexp_inv(P, xmin, alpha, guess=1.):
    """
    Inverse CDF for a piecewise PDF as defined in eqn. 3.10
    of Clauset et al.

    (previous version was incorrect and lead to weird discontinuities in the
    distribution function)
    """
    def equation(x,prob):
        return plexp_cdf(x, xmin, alpha)-prob
    # http://stackoverflow.com/questions/19840425/scipy-optimize-faster-root-finding-over-2d-grid
    def solver(y, x0=guess):
        return scipy.optimize.fsolve(equation, guess, args=(y,))
    f = np.vectorize(solver)
    return f(P)

def pl_inv(P,xm,a):
    """
    Inverse CDF for a pure power-law
    """

    x = (1-P)**(1/(1-a)) * xm
    return x

def test_fitter(xmin=1.0,alpha=2.5,niter=500,npts=1000,invcdf=plexp_inv):
    """
    Tests the power-law fitter

    Examples
    ========
    Example (fig 3.4b in Clauset et al.)::

        xminin=[0.25,0.5,0.75,1,1.5,2,5,10,50,100]
        xmarr,af,ksv,nxarr = plfit.test_fitter(xmin=xminin,niter=1,npts=50000)
        loglog(xminin,xmarr.squeeze(),'x')

    Example 2::

        xminin=[0.25,0.5,0.75,1,1.5,2,5,10,50,100]
        xmarr,af,ksv,nxarr = plfit.test_fitter(xmin=xminin,niter=10,npts=1000)
        loglog(xminin,xmarr.mean(axis=0),'x')

    Example 3::

        xmarr,af,ksv,nxarr = plfit.test_fitter(xmin=1.0,niter=1000,npts=1000)
        hist(xmarr.squeeze());
        # Test results:
        # mean(xmarr) = 0.70, median(xmarr)=0.65 std(xmarr)=0.20
        # mean(af) = 2.51 median(af) = 2.49  std(af)=0.14
        # biased distribution; far from correct value of xmin but close to correct alpha

    Example 4::

        xmarr,af,ksv,nxarr = plfit.test_fitter(xmin=1.0,niter=1000,npts=1000,invcdf=pl_inv)
        print("mean(xmarr): %0.2f median(xmarr): %0.2f std(xmarr): %0.2f" % (mean(xmarr),median(xmarr),std(xmarr)))
        print("mean(af): %0.2f median(af): %0.2f std(af): %0.2f" % (mean(af),median(af),std(af)))
        # mean(xmarr): 1.19 median(xmarr): 1.03 std(xmarr): 0.35
        # mean(af): 2.51 median(af): 2.50 std(af): 0.07

    """
    xmin = np.array(xmin)
    if xmin.shape == ():
        xmin.shape = 1
    lx = len(xmin)
    sz = [niter,lx]
    xmarr,alphaf_v,ksv,nxarr = np.zeros(sz),np.zeros(sz),np.zeros(sz),np.zeros(sz)
    for j in range(lx):
        for i in range(niter):
            randarr = npr.rand(npts)
            fakedata = invcdf(randarr,xmin[j],alpha)
            TEST = plfit(fakedata,quiet=True,silent=True,nosmall=True)
            alphaf_v[i,j] = TEST._alpha
            ksv[i,j] = TEST._ks
            nxarr[i,j] = TEST._ngtx
            xmarr[i,j] = TEST._xmin

    return xmarr,alphaf_v,ksv,nxarr

def discrete_likelihood(data, xmin, alpha):
    """
    Equation B.8 in Clauset

    Given a data set, an xmin value, and an alpha "scaling parameter", computes
    the log-likelihood (the value to be maximized)
    """
    if not scipyOK:
        raise ImportError("Can't import scipy.  Need scipy for zeta function.")
    from scipy.special import zeta as zeta

    zz = data[data>=xmin]
    nn = len(zz)

    sum_log_data = np.log(zz).sum()

    zeta = zeta(alpha, xmin)

    L_of_alpha = -1*nn*log(zeta) - alpha * sum_log_data

    return L_of_alpha

def discrete_likelihood_vector(data, xmin, alpharange=(1.5,3.5), n_alpha=201):
    """
    Compute the likelihood for all "scaling parameters" in the range (alpharange)
    for a given xmin.  This is only part of the discrete value likelihood
    maximization problem as described in Clauset et al
    (Equation B.8)

    *alpharange* [ 2-tuple ]
        Two floats specifying the upper and lower limits of the power law alpha to test
    """
    from scipy.special import zeta as zeta

    zz = data[data>=xmin]
    nn = len(zz)

    alpha_vector = np.linspace(alpharange[0],alpharange[1],n_alpha)
    sum_log_data = np.log(zz).sum()

    # alpha_vector is a vector, xmin is a scalar
    zeta_vector = zeta(alpha_vector, xmin)

    #xminvec = np.arange(1.0,xmin)

    #xminalphasum = np.sum([xm**(-alpha_vector) for xm in xminvec])
    #L = -1*alpha_vector*sum_log_data - nn*log(zeta_vector) - xminalphasum

    L_of_alpha = -1*nn*log(zeta_vector) - alpha_vector * sum_log_data

    return L_of_alpha

def discrete_max_likelihood_arg(data, xmin, alpharange=(1.5,3.5), n_alpha=201):
    """
    Returns the *argument* of the max of the likelihood of the data given an input xmin
    """
    likelihoods = discrete_likelihood_vector(data, xmin, alpharange=alpharange, n_alpha=n_alpha)
    Largmax = np.argmax(likelihoods)
    return Largmax

def discrete_max_likelihood(data, xmin, alpharange=(1.5,3.5), n_alpha=201):
    """
    Returns the *argument* of the max of the likelihood of the data given an input xmin
    """
    likelihoods = discrete_likelihood_vector(data, xmin, alpharange=alpharange, n_alpha=n_alpha)
    Lmax = np.max(likelihoods)
    return Lmax

def most_likely_alpha(data, xmin, alpharange=(1.5,3.5), n_alpha=201):
    """
    Return the most likely alpha for the data given an xmin
    """
    alpha_vector = np.linspace(alpharange[0],alpharange[1],n_alpha)
    return alpha_vector[discrete_max_likelihood_arg(data, xmin,
                                                    alpharange=alpharange,
                                                    n_alpha=n_alpha)]

def discrete_alpha_mle(data, xmin):
    """
    Equation B.17 of Clauset et al 2009

    The Maximum Likelihood Estimator of the "scaling parameter" alpha in the
    discrete case is similar to that in the continuous case
    """
    # boolean indices of positive data
    gexmin = (data>=xmin)
    nn = gexmin.sum()
    if nn < 2:
        return 0
    xx = data[gexmin]
    alpha = 1.0 + float(nn) * (sum(log(xx/(float(xmin)-0.5))))**-1
    return alpha

def discrete_best_alpha(data, alpharangemults=(0.9,1.1), n_alpha=201, approximate=True, verbose=True):
    """
    Use the maximum L to determine the most likely value of alpha

    *alpharangemults* [ 2-tuple ]
        Pair of values indicating multiplicative factors above and below the
        approximate alpha from the MLE alpha to use when determining the
        "exact" alpha (by directly maximizing the likelihood function)
    """

    xmins = np.unique(data)
    if approximate:
        alpha_of_xmin = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]
    else:
        alpha_approx = [ discrete_alpha_mle(data,xmin) for xmin in xmins ]
        alpharanges = [(0.9*a,1.1*a) for a in alpha_approx]
        alpha_of_xmin = [ most_likely_alpha(data,xmin,alpharange=ar,n_alpha=n_alpha) for xmin,ar in zip(xmins,alpharanges) ]
    ksvalues = [ discrete_ksD(data, xmin, alpha) for xmin,alpha in zip(xmins,alpha_of_xmin) ]

    best_index = argmin(ksvalues)
    best_alpha = alpha_of_xmin[best_index]
    best_xmin = xmins[best_index]
    best_ks = ksvalues[best_index]
    best_likelihood = discrete_likelihood(data, best_xmin, best_alpha)

    if verbose:
        print("alpha = %f   xmin = %f   ksD = %f   L = %f   (n<x) = %i  (n>=x) = %i" % (
                best_alpha, best_xmin, best_ks, best_likelihood,
                (data<best_xmin).sum(), (data>=best_xmin).sum()))

    return best_alpha,best_xmin,best_ks,best_likelihood

def discrete_ksD(data, xmin, alpha):
    """
    given a sorted data set, a minimum, and an alpha, returns the power law ks-test
    D value w/data

    The returned value is the "D" parameter in the ks test

    (this is implemented differently from the continuous version because there
    are potentially multiple identical points that need comparison to the power
    law)
    """
    zz = np.sort(data[data>=xmin])
    nn = float(len(zz))
    if nn < 2:
        return np.inf
    #cx = np.arange(nn,dtype='float')/float(nn)
    #cf = 1.0-(zz/xmin)**(1.0-alpha)
    model_cdf = 1.0-(zz.astype('float')/float(xmin))**(1.0-alpha)
    data_cdf = np.searchsorted(zz,zz,side='left')/(float(nn))

    ks = max(abs(model_cdf-data_cdf))
    return ks


# -*- encoding: utf-8 -*-
from __future__ import unicode_literals

try:
    from bleach import clean

except ImportError:

    def clean(*args, **kwargs):
        raise ImportError('Please install `bleach` package to use sanitization.')

from django.contrib.syndication.views import Feed as _Feed
from django.utils.feedgenerator import Rss201rev2Feed as FeedType

from .settings import TURBO_ALLOWED_ATTRS, TURBO_ALLOWED_TAGS

def sanitize_turbo(html, allowed_tags=TURBO_ALLOWED_TAGS, allowed_attrs=TURBO_ALLOWED_ATTRS):
    """Sanitizes HTML, removing not allowed tags and attributes.

    :param str|unicode html:

    :param list allowed_tags: List of allowed tags.
    :param dict allowed_attrs: Dictionary with attributes allowed for tags.

    :rtype: unicode
    """
    return clean(html, tags=allowed_tags, attributes=allowed_attrs, strip=True)

class YandexTurboFeedType(FeedType):

    def __init__(self, *args, **kwargs):

        self._analytics = kwargs.pop('ya_analytics', [])
        self._ads = kwargs.pop('ya_ads', [])

        super(YandexTurboFeedType, self).__init__(*args, **kwargs)

    def rss_attributes(self):
        attrs = super(YandexTurboFeedType, self).rss_attributes()

        attrs.update({
            'xmlns:turbo': 'http://turbo.yandex.ru',
            'xmlns:yandex': 'http://news.yandex.ru',
        })

        return attrs

    def item_attributes(self, item):
        attrs = super(YandexTurboFeedType, self).item_attributes(item)

        if not item['ya_contents']:
            # No turbo content available.
            return attrs

        attrs.update({
            'turbo': 'true',
        })

        return attrs

    def add_root_elements(self, handler):
        super(YandexTurboFeedType, self).add_root_elements(handler)

        for params in self._analytics:
            handler.startElement('turbo:analytics', params)
            handler.endElement('turbo:analytics')

        for params in self._ads:
            handler.startElement('turbo:adNetwork', params)
            handler.endElement('turbo:adNetwork')

    def add_item_elements(self, handler, item):
        super(YandexTurboFeedType, self).add_item_elements(handler, item)

        # todo maybe
        # yandex:related

        turbo_contents = item['ya_contents']

        if not turbo_contents:
            return

        handler.startElement('turbo:content', {})
        handler.ignorableWhitespace('<![CDATA[%s]]>' % turbo_contents)
        handler.endElement('turbo:content')

        source = item['ya_source']
        if source:
            handler.addQuickElement('turbo:source', source)

        topic = item['ya_topic']
        if topic:
            handler.addQuickElement('turbo:topic', topic)

class YandexTurboFeed(_Feed):
    """Yandex Turbo Pages Feed.

    .. code-block:: python

        from yaturbo import YandexTurboFeed

        class TurboFeed(YandexTurboFeed):
            '''
            More information on Django Syndication Feed Framework configuration:
            https://docs.djangoproject.com/en/2.0/ref/contrib/syndication/

            '''

            turbo_sanitize = True  # Let's strip HTML tags unsupported by Turbo pages.

            def item_turbo(self, item):
                return 'turbo contents'

        feed = TurboFeed()

        # configure Yandex Metrika counter
        feed.configure_analytics_yandex('123456789')

        # configure Yandex Advertisement Network
        feed.configure_ad_yandex('A-123', 'page-top')

        urlpatterns = [
            ...
            path('turbo/', feed),
            ...
        ]

    """
    feed_type = YandexTurboFeedType

    turbo_sanitize = False
    """Whether to automatically sanitize HTML contents returned from `.item_turbo()`.

    Can be useful if you do not keep special HTML for Turbo pages.

    """

    def __init__(self):
        super(YandexTurboFeed, self).__init__()
        self.analytics = []
        self.ads = []

        # Django < 1.10 has obfuscated __get_dynamic_attr
        if not hasattr(self, '_get_dynamic_attr'):
            self._get_dynamic_attr = getattr(self, '_Feed__get_dynamic_attr')

    def configure_ad_yandex(self, ident, turbo_id=''):
        """Configure Yandex Advertisement Network.

        :param str|unicode ident: Ad ID.

        :param str|unicode turbo_id: ID of a place (figure) on Turbo page where to put an Ad block.

        """
        self.ads.append({
            'type': 'Yandex',
            'id': ident,
            'turbo-ad-id': turbo_id,
        })

    # todo maybe ad methods for
    # adfox

    def configure_analytics_yandex(self, ident, params=None):
        """Configure Yandex Metrika analytics counter.

        :param str|unicode ident: Metrika counter ID.

        :param dict params: Additional params.

        """
        params = params or {}

        data = {
            'type': 'Yandex',
            'id': ident,
        }

        if params:
            data['params'] = '%s' % params

        self.analytics.append(data)

    def configure_analytics_google(self, ident):
        """Configure Google Analytics counter.

        :param str|unicode ident: Counter ID.

        """
        self.analytics.append({
            'type': 'Google',
            'id': ident,
        })

    # todo maybe analytics methods for
    # LiveInternet
    # Mail.RU
    # Rambler
    # Mediascope(TNS)
    # + custom

    def item_turbo(self, item):
        """This can be overridden to set turbo contents.

        :param item:

        :rtype: str|unicode

        """
        # todo maybe automatic html transform, e.g. with bleach
        return self.item_description(item)

    def item_turbo_source(self, item):
        """This can be overridden to set turbo source URL.

        Can be used with Yandex Metrika.

        :param item:

        :rtype: str|unicode

        """
        return ''

    def item_turbo_topic(self, item):
        """This can be overridden to set turbo page topic (title).

        Can be used with Yandex Metrika.

        :param item:

        :rtype: str|unicode

        """
        return ''

    def item_extra_kwargs(self, item):
        kwargs = super(YandexTurboFeed, self).item_extra_kwargs(item)

        get_dyn = self._get_dynamic_attr

        contents = get_dyn('item_turbo', item)

        if contents and self.turbo_sanitize:
            contents = sanitize_turbo(contents)

        kwargs.update({
            'ya_contents': contents,
            'ya_source': get_dyn('item_turbo_source', item),
            'ya_topic': get_dyn('item_turbo_topic', item),
        })

        return kwargs

    def feed_extra_kwargs(self, obj):
        kwargs = super(YandexTurboFeed, self).feed_extra_kwargs(obj)

        kwargs.update({
            'ya_analytics': self.analytics,
            'ya_ads': self.ads,
        })

        return kwargs


from django import forms
from django.conf import settings

try:
    # Django 1.9
    from django.forms.utils import flatatt
except ImportError:
    # Django <1.9
    from django.forms.util import flatatt
from django.utils.safestring import mark_safe
from django.utils import six

from taggit.models import Tag
from taggit.utils import edit_string_for_tags

class LabelWidget(forms.TextInput):
    """
    Widget class for rendering an item's tags - and all existing tags - as
    selectable labels.
    """
    input_type = "hidden"
    model = Tag

    def __init__(self, *args, **kwargs):
        self.model = kwargs.pop("model", None) or self.model
        super(LabelWidget, self).__init__(*args, **kwargs)

    @property
    def is_hidden(self):
        return False

    def tag_list(self, tags):
        """
        Generates a list of tags identifying those previously selected.

        Returns a list of tuples of the form (<tag name>, <CSS class name>).

        Uses the string names rather than the tags themselves in order to work
        with tag lists built from forms not fully submitted.
        """
        return [
            (tag.name, "selected taggit-tag" if tag.name in tags else "taggit-tag")
            for tag in self.model.objects.all()
        ]

    def format_value(self, value):
        if value is not None and not isinstance(value, six.string_types):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        return value

    def render(self, name, value, attrs={}, **kwargs):
        # Case in which a new form is dispalyed
        if value is None:
            current_tags = []
            formatted_value = ""
            selected_tags = self.tag_list([])

        # Case in which a form is displayed with submitted but not saved
        # details, e.g. invalid form submission
        elif isinstance(value, six.string_types):
            current_tags = [tag.strip(' "') for tag in value.split(",") if tag]
            formatted_value = value
            selected_tags = self.tag_list(current_tags)

        # Case in which value is loaded from saved tags
        else:
            current_tags = [o.tag for o in value.select_related("tag")]
            formatted_value = self.format_value(value)
            selected_tags = self.tag_list([t.name for t in current_tags])

        input_field = super(LabelWidget, self).render(
            name, formatted_value, attrs, **kwargs
        )
        if attrs.get("class") is None:
            attrs.update({"class": "taggit-labels taggit-list"})
        list_attrs = flatatt(attrs)

        tag_li = "".join(
            [
                u"
{0}
".format(tag[0], tag[1])
                for tag in selected_tags
            ]
        )
        tag_ul = u"
{1}
".format(list_attrs, tag_li)
        return mark_safe(u"{0}{1}".format(tag_ul, input_field))

    @property
    def media(self):
        extra = "" if settings.DEBUG else ".min"
        admin_prefix = "admin/js"
        js = [
            "%s/vendor/jquery/jquery%s.js" % (admin_prefix, extra),
            "%s/jquery.init.js" % admin_prefix,
            "%s/core.js" % admin_prefix,
            "taggit_labels/js/taggit_labels.js",
        ]
        css = {"all": ("taggit_labels/css/taggit_labels.css",)}

        return forms.Media(js=js, css=css)


import math as math
import numpy as num

# ~ from myBasic import pickColor

# Global
res = 0.0001
s_res = math.pi / 180.0

def order(v, w, mode=0):
    a = zip(v, w)
    if mode == 0:
        a.sort()
    else:
        a.sort(reverse=True)
    l = zip(*a)
    v = list(l[0])
    w = list(l[1])
    return [v, w]

def cvHull(pL):
    if len(pL) < 3:
        return pL
    from scipy.spatial import ConvexHull
    from scipy.spatial.qhull import QhullError
    S = [xx.std() for xx in pL]
    try:
        hull = ConvexHull(S)
    except QhullError:
        xL = [xx.x for xx in pL]
        xx, pL = order(xL, pL)
        return [pL[0], pL[-1]]
    P = [pL[i] for i in hull.vertices]
    P.append(pL[hull.vertices[0]])
    return P

def solve2p(a, b, c):
    d = float(b ** 2 - 4 * a * c)
    if d < 0:
        return []
    else:
        d = d ** .5
        x1 = (-b + d) / (2 * a)
        x2 = (-b - d) / (2 * a)
    return [x1, x2]

def angleMap(a):
    a1 = a % (2 * math.pi)
    if a1 > math.pi:
        return -2 * math.pi + a1
    else:
        return a1

def uni_obj(ol):
    l = len(ol)
    if l < 2:
        return ol
    out = [ol[0]]
    for i in range(1, l):
        cO = ol[i]
        fg = True
        for w in out:
            if w == cO:
                fg = False
        if fg:
            out.append(cO)
    return out

def circle_intersect(c1, c2):
    d = c1.c.dist(c2.c)
    x1 = (c2.r ** 2 - c1.r ** 2 - d ** 2) / (-2 * d)
    y1 = (c1.r ** 2 - x1 ** 2) ** .5
    y2 = -(c1.r ** 2 - x1 ** 2) ** .5
    v = vec(c1.c, c2.c)
    a = v.angle()
    p1 = point(x1, y1)
    p2 = point(x1, y2)
    p1 = p1.rot(a) + c1.c
    p2 = p2.rot(a) + c1.c
    return [p1, p2]

def circles_intersect(sA):
    # unique touching point between 3 circles
    sA = uni_obj(sA)
    l = len(sA)
    if l < 3:
        raise geoError('InputError')
    # Checking disjoint case
    for i in range(0, l - 1):
        for j in range(i + 1, l):
            if (sA[i].c.dist(sA[j].c) > (sA[i].r + sA[j].r)):
                raise geoError('Disjoint')
    p1, p2 = circle_intersect(sA[0], sA[1])
    p3, p4 = circle_intersect(sA[2], sA[1])
    if p1.dist(p3) < res or p1.dist(p4) < res:
        return p1
    else:
        return p2

def drawC(cA, ax):
    xmin = float('inf')
    xmax = -xmin
    ymin = float('inf')
    ymax = -ymin
    for q, w in enumerate(cA):
        t1 = w.c.x - w.r
        t2 = w.c.x + w.r
        if t1 < xmin:
            xmin = t1
        if t2 > xmax:
            xmax = t2
        t1 = w.c.y - w.r
        t2 = w.c.y + w.r
        if t1 < ymin:
            ymin = t1
        if t2 > ymax:
            ymax = t2
        circ = pl.Circle((w.c.x, w.c.y), radius=w.r, fc='none', lw=2, ec=pickColor(q))
        ax.add_artist(circ)
    ax.set_xlim([xmin, xmax])
    ax.set_ylim([ymin, ymax])

def order(v, w, mode=0):
    a = zip(v, w)
    if mode == 0:
        a.sort()
    else:
        a.sort(reverse=True)
    l = zip(*a)
    v = list(l[0])
    w = list(l[1])
    return [v, w]

class geoError(Exception):
    def __init__(self, value):
        self.tag = value

    def __str__(self):
        return repr(self.tag)

class point:
    def __init__(self, *argv):
        l = len(argv)
        if l == 1:
            self.dim = len(argv[0])
        else:
            self.dim = l
        if l == 1:
            self.x = argv[0][0]
            self.y = argv[0][1]
            try:
                self.z = argv[0][2]
            except IndexError:
                self.z = 0.0
        else:
            if l == 2:
                z = 0
            elif l == 3:
                z = argv[2]
            else:
                raise geoError('Input')
            self.x = float(argv[0])
            self.y = float(argv[1])
            self.z = z

    def __str__(self):
        return 'p(' + str(self.x) + ',' + str(self.y) + ',' + str(self.z) + ')'

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def __sub__(self, other):
        if isinstance(other, point):
            tx = self.x - other.x
            ty = self.y - other.y
            tz = self.z - other.z
        else:
            tx = self.x - other.dx
            ty = self.y - other.dy
            tz = self.z - other.dz
        return point(tx, ty, tz)

    def __add__(self, other):
        if isinstance(other, point):
            tx = self.x + other.x
            ty = self.y + other.y
            tz = self.z + other.z
        else:
            tx = self.x + other.dx
            ty = self.y + other.dy
            tz = self.z + other.dz
        return point(tx, ty, tz)

    def __mul__(self, other):
        return point(other * self.x, other * self.y, other * self.z)

    def __rmul__(self, other):
        return point(other * self.x, other * self.y, other * self.z)

    def __div__(self, other):
        return point(self.x / other, self.y / other, self.z / other)

    def __neg__(self):
        x = -self.x
        y = -self.y
        z = -self.z
        return point(x, y, z)

    def area(self):
        return 0.0

    def dist(self, other):
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2 + (self.z - other.z) ** 2) ** 0.5

    def std(self):
        if self.dim == 2:
            return [self.x, self.y]
        return [self.x, self.y, self.z]

    def c2s(self):
        R = self.dist(point(0, 0, 0))
        lg = math.atan(self.y / self.x)
        lat = acos(self.z / R)
        return (lg, lat, R)

    # ~ def transform(self,p1,p2):
    # ~ if isinstance(p2,point):
    # ~ v=vec(p1,p2)
    # ~ rot=v.angle()
    # ~ return self.transform(p1,rot)
    # ~ else:
    # ~ temp=self-p1
    # ~ rot=p2
    # ~ px=math.cos(rot)*temp.x+math.sin(rot)*temp.y
    # ~ py=-math.sin(rot)*temp.x+math.cos(rot)*temp.y
    # ~ return point(px,py)
    def transform(self, p, rot):
        px = math.cos(rot) * self.x + math.sin(rot) * self.y
        py = -math.sin(rot) * self.x + math.cos(rot) * self.y
        p_t = point(px, py)
        return p_t - p

    def rot(self, a):
        px = math.cos(a) * self.x - math.sin(a) * self.y
        py = math.sin(a) * self.x + math.cos(a) * self.y
        return point(px, py)

    def angle(self, p):
        v = vec(self, p)
        return v.angle()

class vec:
    def __init__(self, *argv):
        if len(argv) >= 2:
            if isinstance(argv[0], point) and isinstance(argv[1], point):
                p = argv[1] - argv[0]
                self.dx = float(p.x)
                self.dy = float(p.y)
                self.dz = float(p.z)
            else:
                self.dx = float(argv[0])
                self.dy = float(argv[1])
                try:
                    self.dz = float(argv[2])
                except IndexError:
                    self.dz = 0.0
        else:
            p = argv[0]
            self.dx = float(p.x)
            self.dy = float(p.y)
            self.dz = float(p.z)

    def __str__(self):
        return 'vec(' + str(self.dx) + ',' + str(self.dy) + ',' + str(self.dz) + ')'

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def __sub__(self, other):
        if isinstance(other, vec):
            tx = self.dx - other.dx
            ty = self.dy - other.dy
            tz = self.dz - other.dz
            return vec(tx, ty, tz)
        else:
            tx = self.dx - other.x
            ty = self.dy - other.y
            tz = self.dz - other.z
            return point(tx, ty, tz)

    def __add__(self, other):
        if isinstance(other, vec):
            tx = self.dx + other.dx
            ty = self.dy + other.dy
            tz = self.dz + other.dz
            return vec(tx, ty, tz)
        else:
            tx = self.dx + other.x
            ty = self.dy + other.y
            tz = self.dz + other.z
            return point(tx, ty, tz)

    def __mul__(self, other):
        if isinstance(other, vec):
            return self.cross(other)
        return vec(other * self.dx, other * self.dy, other * self.dz)

    def __rmul__(self, other):
        if isinstance(other, vec):
            return other.cross(self)
        return vec(other * self.dx, other * self.dy, other * self.dz)

    def __div__(self, other):
        return vec(self.dx / other, self.dy / other, self.dz / other)

    def dot(self, v):
        return self.dx * v.dx + self.dy * v.dy + self.dz * v.dz

    def cross(self, v):
        z = self.dx * v.dy - self.dy * v.dx
        x = self.dy * v.dz - self.dz * v.dy
        y = self.dz * v.dx - self.dx * v.dz
        return vec(x, y, z)

    def mag(self):
        return (self.dx ** 2 + self.dy ** 2 + self.dz ** 2) ** 0.5

    def angle(self, *args):
        x = self.dx
        y = self.dy
        z = self.dz

        if len(args) == 0:
            if self.mag() < res:
                return 0.0
            if x >= 0 and y >= 0:
                try:
                    return math.atan(y / x)
                except ZeroDivisionError:
                    return math.pi / 2
            elif x < 0 and y >= 0:
                return math.pi - math.atan(y / abs(x))
            elif x >= 0 and y < 0:
                try:
                    return -math.atan(abs(y) / x)
                except ZeroDivisionError:
                    return -math.pi / 2
            else:
                return math.atan(abs(y) / abs(x)) - math.pi
        elif len(args) == 1:
            b = args[0]
            try:
                rv = math.acos(self.dot(b) / (self.mag() * b.mag()))
                return rv
            except ZeroDivisionError:
                return 0.0

    def rot(self, a):
        dx = self.dx
        dy = self.dy
        dx_t = self.dx * math.cos(a) - self.dy * math.sin(a)
        dy_t = self.dx * math.sin(a) + self.dy * math.cos(a)
        return vec(dx_t, dy_t)

    def norm(self):
        return self / self.mag()

    def floor(self):
        # return to normal vector in the plane normal to the self for
        # Complete coordinate system
        a = self.dx
        b = self.dy
        c = self.dz
        if self.mag() < res:
            raise geoError('ZeroEntity')
        if (abs(a) < res and abs(b) < res):
            return (vec(0, 0, 1), vec(0, 1, 0), vec(1, 0, 0))
        elif (abs(a) < res and abs(c) < res):
            return (vec(0, 1, 0), vec(1, 0, 0), vec(0, 0, 1))
        elif (abs(c) < res and abs(b) < res):
            return (vec(1, 0, 0), vec(0, 1, 0), vec(0, 0, 1))
        else:
            ex = self
            if abs(c) > res:
                q = 0
                w = .5
                e = -w * b / c
                ey = vec(q, w, e)
            else:
                e = 0
                q = .5
                w = -q * a / b
                ey = vec(q, w, e)
            ez = ex.cross(ey)
        return (ex / ex.mag(), ey / ey.mag(), ez / ez.mag())

class circle:
    def __init__(self, p, r):
        self.c = p
        self.r = float(r)

    def __str__(self):
        return 'Circle[' + self.c.__str__() + ',' + str(self.r) + ']'

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def touch(self, o, fg=0):
        d = self.c.dist(o.c)
        if fg == 0:
            met = self.r + o.r
        else:
            met = 180.0 * (self.r + o.r) / (E.R * math.pi)
        if d < met + res:
            return True
        else:
            return False

    def side(self, p):
        d = p.dist(self.c)
        r = self.r
        if d < r:
            return 1
        elif d < r + res:
            return 0
        else:
            return -1

    def to_poly(self, step):
        alpha = 0
        pll = []
        while alpha < (2 * math.pi):
            x = self.c + point(self.r * math.cos(alpha), self.r * math.sin(alpha))
            pll.append(x)
            alpha = alpha + step
        pll[-1] = pll[0]
        return pll

class line:
    def __init__(self, x, y):
        if isinstance(x, point) and isinstance(y, point):
            if x == y:
                raise geoError('Degenerate')
            try:
                temp = (x.y - y.y) / (x.x - y.x)
                if temp == 0:
                    self.kind = 'Horizental'
                    self.b = x.y
                else:
                    self.kind = 'Normal'
                    self.b = x.y - temp * x.x
            except ZeroDivisionError:
                temp = float('inf')
                self.kind = 'Vertical'
                self.b = x.x
            self.m = temp
        elif (not isinstance(x, point)) and isinstance(y, point):
            self.m = float(x)
            if x == 0:
                self.kind = 'Horizental'
                self.b = y.y
            elif x == float('inf'):
                self.kind = 'Vertical'
                self.b = y.x
            else:
                self.kind = 'Normal'
                self.b = y.y - x * y.x
        else:
            self.m = float(x)
            if x == 0:
                self.kind = 'Horizental'
            elif x == float('inf'):
                self.kind = 'Vertical'
            else:
                self.kind = 'Normal'
            self.b = float(y)

    def __str__(self):
        if self.kind == 'Vertical':
            return 'Line(x=' + str(self.b) + ')'
        elif self.kind == 'Horizental':
            return 'Line(y=' + str(self.b) + ')'
        else:
            return 'Line(' + str(self.m) + 'x+' + str(self.b) + ')'

    def intersect(self, L):
        if isinstance(L, line):
            if self.m == L.m:
                raise geoError('parellel')
            else:
                if self.kind == 'Normal' and L.kind == 'Normal':
                    x = (self.b - L.b) / (L.m - self.m)
                    y = L.m * x + L.b
                    return point(x, y)
                elif self.kind == 'Horizental' and L.kind == 'Normal':
                    x = (self.b - L.b) / L.m
                    return point(x, self.b)
                elif self.kind == 'Vertical' and L.kind == 'Normal':
                    y = L.m * self.b + L.b
                    return point(self.b, y)
                elif L.kind == 'Horizental' and self.kind == 'Normal':
                    x = (L.b - self.b) / self.m
                    return point(x, L.b)
                elif L.kind == 'Vertical' and self.kind == 'Normal':
                    y = self.m * L.b + self.b
                    return point(L.b, y)
                elif self.kind == 'Horizental' and L.kind == 'Vertical':
                    return point(L.b, self.b)
                elif self.kind == 'Vertical' and L.kind == 'Horizental':
                    return point(self.b, L.b)
                else:
                    raise geoError('Unknown')
        else:
            p = L.c
            x0 = p.x
            y0 = p.y
            r = L.r
            m = self.m
            b = self.b
            c0 = b - y0
            if self.kind == 'Vertical':
                x = self.b
                sol = solve2p(1, -2 * y0, (x - x0) ** 2 - r ** 2 + y0 ** 2)
                w = []
                for yy in sol:
                    w.append(point(x, yy))
                return w
            else:
                sol = solve2p(1 + m ** 2, -2 * x0 + 2 * m * c0, x0 ** 2 + c0 ** 2 - r ** 2)
                w = []
                for x in sol:
                    w.append(point(x, m * x + b))
                return w

    def side(self, p):
        if slef.kind == 'Vertical':
            if abs(self.b - p.y) < res:
                met = 0.0
            elif self.b > p.y:
                met = 1
            else:
                met = -1
        elif self.kind == 'Horizental':
            if abs(self.b - p.x) < res:
                met = 0.0
            elif self.b > p.x:
                met = -1
            else:
                met = 1
        else:
            met = L.m * p.x + L.b - p.y
        if abs(met) < res:
            return 0
        elif met < 0.0:
            return -1
        else:
            return 1

    def side(self, p):
        if self.kind == 'Vertical':
            if p.x == self.b:
                return 0
            elif p.x > self.b:
                return -1
            else:
                return 1
        else:
            met = p.y - (self.m * p.x + self.b)
        if met < 0:
            return -1
        elif met > 0:
            return 1
        else:
            return 0

class Triangle:
    def __init__(self, L):
        x = L[0]
        y = L[1]
        z = L[-1]
        L = line(x, y)
        if L.side(z) == 0:
            raise geoError('Degenerate')
        self.a = x
        self.b = y
        self.c = z

    def __str__(self):
        return 'Triangle{' + self.a.__str__().strip('p') + ',' + self.b.__str__().strip(
            'p') + ',' + self.c.__str__().strip('p') + '}'

    def centroid(self):
        V01 = 0.5 * (self.a + self.b)
        V12 = 0.5 * (self.c + self.b)
        L1 = line(V01, self.c)
        L2 = line(V12, self.a)
        return L1.intersect(L2)

    def A(self):
        v1 = vec(self.a, self.b)
        v2 = vec(self.a, self.c)
        return 0.5 * abs(v1.cross(v2))

class Sphere:
    def __init__(self, c, R):
        self.c = c
        self.R = R

    def dist(self, p):
        return abs(p.dist(self.c) - self.R)

    def s2c(self, lon, lat):
        x = self.R * math.cos(lat) * math.cos(lon)
        y = self.R * math.cos(lat) * math.sin(lon)
        z = self.R * math.sin(lat)
        return self.c + point(x, y, z)

    def gcd(self, lon1, lat1, lon2, lat2):
        """
        Calculate the great circle distance between two points
        on the earth (specified in decimal degrees)
        """
        # convert decimal degrees to radians
        lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])

        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
        c = 2 * math.asin(math.sqrt(a))

        dis = E.R * c
        return dis

    def gcd2l(self, d):
        # great circle distance to straight line distance
        theta = d / self.R
        return (2 * (self.R ** 2) * (1 - math.cos(theta))) ** 0.5

    def side(self, p):
        d = pdist(self.c, p)
        if d > R - res and d < R + res:
            return 0
        if d < R:
            return -1
        else:
            return 1

    def small_circle(self, S):
        R = self.R
        r = S.R
        d = self.c.dist(S.c)
        if d > (R + r):
            raise geoError('Disjoint')
        ex1 = vec(S.c - self.c)
        ex, ey, ez = ex1.floor()
        x = (d ** 2 - r ** 2 + R ** 2) / (2 * d)
        temp = (4 * (d ** 2) * (R ** 2)) - ((d ** 2) - (r ** 2) + (R ** 2)) ** 2
        a = math.sqrt(temp) / (2 * d)
        # sampling the small circle and transforming the coordinates
        n = 2 * int(math.pi / s_res)
        theta = num.linspace(0, 2 * math.pi, n)
        sc = []
        for alpha in theta:
            y = a * math.cos(alpha)
            z = a * math.sin(alpha)
            xyz = point(x, y, z)
            pf = xyz.x * ex + xyz.y * ey + xyz.z * ez + self.c
            sc.append(pf)
        return sc

    def map(self, p, inv=False):
        # cartesian to lat/lon
        # if inv is true lat/lon to cartesian
        R = self.R
        if not inv:
            ed = R * (vec(p - self.c).norm())
            ed = point(ed.dx, ed.dy, ed.dz)
            lon = math.atan2(ed.y, ed.x)
            lat1 = math.acos(abs(ed.z) / R)
            if ed.z > 0:
                lat = math.pi / 2 - lat1
            else:
                lat = -(math.pi / 2 - lat1)
            return point(lon, lat) * 180 / math.pi
        if inv:
            p = p * math.pi / 180
            z = R * math.sin(p.y)
            y = R * math.cos(p.y) * math.sin(p.x)
            x = R * math.cos(p.y) * math.cos(p.x)
            return point(x, y, z)

class Plane:
    def __init__(self, v, p):
        self.v = v
        self.p = p

class Polygon:
    def __init__(self, L):
        # L is the list of points on the polygon
        # L[-1]==L[0] Closed polygon
        if L[-1] == L[0]:
            self.L = L
            self.n = len(L)
        else:
            raise geoError('open')

    def area(self):
        s = 0
        for i in range(self.n - 1):
            p1 = self.L[i]
            p2 = self.L[i + 1]
            s = s + p1.x * p2.y - p2.x * p1.y
        return 0.5 * s

    def centroid(self):
        sx = 0
        sy = 0
        for i in range(self.n - 1):
            p1 = self.L[i]
            p2 = self.L[i + 1]
            sx = sx + (p1.x * p2.y - p2.x * p1.y) * (p1.x + p2.x)
            sy = sy + (p1.x * p2.y - p2.x * p1.y) * (p1.y + p2.y)
        p = point(sx, sy)
        return p / (6.0 * self.area())

class Ray:
    def __init__(self, p, a):
        self.c = p
        self.a = angleMap(a)

    def side(self, p):
        ba = p.angle(self.c)
        da = abs(ba - a) / (2 * math.pi)
        if da % (2 * math.pi) < res:
            return 1
        else:
            return 1

    def to_line(self):
        if (abs(self.a - math.pi / 2) < res) or (abs(self.a + math.pi / 2) < res):
            m = float('inf')
            b = self.c.x
            return line(m, b)
        m = math.tan(self.a)
        return line(m, self.c)

    def intersect(self, c):
        if isinstance(c, circle):
            L = self.to_line()
            p = L.intersect(c)
            if len(p) == 0:
                return
            else:
                a1 = self.c.angle(p[0])
                a2 = self.c.angle(p[1])
                if abs(a1 - a2) < res:
                    print('hey')
                    if p[0].dist(self.c) > p[1].dist(self.c):
                        return [p[1], p[0]]
                    else:
                        return [p[0], p[1]]
                elif abs(a1 - self.a) > abs(a2 - self.a):
                    return [p[1]]
                else:
                    return [p[0]]

class ndisc:
    def __init__(self, cA):
        self.cA = cA
        self.n = len(cA)

    def is_disjoint(self):
        l = len(self.cA)
        for i in range(l):
            for j in range(i + 1, l):
                if not self.cA[j].touch(self.cA[i]):
                    return True
        return False

    def sort(self):
        r = [xx.r for xx in self.cA]
        r, self.cA = order(r, self.cA)

    # ~ def remove_ins(self):
    # ~ self.sort()
    # ~ if cA[
    def side(self, p):
        counter = [0, 0, 0]  # [#inside,#on,#outside]
        for cir in self.cA:
            fg = cir.side(p)
            if fg == 1:
                counter[0] = counter[0] + 1
            elif fg == 0:
                counter[1] = counter[1] + 1
            else:
                counter[2] = counter[2] + 1
        return counter

    def get_x0(self, weighted=True):
        l = self.n
        r = [w.r for w in self.cA]
        c = [w.c for w in self.cA]
        S = sum(r)
        W = [(S - w) / ((l - 1) * S) for w in r]
        p0 = point(0, 0)  # Initialized point
        for i in range(l):
            p0 = p0 + W[i] * c[i]
        return p0

    def intersect(self, obj):
        l = self.n
        d = [0.0] * l
        pll = [point(0, 0)] * l
        if isinstance(obj, Ray):
            for i, cir in enumerate(self.cA):
                pll[i] = obj.intersect(cir)[0]
                d[i] = pll[i].dist(obj.c)
        usd, pn = order(d, pll)
        return pn[0]

    def poly(self, step):
        # step is the radian resolution
        pll = []
        cA = self.cA
        if len(cA) == 0:
            raise geoError('InputError')
        elif len(cA) == 1:
            return cA[0].to_poly(step)

        if self.is_disjoint():
            raise geoError('Disjoint')
        self.sort()
        cA = self.cA
        st = False
        fin = False
        cdi = 0
        alpha = 0
        x0 = self.cA[0].c + point(self.cA[0].r, 0)
        xc = x0
        # Finding the first point on Polygon
        while (alpha < (2 * math.pi)):
            if self.side(xc)[-1] == 0:
                pll.append(xc)
                st = True
                break
            else:
                xx = num.cos(step) * cA[cdi].r
                yy = num.sin(step) * cA[cdi].r
                xc_t = point(xx, yy)
                xc = xc_t.transform(-cA[cdi].c, -alpha)
                alpha = alpha + step
        if not st:
            dA = uni_obj(cA)
            if len(dA) == 1:
                return dA[0].to_poly()
            elif len(dA) == 2:
                p_t = circle_intersect(dA[0], dA[1])
                return [p_t]
            else:
                p_t = circles_intersect(dA)
                return [p_t]

        # pivoting
        pv0 = self.get_x0()
        pv = point(pv0.x, pv0.y)
        v0 = vec(xc, pv)
        vc = vec(xc, pv)
        spin = 0.0
        pv_found = False
        while spin < (2 * math.pi):
            if self.side(pv)[0] == self.n:
                pv_found = True
                break
            else:
                vc = vc / 2
                if vc.mag() < 10 * res:
                    spin = spin + 10 * step
                    vc = v0.rot(spin)
                pv = xc + vc
        # pivoting finished
        if not pv_found:
            return [xc]
        alpha = num.linspace(0, 2 * math.pi, int(2 * math.pi / step))
        alpha[-1] = math.pi * 2
        for a in alpha:
            ray = Ray(pv, a)
            try:
                pc = self.intersect(ray)
                pll.append(pc)
            except geoError:
                print('Unknown Error in ray-ndisc intersection')
                raise geoError('Unknown')
        pll[-1] = pll[0]
        return pll

E = Sphere(point(0, 0, 0), 6378100)
# ~

# ~ if __name__=='__main__':
# ~ vc=point(5439680.78365,-959162.488598,3189050.0)
# ~ ws=point(10,30)
# ~ wc=E.map(ws,inv=True)
# ~ st=wc.dist(vc)
# ~ vs=E.map(vc)
# ~ print( st)
# ~ yy=E.gcd(vs.x,vs.y,ws.x,ws.y)
# ~ print( E.gcd2l(yy))


# pylint:disable=line-too-long
"""Parser for ssh public keys. Currently supports ssh-rsa, ssh-dsa, ssh-ed25519 and ssh-dss keys.

import sys

key_data = open("ssh-pubkey-file.pem").read()
ssh_key = SSHKey(key_data)
try:
    ssh_key.parse()
except InvalidKeyError:
    print("Invalid key")
    sys.exit(1)
print(ssh_key.bits)"""

from .exceptions import (InvalidKeyError, InvalidKeyLengthError, InvalidOptionNameError, InvalidOptionsError,
                         InvalidTypeError, MalformedDataError, MissingMandatoryOptionValueError, TooLongKeyError,
                         TooShortKeyError, UnknownOptionNameError)
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric.dsa import DSAParameterNumbers, DSAPublicNumbers
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers

import base64
import binascii
import ecdsa
import hashlib
import re
import struct
import sys
import warnings

__all__ = ["AuthorizedKeysFile", "SSHKey"]

class AuthorizedKeysFile(object):  # pylint:disable=too-few-public-methods
    """Represents a full authorized_keys file.

    Comments and empty lines are ignored."""

    def __init__(self, file_obj, **kwargs):
        self.keys = []
        self.parse(file_obj, **kwargs)

    def parse(self, file_obj, **kwargs):
        for line in file_obj:
            line = line.strip()
            if not line:
                continue
            if line.startswith("#"):
                continue
            ssh_key = SSHKey(line, **kwargs)
            ssh_key.parse()
            self.keys.append(ssh_key)

class SSHKey(object):  # pylint:disable=too-many-instance-attributes
    """Represents a single SSH keypair.

    ssh_key = SSHKey(key_data, strict=True)
    ssh_key.parse()

    strict=True (default) only allows keys ssh-keygen generates. Setting strict mode to false allows
    all keys OpenSSH actually accepts, including highly insecure ones. For example, OpenSSH accepts
    512-bit DSA keys and 64-bit RSA keys which are highly insecure."""

    DSA_MIN_LENGTH_STRICT = 1024
    DSA_MAX_LENGTH_STRICT = 1024
    DSA_MIN_LENGTH_LOOSE = 1
    DSA_MAX_LENGTH_LOOSE = 3072

    DSA_N_LENGTH = 160

    ECDSA_CURVE_DATA = {
        b"nistp256": (ecdsa.curves.NIST256p, hashlib.sha256),
        b"nistp192": (ecdsa.curves.NIST192p, hashlib.sha256),
        b"nistp224": (ecdsa.curves.NIST224p, hashlib.sha256),
        b"nistp384": (ecdsa.curves.NIST384p, hashlib.sha384),
        b"nistp521": (ecdsa.curves.NIST521p, hashlib.sha512),
    }

    RSA_MIN_LENGTH_STRICT = 1024
    RSA_MAX_LENGTH_STRICT = 16384
    RSA_MIN_LENGTH_LOOSE = 768
    RSA_MAX_LENGTH_LOOSE = 16384

    # Valid as of OpenSSH_6.9p1
    # argument name, value is mandatory. Options are case-insensitive, but this list must be in lowercase.
    OPTIONS_SPEC = [
        ("agent-forwarding", False),
        ("cert-authority", False),
        ("command", True),
        ("environment", True),
        ("from", True),
        ("no-agent-forwarding", False),
        ("no-port-forwarding", False),
        ("no-pty", False),
        ("no-user-rc", False),
        ("no-x11-forwarding", False),
        ("permitopen", True),
        ("port-forwarding", False),
        ("principals", True),
        ("pty", False),
        ("restrict", False),
        ("tunnel", True),
        ("user-rc", False),
        ("x11-forwarding", False),
    ]
    OPTION_NAME_RE = re.compile("^[A-Za-z0-9-]+$")

    INT_LEN = 4

    FIELDS = ["rsa", "dsa", "ecdsa", "bits", "comment", "options", "options_raw", "key_type"]

    def __init__(self, keydata=None, **kwargs):
        self.keydata = keydata
        self._decoded_key = None
        self.rsa = None
        self.dsa = None
        self.ecdsa = None
        self.bits = None
        self.comment = None
        self.options = None
        self.options_raw = None
        self.key_type = None
        self.strict_mode = bool(kwargs.get("strict", True))
        self.skip_option_parsing = bool(kwargs.get("skip_option_parsing", False))
        self.disallow_options = bool(kwargs.get("disallow_options", False))
        if keydata:
            try:
                self.parse(keydata)
            except (InvalidKeyError, NotImplementedError):
                pass

    def __str__(self):
        return "Key type: %s, bits: %s, options: %s" % (self.key_type, self.bits, self.options)

    def reset(self):
        """Reset all data fields."""
        for field in self.FIELDS:
            setattr(self, field, None)

    def hash(self):
        """Calculate md5 fingerprint.

        Deprecated, use .hash_md5() instead."""
        warnings.warn("hash() is deprecated. Use hash_md5(), hash_sha256() or hash_sha512() instead.")
        return self.hash_md5().replace(b"MD5:", b"")

    def hash_md5(self):
        """Calculate md5 fingerprint.

        Shamelessly copied from http://stackoverflow.com/questions/6682815/deriving-an-ssh-fingerprint-from-a-public-key-in-python

        For specification, see RFC4716, section 4."""
        fp_plain = hashlib.md5(self._decoded_key).hexdigest()
        return "MD5:" + ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))

    def hash_sha256(self):
        """Calculate sha256 fingerprint."""
        fp_plain = hashlib.sha256(self._decoded_key).digest()
        return (b"SHA256:" + base64.b64encode(fp_plain).replace(b"=", b"")).decode("utf-8")

    def hash_sha512(self):
        """Calculates sha512 fingerprint."""
        fp_plain = hashlib.sha512(self._decoded_key).digest()
        return (b"SHA512:" + base64.b64encode(fp_plain).replace(b"=", b"")).decode("utf-8")

    def _unpack_by_int(self, data, current_position):
        """Returns a tuple with (location of next data field, contents of requested data field)."""
        # Unpack length of data field
        try:
            requested_data_length = struct.unpack('>I', data[current_position:current_position + self.INT_LEN])[0]
        except struct.error:
            raise MalformedDataError("Unable to unpack %s bytes from the data" % self.INT_LEN)

        # Move pointer to the beginning of the data field
        current_position += self.INT_LEN
        remaining_data_length = len(data[current_position:])

        if remaining_data_length < requested_data_length:
            raise MalformedDataError(
                "Requested %s bytes, but only %s bytes available." % (requested_data_length, remaining_data_length)
            )

        next_data = data[current_position:current_position + requested_data_length]
        # Move pointer to the end of the data field
        current_position += requested_data_length
        return current_position, next_data

    @classmethod
    def _parse_long(cls, data):
        """Calculate two's complement."""
        if sys.version < '3':
            # this does not exist in python 3 - undefined-variable disabled to make pylint happier.
            ret = long(0)  # pylint:disable=undefined-variable
            for byte in data:
                ret = (ret << 8) + ord(byte)
        else:
            ret = 0
            for byte in data:
                ret = (ret << 8) + byte
        return ret

    def _split_key(self, data):
        options_raw = None
        # Terribly inefficient way to remove options, but hey, it works.
        if not data.startswith("ssh-") and not data.startswith("ecdsa-"):
            quote_open = False
            for i, character in enumerate(data):
                if character == '"':  # only double quotes are allowed, no need to care about single quotes
                    quote_open = not quote_open
                if quote_open:
                    continue
                if character == " ":
                    # Data begins after the first space
                    options_raw = data[:i]
                    data = data[i + 1:]
                    break
            else:
                raise MalformedDataError("Couldn't find beginning of the key data")
        key_parts = data.strip().split(None, 2)
        if len(key_parts) < 2:  # Key type and content are mandatory fields.
            raise InvalidKeyError("Unexpected key format: at least type and base64 encoded value is required")
        if len(key_parts) == 3:
            self.comment = key_parts[2]
            key_parts = key_parts[0:2]
        if options_raw:
            # Populate and parse options field.
            self.options_raw = options_raw
            if not self.skip_option_parsing:
                self.options = self.parse_options(self.options_raw)
        else:
            # Set empty defaults for fields
            self.options_raw = None
            self.options = {}
        return key_parts

    @classmethod
    def decode_key(cls, pubkey_content):
        """Decode base64 coded part of the key."""
        try:
            decoded_key = base64.b64decode(pubkey_content.encode("ascii"))
        except (TypeError, binascii.Error):
            raise MalformedDataError("Unable to decode the key")
        return decoded_key

    @classmethod
    def _bits_in_number(cls, number):
        return len(format(number, "b"))

    def parse_options(self, options):
        """Parses ssh options string."""
        quote_open = False
        parsed_options = {}

        def parse_add_single_option(opt):
            """Parses and validates a single option, and adds it to parsed_options field."""
            if "=" in opt:
                opt_name, opt_value = opt.split("=", 1)
                opt_value = opt_value.replace('"', '')
            else:
                opt_name = opt
                opt_value = True
            if " " in opt_name or not self.OPTION_NAME_RE.match(opt_name):
                raise InvalidOptionNameError("%s is not valid option name." % opt_name)
            if self.strict_mode:
                for valid_opt_name, value_required in self.OPTIONS_SPEC:
                    if opt_name.lower() == valid_opt_name:
                        if value_required and opt_value is True:
                            raise MissingMandatoryOptionValueError("%s is missing mandatory value." % opt_name)
                        break
                else:
                    raise UnknownOptionNameError("%s is unrecognized option name." % opt_name)
            if opt_name not in parsed_options:
                parsed_options[opt_name] = []
            parsed_options[opt_name].append(opt_value)

        start_of_current_opt = 0
        i = 1  # Need to be set for empty options strings
        for i, character in enumerate(options):
            if character == '"':  # only double quotes are allowed, no need to care about single quotes
                quote_open = not quote_open
            if quote_open:
                continue
            if character == ",":
                opt = options[start_of_current_opt:i]
                parse_add_single_option(opt)
                start_of_current_opt = i + 1
                # Data begins after the first space
        if start_of_current_opt + 1 != i:
            opt = options[start_of_current_opt:]
            parse_add_single_option(opt)
        if quote_open:
            raise InvalidOptionsError("Unbalanced quotes.")
        return parsed_options

    def _process_ssh_rsa(self, data):
        """Parses ssh-rsa public keys."""
        current_position, raw_e = self._unpack_by_int(data, 0)
        current_position, raw_n = self._unpack_by_int(data, current_position)

        unpacked_e = self._parse_long(raw_e)
        unpacked_n = self._parse_long(raw_n)

        self.rsa = RSAPublicNumbers(unpacked_e, unpacked_n).public_key(default_backend())
        self.bits = self.rsa.key_size

        if self.strict_mode:
            min_length = self.RSA_MIN_LENGTH_STRICT
            max_length = self.RSA_MAX_LENGTH_STRICT
        else:
            min_length = self.RSA_MIN_LENGTH_LOOSE
            max_length = self.RSA_MAX_LENGTH_LOOSE
        if self.bits < min_length:
            raise TooShortKeyError(
                "%s key data can not be shorter than %s bits (was %s)" % (self.key_type, min_length, self.bits)
            )
        if self.bits > max_length:
            raise TooLongKeyError(
                "%s key data can not be longer than %s bits (was %s)" % (self.key_type, max_length, self.bits)
            )
        return current_position

    def _process_ssh_dss(self, data):
        """Parses ssh-dsa public keys."""
        data_fields = {}
        current_position = 0
        for item in ("p", "q", "g", "y"):
            current_position, value = self._unpack_by_int(data, current_position)
            data_fields[item] = self._parse_long(value)

        q_bits = self._bits_in_number(data_fields["q"])
        p_bits = self._bits_in_number(data_fields["p"])
        if q_bits != self.DSA_N_LENGTH:
            raise InvalidKeyError("Incorrect DSA key parameters: bits(p)=%s, q=%s" % (self.bits, q_bits))
        if self.strict_mode:
            min_length = self.DSA_MIN_LENGTH_STRICT
            max_length = self.DSA_MAX_LENGTH_STRICT
        else:
            min_length = self.DSA_MIN_LENGTH_LOOSE
            max_length = self.DSA_MAX_LENGTH_LOOSE
        if p_bits < min_length:
            raise TooShortKeyError("%s key can not be shorter than %s bits (was %s)" % (self.key_type, min_length, p_bits))
        if p_bits > max_length:
            raise TooLongKeyError(
                "%s key data can not be longer than %s bits (was %s)" % (self.key_type, max_length, p_bits)
            )

        dsa_parameters = DSAParameterNumbers(data_fields["p"], data_fields["q"], data_fields["g"])
        self.dsa = DSAPublicNumbers(data_fields["y"], dsa_parameters).public_key(default_backend())
        self.bits = self.dsa.key_size

        return current_position

    def _process_ecdsa_sha(self, data):
        """Parses ecdsa-sha public keys."""
        current_position, curve_information = self._unpack_by_int(data, 0)
        if curve_information not in self.ECDSA_CURVE_DATA:
            raise NotImplementedError("Invalid curve type: %s" % curve_information)
        curve, hash_algorithm = self.ECDSA_CURVE_DATA[curve_information]

        current_position, key_data = self._unpack_by_int(data, current_position)
        try:
            # data starts with \x04, which should be discarded.
            ecdsa_key = ecdsa.VerifyingKey.from_string(key_data[1:], curve, hash_algorithm)
        except AssertionError:
            raise InvalidKeyError("Invalid ecdsa key")
        self.bits = int(curve_information.replace(b"nistp", b""))
        self.ecdsa = ecdsa_key
        return current_position

    def _process_ed25516(self, data):
        """Parses ed25516 keys.

        There is no (apparent) way to validate ed25519 keys. This only
        checks data length (256 bits), but does not try to validate
        the key in any way."""

        current_position, verifying_key = self._unpack_by_int(data, 0)
        verifying_key_length = len(verifying_key) * 8
        verifying_key = self._parse_long(verifying_key)

        if verifying_key < 0:
            raise InvalidKeyError("ed25519 verifying key must be >0.")

        self.bits = verifying_key_length
        if self.bits != 256:
            raise InvalidKeyLengthError("ed25519 keys must be 256 bits (was %s bits)" % self.bits)
        return current_position

    def _process_key(self, data):
        if self.key_type == b"ssh-rsa":
            return self._process_ssh_rsa(data)
        elif self.key_type == b"ssh-dss":
            return self._process_ssh_dss(data)
        elif self.key_type.strip().startswith(b"ecdsa-sha"):
            return self._process_ecdsa_sha(data)
        elif self.key_type == b"ssh-ed25519":
            return self._process_ed25516(data)
        else:
            raise NotImplementedError("Invalid key type: %s" % self.key_type)

    def parse(self, keydata=None):
        """Validates SSH public key.

        Throws exception for invalid keys. Otherwise returns None.

        Populates key_type, bits and bits fields.

        For rsa keys, see field "rsa" for raw public key data.
        For dsa keys, see field "dsa".
        For ecdsa keys, see field "ecdsa"."""
        if keydata is None:
            if self.keydata is None:
                raise ValueError("Key data must be supplied either in constructor or to parse()")
            keydata = self.keydata
        else:
            self.reset()
            self.keydata = keydata

        if keydata.startswith("---- BEGIN SSH2 PUBLIC KEY ----"):
            # SSH2 key format
            key_type = None  # There is no redundant key-type field - skip comparing plain-text and encoded data.
            pubkey_content = "".join([line for line in keydata.split("\n") if ":" not in line and "----" not in line])
        else:
            key_parts = self._split_key(keydata)
            key_type = key_parts[0]
            pubkey_content = key_parts[1]

        self._decoded_key = self.decode_key(pubkey_content)

        # Check key type
        current_position, unpacked_key_type = self._unpack_by_int(self._decoded_key, 0)
        if key_type is not None and key_type != unpacked_key_type.decode():
            raise InvalidTypeError("Keytype mismatch: %s != %s" % (key_type, unpacked_key_type))

        self.key_type = unpacked_key_type

        key_data_length = self._process_key(self._decoded_key[current_position:])
        current_position = current_position + key_data_length

        if current_position != len(self._decoded_key):
            raise MalformedDataError("Leftover data: %s bytes" % (len(self._decoded_key) - current_position))

        if self.disallow_options and self.options:
            raise InvalidOptionsError("Options are disallowed.")