def test_dials_integrater_high_pressure_set(monkeypatch):
    """Check that the appropriate PHIL parameter triggers high-pressure mode."""
    # Without the relevant PHIL parameter set, check everything is normal.
    integrater = DialsIntegrater()
    assert not integrater.high_pressure
    # Check we can trigger high-pressure mode with the relevant PHIL parameter.
    monkeypatch.setattr(PhilIndex.params.dials.high_pressure, "correction",
                        True)
    integrater = DialsIntegrater()
    assert integrater.high_pressure
Beispiel #2
0
def Integrater():
    """Return an  Integrater implementation."""

    # FIXME this should take an indexer as an argument...

    integrater = None
    preselection = get_preferences().get("integrater")

    if not integrater and (not preselection or preselection == "dials"):
        try:
            integrater = DialsIntegrater()
            logger.debug("Using Dials Integrater")
            if PhilIndex.params.xia2.settings.scaler == "dials":
                integrater.set_output_format("pickle")
        except NotAvailableError:
            if preselection == "dials":
                raise RuntimeError(
                    "preselected integrater dials not available: "
                    + "dials not installed?"
                )

    if not integrater and (not preselection or preselection == "xdsr"):
        try:
            integrater = XDSIntegrater()
            logger.debug("Using XDS Integrater in new resolution mode")
        except NotAvailableError:
            if preselection == "xdsr":
                raise RuntimeError(
                    "preselected integrater xdsr not available: " + "xds not installed?"
                )

    if not integrater:
        raise RuntimeError("no integrater implementations found")

    # check to see if resolution limits were passed in through the
    # command line...

    dmin = PhilIndex.params.xia2.settings.resolution.d_min
    dmax = PhilIndex.params.xia2.settings.resolution.d_max

    if dmin:
        logger.debug("Adding user-assigned resolution limits:")

        if dmax:

            logger.debug(f"dmin: {dmin:.3f} dmax: {dmax:.2f}")
            integrater.set_integrater_resolution(dmin, dmax, user=True)

        else:

            logger.debug("dmin: %.3f" % dmin)
            integrater.set_integrater_high_resolution(dmin, user=True)

    return integrater
Beispiel #3
0
def exercise_serialization(dials_regression, tmp_dir):
    xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
    template = os.path.join(xia2_demo_data, "insulin_1_###.img")

    os.chdir(tmp_dir)

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

    from dxtbx.datablock import DataBlockTemplateImporter
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]

    from xia2.Schema.XProject import XProject
    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    proj = XProject()
    proj._name = "PROJ1"
    cryst = XCrystal("CRYST1", proj)
    proj.add_crystal(cryst)
    wav = XWavelength("WAVE1", cryst, wavelength=0.98)
    samp = XSample("X1", cryst)
    cryst.add_wavelength(wav)
    cryst.set_ha_info({'atom': 'S'})
    directory, image = os.path.split(imageset.get_path(1))
    wav.add_sweep(name='SWEEP1', sample=samp, directory=directory, image=image)

    import json
    from dxtbx.serialize.load import _decode_dict

    sweep = wav.get_sweeps()[0]
    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    indexer.add_indexer_imageset(imageset)
    indexer.set_indexer_sweep(sweep)
    sweep._indexer = indexer

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    sweep._refiner = refiner

    integrater = DialsIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    #integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_epoch(sweep.get_epoch(1))
    integrater.set_integrater_sweep_name(sweep.get_name())
    integrater.set_integrater_project_info(cryst.get_name(), wav.get_name(),
                                           sweep.get_name())
    sweep._integrater = integrater

    scaler = CCP4ScalerA()
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info(cryst.get_name(), wav.get_name())
    scaler._scalr_xcrystal = cryst
    cryst._scaler = scaler

    s_dict = sweep.to_dict()
    s_str = json.dumps(s_dict, ensure_ascii=True)
    s_dict = json.loads(s_str, object_hook=_decode_dict)
    xsweep = XSweep.from_dict(s_dict)

    w_dict = wav.to_dict()
    w_str = json.dumps(w_dict, ensure_ascii=True)
    w_dict = json.loads(w_str, object_hook=_decode_dict)
    xwav = XWavelength.from_dict(w_dict)
    assert xwav.get_sweeps()[0].get_wavelength() is xwav

    c_dict = cryst.to_dict()
    c_str = json.dumps(c_dict, ensure_ascii=True)
    c_dict = json.loads(c_str, object_hook=_decode_dict)
    xcryst = XCrystal.from_dict(c_dict)
    assert xcryst.get_xwavelength(
        xcryst.get_wavelength_names()[0]).get_crystal() is xcryst

    p_dict = proj.to_dict()
    p_str = json.dumps(p_dict, ensure_ascii=True)
    p_dict = json.loads(p_str, object_hook=_decode_dict)
    xproj = XProject.from_dict(p_dict)
    assert xproj.get_crystals().values()[0].get_project() is xproj

    json_str = proj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.get_crystals().values()[0].get_project() is xproj
    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
    json_str = xproj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.get_crystals().values()[0].get_project() is xproj
    xcryst = xproj.get_crystals().values()[0]
    intgr = xcryst._get_integraters()[0]
    assert intgr.get_integrater_finish_done()
    assert xcryst._get_scaler()._sweep_handler.get_sweep_information(
        intgr.get_integrater_epoch()).get_integrater() is intgr

    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
