Example #1
0
def map_calculate_rings(mp):

  # set up parameters
  mp.structure_generator.random.setstate(mp.random_state)

  es = ewald_sphere()
  es.set_wavelength(mp.beam_properties.wavelength)
  es.set_distance(mp.detector_properties.distance)

  ic = image_composer()
  ic.set_detector_size(mp.detector_properties.detector_size)
  ic.set_beam_center(mp.detector_properties.beam_center)
  ic.set_pixel_size(mp.detector_properties.pixel_size)
  ic.set_ewald_sphere(es)

  im = image_simulator()
  im.structures = mp.structure_generator
  im.image_composer = ic
  im.cached_h = mp.h

  # calculate structure factors
  for i in xrange(mp.n_images):
    im.structures.randomize()
    im.sum_structure_factors()

    # copy intensities to full image
    sf = im.structure_factors
    all_sf = flex.complex_double((mp.detector_properties.detector_size[0]+1)*\
                                 (mp.detector_properties.detector_size[1]+1),\
                                 complex(0.0,0.0))
    k = 0
    for j in xrange(len(mp.use_index)):
      if (mp.use_index[j]):
        all_sf[j] = sf[k]
        k += 1
    im.structure_factors = all_sf

    image_data = im.build_image(n_photons=bp.flux*bp.t)

    if (True):
      for q_i in [0.01, 0.1, 0.2]:
        print q_i, I_q(ic.get_q(),image_data,q_i,mp.dq)
      write_image(file_name='ring.png',
                  detector_size=mp.detector_properties.detector_size,
                  image_data=image_data)

    if (False):
      file_name = './test_rings/' + str(os.getpid()) + '_' + str(i)
      f = open(file_name,'wb')
      pickle.dump(image_data,f,2)
      f.close()

  return mp.n_images
Example #2
0
def test_ewald_sphere():
  es = ewald_sphere()
  es.set_wavelength(1.0)
  es.set_distance(10.0)
  xy = (0.0,0.0)
  h = es.get_h(xy)
  q = es.h_to_q(h)
  assert(approx_equal(h,(0.0,0.0,0.0)))
  assert(approx_equal(q,0.0))
  xy = (10.0,10.0)
  h = es.get_h(xy)
  q = es.h_to_q(h)
  assert(approx_equal(h,(0.57735026919, 0.57735026919, -0.42264973081)))
  assert(approx_equal(q,5.77677116966))
def map_model_I_q(mp):

    # set up parameters
    mp.structure_generator.random.setstate(mp.random_state)

    es = ewald_sphere()
    es.set_wavelength(mp.beam_properties.wavelength)
    es.set_distance(mp.detector_properties.distance)

    ic = image_composer()
    ic.set_detector_size(mp.detector_properties.detector_size)
    ic.set_beam_center(mp.detector_properties.beam_center)
    ic.set_pixel_size(mp.detector_properties.pixel_size)
    ic.set_ewald_sphere(es)

    # initialize image simulator
    im = image_simulator()
    im.structures = mp.structure_generator
    im.image_composer = ic

    # make images
    sums = flex.vec2_double(mp.n_bins)
    for i in xrange(mp.n_images):
        im.structures.randomize()
        im.sum_structure_factors_gpu(gpu=mp.gpu)
        image_data = im.build_image(n_photons=(mp.beam_properties.flux *
                                               mp.beam_properties.t))
        if (False):
            for q_i in [0.01, 0.1, 0.2]:
                print q_i, I_q(im.cached_q, image_data, q_i, mp.dq)
            write_image(detector_size=mp.detector_properties.detector_size,
                        image_data=image_data)
        if (True):
            file_name = '/scratch/home/bkpoon/images/' + str(
                os.getpid()) + '_' + str(i)
            f = open(file_name, 'wb')
            pickle.dump(im.structure_factors, f, 2)
            f.close()

        # convert image data to I(q)
        mv = mean_and_variance_by_q(mp.q_min, mp.q_max, mp.dq, mp.n_bins,
                                    im.cached_q, image_data)
        sums += mv

    return sums
