<commit_before>#!/usr/bin/env python
#
# reciprocal_space.py
#
#  Copyright (C) 2014 <NAME>, <NAME>
#
#  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


class Simulator(object):
  ''' Class to help with simulation from reciprocal space. '''

  def __init__(self, experiment, sigma_b, sigma_m, n_sigma):
    ''' Initialise with models and parameters. '''
    self.experiment = experiment
    self.sigma_b = sigma_b
    self.sigma_m = sigma_m
    self.n_sigma = n_sigma

  def with_given_intensity(self, N, I, B):
    ''' Generate reflections with a given intensity and background. '''
    from dials.array_family import flex
    return self.with_individual_given_intensity(
      N,
      flex.int(N, I),
      flex.int(N, B))

  def with_random_intensity(self, N, Imax, Bmax):
    ''' Generate reflections with a random intensity and background. '''
    from dials.array_family import flex
    if Imax == 0:
      I = flex.size_t(N).as_int()
    else:
      I = flex.random_size_t(N, Imax).as_int()
    if Bmax == 0:
      B = flex.size_t(N).as_int()
    else:
      B = flex.random_size_t(N, Bmax).as_int()
    return self.with_individual_given_intensity(N, I, B)

  def with_individual_given_intensity(self, N, I, B):
    ''' Generate reflections with given intensity and background. '''
    from dials.algorithms.shoebox import MaskForeground
    from dials.array_family import flex
    from dials.util.command_line import Command, ProgressBar
    from dials.algorithms.reflection_basis import CoordinateSystem
    from scitbx.random import variate, poisson_distribution
    from dials.algorithms.simulation import simulate_reciprocal_space_gaussian
    import random

    # Check the lengths
    assert(N == len(I))
    assert(N == len(B))

    # Generate some predictions
    refl = self.generate_predictions(N)

    # Calculate the signal
    progress = ProgressBar(title='Calculating signal for %d reflections' % len(refl))
    s1 = refl['s1']
    phi = refl['xyzcal.mm'].parts()[2]
    bbox = refl['bbox']
    shoebox = refl['shoebox']
    m = int(len(refl) / 100)
    for i in range(len(refl)):
      simulate_reciprocal_space_gaussian(
        self.experiment.beam,
        self.experiment.detector,
        self.experiment.goniometer,
        self.experiment.scan,
        self.sigma_b,
        self.sigma_m,
        s1[i],
        phi[i],
        bbox[i],
        I[i],
        shoebox[i].data)
      if i % m == 0:
        progress.update(100.0 * float(i) / len(refl))
    progress.finished('Calculated signal impacts for %d reflections' % len(refl))

    # Calculate the background
    progress = ProgressBar(title='Calculating background for %d reflections' % len(refl))
    for l in range(len(refl)):
      background = flex.double(flex.grid(shoebox[l].size()), 0.0)
      g = variate(poisson_distribution(mean = B[l]))
      for k in range(background.all()[0]):
        for j in range(background.all()[1]):
          for i in range(background.all()[2]):
            background[k, j, i] += g.next()
      shoebox[l].data += background
      if l % m == 0:
        progress.update(100.0 * float(l) / len(refl))
      progress.update(100.0 * float(l) / len(refl))
    progress.finished('Calculated background for %d reflections' % len(refl))

    # Save the expected intensity and background
    refl['intensity.sim'] = I
    refl['background.sim'] = B

    # Return the reflections
    return refl

  def generate_predictions(self, N):
    ''' Generate some reflections. '''
    from dials.algorithms.shoebox import MaskForeground
    from dials.array_family import flex
    from dials.util.command_line import Command
    from dials.algorithms.reflection_basis import CoordinateSystem
    from dials.algorithms import filtering
    import random

    # Generate a list of reflections
    refl = flex.reflection_table.from_predictions(self.experiment)
<commit_msg>Added check for zero background and intensity to speed up simulation<commit_after>#!/usr/bin/env python
#
# reciprocal_space.py
#
#  Copyright (C) 2014 Diamond Light Source, <NAME>
#
#  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


