示例#1
0
def exercise_ellipsoid(n_trials=100, n_sub_trials=10):
  from gltbx import quadrics
  rnd = random.Random(0)
  for i in xrange(n_trials):
    centre = matrix.col([ rnd.random() for k in xrange(3) ])
    half_lengths = matrix.col([ 0.1 + rnd.random() for k in xrange(3) ])
    r = scitbx.math.euler_angles_as_matrix(
      [ rnd.uniform(0, 360) for i in xrange(3) ], deg=True)
    metrics = r * matrix.diag([ x**2 for x in half_lengths ]) * r.transpose()
    t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
    assert approx_equal(t.translation_part(), centre)
    m = matrix.sqr(t.linear_part())
    assert m.determinant() > 0
    for j in xrange(n_sub_trials):
      y = matrix.col([ rnd.random() for k in xrange(3) ])
      c_y = y.transpose() * y
      x = m*y
      c_x = x.transpose() * metrics.inverse() * x
      assert approx_equal(c_x, c_y)
  r = scitbx.math.euler_angles_as_matrix((30, 115, 260), deg=True)
  centre = matrix.col((-1, 2, 3))
  metrics = r * matrix.diag((-1, 0.1, 1)) * r.transpose()
  t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
  assert t.non_positive_definite()
  x = r * matrix.col((1,0,0))
  assert x.transpose() * metrics.inverse() * x > 0
示例#2
0
def exercise_ellipsoid(n_trials=100, n_sub_trials=10):
    from gltbx import quadrics
    rnd = random.Random(0)
    for i in range(n_trials):
        centre = matrix.col([rnd.random() for k in range(3)])
        half_lengths = matrix.col([0.1 + rnd.random() for k in range(3)])
        r = scitbx.math.euler_angles_as_matrix(
            [rnd.uniform(0, 360) for i in range(3)], deg=True)
        metrics = r * matrix.diag([x**2 for x in half_lengths]) * r.transpose()
        t = quadrics.ellipsoid_to_sphere_transform(centre,
                                                   metrics.as_sym_mat3())
        assert approx_equal(t.translation_part(), centre)
        m = matrix.sqr(t.linear_part())
        assert m.determinant() > 0
        for j in range(n_sub_trials):
            y = matrix.col([rnd.random() for k in range(3)])
            c_y = y.transpose() * y
            x = m * y
            c_x = x.transpose() * metrics.inverse() * x
            assert approx_equal(c_x, c_y)
    r = scitbx.math.euler_angles_as_matrix((30, 115, 260), deg=True)
    centre = matrix.col((-1, 2, 3))
    metrics = r * matrix.diag((-1, 0.1, 1)) * r.transpose()
    t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
    assert t.non_positive_definite()
    x = r * matrix.col((1, 0, 0))
    assert x.transpose() * metrics.inverse() * x > 0
示例#3
0
def print_eigen_values_and_vectors_of_observed_covariance(A, s0):
    """
    Print the eigen values and vectors of a matrix

    """

    # Compute the eigen decomposition of the covariance matrix
    eigen_decomposition = linalg.eigensystem.real_symmetric(
        A.as_flex_double_matrix())
    Q = matrix.sqr(eigen_decomposition.vectors())
    L = matrix.diag(eigen_decomposition.values())

    # Print the matrix eigen values
    logger.info("")
    logger.info("Eigen Values:")
    logger.info("")
    print_matrix(L, indent=2)
    logger.info("")

    logger.info("Eigen Vectors:")
    logger.info("")
    print_matrix(Q, indent=2)
    logger.info("")

    logger.info("Observed covariance in degrees equivalent units")
    logger.info("C1: %.5f degrees" % (sqrt(L[0]) * (180.0 / pi) / s0.length()))
    logger.info("C2: %.5f degrees" % (sqrt(L[3]) * (180.0 / pi) / s0.length()))
示例#4
0
def glm(x, p=None):
    from math import exp, sqrt, log, factorial, lgamma
    from scitbx import matrix
    from scipy.stats import poisson

    beta0 = 0

    if p is None:
        p = [1.0] * len(x)

    X = matrix.rec([1] * len(x), (len(x), 1))
    c = 1.345

    while True:
        n = beta0
        mu = exp(n)
        z = [n + (xx - mu) / mu for xx in x]
        w = [pp * mu for pp in p]
        r = [(xx - mu) / sqrt(mu) for xx in x]
        w2 = [huber(rr, c) for rr in r]

        W = matrix.diag(w)
        # W2 = matrix.diag(w2)
        Z = matrix.col(z)
        beta = (X.transpose() * W * X).inverse() * X.transpose() * W * z
        print beta
        if abs(beta[0] - beta0) < 1e-3:
            break
        beta0 = beta[0]

    # W12 = matrix.diag([sqrt(ww) for ww in w])
    # H = W12 * X * (X.transpose() * W * X).inverse() * X.transpose() * W12

    mu = exp(n)
    return mu
示例#5
0
def print_eigen_values_and_vectors(A):
    """
    Print the eigen values and vectors of a matrix

    """

    # Compute the eigen decomposition of the covariance matrix
    eigen_decomposition = linalg.eigensystem.real_symmetric(
        A.as_flex_double_matrix())
    Q = matrix.sqr(eigen_decomposition.vectors())
    L = matrix.diag(eigen_decomposition.values())

    # Print the matrix eigen values
    logger.info("")
    logger.info("Eigen Values:")
    logger.info("")
    print_matrix(L, indent=2)
    logger.info("")

    logger.info("Eigen Vectors:")
    logger.info("")
    print_matrix(Q, indent=2)
    logger.info("")

    logger.info("Mosaicity in degrees equivalent units")
    logger.info("M1: %.5f degrees" % (sqrt(L[0]) * 180.0 / pi))
    logger.info("M2: %.5f degrees" % (sqrt(L[4]) * 180.0 / pi))
    logger.info("M3: %.5f degrees" % (sqrt(L[8]) * 180.0 / pi))
示例#6
0
def glm33(X, Y, B, P):
  from math import exp , sqrt, log, factorial, lgamma
  from scitbx import matrix
  from scipy.stats import poisson

  X = matrix.rec(list(X), X.all())
  Y = matrix.col(list(Y))
  B = matrix.col(list(B))
  P = matrix.col(list(P))

  while True:
    n = X * B
    mu = matrix.col([exp(nn) for nn in n])

    z = [(yy - mui) / mui for yy, mui in zip(Y, mu)]
    w = [pp * mui for pp, mui in zip(P, mu)]

    W = matrix.diag(w)
    Z = matrix.col(z)
    delta = (X.transpose() * W * X).inverse() * X.transpose()*W*z

    relE = sqrt(sum([d*d for d in delta])/max(1e-10, sum([d*d for d in B])))
    B = B + delta

    # print relE
    if relE < 1e-3:
      break

  return B
def compute_mode(mu, sigma):

    # Compute the eigen decomposition of the covariance matrix
    eigen_decomposition = eigensystem.real_symmetric(
        sigma.as_flex_double_matrix())
    Q = matrix.sqr(eigen_decomposition.vectors())
    L = matrix.diag(eigen_decomposition.values())

    # To simplify calculation of the mode, we change basis
    mu_prime = Q.transpose() * mu

    # Compute the initial value of the lagrange multiplier
    l0 = compute_l0(mu_prime.normalize(), mu_prime, L)

    # Compute the value via Halley's method
    while True:
        mu1, mu2, mu3 = mu_prime
        s1, s2, s3 = L[0], L[4], L[8]
        U = f(l0, mu1, mu2, mu3, s1, s2, s3)
        Up = df(l0, mu1, mu2, mu3, s1, s2, s3)
        Up2 = d2f(l0, mu1, mu2, mu3, s1, s2, s3)
        l = l0 - 2 * U * Up / (2 * Up**2 - U * Up2)
        if abs(l - l0) < 1e-7:
            break
        l0 = l

    # Compute the mode
    x = matrix.col((
        mu_prime[0] / (1 - l * L[0]),
        mu_prime[1] / (1 - l * L[4]),
        mu_prime[2] / (1 - l * L[8]),
    ))

    # Now transform back into the original basis
    return Q * x
示例#8
0
def generate_sigma():
    """
    Generate a random sigma

    """

    # Random eigenvalues
    L = matrix.diag((uniform(1e-5, 1e-4), uniform(1e-5,
                                                  1e-4), uniform(1e-5, 1e-4)))

    # Random angles
    ax = uniform(0, 2 * pi)
    ay = uniform(0, 2 * pi)
    az = uniform(0, 2 * pi)

    # X rotation
    Rx = matrix.sqr((1, 0, 0, 0, cos(ax), -sin(ax), 0, sin(ax), cos(ax)))

    # Y rotation
    Ry = matrix.sqr((cos(ay), 0, sin(ay), 0, 1, 0, -sin(ay), 0, cos(ay)))

    # Z rotation
    Rz = matrix.sqr((cos(az), -sin(az), 0, sin(az), cos(az), 0, 0, 0, 1))

    # Eigen vectors
    Q = Rz * Ry * Rx

    # Compute sigma
    sigma = Q * L * Q.inverse()

    return sigma
