Beispiel #1
0
 def test_bootstrap_crossval(self):
     from rsatoolbox.inference import bootstrap_crossval
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     dis = np.random.rand(11, 190)  # 11 10x10 rdms
     mes = "Euclidean"
     des = {'subj': 0}
     rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7])}
     pattern_des = {
         'type':
         np.array([
             0, 1, 2, 2, 4, 5, 5, 5, 6, 7, 10, 11, 12, 12, 14, 15, 15, 15,
             16, 17
         ])
     }
     rdms = RDMs(dissimilarities=dis,
                 rdm_descriptors=rdm_des,
                 pattern_descriptors=pattern_des,
                 dissimilarity_measure=mes,
                 descriptors=des)
     m = ModelFixed('test', rdms[0])
     bootstrap_crossval(m,
                        rdms,
                        N=10,
                        k_rdm=2,
                        k_pattern=2,
                        pattern_descriptor='type',
                        rdm_descriptor='session')
Beispiel #2
0
def pool_rdm(rdms, method='cosine'):
    """pools multiple RDMs into the one with maximal performance under a given
    evaluation metric
    rdm_descriptors of the generated rdms are empty

    Args:
        rdms (rsatoolbox.rdm.RDMs):
            RDMs to be pooled
        method : String, optional
            Which comparison method to optimize for. The default is 'cosine'.

    Returns:
        rsatoolbox.rdm.RDMs: the pooled RDM, i.e. a RDM with maximal performance
            under the chosen method

    """
    rdm_vec = rdms.get_vectors()
    if method == 'euclid':
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'neg_riem_dist':
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'cosine':
        rdm_vec = rdm_vec / np.sqrt(
            np.nanmean(rdm_vec**2, axis=1, keepdims=True))
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'corr':
        rdm_vec = rdm_vec - np.nanmean(rdm_vec, axis=1, keepdims=True)
        rdm_vec = rdm_vec / np.nanstd(rdm_vec, axis=1, keepdims=True)
        rdm_vec = _nan_mean(rdm_vec)
        rdm_vec = rdm_vec - np.nanmin(rdm_vec)
    elif method == 'cosine_cov':
        rdm_vec = rdm_vec / np.sqrt(
            np.nanmean(rdm_vec**2, axis=1, keepdims=True))
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'corr_cov':
        rdm_vec = rdm_vec - np.nanmean(rdm_vec, axis=1, keepdims=True)
        rdm_vec = rdm_vec / np.nanstd(rdm_vec, axis=1, keepdims=True)
        rdm_vec = _nan_mean(rdm_vec)
        rdm_vec = rdm_vec - np.nanmin(rdm_vec)
    elif method == 'spearman' or method == 'rho-a':
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'rho-a':
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'kendall' or method == 'tau-b':
        Warning('Noise ceiling for tau based on averaged ranks!')
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'tau-a':
        Warning('Noise ceiling for tau based on averaged ranks!')
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    else:
        raise ValueError('Unknown RDM comparison method requested!')
    return RDMs(rdm_vec,
                dissimilarity_measure=rdms.dissimilarity_measure,
                descriptors=rdms.descriptors,
                rdm_descriptors=None,
                pattern_descriptors=rdms.pattern_descriptors)
