def test_translate(dials_regression, tmpdir):
    """Test as written in https://github.com/dials/dials/issues/471. This
  is pretty slow!"""

    tmpdir.chdir()
    dials_regression = "~/sw/cctbx/modules/dials_regression"

    # use the i04_weak_data for this test
    data_dir = os.path.join(dials_regression, "image_examples", "DLS_I04")
    image_path = os.path.join(data_dir, "grid_full_cbf_0005.cbf")

    # Generate distortion maps
    cmd = ("dials.generate_distortion_maps {0} "
           "dx=1 dy=2").format(image_path)
    result = easy_run.fully_buffered(command=cmd).raise_if_errors()

    # Import without correction
    cmd = ("dials.import {0}").format(image_path)
    result = easy_run.fully_buffered(command=cmd).raise_if_errors()
    db1 = DataBlockFactory.from_serialized_format('datablock.json')[0]
    det1 = db1.unique_detectors()[0]

    # Import with correction
    cmd = ("dials.import {0} dx=dx.pickle dy=dy.pickle "
           "output.datablock=corrected_datablock.json").format(image_path)
    result = easy_run.fully_buffered(command=cmd).raise_if_errors()
    db2 = DataBlockFactory.from_serialized_format(
        'corrected_datablock.json')[0]
    det2 = db2.unique_detectors()[0]

    # FIXME, why doesn't db2 have dx, dy set?
    assert db2.extract_imagesets()[0].external_lookup.dx.filename
Пример #2
0
def datablock(filename, check_format=True):
  ''' Load a given JSON or pickle file.

  Params:
    filename The input filename

  Returns:
    The datablock

  '''
  from dxtbx.datablock import DataBlockFactory
  return DataBlockFactory.from_serialized_format(
    filename, check_format=check_format)
print mycrystal

# get a CS-PAD detector for testing
import os
import libtbx.load_env
dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression",
    test=os.path.isdir)
data_dir = os.path.join(dials_regression, "refinement_test_data",
                        "hierarchy_test")
datablock_path = os.path.join(data_dir, "datablock.json")
assert os.path.exists(datablock_path)

# load models
from dxtbx.datablock import DataBlockFactory
datablock = DataBlockFactory.from_serialized_format(datablock_path, check_format=False)
im_set = datablock[0].extract_imagesets()[0]
from copy import deepcopy
cspad = deepcopy(im_set.get_detector())

# get also a hierarchical type P6M detector
data_dir = os.path.join(dials_regression, "refinement_test_data",
                        "metrology", "i03-2.5A-thaumatin-20140514-split")
datablock_path = os.path.join(data_dir, "datablock.json")
datablock = DataBlockFactory.from_serialized_format(datablock_path, check_format=False)
im_set = datablock[0].extract_imagesets()[0]
from copy import deepcopy
p6m = deepcopy(im_set.get_detector())
print p6m[0]

