-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmatrix.py
106 lines (88 loc) · 3.3 KB
/
xmatrix.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 12 14:19:25 2017
@author: Jordan
Creates an x-matrix for analysis
Updating July 8 2017.
We no longer need the patient
"""
import numpy as np
import re
x = []
t = []
global nancount
nancount = 0
def loadfile(file):
tempfile = []
global nancount
data = np.genfromtxt(file,delimiter=',')
# Remove the header row
data = data[1:,:]
# Use RegEx to get the gesture number
trial_info = re.findall('\[([0-9]{1,2})\]',file)
trial_info = np.asarray(trial_info)
trial_info = trial_info.astype(int)
# patient = int(trial_info[0])
# gesture = int(trial_info[1])
# trial = int(trial_info[2])
# Loop through the data and save to an array
for row in data:
# Use if loop to check for valid row data, ignoring ragged data
if np.isnan(row).any() == True:
# print "nan found in file",file
nancount = nancount + 1
else:
newrow = np.concatenate((trial_info,row),axis=0)
tempfile.append(newrow)
def xmatrix(files):
# Accepts a list of files, extracts each row, builds x matrix.
# Updating the file to export a file of following format:
# [patient class trial -data-]
# Therefore files with MxN dimension will return a MxN+3 matrix
# Update function as of July 8 2017 for following output, 60 column array
# [patient, class, trial, x-coord, y-coord, DistIR, RPY, Omron Sensors]
x = []
patient = []
gesture = []
trial = []
global nancount
nancount = 0
# Check if we have a single file
if isinstance(files,str) == True:
# Then just load the single file and output to x and t
data = np.genfromtxt(files,delimiter=',')
data = data[1:,:] # Only consider data from row 1 downwards (due to headers)
trial_info = re.findall('\[([0-9]{1,2})\]',files)
gesture = int(trial_info[1])
for row in data:
x.append(row)
t.append(gesture)
# Otherwise we have multiple files and need to iterate through them.
else:
for file in files:
# Load the data from csv file
data = np.genfromtxt(file,delimiter=',')
# Remove the header row
data = data[1:,:]
# Use RegEx to get the patient, class, trial data
trial_info = re.findall('\[([0-9]{1,2})\]',file)
trial_info = np.asarray(trial_info)
trial_info = trial_info.astype(int)
# Loop through the data and save to an array
for row in data:
# Use if loop to check for valid row data, ignoring ragged data
if np.isnan(row).any() == True:
# print "nan found in file",file
nancount = nancount + 1
else:
x.append(row)
patient.append(trial_info[0])
gesture.append(trial_info[1])
trial.append(trial_info[2])
# Reformat as arrays
x = np.asarray(x)
numrows = len(patient)
patient = np.reshape(patient,(numrows,1))
gesture = np.reshape(gesture,(numrows,1))
trial = np.reshape(trial,(numrows,1))
return x, patient, gesture, trial, nancount