Beispiel #1
0
def exercise_phase_transfer():
  sg = sgtbx.space_group_info("P 21 21 21").group()
  i = flex.miller_index(((1,2,3), (3,0,3)))
  a = flex.double((-3.6,4.6))
  p = flex.complex_double((1+2j, 0))
  assert approx_equal(tuple(miller.phase_transfer(sg, i, a, p, 1.e-10)),
                      ((-1.6099689-3.2199379j), 0j))
  a = flex.complex_double((3.6,4.6))
  try:
    miller.phase_transfer(sg, i, a, p)
  except Exception as e:
    if (str(e.__class__).find("Boost.Python.ArgumentError") < 0):
      raise RuntimeError("Unexpected exception: %s" % str(e))
  else:
    raise Exception_expected

  a = flex.double((-3.6,4.6))
  p = flex.double((10,20))
  t = miller.phase_transfer(sg, i, a, p, True)
  assert approx_equal(tuple(flex.abs(t)), flex.abs(a))
  assert approx_equal(tuple(flex.arg(t, True)), (-170,90))
  p = p * (math.pi/180)
  t = miller.phase_transfer(sg, i, a, p, False)
  assert approx_equal(tuple(flex.abs(t)), flex.abs(a))
  assert approx_equal(tuple(flex.arg(t, True)), (-170,90))
Beispiel #2
0
def exercise_expand():
  sg = sgtbx.space_group("P 41 (1,-1,0)")
  h = flex.miller_index(((3,1,-2), (1,-2,0)))
  assert tuple(sg.is_centric(h)) == (0, 1)
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=False, indices=h, build_iselection=False)
  p1_i0 = ((-3,-1,2), (-1, 3,2),(3,1,2),(1,-3,2),(1,-2, 0),(2,1,0))
  assert tuple(p1.indices) == p1_i0
  assert p1.iselection.size() == 0
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=True, indices=h, build_iselection=False)
  assert tuple(p1.indices) \
      == ((3,1,-2), (1,-3,-2), (-3,-1,-2), (-1,3,-2),
          (1,-2,0), (-2,-1,0), (-1,2,0), (2,1,0))
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=False, indices=h, build_iselection=True)
  assert tuple(p1.indices) == p1_i0
  assert tuple(p1.iselection) == (0,0,0,0,1,1)
  a = flex.double((1,2))
  p = flex.double((10,90))
  p1 = miller.expand_to_p1_phases(
    space_group=sg, anomalous_flag=False, indices=h, data=p, deg=True)
  assert approx_equal(tuple(p1.data), (-10,110,110,-10, 90,30))
  p1 = miller.expand_to_p1_phases(
    space_group=sg, anomalous_flag=True, indices=h, data=p, deg=True)
  assert approx_equal(tuple(p1.data), (10,-110,-110,10, 90,-30,-90,30))
  p = flex.double([x * math.pi/180 for x in p])
  v = [x * math.pi/180 for x in p1.data]
  p1 = miller.expand_to_p1_phases(
    space_group=sg, anomalous_flag=True, indices=h, data=p, deg=False)
  assert approx_equal(tuple(p1.data), v)
  f = flex.polar(a, p)
  p1 = miller.expand_to_p1_complex(
    space_group=sg, anomalous_flag=True, indices=h, data=f)
  assert approx_equal(tuple(flex.abs(p1.data)), (1,1,1,1,2,2,2,2))
  assert approx_equal(tuple(flex.arg(p1.data)), v)
  hl = flex.hendrickson_lattman([(1,2,3,4), (5,6,7,8)])
  p1 = miller.expand_to_p1_hendrickson_lattman(
    space_group=sg, anomalous_flag=True, indices=h, data=hl)
  assert approx_equal(p1.data, [
    [1,2,3,4],
    [1.232051,-1.866025,-4.964102,0.5980762],
    [1.232051,-1.866025,-4.964102,0.5980762],
    [1,2,3,4],
    [5,6,7,8],
    [2.696152,-7.330127,-10.4282,2.062178],
    [-5,-6,7,8],
    [7.696152,-1.330127,3.428203,-10.06218]])
  b = flex.bool([True,False])
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=True, indices=h, build_iselection=True)
  assert b.select(p1.iselection).all_eq(
    flex.bool([True, True, True, True, False, False, False, False]))
  i = flex.int([13,17])
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=True, indices=h, build_iselection=True)
  assert i.select(p1.iselection).all_eq(flex.int([13,13,13,13,17,17,17,17]))
  #
  assert approx_equal(miller.statistical_mean(sg, False, h, a), 4/3.)
  assert approx_equal(miller.statistical_mean(sg, True, h, a), 3/2.)
def calculate_exp_i_two_phi_peaks(xray_structure, d_min,
                                  min_peak_distance,
                                  max_reduced_peaks):
  f_h = xray_structure.structure_factors(
    anomalous_flag=False,
    d_min=d_min).f_calc()
  two_i_phi_h = miller.array(
    miller_set=f_h,
    data=flex.polar(1, flex.arg(f_h.data())*2))
  fft_map = two_i_phi_h.fft_map(
    d_min=d_min,
    symmetry_flags=maptbx.use_space_group_symmetry)
  real_map = fft_map.real_map()
  real_map = maptbx.copy(real_map, flex.grid(real_map.focus()))
  stats = maptbx.statistics(real_map)
  if (stats.max() != 0):
    real_map /= abs(stats.max())
  grid_tags = maptbx.grid_tags(real_map.focus())
  grid_tags.build(fft_map.space_group_info().type(), fft_map.symmetry_flags())
  grid_tags.verify(real_map)
  peak_list = maptbx.peak_list(
    data=real_map,
    tags=grid_tags.tag_array(),
    max_peaks=10*max_reduced_peaks,
    interpolate=True)
  reduced_peaks = peak_cluster_reduction(
    crystal_symmetry=xray_structure,
    peak_list=peak_list,
    min_peak_distance=min_peak_distance,
    max_reduced_peaks=max_reduced_peaks)
  return reduced_peaks
