Beispiel #1
0
def exercise(flags, space_group_info):
    # Prepare a structure compatible with the ShelX model
    xs = random_structure.xray_structure(space_group_info,
                                         elements="random",
                                         n_scatterers=10,
                                         use_u_iso=True,
                                         random_u_iso=True,
                                         use_u_aniso=True)
    xs.apply_symmetry_sites()
    xs.apply_symmetry_u_stars()
    for isotropic, sc in itertools.izip(variate(bernoulli_distribution(0.4)),
                                        xs.scatterers()):
        sc.flags.set_grad_site(True)
        if isotropic:
            sc.flags.set_use_u_iso(True)
            sc.flags.set_use_u_aniso(False)
            sc.flags.set_grad_u_iso(True)
        else:
            sc.flags.set_use_u_iso(False)
            sc.flags.set_use_u_aniso(True)
            sc.flags.set_grad_u_aniso(True)

    not_origin_centric = (xs.space_group().is_centric()
                          and not xs.space_group().is_origin_centric())

    try:
        ins = list(
            shelx.writer.generator(xs,
                                   full_matrix_least_squares_cycles=4,
                                   weighting_scheme_params=(0, 0),
                                   sort_scatterers=False))
    except AssertionError:
        if (not_origin_centric):
            print(
                "Omitted %s\n  because it is centric but not origin centric" %
                xs.space_group().type().hall_symbol())
            return
        raise
    else:
        if (not_origin_centric):
            raise Exception_expected

    ins = cStringIO.StringIO("".join(ins))
    xs1 = xs.from_shelx(file=ins)
    xs.crystal_symmetry().is_similar_symmetry(xs1.crystal_symmetry(),
                                              relative_length_tolerance=1e-3,
                                              absolute_angle_tolerance=1e-3)
    uc = xs.unit_cell()
    uc1 = xs1.unit_cell()
    for sc, sc1 in itertools.izip(xs.scatterers(), xs1.scatterers()):
        assert sc.label.upper() == sc1.label.upper()
        assert sc.scattering_type == sc1.scattering_type
        assert sc.flags.bits == sc1.flags.bits
        assert approx_equal(sc.site, sc1.site, eps=1e-6)
        if sc.flags.use_u_iso():
            assert approx_equal(sc.u_iso, sc1.u_iso, eps=1e-5)
        else:
            assert approx_equal(adptbx.u_star_as_u_cif(uc, sc.u_star),
                                adptbx.u_star_as_u_cif(uc1, sc1.u_star),
                                eps=1e-5)
Beispiel #2
0
def exercise_variate_generators():
    from scitbx.random \
         import variate, normal_distribution, bernoulli_distribution, \
                gamma_distribution, poisson_distribution
    for i in range(10):
        scitbx.random.set_random_seed(0)
        g = variate(normal_distribution())
        assert approx_equal(g(), -0.917787219374)
        assert approx_equal(
            g(10),
            (1.21838707856, 1.732426915, 0.838038157555, -0.296895169923,
             0.246451144946, -0.635474652255, -0.0980626986425, 0.36458295417,
             0.534073780268, -0.665073136294))

    stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
    assert approx_equal(stat.mean, 0, eps=0.005)
    assert approx_equal(stat.biased_variance, 1, eps=0.005)
    assert approx_equal(stat.skew, 0, eps=0.005)
    assert approx_equal(stat.kurtosis, 3, eps=0.005)

    bernoulli_seq = variate(bernoulli_distribution(0.1))
    for b in itertools.islice(bernoulli_seq, 10):
        assert b in (True, False)
    bernoulli_sample = flex.bool(itertools.islice(bernoulli_seq, 10000))
    assert approx_equal(bernoulli_sample.count(True) / len(bernoulli_sample),
                        0.1,
                        eps=0.01)

    # Boost 1.64 changes the exponential distribution to use Ziggurat algorithm
    scitbx.random.set_random_seed(0)
    g = variate(gamma_distribution())
    if (boost_version < 106400):
        assert approx_equal(g(), 0.79587450456577546)
        assert approx_equal(g(2), (0.89856038848394115, 1.2559307580473893))
    else:
        assert approx_equal(g(), 0.864758191783)
        assert approx_equal(g(2), (1.36660841837, 2.26740986094))
    stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
    assert approx_equal(stat.mean, 1, eps=0.005)
    assert approx_equal(stat.skew, 2, eps=0.01)
    assert approx_equal(stat.biased_variance, 1, eps=0.005)
    scitbx.random.set_random_seed(0)
    g = variate(gamma_distribution(alpha=2, beta=3))
    assert approx_equal(g(), 16.670850592722729)
    assert approx_equal(g(2), (10.03662877519449, 3.9357158398972873))
    stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
    assert approx_equal(stat.mean, 6, eps=0.005)
    assert approx_equal(stat.skew, 2 / math.sqrt(2), eps=0.05)
    assert approx_equal(stat.biased_variance, 18, eps=0.05)

    mean = 10.0
    pv = variate(poisson_distribution(mean))
    draws = pv(1000000).as_double()
    m = flex.mean(draws)
    v = flex.mean(draws * draws) - m * m
    assert approx_equal(m, mean, eps=0.05)
    assert approx_equal(v, mean, eps=0.05)