Beispiel #3
0
    def test_demo_rdm_scatterplot(self):
        import os
        from scipy.io import loadmat
        import numpy as np
        from rsatoolbox.rdm import concat, RDMs
        from rsatoolbox.vis import rdm_comparison_scatterplot
        path = os.path.dirname(os.path.abspath(__file__))

        condition_descriptors = {
            'type': (["A"] * 23) + (["B"] * 23) + (["C"] * 46)
        }

        matlab_data = loadmat(
            os.path.join(path, '..', 'demos',
                         '92imageData/92_brainRDMs.mat'))['RDMs']
        n_rdms = len(matlab_data[0])
        rdms_ = RDMs(
            np.array([matlab_data[0][i][0][0] for i in range(n_rdms)]),
            pattern_descriptors=condition_descriptors,
            rdm_descriptors={'name': np.array([f"RDM{i}" for i in range(4)])})

        rdms_a = concat([rdms_[0], rdms_[1]])
        rdms_b = concat([rdms_[2], rdms_[3]])

        rdm_comparison_scatterplot((rdms_a, rdms_b),
                                   show_marginal_distributions=True,
                                   show_identity_line=True,
                                   show_legend=False,
                                   highlight_selector='type',
                                   highlight_categories=["A", "B", "C"],
                                   colors={
                                       "A": (1, 0, 0),
                                       "B": (0, 1, 0),
                                       "C": (0, 0, 1),
                                   })
Beispiel #4
0
 def test_bootstrap_sample_pattern(self):
     from rsatoolbox.inference import bootstrap_sample_pattern
     from rsatoolbox.rdm import RDMs
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     rdm_sample = bootstrap_sample_pattern(rdms)
     assert rdm_sample[0].n_cond == 5
     assert rdm_sample[0].n_rdm == 11
Beispiel #5
0
    def test_Icon_from_rdm(self):
        from rsatoolbox.vis import Icon
        from rsatoolbox.rdm import RDMs

        rdm = RDMs(np.random.rand(1, 190))
        ic = Icon(rdm)
        self.assertEqual(ic.final_image.size[0], 100)
Beispiel #6
0
 def test_eval_fixed(self):
     from rsatoolbox.inference import eval_fixed
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     m = ModelFixed('test', rdms.get_vectors()[0])
     eval_fixed(m, rdms)
Beispiel #7
0
 def test_bootstrap_cv_random(self):
     from rsatoolbox.inference import bootstrap_cv_random
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     dis = np.random.rand(11, 190)  # 11 10x10 rdms
     mes = "Euclidean"
     des = {'subj': 0}
     rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7])}
     pattern_des = {
         'type':
         np.array([
             0, 1, 2, 2, 4, 5, 5, 5, 6, 7, 10, 11, 12, 12, 14, 15, 15, 15,
             16, 17
         ])
     }
     rdms = RDMs(dissimilarities=dis,
                 rdm_descriptors=rdm_des,
                 pattern_descriptors=pattern_des,
                 dissimilarity_measure=mes,
                 descriptors=des)
     m = ModelFixed('test', rdms[0])
     res = bootstrap_cv_random(m,
                               rdms,
                               N=10,
                               n_rdm=2,
                               n_pattern=4,
                               pattern_descriptor='type',
                               rdm_descriptor='session')
     self.assertEqual(res.evaluations.shape[0], 10)
Beispiel #8
0
 def test_eval_bootstrap_rdm(self):
     from rsatoolbox.inference import eval_bootstrap_rdm
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     m = ModelFixed('test', rdms.get_vectors()[0])
     m2 = ModelFixed('test2', rdms.get_vectors()[1])
     value = eval_bootstrap_rdm([m, m2], rdms, N=10)
Beispiel #9
0
 def test_eval_bootstrap_rdm(self):
     from rsatoolbox.inference import eval_bootstrap_rdm
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     m = ModelFixed('test', rdms.get_vectors()[0])
     eval_bootstrap_rdm(m, rdms, N=10)
     eval_bootstrap_rdm(m, rdms, N=10, boot_noise_ceil=True)
Beispiel #10
0
 def test_eval_bootstrap(self):
     from rsatoolbox.inference import eval_bootstrap
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     m = ModelFixed('test', rdms.get_vectors()[0])
     m2 = ModelFixed('test2', rdms.get_vectors()[1])
     result = eval_bootstrap([m, m2], rdms, N=10)
     assert result.evaluations.shape[1] == 2
     assert result.evaluations.shape[0] == 10