def test_lefebvre():
    """Run the test presented in part 4 of the paper Lefebvre et al. (1999),
  http://arxiv.org/abs/hep-ex/9909031."""

    # Simple test following the paper. First MC sim
    mat = matrix.sqr((0.7, 0.2, 0.4, 0.6))
    sig_mat = mat * 0.01
    p_mats = [perturb_mat(mat) for i in range(10000)]
    inv_p_mats = [m.inverse() for m in p_mats]

    # Here use the more exact formula for population covariance as we know the
    # true means rather than sample covariance where we calculate the means
    # from the data
    cov_inv_mat_MC = calc_monte_carlo_population_covariances(
        inv_p_mats, mean_matrix=mat.inverse())

    # Now analytical formula
    cov_mat = matrix.diag([e**2 for e in sig_mat])

    cov_inv_mat = matrix_inverse_error_propagation(mat, cov_mat)
    cov_inv_mat = cov_inv_mat.as_flex_double_matrix()

    # Get fractional differences
    frac = (cov_inv_mat_MC - cov_inv_mat) / cov_inv_mat_MC

    # assert all fractional errors are within 5%. When the random seed is allowed
    # to vary this fails about 7 times in every 100 runs. This is mainly due to
    # the random variation in the MC method itself, as with just 10000 samples
    # one MC estimate compared to another MC estimate regularly has some
    # fractional errors greater than 5%.
    assert all([abs(e) < 0.05 for e in frac])

    return
示例#10
0
def glm33(X, Y, B, P):
    from math import exp, sqrt, log, factorial, lgamma
    from scitbx import matrix
    from scipy.stats import poisson

    X = matrix.rec(list(X), X.all())
    Y = matrix.col(list(Y))
    B = matrix.col(list(B))
    P = matrix.col(list(P))

    while True:
        n = X * B
        mu = matrix.col([exp(nn) for nn in n])

        z = [(yy - mui) / mui for yy, mui in zip(Y, mu)]
        w = [pp * mui for pp, mui in zip(P, mu)]

        W = matrix.diag(w)
        Z = matrix.col(z)
        delta = (X.transpose() * W * X).inverse() * X.transpose() * W * z

        relE = sqrt(
            sum([d * d for d in delta]) / max(1e-10, sum([d * d for d in B])))
        B = B + delta

        # print relE
        if relE < 1e-3:
            break

    return B
示例#11
0
def glm(x, p=None):
    from math import exp, sqrt, log, factorial, lgamma
    from scitbx import matrix
    from scipy.stats import poisson

    beta0 = 0

    if p is None:
        p = [1.0] * len(x)

    X = matrix.rec([1] * len(x), (len(x), 1))
    c = 1.345

    while True:
        n = beta0
        mu = exp(n)
        z = [n + (xx - mu) / mu for xx in x]
        w = [pp * mu for pp in p]
        r = [(xx - mu) / sqrt(mu) for xx in x]
        w2 = [huber(rr, c) for rr in r]

        W = matrix.diag(w)
        # W2 = matrix.diag(w2)
        Z = matrix.col(z)
        beta = (X.transpose() * W * X).inverse() * X.transpose() * W * z
        print(beta)
        if abs(beta[0] - beta0) < 1e-3:
            break
        beta0 = beta[0]

    # W12 = matrix.diag([sqrt(ww) for ww in w])
    # H = W12 * X * (X.transpose() * W * X).inverse() * X.transpose() * W12

    mu = exp(n)
    return mu
示例#12
0
def exercise_impl(svd_impl_name, use_fortran):
    import scitbx.linalg

    svd_impl = getattr(scitbx.linalg, "lapack_%s" % svd_impl_name)
    from scitbx.array_family import flex
    from scitbx import matrix
    from libtbx.test_utils import approx_equal

    #
    for diag in [0, 1]:
        for n in xrange(1, 11):
            a = flex.double(flex.grid(n, n), 0)
            for i in xrange(n):
                a[(i, i)] = diag
            a_inp = a.deep_copy()
            svd = svd_impl(a=a, use_fortran=use_fortran)
            if svd is None:
                if not use_fortran:
                    print "Skipping tests: lapack_%s not available." % svd_impl_name
                return
            assert svd.info == 0
            assert approx_equal(svd.s, [diag] * n)
            assert svd.u.all() == (n, n)
            assert svd.vt.all() == (n, n)

    mt = flex.mersenne_twister(seed=0)
    for m in xrange(1, 11):
        for n in xrange(1, 11):
            a = matrix.rec(elems=tuple(mt.random_double(m * n) * 4 - 2), n=(m, n))
            svd = svd_impl(a=a.transpose().as_flex_double_matrix(), use_fortran=use_fortran)
            assert svd.info == 0
            sigma = matrix.diag(svd.s)  # min(m,n) x min(m,n)
            # FORTRAN layout, so transpose
            u = matrix.rec(svd.u, svd.u.all()).transpose()
            vt = matrix.rec(svd.vt, svd.vt.all()).transpose()
            assert approx_equal(u * sigma * vt, a)
    #
    a = matrix.rec(elems=[0.47, 0.10, -0.21, -0.21, -0.03, 0.35], n=(3, 2))
    svd = svd_impl(a=a.transpose().as_flex_double_matrix(), use_fortran=use_fortran)
    assert svd.info == 0
    assert approx_equal(svd.s, [0.55981345199567534, 0.35931726783538481])
    # again remember column-major storage
    assert approx_equal(
        svd.u,
        [
            0.81402078804155853,
            -0.5136261274467826,
            0.27121644094748704,
            -0.42424674329757839,
            -0.20684171439391938,
            0.88160717215094342,
        ],
    )
    assert approx_equal(svd.vt, [0.8615633693608673, -0.50765003750177129, 0.50765003750177129, 0.8615633693608673])
示例#13
0
def exercise_impl(svd_impl_name, use_fortran):
    import scitbx.linalg
    svd_impl = getattr(scitbx.linalg, "lapack_%s" % svd_impl_name)
    from scitbx.array_family import flex
    from scitbx import matrix
    from libtbx.test_utils import approx_equal
    #
    for diag in [0, 1]:
        for n in range(1, 11):
            a = flex.double(flex.grid(n, n), 0)
            for i in range(n):
                a[(i, i)] = diag
            a_inp = a.deep_copy()
            svd = svd_impl(a=a, use_fortran=use_fortran)
            if (svd is None):
                if (not use_fortran):
                    print("Skipping tests: lapack_%s not available." %
                          svd_impl_name)
                return
            assert svd.info == 0
            assert approx_equal(svd.s, [diag] * n)
            assert svd.u.all() == (n, n)
            assert svd.vt.all() == (n, n)

    mt = flex.mersenne_twister(seed=0)
    for m in range(1, 11):
        for n in range(1, 11):
            a = matrix.rec(elems=tuple(mt.random_double(m * n) * 4 - 2),
                           n=(m, n))
            svd = svd_impl(a=a.transpose().as_flex_double_matrix(),
                           use_fortran=use_fortran)
            assert svd.info == 0
            sigma = matrix.diag(svd.s)  # min(m,n) x min(m,n)
            # FORTRAN layout, so transpose
            u = matrix.rec(svd.u, svd.u.all()).transpose()
            vt = matrix.rec(svd.vt, svd.vt.all()).transpose()
            assert approx_equal(u * sigma * vt, a)
    #
    a = matrix.rec(elems=[0.47, 0.10, -0.21, -0.21, -0.03, 0.35], n=(3, 2))
    svd = svd_impl(a=a.transpose().as_flex_double_matrix(),
                   use_fortran=use_fortran)
    assert svd.info == 0
    assert approx_equal(svd.s, [0.55981345199567534, 0.35931726783538481])
    # again remember column-major storage
    assert approx_equal(svd.u, [
        0.81402078804155853, -0.5136261274467826, 0.27121644094748704,
        -0.42424674329757839, -0.20684171439391938, 0.88160717215094342
    ])
    assert approx_equal(svd.vt, [
        0.8615633693608673, -0.50765003750177129, 0.50765003750177129,
        0.8615633693608673
    ])
示例#14
0
def exercise_tensor_rank_2_orth_and_frac_linear_maps():
    from cctbx import adptbx, sgtbx
    p1 = sgtbx.space_group_info('P1')
    for i in range(100):
        uc = p1.any_compatible_unit_cell(27)
        u_star = matrix.col.random(n=6, a=0, b=1)
        u_iso_ref = adptbx.u_star_as_u_iso(uc, u_star)
        u_iso = matrix.col(uc.u_star_to_u_iso_linear_form()).dot(u_star)
        assert approx_equal(u_iso, u_iso_ref, eps=1e-15)
        u_cart_ref = adptbx.u_star_as_u_cart(uc, u_star)
        u_cart = matrix.sqr(uc.u_star_to_u_cart_linear_map()) * u_star
        assert approx_equal(u_cart, u_cart_ref, eps=1e-15)
        u_cif_ref = adptbx.u_star_as_u_cif(uc, u_star)
        u_cif = matrix.diag(uc.u_star_to_u_cif_linear_map()) * (u_star)
        assert approx_equal(u_cif, u_cif_ref)
示例#15
0
def exercise_tensor_rank_2_orth_and_frac_linear_maps():
  from cctbx import adptbx, sgtbx
  p1 = sgtbx.space_group_info('P1')
  for i in xrange(100):
    uc = p1.any_compatible_unit_cell(27)
    u_star = matrix.col.random(n=6, a=0, b=1)
    u_iso_ref = adptbx.u_star_as_u_iso(uc, u_star)
    u_iso = matrix.col(uc.u_star_to_u_iso_linear_form()).dot(u_star)
    assert approx_equal(u_iso, u_iso_ref, eps=1e-15)
    u_cart_ref = adptbx.u_star_as_u_cart(uc, u_star)
    u_cart = matrix.sqr(uc.u_star_to_u_cart_linear_map()) * u_star
    assert approx_equal(u_cart, u_cart_ref, eps=1e-15)
    u_cif_ref = adptbx.u_star_as_u_cif(uc, u_star)
    u_cif = matrix.diag(uc.u_star_to_u_cif_linear_map())*(u_star)
    assert approx_equal(u_cif, u_cif_ref)