Beispiel #3
0
def exercise_variate_generators():
  from scitbx.random \
       import variate, normal_distribution, bernoulli_distribution, \
              gamma_distribution, poisson_distribution
  for i in xrange(10):
    scitbx.random.set_random_seed(0)
    g = variate(normal_distribution())
    assert approx_equal(g(), -1.2780081289048213)
    assert approx_equal(g(10),
      (-0.40474189234755492, -0.41845505596083288,
       -1.8825790263067721, -1.5779112018107659,
       -1.1888174422378859, -1.8619619179878537,
       -0.53946818661388318, -1.2400941724410812,
       0.64511959841907285, -0.59934120033270688))

  stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
  assert approx_equal(stat.mean,            0, eps=0.005)
  assert approx_equal(stat.biased_variance, 1, eps=0.005)
  assert approx_equal(stat.skew,            0, eps=0.005)
  assert approx_equal(stat.kurtosis,        3, eps=0.005)

  bernoulli_seq = variate(bernoulli_distribution(0.1))
  for b in itertools.islice(bernoulli_seq, 10):
    assert b in (True, False)
  bernoulli_sample = flex.bool(itertools.islice(bernoulli_seq, 10000))
  assert approx_equal(
    bernoulli_sample.count(True)/len(bernoulli_sample),
    0.1,
    eps = 0.01)

  scitbx.random.set_random_seed(0)
  g = variate(gamma_distribution())
  assert approx_equal(g(), 0.79587450456577546)
  assert approx_equal(g(2), (0.89856038848394115, 1.2559307580473893))
  stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
  assert approx_equal(stat.mean,            1, eps=0.005)
  assert approx_equal(stat.skew,            2, eps=0.005)
  assert approx_equal(stat.biased_variance, 1, eps=0.005)
  scitbx.random.set_random_seed(0)
  g = variate(gamma_distribution(alpha=2, beta=3))
  assert approx_equal(g(), 16.670850592722729)
  assert approx_equal(g(2), (10.03662877519449, 3.9357158398972873))
  stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
  assert approx_equal(stat.mean,            6, eps=0.005)
  assert approx_equal(stat.skew,            2/math.sqrt(2), eps=0.05)
  assert approx_equal(stat.biased_variance, 18, eps=0.05)

  mean = 10.0
  pv = variate(poisson_distribution(mean))
  draws = pv(1000000).as_double()
  m = flex.mean(draws)
  v = flex.mean(draws*draws) - m*m
  assert approx_equal(m,mean,eps=0.05)
  assert approx_equal(v,mean,eps=0.05)
