def from_file(cls, fmt, filename=None,
                  structure_file=None, are_coops=False):
        """
        Creates a CompleteCohp object from an output file of a COHP
        calculation. Valid formats are either LMTO (for the Stuttgart
        LMTO-ASA code) or LOBSTER (for the LOBSTER code).

        Args:
            cohp_file: Name of the COHP output file. Defaults to COPL
                for LMTO and COHPCAR.lobster/COOPCAR.lobster for LOBSTER.

            are_coops: Indicates whether the populations are COOPs or
                COHPs. Defaults to False for COHPs.

            fmt: A string for the code that was used to calculate
                the COHPs so that the output file can be handled
                correctly. Can take the values "LMTO" or "LOBSTER".

            structure_file: Name of the file containing the structure.
                If no file name is given, use CTRL for LMTO and POSCAR
                for LOBSTER.

        Returns:
            A CompleteCohp object.
        """
        fmt = fmt.upper()
        if fmt == "LMTO":
            # LMTO COOPs and orbital-resolved COHP cannot be handled yet.
            are_coops = False
            orb_res_cohp = None
            if structure_file is None:
                structure_file = "CTRL"
            if filename is None:
                filename = "COPL"
            cohp_file = LMTOCopl(filename=filename, to_eV=True)
        elif fmt == "LOBSTER":
            if structure_file is None:
                structure_file = "POSCAR"
            if filename is None:
                filename = "COOPCAR.lobster" if are_coops \
                    else "COHPCAR.lobster"
            warnings.warn(
                "The bond labels are currently consistent with ICOHPLIST.lobster/ICOOPLIST.lobster, not with COHPCAR.lobster/COOPCAR.lobster. Please be aware!")
            cohp_file = Cohpcar(filename=filename, are_coops=are_coops)
            orb_res_cohp = cohp_file.orb_res_cohp
        else:
            raise ValueError("Unknown format %s. Valid formats are LMTO "
                             "and LOBSTER." % fmt)

        structure = Structure.from_file(structure_file)
        efermi = cohp_file.efermi
        cohp_data = cohp_file.cohp_data
        energies = cohp_file.energies

        # Lobster shifts the energies so that the Fermi energy is at zero.
        # Shifting should be done by the plotter object though.

        spins = [Spin.up, Spin.down] if cohp_file.is_spin_polarized \
            else [Spin.up]
        if fmt == "LOBSTER":
            energies += efermi

        if orb_res_cohp is not None:
            # If no total COHPs are present, calculate the total
            # COHPs from the single-orbital populations. Total COHPs
            # may not be present when the cohpgenerator keyword is used
            # in LOBSTER versions 2.2.0 and earlier.
            # TODO: Test this more extensively
            for label in orb_res_cohp:
                if cohp_file.cohp_data[label]["COHP"] is None:
                    # print(label)
                    cohp_data[label]["COHP"] = {
                        sp: np.sum([orb_res_cohp[label][orbs]["COHP"][sp] for orbs in orb_res_cohp[label]], axis=0) for
                        sp
                        in spins}
                if cohp_file.cohp_data[label]["ICOHP"] is None:
                    cohp_data[label]["ICOHP"] = \
                        {sp: np.sum([orb_res_cohp[label][orbs]["ICOHP"][sp]
                                     for orbs in orb_res_cohp[label]],
                                    axis=0) for sp in spins}

        if fmt == "LMTO":
            # Calculate the average COHP for the LMTO file to be
            # consistent with LOBSTER output.
            avg_data = {"COHP": {}, "ICOHP": {}}
            for i in avg_data:
                for spin in spins:
                    rows = np.array([cohp_data[label][i][spin]
                                     for label in cohp_data])
                    avg = np.average(rows, axis=0)
                    # LMTO COHPs have 5 significant figures
                    avg_data[i].update({spin:
                                            np.array([round_to_sigfigs(a, 5)
                                                      for a in avg],
                                                     dtype=float)})
            avg_cohp = Cohp(efermi, energies,
                            avg_data["COHP"],
                            icohp=avg_data["ICOHP"])
        else:
            avg_cohp = Cohp(efermi, energies,
                            cohp_data["average"]["COHP"],
                            icohp=cohp_data["average"]["COHP"],
                            are_coops=are_coops)
            del cohp_data["average"]

        cohp_dict = {label: Cohp(efermi, energies,
                                 cohp_data[label]["COHP"],
                                 icohp=cohp_data[label]["ICOHP"],
                                 are_coops=are_coops)
                     for label in cohp_data}

        bond_dict = {label: {"length": cohp_data[label]["length"],
                             "sites": [structure.sites[site]
                                       for site in cohp_data[label]["sites"]]}
                     for label in cohp_data}

        return CompleteCohp(structure, avg_cohp, cohp_dict, bonds=bond_dict,
                            are_coops=are_coops, orb_res_cohp=orb_res_cohp)