Beispiel #4
0
def exercise_dials_integrater(nproc=None):
  if not have_dials_regression:
    print "Skipping exercise_dials_integrater(): dials_regression not configured"
    return

  if nproc is not None:
    from xia2.Handlers.Flags import Flags
    Flags.set_parallel(nproc)

  xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
  template = os.path.join(xia2_demo_data, "insulin_1_###.img")

  cwd = os.path.abspath(os.curdir)
  tmp_dir = os.path.abspath(open_tmp_directory())
  os.chdir(tmp_dir)

  from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
  from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
  from dxtbx.datablock import DataBlockTemplateImporter
  indexer = DialsIndexer()
  indexer.set_working_directory(tmp_dir)
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]
  indexer.add_indexer_imageset(imageset)

  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  cryst = XCrystal("CRYST1", None)
  wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
  samp = XSample("X1", cryst)
  directory, image = os.path.split(imageset.get_path(1))
  sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
  indexer.set_indexer_sweep(sweep)

  from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
  refiner = DialsRefiner()
  refiner.set_working_directory(tmp_dir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
  #refiner.refine()

  integrater = DialsIntegrater()
  integrater.set_working_directory(tmp_dir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.integrate()

  integrater_intensities = integrater.get_integrater_intensities()
  assert os.path.exists(integrater_intensities)
  from iotbx.reflection_file_reader import any_reflection_file
  reader = any_reflection_file(integrater_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 48117) < 100, mtz_object.n_reflections()
  assert mtz_object.column_labels() == [
    'H', 'K', 'L', 'M_ISYM', 'BATCH', 'IPR', 'SIGIPR', 'I', 'SIGI',
    'FRACTIONCALC', 'XDET', 'YDET', 'ROT', 'LP', 'DQE']

  assert integrater.get_integrater_wedge() == (1, 45)
  assert approx_equal(integrater.get_integrater_cell(),
                      (78.14, 78.14, 78.14, 90, 90, 90), eps=1e-1)

  # test serialization of integrater
  json_str = integrater.as_json()
  #print json_str
  integrater2 = DialsIntegrater.from_json(string=json_str)
  integrater2.set_integrater_sweep(sweep, reset=False)
  integrater2_intensities = integrater.get_integrater_intensities()
  assert integrater2_intensities == integrater_intensities

  integrater2.set_integrater_finish_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 48117) < 100, mtz_object.n_reflections()

  integrater2.set_integrater_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 48117) < 100, mtz_object.n_reflections()

  integrater2.set_integrater_prepare_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 48117) < 100, mtz_object.n_reflections()
