def unrestrained_setting(self):
  """ Calculate the basis vectors from N spots using numpy. This is equation 5 in
  Brewster et. al 2015.
  @return a cctbx crystal_orientation object
  """
  from dials.array_family import flex
  from scitbx.matrix import sqr
  import numpy as np

  if len(self.miller_indices) < 3:
    return None

  N = self.miller_indices.size()

  hkl = self.miller_indices.as_double()
  hkl.reshape(flex.grid((N,3)))
  hkl = hkl.as_numpy_array()

  xyz = self.u_vectors.as_double()
  xyz.reshape(flex.grid((N,3)))
  xyz = xyz.as_numpy_array()

  try:
    result = np.linalg.lstsq(hkl,xyz)
  except Exception,e:
    print "Exception while calculating basis vectors: %s"%e.message
    return None
Example #2
0
def plot_valid(b, s):
  from dials.array_family import flex

  b = [0.1, 0.2, 0.3, 0.4, 0.5]
  s = [0.1, 0.3, 0.5, 0.3, 0.1]


  v1 = flex.bool(flex.grid(100, 100))
  v2 = flex.bool(flex.grid(100, 100))
  v3 = flex.bool(flex.grid(100, 100))
  r = [float(ss) / float(bb) for ss, bb in zip(s, b)]

  for BB in range(0, 100):
    for SS in range(0, 100):
      B = -5.0 + BB / 10.0
      S = -5.0 + SS / 10.0
      V1 = True
      V2 = True
      V3 = True
      for i in range(len(b)):
        if B*b[i]+S*s[i] <= 0:
          V1 = False
          break
      for i in range(len(b)):
        if B*b[i] <= -S*s[i]:
          V2 = False
          break

      v1[BB,SS] = V1
      v2[BB,SS] = V2

  from matplotlib import pylab
  pylab.imshow(v1.as_numpy_array())
  pylab.show()
  exit(0)
Example #3
0
def plot_prob_for_zero(c, b, s):
  from math import log, exp, factorial
  from dials.array_family import flex
  L = flex.double(flex.grid(100, 100))
  MASK = flex.bool(flex.grid(100, 100))
  c = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
  b = [bb / sum(b) for bb in b]
  s = [ss / sum(s) for ss in s]
  for BB in range(0, 100):
    for SS in range(0, 100):
      B = 0 + BB / 10000.0
      S = 0 + SS / 40.0
      LL = 0
      for i in range(len(b)):
        if B*b[i] + S*s[i] <= 0:
          MASK[BB, SS] = True
          LL = -999999
          break
        else:
          LL += c[i]*log(B*b[i]+S*s[i]) - log(factorial(c[i])) - B*b[i] - S*s[i]

      L[BB, SS] = LL
  index = flex.max_index(L)
  i = index % 100
  j = index // 100
  B = 0 + j / 10000.0
  S = 0 + i / 40.0
  print flex.max(L), B, S
  from matplotlib import pylab
  import numpy
  im = numpy.ma.masked_array(flex.exp(L).as_numpy_array(), mask=MASK.as_numpy_array())
  pylab.imshow(im)
  pylab.show()
  exit(0)
Example #4
0
    def plot_scale_vs_x_y(self):
        from scitbx.array_family import flex
        from math import ceil

        print "Getting scale"
        points = [(int(xyz[1] / 8), int(xyz[0] / 8)) for xyz in self.xyz]
        scale = [x / d for x, d in zip(self.i_xds, self.i_dials)]

        print "Creating Grid"
        image_size = self.sweep.get_detector()[0].get_image_size()[::-1]
        image_size = (int(ceil(image_size[0] / 8)), int(ceil(image_size[1] / 8)))
        grid = flex.double(flex.grid(image_size))
        count = flex.int(flex.grid(image_size))
        for p, s in zip(points, scale):
            grid[p] += s
            count[p] += 1
        for i in range(len(grid)):
            if count[i] > 0:
                grid[i] /= count[i]

        # grid_points = [(j,i) for j in range(image_size[0]) for i in range(image_size[1])]

        # grid = griddata(points, scale, grid_points)
        # grid.shape = image_size
        from matplotlib import pyplot

        fig, ax = pyplot.subplots()
        pyplot.title("scale vs x/y")
        cax = pyplot.imshow(grid.as_numpy_array())
        cbar = fig.colorbar(cax)
        pyplot.savefig("plot-scale-vs-xy.png")
        pyplot.close()
