def test_prepare_fcma_data(): images = io.load_images_from_dir(data_dir, suffix=suffix) mask = io.load_boolean_mask(mask_file) conditions = io.load_labels(epoch_file) raw_data, _, labels = prepare_fcma_data(images, conditions, mask) expected_raw_data = np.load(expected_dir / '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' from brainiak.fcma.preprocessing import RandomType images = io.load_images_from_dir(data_dir, suffix=suffix) random_raw_data, _, _ = prepare_fcma_data(images, conditions, mask, random=RandomType.REPRODUCIBLE) assert len(random_raw_data) == len(expected_raw_data), \ 'numbers of epochs do not match in test_prepare_fcma_data' images = io.load_images_from_dir(data_dir, suffix=suffix) random_raw_data, _, _ = prepare_fcma_data(images, conditions, mask, random=RandomType.UNREPRODUCIBLE) assert len(random_raw_data) == len(expected_raw_data), \ 'numbers of epochs do not match in test_prepare_fcma_data'
def test_prepare_searchlight_mvpa_data(): images = io.load_images_from_dir(data_dir, suffix=suffix) conditions = io.load_labels(epoch_file) processed_data, labels = prepare_searchlight_mvpa_data(images, conditions) expected_searchlight_processed_data = np.load( expected_dir / 'expected_searchlight_processed_data.npy') for idx in range(len(processed_data)): assert np.allclose(processed_data[idx], expected_searchlight_processed_data[idx]), ( 'raw data do not match in test_prepare_searchlight_mvpa_data') assert np.array_equal(labels, expected_labels), \ 'the labels do not match in test_prepare_searchlight_mvpa_data' from brainiak.fcma.preprocessing import RandomType images = io.load_images_from_dir(data_dir, suffix=suffix) random_processed_data, _ = prepare_searchlight_mvpa_data( images, conditions, random=RandomType.REPRODUCIBLE) assert (len(random_processed_data) == len(expected_searchlight_processed_data)), ( 'numbers of epochs do not match in test_prepare_searchlight_mvpa_data') images = io.load_images_from_dir(data_dir, suffix=suffix) random_processed_data, _ = prepare_searchlight_mvpa_data( images, conditions, random=RandomType.UNREPRODUCIBLE) assert (len(random_processed_data) == len(expected_searchlight_processed_data)), ( 'numbers of epochs do not match in test_prepare_searchlight_mvpa_data')
def test_load_labels(labels_path: Path, expected_condition_spec_shape: Sequence[int], expected_n_subjects: int) -> None: condition_specs = io.load_labels(labels_path) i = 0 for condition_spec in condition_specs: assert condition_spec.shape == expected_condition_spec_shape i += 1 assert i == expected_n_subjects
def test_load_labels(labels_path: Path, expected_condition_spec_shape: Sequence[int], expected_n_subjects: int) -> None: condition_specs = io.load_labels(labels_path) i = 0 for condition_spec in condition_specs: assert condition_spec.shape == expected_condition_spec_shape i += 1 assert i == expected_n_subjects
def test_prepare_searchlight_mvpa_data(): images = io.load_images_from_dir(data_dir, suffix) conditions = io.load_labels(epoch_file) processed_data, labels = prepare_searchlight_mvpa_data(images, conditions) expected_searchlight_processed_data = np.load( expected_dir / 'expected_searchlight_processed_data.npy') for idx in range(len(processed_data)): assert np.allclose(processed_data[idx], expected_searchlight_processed_data[idx]), \ 'raw data do not match in test_prepare_searchlight_mvpa_data' assert np.array_equal(labels, expected_labels), \ 'the labels do not match in test_prepare_searchlight_mvpa_data'
def test_prepare_fcma_data(): images = io.load_images_from_dir(data_dir, suffix) mask = io.load_boolean_mask(mask_file) conditions = io.load_labels(epoch_file) raw_data, _, labels = prepare_fcma_data(images, conditions, mask) expected_raw_data = np.load(expected_dir / '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_mvpa_data(): images = io.load_images_from_dir(data_dir, suffix=suffix) mask = io.load_boolean_mask(mask_file) conditions = io.load_labels(epoch_file) processed_data, labels = prepare_mvpa_data(images, conditions, mask) expected_processed_data = np.load(expected_dir / 'expected_processed_data.npy') assert len(processed_data) == len(expected_processed_data), \ 'numbers of epochs do not match in test_prepare_mvpa_data' for idx in range(len(processed_data)): assert np.allclose(processed_data[idx], expected_processed_data[idx]), ( 'raw data do not match in test_prepare_mvpa_data') assert np.array_equal(labels, expected_labels), \ 'the labels do not match in test_prepare_mvpa_data'
def test_prepare_fcma_data(): images = io.load_images_from_dir(data_dir, suffix=suffix) mask = io.load_boolean_mask(mask_file) conditions = io.load_labels(epoch_file) raw_data, _, labels = prepare_fcma_data(images, conditions, mask) expected_raw_data = np.load(expected_dir / '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' from brainiak.fcma.preprocessing import RandomType images = io.load_images_from_dir(data_dir, suffix=suffix) random_raw_data, _, _ = prepare_fcma_data(images, conditions, mask, random=RandomType.REPRODUCIBLE) assert len(random_raw_data) == len(expected_raw_data), \ 'numbers of epochs do not match in test_prepare_fcma_data' images = io.load_images_from_dir(data_dir, suffix=suffix) random_raw_data, _, _ = prepare_fcma_data(images, conditions, mask, random=RandomType.UNREPRODUCIBLE) assert len(random_raw_data) == len(expected_raw_data), \ 'numbers of epochs do not match in test_prepare_fcma_data'
logging.basicConfig(level=logging.INFO, format=format, stream=sys.stdout) 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] suffix = sys.argv[2] mask_file = sys.argv[3] epoch_file = sys.argv[4] images = io.load_images_from_dir(data_dir, suffix=suffix) mask = io.load_boolean_mask(mask_file) conditions = io.load_labels(epoch_file) raw_data, _, labels = prepare_fcma_data(images, conditions, mask) # setting the random argument produces random voxel selection results # for non-parametric statistical analysis. # There are three random options: # RandomType.NORANDOM is the default # RandomType.REPRODUCIBLE permutes the voxels in the same way every run # RandomType.UNREPRODUCIBLE permutes the voxels differently across runs # example: # from brainiak.fcma.preprocessing import RandomType # raw_data, _, labels = prepare_fcma_data(images, conditions, mask, # random=RandomType.REPRODUCIBLE) # if providing two masks, just append the second mask as the last input argument # and specify raw_data2
if len(sys.argv) != 5: logger.error('the number of input argument is not correct') sys.exit(1) data_dir = sys.argv[1] extension = sys.argv[2] mask_file = sys.argv[3] epoch_file = sys.argv[4] epoch_list = np.load(epoch_file) num_subjects = len(epoch_list) num_epochs_per_subj = epoch_list[0].shape[1] images = io.load_images_from_dir(data_dir, extension) mask = io.load_boolean_mask(mask_file) conditions = io.load_labels(epoch_file) raw_data, _, labels = prepare_fcma_data(images, conditions, mask) example_of_aggregating_sim_matrix(raw_data, labels, num_subjects, num_epochs_per_subj) example_of_cross_validation_with_detailed_info(raw_data, labels, num_subjects, num_epochs_per_subj) example_of_cross_validation_using_model_selection(raw_data, labels, num_subjects, num_epochs_per_subj) # test of two different components for correlation computation # images = io.load_images_from_dir(data_dir, extension) # mask2 = io.load_boolean_mask('face_scene/visual_top_mask.nii.gz') # raw_data, raw_data2, labels = prepare_fcma_data(images, conditions, mask, # mask2) #example_of_correlating_two_components(raw_data, raw_data2, labels, num_subjects, num_epochs_per_subj) #example_of_correlating_two_components_aggregating_sim_matrix(raw_data, raw_data2, labels, num_subjects, num_epochs_per_subj)
else: is_extrinsic = 1 if __name__ == '__main__': # Send a message on the first node if MPI.COMM_WORLD.Get_rank()==0: logger.info( 'Testing for participant %d.\nProgramming starts in %d process(es)' % (int(left_out_subj), MPI.COMM_WORLD.Get_size()) ) # Load in the volumes, mask and labels images = io.load_images_from_dir(data_dir, suffix=suffix) top_n_mask = io.load_boolean_mask(top_n_mask_file) epoch_list = io.load_labels(epoch_file) # Parse the epoch data for useful dimensions epochs_per_subj = epochs_per_subj = epoch_list[0].shape[1] num_subjs = len(epoch_list) # Prepare the data int_data, _, labels = prepare_fcma_data(images, epoch_list, top_n_mask) # What indexes pick out the left out participant? 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 training_idx = list(set(range(len(labels))) - set(range(start_idx, end_idx))) testing_idx = list(range(start_idx, end_idx))