# get a P12M (cropped to middle 18 modules)
Пример #4
0
def test_refinement():
  '''Test a refinement run'''

  dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression",
    test=os.path.isdir)

  # Get a beam and detector from a datablock. This one has a CS-PAD, but that
  # is irrelevant
  data_dir = os.path.join(dials_regression, "refinement_test_data",
                          "hierarchy_test")
  datablock_path = os.path.join(data_dir, "datablock.json")
  assert os.path.exists(datablock_path)

  # load models
  from dxtbx.datablock import DataBlockFactory
  datablock = DataBlockFactory.from_serialized_format(datablock_path, check_format=False)
  im_set = datablock[0].extract_imagesets()[0]
  from copy import deepcopy
  detector = deepcopy(im_set.get_detector())
  beam = im_set.get_beam()

  # Invent a crystal, goniometer and scan for this test
  from dxtbx.model.crystal import crystal_model
  crystal = crystal_model((40.,0.,0.) ,(0.,40.,0.), (0.,0.,40.),
                          space_group_symbol = "P1")
  orig_xl = deepcopy(crystal)

  from dxtbx.model.experiment import goniometer_factory
  goniometer = goniometer_factory.known_axis((1., 0., 0.))

  # Build a mock scan for a 180 degree sweep
  from dxtbx.model.scan import scan_factory
  sf = scan_factory()
  scan = sf.make_scan(image_range = (1,1800),
                      exposure_times = 0.1,
                      oscillation = (0, 0.1),
                      epochs = range(1800),
                      deg = True)
  sweep_range = scan.get_oscillation_range(deg=False)
  im_width = scan.get_oscillation(deg=False)[1]
  assert sweep_range == (0., pi)
  assert approx_equal(im_width, 0.1 * pi / 180.)

  from dxtbx.model.experiment.experiment_list import ExperimentList, Experiment

  # Build an experiment list
  experiments = ExperimentList()
  experiments.append(Experiment(
        beam=beam, detector=detector, goniometer=goniometer,
        scan=scan, crystal=crystal, imageset=None))

  # simulate some reflections
  refs, _ = generate_reflections(experiments)

  # change unit cell a bit (=0.1 Angstrom length upsets, 0.1 degree of
  # alpha and beta angles)
  from dials.algorithms.refinement.parameterisation.crystal_parameters import \
    CrystalUnitCellParameterisation
  xluc_param = CrystalUnitCellParameterisation(crystal)
  xluc_p_vals = xluc_param.get_param_vals()
  cell_params = crystal.get_unit_cell().parameters()
  cell_params = [a + b for a, b in zip(cell_params, [0.1, -0.1, 0.1, 0.1,
                                                     -0.1, 0.0])]
  from cctbx.uctbx import unit_cell
  from rstbx.symmetry.constraints.parameter_reduction import \
      symmetrize_reduce_enlarge
  from scitbx import matrix
  new_uc = unit_cell(cell_params)
  newB = matrix.sqr(new_uc.fractionalization_matrix()).transpose()
  S = symmetrize_reduce_enlarge(crystal.get_space_group())
  S.set_orientation(orientation=newB)
  X = tuple([e * 1.e5 for e in S.forward_independent_parameters()])
  xluc_param.set_param_vals(X)

  # reparameterise the crystal at the perturbed geometry
  xluc_param = CrystalUnitCellParameterisation(crystal)

  # Dummy parameterisations for other models
  beam_param = None
  xlo_param = None
  det_param = None

  # parameterisation of the prediction equation
  from dials.algorithms.refinement.parameterisation.parameter_report import \
      ParameterReporter
  pred_param = TwoThetaPredictionParameterisation(experiments,
    det_param, beam_param, xlo_param, [xluc_param])
  param_reporter = ParameterReporter(det_param, beam_param,
                                     xlo_param, [xluc_param])

  # reflection manager
  refman = TwoThetaReflectionManager(refs, experiments, nref_per_degree=20,
    verbosity=2)

  # reflection predictor
  ref_predictor = TwoThetaExperimentsPredictor(experiments)

  # target function
  target = TwoThetaTarget(experiments, ref_predictor, refman, pred_param)

  # minimisation engine
  from dials.algorithms.refinement.engine \
    import LevenbergMarquardtIterations as Refinery
  refinery = Refinery(target = target,
                      prediction_parameterisation = pred_param,
                      log = None,
                      verbosity = 0,
                      track_step = False,
                      track_gradient = False,
                      track_parameter_correlation = False,
                      max_iterations = 20)

  # Refiner
  from dials.algorithms.refinement.refiner import Refiner
  refiner = Refiner(reflections=refs,
                    experiments=experiments,
                    pred_param=pred_param,
                    param_reporter=param_reporter,
                    refman=refman,
                    target=target,
                    refinery=refinery,
                    verbosity=1)

  history = refiner.run()

  # compare crystal with original crystal
  refined_xl = refiner.get_experiments()[0].crystal

  #print refined_xl
  assert refined_xl.is_similar_to(orig_xl, uc_rel_length_tolerance=0.001,
    uc_abs_angle_tolerance=0.01)

  #print "Unit cell esds:"
  #print refined_xl.get_cell_parameter_sd()

  return
