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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
|
from __future__ import annotations
import json
import math
import os
import shutil
import subprocess
import sys
import pytest
from dxtbx.serialize import load
from dials.algorithms.integration.processor import _average_bbox_size
from dials.array_family import flex
def test_basic_integrate(dials_data, tmp_path):
# Call dials.integrate
exp = load.experiment_list(
dials_data("centroid_test_data", pathlib=True) / "experiments.json"
)
exp[0].identifier = "foo"
exp.as_json(tmp_path / "modified_input.json")
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"modified_input.json",
"profile.fitting=False",
"integration.integrator=3d",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert experiments[0].identifier == "foo"
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
mask = table.get_flags(table.flags.integrated, all=False)
assert len(table) == 1666
assert mask.count(True) == 1666
assert "id" in table
for row in table.rows():
assert row["id"] == 0
assert dict(table.experiment_identifiers()) == {0: "foo"}
originaltable = table
(tmp_path / "integrated.refl").unlink()
for i in range(1, 10):
source = dials_data("centroid_test_data", pathlib=True) / f"centroid_000{i}.cbf"
destination = tmp_path / f"centroid_001{i}.cbf"
try:
destination.symlink_to(source)
except OSError:
shutil.copyfile(source, destination)
with (
dials_data("centroid_test_data", pathlib=True)
.joinpath("experiments.json")
.open("r") as fh
):
j = json.load(fh)
assert j["scan"][0]["image_range"] == [1, 9]
j["scan"][0]["image_range"] = [11, 19]
assert j["scan"][0]["oscillation"] == [0.0, 0.2]
j["scan"][0]["oscillation"] = [360.0, 0.2]
j["experiment"][0]["identifier"] = "bar"
with (tmp_path / "models.expt").open("w") as fh:
json.dump(j, fh)
# Call dials.integrate
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"models.expt",
"profile.fitting=False",
"integration.integrator=3d",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert experiments[0].identifier == "bar"
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert dict(table.experiment_identifiers()) == {0: "bar"}
mask1 = table.get_flags(table.flags.integrated, all=False)
assert len(table) == 1666
assert mask1.count(True) == 1666
mask2 = originaltable.get_flags(table.flags.integrated, all=False)
assert mask1.all_eq(mask2)
t1 = table.select(mask1)
t2 = originaltable.select(mask1)
Cal_P1 = t1["xyzcal.mm"].parts()[2]
Cal_Z1 = t1["xyzcal.px"].parts()[2]
Obs_Z1 = t1["xyzobs.px.value"].parts()[2]
# Obs_P1 = t1['xyzobs.mm.value'].parts()[2]
Cal_Z2 = t2["xyzcal.px"].parts()[2]
Cal_P2 = t2["xyzcal.mm"].parts()[2]
Obs_Z2 = t2["xyzobs.px.value"].parts()[2]
# Obs_P2 = t2['xyzobs.mm.value'].parts()[2]
diff_I = t1["intensity.sum.value"] - t2["intensity.sum.value"]
diff_Cal_Z = Cal_Z1 - (Cal_Z2 + 10)
diff_Obs_Z = Obs_Z1 - (Obs_Z2 + 10)
diff_Cal_P = Cal_P1 - (Cal_P2 + 2 * math.pi)
# diff_Obs_P = Obs_P1 - (Obs_P2 + 2*math.pi)
assert flex.abs(diff_I).all_lt(1e-7)
assert flex.abs(diff_Cal_Z).all_lt(1e-7)
assert flex.abs(diff_Cal_P).all_lt(1e-7)
assert flex.abs(diff_Obs_Z).all_lt(1e-7)
# assert(flex.abs(diff_Obs_P).all_lt(1e-7))
@pytest.mark.parametrize(
("block_size", "block_units"),
[(None, None), (1, "degrees"), (2, "frames"), (1, "frames")],
)
def test_basic_blocking_options(dials_data, tmp_path, block_size, block_units):
exp = load.experiment_list(
dials_data("centroid_test_data", pathlib=True) / "experiments.json"
)
exp[0].identifier = "foo"
exp.as_json(tmp_path / "modified_input.json")
args = [shutil.which("dials.integrate"), "modified_input.json", "nproc=2"]
if block_size:
args.append(f"block.size={block_size}")
if block_units:
args.append(f"block.units={block_units}")
result = subprocess.run(args, cwd=tmp_path, capture_output=True)
assert not result.returncode and not result.stderr
@pytest.mark.skip(reason="3d threaded integrator")
def test_basic_threaded_integrate(dials_data, tmp_path):
"""Test the threaded integrator on single imageset data."""
expts = dials_data("centroid_test_data", pathlib=True) / "indexed.expt"
refls = dials_data("centroid_test_data", pathlib=True) / "indexed.refl"
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"integration.integrator=3d_threaded",
"background.algorithm=glm",
"njobs=2",
"nproc=2",
refls,
expts,
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
assert tmp_path.joinpath("integrated.refl").is_file()
assert tmp_path.joinpath("integrated.expt").is_file()
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert table.size() == 3526
assert set(table["id"]) == {0}
assert table.select(table["id"] == 0).size() == 3526
def test_basic_integrate_output_integrated_only(dials_data, tmp_path):
exp = load.experiment_list(
dials_data("centroid_test_data", pathlib=True) / "experiments.json"
)
exp[0].identifier = "bar"
exp.as_json(tmp_path / "modified_input.json")
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"modified_input.json",
"profile.fitting=False",
"integration.integrator=3d",
"output_unintegrated_reflections=False",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert experiments[0].identifier == "bar"
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
mask = table.get_flags(table.flags.integrated, all=False)
assert len(table) == 1666
assert mask.count(False) == 0
assert "id" in table
for row in table.rows():
assert row["id"] == 0
assert dict(table.experiment_identifiers()) == {0: "bar"}
def test_integration_with_sampling(dials_data, tmp_path):
exp = load.experiment_list(
dials_data("centroid_test_data", pathlib=True) / "experiments.json"
)
exp[0].identifier = "foo"
exp.as_json(tmp_path / "modified_input.json")
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"modified_input.json",
"profile.fitting=False",
"sampling.integrate_all_reflections=False",
"sampling.random_seed=42",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert experiments[0].identifier == "foo"
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
# account for random number generator in sampling
assert len(table) == 839
assert dict(table.experiment_identifiers()) == {0: "foo"}
def test_integration_with_sample_size(dials_data, tmp_path):
exp = load.experiment_list(
dials_data("centroid_test_data", pathlib=True) / "experiments.json"
)
exp[0].identifier = "foo"
exp.as_json(tmp_path / "modified_input.json")
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"modified_input.json",
"profile.fitting=False",
"sampling.integrate_all_reflections=False",
"sampling.random_seed=42",
"sampling.minimum_sample_size=500",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert experiments[0].identifier == "foo"
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert len(table) == 415
assert dict(table.experiment_identifiers()) == {0: "foo"}
def test_integration_with_image_exclusions(dials_data, tmp_path):
exp = load.experiment_list(
dials_data("centroid_test_data", pathlib=True) / "experiments.json"
)
exp[0].identifier = "foo"
exp.as_json(tmp_path / "modified_input.json")
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"modified_input.json",
"profile.fitting=False",
"exclude_images=4:6",
"sampling.random_seed=42",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert experiments[0].identifier == "foo"
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert len(table) == 91
# NB the excluded range of centroids is actually wider than the
# exclude_images specification, because reflections that extend onto the
# excluded images are also rejected.
_, _, z = table["xyzcal.px"].parts()
assert len(z.select((z > 3) & (z < 7))) == 0
def test_imageset_id_output_with_multi_sweep(dials_data, tmp_path):
"""Test that imageset ids are correctly output for multi-sweep integration."""
# Just integrate 15 images for each sweep
images1 = dials_data("l_cysteine_dials_output", pathlib=True) / "l-cyst_01_000*.cbf"
images2 = dials_data("l_cysteine_dials_output", pathlib=True) / "l-cyst_02_000*.cbf"
result = subprocess.run(
[shutil.which("dials.import"), images1, images2],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
result = subprocess.run(
[shutil.which("dials.find_spots"), tmp_path / "imported.expt", "nproc=1"],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
result = subprocess.run(
[
shutil.which("dials.index"),
tmp_path / "imported.expt",
tmp_path / "strong.refl",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
tmp_path / "indexed.expt",
tmp_path / "indexed.refl",
"profile.fitting=False",
"gaussian_rs.min_spots.overall=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert set(table["imageset_id"]) == {0, 1}
# check that we have approx 50% in each
n0 = (table["imageset_id"] == 0).count(True)
n = table.size()
n1 = (table["imageset_id"] == 1).count(True)
assert (n0 / n > 0.4) and (n0 / n < 0.6)
assert (n1 / n > 0.4) and (n1 / n < 0.6)
# now try again with adding unintegrated reflections
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
tmp_path / "indexed.expt",
tmp_path / "indexed.refl",
"profile.fitting=False",
"gaussian_rs.min_spots.overall=0",
"output_unintegrated_reflections=False",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert set(table["imageset_id"]) == {0, 1}
# check that we have approx 50% in each
n0 = (table["imageset_id"] == 0).count(True)
n = table.size()
n1 = (table["imageset_id"] == 1).count(True)
assert (n0 / n > 0.4) and (n0 / n < 0.6)
assert (n1 / n > 0.4) and (n1 / n < 0.6)
def test_basic_integration_with_profile_fitting(dials_data, tmp_path):
expts = dials_data("centroid_test_data", pathlib=True) / "indexed.expt"
refls = dials_data("centroid_test_data", pathlib=True) / "indexed.refl"
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
expts,
refls,
"profile.fitting=True",
"sampling.integrate_all_reflections=False",
"sampling.random_seed=42",
"sampling.minimum_sample_size=500",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert len(table) == 436
prf = table.get_flags(table.flags.integrated_prf)
zero = table["intensity.prf.value"] == 0.0
prf_and_zero = prf & zero
assert prf_and_zero.count(True) == 0
@pytest.mark.xfail(
sys.platform == "darwin",
reason="Not understood apparent platform numeric differences",
)
def test_multi_sweep(dials_data, tmp_path):
expts = str(
dials_data("centroid_test_data", pathlib=True) / "multi_sweep_indexed.expt"
)
refls = str(
dials_data("centroid_test_data", pathlib=True) / "multi_sweep_indexed.refl"
)
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
expts,
refls,
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
assert (tmp_path / "integrated.refl").is_file()
assert (tmp_path / "integrated.expt").is_file()
experiments = load.experiment_list(tmp_path / "integrated.expt")
for i, expt in enumerate(experiments):
assert expt.identifier == str(100 + i)
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert len(table) == 3530
assert dict(table.experiment_identifiers()) == {0: "100", 1: "101"}
# Check the results
T1 = table[:1765]
T2 = table[1765:]
ID1 = list(set(T1["id"]))
ID2 = list(set(T2["id"]))
assert len(ID1) == 1
assert len(ID2) == 1
assert ID1[0] == 0
assert ID2[0] == 1
I1 = T1["intensity.prf.value"]
I2 = T2["intensity.prf.value"]
F1 = T1.get_flags(T1.flags.integrated_prf)
F2 = T2.get_flags(T2.flags.integrated_prf)
assert F1 == F2
I1 = I1.select(F1)
I2 = I2.select(F2)
assert flex.abs(I1 - I2) < 1e-6
def test_multi_lattice(dials_data, tmp_path):
expt = str(dials_data("trypsin_multi_lattice", pathlib=True) / "refined.expt")
refl = str(dials_data("trypsin_multi_lattice", pathlib=True) / "refined.refl")
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"d_min=4",
"prediction.padding=0",
expt,
refl,
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
assert (tmp_path / "integrated.refl").is_file()
assert (tmp_path / "integrated.expt").is_file()
# Expected identifiers (should be unchanged from the input)
expected_identifiers = {
0: "54924b82-9a0e-4b39-840d-0e14b780534b",
1: "6c80adf7-3e6f-47a6-abca-916ec3c056a7",
2: "7a8dbd6a-11be-429a-ba1f-2d855babaaa5",
3: "1b0937cc-e16d-4778-b5b5-4ddc0f772b91",
4: "6c745eeb-b835-4c4e-9a31-348ecd4607d8",
5: "1584899c-8f81-4d82-b093-44abe1a6f859",
}
experiments = load.experiment_list(tmp_path / "integrated.expt")
assert [e.identifier for e in experiments] == list(expected_identifiers.values())
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert len(table) == 4020
assert dict(table.experiment_identifiers()) == expected_identifiers
# all 6 experiments should have an imageset_id of zero as they share an imageset
assert set(table["imageset_id"]) == {0}
# Check output contains from six lattices
exp_id = list(set(table["id"]))
assert len(exp_id) == 6
# Check all lattices have integrated reflections
mask = table.get_flags(table.flags.integrated_prf)
table = table.select(mask)
exp_id = list(set(table["id"]))
assert len(exp_id) == 6
def test_output_rubbish(dials_data, tmp_path):
result = subprocess.run(
[
shutil.which("dials.index"),
dials_data("centroid_test_data", pathlib=True)
/ "imported_experiments.json",
dials_data("centroid_test_data", pathlib=True) / "strong.pickle",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
assert (tmp_path / "indexed.expt").is_file()
assert (tmp_path / "indexed.refl").is_file()
# Call dials.integrate
result = subprocess.run(
[
shutil.which("dials.integrate"),
"nproc=1",
"indexed.expt",
"indexed.refl",
"profile.fitting=False",
"prediction.padding=0",
],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
assert (tmp_path / "integrated.refl").is_file()
table = flex.reflection_table.from_file(tmp_path / "integrated.refl")
assert "id" in table
for row in table.rows():
assert row["id"] == 0
assert list(table.experiment_identifiers().keys()) == [0]
assert list(table.experiment_identifiers().values()) # not empty
def test_integrate_with_kapton(dials_data, tmp_path):
data_dir = dials_data("integration_test_data", pathlib=True)
refl_path = str(data_dir / "kapton-idx-20161021225550223_indexed.refl")
expt_path = str(data_dir / "kapton-idx-20161021225550223_refined.expt")
image_path = str(data_dir / "kapton-20161021225550223.pickle")
mask_path = str(data_dir / "kapton-mask.pickle")
assert os.path.exists(refl_path)
assert os.path.exists(expt_path)
shutil.copy(refl_path, tmp_path)
shutil.copy(image_path, tmp_path)
templ_phil = """
output {
experiments = 'idx-20161021225550223_integrated_experiments_%s.expt'
reflections = 'idx-20161021225550223_integrated_%s.refl'
}
integration {
lookup.mask = '%s'
integrator = stills
profile.fitting = False
background.algorithm = simple
debug {
output = True
separate_files = False
split_experiments = False
}
}
profile {
gaussian_rs.min_spots.overall = 0
}
absorption_correction {
apply = %s
algorithm = fuller_kapton
fuller_kapton {
smart_sigmas = True
}
}
"""
without_kapton_phil = templ_phil % (
"nokapton",
"nokapton",
mask_path,
"False",
)
with_kapton_phil = templ_phil % (
"kapton",
"kapton",
mask_path,
"True",
)
(tmp_path / "integrate_without_kapton.phil").write_text(without_kapton_phil)
(tmp_path / "integrate_with_kapton.phil").write_text(with_kapton_phil)
# Call dials.integrate with and without kapton correction
for phil in "integrate_without_kapton.phil", "integrate_with_kapton.phil":
result = subprocess.run(
[shutil.which("dials.integrate"), "nproc=1", refl_path, expt_path, phil],
cwd=tmp_path,
capture_output=True,
)
assert not result.returncode and not result.stderr
results = []
for mode in "kapton", "nokapton":
table = flex.reflection_table.from_file(
tmp_path / f"idx-20161021225550223_integrated_{mode}.refl"
)
millers = table["miller_index"]
test_indices = {"zero": (-5, 2, -6), "low": (-2, -20, 7), "high": (-1, -10, 4)}
test_rows = {k: millers.first_index(v) for k, v in test_indices.items()}
test_I_sigsqI = {
k: (table[v]["intensity.sum.value"], table[v]["intensity.sum.variance"])
for k, v in test_rows.items()
}
results.append(test_I_sigsqI)
assert results[0]["zero"][0] == results[1]["zero"][0]
assert results[0]["zero"][1] - results[1]["zero"][1] < 0.0001
assert False not in [results[0]["low"][i] > results[1]["low"][i] for i in (0, 1)]
assert False not in [results[0]["high"][i] > results[1]["high"][i] for i in (0, 1)]
def test_average_bbox_size():
"""Test behaviour of function for obtaining average bbox size."""
reflections = flex.reflection_table()
reflections["bbox"] = flex.int6(*(flex.int(10, i) for i in range(6)))
assert _average_bbox_size(reflections) == (1, 1, 1)
|