From 4995e3e9a642e5f5d070c056e4deb46a35ce9231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Bocanegra?= Date: Wed, 10 Jun 2020 01:06:03 +0200 Subject: [PATCH] Add matrix profile threshold Update README --- README.rst | 22 ++++++++-------- khiva/matrix.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 13 deletions(-) diff --git a/README.rst b/README.rst index 20be995..94fa1c4 100644 --- a/README.rst +++ b/README.rst @@ -44,7 +44,7 @@ Then, you can create an array in the device: .. code-block:: python from khiva.array import * - a = Array([1, 2, 3, 4, 5, 6, 7, 8]) + a = Array.from_list([1, 2, 3, 4, 5, 6, 7, 8], dtype.s32) a.display() The previous lines print the dimensions and the content of the created array: @@ -101,7 +101,7 @@ The result is the next one: |8|8.00 | +-+-------+ -Now let's dive into the asynchronous usage of the library. +Now, let's dive into the asynchronous usage of the library. Khiva library provides us several time series analysis functionalities which include features extraction, time-series re-dimension, distance calculations, motifs and discords detection, tools for similarity study, statistical parameters extraction or time series normalization. @@ -112,9 +112,9 @@ the moment that you do not use the functions of this library: .. code-block:: python from khiva.matrix import * - stomp_result = stomp(Array(np.array([11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11])), - Array(np.array([9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9])), - 3) + stomp_result = stomp(Array.from_numpy(np.array([11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11]), dtype.s32), + Array.from_numpy(np.array([9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9]), dtype.s32), + 3) find_best_n_discords_result = find_best_n_discords(stomp_result[0], stomp_result[1], 2) a = find_best_n_discords_result[2].to_numpy() @@ -139,11 +139,11 @@ different devices, knowing that the operations should be executed in the same de from khiva.features import * set_backend(KHIVABackend.KHIVA_BACKEND_OPENCL) set_device(0) - a = Array([1, 2, 3, 4, 5, 6, 7, 8]) + a = Array.from_list([1, 2, 3, 4, 5, 6, 7, 8], dtype.s32) b = mean(a) set_device(1) - c = Array([1, 2, 3, 4, 5, 6, 7, 8]) + c = Array.from_list([1, 2, 3, 4, 5, 6, 7, 8], dtype.s32) d = mean(c) set_backend(KHIVABackend.KHIVA_BACKEND_CPU) @@ -211,13 +211,12 @@ The available data types are the next ones: There are functions that do not support 32 bits floating point data type, so it is necessary to indicate the data type. The following is an example function requiring a 32-bit signed integer array: -.. code-block:: python +.. code:: python - cwt_coefficients_result = cwt_coefficients(Array([[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]), - Array(data=[1, 2, 3], khiva_type=dtype.s32), 2, 2).to_numpy() + cwt_coefficients_result = cwt_coefficients(Array.from_list([[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]], dtype.s32), + Array.from_list([1, 2, 3], dtype.s32), 2, 2).to_numpy() print(cwt_coefficients_result) - The output is: +-------------------------+ @@ -225,6 +224,7 @@ The output is: +-------------------------+ + Limitations ----------- diff --git a/khiva/matrix.py b/khiva/matrix.py index 5076b6a..7a938b9 100644 --- a/khiva/matrix.py +++ b/khiva/matrix.py @@ -285,8 +285,10 @@ def matrix_profile_self_join(time_series, subsequence_length): KhivaLibrary().c_khiva_library.matrix_profile_self_join(ctypes.pointer(time_series.arr_reference), ctypes.c_long( subsequence_length), - ctypes.pointer(profile), - ctypes.pointer(index), + ctypes.pointer( + profile), + ctypes.pointer( + index), ctypes.pointer( error_code), error_message) @@ -329,3 +331,65 @@ def get_chains(time_series, subsequence_length): raise Exception(str(error_message.value.decode())) return Array(array_reference=c) + + +def matrix_profile_self_threshold(time_series, subsequence_length, threshold): + """ Calculates the sum of correlations above a threshold between 'tss' and itself using a subsequence length + of 'm' at each location in 'tss'. + + [1] Yan Zhu, Zachary Zimmerman, Nader Shakibay Senobari, Chin-Chia Michael Yeh, Gareth Funning, Abdullah Mueen, + Philip Brisk and Eamonn Keogh (2016). Matrix Profile II: Exploiting a Novel Algorithm and GPUs to break the one + Hundred Million Barrier for Time Series Motifs and Joins. IEEE ICDM 2016. + + :param time_series: Query and reference time series.. + :param subsequence_length: Subsequence length. + :param threshold: Threshold. + :return The sum of correlations above a threshold between 'time_series' and itself using a subsequence length + of 'subsequence_length' at each location in 'time_series'. + """ + sums = ctypes.c_void_p(0) + + error_code = ctypes.c_int(0) + error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH) + KhivaLibrary().c_khiva_library.matrix_profile_self_threshold(ctypes.pointer(time_series.arr_reference), + ctypes.c_long(subsequence_length), + ctypes.c_double(threshold), + ctypes.pointer(sums), + ctypes.pointer(error_code), + error_message) + if error_code.value != 0: + raise Exception(str(error_message.value.decode())) + + return Array(array_reference=sums) + + +def matrix_profile_threshold(first_time_series, second_time_series, subsequence_length, threshold): + """ Calculates the sum of correlations above a threshold between 'tss' and itself using a subsequence length + of 'm' at each location in 'tss'. + + [1] Yan Zhu, Zachary Zimmerman, Nader Shakibay Senobari, Chin-Chia Michael Yeh, Gareth Funning, Abdullah Mueen, + Philip Brisk and Eamonn Keogh (2016). Matrix Profile II: Exploiting a Novel Algorithm and GPUs to break the one + Hundred Million Barrier for Time Series Motifs and Joins. IEEE ICDM 2016. + + :param first_time_series: Query and reference time series. + :param second_time_series: Reference time series. + :param subsequence_length: Subsequence length. + :param threshold: Threshold. + :return The sum of correlations above a threshold between 'time_series' and itself using a subsequence length + of 'subsequence_length' at each location in 'time_series'. + """ + sums = ctypes.c_void_p(0) + + error_code = ctypes.c_int(0) + error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH) + KhivaLibrary().c_khiva_library.matrix_profile_threshold(ctypes.pointer(first_time_series.arr_reference), + ctypes.pointer(second_time_series.arr_reference), + ctypes.c_long(subsequence_length), + ctypes.c_double(threshold), + ctypes.pointer(sums), + ctypes.pointer(error_code), + error_message) + if error_code.value != 0: + raise Exception(str(error_message.value.decode())) + + return Array(array_reference=sums) \ No newline at end of file