-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomize_json.py
72 lines (49 loc) · 1.79 KB
/
customize_json.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This script is used to customize the json files created from generate_config_files.py,
following Matthieu's instruction. This customization is to ease the reading of the
config files by the listening test software.
"""
import pandas as pd
import glob
import os
import json
#%%
"""
Add end_experiment entry to each trial.
This is not necessary anymore, can be removed.
"""
input_path = 'config_files/json'
output_path = 'config_files/modified_json2'
if not os.path.isdir(output_path):
os.makedirs(output_path)
file_list = glob.glob(os.path.join(input_path, '*.json'))
for file in file_list:
basename = os.path.basename(file)
df = pd.read_json(file)
df['end_experiment'] = ['false']*len(df)
# row = [df.iloc[-1]['subset'], df.iloc[-1]['session'], 'null', 'null', 'true']
# df.loc[len(df)] = row
with open(os.path.join(output_path, basename), 'w') as f:
json.dump(df.to_dict('records'), f, indent=1)
#%%
"""Add a variable with the id of the subject at the beginning of the json.
This was asked by Matthieu and is used to read the file in javascript.
"""
input_path = 'config_files/modified_json2'
output_path = 'config_files/modified_json2'
file_list = glob.glob(os.path.join(input_path, '*.json'))
if not os.path.isdir(output_path):
os.mkdir(output_path)
for file in file_list:
basename = os.path.basename(file)
with open(file, 'r') as f:
lines = f.readlines()
# this is necessary
lines[0] = 'all_trials' + ' = ' + lines[0]
# to match Matthieu's manual modification, just to check, this can be removed
lines[-2] = ' },\n'
new_file = os.path.join(output_path, basename)
with open(new_file, 'w') as f:
f.writelines(lines)