Skip to content

Commit

Permalink
Add tests for rs.utils.apply_to_hkl() use cases that yield fraction…
Browse files Browse the repository at this point in the history
…al HKLs
  • Loading branch information
JBGreisman committed Aug 1, 2022
1 parent aeac62c commit 8639a86
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

import gemmi
import numpy as np
import pandas as pd
import pytest

import reciprocalspaceship as rs


@pytest.fixture
def hkls():
"""
Return all Miller indices with H, K, L values between [-5, 5]
"""
hmin, hmax = -5, 5
H = np.mgrid[hmin : hmax + 1, hmin : hmax + 1, hmin : hmax + 1].reshape((3, -1)).T
return H


@pytest.fixture
def dataset_hkl():
"""
Expand Down
32 changes: 32 additions & 0 deletions tests/utils/test_symop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import gemmi
import numpy as np
import pytest

import reciprocalspaceship as rs


@pytest.mark.parametrize("H_even", [True, False])
@pytest.mark.parametrize(
"op_str", ["x,y,z", "2*x,2*y,2*z", "x,z,y", "1/2*x,y,z", "1/2*x,1/2*y,1/2*z"]
)
def test_apply_to_hkl(hkls, H_even, op_str):
"""
Test rs.utils.apply_to_hkl() detects symops that yield fractional Miller indices.
apply_to_hkl() should raise a RuntimeError if the combination of `H` and `op`
yield fractional Miller indices, and should return new Miller indices all other
cases.
"""
if H_even:
hkls = hkls[~np.any(hkls % 2, axis=1)]

op = gemmi.Op(op_str)

if ((np.array(op.rot) / op.DEN) % 1 == 0).all() or H_even:
H_result = rs.utils.apply_to_hkl(hkls, op)
H_expected = np.array([op.apply_to_hkl(hkl) for hkl in hkls])
assert np.array_equal(H_expected, H_result)
assert H_result.dtype is np.dtype(np.int32)
else:
with pytest.raises(RuntimeError):
H_result = rs.utils.apply_to_hkl(hkls, op)

0 comments on commit 8639a86

Please sign in to comment.