def exercise_expand():
  sg = sgtbx.space_group("P 41 (1,-1,0)")
  h = flex.miller_index(((3,1,-2), (1,-2,0)))
  assert tuple(sg.is_centric(h)) == (0, 1)
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=False, indices=h, build_iselection=False)
  p1_i0 = ((-3,-1,2), (-1, 3,2),(3,1,2),(1,-3,2),(1,-2, 0),(2,1,0))
  assert tuple(p1.indices) == p1_i0
  assert p1.iselection.size() == 0
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=True, indices=h, build_iselection=False)
  assert tuple(p1.indices) \
      == ((3,1,-2), (1,-3,-2), (-3,-1,-2), (-1,3,-2),
          (1,-2,0), (-2,-1,0), (-1,2,0), (2,1,0))
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=False, indices=h, build_iselection=True)
  assert tuple(p1.indices) == p1_i0
  assert tuple(p1.iselection) == (0,0,0,0,1,1)
  a = flex.double((1,2))
  p = flex.double((10,90))
  p1 = miller.expand_to_p1_phases(
    space_group=sg, anomalous_flag=False, indices=h, data=p, deg=True)
  assert approx_equal(tuple(p1.data), (-10,110,110,-10, 90,30))
  p1 = miller.expand_to_p1_phases(
    space_group=sg, anomalous_flag=True, indices=h, data=p, deg=True)
  assert approx_equal(tuple(p1.data), (10,-110,-110,10, 90,-30,-90,30))
  p = flex.double([x * math.pi/180 for x in p])
  v = [x * math.pi/180 for x in p1.data]
  p1 = miller.expand_to_p1_phases(
    space_group=sg, anomalous_flag=True, indices=h, data=p, deg=False)
  assert approx_equal(tuple(p1.data), v)
  f = flex.polar(a, p)
  p1 = miller.expand_to_p1_complex(
    space_group=sg, anomalous_flag=True, indices=h, data=f)
  assert approx_equal(tuple(flex.abs(p1.data)), (1,1,1,1,2,2,2,2))
  assert approx_equal(tuple(flex.arg(p1.data)), v)
  hl = flex.hendrickson_lattman([(1,2,3,4), (5,6,7,8)])
  p1 = miller.expand_to_p1_hendrickson_lattman(
    space_group=sg, anomalous_flag=True, indices=h, data=hl)
  assert approx_equal(p1.data, [
    [1,2,3,4],
    [1.232051,-1.866025,-4.964102,0.5980762],
    [1.232051,-1.866025,-4.964102,0.5980762],
    [1,2,3,4],
    [5,6,7,8],
    [2.696152,-7.330127,-10.4282,2.062178],
    [-5,-6,7,8],
    [7.696152,-1.330127,3.428203,-10.06218]])
  b = flex.bool([True,False])
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=True, indices=h, build_iselection=True)
  assert b.select(p1.iselection).all_eq(
    flex.bool([True, True, True, True, False, False, False, False]))
  i = flex.int([13,17])
  p1 = miller.expand_to_p1_iselection(
    space_group=sg, anomalous_flag=True, indices=h, build_iselection=True)
  assert i.select(p1.iselection).all_eq(flex.int([13,13,13,13,17,17,17,17]))
  #
  assert approx_equal(miller.statistical_mean(sg, False, h, a), 4/3.)
  assert approx_equal(miller.statistical_mean(sg, True, h, a), 3/2.)
 def compute_map(self):
   density_modification.density_modification.compute_map(self)
   if self.model_map_coeffs is not None:
     model_coeffs, dm_coeffs = self.model_map_coeffs.common_sets(self.map_coeffs)
     fft_map = model_coeffs.fft_map(
       resolution_factor=self.params.grid_resolution_factor).apply_sigma_scaling()
     dm_map = dm_coeffs.fft_map(
       resolution_factor=self.params.grid_resolution_factor).apply_sigma_scaling()
     print
     corr = flex.linear_correlation(
       fft_map.real_map_unpadded().as_1d(), dm_map.real_map_unpadded().as_1d())
     print "dm/model correlation:"
     corr.show_summary()
     self.correlation_coeffs.append(corr.coefficient())
     self.mean_phase_errors.append(flex.mean(phase_error(
       flex.arg(model_coeffs.data()),
       flex.arg(dm_coeffs.data())))/density_modification.pi_180)
def check_phase_restrictions(miller_array, epsilon=1.e-10, verbose=0):
  space_group = miller_array.space_group()
  phases = flex.arg(miller_array.data())
  for i,h in enumerate(miller_array.indices()):
    f = miller_array.data()[i]
    if (verbose):
      print h, f, abs(f), phases[i]*180/math.pi
    if (abs(f.real) > epsilon and abs(f.imag) > epsilon):
      assert space_group.is_valid_phase(h, phases[i])
Beispiel #7
0
def check_phase_restrictions(miller_array, epsilon=1.e-10, verbose=0):
    space_group = miller_array.space_group()
    phases = flex.arg(miller_array.data())
    for i, h in enumerate(miller_array.indices()):
        f = miller_array.data()[i]
        if (verbose):
            print h, f, abs(f), phases[i] * 180 / math.pi
        if (abs(f.real) > epsilon and abs(f.imag) > epsilon):
            assert space_group.is_valid_phase(h, phases[i])
