1919from pathlib import Path
2020import simplejson as json
2121from time import sleep , time
22+ import scipy .io as sio
2223
2324from .. import logging , config , __version__ as version
2425from .misc import is_container
@@ -932,3 +933,51 @@ def indirectory(path):
932933 yield
933934 finally :
934935 os .chdir (cwd )
936+
937+ def load_spm_mat (spm_mat_file , ** kwargs ):
938+ try :
939+ mat = sio .loadmat (spm_mat_file , ** kwargs )
940+ except NotImplementedError :
941+ import h5py
942+ import numpy as np
943+ mat = dict (SPM = np .array ([[sio .matlab .mat_struct ()]]))
944+
945+ # Get Vbeta, Vcon, and Vspm file names
946+ with h5py .File (spm_mat_file , "r" ) as h5file :
947+ fnames = dict ()
948+ try :
949+ fnames ["Vbeta" ] = [u"" .join (chr (c [0 ]) for c in h5file [obj_ref [0 ]]) for obj_ref in h5file ["SPM" ]["Vbeta" ]["fname" ]]
950+ except Exception :
951+ fnames ["Vbeta" ] = []
952+ for contr_type in ["Vcon" , "Vspm" ]:
953+ try :
954+ fnames [contr_type ] = [u"" .join (chr (c [0 ]) for c in h5file [obj_ref [0 ]]["fname" ]) for obj_ref in h5file ["SPM" ]["xCon" ][contr_type ]]
955+ except Exception :
956+ fnames [contr_type ] = []
957+
958+ # Structure Vbeta as returned by scipy.io.loadmat
959+ obj_list = []
960+ for i in range (len (fnames ["Vbeta" ])):
961+ obj = sio .matlab .mat_struct ()
962+ setattr (obj , "fname" , np .array ([fnames ["Vbeta" ][i ]]))
963+ obj_list .append (obj )
964+ if len (obj_list ) > 0 :
965+ setattr (mat ["SPM" ][0 , 0 ], "Vbeta" , np .array ([obj_list ]))
966+ else :
967+ setattr (mat ["SPM" ][0 , 0 ], "Vbeta" , np .empty ((0 , 0 ), dtype = object ))
968+
969+ # Structure Vcon and Vspm as returned by scipy.io.loadmat
970+ obj_list = []
971+ for i in range (len (fnames ["Vcon" ])):
972+ obj = sio .matlab .mat_struct ()
973+ for contr_type in ["Vcon" , "Vspm" ]:
974+ temp = sio .matlab .mat_struct ()
975+ setattr (temp , "fname" , np .array ([fnames [contr_type ][i ]]))
976+ setattr (obj , contr_type , np .array ([[temp ]]))
977+ obj_list .append (obj )
978+ if len (obj_list ) > 0 :
979+ setattr (mat ["SPM" ][0 , 0 ], "xCon" , np .array ([obj_list ]))
980+ else :
981+ setattr (mat ["SPM" ][0 , 0 ], "xCon" , np .empty ((0 , 0 ), dtype = object ))
982+
983+ return mat
0 commit comments