Beispiel #11
0
 def test_creation_rdm(self):
     from rsatoolbox.rdm import RDMs
     rdm = np.array(np.ones(6))
     rdm_obj = RDMs(np.array([rdm]))
     m = model.ModelFixed('Test Model', rdm_obj)
     m.fit(rdm_obj)
     pred = m.predict()
     assert np.all(pred == rdm)
     pred_obj = m.predict_rdm()
     assert isinstance(pred_obj, RDMs)
Beispiel #12
0
 def test_bootstrap_testset_rdm(self):
     from rsatoolbox.inference import bootstrap_testset_rdm
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     m = ModelFixed('test', rdms.get_vectors()[0])
     _, _ = bootstrap_testset_rdm(m,
                                  rdms,
                                  method='cosine',
                                  fitter=None,
                                  N=100,
                                  rdm_descriptor=None)
Beispiel #13
0
 def test_pool_standard(self):
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.util.pooling import pool_rdm
     dissimilarities = np.random.rand(5, 10)
     rdms = RDMs(dissimilarities)
     for method in [
             'euclid', 'cosine', 'corr', 'cosine_cov', 'corr_cov',
             'spearman', 'rho-a', 'tau-b', 'tau-a'
     ]:
         pooled_rdm = pool_rdm(rdms, method=method)
         self.assertEqual(pooled_rdm.n_cond, rdms.n_cond)
         self.assertEqual(pooled_rdm.n_rdm, 1)
Beispiel #14
0
 def test_fit(self):
     from rsatoolbox.rdm import RDMs
     rdm = np.random.rand(2, 6)
     pattern_descriptors = {'test': ['a', 'b', 'c', 'd']}
     rdm_descriptors = {'ind': np.array([1, 2])}
     rdm_obj = RDMs(rdm,
                    dissimilarity_measure='euclid',
                    pattern_descriptors=pattern_descriptors,
                    rdm_descriptors=rdm_descriptors)
     m = model.ModelWeighted('Test Model', rdm_obj)
     train = rdm_obj.subset('ind', 2)
     theta = m.fit(train)
Beispiel #15
0
 def test_fit(self):
     from rsatoolbox.rdm import RDMs
     rdm = np.random.rand(5, 15)
     pattern_descriptors = {'test': ['a', 'b', 'c', 'd', 'e', 'f']}
     rdm_descriptors = {'ind': np.array([1, 2, 3, 1, 2])}
     rdm_obj = RDMs(rdm,
                    dissimilarity_measure='euclid',
                    pattern_descriptors=pattern_descriptors,
                    rdm_descriptors=rdm_descriptors)
     m = model.ModelInterpolate('Test Model', rdm_obj)
     train = rdm_obj.subset('ind', 2)
     theta = m.fit(train)
     _ = m.predict(theta)
Beispiel #16
0
 def test_creation_rdm(self):
     from rsatoolbox.rdm import RDMs
     rdm = np.random.rand(2, 6)
     pattern_descriptors = {'test': ['a', 'b', 'c', 'd']}
     rdm_obj = RDMs(rdm,
                    dissimilarity_measure='euclid',
                    pattern_descriptors=pattern_descriptors)
     m = model.ModelWeighted('Test Model', rdm_obj)
     pred = m.predict(np.array([1, 0]))
     assert np.all(pred == rdm[0])
     pred_obj = m.predict_rdm()
     assert isinstance(pred_obj, RDMs)
     assert pred_obj.n_rdm == 1
     assert pred_obj.pattern_descriptors == pattern_descriptors
Beispiel #17
0
 def test_bootstrap_testset_pattern(self):
     from rsatoolbox.inference import bootstrap_testset_pattern
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
     m = ModelFixed('test', rdms.get_vectors()[0])
     m2 = ModelFixed('test2', rdms.get_vectors()[1])
     evaluations, n_cond = bootstrap_testset_pattern(
         [m, m2],
         rdms,
         method='cosine',
         fitter=None,
         N=100,
         pattern_descriptor=None)
