4
4
import sys
5
5
import getopt
6
6
7
- # 抽出したいデータの設備名
8
- DEVICE_CODE = '/dev/input/event4'
7
+ # キーワード
8
+ KEYWORD_LIST = [ 'huawei' , 'pixcel4a' ]
9
9
# 処理ファイルに項目名のindex
10
10
KEY_FROM = 50
11
11
KEY_TO = 68
22
22
VALUE_TO = 79
23
23
# 被験者ID
24
24
USER_COLUMN_NAME = '被験者番号'
25
- USER_ID = None # 変更なら文字列で
26
25
# ミリ秒値の位置
27
26
TIMESTAMP_FROM = 1
28
27
TIMESTAMP_TO = 16
55
54
'from' : VALUE_FROM ,
56
55
'to' : VALUE_TO ,
57
56
},
58
- # {
59
- # 'name': '面積(n)',
60
- # 'bind_name': 'ABS_MT_TOUCH_MAJOR',
61
- # 'from': VALUE_FROM,
62
- # 'to': VALUE_TO,
63
- # }
57
+ {
58
+ 'name' : '面積(n)' ,
59
+ 'bind_name' : 'ABS_MT_TOUCH_MAJOR' ,
60
+ 'from' : VALUE_FROM ,
61
+ 'to' : VALUE_TO ,
62
+ }
64
63
]
65
64
66
65
67
66
def get_options (argv ):
68
67
input_file = ''
69
68
output_file = ''
69
+ device_code = ''
70
+ user_id = ''
70
71
try :
71
- opts , args = getopt .getopt (argv , 'hi:o:' , ['input_file=' , 'output_file=' ])
72
- except getopt .GetoptError :
73
- print ('test.py -i <inputfile> -o <outputfile>' )
72
+ opts , args = getopt .getopt (argv , 'hi:o:d:u:' , ['input_file=' , 'output_file=' , 'device_code=' , 'user_id' ])
73
+ # パラメータが4個ではない場合、エラーを投げる
74
+ if len (opts ) != 4 :
75
+ raise Exception
76
+ except Exception :
77
+ print ('test.py -i <inputfile> -o <outputfile> -d <device_code> -u <user_id>' )
74
78
sys .exit (2 )
75
79
for opt , arg in opts :
76
- if opt == '-h' :
77
- print ('test.py -i <inputfile> -o <outputfile>' )
78
- sys .exit ()
79
- elif opt in ('-i' , '--input_file' ):
80
+ if opt in ('-i' , '--input_file' ):
80
81
input_file = arg
81
82
elif opt in ('-o' , '--output_file' ):
82
83
output_file = arg
83
- if input_file == '' or output_file == '' :
84
- print ('test.py -i <inputfile> -o <outputfile>' )
84
+ elif opt in ('-d' , '--device_code' ):
85
+ device_code = arg
86
+ elif opt in ('-u' , '--user_id' ):
87
+ user_id = arg
88
+ if input_file == '' or output_file == '' or device_code == '' or user_id == '' :
89
+ print ('test.py -i <inputfile> -o <outputfile> -d <device_code> -u <user_id>' )
85
90
sys .exit ()
86
- return [input_file , output_file ]
87
-
88
91
89
- def update_user_id (user_id ):
90
- if user_id == None :
91
- user_id = '1'
92
- elif type (user_id ) is int :
93
- user_id = str (user_id )
94
- return user_id
92
+ return [input_file , output_file , device_code , user_id ]
95
93
96
94
97
95
# 処理ファイルから補充済データ、項目名を取得
98
- def create_data_from_file (file ):
96
+ def create_data_from_file (file_name , keyword , device_code ):
99
97
data_json = {'TIME' : []}
100
98
keys = []
101
99
i = 0
102
- with open (file , 'r' , encoding = 'utf-8' ) as f :
100
+
101
+ if keyword == '' :
102
+ print ('can not found keyword from file name...' )
103
+ sys .exit ()
104
+
105
+ with open (file_name , 'r' , encoding = 'utf-8' ) as f :
103
106
for row in f .readlines ():
104
- if row .find (DEVICE_CODE ) > - 1 and row .find ('add device' ) == - 1 :
105
- data_json ['TIME' ].append (str .strip (row [TIMESTAMP_FROM :TIMESTAMP_TO ]))
107
+ if row .find (device_code ) > - 1 and row .find ('add device' ) == - 1 :
106
108
key = str .strip (row [KEY_FROM :KEY_TO ])
109
+ # pixcel4aの特別のデータを集めないように
110
+ if keyword == 'pixcel4a' :
111
+ if (key == 'ABS_MT_PRESSURE' and row [VALUE_FROM :VALUE_TO ] == '00000000' ) or (
112
+ key == 'ABS_MT_TRACKING_ID' and row [VALUE_FROM :VALUE_TO ] == 'ffffffff' ):
113
+ continue
114
+ data_json ['TIME' ].append (str .strip (row [TIMESTAMP_FROM :TIMESTAMP_TO ]))
107
115
# 生データに項目名及び順番を取得
108
116
if not (key in keys ):
109
117
keys .append (key )
110
118
111
119
# 省略データがある場合
112
120
while key .find (keys [i ]) < 0 and i < len (keys ):
113
121
# 前回のデータを配列に入れる
114
- data_json [keys [i ]].append (data_json [keys [i ]][- 1 ])
122
+ # UPイベントにx軸、y軸以外のデータを0になる
123
+ if (data_json [EVENT_NAME ][- 1 ].find (EVENT_VALUE_TO ) > - 1 ) and (
124
+ not keys [i ] in ['ABS_MT_POSITION_X' , 'ABS_MT_POSITION_Y' ]):
125
+ data_json [keys [i ]].append ('0' )
126
+ else :
127
+ data_json [keys [i ]].append (data_json [keys [i ]][- 1 ])
115
128
i += 1
116
129
117
130
if key .find (keys [i ]) > - 1 :
@@ -128,11 +141,23 @@ def create_data_from_file(file):
128
141
129
142
if i == len (keys ) and key == EVENT_STOP_FLAG :
130
143
i = 0
144
+ if len (keys ) == 0 :
145
+ print ('device_code:' , device_code , 'is missed...' )
146
+ sys .exit ()
131
147
return [data_json , keys ]
132
148
133
149
150
+ def get_keyword (file_name ):
151
+ for keyword in KEYWORD_LIST :
152
+ if file_name .lower ().find (keyword ) == - 1 :
153
+ continue
154
+ else :
155
+ return keyword
156
+ return ''
157
+
158
+
134
159
# データ転置
135
- def data_convert (data_json , keys ):
160
+ def data_convert (data_json , keys , user_id ):
136
161
# 一行に最大イベント数
137
162
max_event_count_in_row = 0
138
163
event_count_in_row = 0
@@ -145,8 +170,8 @@ def data_convert(data_json, keys):
145
170
event_count_in_row += 1
146
171
for col in COLUMNS :
147
172
if col ['name' ] == USER_COLUMN_NAME and data_json [EVENT_NAME ][i ].find (EVENT_VALUE_TO ) != - 1 :
148
- row_data .insert (0 , USER_ID )
149
- elif col ['name' ] != USER_COLUMN_NAME :
173
+ row_data .insert (0 , user_id )
174
+ elif col ['name' ] != USER_COLUMN_NAME and col [ 'bind_name' ] in keys :
150
175
row_data .append (data_json [col ['bind_name' ]][i ])
151
176
# イベントがUPの場合
152
177
if data_json [EVENT_NAME ][i ].find (EVENT_VALUE_TO ) != - 1 :
@@ -165,28 +190,29 @@ def data_convert(data_json, keys):
165
190
for col in COLUMNS :
166
191
if col ['name' ] == USER_COLUMN_NAME and j == 0 :
167
192
header .append (USER_COLUMN_NAME )
168
- elif col ['name' ] != USER_COLUMN_NAME :
193
+ elif col ['name' ] != USER_COLUMN_NAME and col [ 'bind_name' ] in keys :
169
194
header .append (col ['name' ].replace ('n' , str (j + 1 )))
170
195
result_header .append (',' .join (header ) + '\n ' )
171
196
return result_header + result_data
172
197
173
198
174
- def create_file_from_data (data , file ):
199
+ def create_file_from_data (data , file_name ):
175
200
now = datetime .now ()
176
- [name , extension ] = file .split ('.' )
177
- file_name = name + now .strftime ('%Y%m%d%H%M%S%f' ) + '.' + extension
178
- with open (file_name , 'w' , encoding = 'utf-8' ) as f :
201
+ # ファイル名に最後の「.」で切り分け
202
+ [name , extension ] = file_name .rsplit ('.' , 1 )
203
+ output_file_name = name + now .strftime ('%Y%m%d%H%M%S%f' ) + '.' + extension
204
+ with open (output_file_name , 'w' , encoding = 'utf-8' ) as f :
179
205
f .writelines (data )
180
206
181
207
182
208
if __name__ == '__main__' :
183
209
# コマンドのオプションを取得
184
- [input_file , output_file ] = get_options (sys .argv [1 :])
185
- # 被験者番号を更新
186
- USER_ID = update_user_id ( USER_ID )
210
+ [input_file , output_file , device_code , user_id ] = get_options (sys .argv [1 :])
211
+ # キーワードを取得
212
+ keyword = get_keyword ( input_file )
187
213
# 処理ファイルを読み込み
188
- [data_json , keys ] = create_data_from_file (input_file )
214
+ [data_json , keys ] = create_data_from_file (input_file , keyword , device_code )
189
215
# データ処理、転置
190
- convert_data = data_convert (data_json , keys )
216
+ convert_data = data_convert (data_json , keys , user_id )
191
217
# ファイルを出力
192
218
create_file_from_data (convert_data , output_file )
0 commit comments