def test1():

  dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression",
    test=os.path.isdir)

  # use a datablock that contains a CS-PAD detector description
  data_dir = os.path.join(dials_regression, "refinement_test_data",
                          "hierarchy_test")
  datablock_path = os.path.join(data_dir, "datablock.json")
  assert os.path.exists(datablock_path)

  # load models
  from dxtbx.datablock import DataBlockFactory
  datablock = DataBlockFactory.from_serialized_format(datablock_path, check_format=False)
  im_set = datablock[0].extract_imagesets()[0]
  from copy import deepcopy
  detector = deepcopy(im_set.get_detector())
  beam = im_set.get_beam()

  # we'll invent a crystal, goniometer and scan for this test
  from dxtbx.model.crystal import crystal_model
  crystal = crystal_model((40.,0.,0.) ,(0.,40.,0.), (0.,0.,40.),
                          space_group_symbol = "P1")

  from dxtbx.model.experiment import goniometer_factory
  goniometer = goniometer_factory.known_axis((1., 0., 0.))

  # Build a mock scan for a 180 degree sweep
  from dxtbx.model.scan import scan_factory
  sf = scan_factory()
  scan = sf.make_scan(image_range = (1,1800),
                      exposure_times = 0.1,
                      oscillation = (0, 0.1),
                      epochs = range(1800),
                      deg = True)
  sweep_range = scan.get_oscillation_range(deg=False)
  im_width = scan.get_oscillation(deg=False)[1]
  assert sweep_range == (0., pi)
  assert approx_equal(im_width, 0.1 * pi / 180.)

  from dxtbx.model.experiment.experiment_list import ExperimentList, Experiment

  # Build an experiment list
  experiments = ExperimentList()
  experiments.append(Experiment(
        beam=beam, detector=detector, goniometer=goniometer,
        scan=scan, crystal=crystal, imageset=None))

  # simulate some reflections
  refs, ref_predictor = generate_reflections(experiments)

  # move the detector quadrants apart by 2mm both horizontally and vertically
  from dials.algorithms.refinement.parameterisation \
    import DetectorParameterisationHierarchical
  det_param = DetectorParameterisationHierarchical(detector, level=1)
  det_p_vals = det_param.get_param_vals()
  p_vals = list(det_p_vals)
  p_vals[1] += 2
  p_vals[2] -= 2
  p_vals[7] += 2
  p_vals[8] += 2
  p_vals[13] -= 2
  p_vals[14] += 2
  p_vals[19] -= 2
  p_vals[20] -= 2
  det_param.set_param_vals(p_vals)

  # reparameterise the detector at the new perturbed geometry
  det_param = DetectorParameterisationHierarchical(detector, level=1)

  # parameterise other models
  from dials.algorithms.refinement.parameterisation.beam_parameters import \
      BeamParameterisation
  from dials.algorithms.refinement.parameterisation.crystal_parameters import \
      CrystalOrientationParameterisation, CrystalUnitCellParameterisation
  beam_param = BeamParameterisation(beam, goniometer)
  xlo_param = CrystalOrientationParameterisation(crystal)
  xluc_param = CrystalUnitCellParameterisation(crystal)

  # fix beam
  beam_param.set_fixed([True]*3)

  # fix crystal
  xluc_param.set_fixed([True]*6)
  xlo_param.set_fixed([True]*3)

  # parameterisation of the prediction equation
  from dials.algorithms.refinement.parameterisation.prediction_parameters import \
      XYPhiPredictionParameterisation
  from dials.algorithms.refinement.parameterisation.parameter_report import \
      ParameterReporter
  pred_param = XYPhiPredictionParameterisation(experiments,
    [det_param], [beam_param], [xlo_param], [xluc_param])
  param_reporter = ParameterReporter([det_param], [beam_param],
                                     [xlo_param], [xluc_param])

  # reflection manager and target function
  from dials.algorithms.refinement.target import \
    LeastSquaresPositionalResidualWithRmsdCutoff
  from dials.algorithms.refinement.reflection_manager import ReflectionManager
  refman = ReflectionManager(refs, experiments, nref_per_degree=20)

  # set a very tight rmsd target of 1/10000 of a pixel
  target = LeastSquaresPositionalResidualWithRmsdCutoff(experiments,
      ref_predictor, refman, pred_param, restraints_parameterisation=None,
      frac_binsize_cutoff=0.0001)

  # minimisation engine
  from dials.algorithms.refinement.engine \
    import LevenbergMarquardtIterations as Refinery
  refinery = Refinery(target = target,
                      prediction_parameterisation = pred_param,
                      log = None,
                      verbosity = 0,
                      track_step = False,
                      track_gradient = False,
                      track_parameter_correlation = False,
                      max_iterations = 20)

  # Refiner
  from dials.algorithms.refinement.refiner import Refiner
  refiner = Refiner(reflections=refs,
                    experiments=experiments,
                    pred_param=pred_param,
                    param_reporter=param_reporter,
                    refman=refman,
                    target=target,
                    refinery=refinery,
                    verbosity=0)

  history = refiner.run()
  assert history.reason_for_termination == "RMSD target achieved"

  #compare detector with original detector
  orig_det = im_set.get_detector()
  refined_det = refiner.get_experiments()[0].detector

  from scitbx import matrix
  import math
  for op, rp in zip(orig_det, refined_det):
    # compare the origin vectors by...
    o1 = matrix.col(op.get_origin())
    o2 = matrix.col(rp.get_origin())
    # ...their relative lengths
    assert approx_equal(
      math.fabs(o1.length() - o2.length()) / o1.length(), 0, eps=1e-5)
    # ...the angle between them
    assert approx_equal(o1.accute_angle(o2), 0, eps=1e-5)

  print "OK"
  return
