Example #1
0
def ljung_box(tss, lags):
    """ The Ljung–Box test checks that data whithin the time series are independently distributed (i.e. the correlations
    in the population from which the sample is taken are 0, so that any observed correlations in the data result from
    randomness of the sampling process). Data are no independently distributed, if they exhibit serial correlation.
    The test statistic is:

    .. math::
        Q = n\\left(n+2\\right)\sum_{k=1}^h\\frac{\hat{\\rho}^2_k}{n-k}

    where :math:`n` is the sample size, :math:`\hat{\\rho}k` is the sample autocorrelation at lag :math:`k`, and
    :math:`h` is the number of lags being tested. Under :math:`H_0` the statistic Q follows a :math:`\\chi^2{(h)}`.
    For significance level :math:`\\alpha`, the :math:`critical region` for rejection of the hypothesis of randomness
    is:

    .. math::
        Q > \\chi_{1-\\alpha,h}^2

    where :math:`\\chi_{1-\\alpha,h}^2` is the :math:`\\alpha`-quantile of the chi-squared distribution with :math:`h`
    degrees of freedom.

    [1] G. M. Ljung G. E. P. Box (1978). On a measure of lack of fit in time series models.
    Biometrika, Volume 65, Issue 2, 1 August 1978, Pages 297–303.

    :param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and
                dimension one indicates the number of time series.
    :param lags: Number of lags being tested.
    :return: Array containing the Ljung-Box statistic test.
    """
    ljung_box_out = ctypes.c_void_p(0)
    KhivaLibrary().c_khiva_library.ljung_box(
        ctypes.pointer(tss.arr_reference), ctypes.pointer(ctypes.c_long(lags)),
        ctypes.pointer(ljung_box_out))
    return Array(array_reference=ljung_box_out)
Example #2
0
    def _get_data(self):
        """ Retrieves the data from the device to the host.

        :return A numpy array with the data.
        """
        initialized_result_array = np.zeros(self.result_l).astype(
            _get_array_type(self.khiva_type.value))
        c_result_array = (_get_array_type(self.khiva_type.value) *
                          self.result_l)(*initialized_result_array)
        KhivaLibrary().c_khiva_library.get_data(
            ctypes.pointer(self.arr_reference), ctypes.pointer(c_result_array))

        dims = self.get_dims()
        dims = dims[dims > 1]
        a = np.array(c_result_array)

        if self._is_complex():
            a = np.array(np.split(a, self.result_l / 2))
            a = np.apply_along_axis(lambda args: [complex(*args)], 1, a)
            a = a.reshape(dims)
            c = deque(range(len(a.shape)))
            c.rotate(-1)
            a = np.transpose(a, c)
        else:
            dims = deque(dims)
            dims.rotate(1)
            a = a.reshape(dims)

        a = a.astype(_get_numpy_type(self.khiva_type.value))
        return a
Example #3
0
def k_means(tss, k, tolerance=1e-10, max_iterations=100):
    """ Calculates the K-Means algorithm.

    [1] S. Lloyd. 1982. Least squares quantization in PCM. IEEE Transactions on Information Theory, 28, 2,
    Pages 129-137.

    :param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and
    dimension one indicates the number of time series.
    :param k:                   The number of means to be computed.
    :param tolerance:           The error tolerance to stop the computation of the centroids.
    :param max_iterations:      The maximum number of iterations allowed.

    :return: Tuple with an array of centroids and array of labels.
    """
    centroids = ctypes.c_void_p(0)
    labels = ctypes.c_void_p(0)

    error_code = ctypes.c_int(0)
    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
    KhivaLibrary().c_khiva_library.k_means(
        ctypes.pointer(tss.arr_reference), ctypes.pointer(ctypes.c_int(k)),
        ctypes.pointer(centroids), ctypes.pointer(labels),
        ctypes.pointer(ctypes.c_float(tolerance)),
        ctypes.pointer(ctypes.c_int(max_iterations)),
        ctypes.pointer(error_code), error_message)
    if error_code.value != 0:
        raise Exception(str(error_message.value.decode()))

    return ClusteringResult(centroids=Array(centroids), labels=Array(labels))
