Exemple #1
0
 def setUp(self):
     profile = {'Mo': {'r': 0.6, 'w': 1.}}
     self.describer1 = BispectrumCoefficients(rcutfac=4.6,
                                              twojmax=6,
                                              element_profile=profile,
                                              quadratic=False,
                                              pot_fit=True)
     model1 = LinearModel(describer=self.describer1)
     self.potential1 = SNAPotential(model=model1, name='test')
     self.describer2 = BispectrumCoefficients(rcutfac=4.6,
                                              twojmax=6,
                                              element_profile=profile,
                                              quadratic=True,
                                              pot_fit=True)
     model2 = LinearModel(describer=self.describer2)
     self.potential2 = SNAPotential(model=model2, name='test')
     self.test_pool = test_datapool
     self.test_structures = []
     self.test_energies = []
     self.test_forces = []
     self.test_stresses = []
     for d in self.test_pool:
         self.test_structures.append(d['structure'])
         self.test_energies.append(d['outputs']['energy'])
         self.test_forces.append(d['outputs']['forces'])
         self.test_stresses.append(d['outputs']['virial_stress'])
     self.test_struct = self.test_pool[-1]['structure']
Exemple #2
0
    def setUp(self):

        element_profile = {'Ni': {'r': 0.5, 'w': 1}}
        describer1 = BispectrumCoefficients(rcutfac=4.1,
                                            twojmax=8,
                                            element_profile=element_profile,
                                            quadratic=False,
                                            pot_fit=True)
        model1 = LinearModel(describer=describer1)
        model1.model.coef_ = coeff
        model1.model.intercept_ = intercept
        snap1 = SNAPotential(model=model1)
        snap1.specie = Element('Ni')
        self.ff_settings1 = snap1

        describer2 = BispectrumCoefficients(rcutfac=4.1,
                                            twojmax=8,
                                            element_profile=element_profile,
                                            quadratic=True,
                                            pot_fit=True)
        model2 = LinearModel(describer=describer2)
        model2.model.coef_ = coeff
        model2.model.intercept_ = intercept
        snap2 = SNAPotential(model=model2)
        snap2.specie = Element('Ni')
        self.ff_settings2 = snap2

        self.struct = Structure.from_spacegroup('Fm-3m', Lattice.cubic(3.506),
                                                ['Ni'], [[0, 0, 0]])
Exemple #3
0
    def setUp(self):
        class DummyDescriber(MSONable):
            def describe(self, obj):
                pass

            def describe_all(self, n):
                return pd.DataFrame(n)

        self.lm = LinearModel(DummyDescriber())

        self.test_dir = tempfile.mkdtemp()
Exemple #4
0
    def from_config(param_file, coeff_file, **kwargs):
        """
        Initialize potentials with parameters file and coefficient file.

        Args:
            param_file (str): The file storing the configuration of potentials.
            coeff_file (str): The file storing the coefficients of potentials.

        Return:
            SNAPotential.
        """
        with open(coeff_file) as f:
            coeff_lines = f.readlines()
        coeff_lines = [
            line for line in coeff_lines if not line.startswith('#')
        ]
        specie, r, w = coeff_lines[1].split()
        r, w = float(r), int(w)
        element_profile = {specie: {'r': r, 'w': w}}

        rcut_pattern = re.compile('rcutfac (.*?)\n', re.S)
        twojmax_pattern = re.compile('twojmax (\d*)\n', re.S)
        rfac_pattern = re.compile('rfac0 (.*?)\n', re.S)
        rmin_pattern = re.compile('rmin0 (.*?)\n', re.S)
        diagonalstyle_pattern = re.compile('diagonalstyle (.*?)\n', re.S)
        quadratic_pattern = re.compile('quadraticflag (.*?)(?=\n|$)', re.S)

        with zopen(param_file, 'rt') as f:
            param_lines = f.read()

        rcut = float(rcut_pattern.findall(param_lines)[-1])
        twojmax = int(twojmax_pattern.findall(param_lines)[-1])
        rfac = float(rfac_pattern.findall(param_lines)[-1])
        rmin = int(rmin_pattern.findall(param_lines)[-1])
        diagonal = int(diagonalstyle_pattern.findall(param_lines)[-1])
        if quadratic_pattern.findall(param_lines):
            quadratic = bool(int(quadratic_pattern.findall(param_lines)[-1]))
        else:
            quadratic = False

        describer = BispectrumCoefficients(rcutfac=rcut,
                                           twojmax=twojmax,
                                           rfac0=rfac,
                                           element_profile=element_profile,
                                           rmin0=rmin,
                                           diagonalstyle=diagonal,
                                           quadratic=quadratic,
                                           pot_fit=True)
        model = LinearModel(describer=describer, **kwargs)
        model.model.coef_ = np.array(coeff_lines[2:], dtype=np.float)
        model.model.intercept_ = 0
        snap = SNAPotential(model=model)
        snap.specie = Element(specie)
        return snap