def test1():

    dials_regression = libtbx.env.find_in_repositories(
        relative_path="dials_regression", test=os.path.isdir)

    # use a datablock that contains a CS-PAD detector description
    data_dir = os.path.join(dials_regression, "refinement_test_data",
                            "hierarchy_test")
    datablock_path = os.path.join(data_dir, "datablock.json")
    assert os.path.exists(datablock_path)

    # load models
    from dxtbx.datablock import DataBlockFactory
    datablock = DataBlockFactory.from_serialized_format(datablock_path,
                                                        check_format=False)
    im_set = datablock[0].extract_imagesets()[0]
    from copy import deepcopy
    detector = deepcopy(im_set.get_detector())
    beam = im_set.get_beam()

    # we'll invent a crystal, goniometer and scan for this test
    from dxtbx.model import Crystal
    crystal = Crystal((40., 0., 0.), (0., 40., 0.), (0., 0., 40.),
                      space_group_symbol="P1")

    from dxtbx.model import GoniometerFactory
    goniometer = GoniometerFactory.known_axis((1., 0., 0.))

    # Build a mock scan for a 180 degree sweep
    from dxtbx.model import ScanFactory
    sf = ScanFactory()
    scan = sf.make_scan(image_range=(1, 1800),
                        exposure_times=0.1,
                        oscillation=(0, 0.1),
                        epochs=range(1800),
                        deg=True)
    sweep_range = scan.get_oscillation_range(deg=False)
    im_width = scan.get_oscillation(deg=False)[1]
    assert sweep_range == (0., pi)
    assert approx_equal(im_width, 0.1 * pi / 180.)

    from dxtbx.model.experiment_list import ExperimentList, Experiment

    # Build an experiment list
    experiments = ExperimentList()
    experiments.append(
        Experiment(beam=beam,
                   detector=detector,
                   goniometer=goniometer,
                   scan=scan,
                   crystal=crystal,
                   imageset=None))

    # simulate some reflections
    refs, ref_predictor = generate_reflections(experiments)

    # move the detector quadrants apart by 2mm both horizontally and vertically
    from dials.algorithms.refinement.parameterisation \
      import DetectorParameterisationHierarchical
    det_param = DetectorParameterisationHierarchical(detector, level=1)
    det_p_vals = det_param.get_param_vals()
    p_vals = list(det_p_vals)
    p_vals[1] += 2
    p_vals[2] -= 2
    p_vals[7] += 2
    p_vals[8] += 2
    p_vals[13] -= 2
    p_vals[14] += 2
    p_vals[19] -= 2
    p_vals[20] -= 2
    det_param.set_param_vals(p_vals)

    # reparameterise the detector at the new perturbed geometry
    det_param = DetectorParameterisationHierarchical(detector, level=1)

    # parameterise other models
    from dials.algorithms.refinement.parameterisation.beam_parameters import \
        BeamParameterisation
    from dials.algorithms.refinement.parameterisation.crystal_parameters import \
        CrystalOrientationParameterisation, CrystalUnitCellParameterisation
    beam_param = BeamParameterisation(beam, goniometer)
    xlo_param = CrystalOrientationParameterisation(crystal)
    xluc_param = CrystalUnitCellParameterisation(crystal)

    # fix beam
    beam_param.set_fixed([True] * 3)

    # fix crystal
    xluc_param.set_fixed([True] * 6)
    xlo_param.set_fixed([True] * 3)

    # parameterisation of the prediction equation
    from dials.algorithms.refinement.parameterisation.prediction_parameters import \
        XYPhiPredictionParameterisation
    from dials.algorithms.refinement.parameterisation.parameter_report import \
        ParameterReporter
    pred_param = XYPhiPredictionParameterisation(experiments, [det_param],
                                                 [beam_param], [xlo_param],
                                                 [xluc_param])
    param_reporter = ParameterReporter([det_param], [beam_param], [xlo_param],
                                       [xluc_param])

    # reflection manager and target function
    from dials.algorithms.refinement.target import \
      LeastSquaresPositionalResidualWithRmsdCutoff
    from dials.algorithms.refinement.reflection_manager import ReflectionManager
    refman = ReflectionManager(refs, experiments, nref_per_degree=20)

    # set a very tight rmsd target of 1/10000 of a pixel
    target = LeastSquaresPositionalResidualWithRmsdCutoff(
        experiments,
        ref_predictor,
        refman,
        pred_param,
        restraints_parameterisation=None,
        frac_binsize_cutoff=0.0001)

    # minimisation engine
    from dials.algorithms.refinement.engine \
      import LevenbergMarquardtIterations as Refinery
    refinery = Refinery(target=target,
                        prediction_parameterisation=pred_param,
                        log=None,
                        verbosity=0,
                        max_iterations=20)

    # Refiner
    from dials.algorithms.refinement.refiner import Refiner
    refiner = Refiner(reflections=refs,
                      experiments=experiments,
                      pred_param=pred_param,
                      param_reporter=param_reporter,
                      refman=refman,
                      target=target,
                      refinery=refinery,
                      verbosity=0)

    history = refiner.run()
    assert history.reason_for_termination == "RMSD target achieved"

    #compare detector with original detector
    orig_det = im_set.get_detector()
    refined_det = refiner.get_experiments()[0].detector

    from scitbx import matrix
    import math
    for op, rp in zip(orig_det, refined_det):
        # compare the origin vectors by...
        o1 = matrix.col(op.get_origin())
        o2 = matrix.col(rp.get_origin())
        # ...their relative lengths
        assert approx_equal(math.fabs(o1.length() - o2.length()) / o1.length(),
                            0,
                            eps=1e-5)
        # ...the angle between them
        assert approx_equal(o1.accute_angle(o2), 0, eps=1e-5)

    print "OK"
    return
