Passed
Push — master ( 59f8ea...6a3260 )
by Daniel
03:08
created

test_CifReader.test_periodicset_from_ase_atoms()   B

Complexity

Conditions 8

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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