Beispiel #8
0
 def _add_complex(self, amplitudes_label, phases_label, column_types,
                        indices, data):
   mtz_reflection_indices = self.add_column(
     label=amplitudes_label,
     type=column_types[0]).set_reals(
       miller_indices=indices,
       data=flex.abs(data))
   self.add_column(
     label=phases_label,
     type=column_types[1]).set_reals(
       mtz_reflection_indices=mtz_reflection_indices,
       data=flex.arg(data, True))
 def compute_map(self):
     density_modification.density_modification.compute_map(self)
     if self.model_map_coeffs is not None:
         model_coeffs, dm_coeffs = self.model_map_coeffs.common_sets(
             self.map_coeffs)
         fft_map = model_coeffs.fft_map(
             resolution_factor=self.params.grid_resolution_factor
         ).apply_sigma_scaling()
         dm_map = dm_coeffs.fft_map(
             resolution_factor=self.params.grid_resolution_factor
         ).apply_sigma_scaling()
         print()
         corr = flex.linear_correlation(fft_map.real_map_unpadded().as_1d(),
                                        dm_map.real_map_unpadded().as_1d())
         print("dm/model correlation:")
         corr.show_summary()
         self.correlation_coeffs.append(corr.coefficient())
         self.mean_phase_errors.append(
             flex.mean(
                 phase_error(flex.arg(model_coeffs.data()),
                             flex.arg(dm_coeffs.data()))) /
             density_modification.pi_180)
Beispiel #10
0
def randomize_phases(f_calc, fudge_factor):
    assert 0 <= fudge_factor <= 1
    phases = flex.arg(f_calc.data(), True)
    centric_flags = f_calc.centric_flags().data()
    acentric_flags = ~centric_flags
    centric_phases = phases.select(centric_flags)
    acentric_phases = phases.select(acentric_flags)
    sel = flex.random_double(size=centric_phases.size()) < (0.5 * fudge_factor)
    centric_phases.set_selected(sel, centric_phases.select(sel) + 180)
    acentric_phases += (flex.random_double(size=acentric_phases.size()) * 360 -
                        180) * fudge_factor
    phases.set_selected(centric_flags, centric_phases)
    phases.set_selected(acentric_flags, acentric_phases)
    return f_calc.phase_transfer(phases, deg=True)
Beispiel #11
0
def randomize_phases(f_calc, fudge_factor):
  assert 0 <= fudge_factor <= 1
  phases = flex.arg(f_calc.data(), True)
  centric_flags = f_calc.centric_flags().data()
  acentric_flags = ~centric_flags
  centric_phases = phases.select(centric_flags)
  acentric_phases = phases.select(acentric_flags)
  sel = flex.random_double(size=centric_phases.size()) < (0.5 * fudge_factor)
  centric_phases.set_selected(sel, centric_phases.select(sel) + 180)
  acentric_phases += (flex.random_double(size=acentric_phases.size())
                      * 360 - 180) * fudge_factor
  phases.set_selected(centric_flags, centric_phases)
  phases.set_selected(acentric_flags, acentric_phases)
  return f_calc.phase_transfer(phases, deg=True)
