def test_voxel_selection_with_two_masks(): fake_raw_data1 = [create_epoch() for i in range(8)] fake_raw_data2 = [create_epoch() for i in range(8)] labels = [0, 1, 0, 1, 0, 1, 0, 1] # 2 subjects, 4 epochs per subject vs = VoxelSelector(labels, 4, 2, fake_raw_data1, raw_data2=fake_raw_data2, voxel_unit=1) # for cross validation, use SVM with precomputed kernel # no shrinking, set C=1 clf = svm.SVC(kernel='precomputed', shrinking=False, C=1, gamma='auto') results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8 * tuple[1]) expected_output = [3, 3, 3, 6, 6] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via SVM does not provide correct results' # for cross validation, use logistic regression clf = LogisticRegression() results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8 * tuple[1]) expected_output = [3, 4, 4, 6, 6] assert np.allclose(output, expected_output, atol=1), ( "voxel selection via logistic regression does not provide correct " "results")
def test_voxel_selection_with_two_masks(): fake_raw_data1 = [create_epoch() for i in range(8)] fake_raw_data2 = [create_epoch() for i in range(8)] labels = [0, 1, 0, 1, 0, 1, 0, 1] # 2 subjects, 4 epochs per subject vs = VoxelSelector(labels, 4, 2, fake_raw_data1, raw_data2=fake_raw_data2, voxel_unit=1) # for cross validation, use SVM with precomputed kernel # no shrinking, set C=1 clf = svm.SVC(kernel='precomputed', shrinking=False, C=1) results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [3, 3, 3, 6, 6] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via SVM does not provide correct results' # for cross validation, use logistic regression clf = LogisticRegression() results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [3, 4, 4, 6, 6] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via logistic regression does not provide correct results'
def test_voxel_selection(): fake_raw_data = [create_epoch() for i in range(8)] labels = [0, 1, 0, 1, 0, 1, 0, 1] # 2 subjects, 4 epochs per subject vs = VoxelSelector(labels, 4, 2, fake_raw_data, voxel_unit=1) # test scipy normalization fake_corr = prng.rand(1, 4, 5).astype(np.float32) fake_corr = vs._correlation_normalization(fake_corr) if MPI.COMM_WORLD.Get_rank() == 0: expected_fake_corr = [[[1.06988919, 0.51641309, -0.46790636, -1.31926763, 0.2270218], [-1.22142744, -1.39881694, -1.2979387, 1.05702305, -0.6525566], [0.89795232, 1.27406132, 0.36460185, 0.87538344, 1.5227468], [-0.74641371, -0.39165771, 1.40124381, -0.61313909, -1.0972116]]] assert np.allclose(fake_corr, expected_fake_corr), \ 'within-subject normalization does not provide correct results' # for cross validation, use SVM with precomputed kernel # no shrinking, set C=1 clf = svm.SVC(kernel='precomputed', shrinking=False, C=1) results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [7, 4, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via SVM does not provide correct results' # for cross validation, use logistic regression clf = LogisticRegression() results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [6, 3, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), ( "voxel selection via logistic regression does not provide correct " "results")
def test_voxel_selection(): fake_raw_data = [create_epoch() for i in range(8)] labels = [0, 1, 0, 1, 0, 1, 0, 1] # 2 subjects, 4 epochs per subject vs = VoxelSelector(labels, 4, 2, fake_raw_data, voxel_unit=1) # test scipy normalization fake_corr = prng.rand(1, 4, 5).astype(np.float32) fake_corr = vs._correlation_normalization(fake_corr) if MPI.COMM_WORLD.Get_rank() == 0: expected_fake_corr = [[[1.06988919, 0.51641309, -0.46790636, -1.31926763, 0.2270218], [-1.22142744, -1.39881694, -1.2979387, 1.05702305, -0.6525566], [0.89795232, 1.27406132, 0.36460185, 0.87538344, 1.5227468], [-0.74641371, -0.39165771, 1.40124381, -0.61313909, -1.0972116]]] assert np.allclose(fake_corr, expected_fake_corr), \ 'within-subject normalization does not provide correct results' # for cross validation, use SVM with precomputed kernel # no shrinking, set C=1 clf = svm.SVC(kernel='precomputed', shrinking=False, C=1, gamma='auto') results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [7, 4, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via SVM does not provide correct results' # for cross validation, use logistic regression clf = LogisticRegression() results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [6, 3, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), ( "voxel selection via logistic regression does not provide correct " "results")
def test_voxel_selection(): fake_raw_data = [create_epoch() for i in range(8)] labels = [0, 1, 0, 1, 0, 1, 0, 1] # 2 subjects, 4 epochs per subject vs = VoxelSelector(labels, 4, 2, fake_raw_data, voxel_unit=1) # test scipy normalization fake_corr = prng.rand(1, 4, 5).astype(np.float32) fake_corr = vs._correlation_normalization(fake_corr) if MPI.COMM_WORLD.Get_rank() == 0: expected_fake_corr = [[[1.19203866, 0.18862808, -0.54350245, -1.18334889, -0.16860008], [-1.06594729, -1.08742261, -1.19447124, 1.14114654, -0.67860204], [0.7839641, 1.53981364, 0.24948341, 0.82626557, 1.67902875], [-0.91005552, -0.64101928, 1.48848987, -0.78406328, -0.83182675]]] assert np.allclose(fake_corr, expected_fake_corr), \ 'within-subject normalization does not provide correct results' # for cross validation, use SVM with precomputed kernel # no shrinking, set C=1 clf = svm.SVC(kernel='precomputed', shrinking=False, C=1) results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [7, 4, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via SVM does not provide correct results' # for cross validation, use logistic regression clf = LogisticRegression() results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8*tuple[1]) expected_output = [6, 3, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via logistic regression does not provide correct results'
def test_voxel_selection(): fake_raw_data = [create_epoch() for i in range(8)] labels = [0, 1, 0, 1, 0, 1, 0, 1] # 2 subjects, 4 epochs per subject vs = VoxelSelector(labels, 4, 2, fake_raw_data, voxel_unit=1) # test scipy normalization fake_corr = prng.rand(1, 4, 5).astype(np.float32) fake_corr = vs._correlation_normalization(fake_corr) if MPI.COMM_WORLD.Get_rank() == 0: expected_fake_corr = [ [[1.19203866, 0.18862808, -0.54350245, -1.18334889, -0.16860008], [-1.06594729, -1.08742261, -1.19447124, 1.14114654, -0.67860204], [0.7839641, 1.53981364, 0.24948341, 0.82626557, 1.67902875], [-0.91005552, -0.64101928, 1.48848987, -0.78406328, -0.83182675]] ] assert np.allclose(fake_corr, expected_fake_corr), \ 'within-subject normalization does not provide correct results' # for cross validation, use SVM with precomputed kernel # no shrinking, set C=1 clf = svm.SVC(kernel='precomputed', shrinking=False, C=1) results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8 * tuple[1]) expected_output = [7, 4, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), \ 'voxel selection via SVM does not provide correct results' # for cross validation, use logistic regression clf = LogisticRegression() results = vs.run(clf) if MPI.COMM_WORLD.Get_rank() == 0: output = [None] * len(results) for tuple in results: output[tuple[0]] = int(8 * tuple[1]) expected_output = [6, 3, 6, 4, 4] assert np.allclose(output, expected_output, atol=1), ( "voxel selection via logistic regression does not provide correct " "results")
# random=RandomType.REPRODUCIBLE) # if providing two masks, just append the second mask as the last input argument # and specify raw_data2 # example: # images = io.load_images_from_dir(data_dir, extension) # mask2 = io.load_boolean_mask('face_scene/mask.nii.gz') # raw_data, raw_data2, labels = prepare_fcma_data(images, conditions, mask, # mask2) 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(labels[0:204], epochs_per_subj, num_subjs-1, raw_data[0:204]) # if using all subjects vs = VoxelSelector(labels, epochs_per_subj, num_subjs, raw_data) # if providing two masks, just append raw_data2 as the last input argument #vs = VoxelSelector(labels, epochs_per_subj, num_subjs, raw_data, raw_data2=raw_data2) # 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]) mask_img = nib.load(mask_file) mask = mask_img.get_data().astype(np.bool) score_volume = np.zeros(mask.shape, dtype=np.float32) score = np.zeros(len(results), dtype=np.float32) seq_volume = np.zeros(mask.shape, dtype=np.int) seq = np.zeros(len(results), dtype=np.int)
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]) mask_img = nib.load(mask_file) mask = mask_img.get_data().astype(np.bool) score_volume = np.zeros(mask.shape, dtype=np.float32) score = np.zeros(len(results), dtype=np.float32) seq_volume = np.zeros(mask.shape, dtype=np.int) seq = np.zeros(len(results), dtype=np.int)
# Preprocess the data and prepare for FCMA raw_data, _, labels = prepare_fcma_data(images, epoch_list, mask) # enforce left one out file_str = output_dir + '/fc_no' + str(left_out_subj) + '_' start_idx = int(int(left_out_subj) * epochs_per_subj) end_idx = int(start_idx + epochs_per_subj) # Take out the idxs corresponding to all participants but this one subsampled_idx = list( set(range(len(labels))) - set(range(start_idx, end_idx))) labels_subsampled = [labels[i] for i in subsampled_idx] raw_data_subsampled = [raw_data[i] for i in subsampled_idx] # Set up the voxel selection object for fcma vs = VoxelSelector(labels_subsampled, epochs_per_subj, num_subjs - 1, raw_data_subsampled) # for cross validation, use SVM with precomputed kernel clf = SVC(kernel='precomputed', shrinking=False, C=1) 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') # Load in the mask with nibabel mask_img = nib.load(mask_file) mask = mask_img.get_data().astype(np.bool) # Preset the volumes score_volume = np.zeros(mask.shape, dtype=np.float32)
# replace "stream=sys.stdout" with "filename='fcma.log'" logging.basicConfig(level=logging.INFO, format=format, stream=sys.stdout) logger = logging.getLogger(__name__) """ example running command: mpirun -np 2 python voxel_selection.py /Users/yidawang/data/face_scene/raw nii.gz /Users/yidawang/data/face_scene/mask.nii.gz data/fs_epoch_labels.npy 12 18 """ 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_data(data_dir, extension, mask_file, epoch_file) epochs_per_subj = int(sys.argv[5]) num_subjs = int(sys.argv[6]) vs = VoxelSelector(raw_data, epochs_per_subj, labels, num_subjs) # for cross validation, use SVM with precomputed kernel # no shrinking, set C=10 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: print(results[0:100])