Exemplo n.º 1
0
    def test_samples(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=False)
        descriptor = calculator.compute(system, use_native_system=False)

        samples = descriptor.samples
        self.assertEqual(len(samples), 4)

        with self.assertRaisesRegex(ValueError,
                                    "assignment destination is read-only"):
            samples[0] = (3, 4)

        self.assertTrue(np.all(samples["structure"] == [0, 0, 0, 0]))
        self.assertTrue(np.all(samples["center"] == [0, 1, 2, 3]))

        # view & reshape for easier direct comparison of values
        # numpy only consider structured arrays to be equal if they have
        # the same dtype
        samples = samples.view(dtype=np.int32).reshape((samples.shape[0], -1))
        self.assertTrue(np.all(samples[0] == [0, 0]))
        self.assertTrue(np.all(samples[1] == [0, 1]))
        self.assertTrue(np.all(samples[2] == [0, 2]))
        self.assertTrue(np.all(samples[3] == [0, 3]))
Exemplo n.º 2
0
    def test_compute_multiple_systems(self):
        systems = [TestSystem(), TestSystem(), TestSystem()]
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=2,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(systems, use_native_system=False)

        self.assertEqual(descriptor.values.shape, (12, 2))
Exemplo n.º 3
0
    def test_densify(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(system, use_native_system=False)

        self.assertEqual(descriptor.values.shape, (4, 2))
        self.assertEqual(descriptor.gradients.shape, (18, 2))

        descriptor.densify("center")

        self.assertEqual(descriptor.values.shape, (1, 8))
        self.assertEqual(descriptor.gradients.shape, (12, 8))
Exemplo n.º 4
0
    def test_gradients(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=False)
        descriptor = calculator.compute(system, use_native_system=False)
        self.assertEqual(descriptor.gradients, None)

        system = EmptySystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(system, use_native_system=False)
        self.assertEqual(descriptor.gradients.shape, (0, 2))

        system = TestSystem()
        descriptor = calculator.compute(system, use_native_system=False)
        gradients = descriptor.gradients
        self.assertEqual(gradients.shape, (18, 2))
        for i in range(18):
            self.assertTrue(np.all(gradients[i] == (0, 1)))

        with self.assertRaisesRegex(ValueError,
                                    "assignment destination is read-only"):
            gradients[0] = (3, 4)
Exemplo n.º 5
0
 def test_parameters(self):
     calculator = DummyCalculator(cutoff=3.2,
                                  delta=12,
                                  name="foo",
                                  gradients=True)
     self.assertEqual(
         calculator.parameters,
         """{"cutoff": 3.2, "delta": 12, "name": "foo", "gradients": true}""",
     )
Exemplo n.º 6
0
    def test_compute(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=2,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(system, use_native_system=False)

        values = descriptor.values
        self.assertEqual(values.shape, (4, 2))
        self.assertTrue(np.all(values[0] == (2, 1)))
        self.assertTrue(np.all(values[1] == (3, 3)))
        self.assertTrue(np.all(values[2] == (4, 6)))
        self.assertTrue(np.all(values[3] == (5, 5)))

        gradients = descriptor.gradients
        self.assertEqual(gradients.shape, (18, 2))
        for i in range(gradients.shape[0]):
            self.assertTrue(np.all(gradients[i] == (0, 1)))
Exemplo n.º 7
0
    def test_bad_parameters(self):
        message = (
            'json error: invalid type: string "12", expected isize at line 1 column 29'
        )

        with self.assertRaisesRegex(Exception, message):
            _ = DummyCalculator(cutoff=3.2,
                                delta="12",
                                name="foo",
                                gradients=True)
Exemplo n.º 8
0
    def test_compute_partial_features(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=2,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(system, use_native_system=False)

        # From a selection scheme, using numpy array indexing
        features = descriptor.features[[1]]
        descriptor = calculator.compute(system,
                                        use_native_system=False,
                                        selected_features=features)

        values = descriptor.values
        self.assertEqual(values.shape, (4, 1))
        self.assertTrue(np.all(values[0] == [1]))
        self.assertTrue(np.all(values[1] == [3]))
        self.assertTrue(np.all(values[2] == [6]))
        self.assertTrue(np.all(values[3] == [5]))

        gradients = descriptor.gradients
        self.assertEqual(gradients.shape, (18, 1))
        for i in range(gradients.shape[0]):
            self.assertTrue(np.all(gradients[i] == [1]))

        # Manually constructing the selected features
        features = [[1, 0]]
        descriptor = calculator.compute(system,
                                        use_native_system=False,
                                        selected_features=features)

        values = descriptor.values
        self.assertEqual(values.shape, (4, 1))
        self.assertTrue(np.all(values[0] == [2]))
        self.assertTrue(np.all(values[1] == [3]))
        self.assertTrue(np.all(values[2] == [4]))
        self.assertTrue(np.all(values[3] == [5]))

        gradients = descriptor.gradients
        self.assertEqual(gradients.shape, (18, 1))
        for i in range(gradients.shape[0]):
            self.assertTrue(np.all(gradients[i] == [0]))
Exemplo n.º 9
0
    def test_unimplemented(self):
        system = UnimplementedSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=2,
                                     name="",
                                     gradients=True)

        with self.assertRaises(RascalError) as cm:
            _ = calculator.compute(system, use_native_system=False)

        self.assertEqual(
            cm.exception.args[0],
            "error from external code (status -1): call to rascal_system_t.size failed",
        )

        self.assertEqual(
            cm.exception.__cause__.args[0],
            "System.size method is not implemented",
        )
Exemplo n.º 10
0
    def test_exception_in_callback(self):
        def raise_on_log_event(level, message):
            raise Exception("this is an exception")

        set_logging_callback(raise_on_log_event)

        calculator = DummyCalculator(
            cutoff=3.2,
            delta=0,
            name="log-test-warn: testing errors",
            gradients=False,
        )

        with self.assertWarns(Warning) as cm:
            _ = calculator.compute(TestSystem())

        self.assertEqual(
            cm.warnings[0].message.args[0],
            "exception raised in logging callback: this is an exception",
        )
Exemplo n.º 11
0
    def test_compute(self):
        frame = chemfiles.Frame()
        frame.add_atom(chemfiles.Atom("C"), (0, 0, 0))
        frame.add_atom(chemfiles.Atom("C"), (0, 1, 0))
        frame.add_atom(chemfiles.Atom("C"), (0, 2, 0))
        frame.add_atom(chemfiles.Atom("C"), (0, 3, 0))

        calculator = DummyCalculator(cutoff=3.4, delta=1, name="", gradients=False)

        with self.assertRaises(RascalError) as cm:
            calculator.compute(frame, use_native_system=False)

        self.assertEqual(
            cm.exception.__cause__.args[0],
            "chemfiles systems can only be used with 'use_native_system=True'",
        )

        # use_native_system=True should work fine
        descriptor = calculator.compute(frame, use_native_system=True)
        self.assertEqual(descriptor.values.shape, (4, 2))
Exemplo n.º 12
0
    def test_values(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=False)
        descriptor = calculator.compute(system, use_native_system=False)

        values = descriptor.values
        self.assertEqual(values.shape, (4, 2))
        self.assertTrue(np.all(values[0] == (12, 1)))
        self.assertTrue(np.all(values[1] == (13, 3)))
        self.assertTrue(np.all(values[2] == (14, 6)))
        self.assertTrue(np.all(values[3] == (15, 5)))

        with self.assertRaisesRegex(ValueError,
                                    "assignment destination is read-only"):
            values[0] = (3, 4)

        self.assertEqual(descriptor.gradients, None)
Exemplo n.º 13
0
    def test_compute_partial_samples(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=2,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(system, use_native_system=False)

        # From a selection scheme, using numpy array indexing
        samples = descriptor.samples[[0, 2]]
        descriptor = calculator.compute(system,
                                        use_native_system=False,
                                        selected_samples=samples)

        values = descriptor.values
        self.assertEqual(values.shape, (2, 2))
        self.assertTrue(np.all(values[0] == (2, 1)))
        self.assertTrue(np.all(values[1] == (4, 6)))

        gradients = descriptor.gradients
        self.assertEqual(gradients.shape, (9, 2))
        for i in range(gradients.shape[0]):
            self.assertTrue(np.all(gradients[i] == (0, 1)))

        #  Manually constructing the selected samples
        samples = [(0, 0), (0, 3), (0, 1)]
        descriptor = calculator.compute(system,
                                        use_native_system=False,
                                        selected_samples=samples)

        values = descriptor.values
        self.assertEqual(values.shape, (3, 2))
        self.assertTrue(np.all(values[0] == (2, 1)))
        self.assertTrue(np.all(values[1] == (5, 5)))
        self.assertTrue(np.all(values[2] == (3, 3)))

        gradients = descriptor.gradients
        self.assertEqual(gradients.shape, (12, 2))
        for i in range(gradients.shape[0]):
            self.assertTrue(np.all(gradients[i] == (0, 1)))
Exemplo n.º 14
0
    def test_log_message(self):
        recorded_events = []

        def record_log_events(level, message):
            recorded_events.append((level, message))

        set_logging_callback(record_log_events)

        calculator = DummyCalculator(
            cutoff=3.2,
            delta=0,
            name="log-test-info: test info message",
            gradients=False,
        )
        _ = calculator.compute(TestSystem())

        message = ("rascaline::calculators::dummy_calculator -- "
                   "log-test-info: test info message")
        event = (RASCAL_LOG_LEVEL_INFO, message)
        self.assertTrue(event in recorded_events)

        calculator = DummyCalculator(
            cutoff=3.2,
            delta=0,
            name="log-test-warn: this is a test warning message",
            gradients=False,
        )
        _ = calculator.compute(TestSystem())

        message = ("rascaline::calculators::dummy_calculator -- "
                   "log-test-warn: this is a test warning message")
        event = (RASCAL_LOG_LEVEL_WARN, message)
        self.assertTrue(event in recorded_events)
Exemplo n.º 15
0
    def test_gradient_indexes(self):
        system = TestSystem()
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=False)
        descriptor = calculator.compute(system, use_native_system=False)
        self.assertEqual(len(descriptor.gradients_samples), 0)

        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="",
                                     gradients=True)
        descriptor = calculator.compute(system, use_native_system=False)
        gradients_samples = descriptor.gradients_samples
        self.assertEqual(len(gradients_samples), 18)

        with self.assertRaisesRegex(ValueError,
                                    "assignment destination is read-only"):
            gradients_samples[0] = (3, 4)

        expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        self.assertTrue(np.all(gradients_samples["structure"] == expected))

        expected = [0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3]
        self.assertTrue(np.all(gradients_samples["center"] == expected))

        expected = [1, 1, 1, 0, 0, 0, 2, 2, 2, 1, 1, 1, 3, 3, 3, 2, 2, 2]
        self.assertTrue(np.all(gradients_samples["neighbor"] == expected))

        expected = [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]
        self.assertTrue(np.all(gradients_samples["spatial"] == expected))

        # view & reshape for easier direct comparison of values
        # numpy only consider structured arrays to be equal if they have
        # the same dtype
        gradients_samples = gradients_samples.view(dtype=np.int32).reshape(
            (gradients_samples.shape[0], -1))

        self.assertTrue(np.all(gradients_samples[0] == [0, 0, 1, 0]))
        self.assertTrue(np.all(gradients_samples[1] == [0, 0, 1, 1]))
        self.assertTrue(np.all(gradients_samples[2] == [0, 0, 1, 2]))
        self.assertTrue(np.all(gradients_samples[3] == [0, 1, 0, 0]))
        self.assertTrue(np.all(gradients_samples[4] == [0, 1, 0, 1]))
        self.assertTrue(np.all(gradients_samples[5] == [0, 1, 0, 2]))
        self.assertTrue(np.all(gradients_samples[6] == [0, 1, 2, 0]))
        self.assertTrue(np.all(gradients_samples[7] == [0, 1, 2, 1]))
        self.assertTrue(np.all(gradients_samples[8] == [0, 1, 2, 2]))
        self.assertTrue(np.all(gradients_samples[9] == [0, 2, 1, 0]))
        self.assertTrue(np.all(gradients_samples[10] == [0, 2, 1, 1]))
        self.assertTrue(np.all(gradients_samples[11] == [0, 2, 1, 2]))
        self.assertTrue(np.all(gradients_samples[12] == [0, 2, 3, 0]))
        self.assertTrue(np.all(gradients_samples[13] == [0, 2, 3, 1]))
        self.assertTrue(np.all(gradients_samples[14] == [0, 2, 3, 2]))
        self.assertTrue(np.all(gradients_samples[15] == [0, 3, 2, 0]))
        self.assertTrue(np.all(gradients_samples[16] == [0, 3, 2, 1]))
        self.assertTrue(np.all(gradients_samples[17] == [0, 3, 2, 2]))
Exemplo n.º 16
0
    def test_name(self):
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name="foo",
                                     gradients=True)
        self.assertEqual(
            calculator.name,
            "dummy test calculator with cutoff: 3.2 - delta: 12"
            " - name: foo - gradients: true",
        )

        self.assertEqual(calculator.c_name, "dummy_calculator")

        # very long name, checking that we can pass large string back and forth
        name = "abc" * 2048
        calculator = DummyCalculator(cutoff=3.2,
                                     delta=12,
                                     name=name,
                                     gradients=True)
        self.assertEqual(
            calculator.name,
            "dummy test calculator with cutoff: 3.2 - delta: 12"
            f" - name: {name} - gradients: true",
        )