Exemplo n.º 1
0
def test_likelihood_aniso():
    u_star = [0, 0, 0, 0, 0, 0]
    d_star_sq = flex.double(2, 0.25)
    f_obs = flex.double(2, 1.0)
    centric_array = flex.bool(2, True)
    sigma_f_obs = f_obs / 10.0
    sigma_sq = flex.double(2, 1.0)
    epsilon = flex.double(2, 1.0)
    gamma = flex.double(2, 0.0)
    unit_cell = uctbx.unit_cell('20, 30, 40, 90.0, 90.0, 90.0')
    mi = flex.miller_index(((1, 2, 3), (1, 2, 3)))
    xs = crystal.symmetry((20, 30, 40), "P 2 2 2")
    ms = miller.set(xs, mi)
    nll_centric_aniso = scaling.wilson_single_nll_aniso(
        ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0],
        gamma[0], centric_array[0], 0.0, unit_cell, u_star)

    assert approx_equal(nll_centric_aniso, 1.07239)  ## from Mathematica
    nll_acentric_aniso = scaling.wilson_single_nll_aniso(
        ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0],
        gamma[0], centric_array[0], 0.0, unit_cell, u_star)
    centric_array = flex.bool(2, False)
    nll_acentric_aniso = scaling.wilson_single_nll_aniso(
        ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0],
        gamma[0], centric_array[0], 0.0, unit_cell, u_star)
    assert approx_equal(nll_acentric_aniso, 0.306902)  ## from Mathematica

    centric_array = flex.bool(2, True)
    u_star = [1, 1, 1, 0, 0, 0]
    nll_centric_aniso = scaling.wilson_single_nll_aniso(
        ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0],
        gamma[0], centric_array[0], 0.0, unit_cell, u_star)
    assert approx_equal(nll_centric_aniso, 1.535008)  ## from Mathematica
    centric_array = flex.bool(2, False)
    nll_acentric_aniso = scaling.wilson_single_nll_aniso(
        ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0],
        gamma[0], centric_array[0], 0.0, unit_cell, u_star)
    assert approx_equal(nll_acentric_aniso, 0.900003)  ## from Mathematica

    centric_array[1] = True
    nll_total_aniso = scaling.wilson_total_nll_aniso(ms.indices(), f_obs,
                                                     sigma_f_obs, epsilon,
                                                     sigma_sq, gamma,
                                                     centric_array, 0.0,
                                                     unit_cell, u_star)
    assert approx_equal(nll_total_aniso, 2.435011)
Exemplo n.º 2
0
def test_likelihood_aniso():
  u_star = [0,0,0,0,0,0]
  d_star_sq = flex.double(2,0.25)
  f_obs =  flex.double(2,1.0)
  centric_array = flex.bool(2,True)
  sigma_f_obs = f_obs/10.0
  sigma_sq = flex.double(2,1.0)
  epsilon = flex.double(2,1.0)
  gamma =flex.double(2,0.0)
  unit_cell = uctbx.unit_cell('20, 30, 40, 90.0, 90.0, 90.0')
  mi = flex.miller_index(((1,2,3), (1,2,3)))
  xs = crystal.symmetry((20,30,40), "P 2 2 2")
  ms = miller.set(xs, mi)
  nll_centric_aniso = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                                      f_obs[0],
                                                      sigma_f_obs[0],
                                                      epsilon[0],
                                                      sigma_sq[0],
                                                      gamma[0],
                                                      centric_array[0],
                                                      0.0,
                                                      unit_cell,
                                                      u_star)


  assert approx_equal(nll_centric_aniso,  1.07239 ) ## from Mathematica
  nll_acentric_aniso = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                                       f_obs[0],
                                                       sigma_f_obs[0],
                                                       epsilon[0],
                                                       sigma_sq[0],
                                                       gamma[0],
                                                       centric_array[0],
                                                       0.0,
                                                       unit_cell,
                                                       u_star)
  centric_array = flex.bool(2,False)
  nll_acentric_aniso = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                                       f_obs[0],
                                                       sigma_f_obs[0],
                                                       epsilon[0],
                                                       sigma_sq[0],
                                                       gamma[0],
                                                       centric_array[0],
                                                       0.0,
                                                       unit_cell,
                                                       u_star)
  assert approx_equal(nll_acentric_aniso,0.306902 ) ## from Mathematica

  centric_array = flex.bool(2,True)
  u_star = [1,1,1,0,0,0]
  nll_centric_aniso = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                                      f_obs[0],
                                                      sigma_f_obs[0],
                                                      epsilon[0],
                                                      sigma_sq[0],
                                                      gamma[0],
                                                      centric_array[0],
                                                      0.0,
                                                      unit_cell,
                                                      u_star)
  assert approx_equal(nll_centric_aniso,  1.535008 ) ## from Mathematica
  centric_array = flex.bool(2,False)
  nll_acentric_aniso = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                                      f_obs[0],
                                                      sigma_f_obs[0],
                                                      epsilon[0],
                                                      sigma_sq[0],
                                                      gamma[0],
                                                      centric_array[0],
                                                      0.0,
                                                      unit_cell,
                                                      u_star)
  assert approx_equal(nll_acentric_aniso,  0.900003 ) ## from Mathematica

  centric_array[1]=True
  nll_total_aniso = scaling.wilson_total_nll_aniso(ms.indices(),
                                                   f_obs,
                                                   sigma_f_obs,
                                                   epsilon,
                                                   sigma_sq,
                                                   gamma,
                                                   centric_array,
                                                   0.0,
                                                   unit_cell,
                                                   u_star)
  assert approx_equal(nll_total_aniso,  2.435011)
