Beispiel #1
0
def test_real_to_complex_3d(verbose):
  for nx in [3,4]:
    for ny in [4,5]:
      for nz in [7,8]:
        fft = fftpack.real_to_complex_3d((nx,ny,nz))
        m = fft.m_real()
        vd = flex.double(flex.grid(m))
        vc = flex.complex_double(flex.grid((m[0], m[1], m[2]//2)))
        assert vd.size() == 2 * vc.size()
        for i in xrange(vd.size()):
          vd[i] = random.random()*2-1
        vd_orig = vd.deep_copy()
        for i in xrange(vc.size()):
          vc[i] = complex(vd[2*i], vd[2*i+1])
        vdt = fft.forward(vd)
        vct = fft.forward(vc)
        for t in (vdt, vct):
          assert t.origin() == (0,0,0)
          assert t.all() == fft.n_complex()
          assert t.focus() == fft.n_complex()
        if (verbose): show_rseq_3d(vd, fft.m_real(), fft.m_real())
        assert_complex_eq_real(vc, vd)
        vdt = fft.backward(vd)
        vct = fft.backward(vc)
        for t in (vdt, vct):
          assert t.origin() == (0,0,0)
          assert t.all() == fft.m_real()
          assert t.focus() == fft.n_real()
        if (verbose): show_rseq_3d(vd, fft.m_real(), fft.n_real())
        assert_complex_eq_real(vc, vd)
        f = nx*ny*nz
        for ix in xrange(nx):
          for iy in xrange(ny):
            for iz in xrange(nz):
              assert approx_equal(vdt[(ix,iy,iz)], vd_orig[(ix,iy,iz)]*f)
    def tst_larger_output(self):
        from scitbx.array_family import flex

        # Set the size of the grid
        height = 10
        width = 10

        # Create the grid data
        value = 13
        grid = flex.double([value for i in range(height * width)])
        grid.reshape(flex.grid(height, width))

        # Create the grid coordinates
        xy = []
        for j in range(height + 1):
            for i in range(width + 1):
                xy.append((i * 2, j * 2))
        gridxy = flex.vec2_double(xy)
        gridxy.reshape(flex.grid(height + 1, width + 1))

        # Get the output grid
        output = regrid_irregular_grid_to_grid(grid, gridxy, (height * 2, width * 2))

        # Check that each each pixel has a value of 0.25 the input
        eps = 1e-7
        for j in range(1, height):
            for i in range(1, width):
                assert abs(output[j, i] - 0.25 * value) <= eps

        # Test passed
        print "OK"
Beispiel #3
0
def test_complex_to_complex_3d(verbose):
  fft = fftpack.complex_to_complex_3d((3,4,5))
  n = fft.n()
  vc = flex.complex_double(flex.grid(n))
  vd = flex.double(flex.grid((n[0], n[1], 2 * n[2])))
  for i in xrange(vc.size()):
    vc[i] = complex(2*i, 2*i+1)
  for i in xrange(vd.size()):
    vd[i] = i
  vct = fft.forward(vc)
  vdt = fft.forward(vd)
  for t in (vct, vdt):
    assert t.origin() == (0,0,0)
    assert t.all() == fft.n()
    assert t.focus() == fft.n()
  if (verbose): show_cseq(vc)
  assert_complex_eq_real(vc, vd)
  vct = fft.backward(vc)
  vdt = fft.backward(vd)
  for t in (vct, vdt):
    assert t.origin() == (0,0,0)
    assert t.all() == fft.n()
    assert t.focus() == fft.n()
  if (verbose): show_cseq(vc)
  assert_complex_eq_real(vc, vd)
def exercise_expand_mask():
  # case 1: standard
  cmap = flex.double(flex.grid(30,30,30))
  cmap.fill(1)
  for i in range(10,20):
    for j in range(10,20):
      for k in range(10,20):
        cmap[i,j,k] = 10
  co = maptbx.connectivity(map_data=cmap, threshold=5)
  new_mask = co.expand_mask(id_to_expand=1, expand_size=1)
  for i in range(30):
    for j in range(30):
      for k in range(30):
        assert new_mask[i,j,k] == (i in range(9,21) and
            j in range(9,21) and k in range(9,21))

  # case 2: over boundaries
  cmap = flex.double(flex.grid(30,30,30))
  cmap.fill(1)
  cmap[1,1,1] = 10
  co = maptbx.connectivity(map_data=cmap, threshold=5)
  new_mask = co.expand_mask(id_to_expand=1, expand_size=2)
  for i in range(30):
    for j in range(30):
      for k in range(30):
        assert new_mask[i,j,k] == (i in [29,0,1,2,3] and
            j in [29,0,1,2,3] and k in [29,0,1,2,3])
  def run(self):

    from scitbx.array_family import flex

    data_list = []
    mask_list = []
    for i in range(10):
      data = flex.random_int_gaussian_distribution(
          self.size[0] * self.size[1], 100, 10)
      data.reshape(flex.grid(self.size))
      mask = flex.random_bool(self.size[0] * self.size[1], 0.1)
      mask.reshape(flex.grid(self.size))
      data_list.append(data)
      mask_list.append(mask)

    for i in range(10):
      self.label_images.add_image(data_list[i], mask_list[i])

    labels = self.label_images.labels()
    coords = self.label_images.coords()
    values = self.label_images.values()

    assert(len(labels) > 0)
    assert(len(labels) == len(coords))
    assert(len(labels) == len(values))

    self.tst_coords_are_valid(mask_list, coords)
    self.tst_values_are_valid(data_list, mask_list, values)
    self.tst_labels_are_valid(data_list, mask_list, coords, labels)
  def get_mask(self, goniometer=None):
    '''Creates a mask merging untrusted pixels with active areas.'''
    from scitbx.array_family import flex
    detector_base = self.detectorbase
    # get effective active area coordinates
    tile_manager = detector_base.get_tile_manager(detector_base.horizons_phil_cache)
    tiling = tile_manager.effective_tiling_as_flex_int(reapply_peripheral_margin = True)
    # get the raw data to get the size of the mask
    data = self.get_raw_data()
    if tiling is None or len(tiling) == 0:
      return None

    # set the mask to the same dimensions as the data
    mask = flex.bool(flex.grid(data.focus()))

    # set active areas to True so they are not masked
    for i in xrange(len(tiling)//4):
      x1,y1,x2,y2=tiling[4*i:(4*i)+4]
      sub_array = flex.bool(flex.grid(x2-x1,y2-y1),True)
      mask.matrix_paste_block_in_place(sub_array,x1,y1)

    # create untrusted pixel mask
    detector = self.get_detector()
    assert len(detector) == 1
    trusted_mask = detector[0].get_trusted_range_mask(data)

    # returns merged untrusted pixels and active areas using bitwise AND (pixels are accepted
    # if they are inside of the active areas AND inside of the trusted range)
    return (mask & trusted_mask,)
Beispiel #7
0
    def _to_array_all(self, panel=0):
        """ Get the array from all the sweep elements. """

        from scitbx.array_family import flex

        # Get the image dimensions
        size_z = len(self)
        size_y = self.reader().get_image_size(panel)[1]
        size_x = self.reader().get_image_size(panel)[0]

        # Check sizes are valid
        if size_z <= 0 or size_y <= 0 or size_x <= 0:
            raise RuntimeError("Invalid dimensions")

        # Allocate the array
        array = flex.int(flex.grid(size_z, size_y, size_x))

        # Loop through all the images and set the image data
        for k, image in enumerate(self):
            if not isinstance(image, tuple):
                image = (image,)
            im = image[panel]
            im.reshape(flex.grid(1, *im.all()))
            array[k : k + 1, :, :] = im

        # Return the array
        return array
Beispiel #8
0
    def _to_array_w_range(self, item, panel=0):
        """ Get the array from the user specified range. """
        from scitbx.array_family import flex

        # Get the range from the given index item
        z0, z1, y0, y1, x0, x1 = self._get_data_range(item, panel)

        # Get the image dimensions
        size_z = z1 - z0
        size_y = y1 - y0
        size_x = x1 - x0

        # Check sizes are valid
        if size_z <= 0 or size_y <= 0 or size_x <= 0:
            raise RuntimeError("Invalid dimensions")

        # Allocate the array
        array = flex.int(flex.grid(size_z, size_y, size_x))

        # Loop through all the images and set the image data
        for k, index in enumerate(self.indices()[z0:z1]):
            image = self.reader().read(index)
            if not isinstance(image, tuple):
                image = (image,)
            im = image[panel]
            im.reshape(flex.grid(1, *im.all()))
            array[k : k + 1, :, :] = im[0:1, y0:y1, x0:x1]

        # Return the array
        return array
  def tst_with_flat_background_partial(self):

    from dials.algorithms.integration.fit import fit_profile
    from scitbx.array_family import flex
    from tst_profile_helpers import gaussian

    # Create profile
    p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    s = flex.sum(p)
    p = p / s

    # Copy profile
    c0 = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    b = flex.double(flex.grid(9, 9, 9), 5)
    m = flex.bool(flex.grid(9,9,9), True)
    c = c0 + b

    # Get the partial profiles
    pp = p[0:5,:,:]
    mp = m[0:5,:,:]
    cp = c[0:5,:,:]
    bp = b[0:5,:,:]
    c0p = c0[0:5,:,:]

    # Fit
    fit = fit_profile(pp, mp, cp, bp)
    I = fit.intensity()
    V = fit.variance()

    # Test intensity is the same
    eps = 1e-7
    assert(abs(I - flex.sum(c0)) < eps)
    assert(abs(V - (flex.sum(c0p) + flex.sum(bp))) < eps)

    print 'OK'
def exercise_int():
  fa = flex.int_range(1,7)
  na = fa.as_numpy_array()
  assert na.tolist() == list(fa)
  fna = flex.int(na)
  assert fna.all() == (6,)
  assert fna.origin() == (0,)
  assert fna.focus() == (6,)
  assert fna.all_eq(fa)
  fa[0] = 99
  assert na[0] == 1
  fa[0] = 1
  #
  fa.reshape(flex.grid(2,3))
  na = fa.as_numpy_array()
  assert na.tolist() == [[1, 2, 3], [4, 5, 6]]
  fna = flex.int(na)
  assert fna.all() == (2,3)
  assert fna.origin() == (0,0)
  assert fna.focus() == (2,3)
  assert fna.all_eq(fa)
  #
  fa = flex.int_range(4*2*3) + 1
  fa.reshape(flex.grid(4,2,3))
  na = fa.as_numpy_array()
  assert na.tolist() == [
    [[1, 2, 3], [4, 5, 6]],
    [[7, 8, 9], [10, 11, 12]],
    [[13, 14, 15], [16, 17, 18]],
    [[19, 20, 21], [22, 23, 24]]]
  fna = flex.int(na)
  assert fna.all() == (4,2,3)
  assert fna.origin() == (0,0,0)
  assert fna.focus() == (4,2,3)
  assert fna.all_eq(fa)
  def tst_with_flat_background(self):

    from dials.algorithms.integration.fit import fit_profile
    from scitbx.array_family import flex
    from tst_profile_helpers import gaussian

    # Create profile
    p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    s = flex.sum(p)
    p = p / s

    # Copy profile
    c0 = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    b = flex.double(flex.grid(9, 9, 9), 5)
    m = flex.bool(flex.grid(9,9,9), True)
    c = c0 + b

    # Fit
    fit = fit_profile(p, m, c, b)
    I = fit.intensity()
    V = fit.variance()

    # Test intensity is the same
    eps = 1e-7
    assert(abs(I - flex.sum(c0)) < eps)
    assert(abs(V - (flex.sum(c0) + flex.sum(b))) < eps)

    print 'OK'
Beispiel #12
0
def exercise_inverse():

  #standard 2x2 matrix
  a = flex.double( flex.grid(2,2), 0.0 )
  a[(0,0)] = 1
  a[(0,1)] = 2
  a[(1,0)] = 3
  a[(1,1)] = 4
  result,sigma = scitbx.linalg.svd.inverse_via_svd(a)
  assert approx_equal(result[(0,0)],-2.0,eps=1e-6)
  assert approx_equal(result[(0,1)], 1.0,eps=1e-6)
  assert approx_equal(result[(1,0)], 1.5,eps=1e-6)
  assert approx_equal(result[(1,1)],-0.5,eps=1e-6)
  uu = a.matrix_multiply(result)

  for ii in range(10):
    n = 30
    # random matrix
    a = flex.double( flex.grid(n,n),0 )
    for aa in range(n):
      for bb in range(aa,n):
        tmp = random.random()
        a[(aa,bb)]=tmp
        a[(bb,aa)]=tmp

    result,sigma = scitbx.linalg.svd.inverse_via_svd(a.deep_copy(), 1e-6)
    uu = a.matrix_multiply(result)
    for aa in range(n):
      for bb in range(n):
        tmp=0
        if aa==bb: tmp=1.0
        assert approx_equal( uu[(aa,bb)], tmp,1e-3 )
    def tst_larger_input(self):
        from scitbx.array_family import flex

        # Set the size of the grid
        input_height = 20
        input_width = 20
        output_height = 10
        output_width = 10

        # Create the grid data
        value = 13
        grid = flex.double([value for i in range(input_height * input_width)])
        grid.reshape(flex.grid(input_height, input_width))

        # Create the grid coordinates
        xy = []
        for j in range(input_height + 1):
            for i in range(input_width + 1):
                xy.append((i / 2.0, j / 2.0))
        gridxy = flex.vec2_double(xy)
        gridxy.reshape(flex.grid(input_height + 1, input_width + 1))

        # Get the output grid
        output = regrid_irregular_grid_to_grid(grid, gridxy, (output_height, output_width))

        # Check that each each pixel has a value of 4 times the input
        eps = 1e-7
        for j in range(1, output_height):
            for i in range(1, output_width):
                assert abs(output[j, i] - 4 * value) <= eps

        # Test passed
        print "OK"
Beispiel #14
0
def tst_robust_glm():
  from scitbx.glmtbx import robust_glm
  from scitbx.array_family import flex
  from numpy.random import poisson, seed
  from math import exp

  seed(0)

  n_obs = 100

  for c in range(1, 100):

    # Test for a constant value
    X = flex.double([1 for i in range(n_obs)])
    X.reshape(flex.grid(n_obs, 1))
    Y = flex.double(list(poisson(c, n_obs)))
    B = flex.double([0])
    result = robust_glm(X, Y, B, family="poisson", max_iter=100)
    assert(abs(c - exp(result.parameters()[0])) < 0.1*c)

  # Now test with a massive outlier
  for c in range(1, 100):

    # Test for a constant value
    X = flex.double([1 for i in range(n_obs)])
    X.reshape(flex.grid(n_obs, 1))
    Y = flex.double(list(poisson(c, n_obs)))
    Y[n_obs//2] = c * 100
    B = flex.double([0])
    result = robust_glm(X, Y, B, family="poisson", max_iter=100)
    assert(abs(c - exp(result.parameters()[0])) < 0.1*c)

  print 'OK'
Beispiel #15
0
    def generate_linear_background_2d(self, size, bmin, bmax):
        from random import uniform
        from scitbx.array_family import flex
        from scitbx import matrix

        slice_size = (1, size[1], size[2])
        data = flex.double(flex.grid(size), 0)
        mask = flex.bool(flex.grid(size), True)
        params = []
        for k in range(size[0]):
            a00 = uniform(bmin, bmax)
            a01 = uniform(bmin, bmax)
            a10 = uniform(bmin, bmax)
            p00 = matrix.col((0.5, 0.5, a00))
            p01 = matrix.col((8.5, 0.5, a01))
            p10 = matrix.col((0.5, 8.5, a10))
            n = (p01 - p00).cross(p10 - p00)
            b = n[0]
            c = n[1]
            d = n[2]
            a = -(b * 0.5 + c * 0.5 + d * a00)
            a /= -d
            b /= -d
            c /= -d
            for j in range(size[1]):
                for i in range(size[2]):
                    data[k, j, i] = a + b * (i + 0.5) + c * (j + 0.5)
            eps = 1e-7
            assert abs(data[k, 0, 0] - a00) < eps
            assert abs(data[k, 8, 0] - a10) < eps
            assert abs(data[k, 0, 8] - a01) < eps
            params.append((a, b, c))
        return params, data, mask
  def tst_identical(self):

    from dials.algorithms.integration.fit import fit_profile
    from scitbx.array_family import flex
    from tst_profile_helpers import gaussian

    # Create profile
    p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    s = flex.sum(p)
    p = p / s

    # Copy profile
    c = p.deep_copy()
    b = flex.double(flex.grid(9, 9, 9), 0)
    m = flex.bool(flex.grid(9,9,9), True)

    # Fit
    fit = fit_profile(p, m, c, b)
    I = fit.intensity()
    V = fit.variance()

    # Test intensity is the same
    eps = 1e-7
    assert(abs(I - flex.sum(p)) < eps)
    assert(abs(V - I) < eps)

    print 'OK'
Beispiel #17
0
  def run(self):
    from dials.algorithms.image.fill_holes import simple_fill
    from scitbx.array_family import flex
    from random import randint
    from math import sqrt
    import sys

    mask = flex.bool(flex.grid(100, 100), True)
    data = flex.double(flex.grid(100, 100), True)

    for j in range(100):
      for i in range(100):
        data[j,i] = 10 + j * 0.01 + i * 0.01
        if sqrt((j - 50)**2 + (i - 50)**2) <= 10.5:
          mask[j,i] = False
          data[j,i] = 0

    result = simple_fill(data, mask)
    known = data.as_1d().select(mask.as_1d())
    filled = result.as_1d().select(mask.as_1d() == False)
    assert flex.max(filled) <= flex.max(known)
    assert flex.min(filled) >= flex.min(known)


    # Test passed
    print 'OK'
def exercise_c_grid_conversions(verbose=0):
  if (verbose): print 'Checking flex->ref_c_grid conversions'
  a = flex.double(flex.grid((2, 3)))
  assert a.accessor() == rt.use_const_ref_c_grid_2(a)
  assert a.all() == rt.use_const_ref_c_grid_2(a).all()
  assert a.accessor().all() == rt.use_const_ref_c_grid_padded_2(a).all()
  assert a.accessor().all() == rt.use_const_ref_c_grid_padded_2(a).focus()
  a = flex.double(flex.grid((2, 3, 4)))
  assert a.accessor() == rt.use_const_ref_c_grid_3(a)
  assert a.all() == rt.use_const_ref_c_grid_3(a).all()
  assert a.accessor().all() == rt.use_const_ref_c_grid_padded_3(a).all()
  assert a.accessor().all() == rt.use_const_ref_c_grid_padded_3(a).focus()
  a = flex.double(flex.grid((2, 3, 5)).set_focus((2, 3, 4)))
  assert a.accessor().all() == rt.use_const_ref_c_grid_padded_3(a).all()
  assert a.accessor().focus() == rt.use_const_ref_c_grid_padded_3(a).focus()
  assert a.is_0_based()
  assert a.is_padded()
  b = flex.double(flex.grid((1,2,3), (4,6,8)))
  assert not b.is_0_based()
  assert not b.is_padded()
  for c in [a,b]:
    try: rt.use_const_ref_c_grid_3(c)
    except KeyboardInterrupt: raise
    except Exception, e:
      assert str(e).startswith("Python argument types in")
    else: raise RuntimeError("Boost.Python.ArgumentError expected.")
    def tst_identical(self):
        from scitbx.array_family import flex
        from random import uniform

        # Set the size of the grid
        height = 10
        width = 10

        # Create the grid data
        grid = flex.double([uniform(0, 100) for i in range(height * width)])
        grid.reshape(flex.grid(height, width))

        # Create the grid coordinates
        xy = []
        for j in range(height + 1):
            for i in range(width + 1):
                xy.append((i, j))
        gridxy = flex.vec2_double(xy)
        gridxy.reshape(flex.grid(height + 1, width + 1))

        # Get the output grid
        output = regrid_grid_to_irregular_grid(grid, gridxy)

        # Check that each pixel is identical
        eps = 1e-7
        for j in range(height):
            for i in range(width):
                assert abs(output[j, i] - grid[j, i]) <= eps

        # Test passed
        print "OK"
Beispiel #20
0
    def generate_constant_background_3d(self, size, bmin, bmax):
        from random import uniform
        from scitbx.array_family import flex

        c = uniform(bmin, bmax)
        data = flex.double(flex.grid(size), c)
        mask = flex.bool(flex.grid(size), True)
        return c, data, mask
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 initialization(self):
   self.a = flex.double(flex.grid(self.n, self.n), -2./self.m)
   self.a.matrix_diagonal_add_in_place(value=1)
   self.a.resize(flex.grid(self.m,self.n), -2./self.m)
   self.x0 = flex.double(self.n, 1)
   self.tau0 = 1.e-8
   self.delta0 = 10
   self.x_star = flex.double(self.n, -1)
   self.capital_f_x_star = 0.5 * (self.m - self.n)
Beispiel #23
0
def exercise_svd_basic():
  a = flex.double(xrange(1,19))
  sigma = [ 45.8945322027251, 1.6407053035305987, 0 ]
  a.resize(flex.grid(6,3))
  svd = scitbx.linalg.svd.real(
    a.deep_copy(),
    accumulate_u=True,
    accumulate_v=True)
  assert approx_equal(svd.sigma, sigma)
  a1 = svd.reconstruct()
  assert matrix_equality_ratio(a, a1) < 10
  assert matrix_normality_ratio(svd.u) < 10
  assert matrix_normality_ratio(svd.v) < 10
  svd = scitbx.linalg.svd.real(a.deep_copy(),
                             accumulate_u=False, accumulate_v=False)
  assert approx_equal(svd.sigma, sigma)
  assert not svd.u and not svd.v
  try:
    svd.reconstruct()
    raise Exception_expected
  except AssertionError:
    pass

  a = a.matrix_transpose()
  svd = scitbx.linalg.svd.real(
    a.deep_copy(),
    accumulate_u=True,
    accumulate_v=True)
  assert approx_equal(svd.sigma, sigma)
  a1 = svd.reconstruct()
  assert matrix_equality_ratio(a, a1) < 10
  assert matrix_normality_ratio(svd.u) < 10
  assert matrix_normality_ratio(svd.v) < 10

  a = flex.double(xrange(1,13))
  sigma = [25.436835633480246818, 1.7226122475210637387, 0]
  a.reshape(flex.grid(3,4))
  svd = scitbx.linalg.svd.real(
    a.deep_copy(),
    accumulate_u=True,
    accumulate_v=True)
  assert approx_equal(svd.sigma, sigma)
  a1 = svd.reconstruct()
  assert matrix_equality_ratio(a, a1) < 10
  assert matrix_normality_ratio(svd.u) < 10
  assert matrix_normality_ratio(svd.v) < 10

  a = a.matrix_transpose()
  svd = scitbx.linalg.svd.real(
    a.deep_copy(),
    accumulate_u=True,
    accumulate_v=True)
  assert approx_equal(svd.sigma, sigma)
  a1 = svd.reconstruct()
  assert matrix_equality_ratio(a, a1) < 10
  assert matrix_normality_ratio(svd.u) < 10
  assert matrix_normality_ratio(svd.v) < 10
Beispiel #24
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"]))
Beispiel #25
0
def run():
  print __doc__
  g = grid((3,4,6))
  show(g)
  g.set_focus((3,4,5))
  show(g)
  g = grid((-2,-3,-4), (3,4,6))
  show(g)
  g.set_focus((3,4,5))
  show(g)
  print "OK"
Beispiel #26
0
def generate_dials_corrections(image_filename, sensor_thickness_mm,
                               energy_ev = None, method=compute_absolute_offset):
  '''Generate equivalent correction tables equivalent to those from XDS, but using
  the equations above.'''

  from dxtbx import load
  from scitbx import matrix
  from scitbx.array_family import flex
  import math
  import random
  import os

  image = load(image_filename)

  beam = matrix.col(image.get_beam().get_s0()).normalize()

  if energy_ev:
    energy_kev = energy_ev * 0.001
  else:
    wavelength = image.get_beam().get_wavelength()
    energy_kev = 12.3985 / wavelength

  mu = derive_absorption_coefficient_Si(energy_kev)
  d = image.get_detector()[0]
  fast = matrix.col(d.get_fast_axis())
  slow = matrix.col(d.get_slow_axis())
  normal = matrix.col(d.get_normal())
  origin = matrix.col(d.get_origin())
  distance = origin.dot(normal)
  offset = distance * normal - origin
  offset_fast = offset.dot(fast)
  offset_slow = offset.dot(slow)
  pixel_size = d.get_pixel_size()

  # this is in order slow, fast i.e. C order
  image_size = image.get_raw_data().focus()

  F = fast * pixel_size[0]
  S = slow * pixel_size[1]

  fast_parallax = flex.double(flex.grid(image_size))
  slow_parallax = flex.double(flex.grid(image_size))

  for i in range(image_size[0]):
    for j in range(image_size[1]):
      p = (origin + i * S + j * F).normalize()
      theta = p.angle(normal)
      dot_f = - p.dot(fast)
      dot_s = - p.dot(slow)
      offset = method(sensor_thickness_mm, theta, mu)
      fast_parallax[i, j] = dot_f * offset
      slow_parallax[i, j] = dot_s * offset

  return fast_parallax, slow_parallax
Beispiel #27
0
 def slice_callback_with_high_performance_http_data(self):
   BYU = self.object.read()
   linearintdata = flex.int_from_byte_str(BYU)
   provisional_size = linearintdata.size()
   if provisional_size==480285:
     linearintdata.reshape(flex.grid((195,2463)))
   elif provisional_size==6224001:
     linearintdata.reshape(flex.grid((2527,2463)))
   else:
     raise ImageException("wrong number of pixels for Pilatus image")
   del self.object #once the data are copied, close the stream
   return linearintdata
Beispiel #28
0
    def __init__(self):
        from scitbx.array_family import flex

        # Create an image
        self.image = flex.random_double(2000 * 2000)
        self.image.reshape(flex.grid(2000, 2000))
        self.mask = flex.random_bool(2000 * 2000, 0.99)
        self.mask.reshape(flex.grid(2000, 2000))
        self.gain = flex.random_double(2000 * 2000) + 0.5
        self.gain.reshape(flex.grid(2000, 2000))
        self.size = (3, 3)
        self.min_count = 2
  def __init__(O, points, epsilon=1e-2):
    """\
Computation of Minimum-Volume Covering Ellipsoid using the Khachiyan Algorithm.
Based on a Python implementation by Raj Rajashankar (ANL, Nov 2011),
which in turn was based on a Matlab script by Nima Moshtagh (2009,
http://stackoverflow.com/questions/1768197/bounding-ellipse/1768440#1768440).

Caveats:
  - center and radii are correct, but rotation may permute axes
  - scales with the square of the number of points
"""
    d = 3 # d is the dimension
    n = points.size()
    assert n > 0
    #
    from scitbx.array_family import flex
    p = points.as_double()
    p.reshape(flex.grid(n, 3))
    p = p.matrix_transpose()
    q = p.deep_copy()
    q.resize(flex.grid(4, n), 1)
    #
    u = flex.double(n, 1/n)
    umx = flex.double(flex.grid(n, n), 0)
    #
    err = epsilon + 1
    while (err > epsilon):
      umx.matrix_diagonal_set_in_place(u)
      x_inv = q.matrix_multiply(umx).matrix_multiply_transpose(
        q).matrix_inversion()
      m = q.matrix_transpose_multiply(
        x_inv).matrix_multiply(q).matrix_diagonal()
      j = flex.max_index(m)
      maximum = m[j]
      ascent_step_size = (maximum-d-1)/((d+1)*(maximum-1))
      new_u = (1 - ascent_step_size) * u
      new_u[j] += ascent_step_size
      err = flex.sum_sq(new_u - u)**0.5
      u = new_u
    #
    center = p.matrix_multiply(u)
    umx.matrix_diagonal_set_in_place(u)
    t1 = p.matrix_multiply(umx).matrix_multiply_transpose(p)
    t2 = center.matrix_outer_product(center)
    a = (t1 - t2).matrix_inversion() / d
    #
    import scitbx.linalg.svd
    svd = scitbx.linalg.svd.real(a, accumulate_u=False, accumulate_v=True)
    size = 1.0/flex.sqrt(svd.sigma)
    from scitbx import matrix
    O.center = matrix.col(center)
    O.radii = matrix.col(size)
    O.rotation = matrix.sqr(svd.v)
Beispiel #30
0
 def psana_mask_to_dials_mask(self, psana_mask):
   if psana_mask.dtype == np.bool:
     psana_mask = flex.bool(psana_mask)
   else:
     psana_mask = flex.bool(psana_mask == 1)
   assert psana_mask.focus() == (32, 185, 388)
   dials_mask = []
   for i in xrange(32):
     dials_mask.append(psana_mask[i:i+1,:,:194])
     dials_mask[-1].reshape(flex.grid(185,194))
     dials_mask.append(psana_mask[i:i+1,:,194:])
     dials_mask[-1].reshape(flex.grid(185,194))
   return dials_mask
Beispiel #31
0
def _average_transformation(matrices, keys):
    """The _average_transformation() function determines the average
  rotation and translation from the transformation matrices in @p
  matrices with keys matching @p keys.  The function returns a
  two-tuple of the average rotation in quaternion representation and
  the average translation.

  XXX Alternative: use average of normals, weighted by element size,
  to determine average orientation.
  """

    from scitbx.array_family import flex
    from tntbx import svd

    # Sum all rotation matrices and translation vectors.
    sum_R = flex.double(flex.grid(3, 3))
    sum_t = flex.double(flex.grid(3, 1))
    nmemb = 0
    for key in keys:
        T = matrices[key][1]
        sum_R += flex.double((T(0, 0), T(0, 1), T(0, 2), T(1, 0), T(1, 1),
                              T(1, 2), T(2, 0), T(2, 1), T(2, 2)))
        sum_t += flex.double((T(0, 3), T(1, 3), T(2, 3)))
        nmemb += 1
    if nmemb == 0:
        # Return zero-rotation and zero-translation.
        return (matrix.col((1, 0, 0, 0)), matrix.zeros((3, 1)))

    # Calculate average rotation matrix as U * V^T where sum_R = U * S *
    # V^T and S diagonal (Curtis et al. (1993) 377-385 XXX proper
    # citation, repeat search), and convert to quaternion.
    svd = svd(sum_R)
    R_avg = matrix.sqr(list(svd.u().matrix_multiply_transpose(svd.v())))
    o_avg = R_avg.r3_rotation_matrix_as_unit_quaternion()
    t_avg = matrix.col(list(sum_t / nmemb))

    return (o_avg, t_avg)
def test_experimentlist_dumper_dump_scan_varying(dials_regression, tmpdir):
    tmpdir.chdir()
    os.environ['DIALS_REGRESSION'] = dials_regression

    # Get all the filenames
    filename1 = os.path.join(dials_regression, 'experiment_test_data',
                             'experiment_1.json')

    # Read the experiment list in
    elist1 = ExperimentListFactory.from_json_file(filename1)

    # Make trivial scan-varying models
    crystal = elist1[0].crystal
    beam = elist1[0].beam
    goniometer = elist1[0].goniometer
    crystal.set_A_at_scan_points([crystal.get_A()] * 5)
    from scitbx.array_family import flex
    cov_B = flex.double([1e-5] * 9 * 9)
    crystal.set_B_covariance(cov_B)
    cov_B.reshape(flex.grid(1, 9, 9))
    cov_B_array = flex.double(flex.grid(5, 9, 9))
    for i in range(5):
        cov_B_array[i:(i + 1), :, :] = cov_B
    crystal.set_B_covariance_at_scan_points(cov_B_array)

    beam.set_s0_at_scan_points([beam.get_s0()] * 5)
    goniometer.set_setting_rotation_at_scan_points(
        [goniometer.get_setting_rotation()] * 5)

    # Create the experiment list dumper
    dump = ExperimentListDumper(elist1)

    # Dump as JSON file and reload
    filename = 'temp.json'
    dump.as_json(filename)
    elist2 = ExperimentListFactory.from_json_file(filename)
    check(elist1, elist2)
Beispiel #33
0
def test_with_no_background_partial():

    from dials.algorithms.integration.fit import ProfileFitter
    from scitbx.array_family import flex
    from numpy.random import seed

    seed(0)

    # Create profile
    p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    s = flex.sum(p)
    p = p / s

    # Copy profile
    c = add_poisson_noise(100 * p)
    b = flex.double(flex.grid(9, 9, 9), 0)
    m = flex.bool(flex.grid(9, 9, 9), True)

    # Get the partial profiles
    pp = p[0:5, :, :]
    mp = m[0:5, :, :]
    cp = c[0:5, :, :]
    bp = b[0:5, :, :]

    # Fit
    fit = ProfileFitter(cp, bp, mp, pp)
    I = fit.intensity()
    V = fit.variance()
    assert fit.niter() < fit.maxiter()

    Iknown = 94.67151440319306
    Vknown = 94.67151440319306

    # Test intensity is the same
    eps = 1e-7
    assert abs(I[0] - Iknown) < eps
    assert abs(V[0] - Vknown) < eps
Beispiel #34
0
def test_masked_mean_and_variance_filter():
    from scitbx.array_family import flex

    from dials.algorithms.image.filter import mean_and_variance_filter

    # Create an image
    image = flex.random_double(2000 * 2000)
    image.reshape(flex.grid(2000, 2000))
    mask = flex.random_bool(2000 * 2000, 0.99).as_int()
    mask.reshape(flex.grid(2000, 2000))

    # Calculate the summed area table
    mask2 = mask.deep_copy()
    mv = mean_and_variance_filter(image, mask2, (3, 3), 2)
    mean = mv.mean()
    var = mv.sample_variance()

    # For a selection of random points, ensure that the value is the
    # sum of the area under the kernel
    eps = 1e-7
    for i in range(10000):
        i = random.randint(10, 1990)
        j = random.randint(10, 1990)
        m1 = mean[j, i]
        v1 = var[j, i]
        p = image[j - 3:j + 4, i - 3:i + 4]
        m = mask[j - 3:j + 4, i - 3:i + 4]
        if mask[j, i] == 0:
            m2 = 0.0
            v2 = 0.0
        else:
            p = flex.select(p, flags=m)
            mv = flex.mean_and_variance(flex.double(p))
            m2 = mv.mean()
            v2 = mv.unweighted_sample_variance()
        assert m1 == pytest.approx(m2, abs=eps)
        assert v1 == pytest.approx(v2, abs=eps)
Beispiel #35
0
def exercise_complex_to_complex_3d():
    from scitbx.array_family import flex
    from cudatbx import cufft
    from scitbx import fftpack
    import time
    import sys
    print ""
    print "complex_to_complex_3d"
    for n_complex, n_repeats in [((100, 80, 90), 16), ((200, 160, 180), 16)]:
        print "  dimensions:", n_complex
        print "  repeats:", n_repeats
        np = n_complex[0] * n_complex[1] * n_complex[2]
        d0 = flex.polar(
            flex.random_double(size=np) * 2 - 1,
            flex.random_double(size=np) * 2 - 1)
        d0.reshape(flex.grid(n_complex))
        #
        t0 = time.time()
        for i_trial in xrange(n_repeats):
            d = d0.deep_copy()
        overhead = time.time() - t0
        print "    overhead: %.2f seconds" % overhead
        #
        # XXX extra CuFFT to initialize device - can we avoid this somehow?
        d = d0.deep_copy()
        cufft.complex_to_complex_3d(n_complex).forward(d)
        cufft.complex_to_complex_3d(n_complex).backward(d)
        # benchmarking run
        t0 = time.time()
        for i_trial in xrange(n_repeats):
            d = d0.deep_copy()
            cufft.complex_to_complex_3d(n_complex).forward(d)
            cufft.complex_to_complex_3d(n_complex).backward(d)
        print "    cufft:    %6.2f seconds" % (
            (time.time() - t0 - overhead) / n_repeats)
        rw = d / np
        #
        t0 = time.time()
        for i_trial in xrange(n_repeats):
            d = d0.deep_copy()
            fftpack.complex_to_complex_3d(n_complex).forward(d)
            fftpack.complex_to_complex_3d(n_complex).backward(d)
        print "    fftpack:  %6.2f seconds" % (
            (time.time() - t0 - overhead) / n_repeats)
        sys.stdout.flush()
        rp = d / np
        #
        print ""
        assert flex.max(flex.abs(rw - rp)) < 1.e-6
def gaussian(size, a, x0, sx):
    from scitbx.array_family import flex

    result = flex.double(flex.grid(size))

    index = [0] * len(size)
    while True:
        result[index] = evaluate_gaussian(index, a, x0, sx)
        for j in range(len(size)):
            index[j] += 1
            if index[j] < size[j]:
                break
            index[j] = 0
            if j == len(size) - 1:
                return result
Beispiel #37
0
    def constrain_jacobian(self, jacobian):

        # set up result matrix
        nrow = jacobian.all()[0]
        ncol = self._n_unconstrained_params + len(self._constraints)
        constrained_jacobian = flex.double(flex.grid(nrow, ncol))

        # create constrained columns
        constr_block = flex.double(flex.grid(nrow, len(self._constraints)))
        for i, gp in enumerate(self._constrained_gps):
            cols = [jacobian.matrix_copy_column(j) for j in gp]
            sum_col = reduce(lambda x, y: x + y, cols)
            constr_block.matrix_paste_column_in_place(sum_col, i)

        # copy unconstrained columns into the result
        for i, j in enumerate(self._unconstrained_idx):
            col = jacobian.matrix_copy_column(j)
            constrained_jacobian.matrix_paste_column_in_place(col, i)

        # copy the constrained block into the result
        constrained_jacobian.matrix_paste_block_in_place(
            constr_block, 0, self._n_unconstrained_params)

        return constrained_jacobian
Beispiel #38
0
    def test_dispersion_extended_threshold(self):
        from dials.algorithms.image.threshold import (
            DispersionExtendedThreshold,
            DispersionExtendedThresholdDebug,
        )
        from dials.array_family import flex

        nsig_b = 3
        nsig_s = 3
        algorithm = DispersionExtendedThreshold(
            self.image.all(), self.size, nsig_b, nsig_s, 0, self.min_count
        )
        result1 = flex.bool(flex.grid(self.image.all()))
        result2 = flex.bool(flex.grid(self.image.all()))
        algorithm(self.image, self.mask, result1)
        algorithm(self.image, self.mask, self.gain, result2)

        debug = DispersionExtendedThresholdDebug(
            self.image, self.mask, self.size, nsig_b, nsig_s, 0, self.min_count
        )
        result3 = debug.final_mask()

        assert result1.all_eq(result3)

        debug = DispersionExtendedThresholdDebug(
            self.image,
            self.mask,
            self.gain,
            self.size,
            nsig_b,
            nsig_s,
            0,
            self.min_count,
        )
        result4 = debug.final_mask()
        assert result2 == result4
Beispiel #39
0
def exercise_singular_matrix(klass):
    n = 20
    m = 3 * n
    tol = 10 * scitbx.math.double_numeric_limits.epsilon
    rows = [flex.random_double(m) for i in range(n - 2)]
    rows.append(rows[n // 2] + rows[n // 3])
    rows.append(rows[n // 4] - rows[n // 5])
    random.shuffle(rows)
    a = flex.double()
    for r in rows:
        a.extend(r)
    a.reshape(flex.grid(n, m))
    a = a.matrix_transpose()
    svd = klass(a.deep_copy(), accumulate_u=True, accumulate_v=True)
    assert svd.numerical_rank(svd.sigma[0] * tol) == n - 2
Beispiel #40
0
 def jacobian_analytical(self, x):
   x1,x2,x3,x4 = x
   result = flex.double()
   for ui in kowalik_and_osborne_function.us:
     denominator = ui*(ui+x3)+x4
     assert denominator != 0
     denominator_sq = denominator**2
     assert denominator_sq != 0
     result.extend(flex.double([
       -ui*(ui+x2)/denominator,
       -ui*x1/denominator,
       ui**2*x1*(ui+x2)/denominator_sq,
       ui*x1*(ui+x2)/denominator_sq]))
   result.resize(flex.grid(self.m, self.n))
   return result
Beispiel #41
0
 def _construct_grad_block(self, param_grads, i):
     """helper function to construct a block of gradients. The length of
     param_grads is the number of columns of the block. i selects a row of
     interest from the block corresponding to the residual for a particular
     unit cell"""
     mean_grads = param_grads * self._meangradfac
     param_grads *= self._gradfac
     block = sparse.matrix(self._nxls, len(param_grads))
     for j, (g, mg) in enumerate(zip(param_grads, mean_grads)):
         if abs(mg) > 1e-20:  # skip gradients close to zero
             col = flex.double(flex.grid(self._nxls, 1), -1.0 * mg)
             block.assign_block(col, 0, j)
         if abs(g) > 1e-20:  # skip gradient close to zero
             block[i, j] = g
     return block
Beispiel #42
0
def cov(*args):
    """Calculate covariance matrix between the arguments (should be flex.double
    arrays of equal length)"""

    lens = [len(e) for e in args]
    assert all(e == lens[0] for e in lens)

    ncols = len(args)
    cov = flex.double(flex.grid(ncols, ncols))
    for i in range(ncols):
        for j in range(i, ncols):
            cov[i, j] = sample_covariance(args[i], args[j])

    cov.matrix_copy_upper_to_lower_triangle_in_place()
    return cov
Beispiel #43
0
    def run(self):

        from dxtbx.format.image import ImageTileInt
        from scitbx.array_family import flex

        data = flex.int(flex.grid(10, 10))
        name = "TileName"

        tile = ImageTileInt(data, name)

        assert tile.data().all_eq(data)
        assert tile.name() == name
        assert tile.empty() == False

        print "OK"
Beispiel #44
0
    def test_conservation_of_counts():
        from scitbx.array_family import flex
        from scitbx import matrix
        from math import sin, cos, pi

        # Set the size of the grid
        input_height = 10
        input_width = 10
        output_height = 50
        output_width = 50

        # Create the grid data
        grid = flex.double([
            random.uniform(0, 100) for i in range(input_height * input_width)
        ])
        grid.reshape(flex.grid(input_height, input_width))

        # Create the grid coordinates
        xy = []
        angle = random.uniform(0, pi)
        R = matrix.sqr((cos(angle), -sin(angle), sin(angle), cos(angle)))
        for j in range(output_height + 1):
            for i in range(output_width + 1):
                ij = R * matrix.col((i, j))
                xy.append((ij[0], ij[1]))
        gridxy = flex.vec2_double(xy)
        gridxy.reshape(flex.grid(output_height + 1, output_width + 1))
        off = gridxy[output_height // 2, output_width // 2]
        for i in range(len(gridxy)):
            gridxy[i] = (gridxy[i][0] - off[0], gridxy[i][1] - off[1])

        # Get the output grid
        output = regrid_grid_to_irregular_grid(grid, gridxy)
        # Check that the sum of the counts is conserved
        eps = 1e-7
        assert abs(flex.sum(output) - flex.sum(grid)) <= eps
Beispiel #45
0
    def test_known_orientation():
        from scitbx.array_family import flex
        from scitbx import matrix
        from math import sin, cos, pi, sqrt

        # Set the size of the grid
        input_height = 4
        input_width = 4
        output_height = 4
        output_width = 4

        # Create the grid data
        value = 13
        grid = flex.double([value for i in range(input_height * input_width)])
        grid.reshape(flex.grid(input_height, input_width))

        # Create the grid coordinates
        xy = []
        R = matrix.sqr((cos(pi / 4), -sin(pi / 4), sin(pi / 4), cos(pi / 4)))
        for j in range(output_height + 1):
            for i in range(output_width + 1):
                ij = R * matrix.col((i * sqrt(8) / 2, j * sqrt(8) / 2))
                xy.append((ij[0] + 2, ij[1] - 2))
        gridxy = flex.vec2_double(xy)
        gridxy.reshape(flex.grid(output_height + 1, output_width + 1))

        # Get the output grid
        output = regrid_grid_to_irregular_grid(grid, gridxy)

        expected = [[0, 1, 1, 0], [1, 2, 2, 1], [1, 2, 2, 1], [0, 1, 1, 0]]

        # Check that each each pixel has a value of the input
        eps = 1e-7
        for j in range(1, output_height):
            for i in range(1, output_width):
                assert abs(output[j, i] - expected[j][i] * value) <= eps
Beispiel #46
0
  def tst_with_noisy_flat_background_partial(self):

    from dials.algorithms.integration.fit import fit_profile
    from scitbx.array_family import flex
    from tst_profile_helpers import gaussian

    # Create profile
    p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    s = flex.sum(p)
    p = p / s

    # Copy profile
    c0 = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
    n = flex.random_double(9 * 9 * 9)
    b = flex.double(flex.grid(9, 9, 9), 5) + n
    m = flex.bool(flex.grid(9,9,9), True)
    c = c0 + b

    # Get the partial profiles
    pp = p[0:5,:,:]
    mp = m[0:5,:,:]
    cp = c[0:5,:,:]
    bp = b[0:5,:,:]
    c0p = c0[0:5,:,:]

    # Fit
    fit = fit_profile(pp, mp, cp, bp)
    I = fit.intensity()
    V = fit.variance()

    # Test intensity is the same
    eps = 1e-7
    assert(abs(I - flex.sum(c0)) < eps)
    assert(abs(V - (flex.sum(c0p) + flex.sum(bp))) < eps)

    print 'OK'
Beispiel #47
0
def test_pickle():
    from dials.model.data import PixelList
    from scitbx.array_family import flex
    size = (100, 100)
    sf = 10
    image = flex.double(flex.grid(size))
    mask = flex.bool(flex.grid(size))
    for i in range(len(image)):
        image[i] = random.randint(0, 100)
        mask[i] = bool(random.randint(0, 1))
    pl = PixelList(sf, image, mask)
    assert pl.size() == size
    assert pl.frame() == sf

    import six.moves.cPickle as pickle

    obj = pickle.dumps(pl)
    pl2 = pickle.loads(obj)

    assert pl2.size() == size
    assert pl2.frame() == sf
    assert len(pl2) == len(pl)
    assert pl2.index().all_eq(pl.index())
    assert pl2.value().all_eq(pl.value())
Beispiel #48
0
    def _get_endianic_raw_data(self, size):
        big_endian = self._header_dictionary["BYTE_ORDER"] == "big_endian"

        with self.open_file(self._image_file, "rb") as fh:
            fh.seek(self._header_size)

            if big_endian == is_big_endian():
                raw_data = read_uint16(streambuf(fh), int(size[0] * size[1]))
            else:
                raw_data = read_uint16_bs(streambuf(fh),
                                          int(size[0] * size[1]))

        # note that x and y are reversed here
        raw_data.reshape(flex.grid(size[1], size[0]))
        return raw_data
Beispiel #49
0
def ball(np, radius, val=1.0):
    result = flex.double(flex.grid(np, np), 0)
    assert radius < np
    o1 = np / 2.0 - radius / 1.5
    o2 = np / 2.0 + radius / 1.5
    for ii in range(np):
        for jj in range(np):
            dd = (ii - o1)**2.0 + (jj - o1)**2.0
            if dd <= radius**2.0:
                result[(ii, jj)] = val
            dd = (ii - o2)**2.0 + (jj - o2)**2.0
            if dd <= radius**2.0:
                result[(ii, jj)] = val

    return result
Beispiel #50
0
def test_plot_coords():
    coords = flex.double([0.5, 0.5, 0, 1, 0, 1, 1, 0, 1, 0])
    coords.reshape(flex.grid((5, 2)))
    labels = flex.int([-1, 0, 0, 1, 1])
    d = plots.plot_coords(coords, labels=labels)
    assert "cosym_coordinates" in d
    assert set(d["cosym_coordinates"]) == {"data", "help", "layout"}
    assert (d["cosym_coordinates"]["data"][0]["marker"]["color"] ==
            "rgb(0.000000,0.000000,0.000000)")
    assert d["cosym_coordinates"]["data"][0]["x"] == [0.5]
    assert d["cosym_coordinates"]["data"][0]["y"] == [0.5]
    assert d["cosym_coordinates"]["data"][1]["x"] == [0.0, 0.0]
    assert d["cosym_coordinates"]["data"][1]["y"] == [1.0, 1.0]
    assert d["cosym_coordinates"]["data"][2]["x"] == [1.0, 1.0]
    assert d["cosym_coordinates"]["data"][2]["y"] == [0.0, 0.0]
Beispiel #51
0
    def tst_masked_mean_filter(self):
        from dials.algorithms.image.filter import mean_filter
        from scitbx.array_family import flex
        from random import randint

        # Create an image
        image = flex.random_double(2000 * 2000)
        image.reshape(flex.grid(2000, 2000))
        mask = flex.random_bool(2000 * 2000, 0.99).as_int()
        mask.reshape(flex.grid(2000, 2000))

        # Calculate the summed area table
        mask2 = mask.deep_copy()
        mean = mean_filter(image, mask2, (3, 3), 1)

        # For a selection of random points, ensure that the value is the
        # sum of the area under the kernel
        eps = 1e-7
        for i in range(10000):
            i = randint(10, 1990)
            j = randint(10, 1990)
            m1 = mean[j, i]
            p = image[j - 3:j + 4, i - 3:i + 4]
            m = mask[j - 3:j + 4, i - 3:i + 4]
            if mask[j, i] == 0:
                m2 = 0.0
            else:
                p = flex.select(p, flags=m)
                mv = flex.mean_and_variance(flex.double(p))
                m2 = mv.mean()
                s1 = flex.sum(flex.double(p))
                s2 = flex.sum(m.as_1d())
            assert (abs(m1 - m2) <= eps)

        # Test passed
        print 'OK'
Beispiel #52
0
def test_external_lookup():
    import dxtbx.format.image
    from dxtbx.imageset import ExternalLookup
    from scitbx.array_family import flex

    mask = flex.bool(flex.grid(10, 10), True)
    gain = flex.double(flex.grid(10, 10), 1)
    pedestal = flex.double(flex.grid(10, 10), 2)

    lookup = ExternalLookup()
    lookup.mask.data = dxtbx.format.image.ImageBool(
        dxtbx.format.image.ImageTileBool(mask))
    lookup.gain.data = dxtbx.format.image.ImageDouble(
        dxtbx.format.image.ImageTileDouble(gain))
    lookup.pedestal.data = dxtbx.format.image.ImageDouble(
        dxtbx.format.image.ImageTileDouble(pedestal))

    mask2 = lookup.mask.data.tile(0).data()
    gain2 = lookup.gain.data.tile(0).data()
    pedestal2 = lookup.pedestal.data.tile(0).data()

    assert mask2.all_eq(mask)
    assert gain2.all_eq(gain)
    assert pedestal2.all_eq(pedestal)
Beispiel #53
0
 def box_iterator(self):
   p = self.xrs.unit_cell().parameters()
   b = maptbx.boxes_by_dimension(
     n_real = self.n_real,
     dim    = self.box_dimension,
     abc    = p[:3])
   i_box = 0
   for s,e in zip(b.starts, b.ends):
     i_box+=1
     map_box_obs  = maptbx.copy(self.map_data_obs,  s, e)
     map_box_calc = maptbx.copy(self.map_data_calc, s, e)
     map_box_obs.reshape(flex.grid(map_box_obs.all()))
     map_box_calc.reshape(flex.grid(map_box_calc.all()))
     #######
     # XXX Copy-paste from map_box
     abc = []
     for i in range(3):
       abc.append( p[i] * map_box_calc.all()[i]/self.n_real[i] )
     ucb = uctbx.unit_cell(
       parameters=(abc[0],abc[1],abc[2],p[3],p[4],p[5]))
     cs = crystal.symmetry(unit_cell=ucb, space_group="P1")
     #######
     diff_map = scale_two_real_maps_in_fourier_space(
       m1         = map_box_obs,
       m2         = map_box_calc,
       cs         = cs,
       d_min      = self.d_min,
       vector_map = self.vector_map)
     maptbx.set_box(
       map_data_from = diff_map,
       map_data_to   = self.map_result,
       start         = s,
       end           = e)
   sd = self.map_result.sample_standard_deviation()
   if(sd!=0):
     self.map_result = self.map_result/sd
Beispiel #54
0
 def get_raw_data(self):
     # Not intended for production; simply a means to marshall all same-size tile
     # data together to report it out as a single array; used for testing dxtbx.
     keys = self._tiles.keys()
     keys.sort()
     raw = flex.double(
         flex.grid(
             len(keys) * self._tiles[keys[0]].focus()[0],
             self._tiles[keys[0]].focus()[1]))
     slowstride = self._tiles[keys[0]].focus()[0]
     for ik, k in enumerate(keys):
         raw.matrix_paste_block_in_place(block=self._tiles[k],
                                         i_row=slowstride * ik,
                                         i_column=0)
     return raw
Beispiel #55
0
def get_3D_transform():
    with mrcfile.open("new.mrc") as mrc:

        # coerce to double
        realpart = flex.double(mrc.data.astype(np.float64))
        # in-place resize to a good multiple of 2
        realpart.resize(flex.grid(imax, imax, imax))
        complpart = flex.double(flex.grid(imax, imax, imax))
        #from IPython import embed; embed()
        C3D = flex.complex_double(realpart, complpart)

        from matplotlib import pyplot as plt
        from scitbx import fftpack
        #plt.imshow(S2D)
        #plt.show()
        #from IPython import embed; embed()
        print "C3Dfocus", C3D.focus()
        print C3D
        FFT = fftpack.complex_to_complex_3d(
            (C3D.focus()[0], C3D.focus()[1], C3D.focus()[2]))
        c = FFT.forward(C3D)
        print c.focus()
        print c
        return c
Beispiel #56
0
def test_real_to_complex_3d(verbose):
    for nx in [3, 4]:
        for ny in [4, 5]:
            for nz in [7, 8]:
                fft = fftpack.real_to_complex_3d((nx, ny, nz))
                m = fft.m_real()
                vd = flex.double(flex.grid(m))
                vc = flex.complex_double(flex.grid((m[0], m[1], m[2] // 2)))
                assert vd.size() == 2 * vc.size()
                for i in range(vd.size()):
                    vd[i] = random.random() * 2 - 1
                vd_orig = vd.deep_copy()
                for i in range(vc.size()):
                    vc[i] = complex(vd[2 * i], vd[2 * i + 1])
                vdt = fft.forward(vd)
                vct = fft.forward(vc)
                for t in (vdt, vct):
                    assert t.origin() == (0, 0, 0)
                    assert t.all() == fft.n_complex()
                    assert t.focus() == fft.n_complex()
                if (verbose): show_rseq_3d(vd, fft.m_real(), fft.m_real())
                assert_complex_eq_real(vc, vd)
                vdt = fft.backward(vd)
                vct = fft.backward(vc)
                for t in (vdt, vct):
                    assert t.origin() == (0, 0, 0)
                    assert t.all() == fft.m_real()
                    assert t.focus() == fft.n_real()
                if (verbose): show_rseq_3d(vd, fft.m_real(), fft.n_real())
                assert_complex_eq_real(vc, vd)
                f = nx * ny * nz
                for ix in range(nx):
                    for iy in range(ny):
                        for iz in range(nz):
                            assert approx_equal(vdt[(ix, iy, iz)],
                                                vd_orig[(ix, iy, iz)] * f)
Beispiel #57
0
 def jacobian_analytical(self, x):
   x1,x2,x3 = x
   result = flex.double()
   for ti in meyer_function.ts:
     denominator = ti + x3
     assert denominator != 0
     denominator_sq = denominator**2
     assert denominator_sq != 0
     term = math.exp(x2/denominator)
     result.extend(flex.double([
       term,
       x1*term/denominator,
       -x1*term*x2/denominator_sq]))
   result.resize(flex.grid(self.m, self.n))
   return result
Beispiel #58
0
    def build_up(self, objective_only=False):
        mu, sigma, tau = self.x
        residuals = self.exgauss_cdf_array(self.t, mu, sigma, tau)
        residuals -= self.y

        self.reset()
        if objective_only:
            self.add_residuals(residuals, weights=None)
        else:
            grad = self.get_residual_grad(mu, sigma, tau)
            grad_r = (grad[0], grad[1], grad[2])
            jacobian = flex.double(flex.grid(self.n_data, self.n_parameters))
            for j, der_r in enumerate(grad_r):
                jacobian.matrix_paste_column_in_place(der_r, j)
            self.add_equations(residuals, jacobian, weights=None)
Beispiel #59
0
    def get_raw_data(self):
        '''Read the data - assuming it is streaming 4-byte unsigned ints following the
    header...'''

        from boost.python import streambuf
        from dxtbx import read_int32
        from scitbx.array_family import flex
        assert (len(self.get_detector()) == 1)
        size = self.get_detector()[0].get_image_size()
        f = self.open_file(self._image_file)
        f.read(int(self._header_dictionary['HEADER_BYTES']))
        raw_data = read_int32(streambuf(f), int(size[0] * size[1]))
        raw_data.reshape(flex.grid(size[1], size[0]))

        return raw_data
Beispiel #60
0
    def tst_known_offset(self):
        from scitbx.array_family import flex

        # Set the size of the grid
        height = 10
        width = 10

        # Create the grid data
        grid = flex.double([1 for i in range(height * width)])
        grid.reshape(flex.grid(height, width))

        # Create the grid coordinates
        xy = []
        for j in range(height + 1):
            for i in range(width + 1):
                xy.append((i + 0.5, j + 0.5))
        gridxy = flex.vec2_double(xy)
        gridxy.reshape(flex.grid(height + 1, width + 1))

        # Get the output grid
        output = rebin_pixels(grid, gridxy, (height, width))

        # Check that each each pixel along the left and bottom has a value
        # of 0.5 and that everything else is 1
        eps = 1e-7
        assert (abs(output[0, 0] - 0.25) <= eps)
        for i in range(1, width):
            assert (abs(output[0, i] - 0.5) <= eps)
        for j in range(1, height):
            assert (abs(output[j, 0] - 0.5) <= eps)
        for j in range(1, height):
            for i in range(1, width):
                assert (abs(output[j, i] - 1.0) <= eps)

        # Test passed
        print 'OK'