Пример #1
0
 def setUp(self):
     index = 123
     site = Mock(spec=Site)
     label = 'foo'
     site.frac_coords = np.array([1.0, 2.0, 3.0])
     site.coords = np.array([10.0, 11.0, 12.0])
     site.lattice = Mock(spec=Lattice)
     self.site = site
     self.atom = Atom(index=index, site=site, label=label)
Пример #2
0
 def test_distance(self):
     index = 456
     site = Mock(spec=Site)
     label = 'foo'
     other_atom = Atom(index=index, site=site, label=label)
     self.atom.site.distance = Mock(return_value=2.3)
     self.assertEqual(self.atom.distance(other_atom), 2.3)
Пример #3
0
    def __init__(self,
                 structure: Structure,
                 recipes: List[PolyhedraRecipe]) -> None:
        """
        A Configuration object describes a single atomic geometry.

        Args:
            structure (pymatgen.Structure): A pymatgen Structure object for this configuration.
            recipes (list(PolyhedraRecipe): A list of PolyhedraRecipe objects used to construct
                polyhedra for this configuration.

        Attributes:
            atoms (list(Atom)): A list of atoms that make up this configuration.
            polyhedra (list(CoordinationPolyhedron)): A list of polyhedra, generated using
                the PolyhedraRecipe definitions passed in as the `recipes` list.
            central_atoms (list(Atom)): A list of atoms that define the centres of the 
                coordination polyhedra.
            coordination_atoms (list(Atom)): A list of atoms that define the vertices of 
                the coordination polyhedra.
         """
        self.atoms = [Atom(index=i,
                           site=site,
                           label=site.species_string)
                      for i, site in enumerate(structure.sites)]
        self.polyhedra: List[CoordinationPolyhedron] = []
        for recipe in recipes:
            self.polyhedra.extend(recipe.find_polyhedra(self.atoms, structure))
        self.central_atoms = sorted(list(set(
            [p.central_atom for p in self.polyhedra])),
            key=lambda x: x.index)
        self.coordination_atoms = sorted(list(set(flatten(
            [p.vertices for p in self.polyhedra]))),
            key=lambda x: x.index)
Пример #4
0
 def test___eq___when_false(self):
     index = 456
     site = Mock(spec=Site)
     label = 'foo'
     site.frac_coords = np.array([1.0, 2.0, 3.0])
     site.coords = np.array([10.0, 11.0, 12.0])
     site.lattice = Mock(spec=Lattice)
     other_atom = Atom(index=index, site=site, label=label)
     self.assertNotEqual(self.atom, other_atom)
Пример #5
0
 def test___lt__(self):
     index = 456
     site = MagicMock(spec=Site)
     label = 'bar'
     self.frac_coords = np.array([2.0, 3.0, 4.0])
     site.coords = np.array([20.0, 21.0, 22.0])
     site.lattice = Mock(spec=Lattice)
     other_atom = Atom(index=index, site=site, label=label)
     self.assertTrue(self.atom < other_atom)
Пример #6
0
 def test_atom_is_initialised_without_label( self ):
     index = 123
     site = Mock( spec=Site )
     atom = Atom( index=index, site=site )
     self.assertEqual( atom.index, index )
     self.assertEqual( atom.site, site )
     self.assertEqual( atom.label, None )
     self.assertEqual( atom.in_polyhedra, [] )
     self.assertEqual( atom.neighbours, None )
Пример #7
0
 def test_atom_is_initialised_without_label(self):
     index = 123
     site = Mock(spec=Site)
     site.species_string = 'bar'
     atom = Atom(index=index, site=site)
     self.assertEqual(atom.index, index)
     self.assertEqual(atom.site, site)
     self.assertEqual(atom.label, 'bar')
     self.assertEqual(atom.in_polyhedra, [])
     self.assertEqual(atom.neighbours, {})
Пример #8
0
 def test_atom_is_initialised(self):
     index = 123
     site = Mock(spec=Site)
     label = 'foo'
     atom = Atom(index=index, site=site, label=label)
     self.assertEqual(atom.index, index)
     self.assertEqual(atom.site, site)
     self.assertEqual(atom.label, label)
     self.assertEqual(atom.in_polyhedra, [])
     self.assertEqual(atom.neighbours, {})
