1
|
|
|
import pytest |
2
|
|
|
import amd |
3
|
|
|
import warnings |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
@pytest.fixture(scope='module') |
7
|
|
|
def test_cif_paths(data_dir): |
8
|
|
|
cif_names = ['cubic', 'T2_experimental'] |
9
|
|
|
return {name: str(data_dir / f'{name}.cif') for name in cif_names} |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
@pytest.mark.parametrize('reader', ['gemmi', 'pymatgen', 'ase', 'ccdc']) |
13
|
|
|
def test_CifReader(reader, test_cif_paths, reference_data): |
14
|
|
|
for name in test_cif_paths: |
15
|
|
|
try: |
16
|
|
|
read_in = list(amd.CifReader(test_cif_paths[name], reader=reader)) |
17
|
|
|
except ImportError: |
18
|
|
|
pytest.skip( |
19
|
|
|
f'Skipping test_CifReader[{reader}] as {reader} failed to ' |
20
|
|
|
'import.' |
21
|
|
|
) |
22
|
|
|
references = reference_data[name] |
23
|
|
|
if (not len(references) == len(read_in)) or len(read_in) == 0: |
24
|
|
|
pytest.fail( |
25
|
|
|
f'There are {len(references)} references, but {len(read_in)}' |
26
|
|
|
f'structures were read from {name}.' |
27
|
|
|
) |
28
|
|
|
for s, s_ in zip(read_in, references): |
29
|
|
|
if not s == s_['PeriodicSet']: |
30
|
|
|
pytest.fail( |
31
|
|
|
f'Structure {name}:{s.name} read with CifReader disagrees ' |
32
|
|
|
'with reference.' |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_periodicset_from_ase_atoms(test_cif_paths, reference_data): |
37
|
|
|
try: |
38
|
|
|
from ase.io import iread |
39
|
|
|
except ImportError: |
40
|
|
|
pytest.skip( |
41
|
|
|
'Skipping test_periodicset_from_ase_atoms as ase failed to import.' |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
for name in test_cif_paths: |
45
|
|
|
references = reference_data[name] |
46
|
|
|
# ignore ase warning: "crystal system x is not interpreted for space |
47
|
|
|
# group Spacegroup(1, setting=1). This may result in wrong setting!" |
48
|
|
|
with warnings.catch_warnings(): |
49
|
|
|
warnings.simplefilter('ignore') |
50
|
|
|
atoms = list(iread(test_cif_paths[name])) |
51
|
|
|
read_in = [amd.periodicset_from_ase_atoms(a) for a in atoms] |
52
|
|
|
if (not len(references) == len(read_in)) or len(read_in) == 0: |
53
|
|
|
pytest.fail( |
54
|
|
|
f'There are {len(references)} references, but {len(read_in)}' |
55
|
|
|
f'structures were read from {name}.' |
56
|
|
|
) |
57
|
|
|
|
58
|
|
|
for s, s_ in zip(read_in, references): |
59
|
|
|
if not s == s_['PeriodicSet']: |
60
|
|
|
n = s_['PeriodicSet'].name |
61
|
|
|
pytest.fail( |
62
|
|
|
'Structure read with ase.io.iread disagrees with ' |
63
|
|
|
f'reference ({n}).' |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_CifReader_equiv_structs(data_dir): |
68
|
|
|
reader = amd.CifReader(data_dir / 'OJIGOG.cif', show_warnings=False) |
69
|
|
|
pdds = [amd.PDD(struct, 100) for struct in reader] |
70
|
|
|
if amd.emd(pdds[0], pdds[1]) > 0: |
71
|
|
|
pytest.fail( |
72
|
|
|
'Asymmetric structure was read differently than identical ' |
73
|
|
|
'expanded version.' |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_equiv_sites(data_dir): |
78
|
|
|
reader = amd.CifReader(data_dir / 'BABMUQ.cif', show_warnings=False) |
79
|
|
|
pdds = [amd.PDD(s, 100) for s in reader] |
80
|
|
|
if amd.PDD_pdist(pdds): |
81
|
|
|
pytest.fail('Equivalent structures by symmetry differ by PDD.') |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def test_heaviest_component(data_dir, ccdc_enabled): |
85
|
|
|
|
86
|
|
|
if not ccdc_enabled: |
87
|
|
|
pytest.skip( |
88
|
|
|
'Skipping test_CSDReader as csd-python-api failed to import.' |
89
|
|
|
) |
90
|
|
|
|
91
|
|
|
s = amd.CifReader( |
92
|
|
|
data_dir / 'T2-alpha-solvent.cif', |
93
|
|
|
reader='ccdc', |
94
|
|
|
disorder='all_sites', |
95
|
|
|
heaviest_component=True |
96
|
|
|
).read() |
97
|
|
|
|
98
|
|
|
if not s.asymmetric_unit.shape[0] == 26: |
99
|
|
|
pytest.fail('Heaviest component test failed.') |
100
|
|
|
|