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), })
def _dummy_rdm(): import PIL import matplotlib.markers from collections import defaultdict markers = list(matplotlib.markers.MarkerStyle().markers.keys()) images = np.meshgrid(np.linspace(0.5, 1.0, 50), np.linspace(0.5, 1.0, 30), np.linspace(0.5, 1.0, 3)) images = [ this_image * this_ind / 4.0 for this_ind in range(4) for this_image in images ] images = [ PIL.Image.fromarray(255 * this_image, "RGB") for this_image in images ] names = [ this_class + this_ex for this_class in ("a", "b", "c", "d") for this_ex in ("1", "2", "3") ] n_con = len(names) icons = defaultdict(list) for this_marker, this_image, this_name in zip(markers, images, names): icons["image"].append(rsv.Icon(image=this_image)) icons["marker"].append(rsv.Icon(marker=this_marker, color=[0, 0, 0])) icons["string"].append(rsv.Icon(string=this_name)) icons["text"].append(this_name) ROIs = ["X", "Y", "Z"] return rsr.concat([ rsr.RDMs( dissimilarities=np.random.rand(1, int((n_con - 1) * n_con / 2)), dissimilarity_measure="1-rho", rdm_descriptors=dict(ROI=this_roi), pattern_descriptors=icons, ) for this_roi in ROIs ])
def setUp(self): from rsatoolbox.data import Dataset from rsatoolbox.rdm import calc_rdm from rsatoolbox.rdm import concat rdms = [] for _ in range(5): data = np.random.rand(6, 20) data_s = Dataset(data) rdms.append(calc_rdm(data_s)) self.rdms = concat(rdms)
def test_concat_varargs_multiple_rdms(self): from rsatoolbox.rdm import concat dis = np.zeros((8, 10)) dis2 = np.random.rand(8, 10) mes = "Euclidean" des = {'subj': 0} pattern_des = {'type': np.array([0, 1, 2, 2, 4])} rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7])} rdms1 = rsr.RDMs(dissimilarities=dis, pattern_descriptors=pattern_des, dissimilarity_measure=mes, descriptors=des, rdm_descriptors=rdm_des) rdms2 = rsr.RDMs(dissimilarities=dis2, pattern_descriptors=pattern_des, dissimilarity_measure=mes, descriptors=des, rdm_descriptors=rdm_des) rdm_c1 = concat((rdms1, rdms2)) rdm_c2 = concat(rdms1, rdms2) self.assertEqual(rdm_c1.n_rdm, 16) self.assertEqual(rdm_c2.n_rdm, 16) assert_array_equal(rdm_c1.dissimilarities, rdm_c2.dissimilarities)
def test_concat_varargs_one_rdm(self): from rsatoolbox.rdm import concat dis = np.zeros((8, 10)) mes = "Euclidean" des = {'subj': 0} pattern_des = {'type': np.array([0, 1, 2, 2, 4])} rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7])} rdms = rsr.RDMs(dissimilarities=dis, pattern_descriptors=pattern_des, dissimilarity_measure=mes, descriptors=des, rdm_descriptors=rdm_des) rdm_c1 = concat(rdms) self.assertEqual(rdm_c1.n_rdm, 8) assert_array_equal(rdm_c1.dissimilarities, rdms.dissimilarities)
def test_two_rdms_nan(self): from rsatoolbox.model import ModelInterpolate, ModelWeighted from rsatoolbox.model.fitter import fit_regress, fit_optimize_positive from rsatoolbox.rdm import concat, compare rdms = self.rdms.subsample_pattern('index', [0, 1, 1, 3, 4, 5]) model_rdms = concat([rdms[0], rdms[1]]) model_weighted = ModelWeighted('m_weighted', model_rdms) model_interpolate = ModelInterpolate('m_interpolate', model_rdms) for i_method in ['cosine', 'corr', 'cosine_cov', 'corr_cov']: theta_m_i = model_interpolate.fit(rdms, method=i_method) theta_m_w = model_weighted.fit(rdms, method=i_method) theta_m_w_pos = fit_optimize_positive(model_weighted, rdms, method=i_method) theta_m_w_linear = fit_regress(model_weighted, rdms, method=i_method) eval_m_i = np.mean( compare(model_weighted.predict_rdm(theta_m_i), rdms, method=i_method)) eval_m_w = np.mean( compare(model_weighted.predict_rdm(theta_m_w), rdms, method=i_method)) eval_m_w_pos = np.mean( compare(model_weighted.predict_rdm(theta_m_w_pos), rdms, method=i_method)) eval_m_w_linear = np.mean( compare(model_weighted.predict_rdm(theta_m_w_linear), rdms, method=i_method)) self.assertAlmostEqual( eval_m_i, eval_m_w_pos, places=4, msg='weighted fit differs from interpolation fit!' + '\nfor %s' % i_method) self.assertAlmostEqual( eval_m_w, eval_m_w_linear, places=4, msg='regression fit differs from optimization fit!' + '\nfor %s' % i_method)
def test_concat(self): from rsatoolbox.rdm import concat dis = np.zeros((8, 10)) dis2 = np.random.rand(8, 10) mes = "Euclidean" des = {'subj': 0} pattern_des = {'type': np.array([0, 1, 2, 2, 4])} rdm_des = {'session': np.array([0, 1, 2, 2, 4, 5, 6, 7])} rdms1 = rsr.RDMs(dissimilarities=dis, pattern_descriptors=pattern_des, dissimilarity_measure=mes, descriptors=des, rdm_descriptors=rdm_des) rdms2 = rsr.RDMs(dissimilarities=dis2, pattern_descriptors=pattern_des, dissimilarity_measure=mes, descriptors=des, rdm_descriptors=rdm_des) rdms = concat((rdms1, rdms2)) self.assertEqual(rdms.n_rdm, 16) assert len(rdms.rdm_descriptors['session']) == 16