<commit_before>import echidna.core.spectra as spectra


class Shift(object):
    """ A class for shifting the parameter space of a spectra.

    Attributes:
      _shift (float): The factor you want to shift a parameter by.
    """

    def __init__(self):
        """ Initialise the Shift class.
        """
        self._shift = 0.

    def get_shift(self):
        """ Returns the shift factor.

        Returns:
          float: The shift factor.
        """
        return self._shift

    def set_shift(self, shift):
        """ Sets the shift factor.

        Args:
          shift (float): Value you wish to set the shift factor to.
        """
        self._shift = float(shift)

    def shift(self, spectrum, dimension, **kwargs):
        """ Shifts a given spectrum's dimension using interpolation.

        Args:
          spectrum (float): The spectrum you want to shift.
          dimension (string): The dimension of the spectrum you want to shift.
          kwargs (dict): To passed to the interpolation function in
            :class:`echidna.core.spectra.Spectra`

        Returns:
          :class:`echidna.core.spectra.Spectra`: The shifted spectrum.
        """
        shift = self.get_shift()
        step = spectrum.get_config().get_par(dimension).get_width()
        if shift % step == 0.:
            # shift size multiple of step size. Interpolation not required.
            return self.shift_by_bin(spectrum, dimension)
        preshift_sum = spectrum.sum()
        interpolation = spectrum.interpolate1d(dimension, **kwargs)
        shifted_spec = spectra.Spectra(spectrum._name+"_shift" +
                                       str(shift),
                                       spectrum._num_decays,
                                       spectrum.get_config())
        n_dim = len(spectrum._data.shape)
        axis = spectrum.get_config().get_index(dimension)
        par = spectrum.get_config().get_par(dimension)
        low = par._low
        high = par._high
        n_bins = par._bins
        for bin in range(n_bins):
            x = low + (bin + 0.5) * step
            if (x - shift) < low or (x - shift) > high:
                continue  # Trying to shift values outside range (Unknown)
            y = interpolation(x - shift)
            if y <= 0.:  # Cant have negative num_events
                continue
            old_bin1 = par.get_bin(x - shift)
            old_bin_centre1 = par.get_bin_centre(old_bin1)
            if old_bin_centre1 > x - shift:
                old_bin2 = old_bin1 - 1
                if old_bin2 >= 0:
                    x_low1 = old_bin_centre1 - 0.5*step  # Equals x_high2
                    x_high1 = x - shift + 0.5*step
                    area1 = (x_high1 - x_low1)*(interpolation(x_high1) -
                                                interpolation(x_low1))
                    x_low2 = x - shift - 0.5*step
                    area2 = (x_low1 - x_low2)*(interpolation(x_low1) -
                                               interpolation(x_low2))
                else:
                    old_bin2 = 0
                    area2 = 0.  # This will set scale2 == 0
                    area1 = 1.
            else:
                old_bin2 = old_bin1 + 1
                if old_bin2 < n_bins:
                    x_low1 = x - shift - 0.5*step
                    x_high1 = old_bin_centre1 + 0.5*step  # Equals x_low2
                    area1 = numpy.fabs((x_high1 - x_low1) *
                                       (interpolation(x_high1) -
                                        interpolation(x_low1)))
                    x_high2 = x - shift + 0.5*step
                    area2 = numpy.fabs((x_high2 - x_high1) *
                                       (interpolation(x_high2) -
                                        interpolation(x_high1)))
                else:
                    old_bin2 = n_bins - 1
                    area2 = 0.  # This will set scale2 == 0
                    area1 = 1.
            scale1 = area1 / (area1 + area2)
            scale2 = area2 / (area1 + area2)
            # Prepare array split. Is there a better way to do this not using
            # eval and exec?
            cur_slice = "["
            old_slice1 = "["
            old_slice2 = "["<commit_msg>Bug fix for area calc<commit_after>import echidna.core.spectra as spectra