Example #4
0
def k_shape(tss, k, tolerance=1e-10, max_iterations=100):
    """ Calculates the K-Shape algorithm.

    [1] John Paparrizos and Luis Gravano. 2016. k-Shape: Efficient and Accurate Clustering of Time Series.
    SIGMOD Rec. 45, 1 (June 2016), 69-76.


    :param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and
    dimension one indicates the number of time series.
    :param k:                   The number of means to be computed.
    :param tolerance:           The error tolerance to stop the computation of the centroids.
    :param max_iterations:      The maximum number of iterations allowed.

    :return: Tuple with an array of centroids and array of labels.
    """
    centroids = ctypes.c_void_p(0)
    labels = ctypes.c_void_p(0)

    error_code = ctypes.c_int(0)
    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
    KhivaLibrary().c_khiva_library.k_shape(
        ctypes.pointer(tss.arr_reference), ctypes.pointer(ctypes.c_int(k)),
        ctypes.pointer(centroids), ctypes.pointer(labels),
        ctypes.pointer(ctypes.c_float(tolerance)),
        ctypes.pointer(ctypes.c_int(max_iterations)),
        ctypes.pointer(error_code), error_message)
    if error_code.value != 0:
        raise Exception(str(error_message.value.decode()))

    return ClusteringResult(centroids=Array(centroids), labels=Array(labels))
Example #5
0
def sax(a, alphabet_size):
    """ Symbolic Aggregate approXimation (SAX). It transforms a numeric time series into a time series of symbols with
    the same size. The algorithm was proposed by Lin et al.) and extends the PAA-based approach inheriting the original
    algorithm simplicity and low computational complexity while providing satisfactory sensitivity and selectivity in
    range query processing. Moreover, the use of a symbolic representation opened a door to the existing wealth of
    data-structures and string-manipulation algorithms in computer science such as hashing, regular expression, pattern
    matching, suffix trees, and grammatical inference.

    [1] Lin, J., Keogh, E., Lonardi, S. & Chiu, B. (2003) A Symbolic Representation of Time Series, with Implications
    for Streaming Algorithms. In proceedings of the 8th ACM SIGMOD Workshop on Research Issues in Data Mining and
    Knowledge Discovery. San Diego, CA. June 13.

    :param a: KHIVA array with the input time series.
    :param alphabet_size: Number of element within the alphabet.

    :return: KHIVA array of points with the reduced dimensionality.
    """
    b = ctypes.c_void_p(0)
    error_code = ctypes.c_int(0)
    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
    KhivaLibrary().c_khiva_library.sax(
        ctypes.pointer(a.arr_reference),
        ctypes.pointer(ctypes.c_int(alphabet_size)), ctypes.pointer(b),
        ctypes.pointer(error_code), error_message)
    if error_code.value != 0:
        raise Exception(str(error_message.value.decode()))

    return Array(array_reference=b)
Example #6
0
def paa(a, bins):
    """ Piecewise Aggregate Approximation (PAA) approximates a time series :math:`X` of length :math:`n` into vector
    :math:`\\bar{X}=(\\bar{x}_{1},…,\\bar{x}_{M})` of any arbitrary length :math:`M \\leq n` where each of
    :math:`\\bar{x_{i}}` is calculated as follows:

    .. math::
        \\bar{x}_{i} = \\frac{M}{n} \\sum_{j=n/M(i-1)+1}^{(n/M)i} x_{j}.

    Which simply means that in order to reduce the dimensionality from :math:`n` to :math:`M`, we first divide the original
    time series into :math:`M` equally sized frames and secondly compute the mean values for each frame. The sequence
    assembled from the mean values is the PAA approximation (i.e., transform) of the original time series.

    :param a: Set of points.
    :param bins: Sets the total number of divisions.

    :return: KHIVA array of points with the reduced dimensionality.
    """
    b = ctypes.c_void_p(0)
    error_code = ctypes.c_int(0)
    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
    KhivaLibrary().c_khiva_library.paa(ctypes.pointer(a.arr_reference),
                                       ctypes.pointer(ctypes.c_int(bins)),
                                       ctypes.pointer(b),
                                       ctypes.pointer(error_code),
                                       error_message)
    if error_code.value != 0:
        raise Exception(str(error_message.value.decode()))

    return Array(array_reference=b)
