def test_env_methods(structure, mask, cutoff, result): if mask is True: mask = generate_mask(cutoff) else: mask = None structure = deepcopy(structure) structure.forces = np.random.random(size=(len(structure), 3)) env_test = AtomicEnvironment(structure, atom=0, cutoffs=cutoff, cutoffs_mask=mask) assert np.array_equal(structure.forces[0], env_test.force) assert ( str(env_test) == f"Atomic Env. of Type 1 surrounded by {result[0]} atoms of Types [1, 2, 3]" ) the_dict = env_test.as_dict() assert isinstance(the_dict, dict) the_str = env_test.as_str() assert dumps(the_dict, cls=NumpyEncoder) == the_str for key in ["positions", "cell", "atom", "cutoffs", "species"]: assert key in the_dict.keys() # This saves a few seconds, the masked envs take longer to read/write if not mask: with open("test_environment.json", "w") as f: f.write(env_test.as_str()) remade_env = AtomicEnvironment.from_file("test_environment.json") else: remade_env = AtomicEnvironment.from_dict(the_dict) assert isinstance(remade_env, AtomicEnvironment) assert np.array_equal(remade_env.bond_array_2, env_test.bond_array_2) if len(cutoff) > 1: assert np.array_equal(remade_env.bond_array_3, env_test.bond_array_3) if len(cutoff) > 2: assert np.array_equal(remade_env.q_array, env_test.q_array) if not mask: remove("test_environment.json")
from flare.rbcm import RobustBayesianCommitteeMachine from flare.gp import GaussianProcess import os as os import numpy as np from flare.struc import Structure from flare.env import AtomicEnvironment from flare.gp_algebra import get_kernel_vector TEST_DIR = os.path.dirname(__file__) TEST_FILE_DIR = os.path.join(TEST_DIR, "test_files") methanol_frames = Structure.from_file( os.path.join(TEST_FILE_DIR, "methanol_frames.json")) methanol_envs = AtomicEnvironment.from_file( os.path.join(TEST_FILE_DIR, "methanol_envs.json")) def test_basic(): rbcm = RobustBayesianCommitteeMachine() assert isinstance(rbcm, RobustBayesianCommitteeMachine) def test_expert_growth_and_training(): """ Test that as data is added the data is allocated to experts correctly and that various training algorithms can run correctly on it :return: """
def test_passive_learning(): the_gp = GaussianProcess( kernel_name="2+3_mc", hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03, ]), cutoffs=np.array([5, 3]), hyp_labels=["l2", "s2", "l3", "s3", "n0"], maxiter=1, opt_algorithm="L-BFGS-B", ) frames = Structure.from_file( path.join(TEST_FILE_DIR, "methanol_frames.json")) envs = AtomicEnvironment.from_file( path.join(TEST_FILE_DIR, "methanol_envs.json")) cur_gp = deepcopy(the_gp) tt = TrajectoryTrainer(frames=None, gp=cur_gp) # TEST ENVIRONMENT ADDITION envs_species = set(Z_to_element(env.ctype) for env in envs) tt.run_passive_learning(environments=envs, post_build_matrices=False) assert cur_gp.training_statistics["N"] == len(envs) assert set(cur_gp.training_statistics["species"]) == envs_species # TEST FRAME ADDITION: ALL ARE ADDED cur_gp = deepcopy(the_gp) tt.gp = cur_gp tt.run_passive_learning(frames=frames, post_build_matrices=False) assert len(cur_gp.training_data) == sum([len(fr) for fr in frames]) # TEST FRAME ADDITION: MAX OUT MODEL SIZE AT 1 cur_gp = deepcopy(the_gp) tt.gp = cur_gp tt.run_passive_learning(frames=frames, max_model_size=1, post_training_iterations=1) assert len(cur_gp.training_data) == 1 # TEST FRAME ADDITION: EXCLUDE OXYGEN, LIMIT CARBON TO 1, 1 H PER FRAME cur_gp = deepcopy(the_gp) tt.gp = cur_gp tt.run_passive_learning( frames=frames, max_model_elts={ "O": 0, "C": 1, "H": 5 }, max_elts_per_frame={"H": 1}, post_build_matrices=False, ) assert "O" not in cur_gp.training_statistics["species"] assert cur_gp.training_statistics["envs_by_species"]["C"] == 1 assert cur_gp.training_statistics["envs_by_species"]["H"] == 5