<commit_before>import numpy as np
import scipy.signal as signal
import logging
import os
from copy import deepcopy

def import_iir(filename):
    """
    imports csv files generated with Mathematica notebooks of the form
    a1_0,b0_0,b1_0
    a1_1,b0_1,b1_1
    a1_2,b0_2,b1_2
    .
    .
    .

    args:
        filename : string containging to full path of the file (or only the filename if in same directory)

    returns:
        [aIIRfilterLis,bIIRfilterList] : list of two numpy arrays compatable for use
        with the scipy.signal.lfilter() function
        used by filterIIR() function

    """
    IIRfilterList = np.loadtxt(filename,
                               delimiter=',')

    if len(IIRfilterList.shape) == 1:
        IIRfilterList = np.reshape(IIRfilterList,(1,len(IIRfilterList)))

    aIIRfilterList = np.transpose(np.vstack((np.ones(len(IIRfilterList)),
                                             -IIRfilterList[:,0])))
    bIIRfilterList = IIRfilterList[:,1:]

    return [aIIRfilterList,bIIRfilterList]


def filter_fir(kernel,x):
    """
    function to apply a FIR filter to a dataset

    args:
        kernel: FIR filter kernel
        x:      data set
    return:
        y :     data convoluted with kernel, aligned such that pulses do not
                shift (expects kernel to have a impulse like peak)
    """
    iMax = kernel.argmax()
    y = np.convolve(x,kernel,mode='full')[iMax:(len(x)+iMax)]
    return y


def filter_iir(aIIRfilterList, bIIRfilterList, x):
    """
    applies IIR filter to the data x (aIIRfilterList and bIIRfilterList are load by the importIIR() function)

    args:
        aIIRfilterList : array containing the a coefficients of the IIR filters
                         (one row per IIR filter with coefficients 1,-a1,-a2,.. in the form required by scipy.signal.lfilter)
        bIIRfilterList : array containing the b coefficients of the IIR filters
                         (one row per IIR filter with coefficients b0, b1, b2,.. in the form required by scipy.signal.lfilter)
        x : data array to be filtered

    returns:
        y : filtered data array
    """
    y = x
    for a,b in zip(aIIRfilterList,bIIRfilterList):
        y = signal.lfilter(b,a,y)
    return y


def gaussian_filter_kernel(sigma,nr_sigma,dt):
    """
    function to generate a Gaussian filter kernel with specified sigma and
    filter kernel width (nr_sigma).

    Args:
        sigma (float): width of the Gaussian
        nr_sigma (int): specifies the length of the filter kernel
        dt (float): AWG sampling period

    Returns:
        kernel (numpy array): Gaussian filter kernel
    """
    nr_samples = int(nr_sigma*sigma/dt)
    if nr_samples == 0:
        logging.warning('sigma too small (much smaller than sampling rate).')
        return np.array([1])
    gauss_kernel = signal.gaussian(nr_samples, sigma/dt, sym=False)
    gauss_kernel = gauss_kernel/np.sum(gauss_kernel)
    return np.array(gauss_kernel)


def scale_and_negate_IIR(filter_coeffs, scale):
    for i in range(len(filter_coeffs[0])):
        filter_coeffs[0][i][1] *= -1
    filter_coeffs[1][0] /= scale


def combine_FIR_filters(kernels):
    if hasattr(kernels[0], '__iter__'):
        kernel_combined = kernels[0]
        for kernel in kernels[1:]:
            kernel_combined = np.convolve(kernel, kernel_combined)
        return kernel_combined
    else:
        return kernels


def convert_expmod_to_IIR(expmod, dt, inverse_IIR=True):
    """
    Convert an exponential model A + B * exp(- t/tau) (or a list of such
    models) to a first-order IIR filter (or a list of such filters).

    :param expmod: list of exponential models  in the form
        [[A_0, B_0, tau_0], ... ], or a single exponential model
        [A_0, B_0, tau_0].
    :param dt: (float) AWG sampling period
    :param inverse_IIR: (bool, default: True) whether the IIR filters inverting
        the exponential model should be returned.

    :return: A list of IIR filter coefficients in the form
        [aIIRfilterList, bIIRfilterList] according to the definition in
        filter_iir(). If expmod is a single exponential model, a single
        filter is returned in the form [a, b].
    """
    if hasattr(expmod[0], '__iter__'):
        iir = [convert_expmod_to_IIR(e, dt, inverse_IIR) for e in expmod]
        a = [i[0] for i in iir]
        b = [i[1] for i in iir]
    else:
        A, B, tau = expmod
        if np.array(tau).ndim > 0:  # sum of exp mod
            import sympy
            N = len(tau)
            a = sympy.symbols(','.join([f'a{i}' for i in range(N + 1)]))
            tau_s = sympy.symbols(','.join([f'tau{i + 1}' for i in range(
                N)]))<commit_msg>bugfix fpdist.convert_expmod_to_IIR: do not crash in case of single-entry list of taus<commit_after>import numpy as np
import scipy.signal as signal
import logging
import os
from copy import deepcopy