Beispiel #12
0
 def oszlanyi_suto_phase_transfer(self,
                                  source,
                                  delta_varphi=math.pi / 2,
                                  weak_reflection_fraction=0.2,
                                  need_sorting=True):
     """ As per ref. [2] """
     cut = int(weak_reflection_fraction * source.size())
     if need_sorting:
         p = self.sort_permutation(by_value="data", reverse=True)
         target = self.select(p)
         source = source.select(p)
     else:
         target = self
     source_phases = flex.arg(source.data())
     # weak reflections
     phases = source_phases[:cut] + delta_varphi
     moduli = flex.abs(source.data()[:cut])
     # strong ones
     phases.extend(source_phases[cut:])
     moduli.extend(self.data()[cut:])
     return miller.array(self, moduli).phase_transfer(phases)
 def oszlanyi_suto_phase_transfer(self,
                                  source,
                                  delta_varphi=math.pi/2,
                                  weak_reflection_fraction=0.2,
                                  need_sorting=True):
   """ As per ref. [2] """
   cut = int(weak_reflection_fraction * source.size())
   if need_sorting:
     p = self.sort_permutation(by_value="data", reverse=True)
     target = self.select(p)
     source = source.select(p)
   else:
     target = self
   source_phases = flex.arg(source.data())
   # weak reflections
   phases = source_phases[:cut] + delta_varphi
   moduli = flex.abs(source.data()[:cut])
   # strong ones
   phases.extend(source_phases[cut:])
   moduli.extend(self.data()[cut:])
   return miller.array(self, moduli).phase_transfer(phases)
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 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
Beispiel #16
0
 def compute_map_coefficients(self):
   f_obs = self.f_obs_complete.select(self.f_obs_complete.d_spacings().data() >= self.d_min)
   f_calc = f_obs.structure_factors_from_map(self.map, use_sg=True)
   f_obs_active = f_obs.select_indices(self.active_indices)
   minimized = relative_scaling.ls_rel_scale_driver(
     f_obs_active,
     f_calc.as_amplitude_array().select_indices(self.active_indices),
     use_intensities=False,
     use_weights=False)
   #minimized.show()
   f_calc = f_calc.customized_copy(data=f_calc.data()\
                                   * math.exp(-minimized.p_scale)\
                                   * adptbx.debye_waller_factor_u_star(
                                     f_calc.indices(), minimized.u_star))
   f_calc_active = f_calc.common_set(f_obs_active)
   matched_indices = f_obs.match_indices(self.f_obs_active)
   lone_indices_selection = matched_indices.single_selection(0)
   from mmtbx.max_lik import maxlik
   alpha_beta_est = maxlik.alpha_beta_est_manager(
     f_obs=f_obs_active,
     f_calc=f_calc_active,
     free_reflections_per_bin=140,
     flags=flex.bool(f_obs_active.size()),
     interpolation=True,
     epsilons=f_obs_active.epsilons().data().as_double())
   alpha, beta = alpha_beta_est.alpha_beta_for_each_reflection(
     f_obs=self.f_obs_complete.select(self.f_obs_complete.d_spacings().data() >= self.d_min))
   f_obs.data().copy_selected(
     lone_indices_selection.iselection(), flex.abs(f_calc.data()))
   t = maxlik.fo_fc_alpha_over_eps_beta(
     f_obs=f_obs,
     f_model=f_calc,
     alpha=alpha,
     beta=beta)
   hl_coeff = flex.hendrickson_lattman(
     t * flex.cos(f_calc.phases().data()),
     t * flex.sin(f_calc.phases().data()))
   dd = alpha.data()
   #
   hl_array = f_calc.array(
     data=self.hl_coeffs_start.common_set(f_calc).data()+hl_coeff)
   self.compute_phase_source(hl_array)
   fom = flex.abs(self.phase_source.data())
   mFo = hl_array.array(data=f_obs.data()*self.phase_source.data())
   DFc = hl_array.array(data=dd*f_calc.as_amplitude_array().phase_transfer(
       self.phase_source).data())
   centric_flags = f_obs.centric_flags().data()
   acentric_flags = ~centric_flags
   fo_scale = flex.double(centric_flags.size())
   fc_scale = flex.double(centric_flags.size())
   fo_scale.set_selected(acentric_flags, 2)
   fo_scale.set_selected(centric_flags, 1)
   fc_scale.set_selected(acentric_flags, 1)
   fc_scale.set_selected(centric_flags, 0)
   fo_scale.set_selected(lone_indices_selection, 0)
   fc_scale.set_selected(lone_indices_selection, -1)
   self.map_coeffs = hl_array.array(
     data=mFo.data()*fo_scale - DFc.data()*fc_scale)
   self.fom = hl_array.array(data=fom)
   self.hl_coeffs = hl_array
   # statistics
   self.r1_factor = f_obs_active.r1_factor(f_calc_active)
   fom = fom.select(matched_indices.pair_selection(0))
   self.r1_factor_fom = flex.sum(
     fom * flex.abs(f_obs_active.data() - f_calc_active.as_amplitude_array().data())) \
       / flex.sum(fom * f_obs_active.data())
   phase_source, phase_source_previous = self.phase_source.common_sets(
     self.phase_source_previous)
   self.mean_delta_phi = phase_error(
     flex.arg(phase_source.data()), flex.arg(phase_source_previous.data()))
   phase_source, phase_source_initial = self.phase_source.common_sets(
     self.phase_source_initial)
   self.mean_delta_phi_initial = phase_error(
     flex.arg(phase_source.data()), flex.arg(phase_source_initial.data()))
   self.mean_fom = flex.mean(fom)
   fom = f_obs_active.array(data=fom)
   if fom.data().size()<1000: # 2013-12-14 was hard-wired at 1000 tt
     reflections_per_bin=fom.data().size()
   else:
     reflections_per_bin=1000
   fom.setup_binner(reflections_per_bin=reflections_per_bin)
   self.mean_fom_binned = fom.mean(use_binning=True)
  assert approx_equal(tuple(miller.phase_transfer(sg, i, a, p, 1.e-10)),
                      ((-1.6099689-3.2199379j), 0j))
  a = flex.complex_double((3.6,4.6))
  try:
    miller.phase_transfer(sg, i, a, p)
  except Exception, e:
    if (str(e.__class__).find("Boost.Python.ArgumentError") < 0):
      raise RuntimeError("Unexpected exception: %s" % str(e))
  else:
    raise Exception_expected

  a = flex.double((-3.6,4.6))
  p = flex.double((10,20))
  t = miller.phase_transfer(sg, i, a, p, True)
  assert approx_equal(tuple(flex.abs(t)), flex.abs(a))
  assert approx_equal(tuple(flex.arg(t, True)), (-170,90))
  p = p * (math.pi/180)
  t = miller.phase_transfer(sg, i, a, p, False)
  assert approx_equal(tuple(flex.abs(t)), flex.abs(a))
  assert approx_equal(tuple(flex.arg(t, True)), (-170,90))

def exercise_f_calc_map():
  i = flex.miller_index((   (1,1,0), (-1,-1,0), (1,2,3), (3,2,1) ))
  f = flex.complex_double((    1+1j,      2+2j,     3+3j,   4+4j ))
  f_map = miller.f_calc_map(i, f, anomalous_flag=True)
  assert f_map[(1,1,0)] == 1+1j
  assert f_map[(-1,-1,0)] == 2+2j
  assert f_map[(1,2,3)] == 3+3j
  assert f_map[(3,2,1)] == 4+4j
  assert f_map[(2,2,2)] == 0
  f_map = miller.f_calc_map(i, f, anomalous_flag=False)
