<commit_before>#!/usr/bin/env python

'''
Created on Nov 10, 2012

@author: shyue
'''

from __future__ import division

__author__ = "<NAME>"
__copyright__ = "Copyright 2011, The Materials Project"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"
__date__ = "Nov 10, 2012"

import unittest


from pymatgen.core.periodic_table import Element
from pymatgen.core.composition import Composition
import random


class CompositionTest(unittest.TestCase):

    def setUp(self):
        self.comp = list()
        self.comp.append(Composition.from_formula("Li3Fe2(PO4)3"))
        self.comp.append(Composition.from_formula("Li3Fe(PO4)O"))
        self.comp.append(Composition.from_formula("LiMn2O4"))
        self.comp.append(Composition.from_formula("Li4O4"))
        self.comp.append(Composition.from_formula("Li3Fe2Mo3O12"))
        self.comp.append(Composition.from_formula("Li3Fe2((PO4)3(CO3)5)2"))
        self.comp.append(Composition.from_formula("Li1.5Si0.5"))
        self.comp.append(Composition.from_formula("ZnOH"))

        self.indeterminate_comp = []
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("Co1",
                                                                       True)
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("Co1",
                                                                       False)
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("co2o3")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("ncalu")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("calun")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula(
                "liCoo2n (pO4)2")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula(
                "(co)2 (PO)4")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("Fee3"))

    def test_init_(self):
        self.assertRaises(ValueError, Composition, {Element("H"):-0.1})
        f = {'Fe': 4, 'Li': 4, 'O': 16, 'P': 4}
        self.assertEqual("Li4 Fe4 P4 O16", Composition(f).formula)
        f = {None: 4, 'Li': 4, 'O': 16, 'P': 4}
        self.assertRaises(ValueError, Composition, f)
        f = {1: 2, 8: 1}
        self.assertEqual("H2 O1", Composition(f).formula)
        self.assertEqual("Na2 O1", Composition(Na=2, O=1).formula)

    def test_formula(self):
        correct_formulas = ['Li3 Fe2 P3 O12', 'Li3 Fe1 P1 O5', 'Li1 Mn2 O4',
                            'Li4 O4', 'Li3 Fe2 Mo3 O12', 'Li3 Fe2 P6 C10 O54',
                            'Li1.5 Si0.5', 'Zn1 H1 O1']
        all_formulas = [c.formula for c in self.comp]
        self.assertEqual(all_formulas, correct_formulas)
        self.assertRaises(ValueError, Composition.from_formula, "(co2)(po4)2")

    def test_mixed_valence(self):
        comp = Composition({"Fe2+": 2, "Fe3+": 4, "Li+": 8})
        self.assertEqual(comp.reduced_formula, "Li4Fe3")
        self.assertEqual(comp.alphabetical_formula, "Fe6 Li8")
        self.assertEqual(comp.formula, "Li8 Fe6")

    def test_indeterminate_formula(self):
        correct_formulas = []
        correct_formulas.append(["Co1"])
        correct_formulas.append(["Co1", "C1 O1"])
        correct_formulas.append(["Co2 O3", "C1 O5"])
        correct_formulas.append(["N1 Ca1 Lu1", "U1 Al1 C1 N1"])
        correct_formulas.append(["N1 Ca1 Lu1", "U1 Al1 C1 N1"])
        correct_formulas.append(["Li1 Co1 P2 N1 O10", "Li1 P2 C1 N1 O11",
                                 "Li1 Co1 Po8 N1 O2", "Li1 Po8 C1 N1 O3"])
        correct_formulas.append(["Co2 P4 O4", "P4 C2 O6", "Co2 Po4",
                                 "Po4 C2 O2"])
        correct_formulas.append([])
        for i, c in enumerate(correct_formulas):
            self.assertEqual([Composition.from_formula(comp) for comp in c],
                             self.indeterminate_comp[i])

    def test_alphabetical_formula(self):
        correct_formulas = ['Fe2 Li3 O12 P3', 'Fe1 Li3 O5 P1', 'Li1 Mn2 O4',
                            'Li4 O4', 'Fe2 Li3 Mo3 O12', 'C10 Fe2 Li3 O54 P6',
                            'Li1.5 Si0.5', 'H1 O1 Zn1']
        all_formulas = [c.alphabetical_formula for c in self.comp]
        self.assertEqual(all_formulas, correct_formulas)

    def test_reduced_composition(self):
        correct_reduced_formulas = ['Li3Fe2(PO4)3', 'Li3FePO5', 'LiMn2O4',
                                    'Li2O2', 'Li3Fe2(MoO4)3',
                                    'Li3Fe2P6(C5O27)2', 'Li1.5Si0.5', 'ZnHO']
        for i in xrange(len(self.comp)):
            self.assertEqual(self.comp[i]
                             .get_reduced_composition_and_factor()[0],
                             Composition
                             .from_formula(correct_reduced_formulas[i]))

    def test_reduced_formula(self):
        correct_reduced_formulas = ['Li3Fe2(PO4)3', 'Li3FePO5', 'LiMn2O4',
                                    'Li2O2', 'Li3Fe2(MoO4)3',<commit_msg>Fix unittest.<commit_after>#!/usr/bin/env python

'''
Created on Nov 10, 2012

@author: shyue
'''

from __future__ import division

__author__ = "<NAME>"
__copyright__ = "Copyright 2011, The Materials Project"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"
__date__ = "Nov 10, 2012"

import unittest


from pymatgen.core.periodic_table import Element
from pymatgen.core.composition import Composition
import random


class CompositionTest(unittest.TestCase):

    def setUp(self):
        self.comp = list()
        self.comp.append(Composition.from_formula("Li3Fe2(PO4)3"))
        self.comp.append(Composition.from_formula("Li3Fe(PO4)O"))
        self.comp.append(Composition.from_formula("LiMn2O4"))
        self.comp.append(Composition.from_formula("Li4O4"))
        self.comp.append(Composition.from_formula("Li3Fe2Mo3O12"))
        self.comp.append(Composition.from_formula("Li3Fe2((PO4)3(CO3)5)2"))
        self.comp.append(Composition.from_formula("Li1.5Si0.5"))
        self.comp.append(Composition.from_formula("ZnOH"))

        self.indeterminate_comp = []
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("Co1",
                                                                       True)
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("Co1",
                                                                       False)
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("co2o3")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("ncalu")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("calun")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula(
                "liCoo2n (pO4)2")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula(
                "(co)2 (PO)4")
        )
        self.indeterminate_comp.append(
            Composition.ranked_compositions_from_indeterminate_formula("Fee3"))

    def test_init_(self):
        self.assertRaises(ValueError, Composition, {Element("H"):-0.1})
        f = {'Fe': 4, 'Li': 4, 'O': 16, 'P': 4}
        self.assertEqual("Li4 Fe4 P4 O16", Composition(f).formula)
        f = {None: 4, 'Li': 4, 'O': 16, 'P': 4}
        self.assertRaises(ValueError, Composition, f)
        f = {1: 2, 8: 1}
        self.assertEqual("H2 O1", Composition(f).formula)
        self.assertEqual("Na2 O1", Composition(Na=2, O=1).formula)

    def test_formula(self):
        correct_formulas = ['Li3 Fe2 P3 O12', 'Li3 Fe1 P1 O5', 'Li1 Mn2 O4',
                            'Li4 O4', 'Li3 Fe2 Mo3 O12', 'Li3 Fe2 P6 C10 O54',
                            'Li1.5 Si0.5', 'Zn1 H1 O1']
        all_formulas = [c.formula for c in self.comp]
        self.assertEqual(all_formulas, correct_formulas)
        self.assertRaises(ValueError, Composition.from_formula, "(co2)(po4)2")

    def test_mixed_valence(self):
        comp = Composition({"Fe2+": 2, "Fe3+": 4, "Li+": 8})
        self.assertEqual(comp.reduced_formula, "Li4Fe3")
        self.assertEqual(comp.alphabetical_formula, "Fe6 Li8")
        self.assertEqual(comp.formula, "Li8 Fe6")

    def test_indeterminate_formula(self):
        correct_formulas = []
        correct_formulas.append(["Co1"])
        correct_formulas.append(["Co1", "C1 O1"])
        correct_formulas.append(["Co2 O3", "C1 O5"])
        correct_formulas.append(["N1 Ca1 Lu1", "U1 Al1 C1 N1"])
        correct_formulas.append(["N1 Ca1 Lu1", "U1 Al1 C1 N1"])
        correct_formulas.append(["Li1 Co1 P2 N1 O10", "Li1 P2 C1 N1 O11",
                                 "Li1 Co1 Po8 N1 O2", "Li1 Po8 C1 N1 O3"])
        correct_formulas.append(["Co2 P4 O4", "Co2 Po4", "P4 C2 O6",
                                 "Po4 C2 O2"])
        correct_formulas.append([])
        for i, c in enumerate(correct_formulas):
            self.assertEqual([Composition.from_formula(comp) for comp in c],
                             self.indeterminate_comp[i])

    def test_alphabetical_formula(self):
        correct_formulas = ['Fe2 Li3 O12 P3', 'Fe1 Li3 O5 P1', 'Li1 Mn2 O4',
                            'Li4 O4', 'Fe2 Li3 Mo3 O12', 'C10 Fe2 Li3 O54 P6',
                            'Li1.5 Si0.5', 'H1 O1 Zn1']
        all_formulas = [c.alphabetical_formula for c in self.comp]
        self.assertEqual(all_formulas, correct_formulas)

    def test_reduced_composition(self):
        correct_reduced_formulas = ['Li3Fe2(PO4)3', 'Li3FePO5', 'LiMn2O4',
                                    'Li2O2', 'Li3Fe2(MoO4)3',
                                    'Li3Fe2P6(C5O27)2', 'Li1.5Si0.5', 'ZnHO']
        for i in xrange(len(self.comp)):
            self.assertEqual(self.comp[i]
                             .get_reduced_composition_and_factor()[0],
                             Composition
                             .from_formula(correct_reduced_formulas[i]))

    def test_reduced_formula(self):
        correct_reduced_formulas = ['Li3Fe2(PO4)3', 'Li3FePO5', 'LiMn2O4',
                                    'Li2O2', 'Li3Fe2(MoO4)3',
{'fraction_non_alphanumeric': 0.11964153732446416, 'fraction_numerical': 0.04730229120473023, 'mean_word_length': 2.267431331119831, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5202998', 'n_tokens_mistral': 3847, 'n_tokens_neox': 3552, 'n_words': 632}