Пример #7
0
def test_refinement(dials_regression):
  '''Test a refinement run'''

  # Get a beam and detector from a datablock. This one has a CS-PAD, but that
  # is irrelevant
  data_dir = os.path.join(dials_regression, "refinement_test_data",
                          "hierarchy_test")
  datablock_path = os.path.join(data_dir, "datablock.json")
  assert os.path.exists(datablock_path)

  # load models
  from dxtbx.datablock import DataBlockFactory
  datablock = DataBlockFactory.from_serialized_format(datablock_path, check_format=False)
  im_set = datablock[0].extract_imagesets()[0]
  detector = deepcopy(im_set.get_detector())
  beam = im_set.get_beam()

  # Invent a crystal, goniometer and scan for this test
  from dxtbx.model import Crystal
  crystal = Crystal((40.,0.,0.) ,(0.,40.,0.), (0.,0.,40.),
                          space_group_symbol = "P1")
  orig_xl = deepcopy(crystal)

  from dxtbx.model import GoniometerFactory
  goniometer = GoniometerFactory.known_axis((1., 0., 0.))

  # Build a mock scan for a 180 degree sweep
  from dxtbx.model import ScanFactory
  sf = ScanFactory()
  scan = sf.make_scan(image_range = (1,1800),
                      exposure_times = 0.1,
                      oscillation = (0, 0.1),
                      epochs = range(1800),
                      deg = True)
  sweep_range = scan.get_oscillation_range(deg=False)
  im_width = scan.get_oscillation(deg=False)[1]
  assert sweep_range == (0., pi)
  assert approx_equal(im_width, 0.1 * pi / 180.)

  # Build an experiment list
  experiments = ExperimentList()
  experiments.append(Experiment(
        beam=beam, detector=detector, goniometer=goniometer,
        scan=scan, crystal=crystal, imageset=None))

  # simulate some reflections
  refs, _ = generate_reflections(experiments)

  # change unit cell a bit (=0.1 Angstrom length upsets, 0.1 degree of
  # alpha and beta angles)
  from dials.algorithms.refinement.parameterisation.crystal_parameters import \
    CrystalUnitCellParameterisation
  xluc_param = CrystalUnitCellParameterisation(crystal)
  xluc_p_vals = xluc_param.get_param_vals()
  cell_params = crystal.get_unit_cell().parameters()
  cell_params = [a + b for a, b in zip(cell_params, [0.1, -0.1, 0.1, 0.1,
                                                     -0.1, 0.0])]
  from cctbx.uctbx import unit_cell
  from rstbx.symmetry.constraints.parameter_reduction import \
      symmetrize_reduce_enlarge
  from scitbx import matrix
  new_uc = unit_cell(cell_params)
  newB = matrix.sqr(new_uc.fractionalization_matrix()).transpose()
  S = symmetrize_reduce_enlarge(crystal.get_space_group())
  S.set_orientation(orientation=newB)
  X = tuple([e * 1.e5 for e in S.forward_independent_parameters()])
  xluc_param.set_param_vals(X)

  # reparameterise the crystal at the perturbed geometry
  xluc_param = CrystalUnitCellParameterisation(crystal)

  # Dummy parameterisations for other models
  beam_param = None
  xlo_param = None
  det_param = None

  # parameterisation of the prediction equation
  from dials.algorithms.refinement.parameterisation.parameter_report import \
      ParameterReporter
  pred_param = TwoThetaPredictionParameterisation(experiments,
    det_param, beam_param, xlo_param, [xluc_param])
  param_reporter = ParameterReporter(det_param, beam_param,
                                     xlo_param, [xluc_param])

  # reflection manager
  refman = TwoThetaReflectionManager(refs, experiments, nref_per_degree=20,
    verbosity=2)

  # reflection predictor
  ref_predictor = TwoThetaExperimentsPredictor(experiments)

  # target function
  target = TwoThetaTarget(experiments, ref_predictor, refman, pred_param)

  # minimisation engine
  from dials.algorithms.refinement.engine \
    import LevenbergMarquardtIterations as Refinery
  refinery = Refinery(target = target,
                      prediction_parameterisation = pred_param,
                      log = None,
                      verbosity = 0,
                      max_iterations = 20)

  # Refiner
  from dials.algorithms.refinement.refiner import Refiner
  refiner = Refiner(reflections=refs,
                    experiments=experiments,
                    pred_param=pred_param,
                    param_reporter=param_reporter,
                    refman=refman,
                    target=target,
                    refinery=refinery,
                    verbosity=1)

  history = refiner.run()

  # compare crystal with original crystal
  refined_xl = refiner.get_experiments()[0].crystal

  #print refined_xl
  assert refined_xl.is_similar_to(orig_xl, uc_rel_length_tolerance=0.001,
    uc_abs_angle_tolerance=0.01)