Beispiel #18
0
    def process_input_array(self, arr):
        array = arr.deep_copy()
        work_array = arr
        multiplicities = None
        try:
            if self.merge_equivalents:
                array, multiplicities, merge = MergeData(
                    array, self.settings.show_anomalous_pairs)
            settings = self.settings
            data = array.data()
            #import code, traceback; code.interact(local=locals(), banner="".join( traceback.format_stack(limit=10) ) )
            self.missing_set = oop.null()
            #if (array.is_xray_intensity_array()):
            #  data.set_selected(data < 0, flex.double(data.size(), 0.))
            if (array.is_unique_set_under_symmetry()) and (
                    settings.map_to_asu):
                array = array.map_to_asu()
                if (multiplicities is not None):
                    multiplicities = multiplicities.map_to_asu()

            if (settings.d_min is not None):
                array = array.resolution_filter(d_min=settings.d_min)
                if (multiplicities is not None):
                    multiplicities = multiplicities.resolution_filter(
                        d_min=settings.d_min)
            self.filtered_array = array.deep_copy()
            if (settings.expand_anomalous):
                if not array.is_unique_set_under_symmetry():
                    raise Sorry(
                        "Error! Cannot generate bijvoet mates of unmerged reflections."
                    )
                array = array.generate_bijvoet_mates()
                original_symmetry = array.crystal_symmetry()

                if (multiplicities is not None):
                    multiplicities = multiplicities.generate_bijvoet_mates()
            if (self.settings.show_missing):
                self.missing_set = array.complete_set().lone_set(array)
                if self.settings.show_anomalous_pairs:
                    self.missing_set = self.missing_set.select(
                        self.missing_set.centric_flags().data(), negate=True)
            if (settings.expand_to_p1):
                if not array.is_unique_set_under_symmetry():
                    raise Sorry(
                        "Error! Cannot expand unmerged reflections to P1.")
                original_symmetry = array.crystal_symmetry()
                array = array.expand_to_p1().customized_copy(
                    crystal_symmetry=original_symmetry)
                #array = array.niggli_cell().expand_to_p1()
                #self.missing_set = self.missing_set.niggli_cell().expand_to_p1()
                self.missing_set = self.missing_set.expand_to_p1(
                ).customized_copy(crystal_symmetry=original_symmetry)
                if (multiplicities is not None):
                    multiplicities = multiplicities.expand_to_p1(
                    ).customized_copy(crystal_symmetry=original_symmetry)
            data = array.data()
            self.r_free_mode = False
            self.phases = flex.double(data.size(), float('nan'))
            self.radians = flex.double(data.size(), float('nan'))
            self.ampl = flex.double(data.size(), float('nan'))
            self.sigmas = None
            if isinstance(data, flex.bool):
                self.r_free_mode = True
                data_as_float = flex.double(data.size(), 0.0)
                data_as_float.set_selected(data == True,
                                           flex.double(data.size(), 1.0))
                data = data_as_float
                self.data = data  #.deep_copy()
            else:
                if isinstance(data, flex.double):
                    self.data = data  #.deep_copy()
                elif isinstance(data, flex.complex_double):
                    self.data = data  #.deep_copy()
                    self.ampl = flex.abs(data)
                    self.phases = flex.arg(data) * 180.0 / math.pi
                    # purge nan values from array to avoid crash in fmod_positive()
                    #b = flex.bool([bool(math.isnan(e)) for e in self.phases])
                    b = graphics_utils.IsNansArray(self.phases)
                    # replace the nan values with an arbitrary float value
                    self.phases = self.phases.set_selected(b, 42.4242)
                    # Cast negative degrees to equivalent positive degrees
                    self.phases = flex.fmod_positive(self.phases, 360.0)
                    self.radians = flex.arg(data)
                    # replace the nan values with an arbitrary float value
                    self.radians = self.radians.set_selected(b, 0.424242)
                elif hasattr(array.data(), "as_double"):
                    self.data = data
                else:
                    raise RuntimeError("Unexpected data type: %r" % data)
                if (settings.show_data_over_sigma):
                    if (array.sigmas() is None):
                        raise Sorry("sigmas not defined.")
                    sigmas = array.sigmas()
                    non_zero_sel = sigmas != 0
                    array = array.select(non_zero_sel)
                    array = array.customized_copy(data=array.data() /
                                                  array.sigmas())
                    self.data = array.data()
                    if (multiplicities is not None):
                        multiplicities = multiplicities.select(non_zero_sel)
                if array.sigmas() is not None:
                    self.sigmas = array.sigmas()
                else:
                    self.sigmas = None
            work_array = array
        except Exception as e:
            print(to_str(e) + "".join(traceback.format_stack(limit=10)))
            raise e
            return None, None
        work_array.set_info(arr.info())
        multiplicities = multiplicities
        return work_array, multiplicities
Beispiel #19
0
def export_as_cns_hkl(self, file_object, file_name, info, array_names,
                      r_free_flags):
    out = file_object
    if (file_name): print >> out, "{ file:", file_name, "}"
    if (self.info() is not None):
        print >> out, "{", self.info(), "}"
    crystal_symmetry_as_cns_comments(crystal_symmetry=self, out=out)
    for line in info:
        print >> out, "{", line, "}"
    print >> out, "NREFlections=%d" % self.indices().size()
    if (self.anomalous_flag()):
        print >> out, "ANOMalous=TRUE"
    else:
        print >> out, "ANOMalous=FALSe"
    if (self.sigmas() is not None):
        if (array_names is None): array_names = ["FOBS", "SIGMA"]
        else: assert len(array_names) == 2
        assert isinstance(self.data(), flex.double)
        assert isinstance(self.sigmas(), flex.double)
        if (self.is_xray_intensity_array()):
            f_obs = self.f_sq_as_f()
        else:
            f_obs = self
        nf, ns = array_names
        print >> out, "DECLare NAME=%s DOMAin=RECIprocal TYPE=REAL END" % nf
        print >> out, "DECLare NAME=%s DOMAin=RECIprocal TYPE=REAL END" % ns
        if (r_free_flags is None):
            for h, f, s in zip(f_obs.indices(), f_obs.data(), f_obs.sigmas()):
                print >> out, "INDEx %d %d %d" % h, "%s= %.6g %s= %.6g" % (
                    nf, f, ns, s)
        else:
            assert r_free_flags.indices().all_eq(f_obs.indices())
            print >> out, "DECLare NAME=TEST DOMAin=RECIprocal TYPE=INTE END"
            for h, f, s, t in zip(f_obs.indices(), f_obs.data(),
                                  f_obs.sigmas(), r_free_flags.data()):
                print >> out, "INDEx %d %d %d" % h, "%s= %.6g %s= %.6g" % (nf,f,ns,s),\
                  "TEST= %d" % int(t)
    elif (self.is_complex_array()):
        if (array_names is None): array_names = ["F"]
        else: assert len(array_names) == 1
        assert r_free_flags is None
        n = array_names[0]
        print >> out, "DECLare NAME=%s  DOMAin=RECIprocal TYPE=COMPLEX END" % n
        for h, a, p in zip(self.indices(), flex.abs(self.data()),
                           flex.arg(self.data(), True)):
            print >> out, "INDEx %d %d %d" % h, "%s= %.6g %.6g" % (n, a, p)
    elif (self.is_hendrickson_lattman_array()):
        if (array_names is None): array_names = ["PA", "PB", "PC", "PD"]
        else: assert len(array_names) == 4
        assert r_free_flags is None
        for i in range(4):
            print >> out, "DECLare NAME=%s  DOMAin=RECIprocal TYPE=REAL END" % (
                array_names[i])
        print >> out, "GROUp  TYPE=HL"
        for i in range(4):
            print >> out, "     OBJEct=%s" % (array_names[i])
        print >> out, "END"
        for h, hl in zip(self.indices(), self.data()):
            print >> out, "INDEx %d %d %d" % h,
            print >> out, "%s= %.6g" % (array_names[0], hl[0]),
            print >> out, "%s= %.6g" % (array_names[1], hl[1]),
            print >> out, "%s= %.6g" % (array_names[2], hl[2]),
            print >> out, "%s= %.6g" % (array_names[3], hl[3])
    else:
        if (array_names is None): array_names = ["DATA"]
        else: assert len(array_names) == 1
        assert r_free_flags is None
        if (isinstance(self.data(), flex.double)):
            print >> out, \
              "DECLare NAME=%s  DOMAin=RECIprocal TYPE=REAL END" % array_names[0]
            fmt = "%.6g"
        elif (isinstance(self.data(), flex.int)
              or isinstance(self.data(), flex.bool)):
            print >> out, \
              "DECLare NAME=%s  DOMAin=RECIprocal TYPE=INTEger END" % array_names[0]
            fmt = "%d"
        else:
            raise RuntimeError, \
              "Cannot write array type %s to CNS reflection file" % type(self.data())
        fmt = array_names[0] + "= " + fmt
        for h, d in zip(self.indices(), self.data()):
            print >> out, "INDEx %d %d %d" % h, fmt % d