def map_random_image(p=None):

    result = [None for i in xrange(p.n_images)]

    # set random state for current thread
    p.structure_generator.random.setstate(p.random_state)

    es = ewald_sphere()
    es.set_wavelength(p.beam_properties.wavelength)
    es.set_distance(p.detector_properties.distance)

    ic = image_composer()
    ic.set_detector_size(p.detector_properties.detector_size)
    ic.set_beam_center(p.detector_properties.beam_center)
    ic.set_pixel_size(p.detector_properties.pixel_size)
    ic.set_ewald_sphere(es)

    im = image_simulator()
    im.structures = p.structure_generator
    im.image_composer = ic

    h = ic.cache_h()
    sf_length = (p.detector_properties.detector_size[0] + 1) *\
                (p.detector_properties.detector_size[1] + 1)
    n_photons = p.beam_properties.flux * p.beam_properties.t

    for i in xrange(p.n_images):
        sf = flex.complex_double(sf_length, 0.0)
        im.structures.species[0].n_copies = p.n_particles[i]
        im.structures.randomize()
        for j in xrange(p.n_particles[i]):
            image_name = p.base_image_directory + im.structures.random.choice(
                image_list)
            t = im.structures.translations[0][j]
            f = open(image_name, 'rb')
            sf_tmp = pickle.load(f)
            f.close()
            sf_tmp = apply_translation(sf_tmp, h, t)
            sf += sf_tmp
        im.structure_factors = sf
        image_data = im.build_image(n_photons=n_photons)
        result[i] = image_data.deep_copy()

    return result
Example #5
0
  mp.detector_properties = dp
  mp.beam_properties = bp

  mp.n_images = 0
  mp.n_bins = 100
  mp.q_min = 0.005
  mp.q_max = 0.45
  mp.dq = (mp.q_max - mp.q_min)/mp.n_bins

  s = structure_generator()
  s.add_species(pdb_input=pdb.input('6LYZ.pdb'),n_copies=2)
  mp.structure_generator = s

  rings = [0.01, 0.1, 0.2]

  es = ewald_sphere()
  es.set_wavelength(bp.wavelength)
  es.set_distance(dp.distance)

  ic = image_composer()
  ic.set_detector_size(dp.detector_size)
  ic.set_beam_center(dp.beam_center)
  ic.set_pixel_size(dp.pixel_size)
  ic.set_ewald_sphere(es)

  q = ic.get_q()

  # flag pixel corners for each ring
  pixel_xy = list()
  all_h = ic.cache_h()
  use_index = flex.bool(len(all_h),False)