示例#16
0
    def __init__(self, experiment, reflections):
        from dials.algorithms.refinement.parameterisation.crystal_parameters import (
            CrystalUnitCellParameterisation, )
        from dials.algorithms.refinement.parameterisation.crystal_parameters import (
            CrystalOrientationParameterisation, )
        from dials_scratch.jmp.stills import Model
        from dials.array_family import flex
        from scitbx import simplex
        from math import sqrt

        # Store the input
        self.experiment = experiment
        self.reflections = reflections

        self.crystal = self.experiment.crystal

        # Get the current values and generate some offsets
        values = flex.double((0.001, 0, 0.001, 0, 0, 0.001))
        offset = flex.double([0.001 for v in values])

        # The optimization history
        self.history = []

        # Get the initial cell and initial score
        M = matrix.sqr((values[0], 0, 0, values[1], values[2], 0, values[3],
                        values[4], values[5]))
        initial_sigma = M * M.transpose()
        initial_score = self.target(values)

        self.sigma = initial_sigma

        # Perform the optimization
        optimizer = simple_simplex(values, offset, self, 2000)
        result = optimizer.get_solution()
        M = matrix.sqr((result[0], 0, 0, result[1], result[2], 0, result[3],
                        result[4], result[5]))
        self.sigma = M * M.transpose()
        print("Initial sigma:", initial_sigma)
        print("Final sigma:  ", self.sigma)

        # Compute the eigen decomposition of the covariance matrix
        eigen_decomposition = eigensystem.real_symmetric(
            self.sigma.as_flex_double_matrix())
        Q = matrix.sqr(eigen_decomposition.vectors())
        L = matrix.diag(eigen_decomposition.values())
        print(Q)
        print(L)
def test_B_matrix():
    """Test errors in an inverted B matrix, when the errors in B are known (and
  are independent)"""

    Bmat = create_Bmat()
    inv_Bmat = Bmat.inverse()

    # Note that elements in the strict upper triangle of B are zero
    # so their perturbed values will be zero too and the errors in these elements
    # will also be zero (this is what we want)

    # calculate the covariance of elements of inv_B by Monte Carlo simulation,
    # assuming that each element of B has an independent normal error given by a
    # sigma of 1% of the element value
    perturbed_Bmats = [
        perturb_mat(Bmat, fraction_sd=0.01) for i in range(10000)
    ]
    invBs = [m.inverse() for m in perturbed_Bmats]
    cov_invB_MC = calc_monte_carlo_population_covariances(invBs, inv_Bmat)

    # Now calculate using the analytical formula. First need the covariance
    # matrix of B itself. This is the diagonal matrix of errors applied in the
    # simulation.
    n = Bmat.n_rows()
    sig_B = Bmat * 0.01
    cov_B = matrix.diag([e**2 for e in sig_B])

    # Now can use the analytical formula
    cov_invB = matrix_inverse_error_propagation(Bmat, cov_B)
    cov_invB = cov_invB.as_flex_double_matrix()

    # Get fractional differences
    frac = flex.double(flex.grid(n**2, n**2), 0.0)
    for i in range(frac.all()[0]):
        for j in range(frac.all()[1]):
            e1 = cov_invB_MC[i, j]
            e2 = cov_invB[i, j]
            if e1 < 1e-10: continue  # avoid divide-by-zero errors below
            if e2 < 1e-10:
                continue  # avoid elements that are supposed to be zero
            frac[i, j] = (e1 - e2) / e1

    # assert all fractional errors are within 5%
    assert all([abs(e) < 0.05 for e in frac])

    return
示例#18
0
def exercise_eigen_core(diag):
    u = adptbx.random_rotate_ellipsoid(diag + [0., 0., 0.])
    ev = list(adptbx.eigenvalues(u))
    diag.sort()
    ev.sort()
    for i in xrange(3):
        check_eigenvalue(u, ev[i])
    for i in xrange(3):
        assert abs(diag[i] - ev[i]) < 1.e-4
    if (adptbx.is_positive_definite(ev)):
        es = adptbx.eigensystem(u)
        ev = list(es.values())
        ev.sort()
        for i in xrange(3):
            check_eigenvalue(u, ev[i])
        for i in xrange(3):
            assert abs(diag[i] - ev[i]) < 1.e-4
        evec = []
        for i in xrange(3):
            check_eigenvector(u, es.values()[i], es.vectors(i))
            evec.extend(es.vectors(i))
        return  # XXX following tests disabled for the moment
        # sometimes fail if eigenvalues are very similar but not identical
        sqrt_eval = matrix.diag(flex.sqrt(flex.double(es.values())))
        evec = matrix.sqr(evec).transpose()
        sqrt_u = evec * sqrt_eval * evec.transpose()
        u_full = matrix.sym(sym_mat3=u).elems
        assert approx_equal(u_full, (sqrt_u.transpose() * sqrt_u).elems,
                            eps=1.e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u.transpose()).elems,
                            eps=1.e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u).elems, eps=1.e-3)
        sqrt_u_plus_shifts = matrix.sym(sym_mat3=[
            x + 10 * (random.random() - .5) for x in sqrt_u.as_sym_mat3()
        ])
        sts = (sqrt_u_plus_shifts.transpose() *
               sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts *
               sqrt_u_plus_shifts.transpose()).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts * sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
    def __init__(self, nb, bf=1, skew=0, taper=1):
        """
% autoTree  Create System Models of Kinematic Trees
% autoTree(nb,bf,skew,taper) creates system models of kinematic trees
% having revolute joints.  nb and bf specify the number of bodies in the
% tree, and the branching factor, respectively.  The latter is the average
% number of children of a nonterminal node, and must be >=1.  bf=1 produces
% an unbranched tree; bf=2 produces a binary tree; and non-integer values
% produce trees in which the number of children alternates between
% floor(bf) and ceil(bf) in such a way that the average is bf.  Trees are
% constructed (and numbered) breadth-first.  Link i is a thin-walled
% cylindrical tube of length l(i), radius l(i)/20, and mass m(i), lying
% between 0 and l(i) on the x axis of its local coordinate system.  The
% values of l(i) and m(i) are determined by the tapering coefficient:
% l(i)=taper^(i-1) and m(i)=taper^(3*(i-1)).  Thus, if taper=1 then
% m(i)=l(i)=1 for all i.  The inboard joint axis of link i lies on the
% local z axis, and its outboard axis passes through the point (l(i),0,0)
% and is rotated about the x axis by an angle of skew radians relative to
% the inboard axis.  If the link has more than one outboard joint then they
% all have the same axis.  If skew=0 then the mechanism is planar.  The
% final one, two or three arguments can be omitted, in which case they
% assume default values of taper=1, skew=0 and bf=1.
    """
        self.NB = nb
        self.pitch = [0] * nb
        self.parent = [None] * nb
        self.Xtree = []
        self.I = []
        len_ = []
        for i in xrange(nb):
            self.parent[i] = ifloor((i - 1 + math.ceil(bf)) / bf) - 1
            if (self.parent[i] == -1):
                self.Xtree.append(Xtrans([0, 0, 0]))
            else:
                self.Xtree.append(
                    Xrotx(skew) * Xtrans([len_[self.parent[i]], 0, 0]))
            len_.append(taper**i)
            mass = taper**(3 * i)
            CoM = len_[i] * matrix.col([0.5, 0, 0])
            Icm = mass * len_[i]**2 * matrix.diag(
                [0.0025, 1.015 / 12, 1.015 / 12])
            self.I.append(mcI(mass, CoM, Icm))
示例#20
0
  def __init__(self, nb, bf=1, skew=0, taper=1):
    """
% autoTree  Create System Models of Kinematic Trees
% autoTree(nb,bf,skew,taper) creates system models of kinematic trees
% having revolute joints.  nb and bf specify the number of bodies in the
% tree, and the branching factor, respectively.  The latter is the average
% number of children of a nonterminal node, and must be >=1.  bf=1 produces
% an unbranched tree; bf=2 produces a binary tree; and non-integer values
% produce trees in which the number of children alternates between
% floor(bf) and ceil(bf) in such a way that the average is bf.  Trees are
% constructed (and numbered) breadth-first.  Link i is a thin-walled
% cylindrical tube of length l(i), radius l(i)/20, and mass m(i), lying
% between 0 and l(i) on the x axis of its local coordinate system.  The
% values of l(i) and m(i) are determined by the tapering coefficient:
% l(i)=taper^(i-1) and m(i)=taper^(3*(i-1)).  Thus, if taper=1 then
% m(i)=l(i)=1 for all i.  The inboard joint axis of link i lies on the
% local z axis, and its outboard axis passes through the point (l(i),0,0)
% and is rotated about the x axis by an angle of skew radians relative to
% the inboard axis.  If the link has more than one outboard joint then they
% all have the same axis.  If skew=0 then the mechanism is planar.  The
% final one, two or three arguments can be omitted, in which case they
% assume default values of taper=1, skew=0 and bf=1.
    """
    self.NB = nb
    self.pitch = [0] * nb
    self.parent = [None] * nb
    self.Xtree = []
    self.I = []
    len_ = []
    for i in xrange(nb):
      self.parent[i] = ifloor((i-1+math.ceil(bf))/bf)-1
      if (self.parent[i] == -1):
        self.Xtree.append(Xtrans([0,0,0]))
      else:
        self.Xtree.append(Xrotx(skew) * Xtrans([len_[self.parent[i]],0,0]))
      len_.append(taper**i)
      mass = taper**(3*i)
      CoM = len_[i] * matrix.col([0.5,0,0])
      Icm = mass * len_[i]**2 * matrix.diag([0.0025,1.015/12,1.015/12])
      self.I.append(mcI(mass, CoM, Icm))
