# Path: pyDFTutils/phonon/plotphon.py
# def plot_band_weight(kslist,
#                      ekslist,
#                      wkslist=None,
#                      efermi=0,
#                      yrange=None,
#                      output=None,
#                      style='alpha',
#                      color='blue',
#                      axis=None,
#                      width=2,
#                      xticks=None,
#                      title=None):
#     if axis is None:
#         fig, a = plt.subplots()
#         plt.tight_layout(pad=2.19)
#         plt.axis('tight')
#         plt.gcf().subplots_adjust(left=0.17)
#     else:
#         a = axis
#     if title is not None:
#         a.set_title(title)
# 
#     xmax = max(kslist[0])
#     if yrange is None:
#         yrange = (np.array(ekslist).flatten().min() - 66,
#                   np.array(ekslist).flatten().max() + 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=[2] * len(x),
#                     colors=[
#                         colorConverter.to_rgba(
#                             color, alpha=np.abs(lwidth / (width + 0.001)))
#                         for lwidth in lwidths
#                     ])
# 
#             a.add_collection(lc)
#     plt.ylabel('Frequency (cm$^{-1}$)')
#     if axis is None:
#         for ks, eks in zip(kslist, ekslist):
#             plt.plot(ks, eks, color='gray', linewidth=0.001)
#         a.set_xlim(0, xmax)
#         a.set_ylim(yrange)
#         if xticks is not None:
#             plt.xticks(xticks[1], xticks[0])
#         for x in xticks[1]:
#             plt.axvline(x, color='gray', linewidth=0.5)
#         if efermi is not None:
#             plt.axhline(linestyle='--', color='black')
#     return a


import abipy.abilab as abilab
import numpy as np
import matplotlib.pyplot as plt
from ase.build import bulk
from ase.dft.kpoints import get_special_points, bandpath
from pyDFTutils.unfolding.phonon_unfolder import phonon_unfolder
from pyDFTutils.phonon.plotphon import plot_band_weight
    nqpts = len(qpoints)
    nbranch = 3 * len(numbers)
    evals = np.zeros([nqpts, nbranch])
    evecs = np.zeros([nqpts, nbranch, nbranch], dtype='complex128')

    m = np.sqrt(np.kron(masses,[1,1,1]))
    #positions=np.kron(scaled_positions,[1,1,1])
    freqs=phbst.phfreqs
    displ_carts=phbst.phdispl_cart
    
    for iqpt, qpt in enumerate(qpoints):
        print(iqpt, qpt)
        for ibranch in range(nbranch):
            #phmode = ncfile.get_phmode(qpt, ibranch)
            #print(2)
            evals[iqpt, ibranch] = freqs[iqpt, ibranch]
            #evec=phmode.displ_cart *m
            #phase = [np.exp(-2j*np.pi*np.dot(pos,qpt)) for pos in scaled_positions]
            #phase = np.kron(phase,[1,1,1])
            #evec*=phase
            #evec /= np.linalg.norm(evec)
            evec=displacement_cart_to_evec(displ_carts[iqpt, ibranch,: ], masses, scaled_positions, qpoint=qpt, add_phase=True)
            evecs[iqpt,:,ibranch] = evec
            
    uf = phonon_unfolder(atoms,sc_mat,evecs,qpoints,phase=False, ghost_atoms=ghost_atoms)
    weights = uf.get_weights()
    x=np.arange(nqpts)
    freqs=evals
    #names = ['$\Gamma$', 'X', 'W', '$\Gamma$', 'L']
    #ax=plot_band_weight([list(x)]*freqs.shape[1],freqs.T*33.356,weights[:,:].T*0.98+0.01,xticks=[names,X],axis=ax)

# Path: pyDFTutils/phonon/plotphon.py
# def plot_band_weight(kslist,
#                      ekslist,
#                      wkslist=None,
#                      efermi=0,
#                      yrange=None,
#                      output=None,
#                      style='alpha',
#                      color='blue',
#                      axis=None,
#                      width=2,
#                      xticks=None,
#                      title=None):
#     if axis is None:
#         fig, a = plt.subplots()
#         plt.tight_layout(pad=2.19)
#         plt.axis('tight')
#         plt.gcf().subplots_adjust(left=0.17)
#     else:
#         a = axis
#     if title is not None:
#         a.set_title(title)
# 
#     xmax = max(kslist[0])
#     if yrange is None:
#         yrange = (np.array(ekslist).flatten().min() - 66,
#                   np.array(ekslist).flatten().max() + 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=[2] * len(x),
#                     colors=[
#                         colorConverter.to_rgba(
#                             color, alpha=np.abs(lwidth / (width + 0.001)))
#                         for lwidth in lwidths
#                     ])
# 
#             a.add_collection(lc)
#     plt.ylabel('Frequency (cm$^{-1}$)')
#     if axis is None:
#         for ks, eks in zip(kslist, ekslist):
#             plt.plot(ks, eks, color='gray', linewidth=0.001)
#         a.set_xlim(0, xmax)
#         a.set_ylim(yrange)
#         if xticks is not None:
#             plt.xticks(xticks[1], xticks[0])
#         for x in xticks[1]:
#             plt.axvline(x, color='gray', linewidth=0.5)
#         if efermi is not None:
#             plt.axhline(linestyle='--', color='black')
#     return a


# Path: pyDFTutils/unfolding/unfolding/DDB_unfolder.py
import abipy.abilab as abilab
import numpy as np
import matplotlib.pyplot as plt
from ase.build import bulk
from ase.dft.kpoints import get_special_points, bandpath
from pyDFTutils.unfolding.phonon_unfolder import phonon_unfolder
from pyDFTutils.phonon.plotphon import plot_band_weight

    nqpts = len(qpoints)
    nbranch = 3 * len(numbers)
    evals = np.zeros([nqpts, nbranch])
    evecs = np.zeros([nqpts, nbranch, nbranch], dtype='complex128')

    m = np.sqrt(np.kron(masses,[1,1,1]))
    #positions=np.kron(scaled_positions,[1,1,1])
    freqs=phbst.phfreqs
    displ_carts=phbst.phdispl_cart
    
    for iqpt, qpt in enumerate(qpoints):
        print(iqpt, qpt)
        for ibranch in range(nbranch):
            #phmode = ncfile.get_phmode(qpt, ibranch)
            #print(2)
            evals[iqpt, ibranch] = freqs[iqpt, ibranch]
            #evec=phmode.displ_cart *m
            #phase = [np.exp(-2j*np.pi*np.dot(pos,qpt)) for pos in scaled_positions]
            #phase = np.kron(phase,[1,1,1])
            #evec*=phase
            #evec /= np.linalg.norm(evec)
            evec=displacement_cart_to_evec(displ_carts[iqpt, ibranch,: ], masses, scaled_positions, qpoint=qpt, add_phase=True)
            evecs[iqpt,:,ibranch] = evec
            
    uf = phonon_unfolder(atoms,sc_mat,evecs,qpoints,phase=False, ghost_atoms=ghost_atoms)
    weights = uf.get_weights()
    x=np.arange(nqpts)
    freqs=evals
    #names = ['$\Gamma$', 'X', 'W', '$\Gamma$', 'L']
    #ax=plot_band_weight([list(x)]*freqs.shape[1],freqs.T*33.356,weights[:,:].T*0.98+0.01,xticks=[names,X],axis=ax)

    ax=plot_band_weight([list(x)]*freqs.shape[1],freqs.T*8065.6,weights[:,:].T*0.98+0.01,xticks=[knames, kx],style='alpha')