Exemplo n.º 1
0
    def test_parallel_sparse(self):
        """Tests creating sparse output parallelly.
        """
        # Test indices
        samples = [molecule("CO"), molecule("N2O")]
        desc = LMBTR(species=[6, 7, 8],
                     k=[2],
                     grid={"k2": {
                         "n": 100,
                         "min": 0,
                         "max": 2,
                         "sigma": 0.1
                     }},
                     virtual_positions=False,
                     periodic=False,
                     flatten=True,
                     sparse=True)
        n_features = desc.get_number_of_features()

        # Multiple systems, serial job
        output = desc.create(
            system=samples,
            positions=[[0], [0, 1]],
            n_jobs=1,
        ).toarray()
        assumed = np.empty((3, n_features))
        assumed[0, :] = desc.create(samples[0], [0]).toarray()
        assumed[1, :] = desc.create(samples[1], [0]).toarray()
        assumed[2, :] = desc.create(samples[1], [1]).toarray()
        self.assertTrue(np.allclose(output, assumed))

        # Test when position given as indices
        output = desc.create(
            system=samples,
            positions=[[0], [0, 1]],
            n_jobs=2,
        ).toarray()
        assumed = np.empty((3, n_features))
        assumed[0, :] = desc.create(samples[0], [0]).toarray()
        assumed[1, :] = desc.create(samples[1], [0]).toarray()
        assumed[2, :] = desc.create(samples[1], [1]).toarray()
        self.assertTrue(np.allclose(output, assumed))

        # Test with cartesian positions. In this case virtual positions have to
        # be enabled
        desc = LMBTR(species=[6, 7, 8],
                     k=[2],
                     grid={"k2": {
                         "n": 100,
                         "min": 0,
                         "max": 2,
                         "sigma": 0.1
                     }},
                     virtual_positions=True,
                     periodic=False,
                     flatten=True,
                     sparse=True)
        output = desc.create(
            system=samples,
            positions=[[[0, 0, 0], [1, 2, 0]], [[1, 2, 0]]],
            n_jobs=2,
        ).toarray()
        assumed = np.empty((2 + 1, n_features))
        assumed[0, :] = desc.create(samples[0], [[0, 0, 0]]).toarray()
        assumed[1, :] = desc.create(samples[0], [[1, 2, 0]]).toarray()
        assumed[2, :] = desc.create(samples[1], [[1, 2, 0]]).toarray()
        self.assertTrue(np.allclose(output, assumed))
Exemplo n.º 2
0
    def test_number_of_features(self):
        """LMBTR: Tests that the reported number of features is correct.
        """
        # K = 1
        n = 100
        atomic_numbers = [1, 8]
        n_elem = len(atomic_numbers) + 1  # Including ghost atom
        lmbtr = LMBTR(
            species=atomic_numbers,
            k=[1],
            grid={"k1": {
                "min": 1,
                "max": 8,
                "sigma": 0.1,
                "n": 100,
            }},
            virtual_positions=False,
            periodic=False,
            flatten=True)
        n_features = lmbtr.get_number_of_features()
        expected = n_elem * n
        self.assertEqual(n_features, expected)

        # K = 2
        lmbtr = LMBTR(species=atomic_numbers,
                      k={1, 2},
                      grid={
                          "k1": {
                              "min": 1,
                              "max": 8,
                              "sigma": 0.1,
                              "n": 100,
                          },
                          "k2": {
                              "min": 0,
                              "max": 1 / 0.7,
                              "sigma": 0.1,
                              "n": n,
                          }
                      },
                      virtual_positions=False,
                      periodic=False,
                      flatten=True)
        n_features = lmbtr.get_number_of_features()
        expected = n_elem * n + 1 / 2 * (n_elem) * (n_elem + 1) * n
        self.assertEqual(n_features, expected)

        # K = 3
        lmbtr = LMBTR(species=atomic_numbers,
                      k={3},
                      grid={
                          "k1": {
                              "min": 1,
                              "max": 8,
                              "sigma": 0.1,
                              "n": 100,
                          },
                          "k2": {
                              "min": 0,
                              "max": 1 / 0.7,
                              "sigma": 0.1,
                              "n": n,
                          },
                          "k3": {
                              "min": -1,
                              "max": 1,
                              "sigma": 0.1,
                              "n": n,
                          }
                      },
                      virtual_positions=False,
                      periodic=False,
                      flatten=True)
        n_features = lmbtr.get_number_of_features()
        expected = n_elem * 1 / 2 * (n_elem) * (n_elem + 1) * n
        self.assertEqual(n_features, expected)