示例#21
0
def exercise_eigen_core(diag):
    u = adptbx.random_rotate_ellipsoid(diag + [0.0, 0.0, 0.0])
    ev = list(adptbx.eigenvalues(u))
    diag.sort()
    ev.sort()
    for i in xrange(3):
        check_eigenvalue(u, ev[i])
    for i in xrange(3):
        assert abs(diag[i] - ev[i]) < 1.0e-4
    if adptbx.is_positive_definite(ev):
        es = adptbx.eigensystem(u)
        ev = list(es.values())
        ev.sort()
        for i in xrange(3):
            check_eigenvalue(u, ev[i])
        for i in xrange(3):
            assert abs(diag[i] - ev[i]) < 1.0e-4
        evec = []
        for i in xrange(3):
            check_eigenvector(u, es.values()[i], es.vectors(i))
            evec.extend(es.vectors(i))
        return  # XXX following tests disabled for the moment
        # sometimes fail if eigenvalues are very similar but not identical
        sqrt_eval = matrix.diag(flex.sqrt(flex.double(es.values())))
        evec = matrix.sqr(evec).transpose()
        sqrt_u = evec * sqrt_eval * evec.transpose()
        u_full = matrix.sym(sym_mat3=u).elems
        assert approx_equal(u_full, (sqrt_u.transpose() * sqrt_u).elems, eps=1.0e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u.transpose()).elems, eps=1.0e-3)
        assert approx_equal(u_full, (sqrt_u * sqrt_u).elems, eps=1.0e-3)
        sqrt_u_plus_shifts = matrix.sym(sym_mat3=[x + 10 * (random.random() - 0.5) for x in sqrt_u.as_sym_mat3()])
        sts = (sqrt_u_plus_shifts.transpose() * sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts * sqrt_u_plus_shifts.transpose()).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
        sts = (sqrt_u_plus_shifts * sqrt_u_plus_shifts).as_sym_mat3()
        ev = adptbx.eigenvalues(sts)
        assert min(ev) >= 0
示例#22
0
def glm33(x, p):
    from math import exp, sqrt, log, factorial, lgamma
    from scitbx import matrix
    from scipy.stats import poisson

    beta0 = 0

    while True:
        n = beta0
        mu = exp(n)

        z = [(xx - mu) / mu for xx in x]
        w = [pp * mu for pp in p]

        W = matrix.diag(w)
        Z = matrix.col(z)
        X = matrix.rec([1] * len(z), (len(z), 1))
        H = X * (X.transpose() * W * X).inverse() * X.transpose() * W
        H = [H[i + i * len(z)] for i in range(len(z))]
        MSE = sum((xx - mu)**2 for xx in x) / len(x)
        r = [xx - mu for xx in x]
        D = [rr * rr / (1 * MSE) * (hh / (1 - hh)**2) for rr, hh in zip(r, H)]
        N = sum(1 for d in D if d > 4 / len(x))
        # print X.transpose()*W*z
        # print (W*X)[0]
        # print (X.transpose()*W*X).inverse()[0]
        delta = (X.transpose() * W * X).inverse() * X.transpose() * W * z
        # print delta

        relE = sqrt(
            sum([d * d
                 for d in delta]) / max(1e-10, sum([d * d for d in [beta0]])))
        beta0 = beta0 + delta[0]

        # print relE
        if relE < 1e-3:
            break

    return exp(beta0)
 def core(a):
     c = flex.double(a)
     c.resize(flex.grid(a.n))
     u = c.matrix_upper_triangle_as_packed_u()
     gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(
         u, epsilon=1.e-8)
     assert gwm.epsilon == 1.e-8
     u = c.matrix_upper_triangle_as_packed_u()
     gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(
         u)
     assert gwm.epsilon == scitbx.math.floating_point_epsilon_double_get()
     assert gwm.packed_u.id() == u.id()
     p, e = gwm.pivots, gwm.e
     r = matrix.sqr(u.matrix_packed_u_as_upper_triangle())
     if a.n != (0, 0):
         rtr = r.transpose() * r
         pm = p_as_mx(p)
         papt = pm * a * pm.transpose()
         paept = papt + matrix.diag(e)
         delta_decomposition = scitbx.linalg.matrix_equality_ratio(
             paept, rtr)
         assert delta_decomposition < 10, delta_decomposition
         b = flex.random_double(size=a.n[0], factor=2) - 1
         x = gwm.solve(b=b)
         px = pm * matrix.col(x)
         pb = pm * matrix.col(b)
         if 0:
             eigen = scitbx.linalg.eigensystem.real_symmetric(
                 paept.as_flex_double_matrix())
             lambda_ = eigen.values()
             print "condition number: ", lambda_[0] / lambda_[-1]
         delta_solve = scitbx.linalg.matrix_cholesky_test_ratio(
             a=paept.as_flex_double_matrix(),
             x=flex.double(px),
             b=flex.double(pb),
             epsilon=gwm.epsilon)
         assert delta_solve < 10, delta_solve
     return p, e, r
示例#24
0
 def core(a):
   c = flex.double(a)
   c.resize(flex.grid(a.n))
   u = c.matrix_upper_triangle_as_packed_u()
   gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(
     u,
     epsilon=1.e-8)
   assert gwm.epsilon == 1.e-8
   u = c.matrix_upper_triangle_as_packed_u()
   gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(u)
   assert gwm.epsilon == scitbx.math.floating_point_epsilon_double_get()
   assert gwm.packed_u.id() == u.id()
   p, e = gwm.pivots, gwm.e
   r = matrix.sqr(u.matrix_packed_u_as_upper_triangle())
   if a.n != (0,0):
     rtr = r.transpose() * r
     pm = p_as_mx(p)
     papt = pm * a * pm.transpose()
     paept = papt + matrix.diag(e)
     delta_decomposition = scitbx.linalg.matrix_equality_ratio(paept, rtr)
     assert delta_decomposition < 10, delta_decomposition
     b = flex.random_double(size=a.n[0], factor=2)-1
     x = gwm.solve(b=b)
     px = pm * matrix.col(x)
     pb = pm * matrix.col(b)
     if 0:
       eigen = scitbx.linalg.eigensystem.real_symmetric(
         paept.as_flex_double_matrix())
       lambda_ = eigen.values()
       print "condition number: ", lambda_[0]/lambda_[-1]
     delta_solve = scitbx.linalg.matrix_cholesky_test_ratio(
       a=paept.as_flex_double_matrix(),
       x=flex.double(px),
       b=flex.double(pb),
       epsilon=gwm.epsilon)
     assert delta_solve < 10, delta_solve
   return p, e, r
示例#25
0
def glm33(x, p):
  from math import exp , sqrt, log, factorial, lgamma
  from scitbx import matrix
  from scipy.stats import poisson
  beta0 = 0

  while True:
    n = beta0
    mu = exp(n)

    z = [(xx - mu) / mu for xx in x]
    w = [pp * mu for pp in p]

    W = matrix.diag(w)
    Z = matrix.col(z)
    X = matrix.rec([1]*len(z), (len(z),1))
    H = X*(X.transpose() *W* X).inverse() * X.transpose()*W
    H = [H[i+i*len(z)] for i in range(len(z))]
    MSE = sum((xx-mu)**2 for xx in x) / len(x)
    r = [xx - mu for xx in x]
    D = [rr*rr / (1 * MSE) * (hh / (1-hh)**2) for rr, hh in zip(r, H)]
    N = sum(1 for d in D if d > 4 / len(x))
    # print X.transpose()*W*z
    # print (W*X)[0]
    # print (X.transpose()*W*X).inverse()[0]
    delta = (X.transpose() * W * X).inverse() * X.transpose()*W*z
    # print delta

    relE = sqrt(sum([d*d for d in delta])/max(1e-10, sum([d*d for d in [beta0]])))
    beta0 = beta0 + delta[0]

    # print relE
    if relE < 1e-3:
      break

  return exp(beta0)
示例#26
0
def glm2(y):
    from math import sqrt, exp, floor, log
    from scipy.stats import poisson
    from scitbx import matrix
    from dials.array_family import flex

    y = flex.double(y)

    x = flex.double([1.0 for yy in y])
    w = flex.double([1.0 for yy in y])

    X = matrix.rec(x, (len(x), 1))

    c = 1.345

    beta = matrix.col([0])

    maxiter = 10
    accuracy = 1e-3

    for iter in range(maxiter):

        ni = flex.double([1.0 for xx in x])
        sni = flex.sqrt(ni)

        eta = flex.double(X * beta)

        mu = flex.exp(eta)
        dmu_deta = flex.exp(eta)

        Vmu = mu
        sVF = flex.sqrt(Vmu)
        residP = (y - mu) * sni / sVF

        phi = 1
        sV = sVF * sqrt(phi)
        residPS = residP / sqrt(phi)

        H = flex.floor(mu * ni - c * sni * sV)
        K = flex.floor(mu * ni + c * sni * sV)
        # print min(H)
        dpH = flex.double([poisson(mui).pmf(Hi) for mui, Hi in zip(mu, H)])
        dpH1 = flex.double([poisson(mui).pmf(Hi - 1) for mui, Hi in zip(mu, H)])
        dpK = flex.double([poisson(mui).pmf(Ki) for mui, Ki in zip(mu, K)])
        dpK1 = flex.double([poisson(mui).pmf(Ki - 1) for mui, Ki in zip(mu, K)])
        pHm1 = flex.double([poisson(mui).cdf(Hi - 1) for mui, Hi in zip(mu, H)])
        pKm1 = flex.double([poisson(mui).cdf(Ki - 1) for mui, Ki in zip(mu, K)])
        pH = pHm1 + dpH  # = ppois(H,*)
        pK = pKm1 + dpK  # = ppois(K,*)
        E2f = mu * (dpH1 - dpH - dpK1 + dpK) + pKm1 - pHm1
        Epsi = c * (1.0 - pK - pH) + (mu / sV) * (dpH - dpK)
        Epsi2 = c * c * (pH + 1.0 - pK) + E2f
        EpsiS = c * (dpH + dpK) + E2f / sV

        psi = flex.double([huber(rr, c) for rr in residPS])
        cpsi = psi - Epsi
        temp = cpsi * w * sni / sV * dmu_deta
        EEqMat = [0] * len(X)
        for j in range(X.n_rows()):
            for i in range(X.n_columns()):
                k = i + j * X.n_columns()
                EEqMat[k] = X[k] * temp[j]
        EEqMat = matrix.rec(EEqMat, (X.n_rows(), X.n_columns()))
        EEq = []
        for i in range(EEqMat.n_columns()):
            col = []
            for j in range(EEqMat.n_rows()):
                k = i + j * EEqMat.n_columns()
                col.append(EEqMat[k])
            EEq.append(sum(col) / len(col))
        EEq = matrix.col(EEq)
        DiagB = EpsiS / (sni * sV) * w * (ni * dmu_deta) ** 2
        B = matrix.diag(DiagB)
        H = (X.transpose() * B * X) / len(y)

        dbeta = H.inverse() * EEq
        beta_new = beta + dbeta

        relE = sqrt(sum([d * d for d in dbeta]) / max(1e-10, sum([d * d for d in beta])))
        beta = beta_new

        # print relE
        if relE < accuracy:
            break

    weights = [min(1, c / abs(r)) for r in residPS]

    eta = flex.double(X * beta)

    mu = flex.exp(eta)
    return beta
