代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
import os
import json
def parse_test_days(direc_prefix, total_days):
'''
Description:
generate a dictionary that stores the configurations of each day's collected data
by parsing readme.txt
Input:
direc_prefix (str):
folder directory where all the data is stored
total_days (int):
total number of experimental days
Output:
return day_conf
day_conf (dict):
key (str) -- 'day'+str(index) (index starts from 1 to total_days)
value (dict)-- test_conf (dictionary)
test_conf (dict):
key (str) -- "location"
value (str) -- where the experiment was conducted
key (str) -- 'motion'
value (int) -- total number of motion tests conducted
key (str) -- 'empty'
value (int) -- total number of tests conducted when there is nobody inside the lab
key (str) -- 'mixed'
value (int) -- total number of mixed runs
key (str) -- 'mixed_truth'
value (list) -- each entry of the list is also a list that
contains the ground truth of this mixed run.
'''
day_conf = {}
# mapping data to label
label = {'empty': 0, 'motion': 1}
for i in range(1, total_days + 1, 1):
day_index = 'day' + str(i)
d_path = direc_prefix + day_index + '/'
with open(d_path + 'readme.txt', 'r') as f:
print('processing day {}'.format(i))
location, empty_cnt, motion_cnt, mixed_cnt, mixed_state = None, -1, -1, 0, []
for l in f:
m = l.split()
if len(m) == 0:
continue
if 'Location' in m[0]:
location = m[-1]
if 'motion' in m[0]:
motion_cnt = int(m[-1])
if 'empty' in m[0]:
empty_cnt = int(m[-1])
if 'mixed' in m[0]:
mixed_cnt += 1
idx = int(m[0][-2])
mixed_index = 'mixed' + str(idx)
status = m[1:]
mixed_state.append([])
for s in status:
if 'empty' in s:
mixed_state[-1].append(label['empty'])
elif 'motion' in s:
mixed_state[-1].append(label['motion'])
else:
print('undefined status in {}'.format(m[0]))
if location == None or empty_cnt == -1 or motion_cnt == -1:
raise Exception('invalid info {} {} {}'.format(location, empty_cnt, motion_cnt))
day_conf[day_index] = {'location': location, 'motion': motion_cnt,
'empty': empty_cnt, 'mixed': mixed_cnt, 'mixed_truth': mixed_state}
print(day_conf[day_index])
print('\n')
for k, v in day_conf[day_index].items():
if k == 'location' or k == 'mixed_truth':
continue
for j in range(1, v + 1, 1):
f_name = d_path + k + str(j) + '.data'
if not os.path.exists(f_name):
print("{} doesn't exist".format(f_name))
return day_conf
def main():
total_days = 16
data_folder = '/root/share/upload_wifi_data/'
day_conf = parse_test_days(data_folder, total_days)
to_json = json.dumps(day_conf)
# json filename
save_json_filename = 'day_conf.json'
# save day_conf to json file
with open(save_json_filename, 'w') as f:
f.write(to_json)
print('json file was saved as ' + save_json_filename)
if __name__ == "__main__":
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。