Beispiel #5
0
def exercise_serialization(dials_data, tmp_dir):
    base_path = pathlib.Path(tmp_dir)
    template = dials_data("insulin").join("insulin_1_###.img").strpath

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]

    from xia2.Schema.XProject import XProject
    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    proj = XProject(base_path=base_path)
    proj._name = "PROJ1"
    cryst = XCrystal("CRYST1", proj)
    wav = XWavelength("WAVE1", cryst, wavelength=0.98)
    samp = XSample("X1", cryst)
    cryst.add_wavelength(wav)
    cryst.set_ha_info({"atom": "S"})
    cryst.add_sample(samp)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = wav.add_sweep(name="SWEEP1", sample=samp, directory=directory, image=image)
    samp.add_sweep(sweep)

    from dxtbx.serialize.load import _decode_dict

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    indexer.add_indexer_imageset(imageset)
    indexer.set_indexer_sweep(sweep)
    sweep._indexer = indexer

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    refiner.add_refiner_sweep(sweep)
    sweep._refiner = refiner

    integrater = DialsIntegrater()
    integrater.set_output_format("hkl")
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_epoch(sweep.get_epoch(1))
    integrater.set_integrater_sweep_name(sweep.get_name())
    integrater.set_integrater_project_info(
        cryst.get_name(), wav.get_name(), sweep.get_name()
    )
    sweep._integrater = integrater

    scaler = CCP4ScalerA(base_path=base_path)
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info(cryst.get_name(), wav.get_name())
    scaler._scalr_xcrystal = cryst
    cryst._scaler = scaler

    proj.add_crystal(cryst)

    s_dict = sweep.to_dict()
    s_str = json.dumps(s_dict, ensure_ascii=True)
    s_dict = json.loads(s_str, object_hook=_decode_dict)
    xsweep = XSweep.from_dict(s_dict)
    assert xsweep

    w_dict = wav.to_dict()
    w_str = json.dumps(w_dict, ensure_ascii=True)
    w_dict = json.loads(w_str, object_hook=_decode_dict)
    xwav = XWavelength.from_dict(w_dict)
    assert xwav.get_sweeps()[0].get_wavelength() is xwav

    c_dict = cryst.to_dict()
    c_str = json.dumps(c_dict, ensure_ascii=True)
    c_dict = json.loads(c_str, object_hook=_decode_dict)
    xcryst = XCrystal.from_dict(c_dict)
    assert (
        xcryst.get_xwavelength(xcryst.get_wavelength_names()[0]).get_crystal() is xcryst
    )

    p_dict = proj.to_dict()
    p_str = json.dumps(p_dict, ensure_ascii=True)
    p_dict = json.loads(p_str, object_hook=_decode_dict)
    xproj = XProject.from_dict(p_dict)
    assert xproj.path == base_path
    assert list(xproj.get_crystals().values())[0].get_project() is xproj
    assert list(xproj.get_crystals().values())[0]._scaler._base_path == base_path

    json_str = proj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.path == base_path
    assert list(xproj.get_crystals().values())[0].get_project() is xproj
    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
    json_str = xproj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.path == base_path
    # Test that we can serialize to json and back again
    xproj = XProject.from_json(string=xproj.as_json())
    assert xproj.path == base_path
    xcryst = list(xproj.get_crystals().values())[0]
    assert xcryst.get_project() is xproj
    intgr = xcryst._get_integraters()[0]
    assert intgr.get_integrater_finish_done()
    assert (
        xcryst._get_scaler()
        ._sweep_handler.get_sweep_information(intgr.get_integrater_epoch())
        .get_integrater()
        is intgr
    )

    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