示例#27
0
def glm(x, p=None):
    from math import exp, sqrt, log, factorial, lgamma
    from scitbx import matrix
    from scipy.stats import poisson

    beta0 = 0

    if p is None:
        p = [1.0] * len(x)

    while True:
        n = beta0
        mu = exp(n)
        z = [n + (xx - mu) / mu for xx in x]
        w = [pp * mu for pp in p]

        W = matrix.diag(w)
        Z = matrix.col(z)
        X = matrix.rec([1] * len(z), (len(z), 1))

        beta = (X.transpose() * W * X).inverse() * X.transpose() * W * z

        if abs(beta[0] - beta0) < 1e-3:
            break
        beta0 = beta[0]

    n = beta[0]
    mu = exp(n)
    z = [n + (xx - mu) / mu for xx in x]
    w = [pp * mu for pp in p]

    W = matrix.diag(w)
    Z = matrix.col(z)
    X = matrix.rec([1] * len(z), (len(z), 1))
    W12 = matrix.diag([sqrt(ww) for ww in w])
    H = X * (X.transpose() * W * X).inverse() * X.transpose()

    r = [(xx - mu) / mu for xx in x]
    sum1 = 0
    sum2 = 0
    sum3 = 0
    D = []
    for xx in x:
        d = 0
        if xx != 0:
            d += xx * log(xx)
            d -= xx * log(mu)
        d -= xx - mu
        D.append(2 * d)
    # r = []
    # for xx in x:
    #   if xx == 0:
    #     r.append(0)
    #   else:
    #     r.append(2*(log(xx) - log(mu)))
    # D = []
    # for i in range(len(r)):
    #   h = H[i+i*H.n[0]]
    #   d = (r[i]/(1 - h))**2 * h/1.0
    #   D.append(d)

    # print min(x), max(x)
    print(min(D), max(D), F(1, len(x)))

    return exp(beta[0]), max(D) > F(1, len(x))
示例#28
0
def exercise_cif_from_cctbx():
    quartz = xray.structure(crystal_symmetry=crystal.symmetry(
        (5.01, 5.01, 5.47, 90, 90, 120), "P6222"),
                            scatterers=flex.xray_scatterer([
                                xray.scatterer("Si", (1 / 2., 1 / 2., 1 / 3.)),
                                xray.scatterer("O", (0.197, -0.197, 0.83333))
                            ]))
    for sc in quartz.scatterers():
        sc.flags.set_grad_site(True)
    s = StringIO()
    loop = geometry.distances_as_cif_loop(
        quartz.pair_asu_table(distance_cutoff=2),
        site_labels=quartz.scatterers().extract_labels(),
        sites_frac=quartz.sites_frac()).loop
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_bond_atom_site_label_1
  _geom_bond_atom_site_label_2
  _geom_bond_distance
  _geom_bond_site_symmetry_2
  Si  O  1.6160  4_554
  Si  O  1.6160  2_554
  Si  O  1.6160  3_664
  Si  O  1.6160  5_664

""")
    s = StringIO()
    loop = geometry.angles_as_cif_loop(
        quartz.pair_asu_table(distance_cutoff=2),
        site_labels=quartz.scatterers().extract_labels(),
        sites_frac=quartz.sites_frac()).loop
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_angle_atom_site_label_1
  _geom_angle_atom_site_label_2
  _geom_angle_atom_site_label_3
  _geom_angle
  _geom_angle_site_symmetry_1
  _geom_angle_site_symmetry_3
  O   Si  O   101.3  2_554  4_554
  O   Si  O   111.3  3_664  4_554
  O   Si  O   116.1  3_664  2_554
  O   Si  O   116.1  5_664  4_554
  O   Si  O   111.3  5_664  2_554
  O   Si  O   101.3  5_664  3_664
  Si  O   Si  146.9  3      5

""")
    # with a covariance matrix
    flex.set_random_seed(1)
    vcv_matrix = matrix.diag(
      flex.random_double(size=quartz.n_parameters(), factor=1e-5))\
               .as_flex_double_matrix().matrix_symmetric_as_packed_u()
    s = StringIO()
    loop = geometry.distances_as_cif_loop(
        quartz.pair_asu_table(distance_cutoff=2),
        site_labels=quartz.scatterers().extract_labels(),
        sites_frac=quartz.sites_frac(),
        covariance_matrix=vcv_matrix,
        parameter_map=quartz.parameter_map()).loop
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_bond_atom_site_label_1
  _geom_bond_atom_site_label_2
  _geom_bond_distance
  _geom_bond_site_symmetry_2
  Si  O  1.616(14)  4_554
  Si  O  1.616(12)  2_554
  Si  O  1.616(14)  3_664
  Si  O  1.616(12)  5_664

""")
    s = StringIO()
    loop = geometry.angles_as_cif_loop(
        quartz.pair_asu_table(distance_cutoff=2),
        site_labels=quartz.scatterers().extract_labels(),
        sites_frac=quartz.sites_frac(),
        covariance_matrix=vcv_matrix,
        parameter_map=quartz.parameter_map()).loop
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_angle_atom_site_label_1
  _geom_angle_atom_site_label_2
  _geom_angle_atom_site_label_3
  _geom_angle
  _geom_angle_site_symmetry_1
  _geom_angle_site_symmetry_3
  O   Si  O    101.3(8)  2_554  4_554
  O   Si  O   111.3(10)  3_664  4_554
  O   Si  O    116.1(9)  3_664  2_554
  O   Si  O    116.1(9)  5_664  4_554
  O   Si  O   111.3(10)  5_664  2_554
  O   Si  O    101.3(8)  5_664  3_664
  Si  O   Si   146.9(9)  3      5

""")
    cell_vcv = flex.pow2(matrix.diag(flex.random_double(size=6,factor=1e-1))\
                         .as_flex_double_matrix().matrix_symmetric_as_packed_u())
    s = StringIO()
    loop = geometry.distances_as_cif_loop(
        quartz.pair_asu_table(distance_cutoff=2),
        site_labels=quartz.scatterers().extract_labels(),
        sites_frac=quartz.sites_frac(),
        covariance_matrix=vcv_matrix,
        cell_covariance_matrix=cell_vcv,
        parameter_map=quartz.parameter_map()).loop
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_bond_atom_site_label_1
  _geom_bond_atom_site_label_2
  _geom_bond_distance
  _geom_bond_site_symmetry_2
  Si  O  1.616(15)  4_554
  Si  O  1.616(19)  2_554
  Si  O  1.616(15)  3_664
  Si  O  1.616(19)  5_664