class Simulator(object):
  ''' Class to help with simulation from reciprocal space. '''

  def __init__(self, experiment, sigma_b, sigma_m, n_sigma):
    ''' Initialise with models and parameters. '''
    self.experiment = experiment
    self.sigma_b = sigma_b
    self.sigma_m = sigma_m
    self.n_sigma = n_sigma

  def with_given_intensity(self, N, I, B, refl=None):
    ''' Generate reflections with a given intensity and background. '''
    from dials.array_family import flex
    return self.with_individual_given_intensity(
      N,
      flex.int(N, I),
      flex.int(N, B),
      refl)

  def with_random_intensity(self, N, Imax, Bmax, refl=None):
    ''' Generate reflections with a random intensity and background. '''
    from dials.array_family import flex
    if Imax == 0:
      I = flex.size_t(N).as_int()
    else:
      I = flex.random_size_t(N, Imax).as_int()
    if Bmax == 0:
      B = flex.size_t(N).as_int()
    else:
      B = flex.random_size_t(N, Bmax).as_int()
    return self.with_individual_given_intensity(N, I, B, refl)

  def with_individual_given_intensity(self, N, I, B, refl=None):
    ''' Generate reflections with given intensity and background. '''
    from dials.algorithms.shoebox import MaskForeground
    from dials.array_family import flex
    from dials.util.command_line import Command, ProgressBar
    from dials.algorithms.reflection_basis import CoordinateSystem
    from scitbx.random import variate, poisson_distribution
    from dials.algorithms.simulation import simulate_reciprocal_space_gaussian
    import random

    # Check the lengths
    assert(N == len(I))
    assert(N == len(B))

    # Generate some predictions
    if refl is None:
      refl = self.generate_predictions(N)

    # Calculate the signal
    progress = ProgressBar(title='Calculating signal for %d reflections' % len(refl))
    s1 = refl['s1']
    phi = refl['xyzcal.mm'].parts()[2]
    bbox = refl['bbox']
    shoebox = refl['shoebox']
    m = int(len(refl) / 100)
    for i in range(len(refl)):
      if I[i] > 0:
        simulate_reciprocal_space_gaussian(
          self.experiment.beam,
          self.experiment.detector,
          self.experiment.goniometer,
          self.experiment.scan,
          self.sigma_b,
          self.sigma_m,
          s1[i],
          phi[i],
          bbox[i],
          I[i],
          shoebox[i].data)
      if i % m == 0:
        progress.update(100.0 * float(i) / len(refl))
    progress.finished('Calculated signal impacts for %d reflections' % len(refl))

    # Calculate the background
    progress = ProgressBar(title='Calculating background for %d reflections' % len(refl))
    for l in range(len(refl)):
      if B[l] > 0:
        background = flex.double(flex.grid(shoebox[l].size()), 0.0)
        g = variate(poisson_distribution(mean = B[l]))
        for k in range(background.all()[0]):
          for j in range(background.all()[1]):
            for i in range(background.all()[2]):
              background[k, j, i] += g.next()
        shoebox[l].data += background
      if l % m == 0:
        progress.update(100.0 * float(l) / len(refl))
      progress.update(100.0 * float(l) / len(refl))
    progress.finished('Calculated background for %d reflections' % len(refl))

    # Save the expected intensity and background
    refl['intensity.sim'] = I
    refl['background.sim'] = B

    # Return the reflections
    return refl

  def generate_predictions(self, N):
    ''' Generate some reflections. '''
    from dials.algorithms.shoebox import MaskForeground
    from dials.array_family import flex
    from dials.util.command_line import Command
    from dials.algorithms.reflection_basis import CoordinateSystem
    from dials.algorithms import filtering
    import random

    # Generate a list of reflections
    refl = flex.reflection_table.from_predictions(self.experiment)

{'fraction_non_alphanumeric': 0.10858678392573592, 'fraction_numerical': 0.008061561011359472, 'mean_word_length': 3.0615079365079363, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4613891', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2540, 'n_words': 691}