Пример #8
0
# get a CS-PAD detector for testing
import os
import libtbx.load_env

dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression", test=os.path.isdir)
data_dir = os.path.join(dials_regression, "refinement_test_data",
                        "hierarchy_test")
datablock_path = os.path.join(data_dir, "datablock.json")
assert os.path.exists(datablock_path)

# load models
from dxtbx.datablock import DataBlockFactory

datablock = DataBlockFactory.from_serialized_format(datablock_path,
                                                    check_format=False)
im_set = datablock[0].extract_imagesets()[0]
from copy import deepcopy

cspad = deepcopy(im_set.get_detector())

# get also a hierarchical type P6M detector
data_dir = os.path.join(
    dials_regression,
    "refinement_test_data",
    "metrology",
    "i03-2.5A-thaumatin-20140514-split",
)
datablock_path = os.path.join(data_dir, "datablock.json")
datablock = DataBlockFactory.from_serialized_format(datablock_path,
                                                    check_format=False)
Пример #9
0
def update_all_data(reflections_path=None, experiments_path=None):
    dat = InfoData()

    if (reflections_path != None):

        try:
            refl_tabl = flex.reflection_table.from_pickle(reflections_path)
            dat.n_strng = refl_tabl.get_flags(
                refl_tabl.flags.strong).count(True)
            print "dat.n_strng =", dat.n_strng
            dat.n_index = refl_tabl.get_flags(
                refl_tabl.flags.indexed).count(True)
            print "dat.n_index =", dat.n_index
            dat.n_refnd = refl_tabl.get_flags(
                refl_tabl.flags.used_in_refinement).count(True)
            print "dat.n_refnd =", dat.n_refnd
            dat.n_integ_sum = refl_tabl.get_flags(
                refl_tabl.flags.integrated_sum).count(True)
            print "dat.n_integ_sum =", dat.n_integ_sum
            dat.n_integ_prf = refl_tabl.get_flags(
                refl_tabl.flags.integrated_prf).count(True)
            print "dat.n_integ_prf =", dat.n_integ_prf

        except:
            print "failed to find reflections"

    if (experiments_path != None):

        print "trying experiments"
        try:
            experiments = ExperimentListFactory.from_json_file(
                experiments_path, check_format=False)
        except:
            try:
                # FIXME here only take the first datablock. What if there are more?
                datablock = DataBlockFactory.from_serialized_format(
                    experiments_path, check_format=False)[0]

                # FIXME here only take the first model from each
                beam = datablock.unique_beams()[0]
                detector = datablock.unique_detectors()[0]
                scan = datablock.unique_scans()[0]

                # build a pseudo ExperimentList (with empty crystals)
                experiments = ExperimentList()
                experiments.append(
                    Experiment(beam=beam, detector=detector, scan=scan))

            except ValueError:
                print "failed to read json file"
                return dat

        print "len(experiments)", len(experiments)

        # FIXME take just the first experiment. What if there are more?
        exp = experiments[0]

        # Get crystal data
        if exp.crystal is not None:
            unit_cell = exp.crystal.get_unit_cell()
            dat.a, dat.b, dat.c, dat.alpha, dat.beta, dat.gamma = unit_cell.parameters(
            )

            exp_crystal = exp.crystal
            #print "exp_crystal = ", exp_crystal
            b_mat = exp.crystal.get_B()
            dat.b11 = b_mat[0]
            dat.b12 = b_mat[1]
            dat.b13 = b_mat[2]
            dat.b21 = b_mat[3]
            dat.b22 = b_mat[4]
            dat.b23 = b_mat[5]
            dat.b31 = b_mat[6]
            dat.b32 = b_mat[7]
            dat.b33 = b_mat[8]

            sg = str(exp.crystal.get_space_group().info())
            print "spgr = ", sg
            dat.spg_group = sg

            from scitbx import matrix
            u_mat = matrix.sqr(exp.crystal.get_U())

            dat.u11 = b_mat[0]
            dat.u12 = b_mat[1]
            dat.u13 = b_mat[2]
            dat.u21 = b_mat[3]
            dat.u22 = b_mat[4]
            dat.u23 = b_mat[5]
            dat.u31 = b_mat[6]
            dat.u32 = b_mat[7]
            dat.u33 = b_mat[8]

            rot_angs = u_mat.r3_rotation_matrix_as_x_y_z_angles(deg=True)
            print "u_mat =", u_mat

            print "rot_angs =", rot_angs
            dat.r1, dat.r2, dat.r3 = rot_angs

        # Get beam data
        dat.w_lambda = exp.beam.get_wavelength()

        # Get detector data
        # assume details for the panel the beam intersects are the same for the whole detector
        pnl_beam_intersects, (beam_x, beam_y) = \
            exp.detector.get_ray_intersection(exp.beam.get_s0())
        pnl = exp.detector[pnl_beam_intersects]
        print "beam_x, beam_y =", beam_x, beam_y

        dat.xb = beam_x
        dat.yb = beam_y

        dist = pnl.get_distance()

        print "pnl_beam_intersects             ", pnl_beam_intersects
        print "dist                            ", dist

        dat.dd = dist

        dat.img_ran1, dat.img_ran2 = exp.scan.get_image_range()
        dat.oscil1, dat.oscil2 = exp.scan.get_oscillation()

        # is the next line right? check what dials.show does
        dat.e_time = max(exp.scan.get_exposure_times())
        #print set(exp.scan.get_exposure_times())

        dat.n_pans = len(exp.detector)
        dat.x_px_size, dat.y_px_size = pnl.get_pixel_size()
        dat.gain = pnl.get_gain()
        dat.max_res = exp.detector.get_max_resolution(exp.beam.get_s0())

        # manually finding template from experiments_path

        try:
            with open(experiments_path) as infile:
                json_info = json.load(infile)

            if (type(json_info) is dict):
                print "found Dictionary"
                imageset = json_info['imageset']

            elif (type(json_info) is list):
                print "found List"
                imageset = json_info[0]['imageset']

            dat.tmpl_str = imageset[0]['template']

            print "dat.tmpl_str =", dat.tmpl_str

        except:
            print "failed to find template in JSON file"

    return dat
