<commit_before># -*- coding: utf-8 -*-
import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
import pycuda.gpuarray as gpuarray
from pycuda.elementwise import ElementwiseKernel
from pyfft.cuda import Plan


BACKEND = 'cuda'

PI2 = 2 * np.pi


gpu_morlet = ElementwiseKernel(
    'pycuda::complex<float> *dest, '
    'float normal_pi_sqr_1_4, '
    'float scale, '
    'float *angular_frequencies, '
    'float omega0',
    'dest[i] = normal_pi_sqr_1_4 * '
    'powf(exp(-(scale * angular_frequencies[i] - omega0)), 2.0) / 2.0',
    'gpu_morlet',
    preamble='''#include <pycuda-complex.hpp>'''
)


multiply_them = ElementwiseKernel(
    "pycuda::complex<float> *dest, "
    "pycuda::complex<float> *left, "
    "pycuda::complex<float> *right",
    "dest[i] = left[i] * right[i]",
    "multiply_them",
    preamble='''#include <pycuda-complex.hpp>'''
)


class WaveletBox(object):
    def __init__(self, nsamples, time_step, scale_resolution, omega0):
        self.scales = self.autoscales(nsamples, time_step,
                                      scale_resolution, omega0)
        self.angular_frequencies = angularfreq(nsamples, time_step)

        self.wft = morletft(self.scales, self.angular_frequencies,
                            omega0, time_step)

        self.wft_gpu_i = [gpuarray.to_gpu(np.array(wft_i, dtype=np.complex64))
                          for wft_i in self.wft]

        stream = cuda.Stream()

        self.plan = Plan((nsamples,), stream=stream)


    def cwt(self, data, decimate=None):
        x_arr = np.asarray(data, dtype=np.complex64) - np.mean(data)
        x_width = x_arr.shape[0]

        if x_arr.ndim is not 1:
            raise ValueError('data must be an 1d numpy array or list')

        assert x_arr.shape[0] == self.wft.shape[1]

        if decimate:
            result_width = len(self.wft[0][::decimate])

        else:
            result_width = self.wft.shape[1]

        complex_image = np.empty((self.wft.shape[0], result_width),
                                 dtype=np.complex64)

        gpu_x_arr_ft = gpuarray.to_gpu(x_arr)
        self.plan.execute(gpu_x_arr_ft)

        gpu_med = gpuarray.empty_like(gpu_x_arr_ft)

        for i in range(complex_image.shape[0]):
            multiply_them(gpu_med, gpu_x_arr_ft, self.wft_gpu_i[i])

            self.plan.execute(gpu_med, inverse=True)

            if decimate:
                reshaped = gpu_med.reshape((result_width,
                                            x_width / result_width))

                gpu_decimated = extract_columns(reshaped, 0, 1).ravel()

                complex_image[i] = gpu_decimated.get()

            else:
                complex_image[i] = gpu_med.get()

        return complex_image


    def autoscales(self, nsamples, time_step, scale_resolution, omega0):
        """ Compute scales as fractional power of two """

        s0 = (time_step * (omega0 + np.sqrt(2 + omega0**2))) / PI2

        J = int(np.floor(scale_resolution**-1 *
                         np.log2((nsamples * time_step) / s0)))

        return np.fromiter(
            (s0 * 2**(i * scale_resolution) for i in range(J + 1)),
            np.float, J + 1
        )


def normalization(scale, time_step):
    return np.sqrt(PI2 * scale / time_step)


def morletft(scales, angular_frequencies, omega0, time_step):
    """ Fourier tranformed morlet function """

    p = 0.75112554446494251 # pi**(-1.0/4.0)
    wavelet = np.zeros((scales.shape[0], angular_frequencies.shape[0]))
    pos = angular_frequencies > 0

    for i in range(scales.shape[0]):
        n = normalization(scales[i], time_step)

        wavelet[i][pos] = n * p * \
            np.exp(-(scales[i] * angular_frequencies[pos] - omega0)**2 / 2.0)

    return wavelet


