def test_prepare_fcma_data(): raw_data, _, labels = io.prepare_fcma_data(dir, extension, epoch_file, mask_file) expected_raw_data = np.load(os.path.join(os.path.dirname(__file__), "data/expected_raw_data.npy")) assert len(raw_data) == len(expected_raw_data), "numbers of epochs do not match in test_prepare_fcma_data" for idx in range(len(raw_data)): assert np.allclose(raw_data[idx], expected_raw_data[idx]), "raw data do not match in test_prepare_fcma_data" assert np.array_equal(labels, expected_labels), "the labels do not match in test_prepare_fcma_data"
def test_prepare_fcma_data(): raw_data, labels = io.prepare_fcma_data(dir, extension, mask_file, epoch_file) expected_raw_data = np.load( os.path.join(os.path.dirname(__file__), 'data/expected_raw_data.npy')) assert len(raw_data) == len(expected_raw_data), \ 'numbers of epochs do not match in test_prepare_fcma_data' for idx in range(len(raw_data)): assert np.allclose(raw_data[idx], expected_raw_data[idx]), \ 'raw data do not match in test_prepare_fcma_data' assert np.array_equal(labels, expected_labels), \ 'the labels do not match in test_prepare_fcma_data'
logger = logging.getLogger(__name__) """ example running command in run_voxel_selection.sh """ if __name__ == '__main__': if MPI.COMM_WORLD.Get_rank()==0: logger.info( 'programming starts in %d process(es)' % MPI.COMM_WORLD.Get_size() ) data_dir = sys.argv[1] extension = sys.argv[2] mask_file = sys.argv[3] epoch_file = sys.argv[4] raw_data, labels = prepare_fcma_data(data_dir, extension, mask_file, epoch_file) epochs_per_subj = int(sys.argv[5]) num_subjs = int(sys.argv[6]) # the following line is an example to leaving a subject out #vs = VoxelSelector(raw_data[0:204], epochs_per_subj, labels[0:204], num_subjs-1) # if using all subjects vs = VoxelSelector(raw_data, epochs_per_subj, labels, num_subjs) # for cross validation, use SVM with precomputed kernel clf = svm.SVC(kernel='precomputed', shrinking=False, C=10) results = vs.run(clf) # this output is just for result checking if MPI.COMM_WORLD.Get_rank()==0: logger.info( 'correlation-based voxel selection is done' ) #print(results[0:100])
#from sklearn.externals import joblib format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' # if want to output log to a file instead of outputting log to the console, # replace "stream=sys.stdout" with "filename='fcma.log'" logging.basicConfig(level=logging.INFO, format=format, stream=sys.stdout) logger = logging.getLogger(__name__) # python classification.py face_scene bet.nii.gz face_scene/prefrontal_top_mask.nii.gz face_scene/fs_epoch_labels.npy 12 if __name__ == '__main__': data_dir = sys.argv[1] extension = sys.argv[2] mask_file = sys.argv[3] epoch_file = sys.argv[4] raw_data, labels = prepare_fcma_data(data_dir, extension, mask_file, epoch_file) epochs_per_subj = int(sys.argv[5]) # no shrinking, set C=1 use_clf = svm.SVC(kernel='precomputed', shrinking=False, C=1) #use_clf = LogisticRegression() clf = Classifier(use_clf, epochs_per_subj=epochs_per_subj) training_data = raw_data[0:204] test_data = raw_data[204:] clf.fit(training_data, labels[0:204]) # joblib can be used for saving and loading models #joblib.dump(clf, 'model/logistic.pkl') #clf = joblib.load('model/svm.pkl') print(clf.predict(test_data)) print(clf.decision_function(test_data)) print(np.asanyarray(labels[204:]))