Exemple #1
0
def exercise_fftlib_real_complex_3d_real_imag_w(verbose):
    mt = flex.mersenne_twister(seed=0)
    for nu in xrange(1, 9):
        for nv in xrange(1, 9):
            for nw in xrange(2, 9, 2):
                exercise_fftlib_real_complex_3d_real_imag_w_given_dims(
                    mt, nu, nv, nw, verbose)
def compare_times(max_n_power=8):
  from scitbx.linalg import lapack_dsyev
  mt = flex.mersenne_twister(seed=0)
  show_tab_header = True
  tfmt = "%5.2f"
  for n_power in xrange(5,max_n_power+1):
    n = 2**n_power
    l = mt.random_double(size=n*(n+1)//2) * 2 - 1
    a = l.matrix_packed_l_as_symmetric()
    aes = a.deep_copy()
    ala = [a.deep_copy(), a.deep_copy()]
    wla = [flex.double(n, -1e100), flex.double(n, -1e100)]
    t0 = time.time()
    es = eigensystem.real_symmetric(aes)
    tab = [n, tfmt % (time.time() - t0)]
    for i,use_fortran in enumerate([False, True]):
      t0 = time.time()
      info = lapack_dsyev(
        jobz="V", uplo="U", a=ala[i], w=wla[i], use_fortran=use_fortran)
      if (info == 99):
        tab.append(" --- ")
      else:
        assert info == 0
        tab.append(tfmt % (time.time() - t0))
        assert approx_equal(list(reversed(es.values())), wla[i])
    if (show_tab_header):
      print "      time [s]           eigenvalues"
      print " n    es   fem   for     min    max"
      show_tab_header = False
    tab.extend([es.values()[-1], es.values()[0]])
    print "%3d %s %s %s [%6.2f %6.2f]" % tuple(tab)
Exemple #3
0
    def Umats(mos_spread_deg,
              n_mos_doms,
              isotropic=True,
              seed=777,
              norm_dist_seed=777):
        import scitbx
        from scitbx.matrix import col
        import math
        UMAT_nm = flex.mat3_double()
        mersenne_twister = flex.mersenne_twister(seed=seed)
        scitbx.random.set_random_seed(norm_dist_seed)
        rand_norm = scitbx.random.normal_distribution(mean=0,
                                                      sigma=(mos_spread_deg *
                                                             math.pi / 180.0))
        g = scitbx.random.variate(rand_norm)
        mosaic_rotation = g(n_mos_doms)
        for m in mosaic_rotation:
            site = col(mersenne_twister.random_double_point_on_sphere())
            if mos_spread_deg > 0:
                UMAT_nm.append(
                    site.axis_and_angle_as_r3_rotation_matrix(m, deg=False))
            else:
                UMAT_nm.append(
                    site.axis_and_angle_as_r3_rotation_matrix(0, deg=False))
            if isotropic and mos_spread_deg > 0:
                UMAT_nm.append(
                    site.axis_and_angle_as_r3_rotation_matrix((-m), deg=False))

        return UMAT_nm
Exemple #4
0
def compare_fftpack_with_hermft_1d():
    mt = flex.mersenne_twister(seed=0)
    for n_cmpl in xrange(1, 101):
        primes = prime_factors_of(n_cmpl)
        if (n_cmpl != 1 and max(primes) > 19): continue
        n_real = n_cmpl * 2
        m_real = n_real + 2
        z = (mt.random_double(size=m_real) * 2 - 1).as_float()
        # The imaginary parts of the first and last complex values should
        # be zero, but z has random values in these places, to prove that
        # they don't change the result.
        hermft_xy = z.deep_copy()
        hermft_xy[1] = hermft_xy[-2]
        # real part of last complex value stored in imaginary part of first
        # (see ccp4/doc/libfft.doc)
        hermft_xy[-2] = 253
        # random value, for consistency check below
        d = flex.int((m_real, 2, m_real, m_real, m_real))
        ccp4io_dev_ext.fftlib_hermft(xy=hermft_xy, n=n_cmpl, d=d)
        # consistency check first
        assert hermft_xy[-2] == 253
        assert hermft_xy[-1] == z[-1]
        # reset to zero for comparision with fftpack result further down
        hermft_xy[-2] = 0
        hermft_xy[-1] = 0
        fft = scitbx.fftpack.real_to_complex(n_real)
        assert fft.m_real() == m_real
        fftpack_xy = z.as_double()
        for i in xrange(n_cmpl):
            fftpack_xy[i * 2 + 1] *= -1  # conjugate
        fft.backward(fftpack_xy)
        fftpack_xy = fftpack_xy.as_float()
        if (flex.max_absolute(hermft_xy - fftpack_xy) > 1e-5):
            assert approx_equal(hermft_xy, fftpack_xy, eps=1e-5)
Exemple #5
0
def exercise_fftlib_real_complex_3d_real_imag_w(verbose):
  mt = flex.mersenne_twister(seed=0)
  for nu in xrange(1,9):
    for nv in xrange(1,9):
      for nw in xrange(2,9,2):
        exercise_fftlib_real_complex_3d_real_imag_w_given_dims(
          mt, nu, nv, nw, verbose)
Exemple #6
0
def exercise_read_write_defaults(use_mrcfile=True):
  if not use_mrcfile: return

  from cctbx import uctbx, sgtbx, crystal
  from scitbx.array_family import flex
  mt = flex.mersenne_twister(0)
  nxyz = (4,5,6,)
  grid = flex.grid(nxyz)
  real_map_data = mt.random_double(size=grid.size_1d())
  real_map_data.reshape(grid)
  unit_cell=uctbx.unit_cell((10,10,10,90,90,90))
  space_group=sgtbx.space_group_info("P1").group()
  crystal_symmetry=crystal.symmetry(unit_cell=unit_cell,space_group=space_group)
  iotbx.mrcfile.write_ccp4_map(
    file_name="simple.mrc",
    crystal_symmetry=crystal_symmetry,
    map_data=real_map_data)
  input_real_map = iotbx.mrcfile.map_reader(file_name="simple.mrc")

  # check unit cell and space group:
  cs=input_real_map.crystal_symmetry()
  assert cs.is_similar_symmetry(crystal_symmetry)

  # Check writing map with offset
  origin_shift=(5,6,7)
  iotbx.mrcfile.write_ccp4_map(
    file_name="offset.mrc",
    crystal_symmetry=crystal_symmetry,
    origin_shift_grid_units=origin_shift,
    map_data=real_map_data)
  input_real_map = iotbx.mrcfile.map_reader(file_name="offset.mrc")
  map_data=input_real_map.map_data()
  assert map_data.origin()==origin_shift
Exemple #7
0
def check_refine_uc_cr(work_params, image_mdls,
      unit_cell_perturbation_factor=2,
      crystal_rotation_perturbation_angle=10):
  from cctbx import uctbx
  from scitbx.array_family import flex
  from scitbx import matrix
  for i_img,im in enumerate(image_mdls.array):
    print "Image number:", i_img
    mt = flex.mersenne_twister(seed=work_params.noise.random_seed+i_img)
    unit_cell = uctbx.unit_cell([
      v + unit_cell_perturbation_factor*(mt.random_double()-0.5)
        for v in im.unit_cell.parameters()])
    crystal_rotation = matrix.sqr(im.crystal_rotation) \
      * matrix.col(mt.random_double_point_on_sphere()) \
          .axis_and_angle_as_r3_rotation_matrix(
            angle=crystal_rotation_perturbation_angle, deg=True)
    refined = refine_uc_cr.refinery(
      work_params=work_params,
      spots_xy0=im.spot_positions,
      miller_indices=image_mdls.miller_indices.select(im.miller_index_i_seqs),
      unit_cell=unit_cell,
      crystal_rotation_uq=crystal_rotation
        .r3_rotation_matrix_as_unit_quaternion())
    refined.show_summary().show_distances()
    print
Exemple #8
0
def compare_fftpack_with_hermft_1d():
  mt = flex.mersenne_twister(seed=0)
  for n_cmpl in xrange(1, 101):
    primes = prime_factors_of(n_cmpl)
    if (n_cmpl != 1 and max(primes) > 19): continue
    n_real = n_cmpl * 2
    m_real = n_real + 2
    z = (mt.random_double(size=m_real)*2-1).as_float()
      # The imaginary parts of the first and last complex values should
      # be zero, but z has random values in these places, to prove that
      # they don't change the result.
    hermft_xy = z.deep_copy()
    hermft_xy[1] = hermft_xy[-2]
      # real part of last complex value stored in imaginary part of first
      # (see ccp4/doc/libfft.doc)
    hermft_xy[-2] = 253
      # random value, for consistency check below
    d = flex.int((m_real,2,m_real,m_real,m_real))
    ccp4io_dev_ext.fftlib_hermft(xy=hermft_xy, n=n_cmpl, d=d)
    # consistency check first
    assert hermft_xy[-2] == 253
    assert hermft_xy[-1] == z[-1]
    # reset to zero for comparision with fftpack result further down
    hermft_xy[-2] = 0
    hermft_xy[-1] = 0
    fft = scitbx.fftpack.real_to_complex(n_real)
    assert fft.m_real() == m_real
    fftpack_xy = z.as_double()
    for i in xrange(n_cmpl): fftpack_xy[i*2+1] *= -1 # conjugate
    fft.backward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float()
    if (flex.max_absolute(hermft_xy-fftpack_xy) > 1e-5):
      assert approx_equal(hermft_xy, fftpack_xy, eps=1e-5)
Exemple #9
0
def get_unit_points_on_hemisphere():
    mersenne_twister = flex.mersenne_twister(seed=0)
    sphere_points = []
    for m in xrange(20000):
        sphere_pt = col(mersenne_twister.random_double_point_on_sphere())
        sphere_points.append(sphere_pt)
    return sphere_points
def simulation_zigzag(NB=5):
    mersenne_twister = flex.mersenne_twister(seed=0)
    body = six_dof_body(labels=["00", "01", "02"],
                        sites=matrix.col_list([(0.3, -0.5, 0), (0.4, 0.5, 0),
                                               (0, 0, 0)]),
                        bonds=[(0, 2), (1, 2)],
                        mersenne_twister=mersenne_twister)
    body.parent = -1
    bodies = [body]
    vu = matrix.col((0, 1, 0)).rotate_around_origin(axis=matrix.col((1, 0, 0)),
                                                    angle=75,
                                                    deg=True)
    vr = matrix.col((0, 1, 0))
    v = vu
    pivot = matrix.col((0, 0, 0))
    for ib in xrange(1, NB):
        body = revolute_body(labels=[str(ib)],
                             sites=[pivot + v * 0.5],
                             bonds=[(-1, 0)],
                             pivot=pivot,
                             normal=matrix.col((1, 0, 0)),
                             mersenne_twister=mersenne_twister)
        body.parent = ib - 1
        bodies.append(body)
        pivot += v
        if (v is vu): v = vr
        else: v = vu
    return simulation(bodies=bodies)
def simulation_gly_with_nh():
    pdb = """\
ATOM      0  N   GLY A   1      10.949  12.815  15.189  0.00  0.00           N
ATOM      1  CA  GLY A   1      10.405  13.954  15.917  0.00  0.00           C
ATOM      2  C   GLY A   1      10.779  15.262  15.227  0.00  0.00           C
ATOM      3  O   GLY A   1       9.916  16.090  14.936  0.00  0.00           O
ATOM      4  H   GLY A   1      11.792  12.691  15.311  0.00  0.00           H
"""
    labels, sites = pdb_extract(pdb=pdb)
    mersenne_twister = flex.mersenne_twister(seed=0)
    body0 = six_dof_body(labels=labels[:3],
                         sites=sites[:3],
                         bonds=[(0, 1), (1, 2)],
                         mersenne_twister=mersenne_twister)
    body0.parent = -1
    body1 = revolute_body(labels=labels[3:4],
                          sites=sites[3:4],
                          bonds=[(-1, 0)],
                          pivot=sites[2],
                          normal=(sites[2] - sites[1]).normalize(),
                          mersenne_twister=mersenne_twister)
    body1.parent = 0
    body2 = revolute_body(labels=labels[4:],
                          sites=sites[4:],
                          bonds=[(-3, 0)],
                          pivot=sites[0],
                          normal=(sites[0] - sites[1]).normalize(),
                          mersenne_twister=mersenne_twister)
    body2.parent = 0
    return simulation(bodies=[body0, body1, body2])
def ersatz_all_orientations(N_total=100000):
    ori_N_total = N_total  # number of items to simulate
    mt = flex.mersenne_twister(seed=0)
    random_orientations = []
    for iteration in range(ori_N_total):
        random_orientations.append(mt.random_double_r3_rotation_matrix())
    return random_orientations
Exemple #13
0
def exercise_real_to_complex_padding_area():
    mt = flex.mersenne_twister(seed=1)
    for n_real in range(1, 101):
        fft = fftpack.real_to_complex(n_real)
        assert fft.n_real() == n_real
        assert fft.n_complex() == n_real // 2 + 1
        assert fft.m_real() == fft.n_complex() * 2
        m_real = fft.m_real()
        z = mt.random_double(
            size=m_real) * 2 - 1  # non-zero values in padded area
        c = z.deep_copy()
        fft.forward(c)
        if (n_real % 2 == 0):
            assert approx_equal(c[-1],
                                0)  # imaginary part of last complex value
        r = c.deep_copy()
        fft.backward(r)
        r *= (1 / n_real)
        assert approx_equal(r[:n_real], z[:n_real])
        if (n_real % 2 == 0):
            assert approx_equal(r[n_real:], [0, 0])
            q = c.deep_copy()
            q[-1] = 123  # random imaginary part (which should be zero)
            fft.backward(q)
            q *= (1 / n_real)
            assert approx_equal(q, r)  # obtain zeros in padded area anyway
def check_refine_uc_cr(work_params, image_mdls,
      unit_cell_perturbation_factor=2,
      crystal_rotation_perturbation_angle=10):
  from cctbx import uctbx
  from scitbx.array_family import flex
  from scitbx import matrix
  for i_img,im in enumerate(image_mdls.array):
    print "Image number:", i_img
    mt = flex.mersenne_twister(seed=work_params.noise.random_seed+i_img)
    unit_cell = uctbx.unit_cell([
      v + unit_cell_perturbation_factor*(mt.random_double()-0.5)
        for v in im.unit_cell.parameters()])
    crystal_rotation = matrix.sqr(im.crystal_rotation) \
      * matrix.col(mt.random_double_point_on_sphere()) \
          .axis_and_angle_as_r3_rotation_matrix(
            angle=crystal_rotation_perturbation_angle, deg=True)
    refined = refine_uc_cr.refinery(
      work_params=work_params,
      spots_xy0=im.spot_positions,
      miller_indices=image_mdls.miller_indices.select(im.miller_index_i_seqs),
      unit_cell=unit_cell,
      crystal_rotation_uq=crystal_rotation
        .r3_rotation_matrix_as_unit_quaternion())
    refined.show_summary().show_distances()
    print
Exemple #15
0
def exercise_joint_lib_six_dof_aja_simplified():
  tc = test_cases_tardy_pdb.test_cases[9]
  tt = tc.tardy_tree_construct()
  masses = [1.0]*len(tc.sites)
  # arbitrary transformation so that the center of mass is not at the origin
  rt_arbitrary = matrix.col((-0.21,-0.51,0.64)) \
    .rt_for_rotation_around_axis_through(
      point=matrix.col((-0.80, 0.28, -0.89)), angle=-37, deg=True)
  sites = [rt_arbitrary * site for site in tc.sites]
  tm = scitbx.rigid_body.essence.tardy.model(
    labels=tc.labels,
    sites=sites,
    masses=masses,
    tardy_tree=tt,
    potential_obj=None)
  assert len(tm.bodies) == 1
  assert tm.q_packed_size == 7
  mt = flex.mersenne_twister(seed=0)
  for i_trial in xrange(3):
    q = mt.random_double(size=tm.q_packed_size)*2-1
    tm.unpack_q(q_packed=q)
    sm = tm.sites_moved()
    aja = matrix.rt(scitbx.rigid_body.joint_lib_six_dof_aja_simplified(
      center_of_mass=tuple(flex.vec3_double(sites).mean()),
      q=q))
    sm2 = [aja * site for site in sites]
    assert approx_equal(sm2, sm)
Exemple #16
0
def exercise_joint_lib_six_dof_aja_simplified():
    tc = test_cases_tardy_pdb.test_cases[9]
    tt = tc.tardy_tree_construct()
    masses = [1.0] * len(tc.sites)
    # arbitrary transformation so that the center of mass is not at the origin
    rt_arbitrary = matrix.col((-0.21,-0.51,0.64)) \
      .rt_for_rotation_around_axis_through(
        point=matrix.col((-0.80, 0.28, -0.89)), angle=-37, deg=True)
    sites = [rt_arbitrary * site for site in tc.sites]
    tm = scitbx.rigid_body.essence.tardy.model(labels=tc.labels,
                                               sites=sites,
                                               masses=masses,
                                               tardy_tree=tt,
                                               potential_obj=None)
    assert len(tm.bodies) == 1
    assert tm.q_packed_size == 7
    mt = flex.mersenne_twister(seed=0)
    for i_trial in xrange(3):
        q = mt.random_double(size=tm.q_packed_size) * 2 - 1
        tm.unpack_q(q_packed=q)
        sm = tm.sites_moved()
        aja = matrix.rt(
            scitbx.rigid_body.joint_lib_six_dof_aja_simplified(
                center_of_mass=tuple(flex.vec3_double(sites).mean()), q=q))
        sm2 = [aja * site for site in sites]
        assert approx_equal(sm2, sm)
Exemple #17
0
def get_partiality_response(key, one_index, spectra_simulation, ROI):

    N_total = 100000  # number of items to simulate
    spectra = spectra_simulation
    crystal = microcrystal(
        Deff_A=4000, length_um=4.,
        beam_diameter_um=1.0)  # assume smaller than 10 um crystals
    mt = flex.mersenne_twister(seed=0)
    random_orientations = []
    for iteration in range(N_total):
        random_orientations.append(mt.random_double_r3_rotation_matrix())

    iterator = spectra.generate_recast_renormalized_image(image=key,
                                                          energy=7120.,
                                                          total_flux=1e12)

    file_prefix = "key_slow_nonoise_%06d" % key
    rand_ori = sqr(random_orientations[key])

    pixels = run_sim2smv(ROI,
                         prefix=file_prefix,
                         crystal=crystal,
                         spectra=iterator,
                         rotation=rand_ori,
                         quick=False,
                         rank=0)
    return pixels
def channel_wavelength_fmodel(create):
  N_mosaic_domains = 25
  mosaic_spread_deg = 0.05 # interpreted by UMAT_nm as a half-width stddev

  UMAT_nm = flex.mat3_double()
  mersenne_twister = flex.mersenne_twister(seed=0)
  scitbx.random.set_random_seed(1234)
  rand_norm = scitbx.random.normal_distribution(mean=0, sigma=mosaic_spread_deg * math.pi/180.)
  g = scitbx.random.variate(rand_norm)
  mosaic_rotation = g(N_mosaic_domains)
  for m in mosaic_rotation:
    site = col(mersenne_twister.random_double_point_on_sphere())
    UMAT_nm.append( site.axis_and_angle_as_r3_rotation_matrix(m,deg=False) )

  if create: # write the reference for the first time
      cPickle.dump(UMAT_nm,
      open(os.path.join(ls49_big_data,"reference",filename),"wb"),cPickle.HIGHEST_PROTOCOL)
  else: # read the reference and assert sameness to production run
      import six
      if six.PY3:
        UMAT_ref = cPickle.load(open(os.path.join(ls49_big_data,"reference",filename),"rb"),encoding="bytes")
      else:
        UMAT_ref = cPickle.load(open(os.path.join(ls49_big_data,"reference",filename),"rb"))
      for x in range(len(UMAT_nm)):
        print(x," ".join(
          ["%18.15f"%UMAT_ref[x][z] for z in range(9)]
        ))
        assert UMAT_nm[x] == UMAT_ref[x]
  expected_output =  """
def simulation_gly_with_nh():
  pdb = """\
ATOM      0  N   GLY A   1      10.949  12.815  15.189  0.00  0.00           N
ATOM      1  CA  GLY A   1      10.405  13.954  15.917  0.00  0.00           C
ATOM      2  C   GLY A   1      10.779  15.262  15.227  0.00  0.00           C
ATOM      3  O   GLY A   1       9.916  16.090  14.936  0.00  0.00           O
ATOM      4  H   GLY A   1      11.792  12.691  15.311  0.00  0.00           H
"""
  labels, sites = pdb_extract(pdb=pdb)
  mersenne_twister = flex.mersenne_twister(seed=0)
  body0 = six_dof_body(
    labels=labels[:3],
    sites=sites[:3],
    bonds=[(0,1),(1,2)],
    mersenne_twister=mersenne_twister)
  body0.parent = -1
  body1 = revolute_body(
    labels=labels[3:4],
    sites=sites[3:4],
    bonds=[(-1,0)],
    pivot=sites[2],
    normal=(sites[2]-sites[1]).normalize(),
    mersenne_twister=mersenne_twister)
  body1.parent = 0
  body2 = revolute_body(
    labels=labels[4:],
    sites=sites[4:],
    bonds=[(-3,0)],
    pivot=sites[0],
    normal=(sites[0]-sites[1]).normalize(),
    mersenne_twister=mersenne_twister)
  body2.parent = 0
  return simulation(bodies=[body0, body1, body2])
def simulation_zigzag(NB=5):
  mersenne_twister = flex.mersenne_twister(seed=0)
  body = six_dof_body(
    labels=["00", "01", "02"],
    sites=matrix.col_list([
      (0.3,-0.5,0),
      (0.4,0.5,0),
      (0,0,0)]),
    bonds=[(0,2),(1,2)],
    mersenne_twister=mersenne_twister)
  body.parent = -1
  bodies = [body]
  vu = matrix.col((0,1,0)).rotate_around_origin(
    axis=matrix.col((1,0,0)), angle=75, deg=True)
  vr = matrix.col((0,1,0))
  v = vu
  pivot = matrix.col((0,0,0))
  for ib in xrange(1,NB):
    body = revolute_body(
      labels=[str(ib)],
      sites=[pivot + v*0.5],
      bonds=[(-1,0)],
      pivot=pivot,
      normal=matrix.col((1,0,0)),
      mersenne_twister=mersenne_twister)
    body.parent = ib-1
    bodies.append(body)
    pivot += v
    if (v is vu): v = vr
    else:         v = vu
  return simulation(bodies=bodies)
def simulation_ala_no_h():
  pdb = """\
ATOM      0  N   ALA A   1      10.949  12.815  15.189  0.00  0.00           N
ATOM      1  CA  ALA A   1      10.405  13.954  15.917  0.00  0.00           C
ATOM      2  C   ALA A   1      10.779  15.262  15.227  0.00  0.00           C
ATOM      3  CB  ALA A   1      10.908  13.950  17.351  0.00  0.00           C
ATOM      4  O   ALA A   1       9.916  16.090  14.936  0.00  0.00           O
"""
  labels, sites = pdb_extract(pdb=pdb)
  mersenne_twister = flex.mersenne_twister(seed=0)
  body0 = six_dof_body(
    labels=labels[:4],
    sites=sites[:4],
    bonds=[(0,1),(1,2),(1,3)],
    mersenne_twister=mersenne_twister)
  body0.parent = -1
  body1 = revolute_body(
    labels=labels[4:],
    sites=sites[4:],
    bonds=[(-2,0)],
    pivot=sites[2],
    normal=(sites[2]-sites[1]).normalize(),
    mersenne_twister=mersenne_twister)
  body1.parent = 0
  return simulation(bodies=[body0, body1])
Exemple #22
0
def tst_all(quick=False, prefix="step6"):
    from LS49.spectra.generate_spectra import spectra_simulation
    SS = spectra_simulation()
    iterator = SS.generate_recast_renormalized_images(20,
                                                      energy=7120.,
                                                      total_flux=1e12)

    #
    C = microcrystal(
        Deff_A=4000, length_um=4.,
        beam_diameter_um=1.0)  # assume smaller than 10 um crystals
    mt = flex.mersenne_twister(seed=0)

    if quick: prefix_root = prefix + "_%06d"
    else: prefix_root = prefix + "poly_%06d"

    Nimages = 1  # 10000
    for iteration in range(Nimages):
        file_prefix = prefix_root % iteration
        rand_ori = sqr(mt.random_double_r3_rotation_matrix())
        run_sim2smv(prefix=file_prefix,
                    crystal=C,
                    spectra=iterator,
                    rotation=rand_ori,
                    quick=quick,
                    rank=0)
Exemple #23
0
def run_uniform(eta_angle, sample_size=20000, verbose=True):

    UMAT = flex.mat3_double()
    d_UMAT_d_eta = flex.mat3_double()
    # the axis is sampled randomly on a sphere.
    # Alternately it could have been calculated on a regular hemispheric grid
    # but regular grid is not needed; the only goal is to have the ability to compute gradient
    mersenne_twister = flex.mersenne_twister(
        seed=0)  # set seed, get reproducible results

    # for each axis, sample both the + and - angle
    assert sample_size % 2 == 0
    # the angle is sampled uniformly from its distribution

    mosaic_rotation0 = np.array(range(sample_size // 2))
    mosaic_rotation1 = special.erfinv(mosaic_rotation0 / (sample_size // 2))
    d_theta_d_eta = math.sqrt(2.0) * flex.double(mosaic_rotation1)
    mosaic_rotation = (math.pi / 180.) * eta_angle * d_theta_d_eta

    for m, d in zip(mosaic_rotation, d_theta_d_eta):
        site = col(mersenne_twister.random_double_point_on_sphere())
        UMAT.append(site.axis_and_angle_as_r3_rotation_matrix(m, deg=False))
        UMAT.append(site.axis_and_angle_as_r3_rotation_matrix(-m, deg=False))
        d_umat = site.axis_and_angle_as_r3_derivative_wrt_angle(m, deg=False)
        d_UMAT_d_eta.append((math.pi / 180.) * d * d_umat)
        d_umat = site.axis_and_angle_as_r3_derivative_wrt_angle(-m, deg=False)
        d_UMAT_d_eta.append((math.pi / 180.) * -d * d_umat)

    #sanity check on the gaussian distribution
    if verbose:
        nm_angles = check_distributions.get_angular_rotation(UMAT)
        nm_rms_angle = math.sqrt(flex.mean(nm_angles * nm_angles))
        print("Normal rms angle is ", nm_rms_angle)

    return UMAT, d_UMAT_d_eta
Exemple #24
0
def compare_times(max_n_power=8):
    from scitbx.linalg import lapack_dsyev
    mt = flex.mersenne_twister(seed=0)
    show_tab_header = True
    tfmt = "%5.2f"
    for n_power in xrange(5, max_n_power + 1):
        n = 2**n_power
        l = mt.random_double(size=n * (n + 1) // 2) * 2 - 1
        a = l.matrix_packed_l_as_symmetric()
        aes = a.deep_copy()
        ala = [a.deep_copy(), a.deep_copy()]
        wla = [flex.double(n, -1e100), flex.double(n, -1e100)]
        t0 = time.time()
        es = eigensystem.real_symmetric(aes)
        tab = [n, tfmt % (time.time() - t0)]
        for i, use_fortran in enumerate([False, True]):
            t0 = time.time()
            info = lapack_dsyev(jobz="V",
                                uplo="U",
                                a=ala[i],
                                w=wla[i],
                                use_fortran=use_fortran)
            if (info == 99):
                tab.append(" --- ")
            else:
                assert info == 0
                tab.append(tfmt % (time.time() - t0))
                assert approx_equal(list(reversed(es.values())), wla[i])
        if (show_tab_header):
            print "      time [s]           eigenvalues"
            print " n    es   fem   for     min    max"
            show_tab_header = False
        tab.extend([es.values()[-1], es.values()[0]])
        print "%3d %s %s %s [%6.2f %6.2f]" % tuple(tab)
Exemple #25
0
  def __init__(self, values, parameterization, data, indices, bins, seed = None, log=None):
    """
    @param values parameterization of the SD terms
    @param data ISIGI dictionary of unmerged intensities
    @param indices array of miller indices to refine against
    @param bins array of flex.bool object specifying the bins to use to calculate the functional
    @param log Log to print to (none for stdout)
    """
    if log is None:
      log = sys.stdout
    self.log = log
    self.data = data
    self.intensity_bin_selections = bins
    self.indices = indices
    self.parameterization = parameterization
    self.n = 3
    self.x = flex.double([values.SDFAC, values.SDB, values.SDADD])
    self.starting_simplex = []
    if seed is None:
      random_func = flex.random_double
    else:
      print("Using random seed %d"%seed, file=self.log)
      mt = flex.mersenne_twister(seed)
      random_func = mt.random_double

    for i in range(self.n+1):
      self.starting_simplex.append(random_func(self.n))

    self.optimizer = simplex_opt( dimension = self.n,
                                  matrix    = self.starting_simplex,
                                  evaluator = self,
                                  tolerance = 1e-1)
    self.x = self.optimizer.get_solution()
Exemple #26
0
 def process(file_name, clash_threshold=2.0):
   time_start = time.time()
   pdb_inp = iotbx.pdb.input(file_name=file_name)
   pdb_atoms = pdb_inp.atoms()
   print "Time reading pdb file: %.2f" % (time.time() - time_start)
   print "Number of atoms:", pdb_atoms.size()
   pdb_atoms.set_chemical_element_simple_if_necessary()
   sites_cart = pdb_atoms.extract_xyz()
   #
   time_start = time.time()
   bond_list = extract_edge_list(edge_sets=build_simple_two_way_bond_sets(
     sites_cart=sites_cart,
     elements=pdb_atoms.extract_element()))
   print "Time building bond list: %.2f" % (time.time() - time_start)
   print "Number of bonds:", len(bond_list)
   #
   time_start = time.time()
   tardy_tree = scitbx.graph.tardy_tree.construct(
     sites=sites_cart,
     edge_list=bond_list)
   print "Time building tardy tree: %.2f" % (time.time() - time_start)
   #
   time_start = time.time()
   tardy_model = scitbx.rigid_body.tardy_model(
     labels=[atom.id_str() for atom in pdb_atoms],
     sites=sites_cart,
     masses=[1]*sites_cart.size(),
     tardy_tree=tardy_tree,
     potential_obj=None)
   q_size_each_joint = tardy_model.q_size_each_joint()
   q_fixed = tardy_model.pack_q()[:q_size_each_joint[0]]
   assert q_size_each_joint[1:].all_eq(1) # must all be revolute joints
   q_size_moving = q_size_each_joint.size() - 1
   print "Time building tardy model: %.2f" % (time.time() - time_start)
   print "Degrees of freedom:", q_size_moving
   #
   mt = flex.mersenne_twister()
   two_pi = 2 * math.pi
   clash_detector = build_clash_detector(
     n_sites=sites_cart.size(),
     bond_list=bond_list,
     threshold=clash_threshold)
   time_start = time.time()
   n_conf = 10000
   n_clash_conf = 0
   for i_conf in xrange(n_conf):
     q = q_fixed.deep_copy()
     q.extend(mt.random_double(size=q_size_moving)*two_pi)
     tardy_model.unpack_q(q_packed=q)
     conf_sites_cart = tardy_model.sites_moved()
     if (clash_detector.has_clash(sites_cart=conf_sites_cart)):
       n_clash_conf += 1
   time_diff = time.time() - time_start
   print "time / %d conf: %.2f seconds" % (n_conf, time_diff)
   print "time / conf: %.3f milli seconds" % (time_diff / n_conf * 1000)
   if (time_diff != 0):
     print "conf / second: %.2f" % (n_conf / time_diff)
   print "Fraction of conformations with clashes: %d / %d = %.2f %%" % (
     n_clash_conf, n_conf, 100. * n_clash_conf / n_conf)
Exemple #27
0
def add_noise(work_params, pixels):
    if (work_params.noise.max > 0):
        from scitbx.array_family import flex
        mt = flex.mersenne_twister(seed=work_params.noise.random_seed)
        noise = mt.random_size_t(size=pixels.size(),
                                 modulus=work_params.noise.max).as_int()
        noise.reshape(pixels.accessor())
        pixels += noise
Exemple #28
0
def exercise_cholesky():
  mt = flex.mersenne_twister(seed=0)
  for n in range(1,10):
    a = flex.double(n*n,0)
    a.resize(flex.grid(n, n))
    for i in range(n): a[(i,i)] = 1
    c = cholesky_decomposition(a)
    assert c is not None
    assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
    b = mt.random_double(size=n, factor=4)-2
    x = cholesky_solve(c, b)
    assert approx_equal(a.matrix_multiply(x), b)
    d = flex.random_size_t(size=n, modulus=10)
    for i in range(n): a[(i,i)] = d[i]+1
    c = cholesky_decomposition(a)
    assert c is not None
    assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
    b = mt.random_double(size=n, factor=4)-2
    x = cholesky_solve(c, b)
    assert approx_equal(a.matrix_multiply(x), b)
  #
  a = flex.double([8, -6, 0, -6, 9, -2, 0, -2, 8])
  a.resize(flex.grid(3,3))
  c = cholesky_decomposition(a)
  assert c is not None
  assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
  assert approx_equal(c, [
    2.828427125,          0,              0,
    -2.121320344,    2.121320343,         0,
         0.,        -0.9428090418,   2.666666667])
  #
  a0 = matrix.sym(sym_mat3=[3,5,7,1,2,-1])
  for i_trial in range(100):
    r = scitbx.math.euler_angles_as_matrix(
      mt.random_double(size=3,factor=360), deg=True)
    a = flex.double(r * a0 * r.transpose())
    a.resize(flex.grid(3,3))
    c = cholesky_decomposition(a)
    assert c is not None
    assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
    for b in [(0.1,-0.5,2), (-0.3,0.7,-1), (1.3,2.9,4), (-10,-20,17)]:
      b = flex.double(b)
      x = cholesky_solve(c, b)
      assert approx_equal(a.matrix_multiply(x), b)
  #
  for n in range(1,10):
    for i in range(10):
      r = mt.random_double(size=n*n, factor=10)-5
      r.resize(flex.grid(n,n))
      a = r.matrix_multiply(r.matrix_transpose())
      c = cholesky_decomposition(a)
      assert c is not None
      b = mt.random_double(size=n, factor=4)-2
      x = cholesky_solve(c, b)
      assert approx_equal(a.matrix_multiply(x), b)
      a[(i%n,i%n)] *= -1
      c = cholesky_decomposition(a)
      assert c is None
def exercise_cholesky():
  mt = flex.mersenne_twister(seed=0)
  for n in xrange(1,10):
    a = flex.double(n*n,0)
    a.resize(flex.grid(n, n))
    for i in xrange(n): a[(i,i)] = 1
    c = cholesky_decomposition(a)
    assert c is not None
    assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
    b = mt.random_double(size=n, factor=4)-2
    x = cholesky_solve(c, b)
    assert approx_equal(a.matrix_multiply(x), b)
    d = flex.random_size_t(size=n, modulus=10)
    for i in xrange(n): a[(i,i)] = d[i]+1
    c = cholesky_decomposition(a)
    assert c is not None
    assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
    b = mt.random_double(size=n, factor=4)-2
    x = cholesky_solve(c, b)
    assert approx_equal(a.matrix_multiply(x), b)
  #
  a = flex.double([8, -6, 0, -6, 9, -2, 0, -2, 8])
  a.resize(flex.grid(3,3))
  c = cholesky_decomposition(a)
  assert c is not None
  assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
  assert approx_equal(c, [
    2.828427125,          0,              0,
    -2.121320344,    2.121320343,         0,
         0.,        -0.9428090418,   2.666666667])
  #
  a0 = matrix.sym(sym_mat3=[3,5,7,1,2,-1])
  for i_trial in xrange(100):
    r = scitbx.math.euler_angles_as_matrix(
      mt.random_double(size=3,factor=360), deg=True)
    a = flex.double(r * a0 * r.transpose())
    a.resize(flex.grid(3,3))
    c = cholesky_decomposition(a)
    assert c is not None
    assert approx_equal(c.matrix_multiply(c.matrix_transpose()), a)
    for b in [(0.1,-0.5,2), (-0.3,0.7,-1), (1.3,2.9,4), (-10,-20,17)]:
      b = flex.double(b)
      x = cholesky_solve(c, b)
      assert approx_equal(a.matrix_multiply(x), b)
  #
  for n in xrange(1,10):
    for i in xrange(10):
      r = mt.random_double(size=n*n, factor=10)-5
      r.resize(flex.grid(n,n))
      a = r.matrix_multiply(r.matrix_transpose())
      c = cholesky_decomposition(a)
      assert c is not None
      b = mt.random_double(size=n, factor=4)-2
      x = cholesky_solve(c, b)
      assert approx_equal(a.matrix_multiply(x), b)
      a[(i%n,i%n)] *= -1
      c = cholesky_decomposition(a)
      assert c is None
def run(args):
    assert len(args) == 0
    n_trials = 100
    from scitbx.math.minimum_covering_ellipsoid import compute as mce_compute
    from scitbx.array_family import flex
    from libtbx.test_utils import approx_equal, is_below_limit
    # XXX point group 222 should be sufficient, but 432 is currently needed
    point_group_432_rotation_matrices = [(1, 0, 0, 0, 1, 0, 0, 0, 1),
                                         (1, 0, 0, 0, 0, -1, 0, 1, 0),
                                         (1, 0, 0, 0, 0, 1, 0, -1, 0),
                                         (0, 0, 1, 0, 1, 0, -1, 0, 0),
                                         (0, 0, -1, 0, 1, 0, 1, 0, 0),
                                         (0, -1, 0, 1, 0, 0, 0, 0, 1),
                                         (0, 1, 0, -1, 0, 0, 0, 0, 1),
                                         (0, 0, 1, 1, 0, 0, 0, 1, 0),
                                         (0, 1, 0, 0, 0, 1, 1, 0, 0),
                                         (0, -1, 0, 0, 0, -1, 1, 0, 0),
                                         (0, 0, 1, -1, 0, 0, 0, -1, 0),
                                         (0, -1, 0, 0, 0, 1, -1, 0, 0),
                                         (0, 0, -1, -1, 0, 0, 0, 1, 0),
                                         (0, 0, -1, 1, 0, 0, 0, -1, 0),
                                         (0, 1, 0, 0, 0, -1, -1, 0, 0),
                                         (1, 0, 0, 0, -1, 0, 0, 0, -1),
                                         (-1, 0, 0, 0, 1, 0, 0, 0, -1),
                                         (-1, 0, 0, 0, -1, 0, 0, 0, 1),
                                         (0, 1, 0, 1, 0, 0, 0, 0, -1),
                                         (0, -1, 0, -1, 0, 0, 0, 0, -1),
                                         (0, 0, 1, 0, -1, 0, 1, 0, 0),
                                         (0, 0, -1, 0, -1, 0, -1, 0, 0),
                                         (-1, 0, 0, 0, 0, 1, 0, 1, 0),
                                         (-1, 0, 0, 0, 0, -1, 0, -1, 0)]

    def check(center, radii, rotation):
        a, b, c = radii
        points_principal = flex.vec3_double([(-a, 0, 0), (a, 0, 0), (0, -b, 0),
                                             (0, b, 0), (0, 0, -c), (0, 0, c)])
        points = rotation * points_principal + center
        mce = mce_compute(points)
        assert approx_equal(mce.center, center)
        assert approx_equal(sorted(mce.radii), sorted(radii))
        assert approx_equal(mce.rotation.determinant(), 1)
        points_mce = mce.rotation.inverse().elems * (points - mce.center)
        rms = []
        for r in point_group_432_rotation_matrices:
            rp = r * points_mce
            rms.append(rp.rms_difference(points_principal))
        assert is_below_limit(value=min(rms), limit=1e-8, eps=0)

    mt = flex.mersenne_twister(seed=0)
    check((0, 0, 0), (1, 2, 3), (1, 0, 0, 0, 1, 0, 0, 0, 1))
    for i_trial in xrange(n_trials):
        center = list(mt.random_double(size=3) * 8 - 4)
        radii = list(mt.random_double(size=3) * 3 + 0.1)
        rotation = mt.random_double_r3_rotation_matrix()
        check(center, radii, rotation)
    from libtbx.utils import format_cpu_times
    print format_cpu_times()
Exemple #31
0
def add_noise(work_params, pixels):
  if (work_params.noise.max > 0):
    from scitbx.array_family import flex
    mt = flex.mersenne_twister(seed=work_params.noise.random_seed)
    noise = mt.random_size_t(
      size=pixels.size(),
      modulus=work_params.noise.max).as_int()
    noise.reshape(pixels.accessor())
    pixels += noise
def exercise_simulation(
      out, n_trials, n_dynamics_steps, delta_t=0.0001, random_seed=0):
  mersenne_twister = flex.mersenne_twister(seed=random_seed)
  sim_labels = None
  relative_ranges_accu = None
  rms_max_list_accu = None
  for i_trial in xrange(n_trials):
    sim_labels_new, e_tots_list, \
    relative_ranges, rms_max_list = run_simulations(
      out=out,
      mersenne_twister=mersenne_twister,
      n_dynamics_steps=n_dynamics_steps,
      delta_t=delta_t)
    if (sim_labels is None):
      sim_labels = sim_labels_new
    else:
      assert sim_labels == sim_labels_new
    if (relative_ranges_accu is None):
      relative_ranges_accu=[flex.double() for i in xrange(len(relative_ranges))]
    else:
      assert len(relative_ranges) == len(relative_ranges_accu)
    for r,a in zip(relative_ranges, relative_ranges_accu):
      a.append(r)
    if (rms_max_list_accu is None):
      rms_max_list_accu = [flex.double() for i in xrange(len(rms_max_list))]
    else:
      assert len(rms_max_list) == len(rms_max_list_accu)
    for r,a in zip(rms_max_list, rms_max_list_accu):
      a.append(r)
    if (out is sys.stdout):
      f = open("tmp_e_tots_%02d_%02d.xy" % (plot_prefix, i_trial), "w")
      print >> f, "@with g0"
      for i,l in enumerate(sim_labels):
        l = l[l.find('"')+1:].replace('"','')[:-1]
        print >> f, '@ s%d legend "%s"' % (i, l)
      for es in e_tots_list:
        for e in es:
          print >> f, e
        print >> f, "&"
      f.close()
  print >> out, "Accumulated results:"
  print >> out
  for sim_label,accu in zip(sim_labels, relative_ranges_accu):
    print >> out, "relative ranges %s:" % sim_label
    accu.min_max_mean().show(out=out, prefix="  ")
    print >> out
  for sim_label,accu in zip(sim_labels[1:], rms_max_list_accu):
    print >> out, "rms max %s" % sim_labels[0]
    print >> out, "    vs. %s:" % sim_label
    accu.min_max_mean().show(out=out, prefix="  ")
    print >> out
  if (out is not sys.stdout):
    for accu in relative_ranges_accu:
      assert flex.max(accu) < 1.e-4
    for i,accu in enumerate(rms_max_list_accu):
      assert flex.max(accu) < 1.e-4
Exemple #33
0
def exercise_simulation(
      out, n_trials, n_dynamics_steps, delta_t=0.0001, random_seed=0):
  mersenne_twister = flex.mersenne_twister(seed=random_seed)
  sim_labels = None
  relative_ranges_accu = None
  rms_max_list_accu = None
  for i_trial in range(n_trials):
    sim_labels_new, e_tots_list, \
    relative_ranges, rms_max_list = run_simulations(
      out=out,
      mersenne_twister=mersenne_twister,
      n_dynamics_steps=n_dynamics_steps,
      delta_t=delta_t)
    if (sim_labels is None):
      sim_labels = sim_labels_new
    else:
      assert sim_labels == sim_labels_new
    if (relative_ranges_accu is None):
      relative_ranges_accu=[flex.double() for i in range(len(relative_ranges))]
    else:
      assert len(relative_ranges) == len(relative_ranges_accu)
    for r,a in zip(relative_ranges, relative_ranges_accu):
      a.append(r)
    if (rms_max_list_accu is None):
      rms_max_list_accu = [flex.double() for i in range(len(rms_max_list))]
    else:
      assert len(rms_max_list) == len(rms_max_list_accu)
    for r,a in zip(rms_max_list, rms_max_list_accu):
      a.append(r)
    if (out is sys.stdout):
      f = open("tmp_e_tots_%02d_%02d.xy" % (plot_prefix, i_trial), "w")
      print("@with g0", file=f)
      for i,l in enumerate(sim_labels):
        l = l[l.find('"')+1:].replace('"','')[:-1]
        print('@ s%d legend "%s"' % (i, l), file=f)
      for es in e_tots_list:
        for e in es:
          print(e, file=f)
        print("&", file=f)
      f.close()
  print("Accumulated results:", file=out)
  print(file=out)
  for sim_label,accu in zip(sim_labels, relative_ranges_accu):
    print("relative ranges %s:" % sim_label, file=out)
    accu.min_max_mean().show(out=out, prefix="  ")
    print(file=out)
  for sim_label,accu in zip(sim_labels[1:], rms_max_list_accu):
    print("rms max %s" % sim_labels[0], file=out)
    print("    vs. %s:" % sim_label, file=out)
    accu.min_max_mean().show(out=out, prefix="  ")
    print(file=out)
  if (out is not sys.stdout):
    for accu in relative_ranges_accu:
      assert flex.max(accu) < 1.e-4
    for i,accu in enumerate(rms_max_list_accu):
      assert flex.max(accu) < 1.e-4
Exemple #34
0
def exercise_writer():
    from iotbx import file_reader
    from cctbx import uctbx, sgtbx
    from scitbx.array_family import flex
    file_name = libtbx.env.find_in_repositories(
        relative_path=
        "phenix_regression/wizards/partial_refine_001_map_coeffs.mtz",
        test=os.path.isfile)
    if file_name is None:
        print "Can't find map coefficients file, skipping."
        return
    mtz_in = file_reader.any_file(file_name, force_type="hkl").file_object
    miller_arrays = mtz_in.as_miller_arrays()
    map_coeffs = miller_arrays[0]
    fft_map = map_coeffs.fft_map(resolution_factor=1 / 3.0)
    fft_map.apply_sigma_scaling()
    fft_map.as_ccp4_map(file_name="2mFo-DFc.map")
    m = iotbx.ccp4_map.map_reader(file_name="2mFo-DFc.map")
    real_map = fft_map.real_map_unpadded()
    mmm = flex.double(list(real_map)).min_max_mean()
    assert approx_equal(m.unit_cell_parameters,
                        map_coeffs.unit_cell().parameters())
    assert approx_equal(mmm.min, m.header_min)
    assert approx_equal(mmm.max, m.header_max)
    #assert approx_equal(mmm.mean, m.header_mean)
    # random small maps of different sizes
    for nxyz in flex.nested_loop((1, 1, 1), (4, 4, 4)):
        mt = flex.mersenne_twister(0)
        grid = flex.grid(nxyz)
        map = mt.random_double(size=grid.size_1d())
        map.reshape(grid)
        real_map = fft_map.real_map_unpadded()
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            gridding_first=(0, 0, 0),
            gridding_last=tuple(fft_map.n_real()),
            map_data=real_map,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
        m = iotbx.ccp4_map.map_reader(file_name="random.map")
        mmm = flex.double(list(real_map)).min_max_mean()
        assert approx_equal(m.unit_cell_parameters, (1, 1, 1, 90, 90, 90))
        assert approx_equal(mmm.min, m.header_min)
        assert approx_equal(mmm.max, m.header_max)
        #
        gridding_first = (0, 0, 0)
        gridding_last = tuple(fft_map.n_real())
        map_box = maptbx.copy(map, gridding_first, gridding_last)
        map_box.reshape(flex.grid(map_box.all()))
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random_box.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            map_data=map_box,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
Exemple #35
0
def exercise(args):
  assert len(args) == 0
  sites = flex.vec3_double([
    (10.949, 12.815, 15.189),
    (10.405, 13.954, 15.917),
    (10.779, 15.262, 15.227)])

  class energy_cart(object):

    def __init__(self, nodes, homes):
      assert nodes.size() == homes.size()
      self.nodes = nodes
      self.homes = homes

    def functional(self):
      return flex.sum((self.nodes-self.homes).dot())

    def gradients(self):
      return 2*(self.nodes-self.homes)

  def incr_position(rb, i, delta):
    assert 0 <= i < 6
    if (i < 3):
      v = list(rb.ea)
      v[i] += delta
      rb.ea = matrix.col(v)
    else:
      v = list(rb.lt)
      v[i-3] += delta
      rb.lt = matrix.col(v)

  def ea_gradients_fd(rb, energy_cart_function, eps=1.e-6):
    result = []
    for i in xrange(6):
      fs = []
      incr_position(rb=rb, i=i, delta=eps)
      fs.append(energy_cart_function(
        nodes=rb.sites_moved(), homes=rb.sites_orig).functional())
      incr_position(rb=rb, i=i, delta=-eps)
      incr_position(rb=rb, i=i, delta=-eps)
      fs.append(energy_cart_function(
        nodes=rb.sites_moved(), homes=rb.sites_orig).functional())
      incr_position(rb=rb, i=i, delta=eps)
      result.append((fs[0]-fs[1])/(2*eps))
    return result


  rb = rigid_body(sites=sites)
  mt = flex.mersenne_twister()
  n_trials = 4
  for i in xrange(n_trials):
    rb.ea = matrix.col(mt.random_double_point_on_sphere()) * i
    rb.lt = matrix.col(mt.random_double_point_on_sphere()) * i
    show_gradients(rb=rb)

  print "OK"
 def initialization(self):
   self.x0 = self.fit.parameters()
   self.capital_f_x_star = 0.5*self.f(x=self.x0).norm()**2
   if (self.perturb):
     mersenne_twister = flex.mersenne_twister(seed=0)
     self.x0 *= 1 + mersenne_twister.random_double(
       size=self.x0.size(), factor=0.01)
   self.tau0 = 1e-8
   self.delta0 = 10
   self.x_star = None
Exemple #37
0
 def initialization(self):
     self.x0 = self.fit.parameters()
     self.capital_f_x_star = 0.5 * self.f(x=self.x0).norm()**2
     if (self.perturb):
         mersenne_twister = flex.mersenne_twister(seed=0)
         self.x0 *= 1 + mersenne_twister.random_double(size=self.x0.size(),
                                                       factor=0.01)
     self.tau0 = 1e-8
     self.delta0 = 10
     self.x_star = None
Exemple #38
0
def exercise_writer () :
  from iotbx import file_reader
  from cctbx import uctbx, sgtbx
  from scitbx.array_family import flex
  file_name = libtbx.env.find_in_repositories(
    relative_path="phenix_regression/wizards/partial_refine_001_map_coeffs.mtz",
    test=os.path.isfile)
  if file_name is None :
    print "Can't find map coefficients file, skipping."
    return
  mtz_in = file_reader.any_file(file_name, force_type="hkl").file_object
  miller_arrays = mtz_in.as_miller_arrays()
  map_coeffs = miller_arrays[0]
  fft_map = map_coeffs.fft_map(resolution_factor=1/3.0)
  fft_map.apply_sigma_scaling()
  fft_map.as_ccp4_map(file_name="2mFo-DFc.map")
  m = iotbx.ccp4_map.map_reader(file_name="2mFo-DFc.map")
  real_map = fft_map.real_map_unpadded()
  mmm = flex.double(list(real_map)).min_max_mean()
  assert approx_equal(m.unit_cell_parameters,
                      map_coeffs.unit_cell().parameters())
  assert approx_equal(mmm.min, m.header_min)
  assert approx_equal(mmm.max, m.header_max)
  #assert approx_equal(mmm.mean, m.header_mean)
  # random small maps of different sizes
  for nxyz in flex.nested_loop((1,1,1),(4,4,4)):
    mt = flex.mersenne_twister(0)
    grid = flex.grid(nxyz)
    map = mt.random_double(size=grid.size_1d())
    map.reshape(grid)
    real_map = fft_map.real_map_unpadded()
    iotbx.ccp4_map.write_ccp4_map(
      file_name="random.map",
      unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
      space_group=sgtbx.space_group_info("P1").group(),
      gridding_first=(0,0,0),
      gridding_last=tuple(fft_map.n_real()),
      map_data=real_map,
      labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    m = iotbx.ccp4_map.map_reader(file_name="random.map")
    mmm = flex.double(list(real_map)).min_max_mean()
    assert approx_equal(m.unit_cell_parameters, (1,1,1,90,90,90))
    assert approx_equal(mmm.min, m.header_min)
    assert approx_equal(mmm.max, m.header_max)
    #
    gridding_first = (0,0,0)
    gridding_last = tuple(fft_map.n_real())
    map_box = maptbx.copy(map, gridding_first, gridding_last)
    map_box.reshape(flex.grid(map_box.all()))
    iotbx.ccp4_map.write_ccp4_map(
      file_name="random_box.map",
      unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
      space_group=sgtbx.space_group_info("P1").group(),
      map_data=map_box,
      labels=flex.std_string(["iotbx.ccp4_map.tst"]))
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])
def tst_all(
    serial_no
):  #emulates the action of step5_pad.py in assigning a coarse orientation to each simulation event
    mt = flex.mersenne_twister(seed=0)

    Nimages = 100000
    for iteration in range(Nimages):
        rand_ori = sqr(mt.random_double_r3_rotation_matrix())
        if serial_no == iteration:
            return rand_ori
    return None
Exemple #41
0
def exercise_revolute(out, n_trials, n_dynamics_steps, delta_t=0.001):
  mersenne_twister = flex.mersenne_twister(seed=0)
  for i_trial in range(n_trials):
    body = revolute_body(mersenne_twister=mersenne_twister)
    body.parent = -1
    sim = simulation(bodies=[body])
    print("revolute:", file=out)
    relative_range = exercise_sim(
      out=out, n_dynamics_steps=n_dynamics_steps, delta_t=delta_t, sim=sim)
    if (out is not sys.stdout):
      assert relative_range < 1.e-4
def exercise_revolute(out, n_trials, n_dynamics_steps, delta_t=0.001):
  mersenne_twister = flex.mersenne_twister(seed=0)
  for i_trial in xrange(n_trials):
    body = revolute_body(mersenne_twister=mersenne_twister)
    body.parent = -1
    sim = simulation(bodies=[body])
    print >> out, "revolute:"
    relative_range = exercise_sim(
      out=out, n_dynamics_steps=n_dynamics_steps, delta_t=delta_t, sim=sim)
    if (out is not sys.stdout):
      assert relative_range < 1.e-4
Exemple #43
0
def run(args):
    assert len(args) == 0
    exercise_cubicles_max_memory()
    from scitbx.cubicle_neighbors import cubicle_neighbors
    from scitbx.array_family import flex
    main_sites_cart = flex.vec3_double()
    cn = cubicle_neighbors(main_sites_cart=main_sites_cart, cubicle_edge=5)
    nb = cn.neighbors_of(other_sites_cart=main_sites_cart,
                         distance_cutoff_sq=1)
    assert nb.size() == 0
    for xyz in [(0, 0, 0), (0.1, 0.2, -0.3)]:
        main_sites_cart = flex.vec3_double([xyz])
        cn = cubicle_neighbors(main_sites_cart=main_sites_cart, cubicle_edge=5)
        nb = cn.neighbors_of(other_sites_cart=main_sites_cart,
                             distance_cutoff_sq=1)
        assert nb.size() == 1
        assert nb.keys() == [0]
        assert list(nb[0]) == [0]
        nb = cn.neighbors_of(other_sites_cart=flex.vec3_double([(2, 2, 2)]),
                             distance_cutoff_sq=1)
        assert nb.size() == 0
        nb = cn.neighbors_of(other_sites_cart=flex.vec3_double([(2, 2, 2)]),
                             distance_cutoff_sq=25)
        assert nb.size() == 1
    mt = flex.mersenne_twister(seed=0)
    for nm in [3, 5, 8]:
        for no in [1, 7, 9]:
            main_sites_cart = flex.vec3_double(
                list(
                    zip(
                        mt.random_double(size=nm) * 2 - 1,
                        mt.random_double(size=nm) * 2 - 1,
                        mt.random_double(size=nm) * 2 - 1)))
            other_sites_cart = flex.vec3_double(
                list(
                    zip(
                        mt.random_double(size=no) * 2 - 1,
                        mt.random_double(size=no) * 2 - 1,
                        mt.random_double(size=no) * 2 - 1)))
            for distance_cutoff in [0.5, 1]:
                distance_cutoff_sq = distance_cutoff**2
                cn = cubicle_neighbors(main_sites_cart=main_sites_cart,
                                       cubicle_edge=1)
                nb = cn.neighbors_of(other_sites_cart=other_sites_cart,
                                     distance_cutoff_sq=distance_cutoff_sq)
                nb_simple = neighbors_simple(
                    main_sites_cart=main_sites_cart,
                    other_sites_cart=other_sites_cart,
                    distance_cutoff_sq=distance_cutoff_sq)
                assert sorted(nb.keys()) == sorted(nb_simple.keys())
                for j_seq, i_seqs_simple in nb_simple.items():
                    i_seqs = nb[j_seq]
                    assert sorted(i_seqs) == sorted(i_seqs_simple)
    print("OK")
def exercise_six_dof(out, n_trials, n_dynamics_steps, delta_t=0.001):
  mersenne_twister = flex.mersenne_twister(seed=0)
  for n_sites in xrange(1,4):
    for i_trial in xrange(n_trials):
      body = six_dof_body(mersenne_twister=mersenne_twister, n_sites=n_sites)
      body.parent = -1
      sim = simulation(bodies=[body])
      print >> out, "six_dof number of sites:", n_sites
      relative_range = exercise_sim(
        out=out, n_dynamics_steps=n_dynamics_steps, delta_t=delta_t, sim=sim)
      if (out is not sys.stdout):
        assert relative_range < 1.e-4
Exemple #45
0
def exercise_six_dof(out, n_trials, n_dynamics_steps, delta_t=0.001):
  mersenne_twister = flex.mersenne_twister(seed=0)
  for n_sites in range(1,4):
    for i_trial in range(n_trials):
      body = six_dof_body(mersenne_twister=mersenne_twister, n_sites=n_sites)
      body.parent = -1
      sim = simulation(bodies=[body])
      print("six_dof number of sites:", n_sites, file=out)
      relative_range = exercise_sim(
        out=out, n_dynamics_steps=n_dynamics_steps, delta_t=delta_t, sim=sim)
      if (out is not sys.stdout):
        assert relative_range < 1.e-4
Exemple #46
0
def run_sim2smv(fileout):
    SIM = nanoBragg(detpixels_slowfast=(1000, 1000),
                    pixel_size_mm=0.1,
                    Ncells_abc=(5, 5, 5),
                    verbose=0)
    SIM.mosaic_spread_deg = MOSAIC_SPREAD  # apparently this is half width
    SIM.mosaic_domains = SAMPLE_SIZE
    SIM.distance_mm = 100  # this triggers the generation of mosaic distribution
    UMAT_th = SIM.get_mosaic_blocks()  # extract top-hat distributed U-mats

    #sanity checks on the top hat distribution
    th_angles = check_distributions.get_angular_rotation(UMAT_th)
    max_angle = flex.max(th_angles)
    assert max_angle <= MOSAIC_SPREAD + 0.0000001  # need to allow a small epsilon
    assert max_angle > 0.99 * MOSAIC_SPREAD  # insist that max angle is near the limit we gave it
    rms_angle = math.sqrt(flex.mean(th_angles * th_angles))
    print(rms_angle)
    assert rms_angle < MOSAIC_SPREAD

    import scitbx  # compute an array of normally-distributed U-mats into the simulator
    UMAT_nm = flex.mat3_double()
    mersenne_twister = flex.mersenne_twister(
        seed=0)  # set seed, get reproducible results
    scitbx.random.set_random_seed(4321)  # set seed, get reproducibe results
    rand_norm = scitbx.random.normal_distribution(mean=0,
                                                  sigma=rms_angle * math.pi /
                                                  180.)
    g = scitbx.random.variate(rand_norm)
    mosaic_rotation = g(SAMPLE_SIZE)
    for m in mosaic_rotation:
        site = col(mersenne_twister.random_double_point_on_sphere())
        UMAT_nm.append(site.axis_and_angle_as_r3_rotation_matrix(m, deg=False))

    # set the normally-distributed U-mats into the simulator:
    SIM.set_mosaic_blocks(UMAT_nm)

    # get them back
    new_UMAT_nm = SIM.get_mosaic_blocks()

    #double check that simulator does not alter the UMATs
    for iumat in range(0, SAMPLE_SIZE, 100):

        assert UMAT_nm[iumat] == new_UMAT_nm[iumat]

    #sanity check on the gaussian distribution
    nm_angles = check_distributions.get_angular_rotation(new_UMAT_nm)
    nm_rms_angle = math.sqrt(flex.mean(nm_angles * nm_angles))
    print(nm_rms_angle)
    assert approx_equal(rms_angle,nm_rms_angle,eps=1e-03), \
    "The top hat and gaussian models should have similar standard deviations"

    return UMAT_th, new_UMAT_nm
Exemple #47
0
def compare_large_3d_real_imag_w_complete_true_false(verbose):
  mt = flex.mersenne_twister(seed=0)
  nu, nv, nw = 50, 52, 54
  map_size = nu * nv * (nw+2)
  map = (mt.random_double(size=map_size)*2-1).as_float()
  recycled = []
  for complete in [True, False]:
    x = map.deep_copy()
    ccp4io_dev_ext.fftlib_real_to_complex_3d_real_imag_w(
      x=x, nu=nu, nv=nv, nw=nw, complete=complete)
    ccp4io_dev_ext.hermitian_conjugate_3d_real_imag_w(x=x, nu=nu, nv=nv, nw=nw)
    ccp4io_dev_ext.fftlib_complex_to_real_3d_real_imag_w(
      x=x, nu=nu, nv=nv, nw=nw, complete=complete)
    recycled.append(x[:nu*nv*nw])
  show_complete_true_false_cc(nu, nv, nw, recycled, verbose)
Exemple #48
0
def compare_fftpack_with_cmplft_1d():
  mt = flex.mersenne_twister(seed=0)
  for n in xrange(1, 101):
    primes = prime_factors_of(n)
    if (n != 1 and max(primes) > 19): continue
    z = (mt.random_double(size=n*2)*2-1).as_float()
    cmplft_xy = z.deep_copy()
    d = flex.int((2*n,2,2*n,2*n,2*n))
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=n, d=d)
    fft = scitbx.fftpack.complex_to_complex(n)
    fftpack_xy = z.as_double()
    fft.forward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float()
    if (flex.max_absolute(cmplft_xy-fftpack_xy) > 1e-5):
      assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-5)
def exercise():
  from scitbx.array_family import flex
  from scitbx import matrix
  from libtbx.test_utils import approx_equal, show_diff
  import libtbx.load_env
  if (libtbx.env.has_module(name="iotbx")):
    from iotbx.shelx.parsers import decode_variables
    import iotbx.shelx.errors
  else:
    decode_variables = None
  mt = flex.mersenne_twister(seed=0)
  for sgi,site,expected_expr,no_fvar in test_case_generator():
    sps = sgi.any_compatible_crystal_symmetry(volume=1000) \
      .special_position_settings()
    ss = sps.site_symmetry(site)
    assert approx_equal(ss.exact_site(), site)
    sos = ss.special_op_simplified()
    expr = str(sos)
    assert not show_diff(expr, expected_expr)
    def check(site):
      ns = dict(zip("xyz", site))
      expr_site = eval(expr, ns, {})
      assert approx_equal(expr_site, site, 1e-4)
      #
      shifted_site = (
        matrix.col(site) + matrix.col(mt.random_double_point_on_sphere()))
      ns = dict(zip("xyz", shifted_site))
      expr_shifted_site = eval(expr, ns, {})
      expr_shifted_site_exact = ss.special_op() * expr_shifted_site
      assert approx_equal(expr_shifted_site, expr_shifted_site_exact)
    check(site)
    for i_trial in xrange(3):
      shifted_site = ss.special_op() * (
        matrix.col(site) + matrix.col(mt.random_double_point_on_sphere()))
      check(shifted_site)
    fvars = [None] # placeholder for scale factor
    if (decode_variables is not None):
      try:
        coded_variables = ss.shelx_fvar_encoding(site=site, fvars=fvars)
      except iotbx.shelx.errors.error:
        if (not no_fvar): raise
      else:
        assert not no_fvar
      if (not no_fvar):
        values, behaviors = decode_variables(
          free_variable=fvars, coded_variables=coded_variables)
        mismatch = sps.unit_cell().mod_short_distance(site, values)
        assert mismatch < 1e-10
def run(args):
  assert len(args) == 0
  exercise_cubicles_max_memory()
  from scitbx.cubicle_neighbors import cubicle_neighbors
  from scitbx.array_family import flex
  main_sites_cart = flex.vec3_double()
  cn = cubicle_neighbors(main_sites_cart=main_sites_cart, cubicle_edge=5)
  nb = cn.neighbors_of(other_sites_cart=main_sites_cart, distance_cutoff_sq=1)
  assert nb.size() == 0
  for xyz in [(0,0,0), (0.1, 0.2, -0.3)]:
    main_sites_cart = flex.vec3_double([xyz])
    cn = cubicle_neighbors(main_sites_cart=main_sites_cart, cubicle_edge=5)
    nb = cn.neighbors_of(other_sites_cart=main_sites_cart, distance_cutoff_sq=1)
    assert nb.size() == 1
    assert nb.keys() == [0]
    assert list(nb[0]) == [0]
    nb = cn.neighbors_of(
      other_sites_cart=flex.vec3_double([(2,2,2)]), distance_cutoff_sq=1)
    assert nb.size() == 0
    nb = cn.neighbors_of(
      other_sites_cart=flex.vec3_double([(2,2,2)]), distance_cutoff_sq=25)
    assert nb.size() == 1
  mt = flex.mersenne_twister(seed=0)
  for nm in [3,5,8]:
    for no in [1,7,9]:
      main_sites_cart = flex.vec3_double(zip(
        mt.random_double(size=nm)*2-1,
        mt.random_double(size=nm)*2-1,
        mt.random_double(size=nm)*2-1))
      other_sites_cart = flex.vec3_double(zip(
        mt.random_double(size=no)*2-1,
        mt.random_double(size=no)*2-1,
        mt.random_double(size=no)*2-1))
      for distance_cutoff in [0.5, 1]:
        distance_cutoff_sq = distance_cutoff**2
        cn = cubicle_neighbors(main_sites_cart=main_sites_cart, cubicle_edge=1)
        nb = cn.neighbors_of(
          other_sites_cart=other_sites_cart,
          distance_cutoff_sq=distance_cutoff_sq)
        nb_simple = neighbors_simple(
          main_sites_cart=main_sites_cart,
          other_sites_cart=other_sites_cart,
          distance_cutoff_sq=distance_cutoff_sq)
        assert sorted(nb.keys()) == sorted(nb_simple.keys())
        for j_seq,i_seqs_simple in nb_simple.items():
          i_seqs = nb[j_seq]
          assert sorted(i_seqs) == sorted(i_seqs_simple)
  print "OK"
def exercise():
  mt = flex.mersenne_twister(seed=0)
  sz = 12
  for i_trial in xrange(10):
    x = mt.random_double(size=sz)*5-1
    y = mt.random_double(size=sz)*3-1
    for i_w,w in enumerate([flex.double(sz, 1), mt.random_double(size=sz)*7]):
      cc, d_cc, d2_cc = weighted_correlation(
        w, x, y, derivatives_wrt_y_depth=2)
      if (i_w == 0):
        cc_w1 = flex.linear_correlation(x, y).coefficient()
        assert approx_equal(cc, cc_w1)
      d_cc_fd = finite_difference_derivatives(w, x, y, depth=1)
      assert approx_equal(d_cc, d_cc_fd)
      d2_cc_fd = finite_difference_derivatives(w, x, y, depth=2)
      assert approx_equal(d2_cc, d2_cc_fd)
def exercise_revolute(out, n_trials, n_dynamics_steps, delta_t=0.001, NB=3):
  mersenne_twister = flex.mersenne_twister(seed=0)
  relative_ranges = flex.double()
  for i_trial in xrange(n_trials):
    relative_ranges.append(exercise_revolute_sim(
      out=out,
      mersenne_twister=mersenne_twister,
      n_dynamics_steps=n_dynamics_steps,
      delta_t=delta_t,
      NB=[1, NB][min(i_trial, 1)],
      config=["singular", "zigzag", "random"][min(i_trial, 2)]))
  print >> out, "relative ranges:"
  relative_ranges.min_max_mean().show(out=out, prefix="  ")
  if (out is not sys.stdout):
    assert flex.max(relative_ranges) < 0.0006
  print >> out
def simulation_ala_with_h():
  pdb = """\
ATOM      0  N   ALA A   1      10.949  12.815  15.189  0.00  0.00           N
ATOM      1  CA  ALA A   1      10.405  13.954  15.917  0.00  0.00           C
ATOM      2  C   ALA A   1      10.779  15.262  15.227  0.00  0.00           C
ATOM      3  HA  ALA A   1       9.428  13.887  15.936  0.00  0.00           H
ATOM      4  O   ALA A   1       9.916  16.090  14.936  0.00  0.00           O
ATOM      5  H   ALA A   1      11.792  12.691  15.311  0.00  0.00           H
ATOM      6  CB  ALA A   1      10.908  13.950  17.351  0.00  0.00           C
ATOM      7  HB1 ALA A   1      10.627  13.138  17.778  0.00  0.00           H
ATOM      8  HB2 ALA A   1      10.540  14.707  17.813  0.00  0.00           H
ATOM      9  HB3 ALA A   1      11.867  14.004  17.346  0.00  0.00           H
"""
  labels, sites = pdb_extract(pdb=pdb)
  mersenne_twister = flex.mersenne_twister(seed=0)
  body0 = six_dof_body(
    labels=labels[:4],
    sites=sites[:4],
    bonds=[(0,1),(1,2),(1,3)],
    mersenne_twister=mersenne_twister)
  body0.parent = -1
  body1 = revolute_body(
    labels=labels[4:5],
    sites=sites[4:5],
    bonds=[(-2,0)],
    pivot=sites[2],
    normal=(sites[2]-sites[1]).normalize(),
    mersenne_twister=mersenne_twister)
  body1.parent = 0
  body2 = revolute_body(
    labels=labels[5:6],
    sites=sites[5:6],
    bonds=[(-4,0)],
    pivot=sites[0],
    normal=(sites[0]-sites[1]).normalize(),
    mersenne_twister=mersenne_twister)
  body2.parent = 0
  body3 = revolute_body(
    labels=labels[6:],
    sites=sites[6:],
    bonds=[(-3,0),(0,1),(0,2),(0,3)],
    pivot=sites[6],
    normal=(sites[6]-sites[1]).normalize(),
    mersenne_twister=mersenne_twister)
  body3.parent = 0
  return simulation(bodies=[body0, body1, body2, body3])
Exemple #54
0
 def build_one_image(i_img):
   mt = flex.mersenne_twister(seed=work_params.noise.random_seed+i_img)
   scale = int(work_params.signal_max*(0.1+0.9*mt.random_double()))
   crystal_rotation = mt.random_double_r3_rotation_matrix_arvo_1992()
   i_perm = mt.random_size_t() % len(i_calc_data_perms)
   image = image_simple(
     store_miller_index_i_seqs=True,
     store_spots=True,
     store_signals=True,
     set_pixels=True).compute(
       unit_cell=i_calc.unit_cell(),
       miller_indices=i_calc.indices(),
       spot_intensity_factors=i_calc_data_perms[i_perm],
       crystal_rotation_matrix=crystal_rotation,
       ewald_radius=1/work_params.wavelength,
       ewald_proximity=work_params.ewald_proximity,
       signal_max=scale,
       detector_distance=work_params.detector.distance,
       detector_size=work_params.detector.size,
       detector_pixels=work_params.detector.pixels,
       point_spread=work_params.point_spread,
       gaussian_falloff_scale=work_params.gaussian_falloff_scale)
   add_noise(work_params, pixels=image.pixels)
   if (not work_params.index_and_integrate):
     pixels = None
   else:
     pixels = image.pixels
   miller_index_i_seqs = image.miller_index_i_seqs
   if (use_mp):
     # to by-pass portable but slower pickling
     if (pixels is not None):
       assert pixels.is_0_based()
       assert not pixels.is_padded()
       assert pixels.all() == tuple(work_params.detector.pixels)
       pixels = pixels.copy_to_byte_str()
     miller_index_i_seqs = miller_index_i_seqs.copy_to_byte_str()
   return image_model(
     pixels=pixels,
     spot_positions=image.spots,
     spot_intensities=image.signals,
     unit_cell=i_calc.unit_cell(),
     crystal_rotation=crystal_rotation,
     miller_index_i_seqs=miller_index_i_seqs,
     scale=scale,
     i_perm=i_perm)
Exemple #55
0
def compare_fftpack_with_cmplft_3d():
  mt = flex.mersenne_twister(seed=0)
  for nx,ny,nz in [(30,20,40), (7,19,13), (5,11,4)]:
    z = (mt.random_double(size=2*nx*ny*nz)*2-1).as_float()
    cmplft_xy = z.deep_copy()
    d = flex.int([2*nx*ny*nz, 2*nx*ny, 2*nx*ny*nz, 2*nx*ny, 2])
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=nz, d=d)
    d = flex.int([2*nx*ny*nz, 2*nx, 2*nx*ny, 2*nx, 2])
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=ny, d=d)
    d = flex.int([2*nx*ny*nz, 2, 2*nx*ny*nz, 2*nx*ny*nz, 2*nx])
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=nx, d=d)
    fft = scitbx.fftpack.complex_to_complex_3d((nz,ny,nx))
    fftpack_xy = z.as_double()
    fftpack_xy.reshape(flex.grid(nz,ny,2*nx))
    fft.forward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float().as_1d()
    if (flex.max_absolute(cmplft_xy-fftpack_xy) > 1e-4):
      assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-4)
Exemple #56
0
def compare_fftpack_with_realft_1d():
  mt = flex.mersenne_twister(seed=0)
  for n_cmpl in xrange(1, 101):
    primes = prime_factors_of(n_cmpl)
    if (n_cmpl != 1 and max(primes) > 19): continue
    n_real = n_cmpl * 2
    m_real = n_real + 2
    z = (mt.random_double(size=m_real)*2-1).as_float()
    realft_xy = z.deep_copy()
    d = flex.int((m_real,2,m_real,m_real,m_real))
    ccp4io_dev_ext.fftlib_realft(xy=realft_xy, n=n_cmpl, d=d)
    fft = scitbx.fftpack.real_to_complex(n_real)
    assert fft.m_real() == m_real
    fftpack_xy = z.as_double()
    fft.forward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float()
    if (flex.max_absolute(realft_xy-fftpack_xy) > 1e-5):
      assert approx_equal(realft_xy, fftpack_xy, eps=1e-5)
Exemple #57
0
def run(args):
    assert len(args) < 3
    arg_vals = [int(arg) for arg in args]
    arg_vals = arg_vals + [3, 2][len(arg_vals) :]
    n_refl, n_trials = arg_vals
    assert n_refl > 0
    assert n_trials > 0
    if len(args) == 0:
        from libtbx.utils import null_out

        log = null_out()
    else:
        import sys

        log = sys.stdout
    mt = flex.mersenne_twister(seed=0)
    for i_trial in xrange(n_trials):
        exercise(mt, n_refl, log)
    print "OK"
def compute_image(work_params):
  dpx,dpy = work_params.detector.pixels
  from scitbx.array_family import flex
  assert work_params.noise.max > 0
  mt = flex.mersenne_twister(seed=work_params.noise.random_seed)
  image = mt.random_size_t(
    size=dpx*dpy,
    modulus=work_params.noise.max).as_int()
  image.reshape(flex.grid(dpx,dpy))
  pixels_center = None
  if (work_params.fill_beam_center):
    assert (dpx % 2) == (dpy % 2)
    if (dpx % 2 == 0):
      pixels_center = """\
 OOOO
OOOOOO
OOOOOO
OOOOOO
OOOOOO
 OOOO
"""
    else:
      pixels_center = """\
   O
 OOOOO
 OOOOO
OOOOOOO
 OOOOO
 OOOOO
   O
"""
  if (pixels_center is not None):
    lines = pixels_center.splitlines()
    n = max([len(line) for line in lines])
    oi,oj = dpx//2-n//2, dpy//2-n//2
    for i,line in enumerate(lines):
      line = line + " "*(n-len(line))
      for j,c in enumerate(line):
        if (c == "O"):
          pixel = (oi+i, oj+j)
          #print "beam center pixel:", pixel
          image[pixel] = work_params.signal_max
  return image
def exercise_with_random_arguments(n_arguments, n_iterations):
    mt = flex.mersenne_twister(seed=0)
    d = mt.random_double(size=n_arguments) * 100 - 50
    f = d.as_float()
    print "showing wall clock times!"
    t0 = time.time()
    for i_iteration in xrange(n_iterations):
        jef = scitbx.math.jacks_expf(array_of_float=f)
    print "jacks_expf(): %.2f s" % (time.time() - t0)
    for i_iteration in xrange(n_iterations):
        sef = flex.exp(f)
    print "std::exp(float): %.2f s" % (time.time() - t0)
    t0 = time.time()
    for i_iteration in xrange(n_iterations):
        sed = flex.exp(d)
    print "std::exp(double): %.2f s" % (time.time() - t0)
    assert sed.all_gt(0)
    for ef in [jef, sef]:
        max_rel_err = flex.max(flex.abs((ef.as_double() - sed) / sed))
        assert approx_equal(max_rel_err, 0, eps=1e-5)