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_images_from_dir_data_shape( in_dir: Path, expected_image_data_shape: Sequence[int], expected_n_subjects: int ) -> None: for i, image in enumerate(io.load_images_from_dir(in_dir, "bet.nii.gz")): assert image.get_data().shape == (64, 64, 26, 10) assert i + 1 == expected_n_subjects
def test_load_images_from_dir_data_shape( in_dir: Path, expected_image_data_shape: Sequence[int], expected_n_subjects: int ) -> None: i = 0 for image in io.load_images_from_dir(in_dir, "bet.nii.gz"): assert image.get_data().shape == (64, 64, 26, 10) 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'
# 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__) """ 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)
# python3 classification.py face_scene bet.nii.gz face_scene/prefrontal_top_mask.nii.gz face_scene/fs_epoch_labels.npy if __name__ == '__main__': 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)
data_dir = sys.argv[1] suffix = sys.argv[2] mask_file = sys.argv[3] epoch_file = sys.argv[4] # all MPI processes read the mask; the mask file is small mask_image = nib.load(mask_file) mask = io.load_boolean_mask(mask_file) data = None labels = None if MPI.COMM_WORLD.Get_rank()==0: logger.info( 'mask size: %d' % np.sum(mask) ) images = io.load_images_from_dir(data_dir, suffix=suffix) conditions = io.load_labels(epoch_file) data, labels = prepare_searchlight_mvpa_data(images, conditions) # 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 #data, labels = prepare_searchlight_mvpa_data(images, conditions, # random=RandomType.UNREPRODUCIBLE) # the following line is an example to leaving a subject out