Exemplo n.º 3
0
def finite_diffs_aniso(p_scale, u_star, centric=False, h=0.0001):
    d_star_sq = flex.double(2, 0.25)
    f_obs = flex.double(2, 1.0)
    centric_array = flex.bool(2, centric)
    sigma_f_obs = f_obs / 10.0
    sigma_sq = flex.double(2, 1.0)
    epsilon = flex.double(2, 1.0)
    gamma = flex.double(2, 0.0)
    unit_cell = uctbx.unit_cell('20, 30, 40, 90.0, 90.0, 90.0')
    mi = flex.miller_index(((1, 2, 3), (1, 2, 3)))
    xs = crystal.symmetry((20, 30, 40), "P 2 2 2")
    ms = miller.set(xs, mi)

    nll_norm = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                               sigma_f_obs[0], epsilon[0],
                                               sigma_sq[0], gamma[0],
                                               centric_array[0], p_scale,
                                               unit_cell, u_star)

    nll_scale = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                                sigma_f_obs[0], epsilon[0],
                                                sigma_sq[0], gamma[0],
                                                centric_array[0], p_scale + h,
                                                unit_cell, u_star)
    u_star[0] += h
    nll_u11 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                              sigma_f_obs[0], epsilon[0],
                                              sigma_sq[0], gamma[0],
                                              centric_array[0], p_scale,
                                              unit_cell, u_star)
    u_star[0] -= h
    u_star[1] += h
    nll_u22 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                              sigma_f_obs[0], epsilon[0],
                                              sigma_sq[0], gamma[0],
                                              centric_array[0], p_scale,
                                              unit_cell, u_star)
    u_star[1] -= h
    u_star[2] += h
    nll_u33 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                              sigma_f_obs[0], epsilon[0],
                                              sigma_sq[0], gamma[0],
                                              centric_array[0], p_scale,
                                              unit_cell, u_star)
    u_star[2] -= h
    u_star[3] += h
    nll_u12 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                              sigma_f_obs[0], epsilon[0],
                                              sigma_sq[0], gamma[0],
                                              centric_array[0], p_scale,
                                              unit_cell, u_star)
    u_star[3] -= h
    u_star[4] += h
    nll_u13 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                              sigma_f_obs[0], epsilon[0],
                                              sigma_sq[0], gamma[0],
                                              centric_array[0], p_scale,
                                              unit_cell, u_star)
    u_star[4] -= h
    u_star[5] += h
    nll_u23 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0],
                                              sigma_f_obs[0], epsilon[0],
                                              sigma_sq[0], gamma[0],
                                              centric_array[0], p_scale,
                                              unit_cell, u_star)

    g = scaling.wilson_single_nll_aniso_gradient(ms.indices()[0], f_obs[0],
                                                 sigma_f_obs[0], epsilon[0],
                                                 sigma_sq[0], gamma[0],
                                                 centric_array[0], p_scale,
                                                 unit_cell, u_star)

    g2 = scaling.wilson_total_nll_aniso_gradient(ms.indices(), f_obs,
                                                 sigma_f_obs, epsilon,
                                                 sigma_sq, gamma,
                                                 centric_array, p_scale,
                                                 unit_cell, u_star)
    ds = (nll_norm - nll_scale) / -h
    du11 = (nll_norm - nll_u11) / -h
    du22 = (nll_norm - nll_u22) / -h
    du33 = (nll_norm - nll_u33) / -h
    du12 = (nll_norm - nll_u12) / -h
    du13 = (nll_norm - nll_u13) / -h
    du23 = (nll_norm - nll_u23) / -h
    assert approx_equal(ds, g[0]), (ds, g[0])
    assert approx_equal(du11, g[1]), (du11, g[1])
    assert approx_equal(du22, g[2])
    assert approx_equal(du33, g[3])
    assert approx_equal(du12, g[4])
    assert approx_equal(du13, g[5])
    assert approx_equal(du23, g[6])

    assert approx_equal(ds, g2[0] / 2.0)
    assert approx_equal(du11, g2[1] / 2.0)
    assert approx_equal(du22, g2[2] / 2.0)
    assert approx_equal(du33, g2[3] / 2.0)
    assert approx_equal(du12, g2[4] / 2.0)
    assert approx_equal(du13, g2[5] / 2.0)
    assert approx_equal(du23, g2[6] / 2.0)