Beispiel #18
0
 def test_reorder(self):
     from rsatoolbox.rdm import RDMs
     rdm = np.array([[0., 1., 2., 3.], [1., 0., 1., 2.], [2., 1., 0., 1.],
                     [3., 2., 1., 0.]])
     conds = ['a', 'b', 'c', 'd']
     rdms = RDMs(np.atleast_2d(squareform(rdm)),
                 pattern_descriptors=dict(conds=conds))
     conds_ordered = ['b', 'a', 'c', 'd']
     new_order = [conds.index(cond_idx) for cond_idx in conds_ordered]
     rdm_reordered = rdm[np.ix_(new_order, new_order)]
     rdms.reorder(new_order)
     assert_array_equal(np.atleast_2d(squareform(rdm_reordered)),
                        rdms.dissimilarities)
     assert_array_equal(conds_ordered,
                        rdms.pattern_descriptors.get('conds'))
Beispiel #19
0
    def test_t_scipy(self):
        from rsatoolbox.util.inference_util import t_tests
        from rsatoolbox.inference import eval_fixed
        from rsatoolbox.rdm import RDMs
        from rsatoolbox.model import ModelFixed
        import scipy.stats

        rdms = RDMs(np.random.rand(11, 10))  # 11 5x5 rdms
        m = ModelFixed('test', rdms.get_vectors()[0])
        m2 = ModelFixed('test', rdms.get_vectors()[2])
        value = eval_fixed([m, m2], rdms)
        ps = t_tests(value.evaluations, value.diff_var, dof=value.dof)
        scipy_t = scipy.stats.ttest_rel(value.evaluations[0, 0],
                                        value.evaluations[0, 1])
        self.assertAlmostEqual(scipy_t.pvalue, ps[0, 1])
Beispiel #20
0
 def test_bootstrap_sample_rdm_descriptors(self):
     from rsatoolbox.inference import bootstrap_sample_rdm
     from rsatoolbox.rdm import RDMs
     dis = np.random.rand(11, 10)  # 11 5x5 rdms
     mes = "Euclidean"
     des = {'subj': 0}
     rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7])}
     pattern_des = {'type': np.array([0, 1, 2, 2, 4])}
     rdms = RDMs(dissimilarities=dis,
                 rdm_descriptors=rdm_des,
                 pattern_descriptors=pattern_des,
                 dissimilarity_measure=mes,
                 descriptors=des)
     rdm_sample = bootstrap_sample_rdm(rdms, 'session')
     assert rdm_sample[0].n_cond == 5
Beispiel #21
0
    def test_category_condition_idxs_2_categories_by_ints(self):
        from rsatoolbox.util.rdm_utils import category_condition_idxs
        from rsatoolbox.rdm.rdms import RDMs

        n_rdm, n_cond = 4, 8
        dis = np.zeros((n_rdm, n_cond, n_cond))
        rdms = RDMs(dissimilarities=dis,
                    dissimilarity_measure='Euclidean',
                    descriptors={'subj': range(n_rdm)})
        categories = category_condition_idxs(rdms, [1, 2, 1, 2, 1, 2, 1, 2])

        self.assertEqual(categories, {
            'Category 1': [0, 2, 4, 6],
            'Category 2': [1, 3, 5, 7],
        })
Beispiel #22
0
 def test_boot_noise_ceiling_runs_for_method(self, method):
     from rsatoolbox.inference import boot_noise_ceiling
     from rsatoolbox.rdm import RDMs
     dis = np.random.rand(11, 10)  # 11 5x5 rdms
     mes = "Euclidean"
     des = {'subj': 0}
     rdm_des = {'session': np.array([1, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7])}
     pattern_des = {'type': np.array([0, 1, 2, 2, 4])}
     rdms = RDMs(
         dissimilarities=dis,
         rdm_descriptors=rdm_des,
         pattern_descriptors=pattern_des,
         dissimilarity_measure=mes,
         descriptors=des
     )
     _, _ = boot_noise_ceiling(rdms, method=method)