Beispiel #20
0
    def process_input_array(self, arr):
        #array = self.miller_array.deep_copy()
        array = arr.deep_copy()
        multiplicities = None
        if self.merge_equivalents:
            if self.settings.show_anomalous_pairs:
                merge = array.merge_equivalents()
                multiplicities = merge.redundancies()
                asu, matches = multiplicities.match_bijvoet_mates()
                mult_plus, mult_minus = multiplicities.hemispheres_acentrics()
                anom_mult = flex.int(
                    min(p, m)
                    for (p, m) in zip(mult_plus.data(), mult_minus.data()))
                #flex.min_max_mean_double(anom_mult.as_double()).show()
                anomalous_multiplicities = miller.array(
                    miller.set(asu.crystal_symmetry(),
                               mult_plus.indices(),
                               anomalous_flag=False), anom_mult)
                anomalous_multiplicities = anomalous_multiplicities.select(
                    anomalous_multiplicities.data() > 0)

                array = anomalous_multiplicities
                multiplicities = anomalous_multiplicities

            else:
                merge = array.merge_equivalents()
                array = merge.array()
                multiplicities = merge.redundancies()
        settings = self.settings
        data = array.data()
        self.missing_set = oop.null()
        #if (array.is_xray_intensity_array()):
        #  data.set_selected(data < 0, flex.double(data.size(), 0.))
        if (array.is_unique_set_under_symmetry()) and (settings.map_to_asu):
            array = array.map_to_asu()
            if (multiplicities is not None):
                multiplicities = multiplicities.map_to_asu()
        if (settings.d_min is not None):
            array = array.resolution_filter(d_min=settings.d_min)
            if (multiplicities is not None):
                multiplicities = multiplicities.resolution_filter(
                    d_min=settings.d_min)
        self.filtered_array = array.deep_copy()
        if (settings.expand_anomalous):
            array = array.generate_bijvoet_mates()
            original_symmetry = array.crystal_symmetry()

            if (multiplicities is not None):
                multiplicities = multiplicities.generate_bijvoet_mates()
        if (self.settings.show_missing):
            self.missing_set = array.complete_set().lone_set(array)
            if self.settings.show_anomalous_pairs:
                self.missing_set = self.missing_set.select(
                    self.missing_set.centric_flags().data(), negate=True)
        if (settings.expand_to_p1):
            original_symmetry = array.crystal_symmetry()
            array = array.expand_to_p1().customized_copy(
                crystal_symmetry=original_symmetry)
            #array = array.niggli_cell().expand_to_p1()
            #self.missing_set = self.missing_set.niggli_cell().expand_to_p1()
            self.missing_set = self.missing_set.expand_to_p1().customized_copy(
                crystal_symmetry=original_symmetry)
            if (multiplicities is not None):
                multiplicities = multiplicities.expand_to_p1().customized_copy(
                    crystal_symmetry=original_symmetry)
        data = array.data()
        self.r_free_mode = False
        self.phases = flex.double(data.size(), float('nan'))
        self.radians = flex.double(data.size(), float('nan'))
        self.ampl = flex.double(data.size(), float('nan'))
        #if not self.foms:
        #  self.foms = flex.double(data.size(), float('nan'))
        self.sigmas = None
        #self.sigmas = flex.double(data.size(), float('nan'))
        if isinstance(data, flex.bool):
            self.r_free_mode = True
            data_as_float = flex.double(data.size(), 0.0)
            data_as_float.set_selected(data == True,
                                       flex.double(data.size(), 1.0))
            data = data_as_float
            self.data = data.deep_copy()
        else:
            if isinstance(data, flex.double):
                self.data = data.deep_copy()
            elif isinstance(data, flex.complex_double):
                self.data = data.deep_copy()
                self.ampl = flex.abs(data)
                self.phases = flex.arg(data) * 180.0 / math.pi
                # purge nan values from array to avoid crash in fmod_positive()
                b = flex.bool([bool(math.isnan(e)) for e in self.phases])
                # replace the nan values with an arbitrary float value
                self.phases = self.phases.set_selected(b, 42.4242)
                # indicate the coresponding phase/radian is completely undetermnined
                #self.foms = self.foms.set_selected(b, 0.0)
                # Now cast negative degrees to equivalent positive degrees
                self.phases = flex.fmod_positive(self.phases, 360.0)
                self.radians = flex.arg(data)
                # replace the nan values with an arbitrary float value
                self.radians = self.radians.set_selected(b, 0.424242)
            elif hasattr(array.data(), "as_double"):
                self.data = array.data().as_double()
            else:
                raise RuntimeError("Unexpected data type: %r" % data)
            if (settings.show_data_over_sigma):
                if (array.sigmas() is None):
                    raise Sorry("sigmas not defined.")
                sigmas = array.sigmas()
                non_zero_sel = sigmas != 0
                array = array.select(non_zero_sel)
                array = array.customized_copy(data=array.data() /
                                              array.sigmas())
                self.data = array.data()
                if (multiplicities is not None):
                    multiplicities = multiplicities.select(non_zero_sel)
            if array.sigmas() is not None:
                self.sigmas = array.sigmas()
            else:
                self.sigmas = None
        work_array = array
        work_array.set_info(arr.info())
        multiplicities = multiplicities
        return work_array, multiplicities
