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

'''
Created on Jul 22, 2012
'''

from __future__ import division

__author__ = "<NAME>"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "Jul 22, 2012"

import unittest
import os

from nose.exc import SkipTest

from pymatgen.command_line.enumlib_caller import EnumlibAdaptor
from pymatgen import __file__, Element, Structure
from pymatgen.io.cifio import CifParser
from pymatgen.transformations.standard_transformations import SubstitutionTransformation
from pymatgen.util.io_utils import which


if which('multienum.x') and which('makestr.x'):
    enumlib_present = True
else:
    enumlib_present = False


class EnumlibAdaptorTest(unittest.TestCase):

    def test_init(self):
        if not enumlib_present:
            raise SkipTest("enumlib not present. Skipping...")
        test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'test_files')

        parser = CifParser(os.path.join(test_dir, "LiFePO4.cif"))
        struct = parser.get_structures(False)[0]
        subtrans = SubstitutionTransformation({'Li':{'Li':0.5}})
        adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 2)
        adaptor.run()
        structures = adaptor.structures
        self.assertEqual(len(structures), 52)
        for s in structures:
            self.assertAlmostEqual(s.composition.get_atomic_fraction(Element("Li")), 0.5 / 6.5)

        subtrans = SubstitutionTransformation({'Li':{'Li':0.25}})
        adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 1)
        adaptor.run()
        structures = adaptor.structures
        self.assertEqual(len(structures), 1)
        for s in structures:
            self.assertAlmostEqual(s.composition.get_atomic_fraction(Element("Li")), 0.25 / 6.25)

        #Make sure it works for completely disordered structures.
        struct = Structure([[10, 0, 0], [0, 10, 0], [0, 0, 10]], [{'Fe':0.5}],
                           [[0, 0, 0]])
        adaptor = EnumlibAdaptor(struct, 1, 2)
        adaptor.run()
        self.assertEqual(len(adaptor.structures), 3)


if __name__ == '__main__':
    unittest.main()
<commit_msg>Add more unittests for enumlib.<commit_after>#!/usr/bin/env python

'''
Created on Jul 22, 2012
'''

from __future__ import division

__author__ = "<NAME>"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "Jul 22, 2012"

import unittest
import os

from nose.exc import SkipTest

from pymatgen.command_line.enumlib_caller import EnumlibAdaptor
from pymatgen import __file__, Element, Structure
from pymatgen.io.cifio import CifParser
from pymatgen.transformations.standard_transformations import SubstitutionTransformation
from pymatgen.util.io_utils import which
from pymatgen.transformations.site_transformations import RemoveSitesTransformation


if which('multienum.x') and which('makestr.x'):
    enumlib_present = True
else:
    enumlib_present = False


class EnumlibAdaptorTest(unittest.TestCase):

    def test_init(self):
        if not enumlib_present:
            raise SkipTest("enumlib not present. Skipping...")
        test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'test_files')

        parser = CifParser(os.path.join(test_dir, "LiFePO4.cif"))
        struct = parser.get_structures(False)[0]
        subtrans = SubstitutionTransformation({'Li':{'Li':0.5}})
        adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 2)
        adaptor.run()
        structures = adaptor.structures
        self.assertEqual(len(structures), 52)
        for s in structures:
            self.assertAlmostEqual(s.composition.get_atomic_fraction(Element("Li")), 0.5 / 6.5)

        subtrans = SubstitutionTransformation({'Li':{'Li':0.25}})
        adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 1)
        adaptor.run()
        structures = adaptor.structures
        self.assertEqual(len(structures), 1)
        for s in structures:
            self.assertAlmostEqual(s.composition.get_atomic_fraction(Element("Li")), 0.25 / 6.25)

        #Make sure it works for completely disordered structures.
        struct = Structure([[10, 0, 0], [0, 10, 0], [0, 0, 10]], [{'Fe':0.5}],
                           [[0, 0, 0]])
        adaptor = EnumlibAdaptor(struct, 1, 2)
        adaptor.run()
        self.assertEqual(len(adaptor.structures), 3)

        #Make sure it works properly when symmetry is broken by ordered sites.
        parser = CifParser(os.path.join(test_dir, "LiFePO4.cif"))
        struct = parser.get_structures(False)[0]
        subtrans = SubstitutionTransformation({'Li':{'Li':0.25}})
        s = subtrans.apply_transformation(struct)
        #REmove some ordered sites to break symmetry.
        removetrans = RemoveSitesTransformation([4, 7])
        s = removetrans.apply_transformation(s)
        adaptor = EnumlibAdaptor(s, 1, 1)
        adaptor.run()
        structures = adaptor.structures
        self.assertEqual(len(structures), 2)

if __name__ == '__main__':
    unittest.main()

{'fraction_non_alphanumeric': 0.120686331212647, 'fraction_numerical': 0.02583381530749952, 'mean_word_length': 3.3966101694915256, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948962', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1605, 'n_words': 364}