Skip to content

Commit

Permalink
Added some small (missing) helper functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiankmiec committed May 25, 2019
1 parent 4f8c9e6 commit 271bab8
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 105 deletions.
11 changes: 7 additions & 4 deletions new_data_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from ninaeval.config import config_parser, config_setup
from ninaeval.utils.nina_data import NinaDataParser
from ninaeval.utils.data_extract import extract_myo_all_csv

DATA_PATH = "all_data/"
MODEL_PATH = "all_models/"
DATA_PATH = "all_data/"
MODEL_PATH = "all_models/"

def main():

Expand All @@ -21,10 +20,14 @@ def main():

new_data = {}

#
# Your own data paths....
#

# extract_myo_all_csv('/home/skmiec/Documents/ex5/a/myo_all_data.csv', new_data, "s11", "E1")
# extract_myo_all_csv('/home/skmiec/Documents/ex5/b/myo_all_data.csv', new_data, "s11", "E2")
# extract_myo_all_csv('/home/skmiec/Documents/ex5/c/myo_all_data.csv', new_data, "s11", "E3")
#

# extract_myo_all_csv('/home/skmiec/Documents/ex6/a/myo_all_data.csv', new_data, "s12", "E1")
# extract_myo_all_csv('/home/skmiec/Documents/ex6/b/myo_all_data.csv', new_data, "s12", "E2")
# extract_myo_all_csv('/home/skmiec/Documents/ex6/c/myo_all_data.csv', new_data, "s12", "E3")
Expand Down
2 changes: 1 addition & 1 deletion ninaeval/config/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def init_parser():
#
# Dataset choices
#
parser.add_option('--data', action='store', type='choice', default='v1',
parser.add_option('--data', action='store', type='choice', default='base',
choices=list(dataset_choices.keys()))

return parser
Expand Down
24 changes: 20 additions & 4 deletions ninaeval/config/config_setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from ninaeval.models import baseline_model, advanced_model
from ninaeval.config.config_parser import model_choices, feature_choices
from ninaeval.config.config_parser import model_choices, feature_choices, dataset_choices
from ninaeval.utils import nina_data

def get_model(model_abbrev):
"""
:param model_abbrev: An abbreviated model name (config_parser.py).
:return: ClassifierModel
:return: ClassifierModel object
"""

model = None
Expand All @@ -21,7 +22,7 @@ def get_model(model_abbrev):
def get_feat_extract(features_abbrev):
"""
:param features_abbrev: An abbreviated model name (config_parser.py).
:return: FeatureExtractor
:return: FeatureExtractor object
"""

feat_ext = None
Expand All @@ -33,4 +34,19 @@ def get_feat_extract(features_abbrev):
except AttributeError:
pass

return feat_ext
return feat_ext


def get_dataset(dataset_abbrev):
"""
:param dataset_abbrev: An abbreviated dataset name (config_parser.py).
:return: Dataset object
"""

dataset = None
try:
dataset = getattr(nina_data, dataset_choices[dataset_abbrev])
except AttributeError:
pass

return dataset
132 changes: 66 additions & 66 deletions ninaeval/models/advanced_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def extract_feature_point(self, raw_samples):
descriptors = [np.sqrt(np.mean(np.square(data), axis=0)) for data in emg_data]
descriptors = np.array(descriptors)

return descriptors
return descriptors.flatten()

# Window based RMS
#
Expand Down Expand Up @@ -535,63 +535,11 @@ class PaddedMultiRMS(FeatureExtractor):
#
####################################################################################################################
####################################################################################################################
# def extract_feature_point(self, raw_samples):
# # Define data windows
# start_indices = np.linspace(0, raw_samples.shape[0] - self.window_size, num=self.num_descriptors,
# endpoint=False,
# dtype=int)
# emg_data = [raw_samples[y: y+self.window_size] for y in start_indices]
#
# # Create RMS descriptors
# descriptors = np.array([np.mean(np.abs(data), axis=0) for data in emg_data])
# norms = np.linalg.norm(descriptors, axis=1)
# descriptors /= np.expand_dims(norms, axis=1)
# descriptors = self.pca.transform(descriptors)
#
# # Pad with zros
# num_miss = self.num_descriptors - descriptors.shape[0]
# descriptors = np.concatenate((descriptors, np.zeros((num_miss, self.pca_dim))), axis=0)
# descriptors = descriptors.flatten()
# return descriptors
#
# def global_setup(self, all_raw_samples):
#
# all_descriptors = []
#
# for raw_samples in all_raw_samples:
# # Define data windows
# start_indices = np.linspace(0, raw_samples.shape[0] - self.window_size -1 , num=self.num_descriptors, endpoint=False,
# dtype=int)
# emg_data = [raw_samples[y: y + self.window_size] for y in start_indices]
#
# # Create RMS descriptors
# temp = np.array([np.mean(np.abs(data), axis=0) for data in emg_data])
# norm = np.linalg.norm(temp, axis=1)
# temp = temp / np.expand_dims(norm, axis=1)
#
# for i in range(temp.shape[0]):
# all_descriptors.append(temp[i])
#
# all_descriptors = np.array(all_descriptors)
#
# self.pca = PCA(n_components=self.pca_dim)
# self.pca.fit(all_descriptors)


####################################################################################################################
####################################################################################################################
#
# PCA, non-overlapping window version (ORIGINAL)
#
####################################################################################################################
####################################################################################################################
def extract_feature_point(self, raw_samples):
# Define data windows
if raw_samples.shape[0] // self.window_size >= self.num_descriptors:
start_indices = np.linspace(0, raw_samples.shape[0], num=self.num_descriptors, endpoint=False, dtype=int)
else:
num_start_indices = raw_samples.shape[0] // self.window_size
start_indices = [x * self.window_size for x in range(num_start_indices)]
start_indices = np.linspace(0, raw_samples.shape[0] - self.window_size, num=self.num_descriptors,
endpoint=False,
dtype=int)
emg_data = [raw_samples[y: y+self.window_size] for y in start_indices]

# Create RMS descriptors
Expand All @@ -608,28 +556,80 @@ def extract_feature_point(self, raw_samples):

def global_setup(self, all_raw_samples):

all_descriptors = []
all_descriptors = []

for raw_samples in all_raw_samples:
# Define data windows
if raw_samples.shape[0] // self.window_size >= self.num_descriptors:
start_indices = np.linspace(0, raw_samples.shape[0], num=self.num_descriptors, endpoint=False,
dtype=int)
else:
num_start_indices = raw_samples.shape[0] // self.window_size
start_indices = [x * self.window_size for x in range(num_start_indices)]

emg_data = [raw_samples[y: y + self.window_size] for y in start_indices]
start_indices = np.linspace(0, raw_samples.shape[0] - self.window_size -1 , num=self.num_descriptors, endpoint=False,
dtype=int)
emg_data = [raw_samples[y: y + self.window_size] for y in start_indices]

# Create RMS descriptors
temp = np.array([np.mean(np.abs(data), axis=0) for data in emg_data])
norm = np.linalg.norm(temp, axis=1)
temp = temp / np.expand_dims(norm, axis=1)

for i in range(temp.shape[0]):
all_descriptors.append(temp[i])

all_descriptors = np.array(all_descriptors)

self.pca = PCA(n_components=self.pca_dim)
self.pca.fit(all_descriptors)


####################################################################################################################
####################################################################################################################
#
# PCA, non-overlapping window version (ORIGINAL)
#
####################################################################################################################
####################################################################################################################
# def extract_feature_point(self, raw_samples):
# # Define data windows
# if raw_samples.shape[0] // self.window_size >= self.num_descriptors:
# start_indices = np.linspace(0, raw_samples.shape[0], num=self.num_descriptors, endpoint=False, dtype=int)
# else:
# num_start_indices = raw_samples.shape[0] // self.window_size
# start_indices = [x * self.window_size for x in range(num_start_indices)]
# emg_data = [raw_samples[y: y+self.window_size] for y in start_indices]
#
# # Create RMS descriptors
# descriptors = np.array([np.mean(np.abs(data), axis=0) for data in emg_data])
# norms = np.linalg.norm(descriptors, axis=1)
# descriptors /= np.expand_dims(norms, axis=1)
# descriptors = self.pca.transform(descriptors)
#
# # Pad with zros
# num_miss = self.num_descriptors - descriptors.shape[0]
# descriptors = np.concatenate((descriptors, np.zeros((num_miss, self.pca_dim))), axis=0)
# descriptors = descriptors.flatten()
# return descriptors
#
# def global_setup(self, all_raw_samples):
#
# all_descriptors = []
#
# for raw_samples in all_raw_samples:
# # Define data windows
# if raw_samples.shape[0] // self.window_size >= self.num_descriptors:
# start_indices = np.linspace(0, raw_samples.shape[0], num=self.num_descriptors, endpoint=False,
# dtype=int)
# else:
# num_start_indices = raw_samples.shape[0] // self.window_size
# start_indices = [x * self.window_size for x in range(num_start_indices)]
#
# emg_data = [raw_samples[y: y + self.window_size] for y in start_indices]
#
# temp = np.array([np.mean(np.abs(data), axis=0) for data in emg_data])
# norm = np.linalg.norm(temp, axis=1)
# temp = temp / np.expand_dims(norm, axis=1)
# for i in range(temp.shape[0]):
# all_descriptors.append(temp[i])
#
# all_descriptors = np.array(all_descriptors)
# self.pca = PCA(n_components=self.pca_dim)
# self.pca.fit(all_descriptors)
####################################################################################################################
####################################################################################################################

Expand Down Expand Up @@ -704,7 +704,7 @@ class IMUPaddedMultiRMS(FeatureExtractor):
acc_dim = 6
gyro_dim = 6
mag_dim = 8
#imu_dim = 20 # acc: 6, gyro: 6, mag: 8
#imu_dim = 20 # acc: 6, gyro: 6, mag: 8
imu_dim = 12 # acc: 6, gyro: 6, mag: 8

requires_global_setup = True
Expand Down
2 changes: 1 addition & 1 deletion ninaeval/models/baseline_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def train_model(self, train_features, train_labels, valid_features = None, valid
self.num_samples = train_features.shape[0]
self.classifier.fit(train_features, train_labels)

def perform_inference(self, test_features, test_labels):
def perform_inference_helper(self, test_features):
return self.classifier.predict(test_features)

def save_figure(self, path):
Expand Down
16 changes: 11 additions & 5 deletions ninaeval/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def train_model(self, train_features, train_labels, valid_features = None, valid
"""
pass

def perform_inference(self, test_features, test_labels):
def perform_inference(self, test_features, test_labels = None):
"""
Given test features and labels, compute predictions and classifier accuracy,
Expand All @@ -50,15 +50,15 @@ def perform_inference(self, test_features, test_labels):
:return: * (test_labels != None) Classifier accuracy from 0 ~ 1.0.
* (test_labels == None) Predictions
"""
predictions = self.perform_inference(test_features)
predictions = self.perform_inference_helper(test_features)

if test_labels is None:
return predictions
else:
return self.classifier_accuracy(predictions, test_labels)

@abstractmethod
def perform_inference(self, test_features):
def perform_inference_helper(self, test_features):
"""
Helper function for above
"""
Expand Down Expand Up @@ -115,7 +115,7 @@ def per_class_accuracy(self, test_features, test_labels):
:return: [dict] : Each key refers to a per class accuracy
"""

pred_labels = self.perform_inference(test_features)
pred_labels = self.perform_inference_helper(test_features)

test_labels_found = {}
for x in test_labels:
Expand Down Expand Up @@ -230,7 +230,7 @@ def train_model(self, train_features, train_labels, valid_features = None, valid
print("Training complete ({} epochs)...".format(self.num_epoch))


def perform_inference(self, test_features):
def perform_inference_helper(self, test_features):
"""
Helper function for ClassifierModel's perform_inference(self, test_features, test_labels)
"""
Expand All @@ -243,6 +243,12 @@ def perform_inference(self, test_features):

return predictions

def get_class_probabilities(self, test_features):
# Use inference model
self.model.eval()
predictions = (self.model((torch.from_numpy(test_features).float()).to(self.device))).cpu().detach().numpy()
return predictions

def perform_testing(self, test_features, test_labels):
"""
Performs a forward pass on each batch in the validation split
Expand Down
Loading

0 comments on commit 271bab8

Please sign in to comment.