90 lines
2.5 KiB
Python
Executable file
90 lines
2.5 KiB
Python
Executable file
import numpy as np
|
|
import pandas as pd
|
|
from scipy import stats
|
|
|
|
##### PLOTTING
|
|
|
|
# plot a monovariate series
|
|
# aka a row of a file (dataframe)
|
|
# which has been normalized
|
|
def plot_mono_series(frame, index):
|
|
from matplotlib import pyplot as plt
|
|
plt.switch_backend("GTK3Agg")
|
|
s = pd.Series(frame[index,:])
|
|
s.plot()
|
|
plt.show()
|
|
|
|
##### DISCRETIZATION
|
|
|
|
# generate a interval [-1,1]
|
|
# of 2*resolution normally spaced steps
|
|
# using a gaussian distribution
|
|
def gaussian_interval(resolution):
|
|
# use a gaussian distribution, mean 0, std deviation 0.25
|
|
distribution = stats.norm(loc=0, scale=0.25)
|
|
# bounds of range for inverse comulative distribution function
|
|
bounds = distribution.cdf([-1, 1])
|
|
# generate linear space of 2*resolution points using bounds
|
|
linsp = np.linspace(*bounds, num=2*resolution)
|
|
# obtain the array of 2*resolution points
|
|
x = distribution.ppf(linsp)
|
|
return pd.Series(x)
|
|
|
|
# create a new series with the mapping
|
|
# series[n] -> gaussianInterval[inds[n]]
|
|
# ---
|
|
# inds are the indexes of gaussianInterval entries
|
|
# corresponding to series entries
|
|
def discretize_series(series, gaussianInterval):
|
|
inds = np.digitize(series, gaussianInterval)
|
|
res = []
|
|
for n in range(series.size):
|
|
res.append(gaussianInterval[inds[n]-1])
|
|
# print(gaussianInterval[inds[n]-1], "<=", series[n], "<", gaussianInterval[inds[n]])
|
|
return pd.Series(res)
|
|
|
|
# given a dataframe, extract a series from it
|
|
def extract_series(frame, index):
|
|
return pd.Series(frame[index,:])
|
|
|
|
##### GESTURE WORDS
|
|
|
|
# a python iterator
|
|
# used to compute gesture words
|
|
# on a monovariate series
|
|
class SlidingWindow:
|
|
def __init__(self, series, window, step):
|
|
self._series = series
|
|
self._center = int(window/2)
|
|
self._window = window
|
|
self._step = step
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self):
|
|
start = int(self._center - self._window/2)
|
|
stop = int(self._center + self._window/2)
|
|
if self._center <= len(self._series):
|
|
word = self._series[start:stop]
|
|
self._center += self._step
|
|
return word
|
|
else:
|
|
raise StopIteration()
|
|
|
|
# given a monovariate series,
|
|
# 1. slide a window of size 'window' with step 'step'
|
|
# --> SlidingWindow
|
|
# 2. save each window as a gesture word
|
|
# TODO might as well perform some computing here
|
|
def get_gesture_words(series, window, step):
|
|
series = series.values
|
|
gw = []
|
|
sw = SlidingWindow(series, window, step)
|
|
for word in sw:
|
|
gw.append(word)
|
|
return gw
|
|
|
|
|
|
|
|
|