""")
示例#29
0
    def compute(self, reflections):

        print("Computing bbox for %d reflections" % len(reflections))

        # Compute quantile
        quantile = chisq_quantile(2, 0.997)
        D = sqrt(quantile) * 2
        bbox = flex.int6()
        print("ML: %f" % D)
        for i in range(len(reflections)):
            s1 = matrix.col(reflections[i]["s1"])
            s2 = matrix.col(reflections[i]["s2"])

            s0 = matrix.col(self.experiment.beam.get_s0())

            # Ensure our values are ok
            assert s1.length() > 0

            sigma = MosaicityParameterisation(self.parameters).sigma()
            R = compute_change_of_basis_operation(s0, s2)
            S = R * sigma * R.transpose()
            mu = R * s2
            assert abs(1 - mu.normalize().dot(matrix.col((0, 0, 1)))) < 1e-7

            S11 = matrix.sqr((S[0], S[1], S[3], S[4]))
            S12 = matrix.col((S[2], S[5]))
            S21 = matrix.col((S[6], S[7])).transpose()
            S22 = S[8]

            mu1 = matrix.col((mu[0], mu[1]))
            mu2 = mu[2]

            mubar = mu1 + S12 * (1 / S22) * (s0.length() - mu2)
            Sbar = S11 - S12 * (1 / S22) * S21

            eigen_decomposition = eigensystem.real_symmetric(
                Sbar.as_flex_double_matrix())
            Q = matrix.sqr(eigen_decomposition.vectors())
            L = matrix.diag(eigen_decomposition.values())
            max_L = max(L)

            delta = sqrt(max_L) * D

            p1 = mubar + matrix.col((-delta, -delta))
            p2 = mubar + matrix.col((-delta, +delta))
            p3 = mubar + matrix.col((+delta, -delta))
            p4 = mubar + matrix.col((+delta, +delta))

            p1 = matrix.col((p1[0], p1[1], s0.length())).normalize()
            p2 = matrix.col((p2[0], p2[1], s0.length())).normalize()
            p3 = matrix.col((p3[0], p3[1], s0.length())).normalize()
            p4 = matrix.col((p4[0], p4[1], s0.length())).normalize()

            x1 = R.transpose() * p1
            x2 = R.transpose() * p2
            x3 = R.transpose() * p3
            x4 = R.transpose() * p4

            xy1 = self.experiment.detector[0].get_ray_intersection_px(x1)
            xy2 = self.experiment.detector[0].get_ray_intersection_px(x2)
            xy3 = self.experiment.detector[0].get_ray_intersection_px(x3)
            xy4 = self.experiment.detector[0].get_ray_intersection_px(x4)

            xx = (xy1[0], xy2[0], xy3[0], xy4[0])
            yy = (xy1[1], xy2[1], xy3[1], xy4[1])
            x0, x1 = int(floor(min(xx))) - 1, int(ceil(max(xx))) + 1
            y0, y1 = int(floor(min(yy))) - 1, int(ceil(max(yy))) + 1
            assert x1 > x0
            assert y1 > y0
            bbox.append((x0, x1, y0, y1, 0, 1))

        reflections["bbox"] = bbox

        x0, x1, y0, y1, _, _ = bbox.parts()
        xsize, ysize = self.experiment.detector[0].get_image_size()
        selection = (x1 > 0) & (y1 > 0) & (x0 < xsize) & (y0 < ysize)

        reflections = reflections.select(selection)
        print("Filtered reflecions with bbox outside image range")
        print("Kept %d reflections" % len(reflections))
        return reflections
示例#30
0
def exercise_hbond_as_cif_loop():
    xs = sucrose()
    for sc in xs.scatterers():
        sc.flags.set_grad_site(True)
    radii = [
        covalent_radii.table(elt).radius()
        for elt in xs.scattering_type_registry().type_index_pairs_as_dict()
    ]
    asu_mappings = xs.asu_mappings(buffer_thickness=2 * max(radii) + 0.5)
    pair_asu_table = crystal.pair_asu_table(asu_mappings)
    pair_asu_table.add_covalent_pairs(xs.scattering_types(), tolerance=0.5)
    hbonds = [
        geometry.hbond(1, 5, sgtbx.rt_mx('-X,0.5+Y,2-Z')),
        geometry.hbond(5, 14, sgtbx.rt_mx('-X,-0.5+Y,1-Z')),
        geometry.hbond(7, 10, sgtbx.rt_mx('1+X,+Y,+Z')),
        geometry.hbond(10, 0),
        geometry.hbond(12, 14, sgtbx.rt_mx('-1-X,0.5+Y,1-Z')),
        geometry.hbond(14, 12, sgtbx.rt_mx('-1-X,-0.5+Y,1-Z')),
        geometry.hbond(16, 7)
    ]
    loop = geometry.hbonds_as_cif_loop(hbonds,
                                       pair_asu_table,
                                       xs.scatterers().extract_labels(),
                                       sites_frac=xs.sites_frac()).loop
    s = StringIO()
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.8200  2.0636  2.8635  165.0  2_557
  O4   H4   O9  0.8200  2.0559  2.8736  174.9  2_546
  O5   H5   O7  0.8200  2.0496  2.8589  169.0  1_655
  O7   H7   O1  0.8200  2.0573  2.8617  166.8  .
  O8   H8   O9  0.8200  2.1407  2.8943  152.8  2_456
  O9   H9   O8  0.8200  2.1031  2.8943  162.1  2_446
  O10  H10  O5  0.8200  2.0167  2.7979  159.1  .

""")
    # with a covariance matrix
    flex.set_random_seed(1)
    vcv_matrix = matrix.diag(
      flex.random_double(size=xs.n_parameters(), factor=1e-5))\
               .as_flex_double_matrix().matrix_symmetric_as_packed_u()
    loop = geometry.hbonds_as_cif_loop(hbonds,
                                       pair_asu_table,
                                       xs.scatterers().extract_labels(),
                                       sites_frac=xs.sites_frac(),
                                       covariance_matrix=vcv_matrix,
                                       parameter_map=xs.parameter_map()).loop
    s = StringIO()
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(3)    2.86(3)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)    2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.859(19)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(2)    2.86(2)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(3)    2.89(3)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)    2.89(3)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)    2.80(3)     159(3)  .

""")
    cell_vcv = flex.pow2(matrix.diag(flex.random_double(size=6,factor=1e-1))\
                         .as_flex_double_matrix().matrix_symmetric_as_packed_u())
    loop = geometry.hbonds_as_cif_loop(hbonds,
                                       pair_asu_table,
                                       xs.scatterers().extract_labels(),
                                       sites_frac=xs.sites_frac(),
                                       covariance_matrix=vcv_matrix,
                                       cell_covariance_matrix=cell_vcv,
                                       parameter_map=xs.parameter_map()).loop
    s = StringIO()
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(4)  2.86(4)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)  2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.86(2)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(3)  2.86(3)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(4)  2.89(4)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)  2.89(4)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)  2.80(3)     159(3)  .

""")
示例#31
0
def glm2(y):
    from math import sqrt, exp, floor, log
    from scipy.stats import poisson
    from scitbx import matrix
    from dials.array_family import flex

    y = flex.double(y)

    x = flex.double([1.0 for yy in y])
    w = flex.double([1.0 for yy in y])

    X = matrix.rec(x, (len(x), 1))

    c = 1.345

    beta = matrix.col([0])

    maxiter = 10
    accuracy = 1e-3

    for iter in range(maxiter):

        ni = flex.double([1.0 for xx in x])
        sni = flex.sqrt(ni)

        eta = flex.double(X * beta)

        mu = flex.exp(eta)
        dmu_deta = flex.exp(eta)

        Vmu = mu
        sVF = flex.sqrt(Vmu)
        residP = (y - mu) * sni / sVF

        phi = 1
        sV = sVF * sqrt(phi)
        residPS = residP / sqrt(phi)

        H = flex.floor(mu * ni - c * sni * sV)
        K = flex.floor(mu * ni + c * sni * sV)
        # print min(H)
        dpH = flex.double([poisson(mui).pmf(Hi) for mui, Hi in zip(mu, H)])
        dpH1 = flex.double(
            [poisson(mui).pmf(Hi - 1) for mui, Hi in zip(mu, H)])
        dpK = flex.double([poisson(mui).pmf(Ki) for mui, Ki in zip(mu, K)])
        dpK1 = flex.double(
            [poisson(mui).pmf(Ki - 1) for mui, Ki in zip(mu, K)])
        pHm1 = flex.double(
            [poisson(mui).cdf(Hi - 1) for mui, Hi in zip(mu, H)])
        pKm1 = flex.double(
            [poisson(mui).cdf(Ki - 1) for mui, Ki in zip(mu, K)])
        pH = pHm1 + dpH  # = ppois(H,*)
        pK = pKm1 + dpK  # = ppois(K,*)
        E2f = mu * (dpH1 - dpH - dpK1 + dpK) + pKm1 - pHm1
        Epsi = c * (1.0 - pK - pH) + (mu / sV) * (dpH - dpK)
        Epsi2 = c * c * (pH + 1.0 - pK) + E2f
        EpsiS = c * (dpH + dpK) + E2f / sV

        psi = flex.double([huber(rr, c) for rr in residPS])
        cpsi = psi - Epsi
        temp = cpsi * w * sni / sV * dmu_deta
        EEqMat = [0] * len(X)
        for j in range(X.n_rows()):
            for i in range(X.n_columns()):
                k = i + j * X.n_columns()
                EEqMat[k] = X[k] * temp[j]
        EEqMat = matrix.rec(EEqMat, (X.n_rows(), X.n_columns()))
        EEq = []
        for i in range(EEqMat.n_columns()):
            col = []
            for j in range(EEqMat.n_rows()):
                k = i + j * EEqMat.n_columns()
                col.append(EEqMat[k])
            EEq.append(sum(col) / len(col))
        EEq = matrix.col(EEq)
        DiagB = EpsiS / (sni * sV) * w * (ni * dmu_deta)**2
        B = matrix.diag(DiagB)
        H = (X.transpose() * B * X) / len(y)

        dbeta = H.inverse() * EEq
        beta_new = beta + dbeta

        relE = sqrt(
            sum([d * d for d in dbeta]) / max(1e-10, sum([d * d
                                                          for d in beta])))
        beta = beta_new

        # print relE
        if relE < accuracy:
            break

    weights = [min(1, c / abs(r)) for r in residPS]

    eta = flex.double(X * beta)

    mu = flex.exp(eta)
    return beta