Example #7
0
def linear(xss, yss):
    """Calculates a linear least-squares regression for two sets of measurements. Both arrays should have the same
    length.

    :param xss: Expects an input array whose dimension zero is the length of the time series (all the same) and dimension
                one indicates the number of time series.
    :param yss: Expects an input array whose dimension zero is the length of the time series (all the same) and dimension
                one indicates the number of time series.

    :return: slope Slope of the regression line.
            intercept Intercept of the regression line.
            rvalue Correlation coefficient.
            pvalue Two-sided p-value for a hypothesis test whose null hypothesis is that the slope is zero, using Wald
            Test with t-distribution of the test statistic.
            stderrest Standard error of the estimated gradient.
    """
    b = ctypes.c_void_p(0)
    c = ctypes.c_void_p(0)
    d = ctypes.c_void_p(0)
    e = ctypes.c_void_p(0)
    f = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.linear(ctypes.pointer(xss.arr_reference),
                                          ctypes.pointer(yss.arr_reference),
                                          ctypes.pointer(b), ctypes.pointer(c),
                                          ctypes.pointer(d), ctypes.pointer(e),
                                          ctypes.pointer(f))
    return Array(array_reference=b), Array(array_reference=c), Array(
        array_reference=d), Array(array_reference=e), Array(array_reference=f)
Example #8
0
def ramer_douglas_peucker(a, epsilon):
    """ The Ramer–Douglas–Peucker algorithm (RDP) is an algorithm for reducing the number of points in a curve that is
    approximated by a series of points. It reduces a set of points depending on the perpendicular distance of the points
    and epsilon, the greater epsilon, more points are deleted.

    [1] Urs Ramer, "An iterative procedure for the polygonal approximation of plane curves", Computer Graphics and Image
    Processing, 1(3), 244–256 (1972) doi:10.1016/S0146-664X(72)80017-0.

    [2] David Douglas & Thomas Peucker, "Algorithms for the reduction of the number of points required to represent a
    digitized line or its caricature", The Canadian Cartographer 10(2), 112–122 (1973) doi:10.3138/FM57-6770-U75U-7727

    :param a: KHIVA array with the x-coordinates and y-coordinates of the input points (x in column 0 and y in column 1).
    :param epsilon: It acts as the threshold value to decide which points should be considered meaningful or not.

    :return: KHIVA array with the x-coordinates and y-coordinates of the selected points (x in column 0 and y in
            column 1).
    """
    b = ctypes.c_void_p(0)
    error_code = ctypes.c_int(0)
    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
    KhivaLibrary().c_khiva_library.ramer_douglas_peucker(
        ctypes.pointer(a.arr_reference),
        ctypes.pointer(ctypes.c_double(epsilon)), ctypes.pointer(b),
        ctypes.pointer(error_code), error_message)
    if error_code.value != 0:
        raise Exception(str(error_message.value.decode()))

    return Array(array_reference=b)