def from_file ( cls , fmt , filename = None , structure_file = None , are_coops = False ) : fmt = fmt . upper ( ) if fmt == "LMTO" : are_coops = False orb_res_cohp = None if structure_file is None : structure_file = "CTRL" if filename is None : filename = "COPL" cohp_file = LMTOCopl ( filename = filename , to_eV = True ) elif fmt == "LOBSTER" : if structure_file is None : structure_file = "POSCAR" if filename is None : filename = "COOPCAR.lobster" if are_coops else "COHPCAR.lobster" warnings . warn ( "The bond labels are currently consistent with ICOHPLIST.lobster/ICOOPLIST.lobster, not with COHPCAR.lobster/COOPCAR.lobster. Please be aware!" ) cohp_file = Cohpcar ( filename = filename , are_coops = are_coops ) orb_res_cohp = cohp_file . orb_res_cohp else : raise ValueError ( "Unknown format %s. Valid formats are LMTO " "and LOBSTER." % fmt ) structure = Structure . from_file ( structure_file ) efermi = cohp_file . efermi cohp_data = cohp_file . cohp_data energies = cohp_file . energies spins = [ Spin . up , Spin . down ] if cohp_file . is_spin_polarized else [ Spin . up ] if fmt == "LOBSTER" : energies += efermi if orb_res_cohp is not None : for label in orb_res_cohp : if cohp_file . cohp_data [ label ] [ "COHP" ] is None : cohp_data [ label ] [ "COHP" ] = { sp : np . sum ( [ orb_res_cohp [ label ] [ orbs ] [ "COHP" ] [ sp ] for orbs in orb_res_cohp [ label ] ] , axis = 0 ) for sp in spins } if cohp_file . cohp_data [ label ] [ "ICOHP" ] is None : cohp_data [ label ] [ "ICOHP" ] = { sp : np . sum ( [ orb_res_cohp [ label ] [ orbs ] [ "ICOHP" ] [ sp ] for orbs in orb_res_cohp [ label ] ] , axis = 0 ) for sp in spins } if fmt == "LMTO" : avg_data = { "COHP" : { } , "ICOHP" : { } } for i in avg_data : for spin in spins : rows = np . array ( [ cohp_data [ label ] [ i ] [ spin ] for label in cohp_data ] ) avg = np . average ( rows , axis = 0 ) avg_data [ i ] . update ( { spin : np . array ( [ round_to_sigfigs ( a , 5 ) for a in avg ] , dtype = float ) } ) avg_cohp = Cohp ( efermi , energies , avg_data [ "COHP" ] , icohp = avg_data [ "ICOHP" ] ) else : avg_cohp = Cohp ( efermi , energies , cohp_data [ "average" ] [ "COHP" ] , icohp = cohp_data [ "average" ] [ "COHP" ] , are_coops = are_coops ) del cohp_data [ "average" ] cohp_dict = { label : Cohp ( efermi , energies , cohp_data [ label ] [ "COHP" ] , icohp = cohp_data [ label ] [ "ICOHP" ] , are_coops = are_coops ) for label in cohp_data } bond_dict = { label : { "length" : cohp_data [ label ] [ "length" ] , "sites" : [ structure . sites [ site ] for site in cohp_data [ label ] [ "sites" ] ] } for label in cohp_data } return CompleteCohp ( structure , avg_cohp , cohp_dict , bonds = bond_dict , are_coops = are_coops , orb_res_cohp = orb_res_cohp )
Creates a CompleteCohp object from an output file of a COHP
        calculation. Valid formats are either LMTO (for the Stuttgart
        LMTO-ASA code) or LOBSTER (for the LOBSTER code).

        Args:
            cohp_file: Name of the COHP output file. Defaults to COPL
                for LMTO and COHPCAR.lobster/COOPCAR.lobster for LOBSTER.

            are_coops: Indicates whether the populations are COOPs or
                COHPs. Defaults to False for COHPs.

            fmt: A string for the code that was used to calculate
                the COHPs so that the output file can be handled
                correctly. Can take the values "LMTO" or "LOBSTER".

            structure_file: Name of the file containing the structure.
                If no file name is given, use CTRL for LMTO and POSCAR
                for LOBSTER.

        Returns:
            A CompleteCohp object.