-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_eprime_file.py
139 lines (117 loc) · 4.7 KB
/
parse_eprime_file.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
"""
Functions to extract informations from e-prime files
@author: Mehdi Rahim
"""
import os, glob
import numpy as np
import pandas as pd
BASE_DIR = os.path.join('/', 'shfj', 'Ppsypim', 'CAIMAN',
'Stats', 'eprime', 'eprime_files')
DST_BASE_DIR = os.path.join('eprime_files_caiman', 'csv')
# File id and date Parser
def parse_and_correct_file_id_eprime(filename):
""" returns the file_id and the corrected date from the filename
"""
file_id = filename.split('-')[1].strip()
c_date = ''
if len(file_id) == 5:
c_date = file_id[2:4] + '-' + file_id[0:2]
if file_id[4] < '9':
c_date += '-201' + file_id[4]
else:
c_date += '-200' + file_id[4]
return file_id, c_date
# Quick and Dirty Parser
def parse_data_eprime(filename):
""" returns a dict of header informations and a DataFrame of values
at the 3rd level of filename
"""
edf = pd.DataFrame() # Subject dataframe
lvl = {} # E-Prime level 3 dict
hdr = {} # Header dict
level_flag = -1 # Flag on the current header/level
with open(filename, 'rU') as f:
lines = [x.strip() for x in f.read().split('\n')]
for line in lines:
if line in ["*** Header Start ***", "*** Header End ***"]:
# set the flag on header section
level_flag = 0
continue
if line == "*** LogFrame Start ***":
# reset the level 3 dict
lvl = {}
continue
if line == "*** LogFrame End ***":
# append dict according to the level
if lvl:
edf = edf.append(lvl, ignore_index=True)
level_flag = -1
continue
fields = line.split(": ")
fields[0] = fields[0].replace(':', '')
fields[0] = fields[0].replace(' ', '')
if fields[0] == "Level":
level_flag = int(fields[1])
continue
if level_flag == 3:
lvl[fields[0]] = ''
if len(fields) == 2:
lvl[fields[0]] = fields[1]
elif level_flag in [0, 1, 2]:
hdr[fields[0]] = fields[1]
return edf, hdr
##############################################################################
""" Parsing all the subjects and saving :
- a session csv per subject
- a whole subject header csv
"""
header_selected_cols = ['c_Subject', 'Subject', 'c_SessionDate',
'SessionDate', 'SessionTime', 'nbTrials', 'PP.Onset']
eprime_selected_cols = ['TrialList',
'PicturePrime.OnsetTime',
'PictureTarget.OnsetTime',
'PictureTarget.RTTime',
'Target_time',
'Fix_time',
'Ant_time',
'J_time',
'PictureTarget.ACC',
'PictureTarget.CRESP',
'PictureTarget.RESP',
'prize',
'SumPrize',
'CorrectAnswer',
'PictureTarget.OnsetDelay',
'PictureTarget.RT',
'TargetPosition',
'PicturePrime.OnsetDelay']
# header will contain "meta-data" of all the subjects
header = pd.DataFrame()
# list of the eprime files
file_list = glob.glob(os.path.join(BASE_DIR, '*.txt'))
for fn in file_list:
h, fname = os.path.split(fn)
print fname
# Parse data (df) and header informations (hd)
df, hd = parse_data_eprime(fn)
# Add informations about the first onset (if available)
hd['PP.Onset'] = ''
if 'PicturePrime.OnsetTime' in df.keys():
hd['PP.Onset'] = df['PicturePrime.OnsetTime'][0]
# Add informations about the corrected subject id
hd['c_Subject'], hd['c_SessionDate'] = parse_and_correct_file_id_eprime(fn)
# Number of trials in order to check the experimentation integrity
hd['nbTrials'] = np.str(df['TrialList'].count())
# Append each subject
header = header.append(hd, ignore_index=True)
# Save the raw exprimentation data of the current subject
df.to_csv(os.path.join(DST_BASE_DIR, fname + '.csv'), sep=',')
# Save the selected data if the current subject
if hd['nbTrials'] == '66':
df.to_csv(os.path.join(DST_BASE_DIR, 'c_' + fname + '.csv'),
sep=',', columns=eprime_selected_cols)
# Save all subjects meta-data
header.to_csv(os.path.join(DST_BASE_DIR, 'all_subjects.csv'), sep=',')
header.to_csv(os.path.join(DST_BASE_DIR, 'all_subjects_c.csv'), sep=',',
columns=header_selected_cols)