def exercise_truncate(q_large):
    tprs_full = dmtbx.triplet_generator(miller_set=q_large,
                                        discard_weights=True)
    tprs = dmtbx.triplet_generator(miller_set=q_large,
                                   amplitudes=q_large.data(),
                                   max_relations_per_reflection=0,
                                   discard_weights=True)
    assert tprs.n_relations().all_eq(tprs_full.n_relations())
    for n in (1, 10, 100, 1000):
        tprs = dmtbx.triplet_generator(miller_set=q_large,
                                       amplitudes=q_large.data(),
                                       max_relations_per_reflection=n,
                                       discard_weights=True)
        assert (tprs.n_relations() >= n).all_eq(tprs.n_relations() == n)
    n = 3
    tprs = dmtbx.triplet_generator(miller_set=q_large,
                                   amplitudes=q_large.data(),
                                   max_relations_per_reflection=n,
                                   discard_weights=True)
    n_rel_full = tprs_full.n_relations()
    n_rel = tprs.n_relations()
    amp = q_large.data()
    for ih in range(q_large.indices().size()):
        if (n_rel[ih] == n_rel_full[ih]): continue
        aa_full = flex.double()
        for relation in tprs_full.relations_for(ih):
            aa_full.append(amp[relation.ik()] * amp[relation.ihmk()])
        aa = flex.double()
        for relation in tprs.relations_for(ih):
            aa.append(amp[relation.ik()] * amp[relation.ihmk()])
        aa_full = aa_full.select(
            flex.sort_permutation(data=aa_full, reverse=True))
        assert approx_equal(aa_full[:n], aa)
def exercise_truncate(q_large):
    tprs_full = dmtbx.triplet_generator(miller_set=q_large, discard_weights=True)
    tprs = dmtbx.triplet_generator(
        miller_set=q_large, amplitudes=q_large.data(), max_relations_per_reflection=0, discard_weights=True
    )
    assert tprs.n_relations().all_eq(tprs_full.n_relations())
    for n in (1, 10, 100, 1000):
        tprs = dmtbx.triplet_generator(
            miller_set=q_large, amplitudes=q_large.data(), max_relations_per_reflection=n, discard_weights=True
        )
        assert (tprs.n_relations() >= n).all_eq(tprs.n_relations() == n)
    n = 3
    tprs = dmtbx.triplet_generator(
        miller_set=q_large, amplitudes=q_large.data(), max_relations_per_reflection=n, discard_weights=True
    )
    n_rel_full = tprs_full.n_relations()
    n_rel = tprs.n_relations()
    amp = q_large.data()
    for ih in xrange(q_large.indices().size()):
        if n_rel[ih] == n_rel_full[ih]:
            continue
        aa_full = flex.double()
        for relation in tprs_full.relations_for(ih):
            aa_full.append(amp[relation.ik()] * amp[relation.ihmk()])
        aa = flex.double()
        for relation in tprs.relations_for(ih):
            aa.append(amp[relation.ik()] * amp[relation.ihmk()])
        aa_full = aa_full.select(flex.sort_permutation(data=aa_full, reverse=True))
        assert approx_equal(aa_full[:n], aa)
def reciprocal_space_squaring(start, selection_fixed, verbose):
    tprs = dmtbx.triplet_generator(miller_set=start)
    if (0 or verbose):
        for ih in range(start.indices()[:1].size()):
            for relation in tprs.relations_for(ih):
                print(relation.format(start.indices(), ih), end=' ')
                if (not relation.is_sigma_2(ih)):
                    print("not sigma-2", end=' ')
                print()
    amplitudes = abs(start).data()
    if (selection_fixed is not None):
        amplitudes.set_selected(~selection_fixed, 0)
    input_phases = flex.arg(start.data())
    result = tprs.apply_tangent_formula(amplitudes=amplitudes,
                                        phases_rad=input_phases,
                                        selection_fixed=selection_fixed,
                                        use_fixed_only=selection_fixed
                                        is not None)
    if (selection_fixed is not None):
        assert result.select(selection_fixed).all_eq(
            input_phases.select(selection_fixed))
    return result
