File: test_slice_sequence.py

package info (click to toggle)
dials 3.25.0%2Bdfsg3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,112 kB
  • sloc: python: 134,740; cpp: 34,526; makefile: 160; sh: 142
file content (180 lines) | stat: -rw-r--r-- 5,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import annotations

import shutil
import subprocess

import pytest

from dxtbx.serialize import load

from dials.array_family import flex


def test_slice_sequence_and_compare_with_expected_results(dials_data, tmp_path):
    data_dir = dials_data("refinement_test_data", pathlib=True)
    experiments_path = data_dir / "i04-weak.json"
    pickle_path = data_dir / "i04-weak.pickle"

    assert experiments_path.is_file()
    assert pickle_path.is_file()

    result = subprocess.run(
        [
            shutil.which("dials.slice_sequence"),
            experiments_path,
            pickle_path,
            "image_range=1 20",
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr

    # load results
    sliced_exp = load.experiment_list(
        tmp_path / "i04-weak_1_20.expt", check_format=False
    )[0]
    sliced_refs = flex.reflection_table.from_file(tmp_path / "i04-weak_1_20.refl")

    # simple test of results
    assert sliced_exp.scan.get_image_range() == (1, 20)
    assert len(sliced_refs) == 3672


def test_slice_sequence_with_first_images_missing(dials_data, tmp_path):
    """Test slicing where scan image range does not start at 1, exercising
    a case that exposed a bug"""

    data_dir = dials_data("refinement_test_data", pathlib=True)
    experiments_path = data_dir / "i04-weak.json"

    # first slice
    result = subprocess.run(
        [shutil.which("dials.slice_sequence"), experiments_path, "image_range=5,20"],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr

    # second slice
    result = subprocess.run(
        [
            shutil.which("dials.slice_sequence"),
            "i04-weak_5_20.expt",
            "image_range=10,20",
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr

    sliced_exp = load.experiment_list(
        tmp_path / "i04-weak_5_20_10_20.expt", check_format=False
    )[0]
    assert sliced_exp.scan.get_image_range() == (10, 20)
    assert sliced_exp.scan.get_array_range() == (9, 20)
    assert sliced_exp.scan.get_oscillation()[0] == pytest.approx(83.35)


def test_slice_sequence_to_degree_blocks(dials_data, tmp_path):
    """Slice data into 10 degree blocks i.e. 17 datasets"""
    expt = dials_data("l_cysteine_4_sweeps_scaled", pathlib=True) / "scaled_30.expt"
    refl = dials_data("l_cysteine_4_sweeps_scaled", pathlib=True) / "scaled_30.refl"
    subprocess.run(
        [
            shutil.which("dials.slice_sequence"),
            "block_size=10",
            "output.experiments=sliced.expt",
            "output.reflections=sliced.refl",
            expt,
            refl,
        ],
        cwd=tmp_path,
        capture_output=True,
    )

    sliced_expts = load.experiment_list(tmp_path / "sliced.expt", check_format=False)
    assert len(sliced_expts) == 17
    sliced_refl = flex.reflection_table.from_file(tmp_path / "sliced.refl")
    assert len(set(sliced_refl.experiment_identifiers().values())) == 17
    sliced_refl.assert_experiment_identifiers_are_consistent(sliced_expts)


def test_slice_sequence_with_scan_varying_crystal(dials_data, tmp_path):
    """test slicing keeps a scan-varying crystal"""

    expt = dials_data("l_cysteine_4_sweeps_scaled", pathlib=True) / "scaled_30.expt"
    subprocess.run(
        [
            shutil.which("dials.slice_sequence"),
            "image_range=10,20",
            "output.experiments=sliced.expt",
            expt,
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    orig = load.experiment_list(expt, check_format=False)[0]

    sliced = load.experiment_list(tmp_path / "sliced.expt", check_format=False)[0]

    assert sliced.crystal.num_scan_points == 12

    orig_UB = [
        orig.crystal.get_A_at_scan_point(i) for i in range(orig.crystal.num_scan_points)
    ]
    sliced_UB = [
        sliced.crystal.get_A_at_scan_point(i)
        for i in range(sliced.crystal.num_scan_points)
    ]

    for a, b in zip(orig_UB[9:21], sliced_UB):
        assert a == pytest.approx(b)


def test_slice_sequence_exclude_images_multiple(dials_data, tmp_path):
    data_dir = dials_data("refinement_test_data", pathlib=True)
    experiments_path = data_dir / "i04-weak.json"
    pickle_path = data_dir / "i04-weak.pickle"

    assert experiments_path.is_file()
    assert pickle_path.is_file()

    result = subprocess.run(
        [
            shutil.which("dials.slice_sequence"),
            experiments_path,
            pickle_path,
            "exclude_images_multiple=40",
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr

    # load results
    sliced_expts = load.experiment_list(
        tmp_path / "i04-weak_sliced.expt", check_format=False
    )
    sliced_refls = flex.reflection_table.from_file(tmp_path / "i04-weak_sliced.refl")

    # simple test of results
    assert len(sliced_expts) == 14
    assert len(sliced_refls) == 100397
    image_ranges = [e.scan.get_image_range() for e in sliced_expts]
    assert image_ranges == [
        (1, 39),
        (41, 79),
        (81, 119),
        (121, 159),
        (161, 199),
        (201, 239),
        (241, 279),
        (281, 319),
        (321, 359),
        (361, 399),
        (401, 439),
        (441, 479),
        (481, 519),
        (521, 540),
    ]