示例#32
0
def exercise_gill_murray_wright_cholesky_decomposition():

  def p_as_mx(p):
    n = len(p)
    m = flex.double(flex.grid(n,n))
    m.matrix_diagonal_set_in_place(1)
    for i in xrange(n):
      if p[i] != i:
        m.matrix_swap_rows_in_place(i, p[i])
    return matrix.sqr(m)

  def core(a):
    c = flex.double(a)
    c.resize(flex.grid(a.n))
    u = c.matrix_upper_triangle_as_packed_u()
    gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(
      u,
      epsilon=1.e-8)
    assert gwm.epsilon == 1.e-8
    u = c.matrix_upper_triangle_as_packed_u()
    gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(u)
    assert gwm.epsilon == scitbx.math.floating_point_epsilon_double_get()
    assert gwm.packed_u.id() == u.id()
    p, e = gwm.pivots, gwm.e
    r = matrix.sqr(u.matrix_packed_u_as_upper_triangle())
    if a.n != (0,0):
      rtr = r.transpose() * r
      pm = p_as_mx(p)
      papt = pm * a * pm.transpose()
      paept = papt + matrix.diag(e)
      delta_decomposition = scitbx.linalg.matrix_equality_ratio(paept, rtr)
      assert delta_decomposition < 10, delta_decomposition
      b = flex.random_double(size=a.n[0], factor=2)-1
      x = gwm.solve(b=b)
      px = pm * matrix.col(x)
      pb = pm * matrix.col(b)
      if 0:
        eigen = scitbx.linalg.eigensystem.real_symmetric(
          paept.as_flex_double_matrix())
        lambda_ = eigen.values()
        print "condition number: ", lambda_[0]/lambda_[-1]
      delta_solve = scitbx.linalg.matrix_cholesky_test_ratio(
        a=paept.as_flex_double_matrix(),
        x=flex.double(px),
        b=flex.double(pb),
        epsilon=gwm.epsilon)
      assert delta_solve < 10, delta_solve
    return p, e, r
  # empty matrix
  a = matrix.sqr([])
  p, e, r = core(a)
  assert p.size() == 0
  assert e.size() == 0
  assert len(r) == 0
  n_max = 15
  n_trials_per_n = 10
  # identity matrices
  for n in xrange(1,n_max+1):
    a = matrix.diag([1]*n)
    p, e, r = core(a)
    assert list(p) == range(n)
    assert approx_equal(e, [0]*n)
    assert approx_equal(r, a)
  # null matrices
  for n in xrange(1,n_max+1):
    a = matrix.sqr([0]*n*n)
    p, e, r = core(a)
    assert list(p) == range(n)
    assert list(e) == [scitbx.math.floating_point_epsilon_double_get()]*n
    for i in xrange(n):
      for j in xrange(n):
        if (i != j): r(i,j) == 0
        else: r(i,j) == r(0,0)
  # random semi-positive diagonal matrices
  for n in xrange(1,n_max+1):
    for i_trial in xrange(n_trials_per_n):
      a = matrix.diag(flex.random_double(size=n))
      p, e, r = core(a)
      assert approx_equal(e, [0]*n)
      for i in xrange(n):
        for j in xrange(n):
          if (i != j): approx_equal(r(i,j), 0)
  # random diagonal matrices
  for n in xrange(1,n_max+1):
    for i_trial in xrange(n_trials_per_n):
      a = matrix.diag(flex.random_double(size=n, factor=2)-1)
      p, e, r = core(a)
      for i in xrange(n):
        for j in xrange(n):
          if (i != j): approx_equal(r(i,j), 0)
  # random semi-positive definite matrices
  for n in xrange(1,n_max+1):
    for i_trial in xrange(n_trials_per_n):
      m = matrix.sqr(flex.random_double(size=n*n, factor=2)-1)
      a = m.transpose_multiply()
      p, e, r = core(a)
      assert approx_equal(e, [0]*n)
  # random matrices
  for n in xrange(1,n_max+1):
    size = n*(n+1)//2
    for i_trial in xrange(n_trials_per_n):
      a = (flex.random_double(size=size, factor=2)-1) \
            .matrix_packed_u_as_symmetric()
      core(matrix.sqr(a))
      a.matrix_diagonal_set_in_place(0)
      core(matrix.sqr(a))
  # J. Nocedal and S. Wright:
  # Numerical Optimization.
  # Springer, New York, 1999, pp. 145-150.
  for i in xrange(3):
    for j in xrange(3):
      a = flex.double([[4,2,1],[2,6,3],[1,3,-0.004]])
      a.matrix_swap_rows_in_place(i=i, j=j)
      a.matrix_swap_columns_in_place(i=i, j=j)
      p, e, r = core(matrix.sqr(a))
      if (i == 0 and j == 0):
        assert list(p) == [1,1,2] # swap row 0 and 1 and nothing else
      assert approx_equal(e, [0.0, 0.0, 3.008])
      assert approx_equal(r,
        [2.4494897427831779, 0.81649658092772592, 1.2247448713915889,
         0.0, 1.8257418583505538, 0.0,
         0.0, 0.0, 1.2263767773404712])
示例#33
0
def exercise_cif_from_cctbx():
  quartz = xray.structure(
    crystal_symmetry=crystal.symmetry(
      (5.01,5.01,5.47,90,90,120), "P6222"),
    scatterers=flex.xray_scatterer([
      xray.scatterer("Si", (1/2.,1/2.,1/3.)),
      xray.scatterer("O", (0.197,-0.197,0.83333))]))
  for sc in quartz.scatterers():
    sc.flags.set_grad_site(True)
  s = StringIO()
  loop = geometry.distances_as_cif_loop(
    quartz.pair_asu_table(distance_cutoff=2),
    site_labels=quartz.scatterers().extract_labels(),
    sites_frac=quartz.sites_frac()).loop
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_bond_atom_site_label_1
  _geom_bond_atom_site_label_2
  _geom_bond_distance
  _geom_bond_site_symmetry_2
  Si  O  1.6160  4_554
  Si  O  1.6160  2_554
  Si  O  1.6160  3_664
  Si  O  1.6160  5_664

""")
  s = StringIO()
  loop = geometry.angles_as_cif_loop(
    quartz.pair_asu_table(distance_cutoff=2),
    site_labels=quartz.scatterers().extract_labels(),
    sites_frac=quartz.sites_frac()).loop
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_angle_atom_site_label_1
  _geom_angle_atom_site_label_2
  _geom_angle_atom_site_label_3
  _geom_angle
  _geom_angle_site_symmetry_1
  _geom_angle_site_symmetry_3
  O   Si  O   101.3  2_554  4_554
  O   Si  O   111.3  3_664  4_554
  O   Si  O   116.1  3_664  2_554
  O   Si  O   116.1  5_664  4_554
  O   Si  O   111.3  5_664  2_554
  O   Si  O   101.3  5_664  3_664
  Si  O   Si  146.9  3      5

""")
  # with a covariance matrix
  flex.set_random_seed(1)
  vcv_matrix = matrix.diag(
    flex.random_double(size=quartz.n_parameters(), factor=1e-5))\
             .as_flex_double_matrix().matrix_symmetric_as_packed_u()
  s = StringIO()
  loop = geometry.distances_as_cif_loop(
    quartz.pair_asu_table(distance_cutoff=2),
    site_labels=quartz.scatterers().extract_labels(),
    sites_frac=quartz.sites_frac(),
    covariance_matrix=vcv_matrix,
    parameter_map=quartz.parameter_map()).loop
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_bond_atom_site_label_1
  _geom_bond_atom_site_label_2
  _geom_bond_distance
  _geom_bond_site_symmetry_2
  Si  O  1.616(14)  4_554
  Si  O  1.616(12)  2_554
  Si  O  1.616(14)  3_664
  Si  O  1.616(12)  5_664

""")
  s = StringIO()
  loop = geometry.angles_as_cif_loop(
    quartz.pair_asu_table(distance_cutoff=2),
    site_labels=quartz.scatterers().extract_labels(),
    sites_frac=quartz.sites_frac(),
    covariance_matrix=vcv_matrix,
    parameter_map=quartz.parameter_map()).loop
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_angle_atom_site_label_1
  _geom_angle_atom_site_label_2
  _geom_angle_atom_site_label_3
  _geom_angle
  _geom_angle_site_symmetry_1
  _geom_angle_site_symmetry_3
  O   Si  O    101.3(8)  2_554  4_554
  O   Si  O   111.3(10)  3_664  4_554
  O   Si  O    116.1(9)  3_664  2_554
  O   Si  O    116.1(9)  5_664  4_554
  O   Si  O   111.3(10)  5_664  2_554
  O   Si  O    101.3(8)  5_664  3_664
  Si  O   Si   146.9(9)  3      5

""")
  cell_vcv = flex.pow2(matrix.diag(flex.random_double(size=6,factor=1e-1))\
                       .as_flex_double_matrix().matrix_symmetric_as_packed_u())
  s = StringIO()
  loop = geometry.distances_as_cif_loop(
    quartz.pair_asu_table(distance_cutoff=2),
    site_labels=quartz.scatterers().extract_labels(),
    sites_frac=quartz.sites_frac(),
    covariance_matrix=vcv_matrix,
    cell_covariance_matrix=cell_vcv,
    parameter_map=quartz.parameter_map()).loop
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_bond_atom_site_label_1
  _geom_bond_atom_site_label_2
  _geom_bond_distance
  _geom_bond_site_symmetry_2
  Si  O  1.616(15)  4_554
  Si  O  1.616(19)  2_554
  Si  O  1.616(15)  3_664
  Si  O  1.616(19)  5_664