Beispiel #23
0
 def test_sort_by_alpha(self):
     from rsatoolbox.rdm import RDMs
     rdm = np.array([[0., 1., 2., 3.], [1., 0., 1., 2.], [2., 1., 0., 1.],
                     [3., 2., 1., 0.]])
     conds = ['b', 'a', 'c', 'd']
     rdms = RDMs(np.atleast_2d(squareform(rdm)),
                 pattern_descriptors=dict(conds=conds))
     rdms.sort_by(conds='alpha')
     new_order = np.argsort(conds)
     rdm_reordered = rdm[np.ix_(new_order, new_order)]
     self.assertIsNone(
         assert_array_equal(np.atleast_2d(squareform(rdm_reordered)),
                            rdms.dissimilarities))
     self.assertIsNone(
         assert_array_equal(sorted(conds),
                            rdms.pattern_descriptors.get('conds')))
Beispiel #24
0
 def test_pool_nan(self):
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.util.pooling import pool_rdm
     dissimilarities = np.random.rand(5, 10)
     dissimilarities[:, 3] = np.nan
     rdms = RDMs(dissimilarities)
     for method in [
             'euclid', 'cosine', 'corr', 'cosine_cov', 'corr_cov',
             'spearman', 'rho-a', 'tau-b', 'tau-a'
     ]:
         pooled_rdm = pool_rdm(rdms, method=method)
         self.assertEqual(pooled_rdm.n_cond, rdms.n_cond)
         self.assertEqual(pooled_rdm.n_rdm, 1)
         self.assertTrue(np.isnan(pooled_rdm.dissimilarities[0, 3]),
                         'nan got removed while pooling for %s' % method)
         self.assertFalse(np.isnan(pooled_rdm.dissimilarities[0, 4]),
                          'too many nans while pooling for %s' % method)
Beispiel #25
0
 def test_cv_noise_ceiling(self):
     from rsatoolbox.inference import cv_noise_ceiling
     from rsatoolbox.inference import sets_k_fold_rdm
     from rsatoolbox.rdm import RDMs
     dis = np.random.rand(11, 10)  # 11 5x5 rdms
     mes = "Euclidean"
     des = {'subj': 0}
     rdm_des = {'session': np.array([1, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7])}
     pattern_des = {'type': np.array([0, 1, 2, 2, 4])}
     rdms = RDMs(
         dissimilarities=dis,
         rdm_descriptors=rdm_des,
         pattern_descriptors=pattern_des,
         dissimilarity_measure=mes,
         descriptors=des
     )
     _, test_set, ceil_set = sets_k_fold_rdm(rdms, k_rdm=3, random=False)
     _, _ = cv_noise_ceiling(rdms, ceil_set, test_set, method='cosine')
Beispiel #26
0
    def test_category_condition_idxs_2_categories_by_name(self):
        from rsatoolbox.util.rdm_utils import category_condition_idxs
        from rsatoolbox.rdm.rdms import RDMs

        n_rdm, n_cond = 4, 8
        dis = np.zeros((n_rdm, n_cond, n_cond))
        rdms = RDMs(
            dissimilarities=dis,
            # 2 categories, P and Q
            pattern_descriptors={'type': list('PPPPQQQQ')},
            dissimilarity_measure='Euclidean',
            descriptors={'subj': range(n_rdm)})
        categories = category_condition_idxs(rdms, 'type')

        self.assertEqual(categories, {
            'P': [0, 1, 2, 3],
            'Q': [4, 5, 6, 7],
        })
Beispiel #27
0
 def setUp(self):
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.model import ModelFixed
     dis = np.random.rand(11, 190)  # 11 20x20 rdms
     mes = "Euclidean"
     des = {'subj': 0}
     rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7])}
     pattern_des = {
         'type':
         np.array([
             0, 1, 2, 2, 4, 5, 5, 5, 6, 7, 10, 11, 12, 12, 14, 15, 15, 15,
             16, 17
         ])
     }
     rdms = RDMs(dissimilarities=dis,
                 rdm_descriptors=rdm_des,
                 pattern_descriptors=pattern_des,
                 dissimilarity_measure=mes,
                 descriptors=des)
     m = ModelFixed('test', rdms[0])
     self.rdms = rdms
     self.m = m
