-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalizers.py
69 lines (54 loc) · 2.44 KB
/
normalizers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
# TODO: DB5 specific right now, change so extracts by last column in general
def normalize(data : pd.DataFrame, train_reps : list) -> pd.DataFrame:
"""
Given sEMG data and exercise repetitions, extracts the subset of data from specification,
which is centered and scaled to unit variance.
Parameters
----------
data : pd.Dataframe
Collection of sensor data from which repetitions are to be extracted
train_reps : list
Specified exercise repetitions to extract by
Returns
-------
pd.DataFrame
Centered and unit-variance scaled sensor data for specified exercise repetitions
"""
x = [np.where(data.values[:,17] == rep) for rep in train_reps]
indices = np.squeeze(np.concatenate(x, axis = -1))
train_data = data.iloc[indices, :].reset_index(drop=True)
scaler = StandardScaler(with_mean=True,
with_std=True,
copy=False).fit(train_data.iloc[:, :16])
scaled = scaler.transform(data.iloc[:,:16])
normalized = pd.DataFrame(scaled)
normalized['stimulus'], normalized['repetition'] = data['stimulus'], data['repetition']
return normalized
def better_normalize(data : pd.DataFrame, train_reps : list, dims : int) -> pd.DataFrame:
"""
Given sEMG data and exercise repetitions, extracts the subset of data from specification,
which is centered and scaled to unit variance.
Parameters
----------
data : pd.Dataframe
Collection of sensor data from which repetitions are to be extracted
train_reps : list
Specified exercise repetitions to extract by
Returns
-------
pd.DataFrame
Centered and unit-variance scaled sensor data for specified exercise repetitions
"""
x = [np.where(data.repetition.values == rep) for rep in train_reps]
indices = np.squeeze(np.concatenate(x, axis = -1))
train_data = data.iloc[indices, :].reset_index(drop=True)
scaler = StandardScaler(with_mean=True,
with_std=True,
copy=False).fit(train_data.iloc[:, :dims])
scaled = scaler.transform(data.iloc[:,:dims])
normalized = pd.DataFrame(scaled)
normalized['stimulus'], normalized['repetition'] = data['stimulus'], data['repetition']
return normalized