Example #9
0
def partial_autocorrelation(arr, lags):
    """ Calculates the value of the partial autocorrelation function at the given lag. The lag :math:`k'`  partial
    autocorrelation of a time series :math:`\\lbrace x_t, t = 1 \\ldots T \\rbrace` equals the partial correlation of
    :math:`x_t` and :math:`x_{t-k}`, adjusted for the intermediate variables :math:`\\lbrace x_{t-1}, \\ldots, x_{t-k+1}\\rbrace`
    ([1]). Following [2], it can be defined as:

    .. math::
          \\alpha_k = \\frac{ Cov(x_t, x_{t-k} | x_{t-1}, \\ldots, x_{t-k+1})}
          {\\sqrt{ Var(x_t | x_{t-1}, \\ldots, x_{t-k+1}) Var(x_{t-k} | x_{t-1}, \\ldots, x_{t-k+1} )}}

    with (a) :math:`x_t = f(x_{t-1}, \\ldots, x_{t-k+1})` and (b) :math:`x_{t-k} = f(x_{t-1}, \\ldots, x_{t-k+1})`
    being AR(k-1) models that can be fitted by OLS. Be aware that in (a), the regression is done on past values to
    predict :math:`x_t` whereas in (b), future values are used to calculate the past value :math:`x_{t-k}`.
    It is said in [1] that "for an AR(p), the partial autocorrelations :math:`\\alpha_k` will be nonzero for :math:`k<=p`
    and zero for :math:`k>p`."
    With this property, it is used to determine the lag of an AR-Process.

    [1] Box, G. E., Jenkins, G. M., Reinsel, G. C., & Ljung, G. M. (2015).
    Time series analysis: forecasting and control. John Wiley & Sons.
    [2] https://onlinecourses.science.psu.edu/stat510/node/62

    :param arr: KHIVA array with the time series.
    :param lags: KHIVA array with the lags to be calculated.
    :return: KHIVA array with the partial autocorrelation for each time series for the given lag.
    """
    b = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.partial_autocorrelation(ctypes.pointer(arr.arr_reference),
                                                           ctypes.pointer(lags.arr_reference),
                                                           ctypes.pointer(b))

    return Array(array_reference=b)
Example #10
0
def find_best_n_motifs(profile, index, m, n, self_join=False):
    """ This function extracts the best N discords from a previously calculated matrix profile.

    :param profile: KHIVA array with the matrix profile containing the minimum distance of each subsequence.
    :param index: KHIVA array with the matrix profile index containing where each minimum occurs.
    :param m: Subsequence length value used to calculate the input matrix profile.
    :param n: Number of motifs to extract.
    :param self_join: Indicates whether the input profile comes from a self join operation or not. It determines
                      whether the mirror similar region is included in the output or not.
    :return: KHIVA arrays with the motif distances, the motif indices and the subsequence indices.
    """
    b = ctypes.c_void_p(0)
    c = ctypes.c_void_p(0)
    d = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.find_best_n_motifs(ctypes.pointer(profile.arr_reference),
                                                      ctypes.pointer(index.arr_reference),
                                                      ctypes.pointer(ctypes.c_long(m)),
                                                      ctypes.pointer(ctypes.c_long(n)),
                                                      ctypes.pointer(b),
                                                      ctypes.pointer(c),
                                                      ctypes.pointer(d),
                                                      ctypes.pointer(ctypes.c_bool(self_join)))

    return Array(array_reference=b), Array(array_reference=c), Array(
        array_reference=d)
Example #11
0
def max_langevin_fixed_point(arr, m, r):
    """ Largest fixed point of dynamics  :math:`argmax_x {h(x)=0}` estimated from polynomial :math:`h(x)`,
    which has been fitted to the deterministic dynamics of Langevin model

    .. math::
        \\dot(x)(t) = h(x(t)) + R \\mathcal(N)(0,1)

    as described by

        Friedrich et al. (2000): Physics Letters A 271, p. 217-222
        *Extracting model equations from experimental data*

    :param arr: KHIVA array with the time series.
    :param m: Order of polynom to fit for estimating fixed points of dynamics.
    :param r: Number of quantiles to use for averaging.
    :return: KHIVA array with the largest fixed point of deterministic dynamics.
    """
    b = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.max_langevin_fixed_point(ctypes.pointer(arr.arr_reference),
                                                            ctypes.pointer(ctypes.c_int(m)),
                                                            ctypes.pointer(ctypes.c_float(r)),
                                                            ctypes.pointer(b))

    return Array(array_reference=b)