Пример #10
0
def update_all_data(reflections_path=None, experiments_path=None):
    dat = InfoData()
    dat.ref2exp = None

    if reflections_path is not None:

        try:
            refl_tabl = flex.reflection_table.from_file(reflections_path)
            dat.n_strng = refl_tabl.get_flags(
                refl_tabl.flags.strong).count(True)
            dat.n_index = refl_tabl.get_flags(
                refl_tabl.flags.indexed).count(True)
            dat.n_refnd = refl_tabl.get_flags(
                refl_tabl.flags.used_in_refinement).count(True)
            dat.n_integ_sum = refl_tabl.get_flags(
                refl_tabl.flags.integrated_sum).count(True)
            dat.n_integ_prf = refl_tabl.get_flags(
                refl_tabl.flags.integrated_prf).count(True)

        except BaseException as e:
            # We don't want to catch bare exceptions but don't know
            # what this was supposed to catch. Log it.
            logger.info(" >> Caught unknown exception type:",
                        type(e).__name__, e, "N###")

            logger.info("failed to find reflections")
            logger.info("reflections_path = %s", reflections_path)

    if experiments_path is not None:
        try:
            experiments = ExperimentListFactory.from_json_file(
                experiments_path, check_format=False)
        except BaseException as e:
            # We don't want to catch bare exceptions but don't know
            # what this was supposed to catch. Log it.
            logger.info("\n exception #1 %s: %s", type(e).__name__, e)

            # TODO Maybe the next try block is not needed, consider removing all

            try:
                # FIXME here only take the first datablock. What if there are more?
                datablock = DataBlockFactory.from_serialized_format(
                    experiments_path, check_format=False)[0]

                # FIXME here only take the first model from each
                beam = datablock.unique_beams()[0]
                detector = datablock.unique_detectors()[0]
                scan = datablock.unique_scans()[0]

                # build a pseudo ExperimentList (with empty crystals)
                experiments = ExperimentList()
                experiments.append(
                    Experiment(beam=beam, detector=detector, scan=scan))

            except ValueError:
                logger.info("failed to read json file")
                logger.info("experiments_path = %s", experiments_path)
                return dat

        try:
            imageset_tmp = experiments.imagesets()[0]
            mask_file = imageset_tmp.external_lookup.mask.filename

            pick_file = open(mask_file, "rb")
            mask_tup_obj = pickle.load(pick_file)
            pick_file.close()

            dat.mask_flex = mask_tup_obj[0]
            mask_np_arr = dat.mask_flex.as_numpy_array()
            dat.np_mask = mask_np_arr

        except IOError:
            logger.info("No mask in this node")
            dat.np_mask = None
            dat.mask_flex = None

        # FIXME it takes just the first experiment. What if there are more?
        exp = experiments[0]

        # Get crystal data
        if exp.crystal is not None:
            unit_cell = exp.crystal.get_unit_cell()
            dat.a, dat.b, dat.c, dat.alpha, dat.beta, dat.gamma = unit_cell.parameters(
            )

            exp_crystal = exp.crystal
            logger.info("exp_crystal =  %s", exp_crystal)
            b_mat = exp.crystal.get_B()
            dat.b11 = b_mat[0]
            dat.b12 = b_mat[1]
            dat.b13 = b_mat[2]
            dat.b21 = b_mat[3]
            dat.b22 = b_mat[4]
            dat.b23 = b_mat[5]
            dat.b31 = b_mat[6]
            dat.b32 = b_mat[7]
            dat.b33 = b_mat[8]

            sg = str(exp.crystal.get_space_group().info())
            logger.info("spgr =  %s", sg)
            dat.spg_group = sg

            from scitbx import matrix

            u_mat = matrix.sqr(exp.crystal.get_U())

            dat.u11 = b_mat[0]
            dat.u12 = b_mat[1]
            dat.u13 = b_mat[2]
            dat.u21 = b_mat[3]
            dat.u22 = b_mat[4]
            dat.u23 = b_mat[5]
            dat.u31 = b_mat[6]
            dat.u32 = b_mat[7]
            dat.u33 = b_mat[8]

            rot_angs = u_mat.r3_rotation_matrix_as_x_y_z_angles(deg=True)
            logger.info("u_mat = %s", u_mat)

            logger.info("rot_angs = %s", rot_angs)
            dat.r1, dat.r2, dat.r3 = rot_angs

        # Get beam data
        dat.w_lambda = exp.beam.get_wavelength()

        # Get detector data
        # assume details for the panel the beam intersects are the same
        # for the whole detector
        # TODO fix it for I23

        pnl_beam_intersects, (beam_x,
                              beam_y) = exp.detector.get_ray_intersection(
                                  exp.beam.get_s0())
        pnl = exp.detector[pnl_beam_intersects]
        logger.info("beam_x, beam_y = %s %s", beam_x, beam_y)

        dat.n_pan_xb_yb = pnl_beam_intersects
        dat.xb = beam_x
        dat.yb = beam_y

        dist = pnl.get_distance()

        logger.info("pnl_beam_intersects              %s", pnl_beam_intersects)
        logger.info("dist                             %s", dist)

        dat.dd = dist

        dat.img_ran1, dat.img_ran2 = exp.scan.get_image_range()
        dat.oscil1, dat.oscil2 = exp.scan.get_oscillation()

        # is the next line right? check what dials.show does
        dat.e_time = max(exp.scan.get_exposure_times())

        dat.n_pans = len(exp.detector)
        dat.x_px_size, dat.y_px_size = pnl.get_pixel_size()
        dat.gain = pnl.get_gain()
        dat.max_res = exp.detector.get_max_resolution(exp.beam.get_s0())

        # manually finding template from experiments_path

        try:
            with open(experiments_path) as infile:
                json_info = json.load(infile)

            if type(json_info) is dict:
                imageset = json_info["imageset"]

            elif type(json_info) is list:
                imageset = json_info[0]["imageset"]

            dat.tmpl_str = imageset[0]["template"]

            logger.info("dat.tmpl_str = %s", dat.tmpl_str)

        except (KeyError, IndexError):
            logger.info("failed to find template in JSON file")

        dat.ref2exp = exp

    return dat