Beispiel #21
0
def export_as_cns_hkl(self,
      file_object,
      file_name,
      info,
      array_names,
      r_free_flags):
  out = file_object
  if (file_name): print >> out, "{ file:", file_name, "}"
  if (self.info() is not None):
    print >> out, "{", self.info(), "}"
  crystal_symmetry_as_cns_comments(crystal_symmetry=self, out=out)
  for line in info: print >> out, "{", line, "}"
  print >> out, "NREFlections=%d" % self.indices().size()
  if (self.anomalous_flag()):
    print >> out, "ANOMalous=TRUE"
  else:
    print >> out, "ANOMalous=FALSe"
  if (self.sigmas() is not None):
    if (array_names is None): array_names = ["FOBS", "SIGMA"]
    else: assert len(array_names) == 2
    assert isinstance(self.data(), flex.double)
    assert isinstance(self.sigmas(), flex.double)
    if (self.is_xray_intensity_array()):
      f_obs = self.f_sq_as_f()
    else:
      f_obs = self
    nf, ns = array_names
    print >> out, "DECLare NAME=%s DOMAin=RECIprocal TYPE=REAL END" % nf
    print >> out, "DECLare NAME=%s DOMAin=RECIprocal TYPE=REAL END" % ns
    if (r_free_flags is None):
      for h,f,s in zip(f_obs.indices(),f_obs.data(),f_obs.sigmas()):
        print >> out, "INDEx %d %d %d" % h, "%s= %.6g %s= %.6g" % (nf,f,ns,s)
    else:
      assert r_free_flags.indices().all_eq(f_obs.indices())
      print >> out, "DECLare NAME=TEST DOMAin=RECIprocal TYPE=INTE END"
      for h,f,s,t in zip(f_obs.indices(),f_obs.data(),f_obs.sigmas(),
                         r_free_flags.data()):
        print >> out, "INDEx %d %d %d" % h, "%s= %.6g %s= %.6g" % (nf,f,ns,s),\
          "TEST= %d" % int(t)
  elif (self.is_complex_array()):
    if (array_names is None): array_names = ["F"]
    else: assert len(array_names) == 1
    assert r_free_flags is None
    n = array_names[0]
    print >> out, "DECLare NAME=%s  DOMAin=RECIprocal TYPE=COMPLEX END" % n
    for h,a,p in zip(self.indices(),
                     flex.abs(self.data()),
                     flex.arg(self.data(), True)):
      print >> out, "INDEx %d %d %d" % h, "%s= %.6g %.6g" % (n,a,p)
  elif (self.is_hendrickson_lattman_array()):
    if (array_names is None): array_names = ["PA", "PB", "PC", "PD"]
    else: assert len(array_names) == 4
    assert r_free_flags is None
    for i in range(4):
      print >> out, "DECLare NAME=%s  DOMAin=RECIprocal TYPE=REAL END" %(
        array_names[i])
    print >> out, "GROUp  TYPE=HL"
    for i in range(4):
      print >> out, "     OBJEct=%s" %(array_names[i])
    print >> out, "END"
    for h,hl in zip(self.indices(), self.data()):
      print >> out, "INDEx %d %d %d" % h,
      print >> out, "%s= %.6g" % (array_names[0], hl[0]),
      print >> out, "%s= %.6g" % (array_names[1], hl[1]),
      print >> out, "%s= %.6g" % (array_names[2], hl[2]),
      print >> out, "%s= %.6g" % (array_names[3], hl[3])
  else:
    if (array_names is None): array_names = ["DATA"]
    else: assert len(array_names) == 1
    assert r_free_flags is None
    if (isinstance(self.data(), flex.double)):
      print >> out, \
        "DECLare NAME=%s  DOMAin=RECIprocal TYPE=REAL END" % array_names[0]
      fmt = "%.6g"
    elif (   isinstance(self.data(), flex.int)
          or isinstance(self.data(), flex.bool)):
      print >> out, \
        "DECLare NAME=%s  DOMAin=RECIprocal TYPE=INTEger END" % array_names[0]
      fmt = "%d"
    else:
      raise RuntimeError, \
        "Cannot write array type %s to CNS reflection file" % type(self.data())
    fmt = array_names[0] + "= " + fmt
    for h,d in zip(self.indices(),self.data()):
      print >> out, "INDEx %d %d %d" % h, fmt % d
