<commit_before># cython: language_level=3
"""Module for interpolating sampled functions."""

import numpy as np

# Cython import
cimport numpy as np
cimport cython

from cython.parallel import prange

# Import functions from math.h
cdef extern from "math.h":
    double exp(double x)
    double log(double x)

cimport libc.math

dbltype = np.int
ctypedef np.float64_t dbltype_t


def _int_fromfile(cls, file, colspec=None):
        """Perform Interpolation from given file.

        Load up the file and generate an Interpolater.

        colspec - 2 element list of columns to interpolate
        """
        if colspec is None:
            colspec = [0,1]

        if len(colspec) != 2:
            print(f"cls: {cls}, file: {file}, colspec: {colspec}")
            raise InterpolationException("Can only use two columns.")

        d1 = np.loadtxt(file, usecols = colspec)
        return cls(d1)

class InterpolationException(Exception):
    """Exceptions in the Interpolation module. """
    pass


cdef class Interpolater(object):
    """A class to interpolate data sets.

    This class performs cubic spline interpolation. See
    Numerical Recipes for details. Uses Natural spline conditions,
    y''[0] = y''[-1] = 0. Written using Cython for massive speedup.
    """

    cdef np.ndarray __data_
    cdef np.ndarray __y2_

    cdef double * _data_p
    cdef double * _y2_p
    cdef Py_ssize_t _n


    fromfile = classmethod(_int_fromfile)


    def __init__(self, data1, data2=None):
        """Constructor to initialise from data.

        Need to supply a 2d data array of X-Y pairs to
        interpolate. i.e. [[x0,y0], [x1,y1], ...]
        """

        if data2 is None:
            data = data1
        else:
            data = np.dstack((data1, data2))[0]

        # Check to ensure array contains correct data.
        s = data.shape
        if(len(s) != 2):
            raise InterpolationException("Array must be 2d.")
        if(s[1] != 2):
            raise InterpolationException("Array must consist of X-Y pairs.")
        if(s[0] < 4):
            raise InterpolationException("Cubic spline interpolation requires at least 4 points.")

        # Check to ensure data is all valid.
        if(np.isinf(data).any() or np.isnan(data).any()):
            raise InterpolationException("Some values invalid.")

        self.__data_ = np.ascontiguousarray(data)
        self.__gen_spline_()

        self._data_p = <double *>self.__data_.data
        self._y2_p = <double *>self.__y2_.data
        self._n = <Py_ssize_t>self.__data_.shape[0]


    def value(self, x):
        """Returns the value of the function at x. """
        if isinstance(x, np.ndarray):
            return self.value_array(x)

        return self.value_cdef(x)<commit_msg>feat(cubicspline): catch more bad usage<commit_after># cython: language_level=3
"""Module for interpolating sampled functions."""

import numpy as np

# Cython import
cimport numpy as np
cimport cython

from cython.parallel import prange

# Import functions from math.h
cdef extern from "math.h":
    double exp(double x)
    double log(double x)

cimport libc.math

dbltype = np.int
ctypedef np.float64_t dbltype_t


def _int_fromfile(cls, file, colspec=None):
        """Perform Interpolation from given file.

        Load up the file and generate an Interpolater.

        colspec - 2 element list of columns to interpolate
        """
        if colspec is None:
            colspec = [0,1]

        if len(colspec) != 2:
            print(f"cls: {cls}, file: {file}, colspec: {colspec}")
            raise InterpolationException("Can only use two columns.")

        d1 = np.loadtxt(file, usecols = colspec)
        return cls(d1)

class InterpolationException(Exception):
    """Exceptions in the Interpolation module. """
    pass


cdef class Interpolater(object):
    """A class to interpolate data sets.

    This class performs cubic spline interpolation. See
    Numerical Recipes for details. Uses Natural spline conditions,
    y''[0] = y''[-1] = 0. Written using Cython for massive speedup.
    """

    cdef np.ndarray __data_
    cdef np.ndarray __y2_

    cdef double * _data_p
    cdef double * _y2_p
    cdef Py_ssize_t _n


    fromfile = classmethod(_int_fromfile)


    def __init__(self, data1, data2=None):
        """Constructor to initialise from data.

        Need to supply a 2d data array of X-Y pairs to
        interpolate. i.e. [[x0,y0], [x1,y1], ...]
        """

        if data2 is None:
            data = data1
        else:
            try:
                data = np.dstack((data1, data2))[0]
            except ValueError as e:
                raise InterpolationException("Failure stacking x and y data.") from e

        # Check to ensure array contains correct data.
        s = data.shape
        if(len(s) != 2):
            raise InterpolationException("Array must be 2d.")
        if(s[1] != 2):
            raise InterpolationException("Array must consist of X-Y pairs.")
        if(s[0] < 4):
            raise InterpolationException("Cubic spline interpolation requires at least 4 points.")

        # Check to ensure data is all valid.
        if(np.isinf(data).any() or np.isnan(data).any()):
            raise InterpolationException("Some values invalid.")

        self.__data_ = np.ascontiguousarray(data)
        self.__gen_spline_()

        self._data_p = <double *>self.__data_.data
        self._y2_p = <double *>self.__y2_.data
        self._n = <Py_ssize_t>self.__data_.shape[0]


    def value(self, x):
        """Returns the value of the function at x. """
        if isinstance(x, np.ndarray):
            return self.value_array(x)

        return self.value_cdef(x)
{'fraction_non_alphanumeric': 0.10033327486405894, 'fraction_numerical': 0.012629363269601825, 'mean_word_length': 2.398092967818832, 'pattern_counts': {'":': 2, '<': 11, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4006205', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1737, 'n_words': 553}