def map_correlate_gpu(p=None):

    maxint = 2147483647

    # construct image objects
    es = ewald_sphere()
    es.set_wavelength(p.model_properties.beam_properties.wavelength)
    es.set_distance(p.model_properties.detector_properties.distance)

    dg = detector_geometry()
    dg.set_corner_position(
        p.model_properties.detector_properties.corner_position)
    dg.set_detector_size(p.model_properties.detector_properties.detector_size)
    dg.set_pixel_size(p.model_properties.detector_properties.pixel_size)

    ib = image_base()
    ib.set_detector_geometry(dg)
    ib.set_ewald_sphere(es)

    h = ib.get_corner_h()
    q = ib.get_center_q()
    phi = ib.get_center_phi()

    im = image_simulator()
    im.cached_h = h
    im.cached_q = q
    im.structures = p.model_properties.structure_generator
    im.structures.random.setstate(p.model_properties.random_state)
    im.image_base = ib
    n_photons = p.model_properties.beam_properties.flux *\
                p.model_properties.beam_properties.t
    if (im.structures.use_solvent):
        im.bulk_solvent_image = solvent_image(q)
        r_e = 2.818e-5  # radius of electron in Angstroms
        d = p.model_properties.detector_properties.distance
        i000 = flex.max(im.bulk_solvent_image)
        # 1 g/cm^3 (1 mol/18 g) (N_A HOH/1 mol) (10 e/1 HOH) (1 cm^3/1e24 A^3)
        # 0.33456 e / A^3
        particle_volume = 0.0
        for i in xrange(len(im.structures.species)):
            particle_volume += im.structures.species[i].n_copies*(4.0/3.0)*math.pi*\
                               math.pow(im.structures.species[i].radius,3)
        bulk_solvent_electrons = 0.33456 *(im.structures.box_size *\
                                           im.structures.box_size *\
                                           im.structures.box_size - particle_volume)
        pa = p.model_properties.detector_properties.pixel_size[0] *\
             p.model_properties.detector_properties.pixel_size[1]
        scale = bulk_solvent_electrons / i000 * n_photons * (r_e * r_e) / (
            d * d) * pa
        im.bulk_solvent_image = scale * im.bulk_solvent_image

    # results array
    ac = c2_tiles()
    ac.set_ewald_sphere(es)
    ac.add_geometry(dg)
    ac.set_q_limits(p.q_min, p.q_max)
    ac.set_ring_pixel_sizes(p.q_pixel_depth, p.phi_pixel_radius)
    ac.initialize()
    n_q = ac.get_n_rings()
    ac_average = [None for i in xrange(n_q)]
    ac_variance = [0.0 for i in xrange(n_q)]
    for i in xrange(n_q):
        ac_average[i] = flex.double(len(ac.get_c2(i)), 0.0)
    sc_average = flex.double(n_q, 0.0)
    sc_variance = flex.double(n_q, 0.0)

    # select rings if available
    if (p.ring_indices is not None):
        tile = 0
        ring_pixel_indices = flex.int()
        for ring_index in p.ring_indices:
            ring_pixel_indices.extend(ac.get_pixel_indices(ring_index, tile))
        ring_h = flex.vec3_double(ring_pixel_indices.size())
        for i in xrange(ring_pixel_indices.size()):
            ring_h[i] = h[ring_pixel_indices[i]]
        im.cached_h = ring_h

    # construct composite images and process
    for i in xrange(p.model_properties.n_images):
        for j in xrange(len(p.mean)):
            im.structures.species[j].n_copies = p.n_particles[j][i]
        im.structures.randomize()
        image_data = im.build_image(n_photons=n_photons, coherent=p.coherent)

        # copy rings into image
        if (p.ring_indices is not None):
            ring_image_data = image_data.deep_copy()
            image_data = flex.double(h.size(), flex.min(ring_image_data))
            for j in xrange(ring_pixel_indices.size()):
                image_data[ring_pixel_indices[j]] = ring_image_data[j]
        image_data = ib.integrate(image_data)

        if (im.structures.use_solvent):
            image_data = image_data + im.bulk_solvent_image

        # apply Poisson noise to photon count
        if (p.photon_poisson_noise):
            image_data = multiple_poisson\
                         (image_data,
                          im.structures.random.randint(0,maxint)).as_double()

        # subtract background solvent
        if (im.structures.use_solvent):
            image_data = image_data - im.bulk_solvent_image
            image_data = set_negative_to_zero(image_data)

        # process image
        ac.reset_intensities()
        ac.add_intensities(image_data)
        if (p.ring_indices is None):
            ac.process_intensities()
            for j in xrange(n_q):
                current_c2 = ac.get_c2(j)
                ac_average[j] += current_c2
                ac_variance[j] += current_c2 * current_c2
        else:
            for ring_index in p.ring_indices:
                ac.process_ring(ring_index)
                current_c2 = ac.get_c2(ring_index)
                ac_average[ring_index] += current_c2
                ac_variance[ring_index] += current_c2 * current_c2
        current_sc = ac.get_mean_ring_intensities()
        sc_average += current_sc
        sc_variance += current_sc * current_sc

        ## write_image(file_name='test.png',
        ##             detector_size=p.model_properties.detector_properties.detector_size,
        ##             image_data=image_data)
        ## write_image(file_name='rings.png',
        ##             detector_size=p.model_properties.detector_properties.detector_size,
        ##             image_data=ac.bin_mask().as_double())

    ring_q = flex.double(n_q)
    for i in xrange(n_q):
        ring_q[i] = ac.get_ring_q(i)

    result = map_correlate_results()
    result.ring_q = ring_q
    result.ac_average = ac_average
    result.ac_variance = ac_variance
    result.sc_average = sc_average
    result.sc_variance = sc_variance
    return result