""")
示例#34
0
def exercise_hbond_as_cif_loop():
  xs = sucrose()
  for sc in xs.scatterers():
    sc.flags.set_grad_site(True)
  radii = [
    covalent_radii.table(elt).radius() for elt in
    xs.scattering_type_registry().type_index_pairs_as_dict() ]
  asu_mappings = xs.asu_mappings(
    buffer_thickness=2*max(radii) + 0.5)
  pair_asu_table = crystal.pair_asu_table(asu_mappings)
  pair_asu_table.add_covalent_pairs(
    xs.scattering_types(),
    tolerance=0.5)
  hbonds = [
    geometry.hbond(1,5, sgtbx.rt_mx('-X,0.5+Y,2-Z')),
    geometry.hbond(5,14, sgtbx.rt_mx('-X,-0.5+Y,1-Z')),
    geometry.hbond(7,10, sgtbx.rt_mx('1+X,+Y,+Z')),
    geometry.hbond(10,0),
    geometry.hbond(12,14, sgtbx.rt_mx('-1-X,0.5+Y,1-Z')),
    geometry.hbond(14,12, sgtbx.rt_mx('-1-X,-0.5+Y,1-Z')),
    geometry.hbond(16,7)
  ]
  loop = geometry.hbonds_as_cif_loop(
    hbonds, pair_asu_table, xs.scatterers().extract_labels(),
    sites_frac=xs.sites_frac()).loop
  s = StringIO()
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.8200  2.0636  2.8635  165.0  2_557
  O4   H4   O9  0.8200  2.0559  2.8736  174.9  2_546
  O5   H5   O7  0.8200  2.0496  2.8589  169.0  1_655
  O7   H7   O1  0.8200  2.0573  2.8617  166.8  .
  O8   H8   O9  0.8200  2.1407  2.8943  152.8  2_456
  O9   H9   O8  0.8200  2.1031  2.8943  162.1  2_446
  O10  H10  O5  0.8200  2.0167  2.7979  159.1  .

""")
  # with a covariance matrix
  flex.set_random_seed(1)
  vcv_matrix = matrix.diag(
    flex.random_double(size=xs.n_parameters(), factor=1e-5))\
             .as_flex_double_matrix().matrix_symmetric_as_packed_u()
  loop = geometry.hbonds_as_cif_loop(
    hbonds, pair_asu_table, xs.scatterers().extract_labels(),
    sites_frac=xs.sites_frac(),
    covariance_matrix=vcv_matrix,
    parameter_map=xs.parameter_map()).loop
  s = StringIO()
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(3)    2.86(3)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)    2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.859(19)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(2)    2.86(2)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(3)    2.89(3)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)    2.89(3)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)    2.80(3)     159(3)  .

""")
  cell_vcv = flex.pow2(matrix.diag(flex.random_double(size=6,factor=1e-1))\
                       .as_flex_double_matrix().matrix_symmetric_as_packed_u())
  loop = geometry.hbonds_as_cif_loop(
    hbonds, pair_asu_table, xs.scatterers().extract_labels(),
    sites_frac=xs.sites_frac(),
    covariance_matrix=vcv_matrix,
    cell_covariance_matrix=cell_vcv,
    parameter_map=xs.parameter_map()).loop
  s = StringIO()
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(4)  2.86(4)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)  2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.86(2)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(3)  2.86(3)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(4)  2.89(4)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)  2.89(4)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)  2.80(3)     159(3)  .

""")
def exercise_gill_murray_wright_cholesky_decomposition():
    def p_as_mx(p):
        n = len(p)
        m = flex.double(flex.grid(n, n))
        m.matrix_diagonal_set_in_place(1)
        for i in xrange(n):
            if p[i] != i:
                m.matrix_swap_rows_in_place(i, p[i])
        return matrix.sqr(m)

    def core(a):
        c = flex.double(a)
        c.resize(flex.grid(a.n))
        u = c.matrix_upper_triangle_as_packed_u()
        gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(
            u, epsilon=1.e-8)
        assert gwm.epsilon == 1.e-8
        u = c.matrix_upper_triangle_as_packed_u()
        gwm = scitbx.linalg.gill_murray_wright_cholesky_decomposition_in_place(
            u)
        assert gwm.epsilon == scitbx.math.floating_point_epsilon_double_get()
        assert gwm.packed_u.id() == u.id()
        p, e = gwm.pivots, gwm.e
        r = matrix.sqr(u.matrix_packed_u_as_upper_triangle())
        if a.n != (0, 0):
            rtr = r.transpose() * r
            pm = p_as_mx(p)
            papt = pm * a * pm.transpose()
            paept = papt + matrix.diag(e)
            delta_decomposition = scitbx.linalg.matrix_equality_ratio(
                paept, rtr)
            assert delta_decomposition < 10, delta_decomposition
            b = flex.random_double(size=a.n[0], factor=2) - 1
            x = gwm.solve(b=b)
            px = pm * matrix.col(x)
            pb = pm * matrix.col(b)
            if 0:
                eigen = scitbx.linalg.eigensystem.real_symmetric(
                    paept.as_flex_double_matrix())
                lambda_ = eigen.values()
                print "condition number: ", lambda_[0] / lambda_[-1]
            delta_solve = scitbx.linalg.matrix_cholesky_test_ratio(
                a=paept.as_flex_double_matrix(),
                x=flex.double(px),
                b=flex.double(pb),
                epsilon=gwm.epsilon)
            assert delta_solve < 10, delta_solve
        return p, e, r

    # empty matrix
    a = matrix.sqr([])
    p, e, r = core(a)
    assert p.size() == 0
    assert e.size() == 0
    assert len(r) == 0
    n_max = 15
    n_trials_per_n = 10
    # identity matrices
    for n in xrange(1, n_max + 1):
        a = matrix.diag([1] * n)
        p, e, r = core(a)
        assert list(p) == range(n)
        assert approx_equal(e, [0] * n)
        assert approx_equal(r, a)
    # null matrices
    for n in xrange(1, n_max + 1):
        a = matrix.sqr([0] * n * n)
        p, e, r = core(a)
        assert list(p) == range(n)
        assert list(e) == [scitbx.math.floating_point_epsilon_double_get()] * n
        for i in xrange(n):
            for j in xrange(n):
                if (i != j): r(i, j) == 0
                else: r(i, j) == r(0, 0)
    # random semi-positive diagonal matrices
    for n in xrange(1, n_max + 1):
        for i_trial in xrange(n_trials_per_n):
            a = matrix.diag(flex.random_double(size=n))
            p, e, r = core(a)
            assert approx_equal(e, [0] * n)
            for i in xrange(n):
                for j in xrange(n):
                    if (i != j): approx_equal(r(i, j), 0)
    # random diagonal matrices
    for n in xrange(1, n_max + 1):
        for i_trial in xrange(n_trials_per_n):
            a = matrix.diag(flex.random_double(size=n, factor=2) - 1)
            p, e, r = core(a)
            for i in xrange(n):
                for j in xrange(n):
                    if (i != j): approx_equal(r(i, j), 0)
    # random semi-positive definite matrices
    for n in xrange(1, n_max + 1):
        for i_trial in xrange(n_trials_per_n):
            m = matrix.sqr(flex.random_double(size=n * n, factor=2) - 1)
            a = m.transpose_multiply()
            p, e, r = core(a)
            assert approx_equal(e, [0] * n)
    # random matrices
    for n in xrange(1, n_max + 1):
        size = n * (n + 1) // 2
        for i_trial in xrange(n_trials_per_n):
            a = (flex.random_double(size=size, factor=2)-1) \
                  .matrix_packed_u_as_symmetric()
            core(matrix.sqr(a))
            a.matrix_diagonal_set_in_place(0)
            core(matrix.sqr(a))
    # J. Nocedal and S. Wright:
    # Numerical Optimization.
    # Springer, New York, 1999, pp. 145-150.
    for i in xrange(3):
        for j in xrange(3):
            a = flex.double([[4, 2, 1], [2, 6, 3], [1, 3, -0.004]])
            a.matrix_swap_rows_in_place(i=i, j=j)
            a.matrix_swap_columns_in_place(i=i, j=j)
            p, e, r = core(matrix.sqr(a))
            if (i == 0 and j == 0):
                assert list(p) == [1, 1,
                                   2]  # swap row 0 and 1 and nothing else
            assert approx_equal(e, [0.0, 0.0, 3.008])
            assert approx_equal(r, [
                2.4494897427831779, 0.81649658092772592, 1.2247448713915889,
                0.0, 1.8257418583505538, 0.0, 0.0, 0.0, 1.2263767773404712
            ])
示例#36
0
def glm(x, p = None):
  from math import exp , sqrt, log, factorial, lgamma
  from scitbx import matrix
  from scipy.stats import poisson
  beta0 = 0

  if p is None:
    p = [1.0] * len(x)

  while True:
    n = beta0
    mu = exp(n)
    z = [n + (xx - mu) / mu for xx in x]
    w = [pp * mu for pp in p]

    W = matrix.diag(w)
    Z = matrix.col(z)
    X = matrix.rec([1]*len(z), (len(z),1))

    beta = (X.transpose() * W * X).inverse() * X.transpose()*W*z

    if abs(beta[0] - beta0) < 1e-3:
      break
    beta0 = beta[0]

  n = beta[0]
  mu = exp(n)
  z = [n + (xx - mu) / mu for xx in x]
  w = [pp * mu for pp in p]

  W = matrix.diag(w)
  Z = matrix.col(z)
  X = matrix.rec([1]*len(z), (len(z),1))
  W12 = matrix.diag([sqrt(ww) for ww in w])
  H = X * (X.transpose()*W*X).inverse() * X.transpose()

  r = [(xx - mu) / mu for xx in x]
  sum1 = 0
  sum2 = 0
  sum3 = 0
  D = []
  for xx in x:
    d = 0
    if xx != 0:
      d += xx * log(xx)
      d -= xx * log(mu)
    d -= (xx - mu)
    D.append(2*d)
  # r = []
  # for xx in x:
  #   if xx == 0:
  #     r.append(0)
  #   else:
  #     r.append(2*(log(xx) - log(mu)))
  # D = []
  # for i in range(len(r)):
  #   h = H[i+i*H.n[0]]
  #   d = (r[i]/(1 - h))**2 * h/1.0
  #   D.append(d)

  # print min(x), max(x)
  print min(D), max(D), F(1, len(x))

  return exp(beta[0]), max(D) > F(1, len(x))