Exemple #5
0
 def setUp(self):
     element_profile = {'Ni': {'r': 0.5, 'w': 1}}
     describer = BispectrumCoefficients(rcutfac=4.1, twojmax=8,
                                        element_profile=element_profile,
                                        pot_fit=True)
     model = LinearModel(describer=describer)
     model.model.coef_ = coeff
     model.model.intercept_ = intercept
     snap = SNAPotential(model=model)
     snap.specie = Element('Ni')
     self.ff_settings = snap
Exemple #6
0
 def setUp(self):
     element_profile = {'Ni': {'r': 0.5, 'w': 1}}
     describer = BispectrumCoefficients(rcutfac=4.1, twojmax=8,
                                        element_profile=element_profile,
                                        pot_fit=True)
     model = LinearModel(describer=describer)
     model.model.coef_ = coeff
     model.model.intercept_ = intercept
     snap = SNAPotential(model=model)
     snap.specie = Element('Ni')
     self.struct = Structure.from_spacegroup('Fm-3m',
                                             Lattice.cubic(3.506),
                                             ['Ni'], [[0, 0, 0]])
     self.ff_settings = snap
Exemple #7
0
 def test_serialize(self):
     json_str = json.dumps(self.lm.as_dict())
     recover = LinearModel.from_dict(json.loads(json_str))
     self.assertIsNotNone(recover)
Exemple #8
0
class LinearModelTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.x_train = np.random.rand(10, 2)
        cls.coef = np.random.rand(2)
        cls.intercept = np.random.rand()
        cls.y_train = cls.x_train.dot(cls.coef) + cls.intercept

    def setUp(self):
        class DummyDescriber(MSONable):
            def describe(self, obj):
                pass

            def describe_all(self, n):
                return pd.DataFrame(n)

        self.lm = LinearModel(DummyDescriber())

        self.test_dir = tempfile.mkdtemp()

    def tearDown(self):
        # Remove the directory after the test
        shutil.rmtree(self.test_dir)

    def test_fit_predict(self):
        self.lm.fit(inputs=self.x_train, outputs=self.y_train)
        x_test = np.random.rand(10, 2)
        y_test = x_test.dot(self.coef) + self.intercept
        y_pred = self.lm.predict(x_test)
        np.testing.assert_array_almost_equal(y_test, y_pred)
        np.testing.assert_array_almost_equal(self.coef, self.lm.coef)
        self.assertAlmostEqual(self.intercept, self.lm.intercept)

    def test_evaluate_fit(self):
        self.lm.fit(inputs=self.x_train, outputs=self.y_train)
        y_pred = self.lm.evaluate_fit()
        np.testing.assert_array_almost_equal(y_pred, self.y_train)

    def test_serialize(self):
        json_str = json.dumps(self.lm.as_dict())
        recover = LinearModel.from_dict(json.loads(json_str))
        self.assertIsNotNone(recover)

    def model_save_load(self):
        self.lm.save(os.path.join(self.test_dir, 'test_lm.save'))
        ori = self.lm.model.coef_
        self.lm.load(os.path.join(self.test_dir, 'test_lm.save'))
        loaded = self.lm.model.coef_
        self.assertAlmostEqual(ori, loaded)