def exercise_value(f_c_in_p1, f_o, flags, n_sampled):
    """ where we check against the trusted correlation map implemented
  in cctbx.translation_search """
    crystal_gridding = f_o.crystal_gridding(
        symmetry_flags=translation_search.symmetry_flags(
            is_isotropic_search_model=False, have_f_part=False),
        resolution_factor=1 / 3)
    correlation_map = translation_search.fast_nv1995(
        gridding=crystal_gridding.n_real(),
        space_group=f_o.space_group(),
        anomalous_flag=f_o.anomalous_flag(),
        miller_indices_f_obs=f_o.indices(),
        f_obs=f_o.data(),
        f_part=flex.complex_double(),  ## no sub-structure is already fixed
        miller_indices_p1_f_calc=f_c_in_p1.indices(),
        p1_f_calc=f_c_in_p1.data()).target_map()
    nx, ny, nz = n = crystal_gridding.n_real()

    def sampled_indices():
        from random import randrange
        yield (0, 0, 0)
        for i in xrange(n_sampled - 1):
            yield randrange(0, n[0]), randrange(0, n[1]), randrange(0, n[2])

    f_o_sq = f_o.as_intensity_array()
    for i, j, k in sampled_indices():
        x = (i / nx, j / ny, k / nz)
        if random.random() > 0.5: f_o_or_f_o_sq = f_o
        else: f_o_or_f_o_sq = f_o_sq
        gos = symmetrised_shifted_structure_factors(f_o_or_f_o_sq, f_c_in_p1,
                                                    x).misfit(f_o_or_f_o_sq)
        assert approx_equal(gos.correlation, correlation_map[i, j,
                                                             k]), (i, j, k)
def exercise_value(f_c_in_p1, f_o, flags, n_sampled):
  """ where we check against the trusted correlation map implemented
  in cctbx.translation_search """
  crystal_gridding = f_o.crystal_gridding(
    symmetry_flags=translation_search.symmetry_flags(
      is_isotropic_search_model=False,
      have_f_part=False),
    resolution_factor=1/3)
  correlation_map = translation_search.fast_nv1995(
    gridding=crystal_gridding.n_real(),
    space_group=f_o.space_group(),
    anomalous_flag=f_o.anomalous_flag(),
    miller_indices_f_obs=f_o.indices(),
    f_obs=f_o.data(),
    f_part=flex.complex_double(), ## no sub-structure is already fixed
    miller_indices_p1_f_calc=f_c_in_p1.indices(),
    p1_f_calc=f_c_in_p1.data()).target_map()
  nx, ny, nz = n = crystal_gridding.n_real()
  def sampled_indices():
    from random import randrange
    yield (0,0,0)
    for i in xrange(n_sampled - 1):
      yield randrange(0, n[0]), randrange(0, n[1]), randrange(0, n[2])
  f_o_sq = f_o.as_intensity_array()
  for i,j,k in sampled_indices():
    x = (i/nx, j/ny, k/nz)
    if random.random() > 0.5: f_o_or_f_o_sq = f_o
    else: f_o_or_f_o_sq = f_o_sq
    gos = symmetrised_shifted_structure_factors(
      f_o_or_f_o_sq, f_c_in_p1, x).misfit(f_o_or_f_o_sq)
    assert approx_equal(gos.correlation, correlation_map[i,j,k]), (i,j,k)
def exercise_gradient(f_c_in_p1, f_o, flags, n_sampled):
  """ where we use finite differences to check our derivatives """
  from random import random
  from scitbx.math import approx_equal_relatively
  f_o_sq = f_o.as_intensity_array()
  for i in xrange(n_sampled):
    x = mat.col((random(), random(), random()))
    gos = symmetrised_shifted_structure_factors(
      f_o_sq, f_c_in_p1, x, compute_gradient=True).misfit(f_o_sq)
    for j,e in enumerate([ (1,0,0), (0,1,0), (0,0,1) ]):
      h = mat.col(e)*1e-3
      fm3, fm2, fm1, f1, f2, f3 = [
        symmetrised_shifted_structure_factors(
          f_o_sq, f_c_in_p1, y).misfit(f_o_sq).value
        for y in (x-3*h, x-2*h, x-h, x+h, x+2*h, x+3*h) ]
      finite_diff = ((f3 - fm3)/60 - 3/20*(f2 - fm2) + 3/4*(f1 - fm1))/abs(h)
      assert approx_equal_relatively(gos.gradient[j], finite_diff,
                                     relative_error=1e-2,
                                     near_zero_threshold=1e-6), \
             (j, i, tuple(x), gos.gradient[j], finite_diff)
def exercise_gradient(f_c_in_p1, f_o, flags, n_sampled):
    """ where we use finite differences to check our derivatives """
    from random import random
    from scitbx.math import approx_equal_relatively
    f_o_sq = f_o.as_intensity_array()
    for i in xrange(n_sampled):
        x = mat.col((random(), random(), random()))
        gos = symmetrised_shifted_structure_factors(
            f_o_sq, f_c_in_p1, x, compute_gradient=True).misfit(f_o_sq)
        for j, e in enumerate([(1, 0, 0), (0, 1, 0), (0, 0, 1)]):
            h = mat.col(e) * 1e-3
            fm3, fm2, fm1, f1, f2, f3 = [
                symmetrised_shifted_structure_factors(f_o_sq, f_c_in_p1,
                                                      y).misfit(f_o_sq).value
                for y in (x - 3 * h, x - 2 * h, x - h, x + h, x + 2 * h,
                          x + 3 * h)
            ]
            finite_diff = ((f3 - fm3) / 60 - 3 / 20 * (f2 - fm2) + 3 / 4 *
                           (f1 - fm1)) / abs(h)
            assert approx_equal_relatively(gos.gradient[j], finite_diff,
                                           relative_error=1e-2,
                                           near_zero_threshold=1e-6), \
                   (j, i, tuple(x), gos.gradient[j], finite_diff)