Exemplo n.º 4
0
def finite_diffs_aniso(p_scale,
                       u_star,
                       centric=False,
                       h=0.0001):
  d_star_sq = flex.double(2,0.25)
  f_obs =  flex.double(2,1.0)
  centric_array = flex.bool(2,centric)
  sigma_f_obs = f_obs/10.0
  sigma_sq = flex.double(2,1.0)
  epsilon = flex.double(2,1.0)
  gamma =flex.double(2,0.0)
  unit_cell = uctbx.unit_cell('20, 30, 40, 90.0, 90.0, 90.0')
  mi = flex.miller_index(((1,2,3), (1,2,3)))
  xs = crystal.symmetry((20,30,40), "P 2 2 2")
  ms = miller.set(xs, mi)

  nll_norm = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale,
                                             unit_cell,
                                             u_star)

  nll_scale = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale+h,
                                             unit_cell,
                                             u_star)
  u_star[0]+=h
  nll_u11 = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale,
                                             unit_cell,
                                             u_star)
  u_star[0]-=h
  u_star[1]+=h
  nll_u22 = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale,
                                             unit_cell,
                                             u_star)
  u_star[1]-=h
  u_star[2]+=h
  nll_u33 = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale,
                                             unit_cell,
                                             u_star)
  u_star[2]-=h
  u_star[3]+=h
  nll_u12 = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale,
                                             unit_cell,
                                             u_star)
  u_star[3]-=h
  u_star[4]+=h
  nll_u13 = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                             f_obs[0],
                                             sigma_f_obs[0],
                                             epsilon[0],
                                             sigma_sq[0],
                                             gamma[0],
                                             centric_array[0],
                                             p_scale,
                                             unit_cell,
                                             u_star)
  u_star[4]-=h
  u_star[5]+=h
  nll_u23 = scaling.wilson_single_nll_aniso(ms.indices()[0],
                                            f_obs[0],
                                            sigma_f_obs[0],
                                            epsilon[0],
                                            sigma_sq[0],
                                            gamma[0],
                                            centric_array[0],
                                            p_scale,
                                            unit_cell,
                                            u_star)


  g = scaling.wilson_single_nll_aniso_gradient(ms.indices()[0],
                                               f_obs[0],
                                               sigma_f_obs[0],
                                               epsilon[0],
                                               sigma_sq[0],
                                               gamma[0],
                                               centric_array[0],
                                               p_scale,
                                               unit_cell,
                                               u_star)

  g2 = scaling.wilson_total_nll_aniso_gradient(ms.indices(),
                                               f_obs,
                                               sigma_f_obs,
                                               epsilon,
                                               sigma_sq,
                                               gamma,
                                               centric_array,
                                               p_scale,
                                               unit_cell,
                                               u_star)
  ds=(nll_norm-nll_scale)/-h
  du11=(nll_norm-nll_u11)/-h
  du22=(nll_norm-nll_u22)/-h
  du33=(nll_norm-nll_u33)/-h
  du12=(nll_norm-nll_u12)/-h
  du13=(nll_norm-nll_u13)/-h
  du23=(nll_norm-nll_u23)/-h
  assert approx_equal(ds,g[0]), (ds,g[0])
  assert approx_equal(du11,g[1]), (du11,g[1])
  assert approx_equal(du22,g[2])
  assert approx_equal(du33,g[3])
  assert approx_equal(du12,g[4])
  assert approx_equal(du13,g[5])
  assert approx_equal(du23,g[6])

  assert approx_equal(ds,g2[0]/2.0)
  assert approx_equal(du11,g2[1]/2.0)
  assert approx_equal(du22,g2[2]/2.0)
  assert approx_equal(du33,g2[3]/2.0)
  assert approx_equal(du12,g2[4]/2.0)
  assert approx_equal(du13,g2[5]/2.0)
  assert approx_equal(du23,g2[6]/2.0)