class Shift(object):
    """ A class for shifting the parameter space of a spectra.

    Attributes:
      _shift (float): The factor you want to shift a parameter by.
    """

    def __init__(self):
        """ Initialise the Shift class.
        """
        self._shift = 0.

    def get_shift(self):
        """ Returns the shift factor.

        Returns:
          float: The shift factor.
        """
        return self._shift

    def set_shift(self, shift):
        """ Sets the shift factor.

        Args:
          shift (float): Value you wish to set the shift factor to.
        """
        self._shift = float(shift)

    def shift(self, spectrum, dimension, **kwargs):
        """ Shifts a given spectrum's dimension using interpolation.

        Args:
          spectrum (float): The spectrum you want to shift.
          dimension (string): The dimension of the spectrum you want to shift.
          kwargs (dict): To passed to the interpolation function in
            :class:`echidna.core.spectra.Spectra`

        Returns:
          :class:`echidna.core.spectra.Spectra`: The shifted spectrum.
        """
        shift = self.get_shift()
        step = spectrum.get_config().get_par(dimension).get_width()
        if shift % step == 0.:
            # shift size multiple of step size. Interpolation not required.
            return self.shift_by_bin(spectrum, dimension)
        preshift_sum = spectrum.sum()
        interpolation = spectrum.interpolate1d(dimension, **kwargs)
        shifted_spec = spectra.Spectra(spectrum._name+"_shift" +
                                       str(shift),
                                       spectrum._num_decays,
                                       spectrum.get_config())
        n_dim = len(spectrum._data.shape)
        axis = spectrum.get_config().get_index(dimension)
        par = spectrum.get_config().get_par(dimension)
        low = par._low
        high = par._high
        n_bins = par._bins
        for bin in range(n_bins):
            x = low + (bin + 0.5) * step
            if (x - shift) < low or (x - shift) > high:
                continue  # Trying to shift values outside range (Unknown)
            y = interpolation(x - shift)
            if y <= 0.:  # Cant have negative num_events
                continue
            old_bin1 = par.get_bin(x - shift)
            old_bin_centre1 = par.get_bin_centre(old_bin1)
            if old_bin_centre1 > x - shift:
                old_bin2 = old_bin1 - 1
                if old_bin2 >= 0:
                    x_low1 = old_bin_centre1 - 0.5*step  # Equals x_high2
                    x_high1 = x - shift + 0.5*step
                    area1 = numpy.fabs(0.5 * (x_high1 - x_low1) *
                                       (interpolation(x_high1) +
                                        interpolation(x_low1)))
                    x_low2 = x - shift - 0.5*step
                    area2 = numpy.fabs(0.5 * (x_low1 - x_low2) *
                                       (interpolation(x_low1) +
                                        interpolation(x_low2)))
                else:
                    old_bin2 = 0
                    area2 = 0.  # This will set scale2 == 0
                    area1 = 1.
            else:
                old_bin2 = old_bin1 + 1
                if old_bin2 < n_bins:
                    x_low1 = x - shift - 0.5*step
                    x_high1 = old_bin_centre1 + 0.5*step  # Equals x_low2
                    area1 = numpy.fabs(0.5 * (x_high1 - x_low1) *
                                       (interpolation(x_high1) +
                                        interpolation(x_low1)))
                    x_high2 = x - shift + 0.5*step
                    area2 = numpy.fabs(0.5 * (x_high2 - x_high1) *
                                       (interpolation(x_high2) +
                                        interpolation(x_high1)))
                else:
                    old_bin2 = n_bins - 1
                    area2 = 0.  # This will set scale2 == 0
                    area1 = 1.
            scale1 = area1 / (area1 + area2)
            scale2 = area2 / (area1 + area2)
            # Prepare array split. Is there a better way to do this not using
            # eval and exec?
            cur_slice = "["
            old_slice1 = "["
            old_slice2 = "["
{'fraction_non_alphanumeric': 0.08748704961436629, 'fraction_numerical': 0.020950846091861403, 'mean_word_length': 1.2660406885759, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5285715', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2322, 'n_words': 636}