Beispiel #28
0
 def test_save_load_result(self):
     from rsatoolbox.rdm import RDMs
     from rsatoolbox.inference import Result
     from rsatoolbox.inference import load_results
     from rsatoolbox.model import ModelFixed
     import io
     rdm = RDMs(np.random.rand(10),
                pattern_descriptors={
                    'test': ['test1', 'test1', 'test1', 'test3', 'test']
                })
     m1 = ModelFixed('test1', rdm)
     m2 = ModelFixed('test2', np.random.rand(10))
     models = [m1, m2]
     evaluations = np.random.rand(100, 2)
     method = 'test_method'
     cv_method = 'test_cv_method'
     noise_ceiling = np.array([0.5, 0.2])
     res = Result(models, evaluations, method, cv_method, noise_ceiling)
     f = io.BytesIO()  # Essentially a Mock file
     res.save(f, file_type='hdf5')
     res_loaded = load_results(f, file_type='hdf5')
     assert res_loaded.method == method
     assert res_loaded.cv_method == cv_method
     assert np.all(res_loaded.evaluations == evaluations)
Beispiel #29
0
def get_searchlight_RDMs(data_2d, centers, neighbors, events,
                         method='correlation', verbose=True):
    """Iterates over all the searchlight centers and calculates the RDM

    Args:

        data_2d (2D numpy array): brain data,
        shape n_observations x n_channels (i.e. voxels/vertices)

        centers (1D numpy array): center indices for all searchlights as provided
        by rsatoolbox.util.searchlight.get_volume_searchlight

        neighbors (list): list of lists with neighbor voxel indices for all searchlights
        as provided by rsatoolbox.util.searchlight.get_volume_searchlight

        events (1D numpy array): 1D array of length n_observations

        method (str, optional): distance metric,
        see rsatoolbox.rdm.calc for options. Defaults to 'correlation'.

        verbose (bool, optional): Defaults to True.

    Returns:
        RDM [rsatoolbox.rdm.RDMs]: RDMs object with the RDM for each searchlight
                              the RDM.rdm_descriptors['voxel_index']
                              describes the center voxel index each RDM is associated with
    """

    data_2d, centers = np.array(data_2d), np.array(centers)
    n_centers = centers.shape[0]

    # For memory reasons, we chunk the data if we have more than 1000 RDMs
    if n_centers > 1000:
        # we can't run all centers at once, that will take too much memory
        # so lets to some chunking
        chunked_center = np.split(np.arange(n_centers),
                                  np.linspace(0, n_centers,
                                              101, dtype=int)[1:-1])

        # loop over chunks
        n_conds = len(np.unique(events))
        RDM = np.zeros((n_centers, n_conds * (n_conds - 1) // 2))
        for chunks in tqdm(chunked_center, desc='Calculating RDMs...'):
            center_data = []
            for c in chunks:
                # grab this center and neighbors
                center = centers[c]
                center_neighbors = neighbors[c]
                # create a database object with this data
                ds = Dataset(data_2d[:, center_neighbors],
                             descriptors={'center': center},
                             obs_descriptors={'events': events},
                             channel_descriptors={'voxels': center_neighbors})
                center_data.append(ds)

            RDM_corr = calc_rdm(center_data, method=method,
                                descriptor='events')
            RDM[chunks, :] = RDM_corr.dissimilarities
    else:
        center_data = []
        for c in range(n_centers):
            # grab this center and neighbors
            center = centers[c]
            nb = neighbors[c]
            # create a database object with this data
            ds = Dataset(data_2d[:, nb],
                         descriptors={'center': c},
                         obs_descriptors={'events': events},
                         channel_descriptors={'voxels': nb})
            center_data.append(ds)
        # calculate RDMs for each database object
        RDM = calc_rdm(center_data, method=method,
                       descriptor='events').dissimilarities

    SL_rdms = RDMs(RDM,
                   rdm_descriptors={'voxel_index': centers},
                   dissimilarity_measure=method)

    return SL_rdms
Beispiel #30
0
def pool_rdm(rdms, method='cosine', sigma_k=None):
    """pools multiple RDMs into the one with maximal performance under a given
    evaluation metric
    rdm_descriptors of the generated rdms are empty

    Args:
        rdms (pyrsa.rdm.RDMs):
            RDMs to be pooled
        method : String, optional
            Which comparison method to optimize for. The default is 'cosine'.

    Returns:
        pyrsa.rdm.RDMs: the pooled RDM, i.e. a RDM with maximal performance
            under the chosen method

    """
    rdm_vec = rdms.get_vectors()
    if method == 'euclid':
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'cosine':
        rdm_vec = rdm_vec / np.sqrt(
            np.nanmean(rdm_vec**2, axis=1, keepdims=True))
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'corr':
        rdm_vec = rdm_vec - np.nanmean(rdm_vec, axis=1, keepdims=True)
        rdm_vec = rdm_vec / np.nanstd(rdm_vec, axis=1, keepdims=True)
        rdm_vec = _nan_mean(rdm_vec)
        rdm_vec = rdm_vec - np.nanmin(rdm_vec) + 0.01
    elif method == 'cosine_cov':
        v = get_v(rdms.n_cond, sigma_k=sigma_k)
        ok_idx = np.all(np.isfinite(rdm_vec), axis=0)
        v = v[ok_idx][:, ok_idx]
        rdm_vec_nonan = rdm_vec[:, ok_idx]
        v_inv_x = np.array([
            scipy.sparse.linalg.cg(v, rdm_vec_nonan[i], atol=10**-9)[0]
            for i in range(rdms.n_rdm)
        ])
        rdm_norms = np.einsum('ij, ij->i', rdm_vec_nonan,
                              v_inv_x).reshape([rdms.n_rdm, 1])
        rdm_vec = rdm_vec / np.sqrt(rdm_norms)
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'corr_cov':
        rdm_vec = rdm_vec - np.nanmean(rdm_vec, axis=1, keepdims=True)
        v = get_v(rdms.n_cond, sigma_k=sigma_k)
        ok_idx = np.all(np.isfinite(rdm_vec), axis=0)
        v = v[ok_idx][:, ok_idx]
        rdm_vec_nonan = rdm_vec[:, ok_idx]
        v_inv_x = np.array([
            scipy.sparse.linalg.cg(v, rdm_vec_nonan[i], atol=10**-9)[0]
            for i in range(rdms.n_rdm)
        ])
        rdm_norms = np.einsum('ij, ij->i', rdm_vec_nonan,
                              v_inv_x).reshape([rdms.n_rdm, 1])
        rdm_vec = rdm_vec / np.sqrt(rdm_norms)
        rdm_vec = _nan_mean(rdm_vec)
        rdm_vec = rdm_vec - np.nanmin(rdm_vec) + 0.01
    elif method == 'spearman' or method == 'rho-a':
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'rho-a':
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'kendall' or method == 'tau-b':
        Warning('Noise ceiling for tau based on averaged ranks!')
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    elif method == 'tau-a':
        Warning('Noise ceiling for tau based on averaged ranks!')
        rdm_vec = np.array([_nan_rank_data(v) for v in rdm_vec])
        rdm_vec = _nan_mean(rdm_vec)
    else:
        raise ValueError('Unknown RDM comparison method requested!')
    return RDMs(rdm_vec,
                dissimilarity_measure=rdms.dissimilarity_measure,
                descriptors=rdms.descriptors,
                rdm_descriptors=None,
                pattern_descriptors=rdms.pattern_descriptors)