Beispiel #6
0
def exercise_serialization():
  if not have_dials_regression:
    print "Skipping exercise_serialization(): dials_regression not configured"
    return

  from xia2.Handlers.CommandLine import CommandLine

  xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
  template = os.path.join(xia2_demo_data, "insulin_1_###.img")

  cwd = os.path.abspath(os.curdir)
  tmp_dir = os.path.abspath(open_tmp_directory())
  os.chdir(tmp_dir)

  from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
  from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
  from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
  from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

  from dxtbx.datablock import DataBlockTemplateImporter
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]

  from xia2.Schema.XProject import XProject
  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  proj = XProject()
  proj._name = "PROJ1"
  cryst = XCrystal("CRYST1", proj)
  proj.add_crystal(cryst)
  wav = XWavelength("WAVE1", cryst, wavelength=0.98)
  samp = XSample("X1", cryst)
  cryst.add_wavelength(wav)
  cryst.set_ha_info({'atom': 'S'})
  directory, image = os.path.split(imageset.get_path(1))
  wav.add_sweep(name='SWEEP1', sample=samp, directory=directory, image=image)

  import json
  from dxtbx.serialize.load import _decode_dict

  sweep = wav.get_sweeps()[0]
  indexer = DialsIndexer()
  indexer.set_working_directory(tmp_dir)
  indexer.add_indexer_imageset(imageset)
  indexer.set_indexer_sweep(sweep)
  sweep._indexer = indexer

  refiner = DialsRefiner()
  refiner.set_working_directory(tmp_dir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
  sweep._refiner = refiner

  integrater = DialsIntegrater()
  integrater.set_working_directory(tmp_dir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.set_integrater_epoch(sweep.get_epoch(1))
  integrater.set_integrater_sweep_name(sweep.get_name())
  integrater.set_integrater_project_info(
    cryst.get_name(), wav.get_name(), sweep.get_name())
  sweep._integrater = integrater

  scaler = CCP4ScalerA()
  scaler.add_scaler_integrater(integrater)
  scaler.set_scaler_xcrystal(cryst)
  scaler.set_scaler_project_info(cryst.get_name(), wav.get_name())
  scaler._scalr_xcrystal = cryst
  cryst._scaler = scaler

  s_dict = sweep.to_dict()
  s_str = json.dumps(s_dict, ensure_ascii=True)
  s_dict = json.loads(s_str, object_hook=_decode_dict)
  xsweep = XSweep.from_dict(s_dict)

  w_dict = wav.to_dict()
  w_str = json.dumps(w_dict, ensure_ascii=True)
  w_dict = json.loads(w_str, object_hook=_decode_dict)
  xwav = XWavelength.from_dict(w_dict)
  assert xwav.get_sweeps()[0].get_wavelength() is xwav

  c_dict = cryst.to_dict()
  c_str = json.dumps(c_dict, ensure_ascii=True)
  c_dict = json.loads(c_str, object_hook=_decode_dict)
  xcryst = XCrystal.from_dict(c_dict)
  assert xcryst.get_xwavelength(xcryst.get_wavelength_names()[0]).get_crystal() is xcryst

  p_dict = proj.to_dict()
  p_str = json.dumps(p_dict, ensure_ascii=True)
  p_dict = json.loads(p_str, object_hook=_decode_dict)
  xproj = XProject.from_dict(p_dict)
  assert xproj.get_crystals().values()[0].get_project() is xproj

  json_str = proj.as_json()
  xproj = XProject.from_json(string=json_str)
  assert xproj.get_crystals().values()[0].get_project() is xproj
  print xproj.get_output()
  print "\n".join(xproj.summarise())
  json_str = xproj.as_json()
  xproj = XProject.from_json(string=json_str)
  assert xproj.get_crystals().values()[0].get_project() is xproj
  xcryst = xproj.get_crystals().values()[0]
  intgr = xcryst._get_integraters()[0]
  assert intgr.get_integrater_finish_done()
  assert xcryst._get_scaler()._sweep_handler.get_sweep_information(
    intgr.get_integrater_epoch()).get_integrater() is intgr

  print xproj.get_output()
  print "\n".join(xproj.summarise())
  print
Beispiel #7
0
def Integrater():
  '''Return an  Integrater implementation.'''

  # FIXME this should take an indexer as an argument...

  integrater = None
  preselection = get_preferences().get('integrater')

  if not integrater and \
         (not preselection or preselection == 'dials'):
    try:
      integrater = DialsIntegrater()
      Debug.write('Using Dials Integrater')
    except NotAvailableError:
      if preselection == 'dials':
        raise RuntimeError( \
              'preselected integrater dials not available: ' + \
              'dials not installed?')

  if not integrater and (not preselection or preselection == 'mosflmr'):
    try:
      integrater = MosflmIntegrater()
      Debug.write('Using MosflmR Integrater')
      if not get_preferences().get('scaler'):
        add_preference('scaler', 'ccp4a')
    except NotAvailableError:
      if preselection == 'mosflmr':
        raise RuntimeError('preselected integrater mosflmr not available')

  if not integrater and \
         (not preselection or preselection == 'xdsr'):
    try:
      integrater = XDSIntegrater()
      Debug.write('Using XDS Integrater in new resolution mode')
    except NotAvailableError:
      if preselection == 'xdsr':
        raise RuntimeError( \
              'preselected integrater xdsr not available: ' + \
              'xds not installed?')

  if not integrater:
    raise RuntimeError('no integrater implementations found')

  # check to see if resolution limits were passed in through the
  # command line...

  dmin = PhilIndex.params.xia2.settings.resolution.d_min
  dmax = PhilIndex.params.xia2.settings.resolution.d_max

  if dmin:
    Debug.write('Adding user-assigned resolution limits:')

    if dmax:

      Debug.write('dmin: %.3f dmax: %.2f' % (dmin, dmax))
      integrater.set_integrater_resolution(dmin, dmax, user=True)

    else:

      Debug.write('dmin: %.3f' % dmin)
      integrater.set_integrater_high_resolution(dmin, user=True)

  return integrater
Beispiel #8
0
def test_ccp4_scalerA(dials_regression, tmpdir, nproc):
  if nproc is not None:
    from xia2.Handlers.Phil import PhilIndex
    PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

  template = os.path.join(dials_regression, "xia2_demo_data", "insulin_1_###.img")

  tmpdir.chdir()
  tmpdir = tmpdir.strpath

  from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
  from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
  from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
  from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA
  indexer = DialsIndexer()
  indexer.set_working_directory(tmpdir)
  from dxtbx.datablock import DataBlockTemplateImporter
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]
  indexer.add_indexer_imageset(imageset)

  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  cryst = XCrystal("CRYST1", None)
  wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
  samp = XSample("X1", cryst)
  directory, image = os.path.split(imageset.get_path(1))
  with mock.patch.object(sys, 'argv', []):
    sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
  indexer.set_indexer_sweep(sweep)

  refiner = DialsRefiner()
  refiner.set_working_directory(tmpdir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

  integrater = DialsIntegrater()
  integrater.set_working_directory(tmpdir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.set_integrater_sweep_name('SWEEP1')
  integrater.set_integrater_project_info('CRYST1', 'WAVE1', 'SWEEP1')

  scaler = CCP4ScalerA()
  scaler.add_scaler_integrater(integrater)
  scaler.set_scaler_xcrystal(cryst)
  scaler.set_scaler_project_info('CRYST1', 'WAVE1')

  check_scaler_files_exist(scaler)

  # test serialization of scaler
  json_str = scaler.as_json()
  #print json_str
  scaler2 = CCP4ScalerA.from_json(string=json_str)
  scaler2.set_scaler_xcrystal(cryst)

  check_scaler_files_exist(scaler2)

  scaler2.set_scaler_finish_done(False)
  check_scaler_files_exist(scaler2)

  scaler2.set_scaler_done(False)
  check_scaler_files_exist(scaler2)

  scaler2._scalr_integraters = {} # XXX
  scaler2.add_scaler_integrater(integrater)
  scaler2.set_scaler_prepare_done(False)
  check_scaler_files_exist(scaler2)
def exercise_dials_integrater(dials_data, tmp_dir, nproc=None):
    if nproc:
        from xia2.Handlers.Phil import PhilIndex

        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    # refiner.refine()

    integrater = DialsIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)
    from iotbx.reflection_file_reader import any_reflection_file

    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    expected_reflections = 47623
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    assert mtz_object.column_labels() == [
        "H",
        "K",
        "L",
        "M_ISYM",
        "BATCH",
        "IPR",
        "SIGIPR",
        "I",
        "SIGI",
        "BG",
        "SIGBG",
        "FRACTIONCALC",
        "XDET",
        "YDET",
        "ROT",
        "LP",
        "QE",
    ]

    assert integrater.get_integrater_wedge() == (1, 45)
    assert approx_equal(integrater.get_integrater_cell(),
                        (78.14, 78.14, 78.14, 90, 90, 90),
                        eps=1e-1)

    # test serialization of integrater
    json_str = integrater.as_json()
    # print(json_str)
    integrater2 = DialsIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()
Beispiel #10
0
def test_ccp4_scalerA(regression_test, ccp4, dials_data, run_in_tmpdir, nproc):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex

        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    tmpdir = run_in_tmpdir.strpath

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

    indexer = DialsIndexer()
    indexer.set_working_directory(tmpdir)
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    with mock.patch.object(sys, "argv", []):
        sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    refiner = DialsRefiner()
    refiner.set_working_directory(tmpdir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

    integrater = DialsIntegrater()
    integrater.set_working_directory(tmpdir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_sweep_name("SWEEP1")
    integrater.set_integrater_project_info("CRYST1", "WAVE1", "SWEEP1")

    scaler = CCP4ScalerA()
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info("CRYST1", "WAVE1")

    check_scaler_files_exist(scaler)

    # test serialization of scaler
    json_str = scaler.as_json()
    # print json_str
    scaler2 = CCP4ScalerA.from_json(string=json_str)
    scaler2.set_scaler_xcrystal(cryst)

    check_scaler_files_exist(scaler2)

    scaler2.set_scaler_finish_done(False)
    check_scaler_files_exist(scaler2)

    scaler2.set_scaler_done(False)
    check_scaler_files_exist(scaler2)

    scaler2._scalr_integraters = {}  # XXX
    scaler2.add_scaler_integrater(integrater)
    scaler2.set_scaler_prepare_done(False)
    check_scaler_files_exist(scaler2)
def exercise_dials_integrater(xia2_regression_build, tmp_dir, nproc=None):
    if nproc:
        from xia2.Handlers.Phil import PhilIndex
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    xia2_demo_data = os.path.join(xia2_regression_build, "test_data",
                                  "insulin")
    template = os.path.join(xia2_demo_data, "insulin_1_###.img")

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from dxtbx.datablock import DataBlockTemplateImporter
    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    #refiner.refine()

    integrater = DialsIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    #integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)
    from iotbx.reflection_file_reader import any_reflection_file
    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    expected_reflections = 48456
    assert abs(mtz_object.n_reflections() -
               expected_reflections) < 300, mtz_object.n_reflections()

    assert mtz_object.column_labels() == [
        'H', 'K', 'L', 'M_ISYM', 'BATCH', 'IPR', 'SIGIPR', 'I', 'SIGI', 'BG',
        'SIGBG', 'FRACTIONCALC', 'XDET', 'YDET', 'ROT', 'LP', 'QE'
    ]

    assert integrater.get_integrater_wedge() == (1, 45)
    assert approx_equal(integrater.get_integrater_cell(),
                        (78.14, 78.14, 78.14, 90, 90, 90),
                        eps=1e-1)

    # test serialization of integrater
    json_str = integrater.as_json()
    #print(json_str)
    integrater2 = DialsIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert abs(mtz_object.n_reflections() -
               expected_reflections) < 300, mtz_object.n_reflections()

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert abs(mtz_object.n_reflections() -
               expected_reflections) < 300, mtz_object.n_reflections()

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert abs(mtz_object.n_reflections() -
               expected_reflections) < 300, mtz_object.n_reflections()
Beispiel #12
0
def exercise_ccp4_scaler(nproc=None):
  if not have_dials_regression:
    print "Skipping exercise_ccp4_scaler(): dials_regression not configured"
    return

  if nproc is not None:
    from xia2.Handlers.Flags import Flags
    Flags.set_parallel(nproc)

  xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
  template = os.path.join(xia2_demo_data, "insulin_1_###.img")

  cwd = os.path.abspath(os.curdir)
  tmp_dir = os.path.abspath(open_tmp_directory())
  os.chdir(tmp_dir)

  from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
  from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
  from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
  from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA
  indexer = DialsIndexer()
  indexer.set_working_directory(tmp_dir)
  from dxtbx.datablock import DataBlockTemplateImporter
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]
  indexer.add_indexer_imageset(imageset)


  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  cryst = XCrystal("CRYST1", None)
  wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
  samp = XSample("X1", cryst)
  directory, image = os.path.split(imageset.get_path(1))
  sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
  indexer.set_indexer_sweep(sweep)

  refiner = DialsRefiner()
  refiner.set_working_directory(tmp_dir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

  integrater = DialsIntegrater()
  integrater.set_working_directory(tmp_dir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.set_integrater_sweep_name('SWEEP1')
  integrater.set_integrater_project_info('CRYST1', 'WAVE1', 'SWEEP1')

  scaler = CCP4ScalerA()
  scaler.add_scaler_integrater(integrater)
  scaler.set_scaler_xcrystal(cryst)
  scaler.set_scaler_project_info('CRYST1', 'WAVE1')

  check_scaler_files_exist(scaler)

  # test serialization of scaler
  json_str = scaler.as_json()
  #print json_str
  scaler2 = CCP4ScalerA.from_json(string=json_str)
  scaler2.set_scaler_xcrystal(cryst)

  check_scaler_files_exist(scaler2)

  scaler2.set_scaler_finish_done(False)
  check_scaler_files_exist(scaler2)

  scaler2.set_scaler_done(False)
  check_scaler_files_exist(scaler2)

  scaler2._scalr_integraters = {} # XXX
  scaler2.add_scaler_integrater(integrater)
  scaler2.set_scaler_prepare_done(False)
  check_scaler_files_exist(scaler2)
def exercise_dials_integrater(dials_data, tmp_dir, nproc=None):
    if nproc:
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    # refiner.refine()

    integrater = DialsIntegrater()
    integrater.set_output_format("hkl")
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)

    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz", repr(integrater_intensities)
    mtz_object = reader.file_content()
    expected_reflections = 47623
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    assert mtz_object.column_labels() == [
        "H",
        "K",
        "L",
        "M_ISYM",
        "BATCH",
        "IPR",
        "SIGIPR",
        "I",
        "SIGI",
        "BG",
        "SIGBG",
        "FRACTIONCALC",
        "XDET",
        "YDET",
        "ROT",
        "LP",
        "QE",
    ]

    assert integrater.get_integrater_wedge() == (1, 45)
    assert integrater.get_integrater_cell() == pytest.approx(
        (78.14, 78.14, 78.14, 90, 90, 90), abs=1e-1)

    # test serialization of integrater
    json_str = integrater.as_json()
    # print(json_str)
    integrater2 = DialsIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    # Test that diamond anvil cell attenuation correction does something.
    # That it does the right thing is left as a matter for the DIALS tests.
    integrater3 = DialsIntegrater.from_json(string=json_str)
    integrater3.set_integrater_sweep(sweep, reset=False)
    integrater3.set_integrater_done(False)
    integrater3.high_pressure = True
    # Don't get .hkl output because we're applying the attenuation correction to data
    # that weren't actually collected with a diamond anvil cell and some integrated
    # intensities will be rather nonsensical, which causes an error
    # 'cctbx Error: Inconsistent observation/sigma pair in columns: IPR, SIGIPR',
    # when some internal .hkl consistency checks are run, which is not meaningful here.
    integrater3.set_output_format("pickle")
    # Compare the first ten profile-fitted integrated intensities without correction.
    control_reflections = flex.reflection_table.from_file(
        integrater2.get_integrated_reflections())
    valid = control_reflections.get_flags(
        control_reflections.flags.integrated_prf)
    valid = valid.iselection()[:10]
    control_reflections = control_reflections.select(valid)
    # Get the first ten profile-fitted integrated intensities with DAC correction.
    corrected_reflections = flex.reflection_table.from_file(
        integrater3.get_integrated_reflections())
    valid = corrected_reflections.get_flags(
        corrected_reflections.flags.integrated_prf)
    valid = valid.iselection()[:10]
    corrected_reflections = corrected_reflections.select(valid)
    # Check that we're comparing equivalent reflections.
    assert control_reflections["miller_index"] == corrected_reflections[
        "miller_index"]
    control_intensities = control_reflections["intensity.prf.value"]
    corrected_intensities = corrected_reflections["intensity.prf.value"]
    # Check that the reflection intensities are not the same.
    assert pytest.approx(control_intensities) != corrected_intensities
def Integrater():
    """Return an  Integrater implementation."""

    # FIXME this should take an indexer as an argument...

    integrater = None
    preselection = get_preferences().get("integrater")

    if not integrater and (not preselection or preselection == "dials"):
        try:
            integrater = DialsIntegrater()
            Debug.write("Using Dials Integrater")
        except NotAvailableError:
            if preselection == "dials":
                raise RuntimeError(
                    "preselected integrater dials not available: " +
                    "dials not installed?")

    if not integrater and (not preselection or preselection == "mosflmr"):
        try:
            integrater = MosflmIntegrater()
            Debug.write("Using MosflmR Integrater")
            if not get_preferences().get("scaler"):
                add_preference("scaler", "ccp4a")
        except NotAvailableError:
            if preselection == "mosflmr":
                raise RuntimeError(
                    "preselected integrater mosflmr not available")

    if not integrater and (not preselection or preselection == "xdsr"):
        try:
            integrater = XDSIntegrater()
            Debug.write("Using XDS Integrater in new resolution mode")
        except NotAvailableError:
            if preselection == "xdsr":
                raise RuntimeError(
                    "preselected integrater xdsr not available: " +
                    "xds not installed?")

    if not integrater:
        raise RuntimeError("no integrater implementations found")

    # check to see if resolution limits were passed in through the
    # command line...

    dmin = PhilIndex.params.xia2.settings.resolution.d_min
    dmax = PhilIndex.params.xia2.settings.resolution.d_max

    if dmin:
        Debug.write("Adding user-assigned resolution limits:")

        if dmax:

            Debug.write("dmin: %.3f dmax: %.2f" % (dmin, dmax))
            integrater.set_integrater_resolution(dmin, dmax, user=True)

        else:

            Debug.write("dmin: %.3f" % dmin)
            integrater.set_integrater_high_resolution(dmin, user=True)

    return integrater