Пример #1
0
def main(xparm_file, spot_file):

  import dxtbx
  from dxtbx.serialize.xds import to_crystal
  models = dxtbx.load(xparm_file)
  crystal_model = to_crystal(xparm_file)

  from dxtbx.model.experiment.experiment_list import Experiment, ExperimentList
  experiment = Experiment(beam=models.get_beam(),
                          detector=models.get_detector(),
                          goniometer=models.get_goniometer(),
                          scan=models.get_scan(),
                          crystal=crystal_model)

  detector = experiment.detector
  beam = experiment.beam
  goniometer = experiment.goniometer
  scan = experiment.scan

  from iotbx.xds import spot_xds
  spot_xds_handle = spot_xds.reader()
  spot_xds_handle.read_file(spot_file)

  from cctbx.array_family import flex
  centroids_px = flex.vec3_double(spot_xds_handle.centroid)
  miller_indices = flex.miller_index(spot_xds_handle.miller_index)

  # only those reflections that were actually indexed
  centroids_px = centroids_px.select(miller_indices != (0,0,0))
  miller_indices = miller_indices.select(miller_indices != (0,0,0))

  ub = crystal_model.get_A()

  d_spacings = [1.0 / (ub * mi).length() for mi in miller_indices]

  print max(d_spacings)

  # Convert Pixel coordinate into mm/rad
  x, y, z = centroids_px.parts()
  x_mm, y_mm = detector[0].pixel_to_millimeter(flex.vec2_double(x, y)).parts()
  z_rad = scan.get_angle_from_array_index(z, deg=False)
  centroids_mm = flex.vec3_double(x_mm, y_mm, z_rad)

  # then convert detector position to reciprocal space position

  # based on code in dials/algorithms/indexing/indexer2.py
  s1 = detector[0].get_lab_coord(flex.vec2_double(x_mm, y_mm))
  s1 = s1/s1.norms() * (1/beam.get_wavelength())
  S = s1 - beam.get_s0()
  reciprocal_space_points = S.rotate_around_origin(
    goniometer.get_rotation_axis(),
    -z_rad)

  d_spacings = 1/reciprocal_space_points.norms()
  dmax = flex.max(d_spacings)
  print dmax
Пример #2
0
def make_int_pickle(img_dict, filename):
    img_dict['current_orientation'] = [
        crystfel_to_cctbx_coord_system(
            img_dict['aStar'],
            img_dict['bStar'],
            img_dict['cStar'],
        )
    ]
    try:
        final_dict = {
            'observations': [unit_cell_to_symetry_object(img_dict)],
            'mapped_predictions': [flex.vec2_double(img_dict['location'])],
            'xbeam': 96.99,
            'ybeam': 96.97,
            'distance': 150.9,
            "sa_parameters": ['None'],
            "pointgroup": point_group,
            "unit_cell": img_dict['unit cell'],
            'current_orientation': img_dict['current_orientation'],
            'wavelength': img_dict['wavelength'],
            'centering': img_dict['centering'],
            'lattice_type': img_dict['lattice_type']
        }
        pickle.dump(final_dict, open(filename, 'wb'))
        logging.info("dumped image {}".format(filename))
    except AssertionError as a:
        logging.warning("Failed an assertion on image {}! {}".format(
            filename, a.message))
    except Exception:
        logging.warning(
            "Failed to make a dictionairy for image {}".format(filename))