Example #12
0
def linear_trend(arr):
    """ Calculate a linear least-squares regression for the values of the time series versus the sequence from 0 to
    length of the time series minus one.

    :param arr: KHIVA array with the time series.
    :return  a tuple with:
            pvalue: KHIVA array the pvalues for all time series.
            rvalue: KHIVA array The rvalues for all time series.
            intercept: KHIVA array the intercept values for all time series.
            slope: KHIVA array the slope for all time series.
            stdrr: KHIVA array the stderr values for all time series.
    """
    b = ctypes.c_void_p(0)
    c = ctypes.c_void_p(0)
    d = ctypes.c_void_p(0)
    e = ctypes.c_void_p(0)
    f = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.linear_trend(ctypes.pointer(arr.arr_reference),
                                                ctypes.pointer(b),
                                                ctypes.pointer(c),
                                                ctypes.pointer(d),
                                                ctypes.pointer(e),
                                                ctypes.pointer(f)
                                                )

    return Array(array_reference=b), \
           Array(array_reference=c), \
           Array(array_reference=d), \
           Array(array_reference=e), \
           Array(array_reference=f)
Example #13
0
def friedrich_coefficients(arr, m, r):
    """ Coefficients of polynomial :math:`h(x)`, which has been fitted to the deterministic dynamics of Langevin model:
    Largest fixed point of dynamics  :math:`argmax_x {h(x)=0}` estimated from polynomial :math:`h(x)`,
    which has been fitted to the deterministic dynamics of Langevin model:

    .. math::
        \\dot(x)(t) = h(x(t)) + R \\mathcal(N)(0,1)

    as described by [1]. For short time series this method is highly dependent on the parameters.

    [1] Friedrich et al. (2000): Physics Letters A 271, p. 217-222
    Extracting model equations from experimental data.


    :param arr: KHIVA array with the time series.
    :param m: Order of polynom to fit for estimating fixed points of dynamics.
    :param r: Number of quantiles to use for averaging.
    :return: KHIVA array with the coefficients for each time seriess.
    """
    b = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.friedrich_coefficients(ctypes.pointer(arr.arr_reference),
                                                          ctypes.pointer(ctypes.c_int(m)),
                                                          ctypes.pointer(ctypes.c_float(r)),
                                                          ctypes.pointer(b))

    return Array(array_reference=b)
Example #14
0
def fft_coefficient(arr, coefficient):
    """ Calculates the fourier coefficients of the one-dimensional discrete
    Fourier Transform for real input by fast fourier transformation algorithm.

    :param arr: KHIVA array with the time series.
    :param coefficient: The coefficient to extract from the FFT.
    :return: Tuple with:
        real: KHIVA array with the real part of the coefficient.
        imag: KHIVA array with the imaginary part of the coefficient.
        abs: KHIVA array with the absolute value of the coefficient.
        angle: KHIVA array with the angle of the coefficient.
    """
    b = ctypes.c_void_p(0)
    c = ctypes.c_void_p(0)
    d = ctypes.c_void_p(0)
    e = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.fft_coefficient(ctypes.pointer(arr.arr_reference),
                                                   ctypes.pointer(ctypes.c_long(coefficient)),
                                                   ctypes.pointer(b),
                                                   ctypes.pointer(c),
                                                   ctypes.pointer(d),
                                                   ctypes.pointer(e)
                                                   )

    return Array(array_reference=b), Array(array_reference=c,
                                           khiva_type=arr.khiva_type), Array(
        array_reference=d), Array(array_reference=e)
Example #15
0
def decimal_scaling_norm_in_place(tss):
    """ Same as decimal_scaling_norm, but it performs the operation in place, without allocating further memory.

    :param tss: KHIVA array with the time series.
    """
    KhivaLibrary().c_khiva_library.decimal_scaling_norm_in_place(
        ctypes.pointer(tss.arr_reference))