Example #5
0
def show_image(c,b,s, BB=None, SS=None):

  import numpy.ma
  from dials.array_family import flex
  N = 100
  im = flex.double(flex.grid(N, N))
  mask = flex.bool(flex.grid(N, N))
  for j in range(N):
    for i in range(N):
      B = -1.0 + j * 10.0 / N
      S = -1.0 + i * 10.0 / N
      im[j,i], mask[j,i] = func(c,b,s,B,S)
      im[j,i] = -im[j,i]

  masked_im = numpy.ma.array(
    # im.as_numpy_array(),
    flex.exp(im).as_numpy_array(),
    mask = mask.as_numpy_array())
  mask2 = flex.bool(flex.grid(N, N))
  indices = []
  for i in range(len(mask)):
    if mask[i] == False:
      indices.append(i)
  indices = flex.size_t(indices)
  ind = flex.max_index(im.select(indices))
  ind = indices[ind]
  maxy = -1.0 + (ind % N) * 10.0 / N
  maxx = -1.0 + (ind // N) * 10.0 / N
  from matplotlib import pylab
  pylab.imshow(masked_im, origin='bottom', extent=[-1.0, 9.0, -1.0, 9.0])
  if YY is not None and XX is not None:
    pylab.plot(YY, XX)
  pylab.scatter([maxy], [maxx])
  pylab.show()
Example #6
0
 def __init__(self, frames, size):
   from dials.array_family import flex
   self.frames = frames
   nframes = frames[1] - frames[0]
   self.data = []
   self.mask = []
   for sz in size:
     self.data.append(flex.double(flex.grid(nframes, sz[0], sz[1])))
     self.mask.append(flex.bool(flex.grid(nframes, sz[0], sz[1])))
Example #7
0
 def set_image(self, index, data, mask):
   from dials.array_family import flex
   for d1, d2 in zip(self.data, data):
     h,w = d2.all()
     d2.reshape(flex.grid(1,h,w))
     d1[index:index+1,:,:] = d2.as_double()
   for m1, m2 in zip(self.mask, mask):
     h,w = m2.all()
     m2.reshape(flex.grid(1,h,w))
     m1[index:index+1,:,:] = m2
def build_np_img(nrow=64, ncol=64):

  data2d = flex.double(flex.grid(nrow, ncol), 0)
  mask2d = flex.int(flex.grid(nrow, ncol), 0)

  for x in range(nrow):
    for y in range(ncol):
      data2d[x, y] += (x * 1.00000000001 + y * 2.5555555555555) * 0.00000000001
      #print "number to see(aprox) =", data2d[x, y]


  return data2d, mask2d
def generate_shoebox(size, mean, nforeground, ninvalid):
  from dials.algorithms.simulation.generate_test_reflections \
    import random_background_plane2
  from dials.array_family import flex
  from random import sample
  from dials.algorithms.shoebox import MaskCode
  data = flex.double(flex.grid(size), 0.0)
  mask = flex.int(flex.grid(size), MaskCode.Valid | MaskCode.Background)
  random_background_plane2(data, mean, 0, 0, 0)
  for i in sample(range(len(data)), ninvalid):
    mask[i] &= ~MaskCode.Valid
  for i in sample(range(len(data)), nforeground):
    mask[i] |= MaskCode.Foreground
    mask[i] &= ~MaskCode.Background
  return data, mask
Example #10
0
def generate_image(function, height, width):
  from dials.array_family import flex
  image = flex.double(flex.grid(height, width))
  for j in range(height):
    for i in range(width):
      image[j,i] = function(j,i)
  return image
 def build_up(pfh, objective_only=False):
   values = pfh.parameterization(pfh.x)
   assert 0. < values.G , "G-scale value out of range ( < 0 ) within LevMar build_up"
   # XXX revisit these limits.  Seems like an ad hoc approach to have to set these limits
   # However, the assertions are necessary to avoid floating point exceptions at the C++ level
   # Regardless, these tests throw out ~30% of LM14 data, thus search for another approach
   assert -150. < values.BFACTOR < 150. ,"B-factor out of range (+/-150) within LevMar build_up"
   assert -0.5 < 180.*values.thetax/math.pi < 0.5 , "thetax out of range ( |rotx|>.5 degrees ) within LevMar build_up"
   assert -0.5 < 180.*values.thetay/math.pi < 0.5 , "thetay out of range ( |roty|>.5 degrees ) within LevMar build_up"
   assert 0.000001 < values.RS , "RLP size out of range (<0.000001) within LevMar build_up"
   assert values.RS < 0.001 , "RLP size out of range (>0.001) within LevMar build_up"
   residuals = pfh.refinery.fvec_callable(values)
   pfh.reset()
   if objective_only:
     pfh.add_residuals(residuals, weights=pfh.refinery.WEIGHTS)
   else:
     grad_r = pfh.refinery.jacobian_callable(values)
     jacobian = flex.double(
       flex.grid(len(pfh.refinery.MILLER), pfh.n_parameters))
     for j, der_r in enumerate(grad_r):
       jacobian.matrix_paste_column_in_place(der_r,j)
       #print >> pfh.out, "COL",j, list(der_r)
     pfh.add_equations(residuals, jacobian, weights=pfh.refinery.WEIGHTS)
   print >> pfh.out, "rms %10.3f"%math.sqrt(flex.mean(pfh.refinery.WEIGHTS*residuals*residuals)),
   values.show(pfh.out)
Example #12
0
  def __init__(self, parent, refl, orient = "portrait", id = wx.ID_ANY,
               title = "", pos = wx.DefaultPosition, size = wx.DefaultSize,
               style = wx.DEFAULT_FRAME_STYLE,
               name="ShoeboxView"):
    super(ShoeboxView, self).__init__(parent, id, title,
                                  pos, size, style, name)

    dat_flex = refl['shoebox'].data

    n_fr = dat_flex.all()[0]
    self.ImgLst = ImageListCtrl(self, orient)
    for fr in range(n_fr):
      data2d_flex = dat_flex[fr:fr + 1, :, :]
      data2d_flex.reshape(flex.grid(dat_flex.all()[1:]))
      data2d = data2d_flex.as_numpy_array()
      bitmap = GetBitmap_from_np_array(data2d)
      self.ImgLst.AppendBitmap(bitmap)


    self.top_sizer = wx.BoxSizer(wx.HORIZONTAL)

    self.TstButton = wx.Button(self, label="Enbiggen")
    self.TstButton.Bind(wx.EVT_BUTTON, self.OnTstBut)
    self.top_sizer.Add(self.TstButton)

    self.Tst_01_Button = wx.Button(self, label="EnSmallen")
    self.Tst_01_Button.Bind(wx.EVT_BUTTON, self.OnTst_01_But)
    self.top_sizer.Add(self.Tst_01_Button)

    self.bot_sizer = wx.BoxSizer(wx.VERTICAL)
    self.bot_sizer.Add(self.top_sizer)
    self.bot_sizer.Add(self.ImgLst, 1, wx.EXPAND)

    self.SetSizer(self.bot_sizer)
Example #13
0
  def _create_hot_mask(self, imageset, hot_pixels):
    '''
    Find hot pixels in images

    '''
    from dials.array_family import flex

    # Write the hot mask
    if self.write_hot_mask:

      # Create the hot pixel mask
      hot_mask = tuple(flex.bool(flex.grid(p.get_image_size()[::-1]), True)
                       for p in imageset.get_detector())
      num_hot = 0
      if hot_pixels > 0:
        for hp, hm in zip(hot_pixels, hot_mask):
          for i in range(len(hp)):
            hm[hp[i]] = False
          num_hot += len(hp)
      logger.info('Found %d possible hot pixel(s)' % num_hot)

    else:
      hot_mask = None

    # Return the hot mask
    return hot_mask
def make_dx_dy_ellipse(imageset, phi, l1, l2, centre_xy):
    detector = imageset.get_detector()

    # Get fast and slow axes from the first panel. These will form the X and Y
    # directions for the Cartesian coordinates of the correction map
    p0 = detector[0]
    f0, s0 = matrix.col(p0.get_fast_axis()), matrix.col(p0.get_slow_axis())

    # Get the lab coordinate of the centre of the ellipse
    topleft = matrix.col(p0.get_pixel_lab_coord((0, 0)))
    mid = topleft + centre_xy[0] * f0 + centre_xy[1] * s0

    # Get matrix describing the elliptical distortion
    M = ellipse_matrix_form(phi, l1, l2)

    distortion_map_x = []
    distortion_map_y = []

    for panel in detector:
        size_x, size_y = panel.get_pixel_size()
        nx, ny = panel.get_image_size()
        dx = flex.double(flex.grid(nx, ny), 0.0)
        dy = flex.double(flex.grid(nx, ny), 0.0)
        elt = 0
        for j in range(ny):
            for i in range(nx):
                # Get the X,Y coordinate of this pixel in the frame of the correction
                # map, which has its origin at the centre of the ellipse and is aligned
                # along fast, slow of the first panel.
                lab = matrix.col(panel.get_pixel_lab_coord((i + 0.5, j + 0.5)))
                offset = lab - mid
                x = offset.dot(f0)  # undistorted X coordinate (mm)
                y = offset.dot(s0)  # undistorted Y coordinate (mm)
                distort = M * matrix.col((x, y))  # distorted by ellipse matrix

                # store correction in units of the pixel size
                dx[elt] = (x - distort[0]) / size_x
                dy[elt] = (y - distort[1]) / size_y
                elt += 1
        # Add results for this panel
        distortion_map_x.append(dx)
        distortion_map_y.append(dy)

    distortion_map_x = tuple(distortion_map_x)
    distortion_map_y = tuple(distortion_map_y)

    return distortion_map_x, distortion_map_y
Example #15
0
  def tst_deconvolve_3_with_flat_background(self):
    from dials.algorithms.integration.fit import ProfileFitter
    from scitbx.array_family import flex
    from numpy.random import seed
    seed(0)

    I0 = [1000, 2000, 3000]

    # Create profile
    p = self.generate_3_profiles()

    Ical = [[],[],[]]
    for it in range(1):

      # Copy profile
      c = flex.double(flex.grid(40, 9, 9))
      for i in range(p.all()[0]):
        pp = p[i:i+1,:,:,:]
        pp.reshape(c.accessor())
        cc = add_poisson_noise(I0[i] * pp)
        c += cc
      b = flex.double(flex.grid(40, 9, 9), 1)
      m = flex.bool(flex.grid(40,9,9), True)
      c += add_poisson_noise(b)

      # Fit
      fit = ProfileFitter(c, b, m, p)
      I = fit.intensity()
      V = fit.variance()
      assert fit.niter() < fit.maxiter()

      for i in range(3):
        Ical[i].append(I[i])

    for i in range(3):
      Ical[i] = sum(Ical[i]) / len(Ical[i])

    Iknown = [1030.7018033805357, 1948.7152700952695, 2972.983204218213]
    Vknown = [4270.701803380534, 5188.715270095279, 6212.983204218214]

    # Test intensity is the same
    eps = 1e-7
    for i in range(3):
      assert(abs(I[i] - Iknown[i]) < eps)
      assert(abs(V[i] - Vknown[i]) < eps)

    print 'OK'
Example #16
0
  def tst_deconvolve_7_with_no_background(self):
    from dials.algorithms.integration.fit import ProfileFitter
    from scitbx.array_family import flex
    from numpy.random import seed
    seed(0)

    I0 = [1000, 1500, 2000, 2500, 3000, 3500, 4000]

    # Create profile
    p = self.generate_7_profiles()

    Ical = [[],[],[],[],[],[],[]]
    for it in range(1):

      # Copy profile
      c = flex.double(flex.grid(40, 40, 40))
      for i in range(p.all()[0]):
        pp = p[i:i+1,:,:,:]
        pp.reshape(c.accessor())
        cc = add_poisson_noise(I0[i] * pp)
        c += cc
      b = flex.double(flex.grid(40, 40, 40), 0)
      m = flex.bool(flex.grid(40,40,40), True)

      # Fit
      fit = ProfileFitter(c, b, m, p)
      I = fit.intensity()
      V = fit.variance()
      assert fit.niter() < fit.maxiter()

      for i in range(7):
        Ical[i].append(I[i])

    for i in range(7):
      Ical[i] = sum(Ical[i]) / len(Ical[i])

    Iknown = [1012.4193633595916, 1472.3322059495797, 2072.136413825826,
              2486.4902438469253, 3012.6132119521126, 3409.2053517072773,
              3952.803209358826]

    # Test intensity is the same
    eps = 1e-7
    for i in range(7):
      assert(abs(I[i] - Iknown[i]) < eps)
      assert(abs(V[i] - Iknown[i]) < eps)

    print 'OK'
Example #17
0
def generate_image(function, height, width):
    from dials.array_family import flex

    image = flex.double(flex.grid(height, width))
    for j in range(height):
        for i in range(width):
            image[j, i] = function(j, i)
    return image
Example #18
0
def make_dx_dy_translate(imageset, dx, dy):
    images = imageset.indices()
    image = imageset[images[0]]

    if (len(dx) != len(image)) or (len(dx) != len(image)):
        raise Sorry(
            "Please provide separate translations for each panel of the detector"
        )

    distortion_map_x = []
    distortion_map_y = []

    for block, shift_x, shift_y in zip(image, dx, dy):
        distortion_map_x.append(flex.double(flex.grid(block.focus()), shift_x))
        distortion_map_y.append(flex.double(flex.grid(block.focus()), shift_y))

    return tuple(distortion_map_x), tuple(distortion_map_y)
Example #19
0
def compute_profile(experiments, reflection, reference, N):
    from dials.array_family import flex
    from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem
    from dials.algorithms.profile_model.modeller import GridSampler
    from dials_scratch.jmp.sim import compute_profile_internal
    from random import uniform

    sbox = reflection["shoebox"]
    bbox = sbox.bbox
    zs = sbox.zsize()
    ys = sbox.ysize()
    xs = sbox.xsize()

    profile = flex.double(flex.grid(zs, ys, xs))

    m2 = experiments[0].goniometer.get_rotation_axis_datum()
    s0 = experiments[0].beam.get_s0()
    s1 = reflection["s1"]
    phi = reflection["xyzcal.mm"][2]
    detector = experiments[0].detector
    scan = experiments[0].scan

    cs = CoordinateSystem(m2, s0, s1, phi)

    scan_range = scan.get_array_range()
    image_size = detector[0].get_image_size()
    grid_size = (3, 3, 40)
    assert grid_size[0] * grid_size[1] * grid_size[2] == len(reference[0])

    sampler = GridSampler(image_size, scan_range, grid_size)

    xyz = reflection["xyzcal.px"]
    index = sampler.nearest(0, xyz)

    for g in reference[0]:
        assert abs(flex.sum(g) - 1.0) < 1e-7

    grid = reference[0][index]

    sigma_d = experiments[0].profile.sigma_b(deg=False)
    sigma_m = experiments[0].profile.sigma_m(deg=False)
    delta_d = 3.0 * sigma_d
    delta_m = 3.0 * sigma_m

    profile = compute_profile_internal(grid, bbox, zs, ys, xs, N, delta_d,
                                       delta_m, detector, scan, cs)

    # from dials_scratch.jmp.viewer import show_image_stack_multi_view
    # show_image_stack_multi_view(profile.as_numpy_array(), vmax=max(profile))
    sum_p = flex.sum(profile)
    print("Partiality: %f" % sum_p)
    try:
        assert sum_p > 0, "sum_p == 0"
    except Exception as e:
        print(e)
        return None

    return profile
Example #20
0
  def tst_deconvolve_3_with_no_background(self):
    from dials.algorithms.integration.fit import ProfileFitter
    from scitbx.array_family import flex
    from numpy.random import seed
    seed(0)

    I0 = [1000, 2000, 3000]

    # Create profile
    p = self.generate_3_profiles()

    Ical = [[],[],[]]
    for it in range(1):

      # Copy profile
      c = flex.double(flex.grid(40, 9, 9))
      for i in range(p.all()[0]):
        pp = p[i:i+1,:,:,:]
        pp.reshape(c.accessor())
        cc = add_poisson_noise(I0[i] * pp)
        c += cc
      b = flex.double(flex.grid(40, 9, 9), 0)
      m = flex.bool(flex.grid(40,9,9), True)

      # Fit
      fit = ProfileFitter(c, b, m, p)
      I = fit.intensity()
      V = fit.variance()
      assert fit.niter() < fit.maxiter()

      for i in range(3):
        Ical[i].append(I[i])

    for i in range(3):
      Ical[i] = sum(Ical[i]) / len(Ical[i])

    Iknown = [1048.3221116842406, 1920.9035376774107, 2938.7743506383745]

    # Test intensity is the same
    eps = 1e-7
    for i in range(3):
      assert(abs(I[i] - Iknown[i]) < eps)
      assert(abs(V[i] - Iknown[i]) < eps)

    print 'OK'
Example #21
0
    def unrestrained_setting(self):
        """ Calculate the basis vectors from N spots using numpy. This is equation 5 in
  Brewster et. al 2015.
  @return a cctbx crystal_orientation object
  """
        from dials.array_family import flex
        from scitbx.matrix import sqr
        import numpy as np

        if len(self.miller_indices) < 3:
            return None

        N = self.miller_indices.size()

        hkl = self.miller_indices.as_double()
        hkl.reshape(flex.grid((N, 3)))
        hkl = hkl.as_numpy_array()

        xyz = self.u_vectors.as_double()
        xyz.reshape(flex.grid((N, 3)))
        xyz = xyz.as_numpy_array()

        try:
            result = np.linalg.lstsq(hkl, xyz)
        except Exception as e:
            print "Exception while calculating basis vectors: %s" % e.message
            return None

        solution, self.residuals, rank, singular = result[0], result[
            1], result[2], result[3]
        if len(self.residuals) == 0:
            self.residuals = [0, 0,
                              0]  # happens when only 3 spots in the max clique

        #print "Summed squared residuals of x,y,z for %d spots in 1/angstroms: %.7f, %.7f, %.7f"%(N,self.residuals[0],self.residuals[1],self.residuals[2])
        Amatrix = sqr(solution.flatten()).transpose()

        # Johan believes this can happen if there aren't 3 hkls that aren't co-linear (IIRC)
        if Amatrix.determinant() <= 0:
            return None

        from cctbx import crystal_orientation
        ori = crystal_orientation.crystal_orientation(
            Amatrix, crystal_orientation.basis_type.reciprocal)
        return ori
Example #22
0
    def __call__ (self, img_flex, palette_in, min_i, max_i):
        flex_2d_data = img_flex.as_double()
        flex_2d_mask = flex.double(flex.grid(flex_2d_data.all()[0], flex_2d_data.all()[1]), 0)
        arr_i = self.arr_img(flex_2d_data, flex_2d_mask, i_min = min_i, i_max = max_i, palette = palette_in)

        q_img = QImage(arr_i.data, np.size(arr_i[0:1, :, 0:1]),
                       np.size(arr_i[:, 0:1, 0:1]), QImage.Format_RGB32)

        return q_img
Example #23
0
def img_arr_n_cpp(show_nums=True):

    n_col = 20
    n_row = 10

    wx_bmp_arr = rgb_img()
    flex_data_in = flex.double(flex.grid(n_row, n_col), 15)
    flex_mask_in = flex.double(flex.grid(n_row, n_col), 0)

    for col in xrange(n_col):
        for row in xrange(n_row):
            flex_data_in[row, col] = col + row

    err_code = wx_bmp_arr.set_min_max(0.0, 28.0)

    palette = "hot ascend"

    if palette == "black2white":
        palette_num = 1
    elif palette == "white2black":
        palette_num = 2
    elif palette == "hot ascend":
        palette_num = 3
    else:  # assuming "hot descend"
        palette_num = 4
    print "before c++"
    img_array_tmp = wx_bmp_arr.gen_bmp(flex_data_in, flex_mask_in, show_nums,
                                       palette_num)
    print "after c++"
    np_img_array = img_array_tmp.as_numpy_array()

    height = np.size(np_img_array[:, 0:1, 0:1])
    width = np.size(np_img_array[0:1, :, 0:1])

    img_array = np.zeros([height, width, 4], dtype=np.uint8)

    #for some strange reason PyQt4 needs to use RGB as BGR
    img_array[:, :, 0:1] = np_img_array[:, :, 2:3]
    img_array[:, :, 1:2] = np_img_array[:, :, 1:2]
    img_array[:, :, 2:3] = np_img_array[:, :, 0:1]

    print "end of np generator"

    return img_array
Example #24
0
def test_deconvolve_3_with_flat_background():

    np.random.seed(0)

    I0 = [1000, 2000, 3000]

    # Create profile
    p = generate_3_profiles()

    Ical = [[], [], []]
    for it in range(1):

        # Copy profile
        c = flex.double(flex.grid(40, 9, 9))
        for i in range(p.all()[0]):
            pp = p[i:i + 1, :, :, :]
            pp.reshape(c.accessor())
            cc = add_poisson_noise(I0[i] * pp)
            c += cc
        b = flex.double(flex.grid(40, 9, 9), 1)
        m = flex.bool(flex.grid(40, 9, 9), True)
        c += add_poisson_noise(b)

        # Fit
        fit = ProfileFitter(c, b, m, p)
        intensity = fit.intensity()
        V = fit.variance()
        assert fit.niter() < fit.maxiter()

        for i in range(3):
            Ical[i].append(intensity[i])

    for i in range(3):
        Ical[i] = sum(Ical[i]) / len(Ical[i])

    Iknown = [1030.7018033805357, 1948.7152700952695, 2972.983204218213]
    Vknown = [4270.701803380534, 5188.715270095279, 6212.983204218214]

    # Test intensity is the same
    eps = 1e-7
    for i in range(3):
        assert intensity[i] == pytest.approx(Iknown[i], abs=eps)
        assert V[i] == pytest.approx(Vknown[i], abs=eps)
Example #25
0
def refinerdata_testdata(testdata):

    experiment = testdata.experiment
    reflections = testdata.reflections

    panel = experiment.detector[0]
    s0_length = matrix.col(experiment.beam.get_s0()).length()
    reflections["bbox"] = flex.int6(len(reflections))
    reflections["xyzobs.px.value"] = flex.vec3_double(len(reflections))
    reflections["s2"] = reflections["s1"].each_normalize() * s0_length
    reflections["sp"] = flex.vec3_double(len(reflections))
    for i, (x, y, z) in enumerate(reflections["xyzcal.px"]):
        x0 = int(x) - 5
        x1 = int(x) + 5 + 1
        y0 = int(y) - 5
        y1 = int(y) + 5 + 1
        z0 = int(z)
        z1 = z0 + 1
        reflections["bbox"][i] = x0, x1, y0, y1, z0, z1
        reflections["xyzobs.px.value"][i] = (int(x) + 0.5, int(y) + 0.5,
                                             int(z) + 0.5)
        reflections["sp"][i] = (matrix.col(
            panel.get_pixel_lab_coord(
                reflections["xyzobs.px.value"][i][0:2])).normalize() *
                                s0_length)

    reflections["shoebox"] = flex.shoebox(reflections["panel"],
                                          reflections["bbox"],
                                          allocate=True)

    shoebox_data = flex.float(flex.grid(1, 11, 11))
    shoebox_mask = flex.int(flex.grid(1, 11, 11))
    for j in range(11):
        for i in range(11):
            shoebox_data[0, j, i] = (100 * exp(-0.5 * (j - 5)**2 / 1**2) *
                                     exp(-0.5 * (i - 5)**2 / 1**2))
            shoebox_mask[0, j, i] = 5
    for sbox in reflections["shoebox"]:
        sbox.data = shoebox_data
        sbox.mask = shoebox_mask

    return RefinerData.from_reflections(experiment, reflections)
Example #26
0
  def _build_jacobian(d2theta_dp, nelem=None, nparam=None):
    """construct Jacobian from lists of gradient vectors. This method may be
    overridden for the case where these vectors use sparse storage"""

    jacobian = flex.double(flex.grid(nelem, nparam))
    # loop over parameters
    for i in range(nparam):
      col = d2theta_dp[i]
      jacobian.matrix_paste_column_in_place(col, i)

    return jacobian
    def set_my_img(self):
        tm_start = tm_now()

        flex_2d_mask = flex.double(flex.grid(2500, 2400),0)
        img_slice = self.img_pos

        if( self.block_3d_flex == None ):
            q_img = QImage()
        else:

            flex_2d_data = self.block_3d_flex[img_slice:img_slice + 1, 0:2500, 0:2400]
            flex_2d_data.reshape(flex.grid(2500, 2400))

            arr_i = self.arr_img(flex_2d_data, flex_2d_mask, i_min = -3.0, i_max = self.imax)

            q_img = QImage(arr_i.data, np.size(arr_i[0:1, :, 0:1]),
                           np.size(arr_i[:, 0:1, 0:1]), QImage.Format_RGB32)

        self.img_painter.set_img_pix(q_img)
        print "dif_time[set_img_pix(q_img)] =", tm_now() - tm_start
Example #28
0
def test_SingleScaler_expand_scales_to_all_reflections(mock_apm):
    p, e, r = (generated_param(), generated_exp(), generated_refl())
    exp = create_scaling_model(p, e, r)
    p.reflection_selection.method = "use_all"
    scaler = SingleScaler(p, exp[0], r)
    # test expand to all reflections method. First check scales are all 1, then
    # update a component to simulate a minimisation result, then check that
    # scales are set only in all suitable reflections (as it may not be possible
    # to calculate scales for unsuitable reflections!)
    # Must also update the scales in the global_Ih_table
    assert list(scaler.reflection_table["inverse_scale_factor"]) == [1.0] * 8
    scaler.experiment.scaling_model.components[
        "scale"].parameters = flex.double([2.0])
    scaler.expand_scales_to_all_reflections(calc_cov=False)
    assert (list(
        scaler.reflection_table["inverse_scale_factor"]) == [2.0] * 5 + [1.0] +
            [2.0] * 2)
    assert (list(
        scaler.global_Ih_table.blocked_data_list[0].inverse_scale_factors) ==
            [2.0] * 7)

    assert list(
        scaler.reflection_table["inverse_scale_factor_variance"]) == [0.0] * 8
    # now try again
    apm = Mock()
    apm.n_active_params = 2
    var_list = [1.0, 0.1, 0.1, 0.5]
    apm.var_cov_matrix = flex.double(var_list)
    apm.var_cov_matrix.reshape(flex.grid(2, 2))
    scaler.update_var_cov(apm)
    assert scaler.var_cov_matrix[0, 0] == var_list[0]
    assert scaler.var_cov_matrix[0, 1] == var_list[1]
    assert scaler.var_cov_matrix[1, 0] == var_list[2]
    assert scaler.var_cov_matrix[1, 1] == var_list[3]
    assert scaler.var_cov_matrix.non_zeroes == 4
    scaler.expand_scales_to_all_reflections(calc_cov=True)
    assert list(
        scaler.reflection_table["inverse_scale_factor_variance"]
    ) == pytest.approx(
        [2.53320, 1.07106, 1.08125, 1.23219, 1.15442, 0.0, 1.0448, 1.0448],
        1e-4)

    # Second case - when var_cov_matrix is only part of full matrix.
    p, e, r = (generated_param(), generated_exp(), generated_refl())
    exp = create_scaling_model(p, e, r)
    p.reflection_selection.method = "use_all"
    scaler = SingleScaler(p, exp[0], r)
    apm = mock_apm
    scaler.update_var_cov(apm)
    assert scaler.var_cov_matrix.non_zeroes == 1
    assert scaler.var_cov_matrix[0, 0] == 2.0
    assert scaler.var_cov_matrix.n_cols == 2
    assert scaler.var_cov_matrix.n_rows == 2
    assert scaler.var_cov_matrix.non_zeroes == 1
Example #29
0
    def _build_jacobian(d2theta_dp, nelem=None, nparam=None):
        """construct Jacobian from lists of gradient vectors. This method may be
    overridden for the case where these vectors use sparse storage"""

        jacobian = flex.double(flex.grid(nelem, nparam))
        # loop over parameters
        for i in range(nparam):
            col = d2theta_dp[i]
            jacobian.matrix_paste_column_in_place(col, i)

        return jacobian
Example #30
0
    def fisher_information(self):
        """
        Compute the fisher information

        """
        ctot = self.ctot

        # Get info about marginal distribution
        S22 = self.S[2, 2]
        dS22 = [self.dS[2, 2, i] for i in range(self.dS.shape[2])]
        S22_inv = 1 / S22

        # Get info about conditional distribution
        Sbar = self.conditional.sigma()
        dSbar = self.conditional.first_derivatives_of_sigma(
        )  # list of 2x2 arrays
        dmbar = self.conditional.first_derivatives_of_mean(
        )  # list of 2x1 arrays
        Sbar_inv = inv(Sbar)
        dmu = self.dmu

        # Weights for marginal and conditional components
        m_w = ctot
        c_w = ctot

        # Compute the fisher information wrt parameter i j
        I = flex.double(flex.grid(len(dS22), len(dS22)))

        for j in range(len(dS22)):
            for i in range(len(dS22)):
                U = S22_inv * dS22[j] * S22_inv * dS22[i]
                V = np.trace(
                    np.matmul(
                        np.matmul(
                            np.matmul(
                                Sbar_inv,
                                dSbar[j],
                            ),
                            Sbar_inv,
                        ),
                        dSbar[i],
                    ))
                W = 2 * np.trace(
                    np.matmul(
                        np.matmul(
                            Sbar_inv,
                            dmbar[i],
                        ),
                        dmbar[j].T,
                    ))
                X = 2 * dmu[2, i] * S22_inv * dmu[2, j]
                I[j, i] = 0.5 * c_w * (V + W) + 0.5 * m_w * (U + X)

        return I
Example #31
0
def plot_prob_for_zero(c, b, s):
    from math import log, exp, factorial
    from dials.array_family import flex

    L = flex.double(flex.grid(100, 100))
    MASK = flex.bool(flex.grid(100, 100))
    c = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
    b = [bb / sum(b) for bb in b]
    s = [ss / sum(s) for ss in s]
    for BB in range(0, 100):
        for SS in range(0, 100):
            B = 0 + BB / 10000.0
            S = 0 + SS / 40.0
            LL = 0
            for i in range(len(b)):
                if B * b[i] + S * s[i] <= 0:
                    MASK[BB, SS] = True
                    LL = -999999
                    break
                else:
                    LL += (
                        c[i] * log(B * b[i] + S * s[i])
                        - log(factorial(c[i]))
                        - B * b[i]
                        - S * s[i]
                    )

            L[BB, SS] = LL
    index = flex.max_index(L)
    i = index % 100
    j = index // 100
    B = 0 + j / 10000.0
    S = 0 + i / 40.0
    print(flex.max(L), B, S)
    from matplotlib import pylab
    import numpy

    im = numpy.ma.masked_array(flex.exp(L).as_numpy_array(), mask=MASK.as_numpy_array())
    pylab.imshow(im)
    pylab.show()
    exit(0)
Example #32
0
    def __init__(self, experiments):
        """
        Do the labelling

        """
        from dials.algorithms.spot_prediction import PixelToMillerIndex
        from collections import defaultdict
        from math import floor, sqrt
        from dials.array_family import flex

        # Get the experiment
        experiment = experiments[0]

        # Get the image size
        xsize, ysize = experiment.detector[0].get_image_size()

        # A class to map pixels to miller indices
        transform = PixelToMillerIndex(
            experiment.beam, experiment.detector, experiment.crystal
        )

        # For each pixel, assign to a miller index and also compute the distance
        reflections = defaultdict(list)
        for j in range(ysize):
            for i in range(xsize):
                h = transform.h(0, i, j)
                h0 = tuple(map(lambda x: int(floor(x + 0.5)), h))
                d = sqrt(sum(map(lambda x, y: (x - y) ** 2, h, h0)))
                reflections[h0].append((i, j, d))

        # Initialise arrays
        self._indices = flex.miller_index()
        self._distance = flex.double(flex.grid(ysize, xsize))
        self._label = flex.int(flex.grid(ysize, xsize))
        self._pixels = defaultdict(list)
        for index, (h, pixels) in enumerate(reflections.iteritems()):
            self._indices.append(h)
            for i, j, d in pixels:
                self._distance[j, i] = d
                self._label[j, i] = index
                self._pixels[h].append((i, j))
Example #33
0
    def radial_average(self, reference=None):
        image = LunusDIFFIMAGE()
        working_img = self.lunus_data_scitbx.reshape(flex.grid(self.Isizey, self.Isizex))
        image.set_image(working_img)
        self.radial_avg = image.LunusRadialAvgim(self.beam_center_mm_x, self.beam_center_mm_y, self.pixel_size)

        if reference:
            np.savez("reference_radial_average", rad=self.radial_avg)
        else:
            pass
        
        return
Example #34
0
def gaussian(size, a, x0, sx):
    result = flex.real(flex.grid(size))
    index = [0 for i in range(len(size))]
    while True:
        result[index[::-1]] = evaluate_gaussian(index[::-1], a, x0, sx)
        for j in range(len(size)):
            index[j] += 1
            if index[j] < size[::-1][j]:
                break
            index[j] = 0
            if j == len(size) - 1:
                return result
Example #35
0
 def create_mask(self, size, x0, value):
     from scitbx.array_family import flex
     from math import sqrt
     mask = flex.int(flex.grid(size), 0)
     rad = min(s - c for s, c in zip(size, x0))
     for k in range(size[0]):
         for j in range(size[1]):
             for i in range(size[2]):
                 d = sqrt((j - x0[1])**2 + (i - x0[2])**2)
                 if d < rad:
                     mask[k, j, i] = value
     return mask
Example #36
0
  def tst_with_flat_background_partial(self):

    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
    c0 = add_poisson_noise(100 * p)
    b = flex.double(flex.grid(9, 9, 9), 1)
    m = flex.bool(flex.grid(9,9,9), True)
    c = c0 + add_poisson_noise(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 = ProfileFitter(cp, bp, mp, pp)
    I = fit.intensity()
    V = fit.variance()
    assert fit.niter() < fit.maxiter()

    Iknown = 99.06932141277105
    Vknown = 504.06932141277105

    # Test intensity is the same
    eps = 1e-7
    assert(abs(I[0] - Iknown) < eps)
    assert(abs(V[0] - Vknown) < eps)

    print 'OK'
Example #37
0
def test_deconvolve_3_with_no_background():
    np.random.seed(0)

    I0 = [1000, 2000, 3000]

    # Create profile
    p = generate_3_profiles()

    Ical = [[], [], []]
    for it in range(1):

        # Copy profile
        c = flex.double(flex.grid(40, 9, 9))
        for i in range(p.all()[0]):
            pp = p[i:i + 1, :, :, :]
            pp.reshape(c.accessor())
            cc = add_poisson_noise(I0[i] * pp)
            c += cc
        b = flex.double(flex.grid(40, 9, 9), 0)
        m = flex.bool(flex.grid(40, 9, 9), True)

        # Fit
        fit = ProfileFitter(c, b, m, p)
        intensity = fit.intensity()
        V = fit.variance()
        assert fit.niter() < fit.maxiter()

        for i in range(3):
            Ical[i].append(intensity[i])

    for i in range(3):
        Ical[i] = sum(Ical[i]) / len(Ical[i])

    Iknown = [1048.3221116842406, 1920.9035376774107, 2938.7743506383745]

    # Test intensity is the same
    eps = 1e-7
    for i in range(3):
        assert intensity[i] == pytest.approx(Iknown[i], abs=eps)
        assert V[i] == pytest.approx(Iknown[i], abs=eps)
Example #38
0
def px_coords(e, cs, detector, scan):
    xyz = flex.vec3_double(flex.grid(e.all()))
    for k in range(e.all()[0]):
        for j in range(e.all()[1]):
            for i in range(e.all()[2]):
                e1, e2, e3 = e[k, j, i]
                s1d, phid = to_s1_and_phi(e1, e2, e3, cs)
                # s1d = cs.to_beam_vector((e1, e2))
                # phid = cs.to_rotation_angle(e3)
                x, y = detector[0].get_ray_intersection_px(s1d)
                z = scan.get_array_index_from_angle(phid)
                xyz[k, j, i] = (x, y, z)
    return xyz
Example #39
0
def hot(greyscale_flex, params):
    '''Find hot pixels in the image (returns flex bool).'''
    from dials.algorithms.spot_finding.threshold import \
        DispersionThresholdStrategy
    from dials.array_family import flex

    thresholder = DispersionThresholdStrategy(gain=params.gain)
    mask = flex.bool(greyscale_flex.size(), True)
    mask.reshape(flex.grid(*greyscale_flex.focus()))

    threshold_mask = thresholder(greyscale_flex, mask=mask)

    return threshold_mask
Example #40
0
    def correlation(self, x):
        """
        The correlation of the Jacobian

        """
        J = self.jacobian(x)
        C = flex.double(flex.grid(J.all()[1], J.all()[1]))
        for j in range(C.all()[0]):
            for i in range(C.all()[1]):
                a = J[:, i:i + 1].as_1d()
                b = J[:, j:j + 1].as_1d()
                C[j, i] = flex.linear_correlation(a, b).coefficient()
        return C
Example #41
0
def read_images(experiments):
    from dials.array_family import flex

    xsize, ysize = experiments[0].detector[0].get_image_size()
    zsize = experiments[0].scan.get_num_images()

    data = flex.double(flex.grid(zsize, ysize, xsize))
    mask = flex.int(flex.grid(zsize, ysize, xsize))

    iset = experiments[0].imageset

    for i in range(len(iset)):
        print("Reading image %d" % i)
        d = iset.get_raw_data(i)[0].as_double()
        m = iset.get_mask(i)[0].as_1d().as_int()
        m.reshape(flex.grid(ysize, xsize))
        d.reshape(flex.grid(1, ysize, xsize))
        m.reshape(flex.grid(1, ysize, xsize))
        data[i:i + 1, :, :] = d
        mask[i:i + 1, :, :] = m

    return data, mask
Example #42
0
def gaussian(size, a, x0, sx):
    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
Example #43
0
    def second_derivatives(self):
        """
        Compute the second derivatives of Sigma w.r.t the parameters

        """
        l1 = self.params[0]

        d11 = (0, 0, 0, 0, 0, 0, 0, 0, 2)

        d2 = flex.mat3_double([d11])
        d2.reshape(flex.grid(1, 1))

        return d2
Example #44
0
def ts2(x):
  from numpy import median
  from dials.array_family import flex
  r = flex.double(flex.grid(len(x), len(x)))
  for j in range(len(x)):
    for i in range(len(x)):
      r[j,i] = (x[i] + x[j]) / 2.0


  from matplotlib import pylab
  pylab.imshow(r.as_numpy_array())
  pylab.show()
  return median(r)
Example #45
0
    def get_lunus_repl(self):
        P = Profiler("LUNUS")
        # first get the lunus image
        from lunus.command_line.filter_peaks import get_image_params
        from lunus import LunusDIFFIMAGE

        imageset = self.expt.imageset
        data = imageset[0]
        assert isinstance(
            data, tuple)  # assume a tuple of flex::double over detector panels
        # Instantiate a LUNUS diffraction image
        A = LunusDIFFIMAGE(len(data))

        # Populate the image with multipanel data

        for pidx in range(len(data)):
            A.set_image(pidx, data[pidx])

        # Define the LUNUS image parameters
        deck = '''
#lunus input deck
#punchim_xmin=1203
#punchim_ymin=1250
#punchim_xmax=2459
#punchim_ymax=1314
#windim_xmin=100
#windim_ymin=100
#windim_xmax=2362
#windim_ymax=2426
#thrshim_min=0
#thrshim_max=50
modeim_bin_size=1
modeim_kernel_width=15
'''
        image_params = get_image_params(imageset)

        # Set the LUNUS image parameters
        for pidx in range(len(image_params)):
            deck_and_extras = deck + image_params[pidx]
            A.LunusSetparamsim(pidx, deck_and_extras)
        A.LunusModeim()

        # Get the processed image
        lunus_filtered_data = flex.double()
        assert len(data) == 256  # Jungfrau
        for pidx in range(len(data)):
            aye_panel = A.get_image_double(pidx)
            assert aye_panel.focus() == (254, 254)
            lunus_filtered_data.extend(aye_panel.as_1d())
        lunus_filtered_data.reshape(flex.grid((256, 254, 254)))
        self.lunus_filtered_data = lunus_filtered_data.as_numpy_array()
Example #46
0
def make_test_image():
    from scitbx import matrix
    from dials.array_family import flex
    import random
    import math

    xmin, xmax = 0, 4272
    ymin, ymax = 0, 2848
    buffer = 100
    n = 30

    # generate n random positions - well within field of view

    x = flex.double(n)
    y = flex.double(n)
    z = flex.double(n, 0.0)

    for j in range(n):
        x[j] = random.uniform(xmin + buffer, xmax - buffer)
        y[j] = random.uniform(ymin + buffer, ymax - buffer)

    r = background(flex.double(flex.grid(ymax, xmax), 0.0), 3)
    g = background(flex.double(flex.grid(ymax, xmax), 0.0), 3)
    b = background(flex.double(flex.grid(ymax, xmax), 0.0), 3)

    # add some stars

    for xy in zip(x, y):
        star_x, star_y = random_positions(1000, 1).parts()
        star_x += xy[0]
        star_y += xy[1]
        for i, j in zip(star_x.iround(), star_y.iround()):
            r[j, i] += 1
            g[j, i] += 1
            b[j, i] += 1

    from astrotbx.input_output.saver import save_image
    save_image('example.png', r, g, b)
Example #47
0
def profile3d(p, vmin=None, vmax=None):
  ''' Print a 3D profile. '''
  from dials.array_family import flex
  if vmin is None:
    vmin = flex.min(p)
  if vmax is None:
    vmax = flex.max(p)
  nz, ny, nx = p.all()
  text = []
  for k in range(nz):
    p2 = p[k:k+1,:,:]
    p2.reshape(flex.grid(ny, nx))
    text.append(profile2d(p2, vmin=vmin, vmax=vmax))
  return '\n'.join(text)
Example #48
0
def hot_pixel_mask(imageset, reflections):
  depth = imageset.get_array_range()[1] - imageset.get_array_range()[0]
  xylist = filter_reflections(reflections, depth)

  from dials.array_family import flex

  mask = flex.bool(flex.grid(reversed(imageset.get_image_size())), True)

  for x, y in xylist:
    mask[y, x] = False

  print 'Found %d hot pixels' % len(xylist)

  return (mask,)
Example #49
0
def gaussian(size, a, x0, sx):

    from dials.array_family import flex

    result = flex.real(flex.grid(size))
    index = [0 for i in range(len(size))]
    while True:
        result[index[::-1]] = evaluate_gaussian(index[::-1], a, x0, sx)
        for j in range(len(size)):
            index[j] += 1
            if index[j] < size[::-1][j]:
                break
            index[j] = 0
            if j == len(size) - 1:
                return result
    def d2f(I):
      mask = flex.bool(flex.grid(9,9,9), False)
      for k in range(9):
        for j in range(9):
          for i in range(9):
            dx = 5 * (i - 4.5) / 4.5
            dy = 5 * (j - 4.5) / 4.5
            dz = 5 * (k - 4.5) / 4.5
            dd = sqrt(dx**2 + dy**2 + dz**2)
            if dd <= 3:
              mask[k,j,i] = True

      mask = mask.as_1d() & (ref_P.as_1d() > 0)
      p = ref_P.as_1d().select(mask)
      c = max_P.as_1d().select(mask)
      return flex.sum(2*c*c*p*p / (p*I)**3)
    def normalize_profile(self, profile):
        from scitbx.array_family import flex

        max_profile = flex.max(profile)
        threshold = self.threshold * max_profile
        sum_profile = 0.0
        for i in range(len(profile)):
            if profile[i] > threshold:
                sum_profile += profile[i]
            else:
                profile[i] = 0.0

        result = flex.double(flex.grid(profile.all()))
        for i in range(len(profile)):
            result[i] = profile[i] / sum_profile

        return result
Example #52
0
    def __call__(self, img_flex, palette_in, min_i, max_i):
        flex_2d_data = img_flex.as_double()
        flex_2d_mask = flex.double(
            flex.grid(flex_2d_data.all()[0], flex_2d_data.all()[1]), 0
        )
        arr_i = self.arr_img(
            flex_2d_data, flex_2d_mask, i_min=min_i, i_max=max_i, palette=palette_in
        )

        q_img = QImage(
            arr_i.data,
            np.size(arr_i[0:1, :, 0:1]),
            np.size(arr_i[:, 0:1, 0:1]),
            QImage.Format_RGB32,
        )

        return q_img
Example #53
0
  def __call__(self, image, mask):
    '''
    Call the thresholding function

    :param image: The image to process
    :param mask: The mask to use
    :return: The thresholded image

    '''
    from dials.algorithms.image import threshold
    from dials.array_family import flex

    # Initialise the algorithm
    try:
      algorithm = self.algorithm[image.all()]
    except Exception:
      algorithm = threshold.DispersionThreshold(
        image.all(),
        self._kernel_size,
        self._n_sigma_b,
        self._n_sigma_s,
        self._threshold,
        self._min_count)
      self.algorithm[image.all()] = algorithm

    # Set the gain
    if self._gain is not None:
      assert(self._gain > 0)
      self._gain_map = flex.double(image.accessor(), self._gain)
      self._gain = None

    # Compute the threshold
    result = flex.bool(flex.grid(image.all()))
    if self._gain_map:
      algorithm(image, mask, self._gain_map, result)
    else:
      algorithm(image, mask, result)

    # Return the result
    return result
Example #54
0
def test(counts, background_shape, signal_shape):
  S = 1
  B = 1
  for i in range(20):
    S, B = iteration(counts, background_shape, signal_shape, B, S)
    print S, B

  sigma_b, sigma_s = sigmas(counts, background_shape, signal_shape, B, S)
  print sigma_b, sigma_s
  from dials.array_family import flex
  Fs = sum(signal_shape)
  Fb = sum(background_shape)
  Rs = flex.double(flex.grid(100, 100))
  for B in range(1, 101):
    for S in range(1, 101):
      Fb2 = sum([b*c/(B*b+S*s) for c, b, s in zip(counts, background_shape, signal_shape)])
      Fs2 = sum([s*c/(B*b+S*s) for c, b, s in zip(counts, background_shape, signal_shape)])
      R = (Fb2 - Fb)**2 + (Fs2 - Fs)**2
      Rs[B-1,S-1] = Fs2 - Fs
  from matplotlib import pylab
  pylab.imshow(Rs.as_numpy_array())
  pylab.show()
Example #55
0
  def tst_does_bbox_contain_bad_pixels(self):
    from dials.array_family import flex
    from dials.model.data import Shoebox
    from random import randint

    mask = flex.bool(flex.grid(100, 100), True)
    for j in range(100):
      for i in range(40, 60):
        mask[j,i] = False
        mask[i,j] = False

    shoebox = flex.shoebox(1000)
    res = flex.bool(1000)
    for i in range(1000):
      x0 = randint(0, 90)
      y0 = randint(0, 90)
      z0 = randint(0, 90)
      x1 = randint(1, 10) + x0
      y1 = randint(1, 10) + y0
      z1 = randint(1, 10) + z0

      shoebox[i] = Shoebox((x0, x1, y0, y1, z0, z1))

      res2 = False
      if x0 >= 40 and x0 < 60:
        res2 = True
      if x1 > 40 and x1 <= 60:
        res2 = True
      if y0 >= 40 and y0 < 60:
        res2 = True
      if y1 > 40 and y1 <= 60:
        res2 = True

      res[i] = res2

    assert(shoebox.does_bbox_contain_bad_pixels(mask) == res)

    # Test passed
    print 'OK'
Example #56
0
  def tst_consistent(self):

    from random import randint
    from dials.model.data import Shoebox
    from dials.array_family import flex

    shoebox = flex.shoebox(10)

    for i in range(10):
      x0 = randint(0, 1000)
      y0 = randint(0, 1000)
      z0 = randint(0, 1000)
      x1 = randint(1, 10) + x0
      y1 = randint(1, 10) + y0
      z1 = randint(1, 10) + z0
      shoebox[i] = Shoebox((x0, x1, y0, y1, z0, z1))

    assert(shoebox.is_consistent() == flex.bool(10, False))

    for i in range(10):
      shoebox[i].allocate()

    assert(shoebox.is_consistent() == flex.bool(10, True))

    for i in [0, 2, 4, 6, 8]:
      shoebox[i].data.resize(flex.grid(10, 10, 10))

    assert(shoebox.is_consistent() == flex.bool([False, True] * 5))

    for i in range(10):
      shoebox[i].deallocate()

    assert(shoebox.is_consistent() == flex.bool(10, False))

    # Test passed
    print 'OK'
Example #57
0
 0,
 4,
 4,
 3,
 0,
 1,
 4,
 1,
 1,
 1,
 6]
    # a[4] = 10
    # a[50] = 100

    X = flex.double([1] * len(a))
    X.reshape(flex.grid(len(a), 1))

    Y = flex.double(a)
    B = flex.double([0])

    result = robust_glm(X, Y, B, family="poisson")
    print list(result.parameters())


  from matplotlib import pylab
  print "MOM1: ", sum(means1) / len(means1)
  print "MOM2: ", sum(means2) / len(means2)
  pylab.plot(means1, color='black')
  pylab.plot(means2, color='blue')
  pylab.show()
Example #58
0
  # Get the imageset
  assert len(experiments) == 1
  assert len(reflections) == 1
  reflections = reflections[0]
  imageset = experiments[0].imageset

  # Create the reflection lookup
  bbox = reflections['bbox']
  reflection_lookup = defaultdict(list)
  for i in range(len(bbox)):
    for j in range(bbox[i][4],bbox[i][5]):
      reflection_lookup[j].append(i)

  width, height = experiments[0].detector[0].get_image_size()
  sum_background = flex.double(flex.grid(height, width), 0)
  sum_sq_background = flex.double(flex.grid(height, width), 0)
  count = flex.int(flex.grid(height, width), 0)

  # Loop through all images
  print "START"
  for frame in range(len(imageset)):

    # Get the subset of reflections on this image and compute the mask
    subset = reflections.select(flex.size_t(reflection_lookup[frame]))

    subset['shoebox'] = flex.shoebox(
      subset['panel'],
      subset['bbox'],
      allocate=True)
  def run(self):
    from dials.array_family import flex
    import cPickle as pickle
    from math import sqrt
    from dials.algorithms.shoebox import MaskCode
    print self.shoebox_filename
    # Read the data
    rtable = flex.reflection_table.from_pickle(self.reflection_filename)
    shoeboxes, masks = pickle.load(open(self.shoebox_filename, "r"))
    assert(len(rtable) == len(shoeboxes))
    assert(len(rtable) == len(masks))

    # Compute the background for each reflection and check against the values
    # read from the mosflm.lp file. Currently this fails for 1 strange
    # reflection whose pixel values in the mosflm file do not match those
    # extracted from the images.
    count = 0
    VAR1 = []
    VAR2 = []
    DIFF = []
    for i in range(len(rtable)):
      xdet, ydet = rtable[i]["xy"]
      nx = rtable[i]['nx']
      ny = rtable[i]['ny']
      nc = rtable[i]['nc']
      nrx = rtable[i]['nrx']
      nry = rtable[i]['nry']
      bbox = rtable[i]['bbox']
      I = rtable[i]['intensity.sum.value']
      Ivar = rtable[i]['intensity.sum.variance']
      lp = rtable[i]['lp']
      data = shoeboxes[i].as_double()
      mask = masks[i]
      fraction = 1.0
      nsigma = 4
      try:
        # model = PlaneModel(data, mask, fraction, nsigma)
        assert(len(data.all()) == 2)
        assert(len(mask.all()) == 2)
        data.reshape(flex.grid(1, *data.all()))
        mask.reshape(flex.grid(1, *mask.all()))
        self.outlier_rejector(data, mask)
        mask2 = (mask.as_1d() & int(MaskCode.BackgroundUsed)) != 0
        mask2.reshape(flex.grid(*mask.all()))
        model = self.linear_modeller.create(data, mask2)
      except Exception:
        count += 1
        raise
        continue
      # n = model.noutlier()
      assert(len(model.params()) == 3)
      hy = data.all()[1] // 2
      hx = data.all()[2] // 2
      c1 = model.params()[0]
      a1 = model.params()[1]
      b1 = model.params()[2]
      c3 = c1 + a1*(0.5 + hx) + b1*(0.5 + hy)
      # a1 = model.a()
      # b1 = model.b()
      # c1 = model.c()
      a2 = rtable[i]['background'][0]
      b2 = rtable[i]['background'][1]
      c2 = rtable[i]['background'][2]

      try:
        assert(abs(a1 - b2) < 0.01)
        assert(abs(b1 + a2) < 0.01)
        assert(abs(c3 - c2) < 0.1)
      except Exception:
        count += 1
        continue
        #print "BG %d:(%.2f, %.2f, %.1f), (%.2f, %.2f, %.1f): %d" % \
          #(i, a1, b1, c1, a2, b2, c2, n)
        #print "X, Y: ", xdet, ydet
        #print "NX, NY: ", nx, ny
        #print "NRX, NRY, NC", nrx, nry, nc
        #print int(floor(xdet + 0.5)) - nx // 2, int(floor(ydet + 0.5)) - ny // 2
        #print "BBOX: ", bbox
        #print "N Outliers: ", model.noutlier()
        #print "N Background: ", model.nbackground()
        #print "Max DIff: ", model.maxdiff()
        #print data.as_numpy_array().transpose()[::-1,::-1]
        #print mask.as_numpy_array().transpose()[::-1,::-1]
        #raise

      background = data.as_double()
      # hy = background.all()[1] // 2
      # hx = background.all()[2] // 2
      for jj in range(background.all()[1]):
        for ii in range(background.all()[2]):
          # x = ii - hx
          # y = jj - hy
          x = ii + 0.5
          y = jj + 0.5
          background[0, jj,ii] = a1 * x + b1 * y + c1

      # Test the summation results. Edge reflections use profile fitted
      # intensity in MOSFLM. Therefore ignore these. There also appears to be a
      # some discrepancy with very low <= 0 reflections where an extra 0.5 is
      # added. Not sure why this is so ignore these reflections as well.
      from dials.algorithms.integration.sum import integrate_by_summation
      intensity = integrate_by_summation(data.as_double(), background, mask)
      I2 = intensity.intensity()
      Ivar2 = intensity.variance()
      I1 = I
      Ivar1 = Ivar
      if mask.count(0) == 0 and mask.count(2) == 0 and I1 > 0:
        VAR1.append(sqrt(Ivar1))
        VAR2.append(sqrt(Ivar2))
        DIFF.append(sqrt(Ivar1) - sqrt(Ivar2))
        try:
          assert(abs(I1 - I2) < 1.0)
          assert(abs(sqrt(Ivar1) - sqrt(Ivar2)) < 1.0)
        except Exception:
          count += 1
          #import numpy
          #numpy.set_printoptions(precision=4, linewidth=200)
          #print "# %d" % i
          #print "I: %f, %f, %f" % (I2, I1, lp)
          #print "DEBUG: ", c1 * 25
          #print "PF: %f" % rtable[i]['intensity.prf.value']
          #print "BG (%.4f, %.4f, %.4f), (%.2f, %.2f, %.1f): %d" % \
            #(a1, b1, c1, a2, b2, c2, n)
          #print "X, Y: ", xdet, ydet
          #print "NX, NY: ", nx, ny
          #print "NRX, NRY, NC", nrx, nry, nc
          #print int(floor(xdet + 0.5)) - nx // 2, int(floor(ydet + 0.5)) - ny // 2
          #print "BBOX: ", bbox
          #print "N Outliers: ", model.noutlier()
          #print "N Background: ", model.nbackground()
          #print "Max DIff: ", model.maxdiff()
          #temp = (mask == MaskCode.Valid | MaskCode.Foreground).as_1d().as_int()
          #temp.resize(flex.grid(*data.all()))
          #temp = temp.as_double()
          #print data.as_numpy_array().transpose()[::-1,::-1]
          #print (background * temp).as_numpy_array().transpose()[::-1,::-1]
          #print mask.as_numpy_array().transpose()[::-1,::-1]
          #print ((data.as_double() - background) * temp).as_numpy_array().transpose()[::-1,::-1]
          #raise
          continue

    # Only 1 should fail
    assert(count == 1)
    print 'OK'
Example #60
0
#
#  Author: Luis Fuentes-Montero (Luiso)
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package.

from __future__ import division
from dials.array_family import flex

import boost.python
from dials_viewer_ext import gen_font_img

if(__name__ == "__main__"):
  lst_flex = []
  lst_flex_norm = []

  size_xyz = 7

  arr_2d = flex.double(flex.grid(size_xyz, size_xyz), 00)

  tot = 0.0
  for row in range(size_xyz):
    for col in range(size_xyz):
      arr_2d[row, col] += (row * 2 + col * 2)
      tot += arr_2d[row, col]


print "arr_2d.as_numpy_array() = \n", arr_2d.as_numpy_array()
print "gen_font_img(arr_2d).as_numpy_array() = \n", gen_font_img(arr_2d).as_numpy_array()