def reciprocal_space_squaring(start, selection_fixed, verbose):
    tprs = dmtbx.triplet_generator(miller_set=start)
    if 0 or verbose:
        for ih in xrange(start.indices()[:1].size()):
            for relation in tprs.relations_for(ih):
                print relation.format(start.indices(), ih),
                if not relation.is_sigma_2(ih):
                    print "not sigma-2",
                print
    amplitudes = abs(start).data()
    if selection_fixed is not None:
        amplitudes.set_selected(~selection_fixed, 0)
    input_phases = flex.arg(start.data())
    result = tprs.apply_tangent_formula(
        amplitudes=amplitudes,
        phases_rad=input_phases,
        selection_fixed=selection_fixed,
        use_fixed_only=selection_fixed is not None,
    )
    if selection_fixed is not None:
        assert result.select(selection_fixed).all_eq(input_phases.select(selection_fixed))
    return result
def run():
  import sys
  reflection_file_name = sys.argv[1]
  import iotbx.cif
  miller_arrays = iotbx.cif.reader(
    file_path=reflection_file_name).as_miller_arrays()
  for miller_array in miller_arrays:
    s = str(miller_array.info())
    if '_meas' in s:
      if miller_array.is_xray_intensity_array():
        break
      elif miller_array.is_xray_amplitude_array():
        break
  if not ('_meas' in str(miller_array.info())
          and (miller_array.is_xray_amplitude_array()
               or miller_array.is_xray_intensity_array())):
    print "Sorry: CIF does not contain an appropriate miller array"
    return
  miller_array.show_comprehensive_summary()
  print

  if (miller_array.is_xray_intensity_array()):
    print "Converting intensities to amplitudes."
    miller_array = miller_array.as_amplitude_array()
    print

  miller_array.setup_binner(auto_binning=True)
  miller_array.binner().show_summary()
  print

  all_e_values = miller_array.quasi_normalize_structure_factors().sort(
    by_value="data")
  large_e_values = all_e_values.select(all_e_values.data() > 1.2)
  print "number of large_e_values:", large_e_values.size()
  print

  from cctbx import dmtbx
  triplets = dmtbx.triplet_generator(large_e_values)
  from cctbx.array_family import flex
  print "triplets per reflection: min,max,mean: %d, %d, %.2f" % (
    flex.min(triplets.n_relations()),
    flex.max(triplets.n_relations()),
    flex.mean(triplets.n_relations().as_double()))
  print "total number of triplets:", flex.sum(triplets.n_relations())
  print

  input_phases = large_e_values \
    .random_phases_compatible_with_phase_restrictions()
  tangent_formula_phases = input_phases.data()
  for i in xrange(10):
    tangent_formula_phases = triplets.apply_tangent_formula(
      amplitudes=large_e_values.data(),
      phases_rad=tangent_formula_phases,
      selection_fixed=None,
      use_fixed_only=False,
      reuse_results=True)

  e_map_coeff = large_e_values.phase_transfer(
    phase_source=tangent_formula_phases)
  from cctbx import maptbx
  e_map = e_map_coeff.fft_map(
    symmetry_flags=maptbx.use_space_group_symmetry)
  e_map.apply_sigma_scaling()
  e_map.statistics().show_summary(prefix="e_map ")
  print

  peak_search = e_map.peak_search(parameters=maptbx.peak_search_parameters(
    min_distance_sym_equiv=1.2))
  peaks = peak_search.all(max_clusters=10)
  print "e_map peak list"
  print "       fractional coordinates       peak height"
  for site,height in zip(peaks.sites(), peaks.heights()):
    print "  (%9.6f, %9.6f, %9.6f)" % site, "%10.3f" % height
  print

  if (len(sys.argv) > 2):
    coordinate_file_name = sys.argv[2]
    xray_structure = iotbx.cif.reader(
      file_path=coordinate_file_name).build_crystal_structures(
        data_block_name="I")
    xray_structure.show_summary().show_scatterers()
    print

    f_calc = abs(miller_array.structure_factors_from_scatterers(
      xray_structure=xray_structure,
      algorithm="direct").f_calc())
    correlation = flex.linear_correlation(f_calc.data(), miller_array.data())
    assert correlation.is_well_defined()
    print "correlation of f_obs and f_calc: %.4f" % correlation.coefficient()
    print

    reference_model = xray_structure.as_emma_model()
    assert reference_model.unit_cell().is_similar_to(e_map.unit_cell())
    assert reference_model.space_group() == e_map.space_group()
    from cctbx import euclidean_model_matching as emma
    peak_model = emma.model(special_position_settings=reference_model)
    for i,site in enumerate(peaks.sites()):
      peak_model.add_position(emma.position(label="peak%02d" % i, site=site))
    matches = emma.model_matches(
      model1=reference_model,
      model2=peak_model,
      tolerance=1.,
      models_are_diffraction_index_equivalent=True)
    for match in matches.refined_matches:
      match.show()
