# Path: pyDFTutils/wannier90/band_plot.py
# def plot_band_weight(kslist,ekslist,wkslist=None,efermi=None,yrange=None,output=None,style='alpha',color='blue',axis=None,width=10,xticks=None):
#     if axis is None:
#         fig,a = plt.subplots()
#     else:
#         a=axis
# 
#     xmax=max(kslist[0])
#     if yrange is None:
#         yrange=(np.array(ekslist).flatten().min()-0.66,np.array(ekslist).flatten().max()+0.66)
# 
#     if wkslist is not None:
#         for i in range(len(kslist)):
#             x=kslist[i]
#             y=ekslist[i]
#             lwidths=np.array(wkslist[i])*width
#             #lwidths=np.ones(len(x))
#             points = np.array([x, y]).T.reshape(-1, 1, 2)
#             segments = np.concatenate([points[:-1], points[1:]], axis=1)
#             if style=='width':
#                 lc = LineCollection(segments, linewidths=lwidths,colors=color)
#             elif style=='alpha':
#                 lc = LineCollection(segments,linewidths=[4]*len(x), colors=[colorConverter.to_rgba(color,alpha=lwidth/(width+0.001)) for lwidth in lwidths])
# 
#             a.add_collection(lc)
#     if axis is None:
#         for ks , eks in zip(kslist,ekslist):
#             plt.plot(ks,eks,color='gray')
#         a.set_xlim(0,xmax)
#         a.set_ylim(yrange)
#         if xticks is not None:
#             plt.xticks(xticks[1],xticks[0])
#         if efermi is not None:
#             plt.axhline(linestyle='--',color='black')
#     return a


from pythtb import tb_model,w90
from ase.calculators.interface import Calculator,DFTCalculator
from ase.dft.dos import DOS
from ase.dft.kpoints import monkhorst_pack
from occupations import Occupations
from pyDFTutils.wannier90.band_plot import plot_band_weight
import numpy as np
import matplotlib.pyplot as plt

    def get_occupations(self,nel,width=0.2,refresh=False):
        """
        calculate occupations of each eigenvalue.
        the the shape of the occupation is the same as self._eigenvals.
        [eig_k1,eigk2,...], each eig_k is a column with the length=nbands.

        if nspin=2 and fix_spin, there are two fermi energies. NOTE: this conflicts with the DOS caluculation. FIXME.

        :param nel: number of electrons. if fix_spin, the nel is a tuple of (nel_up,nel_dn)

        :Returns:
          self._occupations (np.ndarray) index:[band,kpt,orb,spin] if nspin==2 else [band,kpt,orb] same as eigenvec
        """
        self._nel=nel
        self.get_eigenvalues(refresh=refresh)
        #print self._kweights
        #print self._eigenvals
        if self._nspin ==1 or not self.fix_spin:
            occ=Occupations(nel,width,self._kweights,nspin=self._nspin)
            self._occupations=occ.occupy(self._eigenvals)
            self._efermi=occ.get_mu()
        elif self._nspin==2 and self.fix_spin:
            raise NotImplementedError("current implement on fix_spin is not correct.")
            u"""FIXME: 这根本就不对，eig无法被直接区分为eig_up,eig_dn，不能这样处理"""
            nel_up,nel_dn=nel
            eig_up=self.eigenvals[::2]
            eig_dn=self.eigenvals[1::2]

            occ_up=Occupations(nel_up,width,self._kweights,nspin=1)

# Path: pyDFTutils/wannier90/band_plot.py
# def plot_band_weight(kslist,ekslist,wkslist=None,efermi=None,yrange=None,output=None,style='alpha',color='blue',axis=None,width=10,xticks=None):
#     if axis is None:
#         fig,a = plt.subplots()
#     else:
#         a=axis
# 
#     xmax=max(kslist[0])
#     if yrange is None:
#         yrange=(np.array(ekslist).flatten().min()-0.66,np.array(ekslist).flatten().max()+0.66)
# 
#     if wkslist is not None:
#         for i in range(len(kslist)):
#             x=kslist[i]
#             y=ekslist[i]
#             lwidths=np.array(wkslist[i])*width
#             #lwidths=np.ones(len(x))
#             points = np.array([x, y]).T.reshape(-1, 1, 2)
#             segments = np.concatenate([points[:-1], points[1:]], axis=1)
#             if style=='width':
#                 lc = LineCollection(segments, linewidths=lwidths,colors=color)
#             elif style=='alpha':
#                 lc = LineCollection(segments,linewidths=[4]*len(x), colors=[colorConverter.to_rgba(color,alpha=lwidth/(width+0.001)) for lwidth in lwidths])
# 
#             a.add_collection(lc)
#     if axis is None:
#         for ks , eks in zip(kslist,ekslist):
#             plt.plot(ks,eks,color='gray')
#         a.set_xlim(0,xmax)
#         a.set_ylim(yrange)
#         if xticks is not None:
#             plt.xticks(xticks[1],xticks[0])
#         if efermi is not None:
#             plt.axhline(linestyle='--',color='black')
#     return a


# Path: pyDFTutils/tightbinding/mypythTB.py
from pythtb import tb_model,w90
from ase.calculators.interface import Calculator,DFTCalculator
from ase.dft.dos import DOS
from ase.dft.kpoints import monkhorst_pack
from occupations import Occupations
from pyDFTutils.wannier90.band_plot import plot_band_weight
import numpy as np
import matplotlib.pyplot as plt


    def get_occupations(self,nel,width=0.2,refresh=False):
        """
        calculate occupations of each eigenvalue.
        the the shape of the occupation is the same as self._eigenvals.
        [eig_k1,eigk2,...], each eig_k is a column with the length=nbands.

        if nspin=2 and fix_spin, there are two fermi energies. NOTE: this conflicts with the DOS caluculation. FIXME.

        :param nel: number of electrons. if fix_spin, the nel is a tuple of (nel_up,nel_dn)

        :Returns:
          self._occupations (np.ndarray) index:[band,kpt,orb,spin] if nspin==2 else [band,kpt,orb] same as eigenvec
        """
        self._nel=nel
        self.get_eigenvalues(refresh=refresh)
        #print self._kweights
        #print self._eigenvals
        if self._nspin ==1 or not self.fix_spin:
            occ=Occupations(nel,width,self._kweights,nspin=self._nspin)
            self._occupations=occ.occupy(self._eigenvals)
            self._efermi=occ.get_mu()
        elif self._nspin==2 and self.fix_spin:
            raise NotImplementedError("current implement on fix_spin is not correct.")
            u"""FIXME: 这根本就不对，eig无法被直接区分为eig_up,eig_dn，不能这样处理"""
            nel_up,nel_dn=nel
            eig_up=self.eigenvals[::2]
            eig_dn=self.eigenvals[1::2]

            occ_up=Occupations(nel_up,width,self._kweights,nspin=1)

            occupations_up=occ_up.occupy(eig_up)