예제 #1
0
    def test_orbital_field_matrix(self):
        ofm_maker = OrbitalFieldMatrix(flatten=False)
        ofm = ofm_maker.featurize(self.diamond)[0]
        mtarget = np.zeros((32, 32))
        mtarget[1][1] = 1.4789015  # 1.3675444
        mtarget[1][3] = 1.4789015  # 1.3675444
        mtarget[3][1] = 1.4789015  # 1.3675444
        mtarget[3][3] = 1.4789015  # 1.3675444 if for a coord# of exactly 4
        for i in range(32):
            for j in range(32):
                if not i in [1, 3] and not j in [1, 3]:
                    self.assertEqual(ofm[i, j], 0.0)
        mtarget = np.matrix(mtarget)
        self.assertAlmostEqual(
            np.linalg.norm(ofm - mtarget), 0.0, places=4)

        ofm_maker = OrbitalFieldMatrix(True, flatten=False)
        ofm = ofm_maker.featurize(self.diamond)[0]
        mtarget = np.zeros((39, 39))
        mtarget[1][1] = 1.4789015
        mtarget[1][3] = 1.4789015
        mtarget[3][1] = 1.4789015
        mtarget[3][3] = 1.4789015
        mtarget[1][33] = 1.4789015
        mtarget[3][33] = 1.4789015
        mtarget[33][1] = 1.4789015
        mtarget[33][3] = 1.4789015
        mtarget[33][33] = 1.4789015
        mtarget = np.matrix(mtarget)
        self.assertAlmostEqual(
            np.linalg.norm(ofm - mtarget), 0.0, places=4)

        ofm_flat = OrbitalFieldMatrix(period_tag=False, flatten=True)
        self.assertEqual(len(ofm_flat.feature_labels()), 1024)

        ofm_flat = OrbitalFieldMatrix(period_tag=True, flatten=True)
        self.assertEqual(len(ofm_flat.feature_labels()), 1521)
        ofm_vector = ofm_flat.featurize(self.diamond)
        for ix in [40, 42, 72, 118, 120, 150, 1288, 1320]:
            self.assertAlmostEqual(ofm_vector[ix], 1.4789015345821415)
예제 #2
0
    def test_orbital_field_matrix(self):
        ofm_maker = OrbitalFieldMatrix()
        ofm = ofm_maker.featurize(self.diamond)[0]
        mtarget = np.zeros((32, 32))
        mtarget[1][1] = 1.4789015  # 1.3675444
        mtarget[1][3] = 1.4789015  # 1.3675444
        mtarget[3][1] = 1.4789015  # 1.3675444
        mtarget[3][3] = 1.4789015  # 1.3675444 if for a coord# of exactly 4
        for i in range(32):
            for j in range(32):
                if not i in [1, 3] and not j in [1, 3]:
                    self.assertEqual(ofm[i, j], 0.0)
        mtarget = np.matrix(mtarget)
        self.assertAlmostEqual(
            np.linalg.norm(ofm - mtarget), 0.0, places=4)

        ofm_maker = OrbitalFieldMatrix(True)
        ofm = ofm_maker.featurize(self.diamond)[0]
        mtarget = np.zeros((39, 39))
        mtarget[1][1] = 1.4789015
        mtarget[1][3] = 1.4789015
        mtarget[3][1] = 1.4789015
        mtarget[3][3] = 1.4789015
        mtarget[1][33] = 1.4789015
        mtarget[3][33] = 1.4789015
        mtarget[33][1] = 1.4789015
        mtarget[33][3] = 1.4789015
        mtarget[33][33] = 1.4789015
        mtarget = np.matrix(mtarget)
        self.assertAlmostEqual(
            np.linalg.norm(ofm - mtarget), 0.0, places=4)

        ofm_flat = OrbitalFieldMatrix(period_tag=False, flatten=True)
        self.assertEqual(len(ofm_flat.feature_labels()), 1024)

        ofm_flat = OrbitalFieldMatrix(period_tag=True, flatten=True)
        self.assertEqual(len(ofm_flat.feature_labels()), 1521)
        ofm_vector = ofm_flat.featurize(self.diamond)
        for ix in [40, 42, 72, 118, 120, 150, 1288, 1320]:
            self.assertAlmostEqual(ofm_vector[ix], 1.4789015345821415)
    # Featurize dataframe with OFM and time it
    start = time.monotonic()
    ofm = OrbitalFieldMatrix(period_tag=ROW)
    ofm.set_n_jobs(NJOBS)
    df = ofm.fit_featurize_dataframe(df, 'structure')
    finish = time.monotonic()
    print("TIME TO FEATURIZE OFM %f SECONDS" % (finish - start))
    print()

    # Get OFM descriptor and set up KRR model
    krr = KernelRidge()
    hpsel = GridSearchCV(krr,
                         params['orbital field matrix'],
                         cv=inner_cv,
                         refit=True)
    X = df[ofm.feature_labels()].to_numpy()
    # Flatten each OFM to form a vector descriptor
    XLIST = []
    for i in range(len(X)):
        XLIST.append(X[i].flatten())
    X = np.array(XLIST)
    print(X.shape)
    Y = df['formation_energy_per_atom'].to_numpy()
    mae, rmse, r2 = 0, 0, 0
    # Evaluate OFM
    start = time.monotonic()
    for train_index, test_index in kf.split(X):
        X_train, X_test = X[train_index], X[test_index]
        Y_train, Y_test = Y[train_index], Y[test_index]
        hpsel.fit(X_train, Y_train)
        print("--- OFM PARAM OPT")
    print ("ROW ELEMS", ROW)

    # Featurize dataframe with OFM and time it
    start = time.monotonic()
    ofm = OrbitalFieldMatrix(period_tag=ROW)
    ofm.set_n_jobs(NJOBS)
    df = ofm.fit_featurize_dataframe(df, 'structure')
    finish = time.monotonic()
    print("TIME TO FEATURIZE OFM %f SECONDS" % (finish-start))
    print()

    # Get OFM descriptor and set up KRR model
    krr = KernelRidge()
    hpsel = GridSearchCV(krr, params['orbital field matrix'],
                         cv=inner_cv, refit=True)
    X = df[ofm.feature_labels()].as_matrix()
    # Flatten each OFM to form a vector descriptor
    XLIST = []
    for i in range(len(X)):
        XLIST.append(X[i].flatten())
    X = np.array(XLIST)
    print(X.shape)
    Y = df['formation_energy_per_atom'].as_matrix()
    mae, rmse, r2 = 0, 0, 0
    # Evaluate OFM
    start = time.monotonic()
    for train_index, test_index in kf.split(X):
        X_train, X_test = X[train_index], X[test_index]
        Y_train, Y_test = Y[train_index], Y[test_index]
        hpsel.fit(X_train, Y_train)
        print("--- OFM PARAM OPT")