def run():
    import sys
    reflection_file_name = sys.argv[1]
    import iotbx.cif
    miller_arrays = iotbx.cif.reader(
        file_path=reflection_file_name).as_miller_arrays()
    for miller_array in miller_arrays:
        s = str(miller_array.info())
        if '_meas' in s:
            if miller_array.is_xray_intensity_array():
                break
            elif miller_array.is_xray_amplitude_array():
                break
    if not ('_meas' in str(miller_array.info()) and
            (miller_array.is_xray_amplitude_array()
             or miller_array.is_xray_intensity_array())):
        print "Sorry: CIF does not contain an appropriate miller array"
        return
    miller_array.show_comprehensive_summary()
    print

    if (miller_array.is_xray_intensity_array()):
        print "Converting intensities to amplitudes."
        miller_array = miller_array.as_amplitude_array()
        print

    miller_array.setup_binner(auto_binning=True)
    miller_array.binner().show_summary()
    print

    all_e_values = miller_array.quasi_normalize_structure_factors().sort(
        by_value="data")
    large_e_values = all_e_values.select(all_e_values.data() > 1.2)
    print "number of large_e_values:", large_e_values.size()
    print

    from cctbx import dmtbx
    triplets = dmtbx.triplet_generator(large_e_values)
    from cctbx.array_family import flex
    print "triplets per reflection: min,max,mean: %d, %d, %.2f" % (
        flex.min(triplets.n_relations()), flex.max(triplets.n_relations()),
        flex.mean(triplets.n_relations().as_double()))
    print "total number of triplets:", flex.sum(triplets.n_relations())
    print

    input_phases = large_e_values \
      .random_phases_compatible_with_phase_restrictions()
    tangent_formula_phases = input_phases.data()
    for i in xrange(10):
        tangent_formula_phases = triplets.apply_tangent_formula(
            amplitudes=large_e_values.data(),
            phases_rad=tangent_formula_phases,
            selection_fixed=None,
            use_fixed_only=False,
            reuse_results=True)

    e_map_coeff = large_e_values.phase_transfer(
        phase_source=tangent_formula_phases)
    from cctbx import maptbx
    e_map = e_map_coeff.fft_map(symmetry_flags=maptbx.use_space_group_symmetry)
    e_map.apply_sigma_scaling()
    e_map.statistics().show_summary(prefix="e_map ")
    print

    peak_search = e_map.peak_search(parameters=maptbx.peak_search_parameters(
        min_distance_sym_equiv=1.2))
    peaks = peak_search.all(max_clusters=10)
    print "e_map peak list"
    print "       fractional coordinates       peak height"
    for site, height in zip(peaks.sites(), peaks.heights()):
        print "  (%9.6f, %9.6f, %9.6f)" % site, "%10.3f" % height
    print

    if (len(sys.argv) > 2):
        coordinate_file_name = sys.argv[2]
        xray_structure = iotbx.cif.reader(
            file_path=coordinate_file_name).build_crystal_structures(
                data_block_name="I")
        xray_structure.show_summary().show_scatterers()
        print

        f_calc = abs(
            miller_array.structure_factors_from_scatterers(
                xray_structure=xray_structure, algorithm="direct").f_calc())
        correlation = flex.linear_correlation(f_calc.data(),
                                              miller_array.data())
        assert correlation.is_well_defined()
        print "correlation of f_obs and f_calc: %.4f" % correlation.coefficient(
        )
        print

        reference_model = xray_structure.as_emma_model()
        assert reference_model.unit_cell().is_similar_to(e_map.unit_cell())
        assert reference_model.space_group() == e_map.space_group()
        from cctbx import euclidean_model_matching as emma
        peak_model = emma.model(special_position_settings=reference_model)
        for i, site in enumerate(peaks.sites()):
            peak_model.add_position(
                emma.position(label="peak%02d" % i, site=site))
        matches = emma.model_matches(
            model1=reference_model,
            model2=peak_model,
            tolerance=1.,
            models_are_diffraction_index_equivalent=True)
        for match in matches.refined_matches:
            match.show()