@@ -39,48 +39,76 @@ class Session():
39
39
with the time in milliseconds at which it was printed.
40
40
'''
41
41
42
- def __init__ (self , file_path , int_subject_IDs = True ):
42
+ def __init__ (self , file_path , int_subject_IDs = False ):
43
43
44
- # Load lines from file.
44
+ print ('Importing data file: ' + os .path .split (file_path )[1 ])
45
+ self .file_name = os .path .split (file_path )[1 ]
46
+
47
+ if os .path .splitext (file_path )[1 ] == '.txt' :
45
48
46
- with open ( file_path , 'r' ) as f :
47
- print ( 'Importing data file: ' + os . path . split ( file_path )[ 1 ])
48
- all_lines = [line .strip () for line in f .readlines () if line .strip ()]
49
+ # Load data from txt file.
50
+ with open ( file_path , 'r' ) as f :
51
+ all_lines = [line .strip () for line in f .readlines () if line .strip ()]
49
52
50
- # Extract and store session information.
53
+ # Extract and store session information.
51
54
52
- self . file_name = os . path . split ( file_path )[ 1 ]
55
+ info_lines = [ line [ 2 :] for line in all_lines if line [ 0 ] == 'I' ]
53
56
54
- info_lines = [line [2 :] for line in all_lines if line [0 ]== 'I' ]
57
+ self .experiment_name = next (line for line in info_lines if 'Experiment name' in line ).split (' : ' )[1 ]
58
+ self .task_name = next (line for line in info_lines if 'Task name' in line ).split (' : ' )[1 ]
59
+ subject_ID_string = next (line for line in info_lines if 'Subject ID' in line ).split (' : ' )[1 ]
60
+ datetime_string = next (line for line in info_lines if 'Start date' in line ).split (' : ' )[1 ]
55
61
56
- self .experiment_name = next (line for line in info_lines if 'Experiment name' in line ).split (' : ' )[1 ]
57
- self .task_name = next (line for line in info_lines if 'Task name' in line ).split (' : ' )[1 ]
58
- subject_ID_string = next (line for line in info_lines if 'Subject ID' in line ).split (' : ' )[1 ]
59
- datetime_string = next (line for line in info_lines if 'Start date' in line ).split (' : ' )[1 ]
62
+ self .datetime = datetime .strptime (datetime_string , '%Y/%m/%d %H:%M:%S' )
60
63
61
- if int_subject_IDs : # Convert subject ID string to integer.
62
- self .subject_ID = int ('' .join ([i for i in subject_ID_string if i .isdigit ()]))
63
- else :
64
- self .subject_ID = subject_ID_string
64
+ # Extract and store session data.
65
65
66
- self .datetime = datetime .strptime (datetime_string , '%Y/%m/%d %H:%M:%S' )
67
- self .datetime_string = self .datetime .strftime ('%Y-%m-%d %H:%M:%S' )
66
+ state_IDs = eval (next (line for line in all_lines if line [0 ]== 'S' )[2 :])
67
+ event_IDs = eval (next (line for line in all_lines if line [0 ]== 'E' )[2 :])
68
+
69
+ ID2name = {v : k for k , v in {** state_IDs , ** event_IDs }.items ()}
70
+
71
+ data_lines = [line [2 :].split (' ' ) for line in all_lines if line [0 ]== 'D' ]
72
+
73
+ self .events = [Event (int (dl [0 ]), ID2name [int (dl [1 ])]) for dl in data_lines ]
74
+
75
+ self .times = {event_name : np .array ([ev .time for ev in self .events if ev .name == event_name ])
76
+ for event_name in ID2name .values ()}
68
77
69
- # Extract and store session data.
78
+ self . print_lines = [ line [ 2 :] for line in all_lines if line [ 0 ] == 'P' ]
70
79
71
- state_IDs = eval (next (line for line in all_lines if line [0 ]== 'S' )[2 :])
72
- event_IDs = eval (next (line for line in all_lines if line [0 ]== 'E' )[2 :])
80
+ elif os .path .splitext (file_path )[1 ] == '.tsv' :
73
81
74
- ID2name = { v : k for k , v in { ** state_IDs , ** event_IDs }. items ()}
82
+ # Load tsv file to pandas dataframe.
75
83
76
- data_lines = [ line [ 2 :]. split ( ' ' ) for line in all_lines if line [ 0 ] == 'D' ]
84
+ df = pd . read_csv ( file_path , delimiter = ' \t ' )
77
85
78
- self . events = [ Event ( int ( dl [ 0 ]), ID2name [ int ( dl [ 1 ])]) for dl in data_lines ]
86
+ # Extract and store session information.
79
87
80
- self .times = {event_name : np .array ([ev .time for ev in self .events if ev .name == event_name ])
81
- for event_name in ID2name .values ()}
88
+ self .experiment_name = df .loc [(df ["type" ]== "info" ) & (df ["name" ]== "experiment_name" ), "value" ].item ()
89
+ self .task_name = df .loc [(df ["type" ]== "info" ) & (df ["name" ]== "task_name" ), "value" ].item ()
90
+ subject_ID_string = df .loc [(df ["type" ]== "info" ) & (df ["name" ]== "subject_id" ), "value" ].item ()
91
+ datetime_string = df .loc [(df ["type" ]== "info" ) & (df ["name" ]== "start_time" ), "value" ].item ()
92
+
93
+ self .datetime = datetime .fromisoformat (datetime_string )
94
+
95
+ # Extract and store session data.
96
+
97
+ self .events = [Event (int (row ['time' ]* 1000 ), row ['name' ]) for i ,row
98
+ in df [df ['type' ].isin (['state' , 'event' ])].iterrows ()]
99
+
100
+ self .times = {event_name : np .array ([ev .time for ev in self .events if ev .name == event_name ])
101
+ for event_name in df .loc [df ['type' ].isin (['state' , 'event' ]), 'name' ].unique ()}
102
+
103
+ # Common to both filetypes.
104
+
105
+ if int_subject_IDs : # Convert subject ID string to integer.
106
+ self .subject_ID = int ('' .join ([i for i in subject_ID_string if i .isdigit ()]))
107
+ else :
108
+ self .subject_ID = subject_ID_string
109
+
110
+ self .datetime_string = self .datetime .strftime ('%Y-%m-%d %H:%M:%S' )
82
111
83
- self .print_lines = [line [2 :] for line in all_lines if line [0 ]== 'P' ]
84
112
85
113
#----------------------------------------------------------------------------------
86
114
# Experiment class
0 commit comments