def test_calc_mahalanobis(self): rdm = rsr.calc_rdm(self.test_data, descriptor='conds', method='mahalanobis') assert rdm.n_cond == 6 noise = np.linalg.inv(np.cov(np.random.randn(10, 5).T)) rdm = rsr.calc_rdm(self.test_data, descriptor='conds', method='mahalanobis', noise=noise) assert rdm.n_cond == 6
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_calc_euclid_as_scipy(self, _parse_input): from rsatoolbox.rdm import calc_rdm data = Mock() data.descriptors = {'session': 0, 'subj': 0} data.measurements = np.random.rand(6, 5) desc = [0, 1, 2, 3, 4, 5] _parse_input.return_value = (data.measurements, desc, 'conds') rdm_expected = pdist(data.measurements)**2 / 5 rdms = calc_rdm(data, descriptor='conds', method='euclidean') self.assertIsNone( assert_array_almost_equal(rdm_expected, rdms.dissimilarities.flatten()))
def test_example_dissimilarities(self): # relevant imports import numpy as np from scipy import io import rsatoolbox import rsatoolbox.data as rsd # abbreviation to deal with dataset import rsatoolbox.rdm as rsr # create a dataset object measurements = {'simTruePatterns': np.random.randn(92, 100)} measurements = measurements['simTruePatterns'] nCond = measurements.shape[0] nVox = measurements.shape[1] # now create a dataset object des = {'session': 1, 'subj': 1} obs_des = { 'conds': np.array(['cond_' + str(x) for x in np.arange(nCond)]) } chn_des = { 'voxels': np.array(['voxel_' + str(x) for x in np.arange(nVox)]) } data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) # calculate an RDM RDM_euc = rsr.calc_rdm(data) RDM_corr = rsr.calc_rdm(data, method='correlation', descriptor='conds') # create an RDM object rdm_des = {'RDM': np.array(['RDM_1'])} RDM_euc2 = rsr.RDMs( RDM_euc.dissimilarities, dissimilarity_measure=RDM_euc.dissimilarity_measure, descriptors=RDM_euc.descriptors, rdm_descriptors=rdm_des, pattern_descriptors=obs_des) print(RDM_euc.dissimilarities) # here a vector dist_matrix = RDM_euc.get_matrices() print(dist_matrix)
def test_calc_poisson_extreme_pairs(self): """Check the dissimilarities computed with the 'poisson' method The closest pair should be that between the condition 1 and itself The furthest pair should be that between condition 1 and condition 3 """ rdm = rsr.calc_rdm(self.test_data_deterministic, descriptor='conds', method='poisson') rdm_array = squareform(rdm.get_vectors()[0, :]) closest_pair_index = np.argmin(rdm_array) furthest_pair_index = np.argmax(rdm_array) self.assertEqual(closest_pair_index, 0) self.assertEqual(furthest_pair_index, 2)
def test_calc_mahalanobis(self): rdm = rsr.calc_rdm_unbalanced( self.test_data, descriptor='conds', method='mahalanobis') assert rdm.n_cond == 6 rdm_bal = rsr.calc_rdm_unbalanced( self.test_data, descriptor='conds', method='mahalanobis') rdm_check = rsr.calc_rdm( self.test_data, descriptor='conds', method='mahalanobis') assert_array_almost_equal( rdm_bal.dissimilarities.flatten(), rdm_check.dissimilarities.flatten() )
def test_calc_correlation(self, _parse_input): from rsatoolbox.rdm import calc_rdm data = Mock() data.descriptors = {'session': 0, 'subj': 0} data.measurements = np.random.rand(6, 5) desc = [0, 1, 2, 3, 4, 5] _parse_input.return_value = (data.measurements, desc, 'conds') rdm_expected = 1 - np.corrcoef(data.measurements) rdme = rsr.RDMs(dissimilarities=np.array([rdm_expected]), dissimilarity_measure='correlation', descriptors=data.descriptors) rdm = calc_rdm(data, descriptor='conds', method='correlation') self.assertIsNone( assert_array_almost_equal(rdme.dissimilarities.flatten(), rdm.dissimilarities.flatten()))
def test_calc_crossnobis_noise(self): noise = np.random.randn(10, 5) noise = np.matmul(noise.T, noise) rdm = rsr.calc_rdm_unbalanced(self.test_data, descriptor='conds', cv_descriptor='fold', noise=noise, method='crossnobis') assert rdm.n_cond == 6 rdm_bal = rsr.calc_rdm_unbalanced( self.test_data_balanced, descriptor='conds', cv_descriptor='fold', noise=noise, method='crossnobis') rdm_check = rsr.calc_rdm( self.test_data_balanced, descriptor='conds', cv_descriptor='fold', noise=noise, method='crossnobis') assert_array_almost_equal( rdm_bal.dissimilarities.flatten(), rdm_check.dissimilarities.flatten() )
def test_calc_euclid_nconds(self): d = self.test_data rdm = rsr.calc_rdm([d, d], descriptor='conds', method='euclidean') assert rdm.n_cond == 6
def test_calc_poisson_cv(self): rdm = rsr.calc_rdm(self.test_data, descriptor='conds', cv_descriptor='fold', method='poisson_cv') assert rdm.n_cond == 6
def test_calc_poisson_6_conditions(self): rdm = rsr.calc_rdm(self.test_data, descriptor='conds', method='poisson') assert rdm.n_cond == 6
def test_calc_list_descriptors(self): rdm = rsr.calc_rdm([self.test_data, self.test_data, self.test_data], descriptor='conds', method='euclidean') assert np.all(rdm.rdm_descriptors['subj'] == np.array([0, 0, 0]))