Commit 9dba0c5c authored by paugier's avatar paugier
Browse files

Clean up dtw_cort_dist

parent eb506e6c
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*.so *.so
*.o *.o
*.pstats *.pstats
*.mod
_build _build
build build
...@@ -24,4 +25,6 @@ ipynb/index.rst ...@@ -24,4 +25,6 @@ ipynb/index.rst
ipynb/index.html ipynb/index.html
pyfiles/dtw_cort_dist/V5_cython/*.c pyfiles/dtw_cort_dist/V5_cython/*.c
pyfiles/dtw_cort_dist/V5_cython/*.html pyfiles/dtw_cort_dist/V5_cython/*.html
\ No newline at end of file **/V*/res_cort.npy
**/V*/res_dtw.npy
\ No newline at end of file
...@@ -19,8 +19,7 @@ def serie_pair_index_generator(number): ...@@ -19,8 +19,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
...@@ -39,7 +38,6 @@ def DTWDistance(s1, s2): ...@@ -39,7 +38,6 @@ def DTWDistance(s1, s2):
_dtw_mat[0, 0] = abs(s1[0] - s2[0]) _dtw_mat[0, 0] = abs(s1[0] - s2[0])
# two special cases : filling first row and columns # two special cases : filling first row and columns
for j in range(1, len_s2): for j in range(1, len_s2):
dist = abs(s1[0] - s2[j]) dist = abs(s1[0] - s2[j])
_dtw_mat[0, j] = dist + _dtw_mat[0, j - 1] _dtw_mat[0, j] = dist + _dtw_mat[0, j - 1]
......
...@@ -5,7 +5,6 @@ from runpy import run_path ...@@ -5,7 +5,6 @@ from runpy import run_path
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
import math
util = run_path(Path(__file__).absolute().parent.parent / "util.py") util = run_path(Path(__file__).absolute().parent.parent / "util.py")
...@@ -20,8 +19,7 @@ def serie_pair_index_generator(number): ...@@ -20,8 +19,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
...@@ -68,14 +66,12 @@ def cort(s1, s2): ...@@ -68,14 +66,12 @@ def cort(s1, s2):
:rtype: float64 :rtype: float64
""" """
slope_1 = s1[1:] - s1[:-1] slope_1 = s1[1:] - s1[:-1]
slope_2 = s2[1:] - s2[:-1] slope_2 = s2[1:] - s2[:-1]
num = np.sum(slope_1 * slope_2) num = np.sum(slope_1 * slope_2)
sum_square_x = np.sum(slope_1 * slope_1) sum_square_x = np.sum(slope_1 * slope_1)
sum_square_y = np.sum(slope_2 * slope_2) sum_square_y = np.sum(slope_2 * slope_2)
return num / (np.sqrt(sum_square_x * sum_square_y))
return num / (math.sqrt(sum_square_x * sum_square_y))
def compute(series, nb_series): def compute(series, nb_series):
......
...@@ -30,8 +30,7 @@ def serie_pair_index_generator(number): ...@@ -30,8 +30,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
......
...@@ -27,8 +27,7 @@ def serie_pair_index_generator(number): ...@@ -27,8 +27,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
......
#!/usr/bin/python3 #!/usr/bin/env python3
import numpy as np import numpy as np
import sysconfig import sysconfig
...@@ -25,8 +25,11 @@ def serie_pair_index_generator(number): ...@@ -25,8 +25,11 @@ def serie_pair_index_generator(number):
:returns: pairs (lower, greater) :returns: pairs (lower, greater)
:rtype: a generator :rtype: a generator
""" """
return ((_idx_greater, _idx_lower) for _idx_greater in range(number) return (
for _idx_lower in range(number) if _idx_lower < _idx_greater) (_idx_greater, _idx_lower)
for _idx_greater in range(number)
for _idx_lower in range(_idx_greater)
)
def cDTW(serie_a, serie_b): def cDTW(serie_a, serie_b):
...@@ -50,6 +53,7 @@ def cort(serie_a, serie_b): ...@@ -50,6 +53,7 @@ def cort(serie_a, serie_b):
ret = dllib.cort(a_ptr, b_ptr, len(serie_a)) ret = dllib.cort(a_ptr, b_ptr, len(serie_a))
return ret return ret
def compute(series, nb_series): def compute(series, nb_series):
gen = serie_pair_index_generator(nb_series) gen = serie_pair_index_generator(nb_series)
......
...@@ -10,6 +10,7 @@ from dtw_cort import cort, DTWDistance ...@@ -10,6 +10,7 @@ from dtw_cort import cort, DTWDistance
util = run_path(Path(__file__).absolute().parent.parent / "util.py") util = run_path(Path(__file__).absolute().parent.parent / "util.py")
def serie_pair_index_generator(number): def serie_pair_index_generator(number):
""" generator for pair index (i, j) such that i < j < number """ generator for pair index (i, j) such that i < j < number
...@@ -20,8 +21,7 @@ def serie_pair_index_generator(number): ...@@ -20,8 +21,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
......
all: all:
transonic dtw_cort_dist_mat.py transonic dtw_cort_dist_mat.py -pf "-march=native -O3 -DUSE_XSIMD"
clean: clean:
rm -rf __pythran__ rm -rf __pythran__
\ No newline at end of file
...@@ -21,8 +21,7 @@ def serie_pair_index_generator(number): ...@@ -21,8 +21,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
...@@ -62,23 +61,19 @@ def DTWDistance(s1, s2): ...@@ -62,23 +61,19 @@ def DTWDistance(s1, s2):
def cort(s1, s2): def cort(s1, s2):
""" Computes the cort between serie one and two (assuming they have the same length) """ Computes the cort between series one and two (assuming they have the same length)
:param s1: the first serie (or any iterable over floats64) :param s1: the first series (or any iterable over floats64)
:param s2: the second serie (or any iterable over floats64) :param s2: the second series (or any iterable over floats64)
:returns: the cort distance :returns: the cort distance
:rtype: float64 :rtype: float64
""" """
num = 0.0 slope_1 = s1[1:] - s1[:-1]
sum_square_x = 0.0 slope_2 = s2[1:] - s2[:-1]
sum_square_y = 0.0 num = np.sum(slope_1 * slope_2)
for t in range(len(s1) - 1): sum_square_x = np.sum(slope_1 * slope_1)
slope_1 = s1[t + 1] - s1[t] sum_square_y = np.sum(slope_2 * slope_2)
slope_2 = s2[t + 1] - s2[t]
num += slope_1 * slope_2
sum_square_x += slope_1 * slope_1
sum_square_y += slope_2 * slope_2
return num / (np.sqrt(sum_square_x * sum_square_y)) return num / (np.sqrt(sum_square_x * sum_square_y))
......
...@@ -22,8 +22,7 @@ def serie_pair_index_generator(number): ...@@ -22,8 +22,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
...@@ -63,23 +62,19 @@ def DTWDistance(s1, s2): ...@@ -63,23 +62,19 @@ def DTWDistance(s1, s2):
def cort(s1, s2): def cort(s1, s2):
""" Computes the cort between serie one and two (assuming they have the same length) """ Computes the cort between series one and two (assuming they have the same length)
:param s1: the first serie (or any iterable over floats64) :param s1: the first series (or any iterable over floats64)
:param s2: the second serie (or any iterable over floats64) :param s2: the second series (or any iterable over floats64)
:returns: the cort distance :returns: the cort distance
:rtype: float64 :rtype: float64
""" """
num = 0.0 slope_1 = s1[1:] - s1[:-1]
sum_square_x = 0.0 slope_2 = s2[1:] - s2[:-1]
sum_square_y = 0.0 num = np.sum(slope_1 * slope_2)
for t in range(len(s1) - 1): sum_square_x = np.sum(slope_1 * slope_1)
slope_1 = s1[t + 1] - s1[t] sum_square_y = np.sum(slope_2 * slope_2)
slope_2 = s2[t + 1] - s2[t]
num += slope_1 * slope_2
sum_square_x += slope_1 * slope_1
sum_square_y += slope_2 * slope_2
return num / (np.sqrt(sum_square_x * sum_square_y)) return num / (np.sqrt(sum_square_x * sum_square_y))
......
all: all:
pythran kernels_pythran.py pythran kernels_pythran.py -march=native -O3 -DUSE_XSIMD
clean: clean:
rm -rf *.so rm -rf *.so
\ No newline at end of file
...@@ -6,8 +6,7 @@ def serie_pair_index_generator(number): ...@@ -6,8 +6,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if (_idx_lower < _idx_greater)
) )
...@@ -37,17 +36,20 @@ def DTWDistance(s1, s2): ...@@ -37,17 +36,20 @@ def DTWDistance(s1, s2):
def cort(s1, s2): def cort(s1, s2):
" Computes the cort between serie one and two (assuming they have the same length)\n\n :param s1: the first serie (or any iterable over floats64)\n :param s2: the second serie (or any iterable over floats64)\n :returns: the cort distance\n :rtype: float64\n\n " """ Computes the cort between series one and two (assuming they have the same length)
num = 0.0
sum_square_x = 0.0 :param s1: the first series (or any iterable over floats64)
sum_square_y = 0.0 :param s2: the second series (or any iterable over floats64)
for t in range((len(s1) - 1)): :returns: the cort distance
slope_1 = s1[(t + 1)] - s1[t] :rtype: float64
slope_2 = s2[(t + 1)] - s2[t]
num += slope_1 * slope_2 """
sum_square_x += slope_1 * slope_1 slope_1 = s1[1:] - s1[:-1]
sum_square_y += slope_2 * slope_2 slope_2 = s2[1:] - s2[:-1]
return num / np.sqrt((sum_square_x * sum_square_y)) num = np.sum(slope_1 * slope_2)
sum_square_x = np.sum(slope_1 * slope_1)
sum_square_y = np.sum(slope_2 * slope_2)
return num / (np.sqrt(sum_square_x * sum_square_y))
# pythran export compute(float64[:, :], int) # pythran export compute(float64[:, :], int)
......
...@@ -21,8 +21,7 @@ def serie_pair_index_generator(number): ...@@ -21,8 +21,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
......
...@@ -20,8 +20,7 @@ def serie_pair_index_generator(number): ...@@ -20,8 +20,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
...@@ -36,6 +35,7 @@ def DTWDistance(s1, s2): ...@@ -36,6 +35,7 @@ def DTWDistance(s1, s2):
dtw_result = dtw_cort.dtwdistance(s1, s2) dtw_result = dtw_cort.dtwdistance(s1, s2)
return dtw_result return dtw_result
def cort(s1, s2): def cort(s1, s2):
""" Computes the cort between serie one and two (assuming they have the same length) """ Computes the cort between serie one and two (assuming they have the same length)
......
...@@ -17,8 +17,7 @@ def serie_pair_index_generator(number): ...@@ -17,8 +17,7 @@ def serie_pair_index_generator(number):
return ( return (
(_idx_greater, _idx_lower) (_idx_greater, _idx_lower)
for _idx_greater in range(number) for _idx_greater in range(number)
for _idx_lower in range(number) for _idx_lower in range(_idx_greater)
if _idx_lower < _idx_greater
) )
......
...@@ -17,6 +17,7 @@ cffi ...@@ -17,6 +17,7 @@ cffi
cython cython
numba numba
pythran pythran
xsimd
transonic transonic
# parallel # parallel
mpi4py mpi4py
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment