Thanks to visit codestin.com
Credit goes to github.com

Skip to content

2 根据关键字进行不同的处理 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 70 additions & 44 deletions txtToCsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import sys
import getopt

# 抽出したいデータの設備名
DEVICE_CODE = '/dev/input/event4'
# キーワード
KEYWORD_LIST = ['huawei', 'pixcel4a']
# 処理ファイルに項目名のindex
KEY_FROM = 50
KEY_TO = 68
Expand All @@ -22,7 +22,6 @@
VALUE_TO = 79
# 被験者ID
USER_COLUMN_NAME = '被験者番号'
USER_ID = None # 変更なら文字列で
# ミリ秒値の位置
TIMESTAMP_FROM = 1
TIMESTAMP_TO = 16
Expand Down Expand Up @@ -55,63 +54,77 @@
'from': VALUE_FROM,
'to': VALUE_TO,
},
# {
# 'name': '面積(n)',
# 'bind_name': 'ABS_MT_TOUCH_MAJOR',
# 'from': VALUE_FROM,
# 'to': VALUE_TO,
# }
{
'name': '面積(n)',
'bind_name': 'ABS_MT_TOUCH_MAJOR',
'from': VALUE_FROM,
'to': VALUE_TO,
}
]


def get_options(argv):
input_file = ''
output_file = ''
device_code = ''
user_id = ''
try:
opts, args = getopt.getopt(argv, 'hi:o:', ['input_file=', 'output_file='])
except getopt.GetoptError:
print('test.py -i <inputfile> -o <outputfile>')
opts, args = getopt.getopt(argv, 'hi:o:d:u:', ['input_file=', 'output_file=', 'device_code=', 'user_id'])
# パラメータが4個ではない場合、エラーを投げる
if len(opts) != 4:
raise Exception
except Exception:
print('test.py -i <inputfile> -o <outputfile> -d <device_code> -u <user_id>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ('-i', '--input_file'):
if opt in ('-i', '--input_file'):
input_file = arg
elif opt in ('-o', '--output_file'):
output_file = arg
if input_file == '' or output_file == '':
print('test.py -i <inputfile> -o <outputfile>')
elif opt in ('-d', '--device_code'):
device_code = arg
elif opt in ('-u', '--user_id'):
user_id = arg
if input_file == '' or output_file == '' or device_code == '' or user_id == '':
print('test.py -i <inputfile> -o <outputfile> -d <device_code> -u <user_id>')
sys.exit()
return [input_file, output_file]


def update_user_id(user_id):
if user_id == None:
user_id = '1'
elif type(user_id) is int:
user_id = str(user_id)
return user_id
return [input_file, output_file, device_code, user_id]


# 処理ファイルから補充済データ、項目名を取得
def create_data_from_file(file):
def create_data_from_file(file_name, keyword, device_code):
data_json = {'TIME': []}
keys = []
i = 0
with open(file, 'r', encoding='utf-8') as f:

if keyword == '':
print('can not found keyword from file name...')
sys.exit()

with open(file_name, 'r', encoding='utf-8') as f:
for row in f.readlines():
if row.find(DEVICE_CODE) > -1 and row.find('add device') == -1:
data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO]))
if row.find(device_code) > -1 and row.find('add device') == -1:
key = str.strip(row[KEY_FROM:KEY_TO])
# pixcel4aの特別のデータを集めないように
if keyword == 'pixcel4a':
if (key == 'ABS_MT_PRESSURE' and row[VALUE_FROM:VALUE_TO] == '00000000') or (
key == 'ABS_MT_TRACKING_ID' and row[VALUE_FROM:VALUE_TO] == 'ffffffff'):
continue
data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO]))
# 生データに項目名及び順番を取得
if not (key in keys):
keys.append(key)

# 省略データがある場合
while key.find(keys[i]) < 0 and i < len(keys):
# 前回のデータを配列に入れる
data_json[keys[i]].append(data_json[keys[i]][-1])
# UPイベントにx軸、y軸以外のデータを0になる
if (data_json[EVENT_NAME][-1].find(EVENT_VALUE_TO) > -1) and (
not keys[i] in ['ABS_MT_POSITION_X', 'ABS_MT_POSITION_Y']):
data_json[keys[i]].append('0')
else:
data_json[keys[i]].append(data_json[keys[i]][-1])
i += 1

if key.find(keys[i]) > -1:
Expand All @@ -128,11 +141,23 @@ def create_data_from_file(file):

if i == len(keys) and key == EVENT_STOP_FLAG:
i = 0
if len(keys) == 0:
print('device_code:', device_code, 'is missed...')
sys.exit()
return [data_json, keys]


def get_keyword(file_name):
for keyword in KEYWORD_LIST:
if file_name.lower().find(keyword) == -1:
continue
else:
return keyword
return ''


# データ転置
def data_convert(data_json, keys):
def data_convert(data_json, keys, user_id):
# 一行に最大イベント数
max_event_count_in_row = 0
event_count_in_row = 0
Expand All @@ -145,8 +170,8 @@ def data_convert(data_json, keys):
event_count_in_row += 1
for col in COLUMNS:
if col['name'] == USER_COLUMN_NAME and data_json[EVENT_NAME][i].find(EVENT_VALUE_TO) != -1:
row_data.insert(0, USER_ID)
elif col['name'] != USER_COLUMN_NAME:
row_data.insert(0, user_id)
elif col['name'] != USER_COLUMN_NAME and col['bind_name'] in keys:
row_data.append(data_json[col['bind_name']][i])
# イベントがUPの場合
if data_json[EVENT_NAME][i].find(EVENT_VALUE_TO) != -1:
Expand All @@ -165,28 +190,29 @@ def data_convert(data_json, keys):
for col in COLUMNS:
if col['name'] == USER_COLUMN_NAME and j == 0:
header.append(USER_COLUMN_NAME)
elif col['name'] != USER_COLUMN_NAME:
elif col['name'] != USER_COLUMN_NAME and col['bind_name'] in keys:
header.append(col['name'].replace('n', str(j + 1)))
result_header.append(','.join(header) + '\n')
return result_header + result_data


def create_file_from_data(data, file):
def create_file_from_data(data, file_name):
now = datetime.now()
[name, extension] = file.split('.')
file_name = name + now.strftime('%Y%m%d%H%M%S%f') + '.' + extension
with open(file_name, 'w', encoding='utf-8') as f:
# ファイル名に最後の「.」で切り分け
[name, extension] = file_name.rsplit('.', 1)
output_file_name = name + now.strftime('%Y%m%d%H%M%S%f') + '.' + extension
with open(output_file_name, 'w', encoding='utf-8') as f:
f.writelines(data)


if __name__ == '__main__':
# コマンドのオプションを取得
[input_file, output_file] = get_options(sys.argv[1:])
# 被験者番号を更新
USER_ID = update_user_id(USER_ID)
[input_file, output_file, device_code, user_id] = get_options(sys.argv[1:])
# キーワードを取得
keyword = get_keyword(input_file)
# 処理ファイルを読み込み
[data_json, keys] = create_data_from_file(input_file)
[data_json, keys] = create_data_from_file(input_file, keyword, device_code)
# データ処理、転置
convert_data = data_convert(data_json, keys)
convert_data = data_convert(data_json, keys, user_id)
# ファイルを出力
create_file_from_data(convert_data, output_file)