Пример #3
0
 def compute_functional_and_gradients(self):
     x_set = np.array(self.x).reshape((len(self.x) // 2, 2))
     r_grid = flex.double(self.args)
     x_vec_set = flex.vec2_double(x_set)
     self.f = index_ambiguity.calc_BD_alg_2_sum_sqr(r_grid, x_vec_set)
     self.g = index_ambiguity.calc_gradient_BD_alg_2(r_grid, x_vec_set)
     return self.f, self.g
Пример #4
0
 def as_miller_arrays(self,
                      crystal_symmetry=None,
                      force_symmetry=False,
                      merge_equivalents=True,
                      base_array_info=None,
                      anomalous=None):
     if (base_array_info is None):
         base_array_info = miller.array_info(
             source_type="xds_integrate_hkl")
     from cctbx.array_family import flex
     from cctbx import crystal, miller, sgtbx
     crystal_symmetry = crystal.symmetry(
         unit_cell=self.unit_cell,
         space_group_info=sgtbx.space_group_info(number=self.space_group))
     indices = flex.miller_index(self.hkl)
     miller_set = miller.set(crystal_symmetry,
                             indices,
                             anomalous_flag=anomalous)
     return (miller.array(
         miller_set,
         data=flex.double(self.iobs),
         sigmas=flex.double(self.sigma)).set_info(
             base_array_info.customized_copy(labels=["iobs", "sigma_iobs"])
         ).set_observation_type_xray_intensity(),
             miller.array(miller_set, data=flex.vec3_double(
                 self.xyzcal)).set_info(
                     base_array_info.customized_copy(labels=["xyzcal"])),
             miller.array(miller_set, data=flex.vec3_double(
                 self.xyzobs)).set_info(
                     base_array_info.customized_copy(labels=["xyzobs"])),
             miller.array(miller_set, data=flex.double(self.rlp)).set_info(
                 base_array_info.customized_copy(labels=["rlp"])),
             miller.array(miller_set, data=flex.double(self.peak)).set_info(
                 base_array_info.customized_copy(labels=["peak"])),
             miller.array(miller_set, data=flex.double(self.corr)).set_info(
                 base_array_info.customized_copy(labels=["corr"])),
             miller.array(miller_set, data=flex.double(self.maxc)).set_info(
                 base_array_info.customized_copy(labels=["maxc"])),
             miller.array(miller_set, data=flex.vec2_double(
                 self.alfbet0)).set_info(
                     base_array_info.customized_copy(labels=["alfbet0"])),
             miller.array(miller_set, data=flex.vec2_double(
                 self.alfbet1)).set_info(
                     base_array_info.customized_copy(labels=["alfbet1"])),
             miller.array(miller_set, data=flex.double(self.psi)).set_info(
                 base_array_info.customized_copy(labels=["psi"])))
 def as_miller_arrays(self,
                      crystal_symmetry=None,
                      force_symmetry=False,
                      merge_equivalents=True,
                      base_array_info=None):
   if (base_array_info is None):
     base_array_info = miller.array_info(source_type="xds_integrate_hkl")
   from cctbx.array_family import flex
   from cctbx import crystal, miller, sgtbx
   crystal_symmetry = crystal.symmetry(
     unit_cell=self.unit_cell,
     space_group_info=sgtbx.space_group_info(number=self.space_group))
   indices = flex.miller_index(self.hkl)
   miller_set = miller.set(crystal_symmetry, indices)
   return (miller.array(
     miller_set, data=flex.double(self.iobs), sigmas=flex.double(self.sigma))
           .set_info(base_array_info.customized_copy(
             labels=["iobs", "sigma_iobs"])),
           miller.array(miller_set, data=flex.vec3_double(self.xyzcal))
           .set_info(base_array_info.customized_copy(
             labels=["xyzcal"])),
           miller.array(miller_set, data=flex.vec3_double(self.xyzobs))
           .set_info(base_array_info.customized_copy(
             labels=["xyzobs"])),
           miller.array(miller_set, data=flex.double(self.rlp))
           .set_info(base_array_info.customized_copy(
             labels=["rlp"])),
           miller.array(miller_set, data=flex.double(self.peak))
           .set_info(base_array_info.customized_copy(
             labels=["peak"])),
           miller.array(miller_set, data=flex.double(self.corr))
           .set_info(base_array_info.customized_copy(
             labels=["corr"])),
           miller.array(miller_set, data=flex.double(self.maxc))
           .set_info(base_array_info.customized_copy(
             labels=["maxc"])),
           miller.array(miller_set, data=flex.vec2_double(self.alfbet0))
           .set_info(base_array_info.customized_copy(
             labels=["alfbet0"])),
           miller.array(miller_set, data=flex.vec2_double(self.alfbet1))
           .set_info(base_array_info.customized_copy(
             labels=["alfbet1"])),
           miller.array(miller_set, data=flex.double(self.psi))
           .set_info(base_array_info.customized_copy(
             labels=["psi"])))
Пример #6
0
def get_experiment_xvectors(experiments):

    beam = experiments[0].beam
    detector = experiments[0].detector

    x = []

    for panel in detector:
        lab_coordinates = flex.vec3_double()
        pixels = flex.vec2_double(panel.get_image_size())
        mms = panel.pixel_to_millimeter(pixels)
        lab_coordinates.extend(panel.get_lab_coord(mms))

        # generate s1 vectors
        s1 = lab_coordinates.each_normalize() * (1 / beam.get_wavelength())
        # Generate x vectors
        x.append((s1 - beam.get_s0()))

    return (x)
Пример #7
0
def make_int_pickle(img_dict, filename):
  img_dict['current_orientation'] = [crystfel_to_cctbx_coord_system(
                    img_dict['aStar'],
                    img_dict['bStar'],
                    img_dict['cStar'],)]
  try:
    final_dict = {'observations': [unit_cell_to_symetry_object(img_dict)],
                  'mapped_predictions': [flex.vec2_double(img_dict['location'])],
                  'xbeam': 96.99,
                  'ybeam': 96.97,
                  'distance': 150.9,
                  "sa_parameters": ['None'],
                  "pointgroup": point_group,
                  "unit_cell": img_dict['unit cell'],
                  'current_orientation': img_dict['current_orientation'],
                  'wavelength': img_dict['wavelength'],
                  'centering': img_dict['centering'],
                  'lattice_type': img_dict['lattice_type']}
    pickle.dump(final_dict, open(filename, 'wb'))
    logging.info("dumped image {}".format(filename))
  except AssertionError as a:
    logging.warning("Failed an assertion on image {}! {}".format(filename, a.message))
  except Exception:
    logging.warning("Failed to make a dictionairy for image {}".format(filename))
Пример #8
0
def stereographic_projection(points, reference_poles):
  # http://dx.doi.org/10.1107/S0021889868005029
  # J. Appl. Cryst. (1968). 1, 68-70
  # The construction of stereographic projections by computer
  # G. K. Stokes, S. R. Keown and D. J. Dyson

  assert len(reference_poles) == 3
  r_0, r_1, r_2 = reference_poles

  projections = flex.vec2_double()

  for p in points:
    r_i = matrix.col(p)
    # theta is the angle between r_i and the plane normal, r_0
    cos_theta = r_i.cos_angle(r_0)
    if cos_theta < 0:
      r_i = -r_i
      cos_theta = r_i.cos_angle(r_0)

    # alpha is the angle between r_i and r_1
    cos_alpha = r_i.cos_angle(r_1)
    theta = math.acos(cos_theta)
    cos_phi = cos_alpha/math.sin(theta)
    if abs(cos_phi) > 1:
      cos_phi = math.copysign(1, cos_phi)
    phi = math.acos(cos_phi)

    N = r_i.dot(r_2)
    r = math.tan(theta/2)
    x = r * cos_phi
    y = r * math.sin(phi)
    y = math.copysign(y, N)

    projections.append((x,y))

  return projections
Пример #9
0
def stereographic_projection(points, reference_poles):
    # https://doi.org/10.1107/S0021889868005029
    # J. Appl. Cryst. (1968). 1, 68-70
    # The construction of stereographic projections by computer
    # G. K. Stokes, S. R. Keown and D. J. Dyson

    assert len(reference_poles) == 3
    r_0, r_1, r_2 = reference_poles

    projections = flex.vec2_double()

    for p in points:
        r_i = matrix.col(p)
        # theta is the angle between r_i and the plane normal, r_0
        cos_theta = r_i.cos_angle(r_0)
        if cos_theta < 0:
            r_i = -r_i
            cos_theta = r_i.cos_angle(r_0)

        # alpha is the angle between r_i and r_1
        cos_alpha = r_i.cos_angle(r_1)
        theta = math.acos(cos_theta)
        cos_phi = cos_alpha / math.sin(theta)
        if abs(cos_phi) > 1:
            cos_phi = math.copysign(1, cos_phi)
        phi = math.acos(cos_phi)

        N = r_i.dot(r_2)
        r = math.tan(theta / 2)
        x = r * cos_phi
        y = r * math.sin(phi)
        y = math.copysign(y, N)

        projections.append((x, y))

    return projections
Пример #10
0
def xds_check_indexer_solution(xparm_file, spot_file):
    '''Read XPARM file from XDS IDXREF (assumes that this is in the putative
  correct symmetry, not P1! and test centring operations if present. Note
  that a future version will boost to the putative correct symmetry (or
  an estimate of it) and try this if it is centred. Returns tuple
  (space_group_number, cell).'''

    from scitbx import matrix
    from dxtbx.serialize.xds import to_crystal as xparm_to_crystal
    cm = xparm_to_crystal(xparm_file)
    sg = cm.get_space_group()
    spacegroup = sg.type().hall_symbol()
    space_group_number = sg.type().number()
    A_inv = matrix.sqr(cm.get_A()).inverse()
    cell = cm.get_unit_cell().parameters()

    import dxtbx
    models = dxtbx.load(xparm_file)
    detector = models.get_detector()
    beam = models.get_beam()
    goniometer = models.get_goniometer()
    scan = models.get_scan()

    from iotbx.xds import spot_xds
    spot_xds_handle = spot_xds.reader()
    spot_xds_handle.read_file(spot_file)

    from cctbx.array_family import flex
    centroids_px = flex.vec3_double(spot_xds_handle.centroid)
    miller_indices = flex.miller_index(spot_xds_handle.miller_index)

    # Convert Pixel coordinate into mm/rad
    x, y, z = centroids_px.parts()
    x_mm, y_mm = detector[0].pixel_to_millimeter(flex.vec2_double(x,
                                                                  y)).parts()
    z_rad = scan.get_angle_from_array_index(z, deg=False)
    centroids_mm = flex.vec3_double(x_mm, y_mm, z_rad)

    # then convert detector position to reciprocal space position

    # based on code in dials/algorithms/indexing/indexer2.py
    s1 = detector[0].get_lab_coord(flex.vec2_double(x_mm, y_mm))
    s1 = s1 / s1.norms() * (1 / beam.get_wavelength())
    S = s1 - beam.get_s0()
    # XXX what about if goniometer fixed rotation is not identity?
    reciprocal_space_points = S.rotate_around_origin(
        goniometer.get_rotation_axis(), -z_rad)

    # now index the reflections
    hkl_float = tuple(A_inv) * reciprocal_space_points
    hkl_int = hkl_float.iround()

    # check if we are within 0.1 lattice spacings of the closest
    # lattice point - a for a random point this will be about 0.8% of
    # the time...
    differences = hkl_float - hkl_int.as_vec3_double()
    dh, dk, dl = [flex.abs(d) for d in differences.parts()]
    tolerance = 0.1
    sel = (dh < tolerance) and (dk < tolerance) and (dl < tolerance)

    is_sys_absent = sg.is_sys_absent(
        flex.miller_index(list(hkl_int.select(sel))))

    total = is_sys_absent.size()
    absent = is_sys_absent.count(True)
    present = total - absent

    # now, if the number of absences is substantial, need to consider
    # transforming this to a primitive basis

    Debug.write('Absent: %d  vs.  Present: %d Total: %d' % \
                (absent, present, total))

    # now see if this is compatible with a centred lattice or suggests
    # a primitive basis is correct

    sd = math.sqrt(absent)

    if (absent - 3 * sd) / total < 0.008:
        # everything is peachy

        return s2l(space_group_number), tuple(cell)

    # ok if we are here things are not peachy, so need to calculate the
    # spacegroup number without the translation operators

    sg_new = sg.build_derived_group(True, False)
    space_group_number_primitive = sg_new.type().number()

    # also determine the best setting for the new cell ...

    symm = crystal.symmetry(unit_cell=cell, space_group=sg_new)

    rdx = symm.change_of_basis_op_to_best_cell()
    symm_new = symm.change_basis(rdx)
    cell_new = symm_new.unit_cell().parameters()

    return s2l(space_group_number_primitive), tuple(cell_new)
Пример #11
0
def projections_as_dict(projections):
    projections_all = flex.vec2_double()
    for proj in projections:
        projections_all.extend(proj)

    data = []
    x, y = projections_all.parts()
    data.append(
        {
            "x": list(x),
            "y": list(y),
            "mode": "markers",
            "type": "scatter",
            "name": "stereographic_projections",
            "showlegend": False,
        }
    )
    data.append(
        {
            "x": [0],
            "y": [0],
            "mode": "markers",
            "marker": {
                "color": "black",
                "size": 25,
                "symbol": "cross-thin",
                "line": {"width": 1},
            },
            "showlegend": False,
        }
    )

    d = {
        "data": data,
        "layout": {
            "title": "Stereographic projections",
            "hovermode": False,
            "xaxis": {
                "range": [-1.0, 1.0],
                "showgrid": False,
                "zeroline": False,
                "showline": False,
                "ticks": "",
                "showticklabels": False,
            },
            "yaxis": {
                "range": [-1.0, 1.0],
                "showgrid": False,
                "zeroline": False,
                "showline": False,
                "ticks": "",
                "showticklabels": False,
            },
            "shapes": [
                {
                    "type": "circle",
                    "xref": "x",
                    "yref": "y",
                    "x0": -1,
                    "y0": -1,
                    "x1": 1,
                    "y1": 1,
                    "line": {"color": "black"},
                }
            ],
        },
    }
    return d
Пример #12
0
    Isize2 = I.size2
    pixel_size = SF.pixel_size
    # prepare the list of arguments to run pixmap in parallel
    pixmap_tasks = [(Isize1,Isize2,this_frame_phi_deg,SF.pixel_size,SF.size1,SF.size2,results.horizons_phil.spot_convention,procid) for procid in range(nproc)]
    # run pixmap in parallel and collect results
    raw_spot_input_it = pool.map(pixmapstar,pixmap_tasks)
    # gather pixmap results into a single collection of data points
    for raw_spot_input_this in raw_spot_input_it:
      raw_spot_input_all.extend(raw_spot_input_this)
      #      for j in raw_spot_input_it[i]:
      # raw_spot_input_all.append(j)
      # print "len(raw_spot_input_all) = ",len(raw_spot_input_all),"; I.size1*I.size2 = ",I.size1*I.size2
    """
    lab_coordinates = flex.vec3_double()
    for panel in detector: 
      pixels = flex.vec2_double(panel.get_image_size())
      #for j in xrange(panel.get_image_size()[1]):
      #  for i in xrange(panel.get_image_size()[0]):
#	  pixels.append((i,j))
      mms = panel.pixel_to_millimeter(pixels)
      lab_coordinates.extend(panel.get_lab_coord(mms))

    # generate s1 vectors
    s1_vectors = lab_coordinates.each_normalize() * (1/beam.get_wavelength())
    # Generate x vectors
    x_vectors = s1_vectors - beam.get_s0()

    print "there are ",x_vectors.size()," elements in x_vectors"
    tel = time()-t0
    print "done creating pixel map (",tel," sec)"
    
Пример #13
0
            terms0 = G.images_strong[key][HKL]["obs"] * terms1
            numerator += flex.sum(terms0)
            denominator += flex.sum(terms2)
        G.images_Gi[key] = numerator / denominator
        if key % 100 == 0: print(key, "Gi:", G.images_Gi[key])

    test_Gi_factor(G)
    """start here.  why are our Gi factors so screwed up?"""
    exit()

    print("Got Gi scale factors")
    from IPython import embed
    embed()
    # Now loop over energies and try to focus on the fpfdp problem
    result_energies = flex.double()
    result_FE1_fpfdp = flex.vec2_double()
    result_FE2_fpfdp = flex.vec2_double()
    for iE, Energy in enumerate(range(7110, 7131)):
        Eidx = iE + 20  # index into the G arrays, for that particular energy
        try:
            A = lbfgs_fpfdp_fit(energy=Energy, all_data=G, Eidx=Eidx)
            result_energies.append(Energy)
            result_FE1_fpfdp.append((A.a[0], A.a[1]))
            result_FE2_fpfdp.append((A.a[2], A.a[3]))
        except Exception:
            pass

    from matplotlib import pyplot as plt
    from LS49.sim.fdp_plot import george_sherrell
    GS = george_sherrell("data_sherrell/pf-rd-ox_fftkk.out")
    GS.plot_them(plt, f1="b.", f2="b.")
Пример #14
0
def xds_check_indexer_solution(xparm_file,
                               spot_file):
  '''Read XPARM file from XDS IDXREF (assumes that this is in the putative
  correct symmetry, not P1! and test centring operations if present. Note
  that a future version will boost to the putative correct symmetry (or
  an estimate of it) and try this if it is centred. Returns tuple
  (space_group_number, cell).'''

  from dxtbx.serialize.xds import to_crystal as xparm_to_crystal
  cm = xparm_to_crystal(xparm_file)
  sg = cm.get_space_group()
  spacegroup = sg.type().hall_symbol()
  space_group_number = sg.type().number()
  A_inv = cm.get_A().inverse()
  cell = cm.get_unit_cell().parameters()

  import dxtbx
  models = dxtbx.load(xparm_file)
  detector = models.get_detector()
  beam = models.get_beam()
  goniometer = models.get_goniometer()
  scan = models.get_scan()

  from iotbx.xds import spot_xds
  spot_xds_handle = spot_xds.reader()
  spot_xds_handle.read_file(spot_file)

  from cctbx.array_family import flex
  centroids_px = flex.vec3_double(spot_xds_handle.centroid)
  miller_indices = flex.miller_index(spot_xds_handle.miller_index)

  # Convert Pixel coordinate into mm/rad
  x, y, z = centroids_px.parts()
  x_mm, y_mm = detector[0].pixel_to_millimeter(flex.vec2_double(x, y)).parts()
  z_rad = scan.get_angle_from_array_index(z, deg=False)
  centroids_mm = flex.vec3_double(x_mm, y_mm, z_rad)

  # then convert detector position to reciprocal space position

  # based on code in dials/algorithms/indexing/indexer2.py
  s1 = detector[0].get_lab_coord(flex.vec2_double(x_mm, y_mm))
  s1 = s1/s1.norms() * (1/beam.get_wavelength())
  S = s1 - beam.get_s0()
  # XXX what about if goniometer fixed rotation is not identity?
  reciprocal_space_points = S.rotate_around_origin(
    goniometer.get_rotation_axis(),
    -z_rad)

  # now index the reflections
  hkl_float = tuple(A_inv) * reciprocal_space_points
  hkl_int = hkl_float.iround()

  # check if we are within 0.1 lattice spacings of the closest
  # lattice point - a for a random point this will be about 0.8% of
  # the time...
  differences = hkl_float - hkl_int.as_vec3_double()
  dh, dk, dl = [flex.abs(d) for d in differences.parts()]
  tolerance = 0.1
  sel = (dh < tolerance) and (dk < tolerance) and (dl < tolerance)

  is_sys_absent = sg.is_sys_absent(
    flex.miller_index(list(hkl_int.select(sel))))

  total = is_sys_absent.size()
  absent = is_sys_absent.count(True)
  present = total - absent

  # now, if the number of absences is substantial, need to consider
  # transforming this to a primitive basis

  Debug.write('Absent: %d  vs.  Present: %d Total: %d' % \
              (absent, present, total))

  # now see if this is compatible with a centred lattice or suggests
  # a primitive basis is correct

  sd = math.sqrt(absent)

  if (absent - 3 * sd) / total < 0.008:
    # everything is peachy

    return s2l(space_group_number), tuple(cell)

  # ok if we are here things are not peachy, so need to calculate the
  # spacegroup number without the translation operators

  sg_new = sg.build_derived_group(True, False)
  space_group_number_primitive = sg_new.type().number()

  # also determine the best setting for the new cell ...

  symm = crystal.symmetry(unit_cell = cell,
                          space_group = sg_new)

  rdx = symm.change_of_basis_op_to_best_cell()
  symm_new = symm.change_basis(rdx)
  cell_new = symm_new.unit_cell().parameters()

  return s2l(space_group_number_primitive), tuple(cell_new)
Пример #15
0
    #    I.read()
    #    DATA = I.linearintdata
    import dxtbx
    img = dxtbx.load(imgname)
    detector = img.get_detector()
    beam = img.get_beam()
    scan = img.get_scan()
    gonio = img.get_goniometer()

    print "transform pixel numbers to mm positions and rotational degrees"

    print "Creating pixel map..."
    t0 = time()
    lab_coordinates = flex.vec3_double()
    for panel in detector:
        pixels = flex.vec2_double(panel.get_image_size())
        #for j in xrange(panel.get_image_size()[1]):
        #  for i in xrange(panel.get_image_size()[0]):
        #	  pixels.append((i,j))
        mms = panel.pixel_to_millimeter(pixels)
        lab_coordinates.extend(panel.get_lab_coord(mms))

        # generate s1 vectors
    s1_vectors = lab_coordinates.each_normalize() * (1 / beam.get_wavelength())
    # Generate x vectors
    x_vectors = np.asarray(s1_vectors - beam.get_s0())

    #  print "there are ",x_vectors.size()," elements in x_vectors"
    tel = time() - t0
    print "done creating pixel map (", tel, " sec)"
def at_one_eV(eV, values, tst_delF=False):
    # values is required to be a flex double with (fp-FE1,fdp-FE1,fp-FE2,fdp-FE2)
    resolution = (2.095, 2.505)
    from LS49.sim.step5_pad import pdb_lines
    angstroms = eV_as_angstroms(eV)
    GF = gen_fmodel(resolution=resolution,
                    pdb_text=pdb_lines,
                    algorithm="fft",
                    wavelength=angstroms)
    GF.set_k_sol(0.435)
    GF.reset_wavelength(angstroms)
    GF.reset_specific_to_fpfdp(label_has="FE1", fp=values[0], fdp=values[1])
    GF.reset_specific_to_fpfdp(label_has="FE2", fp=values[2], fdp=values[3])
    ALLmodel, ALLcalc = GF.get_complex()
    I = ALLmodel.indices()
    All = ALLmodel.data()
    MetalloProtein = ALLcalc.data()
    Bulk = All - MetalloProtein
    re, im = All.parts()
    vec2All = flex.vec2_double(re, im)
    if tst_delF:
        delF_FE1fp = flex.vec2_double(len(I))
        delF_FE1fdp = flex.vec2_double(len(I))
        for workup in [("FE1  FES A 201", "FE1", values[0], values[1]),
                       ("FE1  FES B 202", "FE1", values[0], values[1])]:
            FP, FDP = single_atom_workup(workup, resolution, angstroms)
            delF_FE1fp += FP
            delF_FE1fdp += FDP
        print("OKT")

        delF_FE2fp = flex.vec2_double(len(I))
        delF_FE2fdp = flex.vec2_double(len(I))
        for workup in [("FE2  FES A 201", "FE2", values[2], values[3]),
                       ("FE2  FES B 202", "FE2", values[2], values[3])]:
            FP, FDP = single_atom_workup(workup, resolution, angstroms)
            delF_FE2fp += FP
            delF_FE2fdp += FDP
        print("OKT")
        re, im = MetalloProtein.parts()
        vec2MP = flex.vec2_double(re, im)

        return (vec2MP, delF_FE1fp, delF_FE1fdp, delF_FE2fp, delF_FE2fdp)
    # got the All data into vec2 form
    FdotdelF_FE1fp = 0.
    FdotdelF_FE1fdp = 0.
    for workup in [("FE1  FES A 201", "FE1", values[0], values[1]),
                   ("FE1  FES B 202", "FE1", values[0], values[1])]:
        FP, FDP = single_atom_workup(workup, resolution, angstroms)
        FdotdelF_FE1fp += vec2All.dot(FP)
        FdotdelF_FE1fdp += vec2All.dot(FDP)
        #print ("OK")

    FdotdelF_FE2fp = 0.
    FdotdelF_FE2fdp = 0.
    for workup in [("FE2  FES A 201", "FE2", values[2], values[3]),
                   ("FE2  FES B 202", "FE2", values[2], values[3])]:
        FP, FDP = single_atom_workup(workup, resolution, angstroms)
        FdotdelF_FE2fp += vec2All.dot(FP)
        FdotdelF_FE2fdp += vec2All.dot(FDP)
        #print ("OK")
    return (I, vec2All.dot(vec2All), FdotdelF_FE1fp, FdotdelF_FE1fdp,
            FdotdelF_FE2fp, FdotdelF_FE2fdp)
Пример #17
0
def projections_as_dict(projections):
    projections_all = flex.vec2_double()
    for proj in projections:
        projections_all.extend(proj)

    data = []
    x, y = projections_all.parts()
    data.append({
        'x': list(x),
        'y': list(y),
        'mode': 'markers',
        'type': 'scatter',
        'name': 'stereographic_projections',
        'showlegend': False,
    })
    data.append({
        'x': [0],
        'y': [0],
        'mode': 'markers',
        'marker': {
            'color': 'black',
            'size': 25,
            'symbol': 'cross-thin',
            'line': {
                'width': 1
            }
        },
        'showlegend': False,
    })

    d = {
        'data': data,
        'layout': {
            'title':
            'Stereographic projections',
            'hovermode':
            False,
            'xaxis': {
                'range': [-1.0, 1.0],
                'showgrid': False,
                'zeroline': False,
                'showline': False,
                'ticks': '',
                'showticklabels': False,
            },
            'yaxis': {
                'range': [-1.0, 1.0],
                'showgrid': False,
                'zeroline': False,
                'showline': False,
                'ticks': '',
                'showticklabels': False,
            },
            'shapes': [{
                'type': 'circle',
                'xref': 'x',
                'yref': 'y',
                'x0': -1,
                'y0': -1,
                'x1': 1,
                'y1': 1,
                'line': {
                    'color': 'black',
                }
            }]
        },
    }
    return d
Пример #18
0
def run(args):
  from dials.util.options import OptionParser
  import libtbx.load_env

  usage = "%s [options] find_spots.json" %(
    libtbx.env.dispatcher_name)

  parser = OptionParser(
    usage=usage,
    phil=phil_scope,
    epilog=help_message)

  params, options, args = parser.parse_args(
    show_diff_phil=True, return_unhandled=True)

  positions = None
  if params.positions is not None:
    with open(params.positions, 'rb') as f:
      positions = flex.vec2_double()
      for line in f.readlines():
        line = line.replace('(', ' ').replace(')', '').replace(',', ' ').strip().split()
        assert len(line) == 3
        i, x, y = [float(l) for l in line]
        positions.append((x, y))

  assert len(args) == 1
  json_file = args[0]
  import json

  with open(json_file, 'rb') as f:
    results = json.load(f)

  n_indexed = flex.double()
  fraction_indexed = flex.double()
  n_spots = flex.double()
  n_lattices = flex.double()
  crystals = []
  image_names = flex.std_string()

  for r in results:
    n_spots.append(r['n_spots_total'])
    image_names.append(str(r['image']))
    if 'n_indexed' in r:
      n_indexed.append(r['n_indexed'])
      fraction_indexed.append(r['fraction_indexed'])
      n_lattices.append(len(r['lattices']))
      for d in r['lattices']:
        from dxtbx.serialize.crystal import from_dict
        crystals.append(from_dict(d['crystal']))
    else:
      n_indexed.append(0)
      fraction_indexed.append(0)
      n_lattices.append(0)

  import matplotlib
  matplotlib.use('Agg')
  from matplotlib import pyplot

  blue = '#3498db'
  red = '#e74c3c'

  marker = 'o'
  alpha = 0.5
  lw = 0

  plot = True
  table = True
  grid = params.grid

  from libtbx import group_args
  from dials.algorithms.peak_finding.per_image_analysis \
       import plot_stats, print_table

  estimated_d_min = flex.double()
  d_min_distl_method_1 = flex.double()
  d_min_distl_method_2 = flex.double()
  n_spots_total = flex.int()
  n_spots_no_ice = flex.int()
  total_intensity = flex.double()

  for d in results:
    estimated_d_min.append(d['estimated_d_min'])
    d_min_distl_method_1.append(d['d_min_distl_method_1'])
    d_min_distl_method_2.append(d['d_min_distl_method_2'])
    n_spots_total.append(d['n_spots_total'])
    n_spots_no_ice.append(d['n_spots_no_ice'])
    total_intensity.append(d['total_intensity'])

  stats = group_args(image=image_names,
                     n_spots_total=n_spots_total,
                     n_spots_no_ice=n_spots_no_ice,
                     n_spots_4A=None,
                     total_intensity=total_intensity,
                     estimated_d_min=estimated_d_min,
                     d_min_distl_method_1=d_min_distl_method_1,
                     d_min_distl_method_2=d_min_distl_method_2,
                     noisiness_method_1=None,
                     noisiness_method_2=None)

  if plot:
    plot_stats(stats)
    pyplot.clf()
  if table:
    print_table(stats)

  print "Number of indexed lattices: ", (n_indexed > 0).count(True)

  print "Number with valid d_min but failed indexing: ", (
    (d_min_distl_method_1 > 0) &
    (d_min_distl_method_2 > 0) &
    (estimated_d_min > 0) &
    (n_indexed == 0)).count(True)

  n_rows = 10
  n_rows = min(n_rows, len(n_spots_total))
  perm_n_spots_total = flex.sort_permutation(n_spots_total, reverse=True)
  print 'Top %i images sorted by number of spots:' %n_rows
  print_table(stats, perm=perm_n_spots_total, n_rows=n_rows)

  n_bins = 20
  spot_count_histogram(
    n_spots_total, n_bins=n_bins, filename='hist_n_spots_total.png', log=True)
  spot_count_histogram(
    n_spots_no_ice, n_bins=n_bins, filename='hist_n_spots_no_ice.png', log=True)
  spot_count_histogram(
    n_indexed.select(n_indexed > 0), n_bins=n_bins, filename='hist_n_indexed.png', log=False)

  if len(crystals):
    plot_unit_cell_histograms(crystals)

  if params.stereographic_projections and len(crystals):
    from dxtbx.datablock import DataBlockFactory
    datablocks = DataBlockFactory.from_filenames(
      [image_names[0]], verbose=False)
    assert len(datablocks) == 1
    imageset = datablocks[0].extract_imagesets()[0]
    s0 = imageset.get_beam().get_s0()
    # XXX what if no goniometer?
    rotation_axis = imageset.get_goniometer().get_rotation_axis()

    indices = ((1,0,0), (0,1,0), (0,0,1))
    for i, index in enumerate(indices):

      from cctbx import crystal, miller
      from scitbx import matrix
      miller_indices = flex.miller_index([index])
      symmetry = crystal.symmetry(
        unit_cell=crystals[0].get_unit_cell(),
        space_group=crystals[0].get_space_group())
      miller_set = miller.set(symmetry, miller_indices)
      d_spacings = miller_set.d_spacings()
      d_spacings = d_spacings.as_non_anomalous_array().expand_to_p1()
      d_spacings = d_spacings.generate_bijvoet_mates()
      miller_indices = d_spacings.indices()

      # plane normal
      d0 = matrix.col(s0).normalize()
      d1 = d0.cross(matrix.col(rotation_axis)).normalize()
      d2 = d1.cross(d0).normalize()
      reference_poles = (d0, d1, d2)

      from dials.command_line.stereographic_projection import stereographic_projection
      projections = []

      for cryst in crystals:
        reciprocal_space_points = list(cryst.get_U() * cryst.get_B()) * miller_indices.as_vec3_double()
        projections.append(stereographic_projection(
          reciprocal_space_points, reference_poles))

        #from dials.algorithms.indexing.compare_orientation_matrices import \
        #  difference_rotation_matrix_and_euler_angles
        #R_ij, euler_angles, cb_op = difference_rotation_matrix_and_euler_angles(
        #  crystals[0], cryst)
        #print max(euler_angles)

      from dials.command_line.stereographic_projection import plot_projections
      plot_projections(projections, filename='projections_%s.png' %('hkl'[i]))
      pyplot.clf()

  def plot_grid(values, grid, file_name, cmap=pyplot.cm.Reds,
                vmin=None, vmax=None, invalid='white'):
    values = values.as_double()
    # At DLS, fast direction appears to be largest direction
    if grid[0] > grid[1]:
      values.reshape(flex.grid(reversed(grid)))
      values = values.matrix_transpose()
    else:
      values.reshape(flex.grid(grid))

    Z = values.as_numpy_array()

    #f, (ax1, ax2) = pyplot.subplots(2)
    f, ax1 = pyplot.subplots(1)

    mesh1 = ax1.pcolormesh(
      values.as_numpy_array(), cmap=cmap, vmin=vmin, vmax=vmax)
    mesh1.cmap.set_under(color=invalid, alpha=None)
    mesh1.cmap.set_over(color=invalid, alpha=None)
    #mesh2 = ax2.contour(Z, cmap=cmap, vmin=vmin, vmax=vmax)
    #mesh2 = ax2.contourf(Z, cmap=cmap, vmin=vmin, vmax=vmax)
    ax1.set_aspect('equal')
    ax1.invert_yaxis()
    #ax2.set_aspect('equal')
    #ax2.invert_yaxis()
    pyplot.colorbar(mesh1, ax=ax1)
    #pyplot.colorbar(mesh2, ax=ax2)
    pyplot.savefig(file_name, dpi=600)
    pyplot.clf()

  def plot_positions(values, positions, file_name, cmap=pyplot.cm.Reds,
                     vmin=None, vmax=None, invalid='white'):
    values = values.as_double()
    assert positions.size() >= values.size()
    positions = positions[:values.size()]

    if vmin is None:
      vmin = flex.min(values)
    if vmax is None:
      vmax = flex.max(values)

    x, y = positions.parts()
    dx = flex.abs(x[1:] - x[:-1])
    dy = flex.abs(y[1:] - y[:-1])
    dx = dx.select(dx > 0)
    dy = dy.select(dy > 0)

    scale = 1/flex.min(dx)
    #print scale
    x = (x * scale).iround()
    y = (y * scale).iround()

    from libtbx.math_utils import iceil
    z = flex.double(flex.grid(iceil(flex.max(y))+1, iceil(flex.max(x))+1), -2)
    #print z.all()
    for x_, y_, z_ in zip(x, y, values):
      z[y_, x_] = z_

    plot_grid(z.as_1d(), z.all(), file_name, cmap=cmap, vmin=vmin, vmax=vmax,
              invalid=invalid)
    return

  if grid is not None or positions is not None:
    if grid is not None:
      positions = tuple(reversed(grid))
      plotter = plot_grid
    else:
      plotter = plot_positions

    cmap = pyplot.get_cmap(params.cmap)
    plotter(n_spots_total, positions, 'grid_spot_count_total.png', cmap=cmap,
            invalid=params.invalid)
    plotter(n_spots_no_ice, positions, 'grid_spot_count_no_ice.png', cmap=cmap,
            invalid=params.invalid)
    plotter(total_intensity, positions, 'grid_total_intensity.png', cmap=cmap,
            invalid=params.invalid)
    if flex.max(n_indexed) > 0:
      plotter(n_indexed, positions, 'grid_n_indexed.png', cmap=cmap,
              invalid=params.invalid)
      plotter(fraction_indexed, positions, 'grid_fraction_indexed.png',
              cmap=cmap, vmin=0, vmax=1, invalid=params.invalid)

    for i, d_min in enumerate((estimated_d_min, d_min_distl_method_1, d_min_distl_method_2)):
      from cctbx import uctbx
      d_star_sq = uctbx.d_as_d_star_sq(d_min)
      d_star_sq.set_selected(d_star_sq == 1, 0)
      vmin = flex.min(d_star_sq.select(d_star_sq > 0))
      vmax = flex.max(d_star_sq)

      vmin = flex.min(d_min.select(d_min > 0))
      vmax = flex.max(d_min)
      cmap = pyplot.get_cmap('%s_r' %params.cmap)
      d_min.set_selected(d_min <= 0, vmax)

      if i == 0:
        plotter(d_min, positions, 'grid_d_min.png', cmap=cmap, vmin=vmin,
                vmax=vmax, invalid=params.invalid)
      else:
        plotter(
          d_min, positions, 'grid_d_min_method_%i.png' %i, cmap=cmap,
          vmin=vmin, vmax=vmax, invalid=params.invalid)

  if flex.max(n_indexed) > 0:
    pyplot.hexbin(
      n_spots, n_indexed, bins='log', cmap=pyplot.cm.jet, gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(n_spots, n_indexed, marker=marker, alpha=alpha, c=blue, lw=lw)
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    pyplot.plot([0, max(n_spots)], [0, max(n_spots)], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel('# spots')
    pyplot.ylabel('# indexed')
    pyplot.savefig('n_spots_vs_n_indexed.png')
    pyplot.clf()

    pyplot.hexbin(
      n_spots, fraction_indexed, bins='log', cmap=pyplot.cm.jet, gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(
      #n_spots, fraction_indexed, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel('# spots')
    pyplot.ylabel('Fraction indexed')
    pyplot.savefig('n_spots_vs_fraction_indexed.png')
    pyplot.clf()

    pyplot.hexbin(
      n_indexed, fraction_indexed, bins='log', cmap=pyplot.cm.jet, gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(
      #n_indexed, fraction_indexed, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel('# indexed')
    pyplot.ylabel('Fraction indexed')
    pyplot.savefig('n_indexed_vs_fraction_indexed.png')
    pyplot.clf()

    pyplot.hexbin(
      n_spots, n_lattices, bins='log', cmap=pyplot.cm.jet, gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(
      #n_spots, n_lattices, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel('# spots')
    pyplot.ylabel('# lattices')
    pyplot.savefig('n_spots_vs_n_lattices.png')
    pyplot.clf()

  #pyplot.scatter(
  #  estimated_d_min, d_min_distl_method_1, marker=marker, alpha=alpha, c=blue, lw=lw)
  pyplot.hexbin(estimated_d_min, d_min_distl_method_1, bins='log',
                cmap=pyplot.cm.jet, gridsize=50)
  pyplot.colorbar()
  #pyplot.gca().set_aspect('equal')
  xlim = pyplot.xlim()
  ylim = pyplot.ylim()
  m = max(max(estimated_d_min), max(d_min_distl_method_1))
  pyplot.plot([0, m], [0, m], c=red)
  pyplot.xlim(0, xlim[1])
  pyplot.ylim(0, ylim[1])
  pyplot.xlabel('estimated_d_min')
  pyplot.ylabel('d_min_distl_method_1')
  pyplot.savefig('d_min_vs_distl_method_1.png')
  pyplot.clf()

  #pyplot.scatter(
  #  estimated_d_min, d_min_distl_method_2, marker=marker, alpha=alpha, c=blue, lw=lw)
  pyplot.hexbin(estimated_d_min, d_min_distl_method_2, bins='log',
                cmap=pyplot.cm.jet, gridsize=50)
  pyplot.colorbar()
  #pyplot.gca().set_aspect('equal')
  xlim = pyplot.xlim()
  ylim = pyplot.ylim()
  m = max(max(estimated_d_min), max(d_min_distl_method_2))
  pyplot.plot([0, m], [0, m], c=red)
  pyplot.xlim(0, xlim[1])
  pyplot.ylim(0, ylim[1])
  pyplot.xlabel('estimated_d_min')
  pyplot.ylabel('d_min_distl_method_2')
  pyplot.savefig('d_min_vs_distl_method_2.png')
  pyplot.clf()

  #pyplot.scatter(
  #  d_min_distl_method_1, d_min_distl_method_2, marker=marker, alpha=alpha, c=blue, lw=lw)
  pyplot.hexbin(d_min_distl_method_1, d_min_distl_method_2, bins='log',
                cmap=pyplot.cm.jet, gridsize=50)
  pyplot.colorbar()
  #pyplot.gca().set_aspect('equal')
  xlim = pyplot.xlim()
  ylim = pyplot.ylim()
  m = max(max(d_min_distl_method_1), max(d_min_distl_method_2))
  pyplot.plot([0, m], [0, m], c=red)
  pyplot.xlim(0, xlim[1])
  pyplot.ylim(0, ylim[1])
  pyplot.xlabel('d_min_distl_method_1')
  pyplot.ylabel('d_min_distl_method_2')
  pyplot.savefig('distl_method_1_vs_distl_method_2.png')
  pyplot.clf()

  pyplot.hexbin(
    n_spots, estimated_d_min, bins='log', cmap=pyplot.cm.jet, gridsize=50)
  pyplot.colorbar()
  #pyplot.scatter(
    #n_spots, estimated_d_min, marker=marker, alpha=alpha, c=blue, lw=lw)
  pyplot.xlim(0, pyplot.xlim()[1])
  pyplot.ylim(0, pyplot.ylim()[1])
  pyplot.xlabel('# spots')
  pyplot.ylabel('estimated_d_min')
  pyplot.savefig('n_spots_vs_d_min.png')
  pyplot.clf()

  pyplot.hexbin(
    n_spots, d_min_distl_method_1, bins='log', cmap=pyplot.cm.jet, gridsize=50)
  pyplot.colorbar()
  #pyplot.scatter(
    #n_spots, d_min_distl_method_1, marker=marker, alpha=alpha, c=blue, lw=lw)
  pyplot.xlim(0, pyplot.xlim()[1])
  pyplot.ylim(0, pyplot.ylim()[1])
  pyplot.xlabel('# spots')
  pyplot.ylabel('d_min_distl_method_1')
  pyplot.savefig('n_spots_vs_distl_method_1.png')
  pyplot.clf()

  pyplot.hexbin(
    n_spots, d_min_distl_method_2, bins='log', cmap=pyplot.cm.jet, gridsize=50)
  pyplot.colorbar()
  #pyplot.scatter(
    #n_spots, d_min_distl_method_2, marker=marker, alpha=alpha, c=blue, lw=lw)
  pyplot.xlim(0, pyplot.xlim()[1])
  pyplot.ylim(0, pyplot.ylim()[1])
  pyplot.xlabel('# spots')
  pyplot.ylabel('d_min_distl_method_2')
  pyplot.savefig('n_spots_vs_distl_method_2.png')
  pyplot.clf()
Пример #19
0
def run_detail(show_plot, save_plot):
    P = Profiler("0. Read data")
    import pickle
    DP = pickle.load(open("mbhresults.pickle", "rb"))
    coord_x = DP.get("coord_x")
    coord_y = DP.get("coord_y")
    del P

    print(("There are %d points" % (len(coord_x))))
    if show_plot or save_plot:
        import matplotlib
        if not show_plot:
            # http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear
            matplotlib.use('Agg')  # use a non-interactive backend
        from matplotlib import pyplot as plt
        plt.plot(coord_x, coord_y, "k.", markersize=3.)
        plt.axes().set_aspect("equal")
        if save_plot:
            plt.savefig(plot_name,
                        size_inches=(10, 10),
                        dpi=300,
                        bbox_inches='tight')
        if show_plot:
            plt.show()

    print("Now constructing a Dij matrix.")
    P = Profiler("1. compute Dij matrix")
    NN = len(coord_x)
    embedded_vec2 = flex.vec2_double(coord_x, coord_y)
    Dij = embedded_vec2.distance_matrix(embedded_vec2)
    del P

    d_c = 0.04  # the distance cutoff, such that average item neighbors 1-2% of all items
    CM = clustering_manager(Dij=Dij, d_c=d_c)

    appcolors = [
        'b', 'r', '#ff7f0e', '#2ca02c', '#9467bd', '#8c564b', '#e377c2',
        '#7f7f7f', '#bcbd22', '#17becf'
    ]
    if show_plot:
        #Decision graph
        from matplotlib import pyplot as plt
        plt.plot(CM.rho, CM.delta, "r.", markersize=3.)
        for x in range(NN):
            if CM.cluster_id_maxima[x] >= 0:
                plt.plot([CM.rho[x]], [CM.delta[x]], "ro")
        plt.show()

        #No-halo plot
        from matplotlib import pyplot as plt
        colors = [appcolors[i % 10] for i in CM.cluster_id_full]

        plt.scatter(coord_x,
                    coord_y,
                    marker='o',
                    color=colors,
                    linewidths=0.4,
                    edgecolor='k')
        plt.axes().set_aspect("equal")
        plt.show()

        #Final plot
        halo = (CM.cluster_id_final == -1)
        core = ~halo
        plt.plot(coord_x.select(halo), coord_y.select(halo), "k.")
        colors = [appcolors[i % 10] for i in CM.cluster_id_final.select(core)]
        plt.scatter(coord_x.select(core),
                    coord_y.select(core),
                    marker="o",
                    color=colors,
                    linewidths=0.4,
                    edgecolor='k')
        plt.axes().set_aspect("equal")
        plt.show()
Пример #20
0
    def ewald_proximity_test(self, beams, hkl_list, A, detector):
        ''' test used to determine which spots are on the Ewald sphere,
    returns a dictionary of results including a reflection table, spots arrays
    and orientation matrix '''

        wavelength1 = beams[0].get_wavelength()
        wavelength2 = beams[1].get_wavelength()
        s0_1 = col((0, 0, -1 / wavelength1))
        s0_2 = col((0, 0, -1 / wavelength2))

        #create variables for h arrays
        filtered1 = flex.miller_index()
        #create variables for spots indices
        spots1 = flex.vec2_double()

        #create variables for h arrays
        filtered2 = flex.miller_index()
        #create variables for spots indices
        spots2 = flex.vec2_double()

        self.A = A
        q = flex.mat3_double([A] * len(
            hkl_list.all_selection())) * hkl_list.indices().as_vec3_double()

        EQ1 = q + s0_1
        EQ2 = q + s0_2
        len_EQ1 = flex.double([col(v).length() for v in EQ1])
        ratio1 = len_EQ1 * wavelength1
        len_EQ2 = flex.double([col(v).length() for v in EQ2])
        ratio2 = len_EQ2 * wavelength2

        for i in range(len(EQ1)):
            if ratio1[i] > 0.998 and ratio1[i] < 1.002:
                pix = detector[0].get_ray_intersection_px(EQ1[i])
                if detector[0].is_coord_valid(pix):
                    spots1.append(pix)
                    filtered1.append(hkl_list.indices()[i])

        for i in range(len(EQ2)):
            if ratio2[i] > 0.998 and ratio2[i] < 1.002:
                pix = detector[0].get_ray_intersection_px(EQ2[i])
                if detector[0].is_coord_valid(pix):
                    spots2.append(pix)
                    filtered2.append(hkl_list.indices()[i])

        #create reflection table
        #filtered s1 and r for reflection table

        spots = spots1.concatenate(spots2)
        lab_coord1 = flex.vec3_double(
            [detector[0].get_pixel_lab_coord(i) for i in spots1])
        lab_coord2 = flex.vec3_double(
            [detector[0].get_pixel_lab_coord(i) for i in spots2])
        s1_vecs1 = lab_coord1.each_normalize() * (1 / wavelength1)
        s0_vec1 = col((0, 0, -1 / wavelength1))
        r_vecs1 = s1_vecs1 - s0_vec1
        s1_vecs2 = lab_coord2.each_normalize() * (1 / wavelength2)
        s0_vec2 = col((0, 0, -1 / wavelength2))
        r_vecs2 = s1_vecs2 - s0_vec2

        #create one large reflection table for both experiments
        x_px, y_px = tuple(spots.parts()[0:2])

        px_array = flex.vec3_double(x_px, y_px, flex.double(len(x_px), 0.0))
        px_to_mm = detector[0].pixel_to_millimeter(spots)
        x_mm, y_mm = tuple(px_to_mm.parts()[0:2])
        mm_array = flex.vec3_double(x_mm, y_mm, flex.double(len(x_px), 0.0))
        px_var = flex.vec3_double(flex.double(len(x_px), 0.25),
                                  flex.double(len(x_px), 0.25),
                                  flex.double(len(x_px), 0.0))
        px_sq_err = flex.vec3_double(flex.double(len(x_px), 0.1),
                                     flex.double(len(x_px), 0.1),
                                     flex.double(len(x_px), 0.0))
        mm_var = flex.vec3_double(flex.double(len(x_px), 0.05),
                                  flex.double(len(x_px), 0.05),
                                  flex.double(len(x_px), 0.05))
        mm_sq_err = flex.vec3_double(flex.double(len(x_px), 0.01),
                                     flex.double(len(x_px), 0.01),
                                     flex.double(len(x_px), 0.01))

        obs_array = get_dials_obs(px_positions=px_array,
                                  px_var=px_var,
                                  px_sq_err=px_sq_err,
                                  mm_positions=mm_array,
                                  mm_var=mm_var,
                                  mm_sq_err=mm_sq_err)
        shoeboxes = flex.shoebox(len(obs_array))
        refl = flex.reflection_table(obs_array, shoeboxes)
        refl['id'] = flex.int(len(refl), -1)
        s1_vecs = s1_vecs1.concatenate(s1_vecs2)

        refl['s1'] = s1_vecs

        refl['xyzobs.px.variance'] = px_var
        refl['xyzobs.mm.value'] = mm_array
        refl['xyzobs.mm.variance'] = flex.vec3_double([
            (v[0] * 0.005, v[1] * 0.005, v[2] * 0.0)
            for v in refl['xyzobs.px.variance']
        ])
        filtered = filtered1.concatenate(filtered2)
        refl[
            'miller_index'] = filtered  # is reset to all 0s once close spots are merged
        bbox = flex.int6([(0, 1, 0, 1, 0, 1)] * len(refl))
        refl['bbox'] = bbox
        #refl['rlp'] = r_vecs
        refl['xyzobs.px.value'] = px_array

        # define experiment ids for each wavelength to check simulation with
        #indexing results in separate new column of reflection table not used in indexing
        exp_id1 = flex.int(len(spots1), 0)
        exp_id2 = flex.int(len(spots2), 1)
        exp_id = exp_id1.concatenate(exp_id2)
        refl['set_id'] = exp_id

        self.refl = refl
        sim_res = {
            'reflection_table': refl,
            'all_spots': spots,
            'wavelength_1_spots': spots1,
            'wavelength_2_spots': spots2,
            'input_orientation': A
        }

        return sim_res
Пример #21
0
  def find_peaks_clean(self):
    import omptbx
    # doesn't seem to be any benefit to using more than say 8 threads
    num_threads = min(8, omptbx.omp_get_num_procs(), self.params.nproc)
    omptbx.omp_set_num_threads(num_threads)
    d_min = self.params.fft3d.reciprocal_space_grid.d_min
    rlgrid = 2 / (d_min * self.gridding[0])

    frame_number = self.reflections['xyzobs.px.value'].parts()[2]
    scan_range_min = max(
      int(math.floor(flex.min(frame_number))),
      self.imagesets[0].get_array_range()[0]) # XXX what about multiple imagesets?
    scan_range_max = min(
      int(math.ceil(flex.max(frame_number))),
      self.imagesets[0].get_array_range()[1]) # XXX what about multiple imagesets?
    scan_range = self.params.scan_range
    if not len(scan_range):
      scan_range = [[scan_range_min, scan_range_max]]

    scan = self.imagesets[0].get_scan() # XXX what about multiple imagesets?
    angle_ranges = [
      [scan.get_angle_from_array_index(i, deg=False) for i in range_]
      for range_ in scan_range]

    grid = flex.double(flex.grid(self.gridding), 0)
    sampling_volume_map(grid, flex.vec2_double(angle_ranges),
                        self.imagesets[0].get_beam().get_s0(),
                        self.imagesets[0].get_goniometer().get_rotation_axis(),
                        rlgrid, d_min, self.params.b_iso)

    fft = fftpack.complex_to_complex_3d(self.gridding)
    grid_complex = flex.complex_double(
      reals=grid,
      imags=flex.double(grid.size(), 0))
    grid_transformed = fft.forward(grid_complex)
    grid_real = flex.pow2(flex.real(grid_transformed))

    gamma = 1
    peaks = flex.vec3_double()
    #n_peaks = 200
    n_peaks = 100 # XXX how many do we need?

    dirty_beam = grid_real
    dirty_map = self.grid_real.deep_copy()
    import time
    t0 = time.time()
    peaks = clean_3d(dirty_beam, dirty_map, n_peaks, gamma=gamma)
    t1 = time.time()
    #print "clean_3d took %.2f s" %(t1-t0)

    reciprocal_lattice_points = self.reflections['rlp'].select(
      self.reflections_used_for_indexing)

    peaks = self.optimise_peaks(peaks, reciprocal_lattice_points)

    peaks_frac = flex.vec3_double()
    for p in peaks:
      peaks_frac.append((p[0]/self.gridding[0],
                         p[1]/self.gridding[1],
                         p[2]/self.gridding[2]))
      #print p, peaks_frac[-1]

    if self.params.debug:
      self.debug_write_ccp4_map(grid, "sampling_volume.map")
      self.debug_write_ccp4_map(grid_real, "sampling_volume_FFT.map")
      self.debug_write_ccp4_map(dirty_map, "clean.map")

    self.sites = peaks_frac
    # we don't really know the "volume"" of the peaks, but this method should
    # find the peaks in order of their intensity (strongest first)
    self.volumes = flex.double(range(len(self.sites), 0, -1))

    return
Пример #22
0
def single_atom_workup(workup, resolution, angstroms):
    print("Testing f0 for workup %s" % (str(workup)))
    # Next, get F's with just Fe1,
    plines_src = open("1m2a.pdb", "r").readlines()
    plines_dst = []
    for line in plines_src:
        if (not ("HETATM" in line) and not ("ATOM" in line)) or (workup[0]
                                                                 in line):
            plines_dst.append(line)
    pdb_lines = "".join(plines_dst)
    #print (pdb_lines)
    GF = gen_fmodel(resolution=resolution,
                    pdb_text=pdb_lines,
                    algorithm="direct",
                    wavelength=angstroms,
                    verbose=False)
    GF.set_k_sol(0.435)
    GF.reset_wavelength(angstroms)
    GF.reset_specific_to_fpfdp(label_has=workup[1],
                               fp=workup[2],
                               fdp=workup[3])
    Fe1model, metalcalc = GF.get_complex()

    sc = GF.xray_structure.scatterers()[0]
    fpfdp = (complex(sc.fp, sc.fdp))

    GF.reset_wavelength(angstroms)
    GF.zero_out_specific_at_wavelength(label_has=workup[1])
    Fe1model, metalzcalc = GF.get_complex()
    re, im = metalzcalc.data().parts()
    vec2metalzcalc = flex.vec2_double(re, im)
    vecf0 = flex.double()
    #from IPython import embed; embed()
    # workaround for lack of a mat2_double class
    #delf_delfp = flex.mat2_double()
    #delf_delfdp = flex.mat2_double()
    delf_delfp1 = flex.vec2_double()
    delf_delfdp1 = flex.vec2_double()
    delf_delfp2 = flex.vec2_double()
    delf_delfdp2 = flex.vec2_double()

    for i in range(len(metalcalc.indices())):
        K1 = metalcalc.data()[i]
        K2 = metalzcalc.data()[i]
        lhs = (K1 / K2) - 1.
        f0 = fpfdp / lhs
        f0rr = f0.real
        vecf0.append(f0rr)
        #delf_delfp.append((1./f0rr,0.,0.,1./f0rr))
        #delf_delfdp.append((0.,-1./f0rr,1./f0rr,0.))
        delf_delfp1.append((1. / f0rr, 0.))
        delf_delfp2.append((0., 1. / f0rr))
        delf_delfdp1.append((
            0.,
            -1. / f0rr,
        ))
        delf_delfdp2.append((1. / f0rr, 0.))
    vec2an_bn = vec2metalzcalc
    #return delf_delfp * vec2an_bn, delf_delfdp * vec2an_bn
    return (flex.vec2_double(delf_delfp1.dot(vec2an_bn),
                             delf_delfp2.dot(vec2an_bn)),
            flex.vec2_double(delf_delfdp1.dot(vec2an_bn),
                             delf_delfdp2.dot(vec2an_bn)))
Пример #23
0
def run(args):
    from dials.util.options import OptionParser
    import libtbx.load_env

    usage = "%s [options] find_spots.expt" % (libtbx.env.dispatcher_name)

    parser = OptionParser(usage=usage, phil=phil_scope, epilog=help_message)

    params, options, args = parser.parse_args(
        show_diff_phil=True, return_unhandled=True
    )

    positions = None
    if params.positions is not None:
        with open(params.positions, "rb") as f:
            positions = flex.vec2_double()
            for line in f.readlines():
                line = (
                    line.replace("(", " ")
                    .replace(")", "")
                    .replace(",", " ")
                    .strip()
                    .split()
                )
                assert len(line) == 3
                i, x, y = [float(l) for l in line]
                positions.append((x, y))

    assert len(args) == 1
    json_file = args[0]
    import json

    with open(json_file, "rb") as f:
        results = json.load(f)

    n_indexed = flex.double()
    fraction_indexed = flex.double()
    n_spots = flex.double()
    n_lattices = flex.double()
    crystals = []
    image_names = flex.std_string()

    for r in results:
        n_spots.append(r["n_spots_total"])
        image_names.append(str(r["image"]))
        if "n_indexed" in r:
            n_indexed.append(r["n_indexed"])
            n_lattices.append(len(r["lattices"]))
            for d in r["lattices"]:
                from dxtbx.model.crystal import CrystalFactory

                crystals.append(CrystalFactory.from_dict(d["crystal"]))
        else:
            n_indexed.append(0)
            n_lattices.append(0)

    if n_indexed.size():
        sel = n_spots > 0
        fraction_indexed = flex.double(n_indexed.size(), 0)
        fraction_indexed.set_selected(sel, n_indexed.select(sel) / n_spots.select(sel))

    import matplotlib

    matplotlib.use("Agg")
    from matplotlib import pyplot

    red = "#e74c3c"

    plot = True
    table = True
    grid = params.grid

    from libtbx import group_args
    from dials.algorithms.spot_finding.per_image_analysis import plot_stats, print_table

    estimated_d_min = flex.double()
    d_min_distl_method_1 = flex.double()
    d_min_distl_method_2 = flex.double()
    n_spots_total = flex.int()
    n_spots_no_ice = flex.int()
    total_intensity = flex.double()

    for d in results:
        estimated_d_min.append(d["estimated_d_min"])
        d_min_distl_method_1.append(d["d_min_distl_method_1"])
        d_min_distl_method_2.append(d["d_min_distl_method_2"])
        n_spots_total.append(d["n_spots_total"])
        n_spots_no_ice.append(d["n_spots_no_ice"])
        total_intensity.append(d["total_intensity"])

    stats = group_args(
        image=image_names,
        n_spots_total=n_spots_total,
        n_spots_no_ice=n_spots_no_ice,
        n_spots_4A=None,
        n_indexed=n_indexed,
        fraction_indexed=fraction_indexed,
        total_intensity=total_intensity,
        estimated_d_min=estimated_d_min,
        d_min_distl_method_1=d_min_distl_method_1,
        d_min_distl_method_2=d_min_distl_method_2,
        noisiness_method_1=None,
        noisiness_method_2=None,
    )

    if plot:
        plot_stats(stats)
        pyplot.clf()
    if table:
        print_table(stats)

    n_rows = 10
    n_rows = min(n_rows, len(n_spots_total))
    perm_n_spots_total = flex.sort_permutation(n_spots_total, reverse=True)
    print("Top %i images sorted by number of spots:" % n_rows)
    print_table(stats, perm=perm_n_spots_total, n_rows=n_rows)
    if flex.max(n_indexed) > 0:
        perm_n_indexed = flex.sort_permutation(n_indexed, reverse=True)
        print("Top %i images sorted by number of indexed reflections:" % n_rows)
        print_table(stats, perm=perm_n_indexed, n_rows=n_rows)

    print("Number of indexed lattices: ", (n_indexed > 0).count(True))

    print(
        "Number with valid d_min but failed indexing: ",
        (
            (d_min_distl_method_1 > 0)
            & (d_min_distl_method_2 > 0)
            & (estimated_d_min > 0)
            & (n_indexed == 0)
        ).count(True),
    )

    n_bins = 20
    spot_count_histogram(
        n_spots_total, n_bins=n_bins, filename="hist_n_spots_total.png", log=True
    )
    spot_count_histogram(
        n_spots_no_ice, n_bins=n_bins, filename="hist_n_spots_no_ice.png", log=True
    )
    spot_count_histogram(
        n_indexed.select(n_indexed > 0),
        n_bins=n_bins,
        filename="hist_n_indexed.png",
        log=False,
    )

    if len(crystals):
        plot_unit_cell_histograms(crystals)

    if params.stereographic_projections and len(crystals):
        from dxtbx.model.experiment_list import ExperimentListFactory

        experiments = ExperimentListFactory.from_filenames(
            [image_names[0]], verbose=False
        )
        assert len(experiments) == 1
        imageset = experiments.imagesets()[0]
        s0 = imageset.get_beam().get_s0()
        # XXX what if no goniometer?
        rotation_axis = imageset.get_goniometer().get_rotation_axis()

        indices = ((1, 0, 0), (0, 1, 0), (0, 0, 1))
        for i, index in enumerate(indices):

            from cctbx import crystal, miller
            from scitbx import matrix

            miller_indices = flex.miller_index([index])
            symmetry = crystal.symmetry(
                unit_cell=crystals[0].get_unit_cell(),
                space_group=crystals[0].get_space_group(),
            )
            miller_set = miller.set(symmetry, miller_indices)
            d_spacings = miller_set.d_spacings()
            d_spacings = d_spacings.as_non_anomalous_array().expand_to_p1()
            d_spacings = d_spacings.generate_bijvoet_mates()
            miller_indices = d_spacings.indices()

            # plane normal
            d0 = matrix.col(s0).normalize()
            d1 = d0.cross(matrix.col(rotation_axis)).normalize()
            d2 = d1.cross(d0).normalize()
            reference_poles = (d0, d1, d2)

            from dials.command_line.stereographic_projection import (
                stereographic_projection,
            )

            projections = []

            for cryst in crystals:
                reciprocal_space_points = (
                    list(cryst.get_U() * cryst.get_B())
                    * miller_indices.as_vec3_double()
                )
                projections.append(
                    stereographic_projection(reciprocal_space_points, reference_poles)
                )

                # from dials.algorithms.indexing.compare_orientation_matrices import \
                #  difference_rotation_matrix_and_euler_angles
                # R_ij, euler_angles, cb_op = difference_rotation_matrix_and_euler_angles(
                #  crystals[0], cryst)
                # print max(euler_angles)

            from dials.command_line.stereographic_projection import plot_projections

            plot_projections(projections, filename="projections_%s.png" % ("hkl"[i]))
            pyplot.clf()

    def plot_grid(
        values,
        grid,
        file_name,
        cmap=pyplot.cm.Reds,
        vmin=None,
        vmax=None,
        invalid="white",
    ):
        values = values.as_double()
        # At DLS, fast direction appears to be largest direction
        if grid[0] > grid[1]:
            values.reshape(flex.grid(reversed(grid)))
            values = values.matrix_transpose()
        else:
            values.reshape(flex.grid(grid))

        f, ax1 = pyplot.subplots(1)

        mesh1 = ax1.pcolormesh(values.as_numpy_array(), cmap=cmap, vmin=vmin, vmax=vmax)
        mesh1.cmap.set_under(color=invalid, alpha=None)
        mesh1.cmap.set_over(color=invalid, alpha=None)
        ax1.set_aspect("equal")
        ax1.invert_yaxis()
        pyplot.colorbar(mesh1, ax=ax1)
        pyplot.savefig(file_name, dpi=600)
        pyplot.clf()

    def plot_positions(
        values,
        positions,
        file_name,
        cmap=pyplot.cm.Reds,
        vmin=None,
        vmax=None,
        invalid="white",
    ):
        values = values.as_double()
        assert positions.size() >= values.size()
        positions = positions[: values.size()]

        if vmin is None:
            vmin = flex.min(values)
        if vmax is None:
            vmax = flex.max(values)

        x, y = positions.parts()
        dx = flex.abs(x[1:] - x[:-1])
        dy = flex.abs(y[1:] - y[:-1])
        dx = dx.select(dx > 0)
        dy = dy.select(dy > 0)

        scale = 1 / flex.min(dx)
        # print scale
        x = (x * scale).iround()
        y = (y * scale).iround()

        from libtbx.math_utils import iceil

        z = flex.double(flex.grid(iceil(flex.max(y)) + 1, iceil(flex.max(x)) + 1), -2)
        # print z.all()
        for x_, y_, z_ in zip(x, y, values):
            z[y_, x_] = z_

        plot_grid(
            z.as_1d(),
            z.all(),
            file_name,
            cmap=cmap,
            vmin=vmin,
            vmax=vmax,
            invalid=invalid,
        )
        return

    if grid is not None or positions is not None:
        if grid is not None:
            positions = tuple(reversed(grid))
            plotter = plot_grid
        else:
            plotter = plot_positions

        cmap = pyplot.get_cmap(params.cmap)
        plotter(
            n_spots_total,
            positions,
            "grid_spot_count_total.png",
            cmap=cmap,
            invalid=params.invalid,
        )
        plotter(
            n_spots_no_ice,
            positions,
            "grid_spot_count_no_ice.png",
            cmap=cmap,
            invalid=params.invalid,
        )
        plotter(
            total_intensity,
            positions,
            "grid_total_intensity.png",
            cmap=cmap,
            invalid=params.invalid,
        )
        if flex.max(n_indexed) > 0:
            plotter(
                n_indexed,
                positions,
                "grid_n_indexed.png",
                cmap=cmap,
                invalid=params.invalid,
            )
            plotter(
                fraction_indexed,
                positions,
                "grid_fraction_indexed.png",
                cmap=cmap,
                vmin=0,
                vmax=1,
                invalid=params.invalid,
            )

        for i, d_min in enumerate(
            (estimated_d_min, d_min_distl_method_1, d_min_distl_method_2)
        ):
            vmin = flex.min(d_min.select(d_min > 0))
            vmax = flex.max(d_min)
            cmap = pyplot.get_cmap("%s_r" % params.cmap)
            d_min.set_selected(d_min <= 0, vmax)

            if i == 0:
                plotter(
                    d_min,
                    positions,
                    "grid_d_min.png",
                    cmap=cmap,
                    vmin=vmin,
                    vmax=vmax,
                    invalid=params.invalid,
                )
            else:
                plotter(
                    d_min,
                    positions,
                    "grid_d_min_method_%i.png" % i,
                    cmap=cmap,
                    vmin=vmin,
                    vmax=vmax,
                    invalid=params.invalid,
                )

    if flex.max(n_indexed) > 0:
        pyplot.hexbin(n_spots, n_indexed, bins="log", cmap=pyplot.cm.jet, gridsize=50)
        pyplot.colorbar()
        xlim = pyplot.xlim()
        ylim = pyplot.ylim()
        pyplot.plot([0, max(n_spots)], [0, max(n_spots)], c=red)
        pyplot.xlim(0, xlim[1])
        pyplot.ylim(0, ylim[1])
        pyplot.xlabel("# spots")
        pyplot.ylabel("# indexed")
        pyplot.savefig("n_spots_vs_n_indexed.png")
        pyplot.clf()

        pyplot.hexbin(
            n_spots, fraction_indexed, bins="log", cmap=pyplot.cm.jet, gridsize=50
        )
        pyplot.colorbar()
        pyplot.xlim(0, pyplot.xlim()[1])
        pyplot.ylim(0, pyplot.ylim()[1])
        pyplot.xlabel("# spots")
        pyplot.ylabel("Fraction indexed")
        pyplot.savefig("n_spots_vs_fraction_indexed.png")
        pyplot.clf()

        pyplot.hexbin(
            n_indexed, fraction_indexed, bins="log", cmap=pyplot.cm.jet, gridsize=50
        )
        pyplot.colorbar()
        pyplot.xlim(0, pyplot.xlim()[1])
        pyplot.ylim(0, pyplot.ylim()[1])
        pyplot.xlabel("# indexed")
        pyplot.ylabel("Fraction indexed")
        pyplot.savefig("n_indexed_vs_fraction_indexed.png")
        pyplot.clf()

        pyplot.hexbin(n_spots, n_lattices, bins="log", cmap=pyplot.cm.jet, gridsize=50)
        pyplot.colorbar()
        pyplot.xlim(0, pyplot.xlim()[1])
        pyplot.ylim(0, pyplot.ylim()[1])
        pyplot.xlabel("# spots")
        pyplot.ylabel("# lattices")
        pyplot.savefig("n_spots_vs_n_lattices.png")
        pyplot.clf()

    pyplot.hexbin(
        estimated_d_min,
        d_min_distl_method_1,
        bins="log",
        cmap=pyplot.cm.jet,
        gridsize=50,
    )
    pyplot.colorbar()
    # pyplot.gca().set_aspect('equal')
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    m = max(max(estimated_d_min), max(d_min_distl_method_1))
    pyplot.plot([0, m], [0, m], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel("estimated_d_min")
    pyplot.ylabel("d_min_distl_method_1")
    pyplot.savefig("d_min_vs_distl_method_1.png")
    pyplot.clf()

    pyplot.hexbin(
        estimated_d_min,
        d_min_distl_method_2,
        bins="log",
        cmap=pyplot.cm.jet,
        gridsize=50,
    )
    pyplot.colorbar()
    # pyplot.gca().set_aspect('equal')
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    m = max(max(estimated_d_min), max(d_min_distl_method_2))
    pyplot.plot([0, m], [0, m], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel("estimated_d_min")
    pyplot.ylabel("d_min_distl_method_2")
    pyplot.savefig("d_min_vs_distl_method_2.png")
    pyplot.clf()

    pyplot.hexbin(
        d_min_distl_method_1,
        d_min_distl_method_2,
        bins="log",
        cmap=pyplot.cm.jet,
        gridsize=50,
    )
    pyplot.colorbar()
    # pyplot.gca().set_aspect('equal')
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    m = max(max(d_min_distl_method_1), max(d_min_distl_method_2))
    pyplot.plot([0, m], [0, m], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel("d_min_distl_method_1")
    pyplot.ylabel("d_min_distl_method_2")
    pyplot.savefig("distl_method_1_vs_distl_method_2.png")
    pyplot.clf()

    pyplot.hexbin(n_spots, estimated_d_min, bins="log", cmap=pyplot.cm.jet, gridsize=50)
    pyplot.colorbar()
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel("# spots")
    pyplot.ylabel("estimated_d_min")
    pyplot.savefig("n_spots_vs_d_min.png")
    pyplot.clf()

    pyplot.hexbin(
        n_spots, d_min_distl_method_1, bins="log", cmap=pyplot.cm.jet, gridsize=50
    )
    pyplot.colorbar()
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel("# spots")
    pyplot.ylabel("d_min_distl_method_1")
    pyplot.savefig("n_spots_vs_distl_method_1.png")
    pyplot.clf()

    pyplot.hexbin(
        n_spots, d_min_distl_method_2, bins="log", cmap=pyplot.cm.jet, gridsize=50
    )
    pyplot.colorbar()
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel("# spots")
    pyplot.ylabel("d_min_distl_method_2")
    pyplot.savefig("n_spots_vs_distl_method_2.png")
    pyplot.clf()
Пример #24
0
def run(args):
    from dials.util.options import OptionParser
    import libtbx.load_env

    usage = "%s [options] find_spots.json" % (libtbx.env.dispatcher_name)

    parser = OptionParser(usage=usage, phil=phil_scope, epilog=help_message)

    params, options, args = parser.parse_args(show_diff_phil=True,
                                              return_unhandled=True)

    positions = None
    if params.positions is not None:
        with open(params.positions, 'rb') as f:
            positions = flex.vec2_double()
            for line in f.readlines():
                line = line.replace('(', ' ').replace(')', '').replace(
                    ',', ' ').strip().split()
                assert len(line) == 3
                i, x, y = [float(l) for l in line]
                positions.append((x, y))

    assert len(args) == 1
    json_file = args[0]
    import json

    with open(json_file, 'rb') as f:
        results = json.load(f)

    n_indexed = flex.double()
    fraction_indexed = flex.double()
    n_spots = flex.double()
    n_lattices = flex.double()
    crystals = []
    image_names = flex.std_string()

    for r in results:
        n_spots.append(r['n_spots_total'])
        image_names.append(str(r['image']))
        if 'n_indexed' in r:
            n_indexed.append(r['n_indexed'])
            n_lattices.append(len(r['lattices']))
            for d in r['lattices']:
                from dxtbx.serialize.crystal import from_dict
                crystals.append(from_dict(d['crystal']))
        else:
            n_indexed.append(0)
            n_lattices.append(0)

    if n_indexed.size():
        sel = n_spots > 0
        fraction_indexed = flex.double(n_indexed.size(), 0)
        fraction_indexed.set_selected(
            sel,
            n_indexed.select(sel) / n_spots.select(sel))

    import matplotlib
    matplotlib.use('Agg')
    from matplotlib import pyplot

    blue = '#3498db'
    red = '#e74c3c'

    marker = 'o'
    alpha = 0.5
    lw = 0

    plot = True
    table = True
    grid = params.grid

    from libtbx import group_args
    from dials.algorithms.spot_finding.per_image_analysis \
         import plot_stats, print_table

    estimated_d_min = flex.double()
    d_min_distl_method_1 = flex.double()
    d_min_distl_method_2 = flex.double()
    n_spots_total = flex.int()
    n_spots_no_ice = flex.int()
    total_intensity = flex.double()

    for d in results:
        estimated_d_min.append(d['estimated_d_min'])
        d_min_distl_method_1.append(d['d_min_distl_method_1'])
        d_min_distl_method_2.append(d['d_min_distl_method_2'])
        n_spots_total.append(d['n_spots_total'])
        n_spots_no_ice.append(d['n_spots_no_ice'])
        total_intensity.append(d['total_intensity'])

    stats = group_args(image=image_names,
                       n_spots_total=n_spots_total,
                       n_spots_no_ice=n_spots_no_ice,
                       n_spots_4A=None,
                       n_indexed=n_indexed,
                       fraction_indexed=fraction_indexed,
                       total_intensity=total_intensity,
                       estimated_d_min=estimated_d_min,
                       d_min_distl_method_1=d_min_distl_method_1,
                       d_min_distl_method_2=d_min_distl_method_2,
                       noisiness_method_1=None,
                       noisiness_method_2=None)

    if plot:
        plot_stats(stats)
        pyplot.clf()
    if table:
        print_table(stats)

    n_rows = 10
    n_rows = min(n_rows, len(n_spots_total))
    perm_n_spots_total = flex.sort_permutation(n_spots_total, reverse=True)
    print 'Top %i images sorted by number of spots:' % n_rows
    print_table(stats, perm=perm_n_spots_total, n_rows=n_rows)
    if flex.max(n_indexed) > 0:
        perm_n_indexed = flex.sort_permutation(n_indexed, reverse=True)
        print 'Top %i images sorted by number of indexed reflections:' % n_rows
        print_table(stats, perm=perm_n_indexed, n_rows=n_rows)

    print "Number of indexed lattices: ", (n_indexed > 0).count(True)

    print "Number with valid d_min but failed indexing: ", (
        (d_min_distl_method_1 > 0) & (d_min_distl_method_2 > 0) &
        (estimated_d_min > 0) & (n_indexed == 0)).count(True)

    n_bins = 20
    spot_count_histogram(n_spots_total,
                         n_bins=n_bins,
                         filename='hist_n_spots_total.png',
                         log=True)
    spot_count_histogram(n_spots_no_ice,
                         n_bins=n_bins,
                         filename='hist_n_spots_no_ice.png',
                         log=True)
    spot_count_histogram(n_indexed.select(n_indexed > 0),
                         n_bins=n_bins,
                         filename='hist_n_indexed.png',
                         log=False)

    if len(crystals):
        plot_unit_cell_histograms(crystals)

    if params.stereographic_projections and len(crystals):
        from dxtbx.datablock import DataBlockFactory
        datablocks = DataBlockFactory.from_filenames([image_names[0]],
                                                     verbose=False)
        assert len(datablocks) == 1
        imageset = datablocks[0].extract_imagesets()[0]
        s0 = imageset.get_beam().get_s0()
        # XXX what if no goniometer?
        rotation_axis = imageset.get_goniometer().get_rotation_axis()

        indices = ((1, 0, 0), (0, 1, 0), (0, 0, 1))
        for i, index in enumerate(indices):

            from cctbx import crystal, miller
            from scitbx import matrix
            miller_indices = flex.miller_index([index])
            symmetry = crystal.symmetry(
                unit_cell=crystals[0].get_unit_cell(),
                space_group=crystals[0].get_space_group())
            miller_set = miller.set(symmetry, miller_indices)
            d_spacings = miller_set.d_spacings()
            d_spacings = d_spacings.as_non_anomalous_array().expand_to_p1()
            d_spacings = d_spacings.generate_bijvoet_mates()
            miller_indices = d_spacings.indices()

            # plane normal
            d0 = matrix.col(s0).normalize()
            d1 = d0.cross(matrix.col(rotation_axis)).normalize()
            d2 = d1.cross(d0).normalize()
            reference_poles = (d0, d1, d2)

            from dials.command_line.stereographic_projection import stereographic_projection
            projections = []

            for cryst in crystals:
                reciprocal_space_points = list(
                    cryst.get_U() *
                    cryst.get_B()) * miller_indices.as_vec3_double()
                projections.append(
                    stereographic_projection(reciprocal_space_points,
                                             reference_poles))

                #from dials.algorithms.indexing.compare_orientation_matrices import \
                #  difference_rotation_matrix_and_euler_angles
                #R_ij, euler_angles, cb_op = difference_rotation_matrix_and_euler_angles(
                #  crystals[0], cryst)
                #print max(euler_angles)

            from dials.command_line.stereographic_projection import plot_projections
            plot_projections(projections,
                             filename='projections_%s.png' % ('hkl'[i]))
            pyplot.clf()

    def plot_grid(values,
                  grid,
                  file_name,
                  cmap=pyplot.cm.Reds,
                  vmin=None,
                  vmax=None,
                  invalid='white'):
        values = values.as_double()
        # At DLS, fast direction appears to be largest direction
        if grid[0] > grid[1]:
            values.reshape(flex.grid(reversed(grid)))
            values = values.matrix_transpose()
        else:
            values.reshape(flex.grid(grid))

        Z = values.as_numpy_array()

        #f, (ax1, ax2) = pyplot.subplots(2)
        f, ax1 = pyplot.subplots(1)

        mesh1 = ax1.pcolormesh(values.as_numpy_array(),
                               cmap=cmap,
                               vmin=vmin,
                               vmax=vmax)
        mesh1.cmap.set_under(color=invalid, alpha=None)
        mesh1.cmap.set_over(color=invalid, alpha=None)
        #mesh2 = ax2.contour(Z, cmap=cmap, vmin=vmin, vmax=vmax)
        #mesh2 = ax2.contourf(Z, cmap=cmap, vmin=vmin, vmax=vmax)
        ax1.set_aspect('equal')
        ax1.invert_yaxis()
        #ax2.set_aspect('equal')
        #ax2.invert_yaxis()
        pyplot.colorbar(mesh1, ax=ax1)
        #pyplot.colorbar(mesh2, ax=ax2)
        pyplot.savefig(file_name, dpi=600)
        pyplot.clf()

    def plot_positions(values,
                       positions,
                       file_name,
                       cmap=pyplot.cm.Reds,
                       vmin=None,
                       vmax=None,
                       invalid='white'):
        values = values.as_double()
        assert positions.size() >= values.size()
        positions = positions[:values.size()]

        if vmin is None:
            vmin = flex.min(values)
        if vmax is None:
            vmax = flex.max(values)

        x, y = positions.parts()
        dx = flex.abs(x[1:] - x[:-1])
        dy = flex.abs(y[1:] - y[:-1])
        dx = dx.select(dx > 0)
        dy = dy.select(dy > 0)

        scale = 1 / flex.min(dx)
        #print scale
        x = (x * scale).iround()
        y = (y * scale).iround()

        from libtbx.math_utils import iceil
        z = flex.double(
            flex.grid(iceil(flex.max(y)) + 1,
                      iceil(flex.max(x)) + 1), -2)
        #print z.all()
        for x_, y_, z_ in zip(x, y, values):
            z[y_, x_] = z_

        plot_grid(z.as_1d(),
                  z.all(),
                  file_name,
                  cmap=cmap,
                  vmin=vmin,
                  vmax=vmax,
                  invalid=invalid)
        return

    if grid is not None or positions is not None:
        if grid is not None:
            positions = tuple(reversed(grid))
            plotter = plot_grid
        else:
            plotter = plot_positions

        cmap = pyplot.get_cmap(params.cmap)
        plotter(n_spots_total,
                positions,
                'grid_spot_count_total.png',
                cmap=cmap,
                invalid=params.invalid)
        plotter(n_spots_no_ice,
                positions,
                'grid_spot_count_no_ice.png',
                cmap=cmap,
                invalid=params.invalid)
        plotter(total_intensity,
                positions,
                'grid_total_intensity.png',
                cmap=cmap,
                invalid=params.invalid)
        if flex.max(n_indexed) > 0:
            plotter(n_indexed,
                    positions,
                    'grid_n_indexed.png',
                    cmap=cmap,
                    invalid=params.invalid)
            plotter(fraction_indexed,
                    positions,
                    'grid_fraction_indexed.png',
                    cmap=cmap,
                    vmin=0,
                    vmax=1,
                    invalid=params.invalid)

        for i, d_min in enumerate(
            (estimated_d_min, d_min_distl_method_1, d_min_distl_method_2)):
            from cctbx import uctbx
            d_star_sq = uctbx.d_as_d_star_sq(d_min)
            d_star_sq.set_selected(d_star_sq == 1, 0)
            vmin = flex.min(d_star_sq.select(d_star_sq > 0))
            vmax = flex.max(d_star_sq)

            vmin = flex.min(d_min.select(d_min > 0))
            vmax = flex.max(d_min)
            cmap = pyplot.get_cmap('%s_r' % params.cmap)
            d_min.set_selected(d_min <= 0, vmax)

            if i == 0:
                plotter(d_min,
                        positions,
                        'grid_d_min.png',
                        cmap=cmap,
                        vmin=vmin,
                        vmax=vmax,
                        invalid=params.invalid)
            else:
                plotter(d_min,
                        positions,
                        'grid_d_min_method_%i.png' % i,
                        cmap=cmap,
                        vmin=vmin,
                        vmax=vmax,
                        invalid=params.invalid)

    if flex.max(n_indexed) > 0:
        pyplot.hexbin(n_spots,
                      n_indexed,
                      bins='log',
                      cmap=pyplot.cm.jet,
                      gridsize=50)
        pyplot.colorbar()
        #pyplot.scatter(n_spots, n_indexed, marker=marker, alpha=alpha, c=blue, lw=lw)
        xlim = pyplot.xlim()
        ylim = pyplot.ylim()
        pyplot.plot([0, max(n_spots)], [0, max(n_spots)], c=red)
        pyplot.xlim(0, xlim[1])
        pyplot.ylim(0, ylim[1])
        pyplot.xlabel('# spots')
        pyplot.ylabel('# indexed')
        pyplot.savefig('n_spots_vs_n_indexed.png')
        pyplot.clf()

        pyplot.hexbin(n_spots,
                      fraction_indexed,
                      bins='log',
                      cmap=pyplot.cm.jet,
                      gridsize=50)
        pyplot.colorbar()
        #pyplot.scatter(
        #n_spots, fraction_indexed, marker=marker, alpha=alpha, c=blue, lw=lw)
        pyplot.xlim(0, pyplot.xlim()[1])
        pyplot.ylim(0, pyplot.ylim()[1])
        pyplot.xlabel('# spots')
        pyplot.ylabel('Fraction indexed')
        pyplot.savefig('n_spots_vs_fraction_indexed.png')
        pyplot.clf()

        pyplot.hexbin(n_indexed,
                      fraction_indexed,
                      bins='log',
                      cmap=pyplot.cm.jet,
                      gridsize=50)
        pyplot.colorbar()
        #pyplot.scatter(
        #n_indexed, fraction_indexed, marker=marker, alpha=alpha, c=blue, lw=lw)
        pyplot.xlim(0, pyplot.xlim()[1])
        pyplot.ylim(0, pyplot.ylim()[1])
        pyplot.xlabel('# indexed')
        pyplot.ylabel('Fraction indexed')
        pyplot.savefig('n_indexed_vs_fraction_indexed.png')
        pyplot.clf()

        pyplot.hexbin(n_spots,
                      n_lattices,
                      bins='log',
                      cmap=pyplot.cm.jet,
                      gridsize=50)
        pyplot.colorbar()
        #pyplot.scatter(
        #n_spots, n_lattices, marker=marker, alpha=alpha, c=blue, lw=lw)
        pyplot.xlim(0, pyplot.xlim()[1])
        pyplot.ylim(0, pyplot.ylim()[1])
        pyplot.xlabel('# spots')
        pyplot.ylabel('# lattices')
        pyplot.savefig('n_spots_vs_n_lattices.png')
        pyplot.clf()

    #pyplot.scatter(
    #  estimated_d_min, d_min_distl_method_1, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.hexbin(estimated_d_min,
                  d_min_distl_method_1,
                  bins='log',
                  cmap=pyplot.cm.jet,
                  gridsize=50)
    pyplot.colorbar()
    #pyplot.gca().set_aspect('equal')
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    m = max(max(estimated_d_min), max(d_min_distl_method_1))
    pyplot.plot([0, m], [0, m], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel('estimated_d_min')
    pyplot.ylabel('d_min_distl_method_1')
    pyplot.savefig('d_min_vs_distl_method_1.png')
    pyplot.clf()

    #pyplot.scatter(
    #  estimated_d_min, d_min_distl_method_2, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.hexbin(estimated_d_min,
                  d_min_distl_method_2,
                  bins='log',
                  cmap=pyplot.cm.jet,
                  gridsize=50)
    pyplot.colorbar()
    #pyplot.gca().set_aspect('equal')
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    m = max(max(estimated_d_min), max(d_min_distl_method_2))
    pyplot.plot([0, m], [0, m], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel('estimated_d_min')
    pyplot.ylabel('d_min_distl_method_2')
    pyplot.savefig('d_min_vs_distl_method_2.png')
    pyplot.clf()

    #pyplot.scatter(
    #  d_min_distl_method_1, d_min_distl_method_2, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.hexbin(d_min_distl_method_1,
                  d_min_distl_method_2,
                  bins='log',
                  cmap=pyplot.cm.jet,
                  gridsize=50)
    pyplot.colorbar()
    #pyplot.gca().set_aspect('equal')
    xlim = pyplot.xlim()
    ylim = pyplot.ylim()
    m = max(max(d_min_distl_method_1), max(d_min_distl_method_2))
    pyplot.plot([0, m], [0, m], c=red)
    pyplot.xlim(0, xlim[1])
    pyplot.ylim(0, ylim[1])
    pyplot.xlabel('d_min_distl_method_1')
    pyplot.ylabel('d_min_distl_method_2')
    pyplot.savefig('distl_method_1_vs_distl_method_2.png')
    pyplot.clf()

    pyplot.hexbin(n_spots,
                  estimated_d_min,
                  bins='log',
                  cmap=pyplot.cm.jet,
                  gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(
    #n_spots, estimated_d_min, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel('# spots')
    pyplot.ylabel('estimated_d_min')
    pyplot.savefig('n_spots_vs_d_min.png')
    pyplot.clf()

    pyplot.hexbin(n_spots,
                  d_min_distl_method_1,
                  bins='log',
                  cmap=pyplot.cm.jet,
                  gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(
    #n_spots, d_min_distl_method_1, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel('# spots')
    pyplot.ylabel('d_min_distl_method_1')
    pyplot.savefig('n_spots_vs_distl_method_1.png')
    pyplot.clf()

    pyplot.hexbin(n_spots,
                  d_min_distl_method_2,
                  bins='log',
                  cmap=pyplot.cm.jet,
                  gridsize=50)
    pyplot.colorbar()
    #pyplot.scatter(
    #n_spots, d_min_distl_method_2, marker=marker, alpha=alpha, c=blue, lw=lw)
    pyplot.xlim(0, pyplot.xlim()[1])
    pyplot.ylim(0, pyplot.ylim()[1])
    pyplot.xlabel('# spots')
    pyplot.ylabel('d_min_distl_method_2')
    pyplot.savefig('n_spots_vs_distl_method_2.png')
    pyplot.clf()