-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_preprocessing.py
More file actions
115 lines (77 loc) · 3.39 KB
/
Copy pathkey_preprocessing.py
File metadata and controls
115 lines (77 loc) · 3.39 KB
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
107
108
109
110
111
112
113
114
115
import numpy as np
import sys, argparse
# This script is written for prprocessing input logs of keyboard.
# In our logging system, all the pressed keys are collected as a string at every frame (e.g. 'a/s/d').
# If you want to analyze a specific button inputs, you should extract specific inputs to analyze and convert it to integer (1 or 0).
# This script is an example for extracting the button of interest. (e.g. 'a/s/d' == extracting 'a' ==> 1 )
parser = argparse.ArgumentParser()
parser.add_argument('--task', required=True, type=str, help='data to be analyze')
def get_key_down(inputs:np.array):
'''extract timings when a button gets pressed
returnsdd np.array of boolean
'''
prev = inputs[:-1]
cur = inputs[1:]
down = np.full(len(inputs), False)
down[1:] = (prev == False) & (cur == True)
return down
def get_key_up(inputs:np.array):
'''extract timings when a button gets released
'''
prev = inputs[:-1]
cur = inputs[1:]
up = np.full(len(inputs), False)
up[1:] = (prev == True) & (cur == False)
return up
def extract_key_pressed(key_inputs:np.array, key:str):
'''parse string(pressed inputs) to array
'''
return np.asarray([(key in key_input.split('/')) for key_input in key_inputs])
def execute_dino_preprocessing():
''' extract 'space' input signals
'''
PID = ["".join(['S', str(200+i)]) for i in range(1, 21)]
for pid in PID:
data = np.loadtxt('rawdata/dino/{}.csv'.format(pid), delimiter=',', dtype=str)
key_inputs = data[:,3]
target_key = extract_key_pressed(key_inputs, 'space')
target_key_down = get_key_down(target_key)
new_key_inputs = np.zeros(len(key_inputs))
new_key_inputs[target_key_down] = 1
data[:,3] = new_key_inputs
np.savetxt("rawdata/dino/{}_space.csv".format(pid), data, fmt='%s', delimiter=',')
def execute_tabsonic_preprocessing():
'''extract 's', 'd', 'f', 'j', 'k', and 'l' input signals.
each key is encoded in binary
'''
PID = ["".join(['S', str(200+i)]) for i in range(1, 21)]
MUSIC = ['A', 'B', 'C', 'D', 'E']
SOUND = ['O', 'X']
KEY = ['s', 'd', 'f', 'j', 'k', 'l']
for pid in PID:
for music in MUSIC:
for sound in SOUND:
data = np.loadtxt('rawdata/tabsonic/{}_{}{}.csv'.format(pid, music, sound), delimiter=',', dtype=str)
key_inputs = data[:,3] #get raw data
new_key_inputs = np.zeros(len(key_inputs)) # create an empty column
for i, k in enumerate(KEY):
# 1. for each key, extract timing at which the target key is down
# 2. update 'new key inputs' after encoding
target_key = extract_key_pressed(key_inputs, k)
target_key_down = get_key_down(target_key)
new_key_inputs[target_key_down] += 2**i
# save data
data[:,3] = new_key_inputs
np.savetxt('rawdata/{}_{}{}_all.csv'.format(pid, music, sound), data, fmt='%s', delimiter=',')
def execute_custom_preprocessing():
'''write your own if needed
'''
pass
if __name__ == "__main__":
args = parser.parse_args()
if args.task == 'dino':
execute_dino_preprocessing()
elif args.task == 'tabsonic':
execute_tabsonic_preprocessing()
else:
execute_custom_preprocessing()