def to_csv(self, filename: str, latexify_names: bool = False): """ Exports PDEntries to a csv Args: filename: Filename to write to. entries: PDEntries to export. latexify_names: Format entry names to be LaTex compatible, e.g., Li_{2}O """ elements = set() for entry in self.entries: elements.update(entry.composition.elements) elements = sorted(list(elements), key=lambda a: a.X) writer = csv.writer(open(filename, "w"), delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) writer.writerow(["Name"] + elements + ["Energy"]) for entry in self.entries: row = [ entry.name if not latexify_names else re.sub( r"([0-9]+)", r"_{\1}", entry.name) ] row.extend([entry.composition[el] for el in elements]) row.append(entry.energy) writer.writerow(row)
def from_csv(cls, filename: str): """ Imports PDEntries from a csv. Args: filename: Filename to import from. Returns: List of Elements, List of PDEntries """ with open(filename, "r", encoding="utf-8") as f: reader = csv.reader(f, delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) entries = list() header_read = False elements = None for row in reader: if not header_read: elements = row[1:(len(row) - 1)] header_read = True else: name = row[0] energy = float(row[-1]) comp = dict() for ind in range(1, len(row) - 1): if float(row[ind]) > 0: comp[Element(elements[ind - 1])] = float(row[ind]) entries.append(PDEntry(Composition(comp), energy, name)) return cls(entries)
def to_csv(filename, entries, latexify_names=False): """ Exports Pourbaix entries to a csv Args: filename: Filename to write to. entries: Entries to export. latexify_names: Format entry names to be LaTex compatible, e.g., Li_{2}O """ elements = set() # TODO: oh god please fix this next line list(map(elements.update, [entry.entry.composition.elements for entry in entries])) elements = sorted(list(elements), key=lambda a: a.X) with open(filename, "w") as f: writer = csv.writer(f, delimiter=unicode2str(","), quotechar=unicode2str('"'), quoting=csv.QUOTE_MINIMAL) writer.writerow(["Name"] + elements + ["Energy"] + ["Entry Type"] + ["Charge"] + ["Concentration"]) for entry in entries: row = [entry.name if not latexify_names else re.sub(r"([0-9]+)", r"_{\1}", entry.name)] if entry.phase_type == "Solid": reduction_fac = entry.entry.composition.get_reduced_composition_and_factor()[1] else: reduction_fac = 1.0 row.extend([entry.entry.composition[el] / reduction_fac for el in elements]) if entry.phase_type == "Solid": reduction_fac = 1.0 row.append(entry.g0 / reduction_fac) row.append(entry.phase_type) row.append(entry.charge / reduction_fac) row.append(entry.conc) writer.writerow(row)
def from_csv(filename): """ Imports PDEntries from a csv. Args: filename: Filename to import from. Returns: List of Elements, List of PDEntries """ with open(filename, "r", encoding="utf-8") as f: reader = csv.reader(f, delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) entries = list() header_read = False for row in reader: if not header_read: elements = row[1:(len(row) - 1)] header_read = True else: name = row[0] energy = float(row[-1]) comp = dict() for ind in range(1, len(row) - 1): if float(row[ind]) > 0: comp[Element(elements[ind - 1])] = float(row[ind]) entries.append(PDEntry(Composition(comp), energy, name)) elements = [Element(el) for el in elements] return elements, entries
def to_csv(filename, entries, latexify_names=False): """ Exports PDEntries to a csv Args: filename: Filename to write to. entries: PDEntries to export. latexify_names: Format entry names to be LaTex compatible, e.g., Li_{2}O """ import csv elements = set() for entry in entries: elements.update(entry.composition.elements) elements = sorted(list(elements), key=lambda a: a.X) writer = csv.writer(open(filename, "wb"), delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) writer.writerow(["Name"] + elements + ["Energy"]) for entry in entries: row = [entry.name if not latexify_names else re.sub(r"([0-9]+)", r"_{\1}", entry.name)] row.extend([entry.composition[el] for el in elements]) row.append(entry.energy) writer.writerow(row)
def to_csv(filename, entries, latexify_names=False): """ Exports Pourbaix entries to a csv Args: filename: Filename to write to. entries: Entries to export. latexify_names: Format entry names to be LaTex compatible, e.g., Li_{2}O """ elements = set() #TODO: oh god please fix this next line list( map(elements.update, [entry.entry.composition.elements for entry in entries])) elements = sorted(list(elements), key=lambda a: a.X) with open(filename, "w") as f: writer = csv.writer(f, delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) writer.writerow(["Name"] + elements + ["Energy"] + ["Entry Type"] + ["Charge"] + ["Concentration"]) for entry in entries: row = [ entry.name if not latexify_names else re.sub( r"([0-9]+)", r"_{\1}", entry.name) ] if entry.phase_type == "Solid": reduction_fac = entry.entry.composition.\ get_reduced_composition_and_factor()[1] else: reduction_fac = 1.0 row.extend([ entry.entry.composition[el] / reduction_fac for el in elements ]) if entry.phase_type == "Solid": reduction_fac = 1.0 row.append(entry.g0 / reduction_fac) row.append(entry.phase_type) row.append(entry.charge / reduction_fac) row.append(entry.conc) writer.writerow(row)
def __init__(self, filename): # read in data from file self._chemsys_entries = defaultdict(list) filename = os.path.join(os.path.dirname(__file__), filename) reader = csv.reader(open(filename, "rt"), quotechar=unicode2str("|")) for row in reader: comp = Composition(row[0]) cost_per_mol = float(row[1]) * comp.weight.to("kg") * AVOGADROS_CONST pde = CostEntry(comp.formula, cost_per_mol, row[2], row[3]) chemsys = "-".join(sorted([el.symbol for el in pde.composition.elements])) self._chemsys_entries[chemsys].append(pde)
def from_csv(filename): """ Imports PourbaixEntries from a csv. Args: filename: Filename to import from. Returns: List of Entries """ with open(filename, "r") as f: reader = csv.reader(f, delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) entries = list() header_read = False for row in reader: if not header_read: elements = row[1:(len(row) - 4)] header_read = True else: name = row[0] energy = float(row[-4]) conc = float(row[-1]) comp = dict() for ind in range(1, len(row) - 4): if float(row[ind]) > 0: comp[Element(elements[ind - 1])] = float(row[ind]) phase_type = row[-3] if phase_type == "Ion": PoE = PourbaixEntry( IonEntry(Ion.from_formula(name), energy)) PoE.conc = conc PoE.name = name entries.append(PoE) else: entries.append( PourbaixEntry(PDEntry(Composition(comp), energy))) elements = [Element(el) for el in elements] return elements, entries
def __init__(self, filename): # read in data from file self._chemsys_entries = defaultdict(list) filename = os.path.join(os.path.dirname(__file__), filename) reader = csv.reader(open(filename, "rt"), quotechar=unicode2str("|")) for row in reader: comp = Composition(row[0]) cost_per_mol = float(row[1]) * comp.weight.to("kg") * const.N_A pde = CostEntry(comp.formula, cost_per_mol, row[2], row[3]) chemsys = "-".join( sorted([el.symbol for el in pde.composition.elements])) self._chemsys_entries[chemsys].append(pde)
def from_csv(filename): """ Imports PourbaixEntries from a csv. Args: filename: Filename to import from. Returns: List of Entries """ with open(filename, "r") as f: reader = csv.reader(f, delimiter=unicode2str(","), quotechar=unicode2str("\""), quoting=csv.QUOTE_MINIMAL) entries = list() header_read = False for row in reader: if not header_read: elements = row[1:(len(row) - 4)] header_read = True else: name = row[0] energy = float(row[-4]) conc = float(row[-1]) comp = dict() for ind in range(1, len(row) - 4): if float(row[ind]) > 0: comp[Element(elements[ind - 1])] = float(row[ind]) phase_type = row[-3] if phase_type == "Ion": PoE = PourbaixEntry(IonEntry(Ion.from_formula(name), energy)) PoE.conc = conc PoE.name = name entries.append(PoE) else: entries.append(PourbaixEntry(PDEntry(Composition(comp), energy))) elements = [Element(el) for el in elements] return elements, entries
def test_unicode2str(self): if sys.version_info.major < 3: self.assertEqual(type(unicode2str("a")), str) else: self.assertEqual(type(unicode2str("a")), str)