Beispiel #22
0
 def compute_map_coefficients(self):
     f_obs = self.f_obs_complete.select(
         self.f_obs_complete.d_spacings().data() >= self.d_min)
     f_calc = f_obs.structure_factors_from_map(self.map, use_sg=True)
     f_obs_active = f_obs.select_indices(self.active_indices)
     minimized = relative_scaling.ls_rel_scale_driver(
         f_obs_active,
         f_calc.as_amplitude_array().select_indices(self.active_indices),
         use_intensities=False,
         use_weights=False)
     #minimized.show()
     f_calc = f_calc.customized_copy(data=f_calc.data()\
                                     * math.exp(-minimized.p_scale)\
                                     * adptbx.debye_waller_factor_u_star(
                                       f_calc.indices(), minimized.u_star))
     f_calc_active = f_calc.common_set(f_obs_active)
     matched_indices = f_obs.match_indices(self.f_obs_active)
     lone_indices_selection = matched_indices.single_selection(0)
     from mmtbx.max_lik import maxlik
     alpha_beta_est = maxlik.alpha_beta_est_manager(
         f_obs=f_obs_active,
         f_calc=f_calc_active,
         free_reflections_per_bin=140,
         flags=flex.bool(f_obs_active.size()),
         interpolation=True,
         epsilons=f_obs_active.epsilons().data().as_double())
     alpha, beta = alpha_beta_est.alpha_beta_for_each_reflection(
         f_obs=self.f_obs_complete.select(
             self.f_obs_complete.d_spacings().data() >= self.d_min))
     f_obs.data().copy_selected(lone_indices_selection.iselection(),
                                flex.abs(f_calc.data()))
     t = maxlik.fo_fc_alpha_over_eps_beta(f_obs=f_obs,
                                          f_model=f_calc,
                                          alpha=alpha,
                                          beta=beta)
     hl_coeff = flex.hendrickson_lattman(
         t * flex.cos(f_calc.phases().data()),
         t * flex.sin(f_calc.phases().data()))
     dd = alpha.data()
     #
     hl_array = f_calc.array(
         data=self.hl_coeffs_start.common_set(f_calc).data() + hl_coeff)
     self.compute_phase_source(hl_array)
     fom = flex.abs(self.phase_source.data())
     mFo = hl_array.array(data=f_obs.data() * self.phase_source.data())
     DFc = hl_array.array(data=dd *
                          f_calc.as_amplitude_array().phase_transfer(
                              self.phase_source).data())
     centric_flags = f_obs.centric_flags().data()
     acentric_flags = ~centric_flags
     fo_scale = flex.double(centric_flags.size())
     fc_scale = flex.double(centric_flags.size())
     fo_scale.set_selected(acentric_flags, 2)
     fo_scale.set_selected(centric_flags, 1)
     fc_scale.set_selected(acentric_flags, 1)
     fc_scale.set_selected(centric_flags, 0)
     fo_scale.set_selected(lone_indices_selection, 0)
     fc_scale.set_selected(lone_indices_selection, -1)
     self.map_coeffs = hl_array.array(data=mFo.data() * fo_scale -
                                      DFc.data() * fc_scale)
     self.fom = hl_array.array(data=fom)
     self.hl_coeffs = hl_array
     # statistics
     self.r1_factor = f_obs_active.r1_factor(f_calc_active)
     fom = fom.select(matched_indices.pair_selection(0))
     self.r1_factor_fom = flex.sum(
       fom * flex.abs(f_obs_active.data() - f_calc_active.as_amplitude_array().data())) \
         / flex.sum(fom * f_obs_active.data())
     phase_source, phase_source_previous = self.phase_source.common_sets(
         self.phase_source_previous)
     self.mean_delta_phi = phase_error(
         flex.arg(phase_source.data()),
         flex.arg(phase_source_previous.data()))
     phase_source, phase_source_initial = self.phase_source.common_sets(
         self.phase_source_initial)
     self.mean_delta_phi_initial = phase_error(
         flex.arg(phase_source.data()),
         flex.arg(phase_source_initial.data()))
     self.mean_fom = flex.mean(fom)
     fom = f_obs_active.array(data=fom)
     if fom.data().size() < 1000:  # 2013-12-14 was hard-wired at 1000 tt
         reflections_per_bin = fom.data().size()
     else:
         reflections_per_bin = 1000
     fom.setup_binner(reflections_per_bin=reflections_per_bin)
     self.mean_fom_binned = fom.mean(use_binning=True)
Beispiel #23
0
    assert approx_equal(tuple(miller.phase_transfer(sg, i, a, p, 1.e-10)),
                        ((-1.6099689 - 3.2199379j), 0j))
    a = flex.complex_double((3.6, 4.6))
    try:
        miller.phase_transfer(sg, i, a, p)
    except Exception, e:
        if (str(e.__class__).find("Boost.Python.ArgumentError") < 0):
            raise RuntimeError("Unexpected exception: %s" % str(e))
    else:
        raise Exception_expected

    a = flex.double((-3.6, 4.6))
    p = flex.double((10, 20))
    t = miller.phase_transfer(sg, i, a, p, True)
    assert approx_equal(tuple(flex.abs(t)), flex.abs(a))
    assert approx_equal(tuple(flex.arg(t, True)), (-170, 90))
    p = p * (math.pi / 180)
    t = miller.phase_transfer(sg, i, a, p, False)
    assert approx_equal(tuple(flex.abs(t)), flex.abs(a))
    assert approx_equal(tuple(flex.arg(t, True)), (-170, 90))


def exercise_f_calc_map():
    i = flex.miller_index(((1, 1, 0), (-1, -1, 0), (1, 2, 3), (3, 2, 1)))
    f = flex.complex_double((1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j))
    f_map = miller.f_calc_map(i, f, anomalous_flag=True)
    assert f_map[(1, 1, 0)] == 1 + 1j
    assert f_map[(-1, -1, 0)] == 2 + 2j
    assert f_map[(1, 2, 3)] == 3 + 3j
    assert f_map[(3, 2, 1)] == 4 + 4j
    assert f_map[(2, 2, 2)] == 0