# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Studing fdasrvf | Python
#
# * Python pkg: https://github.com/jdtuck/fdasrsf_python
#
# * All pkgs: http://research.tetonedge.net/software.html

# + code_folding=[]
# loading modules

# %matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

import seaborn as sns
from tqdm import tqdm
from time import time



# Registration packages
import fdasrsf as fs
# -

df = pd.read_csv("data_temp.csv", index_col=0)
df.shape

df.plot(x="time")

df.iloc[:,:-1].head()

df["time"]

# +
f     = np.copy(df.iloc[:,:-1].values) # all cols but the last one (which is the pseudo_time)
t_vec = np.copy(df["time"].values)  # only the last col, which is the pseudo_time

print(f.shape, t_vec.shape)
print(f.dtype, t_vec.dtype)

# +
tic = time() # 

obj = fs.fdawarp(f, t_vec)

obj.srsf_align(smoothdata=False, MaxItr=5)

elapsed = time()-tic
print("- Elapsed time:")
print("%.4f (s)"%(elapsed))
# -

obj.plot()

# ### outro
#
# * https://fdasrsf-python.readthedocs.io/en/latest/_modules/time_warping.html#fdawarp.multiple_align_functions

import fdasrsf.utility_functions as uf
from joblib import Parallel, delayed

# +
omethod="DP2"
lam=0.0

grid_dim=7

# +
M = f.shape[0]
N = f.shape[1]
    
eps = np.finfo(np.double).eps


# Compute SRSF function from data
f, g, g2 = uf.gradient_spline(t_vec, f, smooth=False)
q = g / np.sqrt(abs(g) + eps)

print("Initializing...")
mnq = q.mean(axis=1) # mean srvf
a = mnq.repeat(N)
d1 = a.reshape(M, N)
d = (q - d1) ** 2
dqq = np.sqrt(d.sum(axis=0))
min_ind = dqq.argmin()
mq = q[:, min_ind]
mf = f[:, min_ind]
# -

out = Parallel(n_jobs=-2)(delayed(uf.optimum_reparam)(mq, t_vec,
                        q[:, n], omethod, lam, grid_dim) for n in range(N))
gam = np.array(out)
gam = gam.transpose()

gam.shape

plt.plot(gam);

plt.plot(obj.gam);

plt.plot(uf.SqrtMeanInverse(obj.gam));

mnq.shape

df.plot(x="time")

plt.plot(mnq)



# +
tic = time() # 

obj = fs.fdawarp(f, t_vec)

obj.multiple_align_functions(mu=f[:,0], smoothdata=False)

elapsed = time()-tic
print("- Elapsed time:")
print("%.4f (s)"%(elapsed))
# -

obj.plot()



StarcoderdataJupyterScriptsDedupFiltered