def test_random_structure_setup(): struct, forces = get_random_structure(cell=np.eye(3), unique_species=[1, 2], noa=2) assert np.equal(struct.cell, np.eye(3)).all() assert len(struct.positions) == 2
def test_file_load(): struct1, forces = get_random_structure(cell=np.eye(3), unique_species=[1, 2], noa=2) struct2, forces = get_random_structure(cell=np.eye(3), unique_species=[1, 2], noa=2) with open("test_write.json",'w') as f: f.write(dumps(struct1.as_dict(),cls=NumpyEncoder)) with pytest.raises(NotImplementedError): Structure.from_file(file_name='test_write.json', format='xyz') struct1a = Structure.from_file('test_write.json') assert dumpcompare(struct1.as_dict() , struct1a.as_dict()) os.remove('test_write.json') with open("test_write_set.json", 'w') as f: f.write(dumps(struct1.as_dict(), cls=NumpyEncoder)+'\n') f.write(dumps(struct2.as_dict(), cls=NumpyEncoder)+'\n') structs = Structure.from_file('test_write_set.json') for struc in structs: assert isinstance(struc, Structure) assert len(structs) == 2 assert dumpcompare(structs[0].as_dict(), struct1.as_dict()) assert dumpcompare(structs[1].as_dict(), struct2.as_dict()) os.remove('test_write_set.json') with pytest.raises(FileNotFoundError): Structure.from_file('fhqwhgads') vasp_struct = Structure.from_file('./test_files/test_POSCAR') assert isinstance(vasp_struct, Structure) assert len(vasp_struct) == 6
def test_file_load(): pmgstruc = pytest.importorskip("pymatgen.core.structure") struct1, forces = get_random_structure(cell=np.eye(3), unique_species=[1, 2], noa=2) struct2, forces = get_random_structure(cell=np.eye(3), unique_species=[1, 2], noa=2) with open("test_write.json", "w") as f: f.write(dumps(struct1.as_dict(), cls=NumpyEncoder)) with pytest.raises(NotImplementedError): Structure.from_file(file_name="test_write.json", format="xyz") struct1a = Structure.from_file("test_write.json") assert dumpcompare(struct1.as_dict(), struct1a.as_dict()) os.remove("test_write.json") with open("test_write_set.json", "w") as f: f.write(dumps(struct1.as_dict(), cls=NumpyEncoder) + "\n") f.write(dumps(struct2.as_dict(), cls=NumpyEncoder) + "\n") structs = Structure.from_file("test_write_set.json") for struc in structs: assert isinstance(struc, Structure) assert len(structs) == 2 assert dumpcompare(structs[0].as_dict(), struct1.as_dict()) assert dumpcompare(structs[1].as_dict(), struct2.as_dict()) os.remove("test_write_set.json") with pytest.raises(FileNotFoundError): Structure.from_file("fhqwhgads") vasp_struct = Structure.from_file("./test_files/test_POSCAR") assert isinstance(vasp_struct, Structure) assert len(vasp_struct) == 6
def test_std_in_bound_per_species(): test_structure, _ = get_random_structure(np.eye(3), ['H', 'O'], 3) test_structure.species_labels = ['H', 'H', 'O'] test_structure.stds = np.array([[1, 0, 0], [2, 0, 0], [3, 0, 0]]) # Test that 'test mode' works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=0, abs_std_tolerance=0, noise=0, structure=test_structure) assert result is True and target_atoms == [-1] # Test that high abs tolerance works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=0, abs_std_tolerance=4, noise=0, structure=test_structure) assert result is True and target_atoms == [-1] # Test that low abs tolerance works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=0, abs_std_tolerance=2.9, noise=0, structure=test_structure) assert result is False and target_atoms == [2] # Test that high rel tolerance works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=0, noise=4, structure=test_structure) assert result is True and target_atoms == [-1] # Test that low rel tolerance works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=0, noise=2.9, structure=test_structure) assert result is False and target_atoms == [2] # Test that both work result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure) assert result is False and target_atoms == [2, 1, 0] # Test that the max atoms added works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure, max_atoms_added=2) assert result is False and target_atoms == [2, 1] result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure, max_atoms_added=1) assert result is False and target_atoms == [2] # Test that max by species works result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure, max_atoms_added=1, max_by_species={'H': 1, 'O': 0}) assert result is False and target_atoms == [1] result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure, max_atoms_added=1, max_by_species={'H': 0, 'O': 1}) assert result is False and target_atoms == [2] result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure, max_atoms_added=1, max_by_species={'H': 2, 'O': 0}) assert result is False and target_atoms == [1] result, target_atoms = \ is_std_in_bound_per_species(rel_std_tolerance=1, abs_std_tolerance=.1, noise=2.9, structure=test_structure, max_atoms_added=3, max_by_species={'H': 2, 'O': 0}) assert result is False and target_atoms == [1, 0]
def test_force_in_bound_per_species(): test_structure, _ = get_random_structure(np.eye(3), ['H', 'O'], 3) test_structure.species_labels = ['H', 'H', 'O'] test_structure.forces = np.array([[0, 0, 0], [2, 0, 0], [3, 0, 0]]) true_forces = np.array([[.001, 0, 0], [1, 0, 0], [5, 0, 0]]) # Test that 'test mode' works result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=0, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure) assert result is True and target_atoms == [-1] # Test that high abs tolerance works result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=10, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure) assert result is True and target_atoms == [-1] # Test that low abs tolerance works result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=1.5, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure) assert result is False and target_atoms == [2] # Test that the max atoms added works result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=.01, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=2) assert result is False and target_atoms == [2, 1] result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=.00001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=1) assert result is False and target_atoms == [2] # Test that max by species works result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=.00001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=1, max_by_species={'H': 1, 'O': 0}) assert result is False and target_atoms == [1] result, target_atoms = \ is_force_in_bound_per_species(abs_force_tolerance=.00001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=1, max_by_species={'H': 0, 'O': 1}) assert result is False and target_atoms == [2] # Test max force error feature result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=.9, max_force_error=.00000000001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is True and target_atoms == [-1] result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=.9, max_force_error=1.5, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is False and target_atoms == [1]
def test_std_in_bound_per_species(): test_structure, _ = get_random_structure(np.eye(3), ["H", "O"], 3) test_structure.species_labels = ["H", "H", "O"] test_structure.stds = np.array([[1, 0, 0], [2, 0, 0], [3, 0, 0]]) # Test that 'test mode' works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=0, abs_std_tolerance=0, noise=0, structure=test_structure ) assert result is True and target_atoms == [-1] # Test that high abs tolerance works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=0, abs_std_tolerance=4, noise=0, structure=test_structure ) assert result is True and target_atoms == [-1] # Test that low abs tolerance works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=0, abs_std_tolerance=2.9, noise=0, structure=test_structure ) assert result is False and target_atoms == [2] # Test that high rel tolerance works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0, noise=4, structure=test_structure ) assert result is True and target_atoms == [-1] # Test that low rel tolerance works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0, noise=2.9, structure=test_structure ) assert result is False and target_atoms == [2] # Test that both work result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure ) assert result is False and target_atoms == [2, 1, 0] # Test that the max atoms added works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=2, ) assert result is False and target_atoms == [2, 1] result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=1, ) assert result is False and target_atoms == [2] # Test that max by species works result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=1, max_by_species={"H": 1, "O": 0}, ) assert result is False and target_atoms == [1] result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=1, max_by_species={"H": 0, "O": 1}, ) assert result is False and target_atoms == [2] result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=1, max_by_species={"H": 2, "O": 0}, ) assert result is False and target_atoms == [1] result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=3, max_by_species={"H": 2, "O": 0}, ) assert result is False and target_atoms == [1, 0] # Ensure NANS can be handled test_structure.stds = np.array([[np.nan, np.nan, np.nan], [2, 0, 0], [3, 0, 0]]) result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=3, max_by_species={"H": 2, "O": 0}, ) assert result is False and target_atoms == [1] test_structure.stds = np.array( [[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [3, 0, 0]] ) result, target_atoms = is_std_in_bound_per_species( rel_std_tolerance=1, abs_std_tolerance=0.1, noise=2.9, structure=test_structure, max_atoms_added=3, max_by_species={"H": 2, "O": 0}, ) assert result is True and target_atoms == [-1]
def test_force_in_bound_per_species(): test_structure, _ = get_random_structure(np.eye(3), ["H", "O"], 3) test_structure.species_labels = ["H", "H", "O"] test_structure.forces = np.array([[0, 0, 0], [2, 0, 0], [3, 0, 0]]) true_forces = np.array([[0.001, 0, 0], [1, 0, 0], [5, 0, 0]]) # Test that 'test mode' works result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is True and target_atoms == [-1] # Test that high abs tolerance works result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=10, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is True and target_atoms == [-1] # Test that low abs tolerance works result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=1.5, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is False and target_atoms == [2] # Test that the max atoms added works result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.01, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=2, ) assert result is False and target_atoms == [2, 1] result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.00001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=1, ) assert result is False and target_atoms == [2] # Test that max by species works result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.00001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=1, max_by_species={"H": 1, "O": 0}, ) assert result is False and target_atoms == [1] result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.00001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, max_atoms_added=1, max_by_species={"H": 0, "O": 1}, ) assert result is False and target_atoms == [2] # Test max force error feature result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.9, max_force_error=0.00000000001, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is True and target_atoms == [-1] result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.9, max_force_error=1.5, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is False and target_atoms == [1] # Ensure that nans can be handled test_structure.forces = np.array([[np.nan, np.nan, np.nan], [2, 0, 0], [3, 0, 0]]) result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=1, max_force_error=1.5, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is False and target_atoms == [1] result, target_atoms = is_force_in_bound_per_species( abs_force_tolerance=0.9, max_force_error=500, predicted_forces=test_structure.forces, label_forces=true_forces, structure=test_structure, ) assert result is False and set(target_atoms) == set([1, 2])