Пример #9
0
class TestAtom(unittest.TestCase):

    def setUp(self):
        index = 123
        site = Mock(spec=Site)
        label = 'foo'
        site.frac_coords = np.array([1.0, 2.0, 3.0])
        site.coords = np.array([10.0, 11.0, 12.0])
        site.lattice = Mock(spec=Lattice)
        self.site = site
        self.atom = Atom(index=index, site=site, label=label)

    def test_frac_coords(self):
        np.testing.assert_array_equal(
            self.atom.frac_coords, self.site.frac_coords)

    def test_coords(self):
        np.testing.assert_array_equal(self.atom.coords, self.site.coords)

    def test_lattice(self):
        self.assertEqual(self.atom.lattice, self.site.lattice)

    def test___lt__(self):
        index = 456
        site = MagicMock(spec=Site)
        label = 'bar'
        self.frac_coords = np.array([2.0, 3.0, 4.0])
        site.coords = np.array([20.0, 21.0, 22.0])
        site.lattice = Mock(spec=Lattice)
        other_atom = Atom(index=index, site=site, label=label)
        self.assertTrue(self.atom < other_atom)

    def test___lt__raises_TypeError_if_not_passed_Atom(self):
        with self.assertRaises(TypeError):
            self.atom < 'foo'    

    def test___eq___when_true(self):
        index = 123
        site = Mock(spec=Site)
        label = 'foo'
        site.frac_coords = np.array([1.0, 2.0, 3.0])
        site.coords = np.array([10.0, 11.0, 12.0])
        site.lattice = Mock(spec=Lattice)
        other_atom = Atom(index=index, site=site, label=label)
        self.assertEqual(self.atom, other_atom)

    def test___eq___when_false(self):
        index = 456
        site = Mock(spec=Site)
        label = 'foo'
        site.frac_coords = np.array([1.0, 2.0, 3.0])
        site.coords = np.array([10.0, 11.0, 12.0])
        site.lattice = Mock(spec=Lattice)
        other_atom = Atom(index=index, site=site, label=label)
        self.assertNotEqual(self.atom, other_atom)

    def test___eq__is_False_if_not_passed_Atom(self):
        self.assertNotEqual(self.atom, 'foo')

    def test___hash__(self):
        self.assertEqual(hash(self.atom), self.atom.index)

    def test_distance(self):
        index = 456
        site = Mock(spec=Site)
        label = 'foo'
        other_atom = Atom(index=index, site=site, label=label)
        self.atom.site.distance = Mock(return_value=2.3)
        self.assertEqual(self.atom.distance(other_atom), 2.3)

    def test_as_dict(self):
        self.atom.site.as_dict = Mock(return_value={'key': 'value'})
        mock_other_polyhedra = Mock(spec=CoordinationPolyhedron)
        mock_other_polyhedra.index = 15
        self.atom.in_polyhedra = [mock_other_polyhedra]
        expected_dict = {'index': 123,
                         'site': {'key': 'value'},
                         'label': 'foo', 'in_polyhedra': [15],
                         'neighbours': {},
                         '@module': 'polyhedral_analysis.atom',
                         '@class': 'Atom'}
        self.assertEqual(self.atom.as_dict(), expected_dict)

    def test_to_json(self):
        self.atom.site.as_dict = Mock(return_value={'key': 'value'})
        mock_other_polyhedra = Mock(spec=CoordinationPolyhedron)
        mock_other_polyhedra.index = 15
        self.atom.in_polyhedra = [mock_other_polyhedra]
        expected_dict = {'index': 123,
                         'site': {'key': 'value'},
                         'label': 'foo', 'in_polyhedra': [15],
                         'neighbours': {},
                         '@module': 'polyhedral_analysis.atom',
                         '@class': 'Atom'}
        self.assertEqual(self.atom.to(), json.dumps(expected_dict))

    def test_to_json_file(self):
        self.atom.site.as_dict = Mock(return_value={'key': 'value'})
        mock_other_polyhedra = Mock(spec=CoordinationPolyhedron)
        mock_other_polyhedra.index = 15
        self.atom.in_polyhedra = [mock_other_polyhedra]
        expected_dict = {'index': 123,
                         'site': {'key': 'value'},
                         'label': 'foo', 'in_polyhedra': [15],
                         'neighbours': {},
                         '@module': 'polyhedral_analysis.atom',
                         '@class': 'Atom'}
        with patch('polyhedral_analysis.atom.zopen', mock_open(), create=True) as m:
            self.atom.to(filename='filename')
        m.assert_called_once_with('filename', 'wt')
        m().write.assert_called_once_with(json.dumps(expected_dict))

    def test_to_json_file_with_fmt_equals_JSON(self):
        self.atom.site.as_dict = Mock(return_value={'key': 'value'})
        mock_other_polyhedra = Mock(spec=CoordinationPolyhedron)
        mock_other_polyhedra.index = 15
        self.atom.in_polyhedra = [mock_other_polyhedra]
        expected_dict = {'index': 123,
                         'site': {'key': 'value'},
                         'label': 'foo', 'in_polyhedra': [15],
                         'neighbours': {},
                         '@module': 'polyhedral_analysis.atom',
                         '@class': 'Atom'}
        with patch('polyhedral_analysis.atom.zopen', mock_open(), create=True) as m:
            self.atom.to(filename='filename', fmt='JSON')
        m.assert_called_once_with('filename', 'wt')
        m().write.assert_called_once_with(json.dumps(expected_dict))

    def test_to_json_file_with_unknown_fmt_raises_ValueError(self):
        self.atom.site.as_dict = Mock(return_value={'key': 'value'})
        mock_other_polyhedra = Mock(spec=CoordinationPolyhedron)
        mock_other_polyhedra.index = 15
        self.atom.in_polyhedra = [mock_other_polyhedra]
        with self.assertRaises(ValueError):
            self.atom.to(filename='filename', fmt='FOO')