def import_iir(filename):
    """
    imports csv files generated with Mathematica notebooks of the form
    a1_0,b0_0,b1_0
    a1_1,b0_1,b1_1
    a1_2,b0_2,b1_2
    .
    .
    .

    args:
        filename : string containging to full path of the file (or only the filename if in same directory)

    returns:
        [aIIRfilterLis,bIIRfilterList] : list of two numpy arrays compatable for use
        with the scipy.signal.lfilter() function
        used by filterIIR() function

    """
    IIRfilterList = np.loadtxt(filename,
                               delimiter=',')

    if len(IIRfilterList.shape) == 1:
        IIRfilterList = np.reshape(IIRfilterList,(1,len(IIRfilterList)))

    aIIRfilterList = np.transpose(np.vstack((np.ones(len(IIRfilterList)),
                                             -IIRfilterList[:,0])))
    bIIRfilterList = IIRfilterList[:,1:]

    return [aIIRfilterList,bIIRfilterList]


def filter_fir(kernel,x):
    """
    function to apply a FIR filter to a dataset

    args:
        kernel: FIR filter kernel
        x:      data set
    return:
        y :     data convoluted with kernel, aligned such that pulses do not
                shift (expects kernel to have a impulse like peak)
    """
    iMax = kernel.argmax()
    y = np.convolve(x,kernel,mode='full')[iMax:(len(x)+iMax)]
    return y


def filter_iir(aIIRfilterList, bIIRfilterList, x):
    """
    applies IIR filter to the data x (aIIRfilterList and bIIRfilterList are load by the importIIR() function)

    args:
        aIIRfilterList : array containing the a coefficients of the IIR filters
                         (one row per IIR filter with coefficients 1,-a1,-a2,.. in the form required by scipy.signal.lfilter)
        bIIRfilterList : array containing the b coefficients of the IIR filters
                         (one row per IIR filter with coefficients b0, b1, b2,.. in the form required by scipy.signal.lfilter)
        x : data array to be filtered

    returns:
        y : filtered data array
    """
    y = x
    for a,b in zip(aIIRfilterList,bIIRfilterList):
        y = signal.lfilter(b,a,y)
    return y


def gaussian_filter_kernel(sigma,nr_sigma,dt):
    """
    function to generate a Gaussian filter kernel with specified sigma and
    filter kernel width (nr_sigma).

    Args:
        sigma (float): width of the Gaussian
        nr_sigma (int): specifies the length of the filter kernel
        dt (float): AWG sampling period

    Returns:
        kernel (numpy array): Gaussian filter kernel
    """
    nr_samples = int(nr_sigma*sigma/dt)
    if nr_samples == 0:
        logging.warning('sigma too small (much smaller than sampling rate).')
        return np.array([1])
    gauss_kernel = signal.gaussian(nr_samples, sigma/dt, sym=False)
    gauss_kernel = gauss_kernel/np.sum(gauss_kernel)
    return np.array(gauss_kernel)


def scale_and_negate_IIR(filter_coeffs, scale):
    for i in range(len(filter_coeffs[0])):
        filter_coeffs[0][i][1] *= -1
    filter_coeffs[1][0] /= scale


def combine_FIR_filters(kernels):
    if hasattr(kernels[0], '__iter__'):
        kernel_combined = kernels[0]
        for kernel in kernels[1:]:
            kernel_combined = np.convolve(kernel, kernel_combined)
        return kernel_combined
    else:
        return kernels


def convert_expmod_to_IIR(expmod, dt, inverse_IIR=True):
    """
    Convert an exponential model A + B * exp(- t/tau) (or a list of such
    models) to a first-order IIR filter (or a list of such filters).

    :param expmod: list of exponential models  in the form
        [[A_0, B_0, tau_0], ... ], or a single exponential model
        [A_0, B_0, tau_0].
    :param dt: (float) AWG sampling period
    :param inverse_IIR: (bool, default: True) whether the IIR filters inverting
        the exponential model should be returned.

    :return: A list of IIR filter coefficients in the form
        [aIIRfilterList, bIIRfilterList] according to the definition in
        filter_iir(). If expmod is a single exponential model, a single
        filter is returned in the form [a, b].
    """
    if hasattr(expmod[0], '__iter__'):
        iir = [convert_expmod_to_IIR(e, dt, inverse_IIR) for e in expmod]
        a = [i[0] for i in iir]
        b = [i[1] for i in iir]
    else:
        A, B, tau = expmod
        if np.array(tau).ndim > 0:  # sum of exp mod
            import sympy
            N = len(tau)
            a = sympy.symbols(','.join([f'a{i}' for i in range(N + 1)]))
            tau_s = sympy.symbols(','.join([f'tau{i + 1}' for i in range(
                N)]))
            if N == 1:
                tau_s = [tau_s]
{'fraction_non_alphanumeric': 0.09325936780406113, 'fraction_numerical': 0.010780824785430186, 'mean_word_length': 2.480874316939891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '531181', 'n_tokens_mistral': 3134, 'n_tokens_neox': 3017, 'n_words': 971}