Beispiel #4
0
def exercise(flags, space_group_info):
    # Prepare a structure compatible with the ShelX model
    xs = random_structure.xray_structure(
        space_group_info, elements="random", n_scatterers=10, use_u_iso=True, random_u_iso=True, use_u_aniso=True
    )
    xs.apply_symmetry_sites()
    xs.apply_symmetry_u_stars()
    for isotropic, sc in itertools.izip(variate(bernoulli_distribution(0.4)), xs.scatterers()):
        sc.flags.set_grad_site(True)
        if isotropic:
            sc.flags.set_use_u_iso(True)
            sc.flags.set_use_u_aniso(False)
            sc.flags.set_grad_u_iso(True)
        else:
            sc.flags.set_use_u_iso(False)
            sc.flags.set_use_u_aniso(True)
            sc.flags.set_grad_u_aniso(True)

    not_origin_centric = xs.space_group().is_centric() and not xs.space_group().is_origin_centric()

    try:
        ins = list(
            shelx.writer.generator(
                xs, full_matrix_least_squares_cycles=4, weighting_scheme_params=(0, 0), sort_scatterers=False
            )
        )
    except AssertionError:
        if not_origin_centric:
            print("Omitted %s\n  because it is centric but not origin centric" % xs.space_group().type().hall_symbol())
            return
        raise
    else:
        if not_origin_centric:
            raise Exception_expected

    ins = cStringIO.StringIO("".join(ins))
    xs1 = xs.from_shelx(file=ins)
    xs.crystal_symmetry().is_similar_symmetry(
        xs1.crystal_symmetry(), relative_length_tolerance=1e-3, absolute_angle_tolerance=1e-3
    )
    uc = xs.unit_cell()
    uc1 = xs1.unit_cell()
    for sc, sc1 in itertools.izip(xs.scatterers(), xs1.scatterers()):
        assert sc.label.upper() == sc1.label.upper()
        assert sc.scattering_type == sc1.scattering_type
        assert sc.flags.bits == sc1.flags.bits
        assert approx_equal(sc.site, sc1.site, eps=1e-6)
        if sc.flags.use_u_iso():
            assert approx_equal(sc.u_iso, sc1.u_iso, eps=1e-5)
        else:
            assert approx_equal(
                adptbx.u_star_as_u_cif(uc, sc.u_star), adptbx.u_star_as_u_cif(uc1, sc1.u_star), eps=1e-5
            )
Beispiel #5
0
def exercise_variate_generators():
    from scitbx.random \
         import variate, normal_distribution, bernoulli_distribution, \
                gamma_distribution, poisson_distribution
    for i in xrange(10):
        scitbx.random.set_random_seed(0)
        g = variate(normal_distribution())
        assert approx_equal(g(), -1.2780081289048213)
        assert approx_equal(
            g(10),
            (-0.40474189234755492, -0.41845505596083288, -1.8825790263067721,
             -1.5779112018107659, -1.1888174422378859, -1.8619619179878537,
             -0.53946818661388318, -1.2400941724410812, 0.64511959841907285,
             -0.59934120033270688))

    stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
    assert approx_equal(stat.mean, 0, eps=0.005)
    assert approx_equal(stat.biased_variance, 1, eps=0.005)
    assert approx_equal(stat.skew, 0, eps=0.005)
    assert approx_equal(stat.kurtosis, 3, eps=0.005)

    bernoulli_seq = variate(bernoulli_distribution(0.1))
    for b in itertools.islice(bernoulli_seq, 10):
        assert b in (True, False)
    bernoulli_sample = flex.bool(itertools.islice(bernoulli_seq, 10000))
    assert approx_equal(bernoulli_sample.count(True) / len(bernoulli_sample),
                        0.1,
                        eps=0.01)

    scitbx.random.set_random_seed(0)
    g = variate(gamma_distribution())
    assert approx_equal(g(), 0.79587450456577546)
    assert approx_equal(g(2), (0.89856038848394115, 1.2559307580473893))
    stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
    assert approx_equal(stat.mean, 1, eps=0.005)
    assert approx_equal(stat.skew, 2, eps=0.005)
    assert approx_equal(stat.biased_variance, 1, eps=0.005)
    scitbx.random.set_random_seed(0)
    g = variate(gamma_distribution(alpha=2, beta=3))
    assert approx_equal(g(), 16.670850592722729)
    assert approx_equal(g(2), (10.03662877519449, 3.9357158398972873))
    stat = basic_statistics(flex.double(itertools.islice(g, 1000000)))
    assert approx_equal(stat.mean, 6, eps=0.005)
    assert approx_equal(stat.skew, 2 / math.sqrt(2), eps=0.05)
    assert approx_equal(stat.biased_variance, 18, eps=0.05)

    mean = 10.0
    pv = variate(poisson_distribution(mean))
    draws = pv(1000000).as_double()
    m = flex.mean(draws)
    v = flex.mean(draws * draws) - m * m
    assert approx_equal(m, mean, eps=0.05)
    assert approx_equal(v, mean, eps=0.05)