Example #16
0
def time_reversal_asymmetry_statistic(arr, lag):
    """ This function calculates the value of:

    .. math::

        \\frac{1}{n-2lag} \\sum_{i=0}^{n-2lag} x_{i + 2 \\cdot lag}^2 \\cdot x_{i + lag} - x_{i + lag} \\cdot  x_{i}^2

    which is

    .. math::

        \\mathbb{E}[L^2(X)^2 \\cdot L(X) - L(X) \\cdot X^2]

    where :math:`\\mathbb{E}` is the mean and :math:`L` is the lag operator. It was proposed in [1] as a promising
    feature to extract from time series.

    :param arr: KHIVA array with the time series.
    :param lag: The lag to be computed.
    :return: KHIVA array containing the count of the given value in each time series.
    """
    b = ctypes.c_void_p(0)

    KhivaLibrary().c_khiva_library.time_reversal_asymmetry_statistic(ctypes.pointer(arr.arr_reference),
                                                                     ctypes.pointer(ctypes.c_int(lag)),
                                                                     ctypes.pointer(b))

    return Array(array_reference=b)
Example #17
0
def group_by(tss, aggregation_function, n_columns_key=1, n_columns_value=1):
    """ Group by operation in the input array using n_columns_key columns as group keys and n_columns_value columns as
    values. The data is expected to be sorted. The aggregation function determines the operation to aggregate the
    values.

    :param tss: KHIVA array with the time series.
    :param aggregation_function: Function to be used in the aggregation. It receives an integer which indicates
                                the function to be applied.
                                0 : mean,
                                1 : median
                                2 : min,
                                3 : max,
                                4 : stdev,
                                5 : var,
                                default : mean
    :param n_columns_key: Number of columns conforming the key.
    :param n_columns_value: Number of columns conforming the value (they are expected to be consecutive to the column
                            keys).

    :return: KHIVA array with the values of the group keys aggregated using the aggregation_function.
    """
    b = ctypes.c_void_p(0)
    KhivaLibrary().c_khiva_library.group_by(ctypes.pointer(tss.arr_reference),
                                            ctypes.pointer(ctypes.c_int(aggregation_function)),
                                            ctypes.pointer(ctypes.c_int(n_columns_key)),
                                            ctypes.pointer(ctypes.c_int(n_columns_value)),
                                            ctypes.pointer(b))

    return Array(array_reference=b)
Example #18
0
def max_min_norm(tss, high=1.0, low=0.0, epsilon=0.00000001):
    """ Normalizes the given time series according to its minimum and maximum value and adjusts each value within the
    range [low, high].

    :param tss: KHIVA array with the time series.
    :param high: Maximum final value (Defaults to 1.0).
    :param low: Minimum final value (Defaults to 0.0).
    :param epsilon: Safeguard for constant (or near constant) time series as the operation implies a unit scale
                    operation between min and max values in the tss.

    :return: KHIVA array with the same dimensions as tss where the time series have been adjusted for zero mean and
            one as standard deviation.
    """
    b = ctypes.c_void_p(0)
    error_code = ctypes.c_int(0)
    error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
    KhivaLibrary().c_khiva_library.max_min_norm(ctypes.pointer(tss.arr_reference),
                                                ctypes.pointer(
                                                    ctypes.c_double(high)),
                                                ctypes.pointer(
                                                    ctypes.c_double(low)),
                                                ctypes.pointer(
                                                    ctypes.c_double(epsilon)),
                                                ctypes.pointer(b), ctypes.pointer(error_code), error_message)
    if error_code.value != 0:
        raise Exception(str(error_message.value.decode()))

    return Array(array_reference=b)
Example #19
0
 def __del__(self):
     """
     Class destructor.
     """
     if not self.arrayfire_reference:
         KhivaLibrary().c_khiva_library.delete_array(
             ctypes.pointer(self.arr_reference))
Example #20
0
 def __invert__(self):
     """
     Return ~self
     """
     result = ctypes.c_void_p(0)
     KhivaLibrary().c_khiva_library.khiva_not(
         ctypes.pointer(self.arr_reference), ctypes.pointer(result))
     return Array(array_reference=result)
