Beispiel #1
0
 def check_f_calc_derivs():
     eps = 1e-6
     g_fin = flex.complex_double()
     c_fin = flex.vec3_double()
     for ih in xrange(f_calc.size()):
         c_orig = f_calc[ih]
         g_fin_ab = []
         c_fin_ab = []
         for iab in [0, 1]:
             fs = []
             gs = []
             for signed_eps in [eps, -eps]:
                 if iab == 0:
                     f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
                 else:
                     f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
                 trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
                 fs.append(trg_eps.t)
                 gs.append(trg_eps.f_calc_gradients[ih])
             g_fin_ab.append((fs[0] - fs[1]) / (2 * eps))
             c_fin_ab.append((gs[0] - gs[1]) / (2 * eps))
         g_fin.append(complex(*g_fin_ab))
         assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
         c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
         f_calc[ih] = c_orig
     for pn, f, a in zip(g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
         print >> log, "g fin %s:" % pn, numstr(f)
         print >> log, "  ana %s:" % pn, numstr(a)
     assert approx_equal(trg.f_calc_gradients, g_fin)
     for pn, f, a in zip(["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
         print >> log, "c fin %s:" % pn, numstr(f)
         print >> log, "  ana %s:" % pn, numstr(a)
     assert approx_equal(trg.f_calc_hessians, c_fin)
Beispiel #2
0
 def check_f_calc_derivs():
   eps = 1e-6
   g_fin = flex.complex_double()
   c_fin = flex.vec3_double()
   for ih in xrange(f_calc.size()):
     c_orig = f_calc[ih]
     g_fin_ab = []
     c_fin_ab = []
     for iab in [0,1]:
       fs = []
       gs = []
       for signed_eps in [eps, -eps]:
         if (iab == 0):
           f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
         else:
           f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
         trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
         fs.append(trg_eps.t)
         gs.append(trg_eps.f_calc_gradients[ih])
       g_fin_ab.append((fs[0]-fs[1])/(2*eps))
       c_fin_ab.append((gs[0]-gs[1])/(2*eps))
     g_fin.append(complex(*g_fin_ab))
     assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
     c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
     f_calc[ih] = c_orig
   for pn,f,a in zip(
         g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
     print >> log, "g fin %s:" % pn, numstr(f)
     print >> log, "  ana %s:" % pn, numstr(a)
   assert approx_equal(trg.f_calc_gradients, g_fin)
   for pn,f,a in zip(
         ["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
     print >> log, "c fin %s:" % pn, numstr(f)
     print >> log, "  ana %s:" % pn, numstr(a)
   assert approx_equal(trg.f_calc_hessians, c_fin)
Beispiel #3
0
 def check_f_calc_abs_derivs():
     eps = 1e-6
     g_fin = flex.double()
     c_fin = flex.double()
     for ih in range(f_calc_abs.size()):
         fs = []
         gs = []
         c_orig = f_calc_abs[ih]
         for signed_eps in [eps, -eps]:
             f_calc_abs[ih] = c_orig + signed_eps
             trg_eps = r1.target(f_obs=f_obs, f_calc_abs=f_calc_abs)
             fs.append(trg_eps.t)
             gs.append(trg_eps.g[ih])
         g_fin.append((fs[0] - fs[1]) / (2 * eps))
         c_fin.append((gs[0] - gs[1]) / (2 * eps))
         f_calc_abs[ih] = c_orig
     print("g fin:", numstr(g_fin), file=log)
     print("  ana:", numstr(trg.g), file=log)
     assert approx_equal(trg.g, g_fin)
     print("c fin:", numstr(c_fin), file=log)
     print("  ana:", numstr(trg.c), file=log)
     assert approx_equal(trg.c, c_fin)
Beispiel #4
0
 def check_f_calc_abs_derivs():
     eps = 1e-6
     g_fin = flex.double()
     c_fin = flex.double()
     for ih in xrange(f_calc_abs.size()):
         fs = []
         gs = []
         c_orig = f_calc_abs[ih]
         for signed_eps in [eps, -eps]:
             f_calc_abs[ih] = c_orig + signed_eps
             trg_eps = r1.target(f_obs=f_obs, f_calc_abs=f_calc_abs)
             fs.append(trg_eps.t)
             gs.append(trg_eps.g[ih])
         g_fin.append((fs[0] - fs[1]) / (2 * eps))
         c_fin.append((gs[0] - gs[1]) / (2 * eps))
         f_calc_abs[ih] = c_orig
     print >> log, "g fin:", numstr(g_fin)
     print >> log, "  ana:", numstr(trg.g)
     assert approx_equal(trg.g, g_fin)
     print >> log, "c fin:", numstr(c_fin)
     print >> log, "  ana:", numstr(trg.c)
     assert approx_equal(trg.c, c_fin)
Beispiel #5
0
def exercise(mt, n_refl, log):
    f_obs = mt.random_double(size=n_refl)
    f_calc = flex.complex_double(mt.random_double(size=f_obs.size()),
                                 mt.random_double(size=f_obs.size()))
    f_calc_abs = flex.abs(f_calc)
    trg = r1.target(f_obs=f_obs, f_calc=f_calc)

    def check_f_calc_abs_derivs():
        eps = 1e-6
        g_fin = flex.double()
        c_fin = flex.double()
        for ih in range(f_calc_abs.size()):
            fs = []
            gs = []
            c_orig = f_calc_abs[ih]
            for signed_eps in [eps, -eps]:
                f_calc_abs[ih] = c_orig + signed_eps
                trg_eps = r1.target(f_obs=f_obs, f_calc_abs=f_calc_abs)
                fs.append(trg_eps.t)
                gs.append(trg_eps.g[ih])
            g_fin.append((fs[0] - fs[1]) / (2 * eps))
            c_fin.append((gs[0] - gs[1]) / (2 * eps))
            f_calc_abs[ih] = c_orig
        print("g fin:", numstr(g_fin), file=log)
        print("  ana:", numstr(trg.g), file=log)
        assert approx_equal(trg.g, g_fin)
        print("c fin:", numstr(c_fin), file=log)
        print("  ana:", numstr(trg.c), file=log)
        assert approx_equal(trg.c, c_fin)

    def check_f_calc_derivs():
        eps = 1e-6
        g_fin = flex.complex_double()
        c_fin = flex.vec3_double()
        for ih in range(f_calc.size()):
            c_orig = f_calc[ih]
            g_fin_ab = []
            c_fin_ab = []
            for iab in [0, 1]:
                fs = []
                gs = []
                for signed_eps in [eps, -eps]:
                    if (iab == 0):
                        f_calc[ih] = complex(c_orig.real + signed_eps,
                                             c_orig.imag)
                    else:
                        f_calc[ih] = complex(c_orig.real,
                                             c_orig.imag + signed_eps)
                    trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
                    fs.append(trg_eps.t)
                    gs.append(trg_eps.f_calc_gradients[ih])
                g_fin_ab.append((fs[0] - fs[1]) / (2 * eps))
                c_fin_ab.append((gs[0] - gs[1]) / (2 * eps))
            g_fin.append(complex(*g_fin_ab))
            assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
            c_fin.append(
                (c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
            f_calc[ih] = c_orig
        for pn, f, a in zip(g_fin.part_names(), g_fin.parts(),
                            trg.f_calc_gradients.parts()):
            print("g fin %s:" % pn, numstr(f), file=log)
            print("  ana %s:" % pn, numstr(a), file=log)
        assert approx_equal(trg.f_calc_gradients, g_fin)
        for pn, f, a in zip(["aa", "bb", "ab"], c_fin.parts(),
                            trg.f_calc_hessians.parts()):
            print("c fin %s:" % pn, numstr(f), file=log)
            print("  ana %s:" % pn, numstr(a), file=log)
        assert approx_equal(trg.f_calc_hessians, c_fin)

    check_f_calc_abs_derivs()
    check_f_calc_derivs()
    #
    f_calc[0] = 0j
    f_calc_abs = flex.abs(f_calc)
    trg = r1.target(f_obs=f_obs, f_calc=f_calc)
    assert trg.gradients_work()[0] == 0j
    assert trg.hessians_work()[0] == (0, 0, 0)
Beispiel #6
0
def exercise(mt, n_refl, log):
    f_obs = mt.random_double(size=n_refl)
    f_calc = flex.complex_double(mt.random_double(size=f_obs.size()), mt.random_double(size=f_obs.size()))
    f_calc_abs = flex.abs(f_calc)
    trg = r1.target(f_obs=f_obs, f_calc=f_calc)

    def check_f_calc_abs_derivs():
        eps = 1e-6
        g_fin = flex.double()
        c_fin = flex.double()
        for ih in xrange(f_calc_abs.size()):
            fs = []
            gs = []
            c_orig = f_calc_abs[ih]
            for signed_eps in [eps, -eps]:
                f_calc_abs[ih] = c_orig + signed_eps
                trg_eps = r1.target(f_obs=f_obs, f_calc_abs=f_calc_abs)
                fs.append(trg_eps.t)
                gs.append(trg_eps.g[ih])
            g_fin.append((fs[0] - fs[1]) / (2 * eps))
            c_fin.append((gs[0] - gs[1]) / (2 * eps))
            f_calc_abs[ih] = c_orig
        print >> log, "g fin:", numstr(g_fin)
        print >> log, "  ana:", numstr(trg.g)
        assert approx_equal(trg.g, g_fin)
        print >> log, "c fin:", numstr(c_fin)
        print >> log, "  ana:", numstr(trg.c)
        assert approx_equal(trg.c, c_fin)

    def check_f_calc_derivs():
        eps = 1e-6
        g_fin = flex.complex_double()
        c_fin = flex.vec3_double()
        for ih in xrange(f_calc.size()):
            c_orig = f_calc[ih]
            g_fin_ab = []
            c_fin_ab = []
            for iab in [0, 1]:
                fs = []
                gs = []
                for signed_eps in [eps, -eps]:
                    if iab == 0:
                        f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
                    else:
                        f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
                    trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
                    fs.append(trg_eps.t)
                    gs.append(trg_eps.f_calc_gradients[ih])
                g_fin_ab.append((fs[0] - fs[1]) / (2 * eps))
                c_fin_ab.append((gs[0] - gs[1]) / (2 * eps))
            g_fin.append(complex(*g_fin_ab))
            assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
            c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
            f_calc[ih] = c_orig
        for pn, f, a in zip(g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
            print >> log, "g fin %s:" % pn, numstr(f)
            print >> log, "  ana %s:" % pn, numstr(a)
        assert approx_equal(trg.f_calc_gradients, g_fin)
        for pn, f, a in zip(["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
            print >> log, "c fin %s:" % pn, numstr(f)
            print >> log, "  ana %s:" % pn, numstr(a)
        assert approx_equal(trg.f_calc_hessians, c_fin)

    check_f_calc_abs_derivs()
    check_f_calc_derivs()
    #
    f_calc[0] = 0j
    f_calc_abs = flex.abs(f_calc)
    trg = r1.target(f_obs=f_obs, f_calc=f_calc)
    assert trg.gradients_work()[0] == 0j
    assert trg.hessians_work()[0] == (0, 0, 0)
Beispiel #7
0
 def __get_tg(O, f_cbs, derivatives_depth=2, target=Auto, weights=Auto):
   if (target is Auto): target = O.params.target
   if (weights is Auto): weights = O.weights
   if (target.obs_type == "F"):
     obs = O.f_obs
   else:
     obs = O.i_obs
   if (target.type == "ls"):
     if (target.weighting_scheme in ["unit", "shelxl_wght_once"]):
       return xray.targets_least_squares(
         compute_scale_using_all_data=True,
         obs_type=target.obs_type,
         obs=obs.data(),
         weights=O.weights,
         r_free_flags=None,
         f_calc=f_cbs.data(),
         derivatives_depth=derivatives_depth,
         scale_factor=0)
     if (target.weighting_scheme != "shelxl_wght"):
       raise RuntimeError(
         "Unknown: target.weighting_scheme = %s" % target.weighting_scheme)
     assert target.obs_type == "I"
     if (derivatives_depth == 0):
       return O.calc_shelxl_wght_ls(f_cbs=f_cbs.data(), need="t")
     return xray.targets.shelxl_wght_ls(
       f_obs=O.f_obs.data(),
       i_obs=O.i_obs.data(),
       i_sig=O.i_obs.sigmas(),
       f_calc=f_cbs.data(),
       i_calc=None,
       wa=0.1,
       wb=0)
   elif (target.type == "cc"):
     return xray.targets_correlation(
       obs_type=target.obs_type,
       obs=obs.data(),
       weights=O.weights,
       r_free_flags=None,
       f_calc=f_cbs.data(),
       derivatives_depth=derivatives_depth)
   elif (target.type == "r1"):
     assert target.obs_type == "F"
     assert target.weighting_scheme == "unit"
     from cctbx.xray.targets import r1
     return r1.target(
       f_obs=O.f_obs.data(),
       f_calc=f_cbs.data())
   elif (target.type == "ml"):
     assert derivatives_depth in [0,1]
     if (O.epsilons is None):
       raise RuntimeError(
         "target.type=ml can only be used if bulk_solvent_correction=True")
     return xray.targets_maximum_likelihood_criterion(
       f_obs=O.f_obs.data(),
       r_free_flags=O.r_free_flags.data(),
       f_calc=f_cbs.data(),
       alpha=O.alpha_beta[0].data(),
       beta=O.alpha_beta[1].data(),
       scale_factor=1,
       epsilons=O.epsilons.data().as_double(),
       centric_flags=O.centric_flags.data(),
       compute_gradients=bool(derivatives_depth))
   raise RuntimeError("Unknown target_type: %s" % target.type)
Beispiel #8
0
 def __get_tg(O, f_cbs, derivatives_depth=2, target=Auto, weights=Auto):
   if (target is Auto): target = O.params.target
   if (weights is Auto): weights = O.weights
   if (target.obs_type == "F"):
     obs = O.f_obs
   else:
     obs = O.i_obs
   if (target.type == "ls"):
     if (target.weighting_scheme in ["unit", "shelxl_wght_once"]):
       return xray.targets_least_squares(
         compute_scale_using_all_data=True,
         obs_type=target.obs_type,
         obs=obs.data(),
         weights=O.weights,
         r_free_flags=None,
         f_calc=f_cbs.data(),
         derivatives_depth=derivatives_depth,
         scale_factor=0)
     if (target.weighting_scheme != "shelxl_wght"):
       raise RuntimeError(
         "Unknown: target.weighting_scheme = %s" % target.weighting_scheme)
     assert target.obs_type == "I"
     if (derivatives_depth == 0):
       return O.calc_shelxl_wght_ls(f_cbs=f_cbs.data(), need="t")
     return xray.targets.shelxl_wght_ls(
       f_obs=O.f_obs.data(),
       i_obs=O.i_obs.data(),
       i_sig=O.i_obs.sigmas(),
       f_calc=f_cbs.data(),
       i_calc=None,
       wa=0.1,
       wb=0)
   elif (target.type == "cc"):
     return xray.targets_correlation(
       obs_type=target.obs_type,
       obs=obs.data(),
       weights=O.weights,
       r_free_flags=None,
       f_calc=f_cbs.data(),
       derivatives_depth=derivatives_depth)
   elif (target.type == "r1"):
     assert target.obs_type == "F"
     assert target.weighting_scheme == "unit"
     from cctbx.xray.targets import r1
     return r1.target(
       f_obs=O.f_obs.data(),
       f_calc=f_cbs.data())
   elif (target.type == "ml"):
     assert derivatives_depth in [0,1]
     if (O.epsilons is None):
       raise RuntimeError(
         "target.type=ml can only be used if bulk_solvent_correction=True")
     return xray.mlf_target_and_gradients(
       f_obs=O.f_obs.data(),
       r_free_flags=O.r_free_flags.data(),
       f_calc=f_cbs.data(),
       alpha=O.alpha_beta[0].data(),
       beta=O.alpha_beta[1].data(),
       scale_factor=1,
       epsilons=O.epsilons.data().as_double(),
       centric_flags=O.centric_flags.data(),
       compute_gradients=bool(derivatives_depth))
   raise RuntimeError("Unknown target_type: %s" % target.type)