def angularfreq(nsamples, time_step):
    """ Compute angular frequencies """

    N2 = nsamples / 2.0

    return np.fromiter(
        (
            PI2 * (i if i <= N2 else i - nsamples) / (nsamples * time_step)
            for i in range(nsamples)
        ),<commit_msg>Refactor names pi_sqr_1_4, norma<commit_after># -*- coding: utf-8 -*-
import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
import pycuda.gpuarray as gpuarray
from pycuda.elementwise import ElementwiseKernel
from pyfft.cuda import Plan


BACKEND = 'cuda'

PI2 = 2 * np.pi


gpu_morlet = ElementwiseKernel(
    'pycuda::complex<float> *dest, '
    'float normal_pi_sqr_1_4, '
    'float scale, '
    'float *angular_frequencies, '
    'float omega0',
    'dest[i] = normal_pi_sqr_1_4 * '
    'powf(exp(-(scale * angular_frequencies[i] - omega0)), 2.0) / 2.0',
    'gpu_morlet',
    preamble='''#include <pycuda-complex.hpp>'''
)


multiply_them = ElementwiseKernel(
    "pycuda::complex<float> *dest, "
    "pycuda::complex<float> *left, "
    "pycuda::complex<float> *right",
    "dest[i] = left[i] * right[i]",
    "multiply_them",
    preamble='''#include <pycuda-complex.hpp>'''
)


class WaveletBox(object):
    def __init__(self, nsamples, time_step, scale_resolution, omega0):
        self.scales = self.autoscales(nsamples, time_step,
                                      scale_resolution, omega0)
        self.angular_frequencies = angularfreq(nsamples, time_step)

        self.wft = morletft(self.scales, self.angular_frequencies,
                            omega0, time_step)

        self.wft_gpu_i = [gpuarray.to_gpu(np.array(wft_i, dtype=np.complex64))
                          for wft_i in self.wft]

        stream = cuda.Stream()

        self.plan = Plan((nsamples,), stream=stream)


    def cwt(self, data, decimate=None):
        x_arr = np.asarray(data, dtype=np.complex64) - np.mean(data)
        x_width = x_arr.shape[0]

        if x_arr.ndim is not 1:
            raise ValueError('data must be an 1d numpy array or list')

        assert x_arr.shape[0] == self.wft.shape[1]

        if decimate:
            result_width = len(self.wft[0][::decimate])

        else:
            result_width = self.wft.shape[1]

        complex_image = np.empty((self.wft.shape[0], result_width),
                                 dtype=np.complex64)

        gpu_x_arr_ft = gpuarray.to_gpu(x_arr)
        self.plan.execute(gpu_x_arr_ft)

        gpu_med = gpuarray.empty_like(gpu_x_arr_ft)

        for i in range(complex_image.shape[0]):
            multiply_them(gpu_med, gpu_x_arr_ft, self.wft_gpu_i[i])

            self.plan.execute(gpu_med, inverse=True)

            if decimate:
                reshaped = gpu_med.reshape((result_width,
                                            x_width / result_width))

                gpu_decimated = extract_columns(reshaped, 0, 1).ravel()

                complex_image[i] = gpu_decimated.get()

            else:
                complex_image[i] = gpu_med.get()

        return complex_image


    def autoscales(self, nsamples, time_step, scale_resolution, omega0):
        """ Compute scales as fractional power of two """

        s0 = (time_step * (omega0 + np.sqrt(2 + omega0**2))) / PI2

        J = int(np.floor(scale_resolution**-1 *
                         np.log2((nsamples * time_step) / s0)))

        return np.fromiter(
            (s0 * 2**(i * scale_resolution) for i in range(J + 1)),
            np.float, J + 1
        )


def normalization(scale, time_step):
    return np.sqrt(PI2 * scale / time_step)


def morletft(scales, angular_frequencies, omega0, time_step):
    """ Fourier tranformed morlet function """

    pi_sqr_1_4 = 0.75112554446494251 # pi**(-1.0/4.0)
    wavelet = np.zeros((scales.shape[0], angular_frequencies.shape[0]))
    pos = angular_frequencies > 0

    for i in range(scales.shape[0]):
        norma = normalization(scales[i], time_step)

        wavelet[i][pos] = norma * pi_sqr_1_4 * \
            np.exp(-(scales[i] * angular_frequencies[pos] - omega0)**2 / 2.0)

    return wavelet


def angularfreq(nsamples, time_step):
    """ Compute angular frequencies """

    N2 = nsamples / 2.0

    return np.fromiter(
        (
            PI2 * (i if i <= N2 else i - nsamples) / (nsamples * time_step)
            for i in range(nsamples)
        ),
{'fraction_non_alphanumeric': 0.12368972746331237, 'fraction_numerical': 0.02145763965963744, 'mean_word_length': 2.3021172638436482, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1533501', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2729, 'n_words': 547}