Example #21
0
    def get_dims(self):
        """ Gets the dimensions of the KHIVA array.

        :return: The dimensions of the KHIVA array.
        """
        c_array_n = (ctypes.c_longlong * 4)(*(np.zeros(4)).astype(np.longlong))
        KhivaLibrary().c_khiva_library.get_dims(
            ctypes.pointer(self.arr_reference), ctypes.pointer(c_array_n))
        return np.array(c_array_n)
Example #22
0
 def __irshift__(self, other):
     """
     Perform self >>= other.
     """
     result = ctypes.c_void_p(0)
     KhivaLibrary().c_khiva_library.khiva_bitshiftr(
         ctypes.pointer(self.arr_reference),
         ctypes.pointer(ctypes.c_int32(other)), ctypes.pointer(result))
     return Array(array_reference=result)
Example #23
0
 def __ixor__(self, other):
     """
     Perform self ^= other.
     """
     result = ctypes.c_void_p(0)
     KhivaLibrary().c_khiva_library.khiva_bitxor(
         ctypes.pointer(self.arr_reference),
         ctypes.pointer(other.arr_reference), ctypes.pointer(result))
     return Array(array_reference=result)
Example #24
0
 def __and__(self, other):
     """
     Return self & other.
     """
     result = ctypes.c_void_p(0)
     KhivaLibrary().c_khiva_library.khiva_bitand(
         ctypes.pointer(self.arr_reference),
         ctypes.pointer(other.arr_reference), ctypes.pointer(result))
     return Array(array_reference=result)
Example #25
0
 def __rpow__(self, other):
     """
     Return other ** self.
     """
     result = ctypes.c_void_p(0)
     KhivaLibrary().c_khiva_library.khiva_pow(
         ctypes.pointer(self.arr_reference),
         ctypes.pointer(other.arr_reference), ctypes.pointer(result))
     return Array(array_reference=result)
Example #26
0
 def __idiv__(self, other):
     """
     Perform other / self.
     """
     result = ctypes.c_void_p(0)
     KhivaLibrary().c_khiva_library.khiva_div(
         ctypes.pointer(self.arr_reference),
         ctypes.pointer(other.arr_reference), ctypes.pointer(result))
     return Array(array_reference=result)
Example #27
0
    def get_type(self):
        """ Gets the type of the KHIVA array.

        :return: The type of the KHIVA array.
        """
        c_type = ctypes.c_int()
        KhivaLibrary().c_khiva_library.get_type(
            ctypes.pointer(self.arr_reference), ctypes.pointer(c_type))
        return dtype(c_type.value)
Example #28
0
    def copy(self):
        """
        Performs a deep copy of the array.

        return: An identical copy of self.
        """
        result = ctypes.c_void_p(0)
        KhivaLibrary().c_khiva_library.copy(ctypes.pointer(self.arr_reference),
                                            ctypes.pointer(result))
        return Array(array_reference=result)
Example #29
0
def znorm_in_place(tss, epsilon=0.00000001):
    """ Adjusts the time series in the given input and performs z-norm
    in place (without allocating further memory).

    :param tss: KHIVA array with the time series.
    :param epsilon: epsilon Minimum standard deviation to consider. It acts as a gatekeeper for
                    those time series that may be constant or near constant.
    """
    KhivaLibrary().c_khiva_library.znorm_in_place(
        ctypes.pointer(tss.arr_reference),
        ctypes.pointer(ctypes.c_double(epsilon)))
Example #30
0
def mean_norm_in_place(tss):
    """ Normalizes the given time series according to its maximum-minimum value and its mean. It follows the following
    formulae:

    .. math:
        \\acute{x} = frac{x - mean(x)}{max(x) - min(x)}.

    :param tss: KHIVA array with the time series.
    """
    KhivaLibrary().c_khiva_library.mean_norm_in_place(
        ctypes.pointer(tss.arr_reference))