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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/sebastianPsm/orc/blob/master/sample_data/rowing_data_MASS_Algorithm_Test.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="1iI2dqcWwKrt" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 127} outputId="4c84c9a2-35a4-4e91-e606-7730604fd190"
# !pip install mass_ts

# + id="fRi1zMwbwESv" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 73} outputId="d1810353-6c71-4adf-ec15-45817f4e06be"
import datetime
import numpy as np
import pandas as pd
import altair as alt
import matplotlib.pyplot as plt
import mass_ts as mts # https://www.cs.unm.edu/~mueen/FastestSimilaritySearch.html

# + id="5ka4nU0MwPOh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 215} outputId="da6b4d4a-e80f-451b-9e9d-fd2a606f310f"
# Clone orc repo with rowing data
# !git clone -l -s git://github.com/sebastianpsm/orc.git cloned-repo
# %cd cloned-repo
# !ls
# %cd ..

# + id="X67Lhqf6a6Jk" colab_type="code" colab={}
data = pd.read_csv("cloned-repo/sample_data/20200517.CSV")
fs = 50 # sample freq. [Hz]
rec_data = "17.5.20 7:15"

# + id="5QblzeFrbfXv" colab_type="code" colab={}
# Add time column
data["time_delta"] = data["time_delta[ms]"].astype('timedelta64[ms]')
data["t"] = data["time_delta"].cumsum() + datetime.datetime.strptime(rec_data, "%d.%m.%y %H:%M")

# + id="Sf3I-WcHhsor" colab_type="code" colab={}
# Filter
rolling_window = 25
data["accel_x_filter"] = data["accel_x[G]"].rolling(rolling_window).mean()
data["accel_y_filter"] = data["accel_y[G]"].rolling(rolling_window).mean()
data["accel_z_filter"] = data["accel_z[G]"].rolling(rolling_window).mean()
data["gyro_x_filter"] = data["gyro_x[deg/s]"].rolling(rolling_window).mean()
data["gyro_y_filter"] = data["gyro_x[deg/s]"].rolling(rolling_window).mean()
data["gyro_z_filter"] = data["gyro_x[deg/s]"].rolling(rolling_window).mean()
data = data.dropna()

# + id="YIL1x8KNiRqi" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 405} outputId="f5a2d668-4f68-491b-cdb5-e5c6240538d5"
start=10000
stop=start+5000

plot_data = data[start:stop]
plot_data = plot_data[["t", "accel_x_filter", "accel_y_filter", "accel_z_filter"]]

accel_x_plot = alt.Chart(plot_data).mark_line(color="red").encode(alt.X("t:T", title="seconds"), alt.Y("accel_x_filter", title="acceleration [G]"))
accel_y_plot = alt.Chart(plot_data).mark_line(color="green").encode(alt.X("t:T", title="seconds"), alt.Y("accel_y_filter", title="acceleration [G]"))
accel_z_plot = alt.Chart(plot_data).mark_line(color="blue").encode(alt.X("t:T", title="seconds"), alt.Y("accel_z_filter", title="acceleration [G]"))
(accel_x_plot + accel_y_plot + accel_z_plot).properties(width=800,title="Acceleration [G]").interactive(bind_y=False)

# + id="eebLXAndjK-H" colab_type="code" colab={}
start = 1095
stop = 1245
len  = stop-start
q = np.array(plot_data["accel_z_filter"][start:stop])
ts = np.array(data["accel_z_filter"])
distances = mts.mass2(ts,q)

# + id="7xvcSx7TIzyb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 283} outputId="de5b33cb-df78-4eb3-a4ab-af9b3a7fa924"
plt.plot(np.abs(distances)[10000:15000])

# + id="soGxNPxHj8Gb" colab_type="code" colab={}
k = 200
exclusion_zone = 25
top_motifs = mts.top_k_motifs(distances, k, exclusion_zone)

# + id="9TZhi83FkH_U" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 265} outputId="da11aa41-1a82-4d62-ce13-1c537365c81b"
for idx, top_motif in enumerate(top_motifs):
  motiv = data["accel_z_filter"][top_motif:top_motif+len]
  plt.plot(range(0,len), motiv)

StarcoderdataJupyterScriptsDedupFiltered