forked from KamitaniLab/GenericObjectDecoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertdata_MatlabResults.py
84 lines (63 loc) · 3.25 KB
/
convertdata_MatlabResults.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
"""
Convert unit feature prediciton results from mat to pkl
This file is a part of GenericDecoding_demo.
"""
import sys
import os
import glob
import pickle
from itertools import product
from time import time
import numpy as np
import h5py
#------------------------------------------------------------------------------#
# Global settings #
#------------------------------------------------------------------------------#
analysis_name = __file__
subject_list = ('Subject1', 'Subject2', 'Subject3', 'Subject4', 'Subject5')
roi_list = ('V1', 'V2', 'V3', 'V4', 'LOC', 'FFA', 'PPA', 'LVC', 'HVC', 'VC')
nvox_list = (500, 500, 500, 500, 500, 500, 500, 1000, 1000, 1000)
nvox_dict = dict(zip(roi_list, nvox_list))
feature_file = 'ImageFeatures.mat'
feature_list = ('cnn1', 'cnn2', 'cnn3', 'cnn4', 'cnn5', 'cnn6', 'cnn7', 'cnn8',
'hmax1', 'hmax2', 'hmax3', 'gist', 'sift')
## Directory to save results and temporally files
result_dir = './results_matlab'
#------------------------------------------------------------------------------#
# Main #
#------------------------------------------------------------------------------#
if __name__ == "__main__":
print "Running " + analysis_name
for sbj, roi, feat in product(subject_list, roi_list, feature_list):
print "Data %s-%s-%s" % (sbj, roi, feat)
result_unit_file = os.path.join(result_dir, sbj, roi, feat + '.pkl')
if os.path.isfile(result_unit_file):
continue
else:
# Load unit result file
print "Loading feature prediction results from mat files"
flist = glob.glob(os.path.join(result_dir, sbj, roi, feat, "unit*.mat"))
flist_unit = [f for f in flist if os.path.split(f)[1] != 'unit_all.mat']
flist_unit.sort()
results_unit = []
for i, f in enumerate(flist_unit):
print 'Loading %s' % f
dat = h5py.File(f)
results_unit.append({'subject' : sbj,
'roi' : roi,
'feature' : feat,
'unit' : i + 1,
'predict_percept' : np.array(dat['predict']['percept']).flatten(),
'predict_imagery' : np.array(dat['predict']['imagery']).flatten(),
'predict_percept_catave' : np.array(dat['predict']['perceptCatAve']).flatten(),
'predict_imagery_catave' : np.array(dat['predict']['imageryCatAve']).flatten(),
'category_test_percept' : np.array(dat['predict']['categoryImagery']).flatten(),
'category_test_imagery' : np.array(dat['predict']['categoryPercept']).flatten()})
# Save unit results
print "Saving %s" % result_unit_file
res_dir, res_file = os.path.split(result_unit_file)
if not os.path.isdir(res_dir):
os.makedirs(res_dir)
with open(result_unit_file, 'wb') as f:
pickle.dump(results_unit, f)
print "Done"