def test_odd_even_split_nested(self): measurements = np.random.rand(16, 10) des = {'session': 0, 'subj': 0} chn_des = {'rois': np.array([chr(l) for l in range(65, 75)])} conds = np.array([str(i) for i in range(1, 5)]) runs = np.array([i for i in range(1, 5)]) self.full_data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors={ 'conds': np.hstack( (conds, conds, conds, conds)), 'runs': np.repeat(runs, 4) }, channel_descriptors=chn_des) self.odd_data = rsd.Dataset(measurements=np.append(measurements[0:4], measurements[8:12], axis=0), descriptors=des, obs_descriptors={ 'conds': np.hstack((conds, conds)), 'runs': np.repeat(runs[0::2], 4) }, channel_descriptors=chn_des) self.even_data = rsd.Dataset(measurements=np.append( measurements[4:8], measurements[12:16], axis=0), descriptors=des, obs_descriptors={ 'conds': np.hstack((conds, conds)), 'runs': np.repeat(runs[1::2], 4) }, channel_descriptors=chn_des) self.odd_split, self.even_split = self.full_data.nested_odd_even_split( 'conds', 'runs') self.odd_split.sort_by('runs') self.even_split.sort_by('runs') np.testing.assert_array_equal(self.odd_data.measurements, self.odd_split.measurements) self.assertEqual(self.odd_data.descriptors, self.odd_split.descriptors) np.testing.assert_array_equal(self.odd_data.obs_descriptors['conds'], self.odd_split.obs_descriptors['conds']) np.testing.assert_array_equal( self.odd_data.channel_descriptors['rois'], self.odd_split.channel_descriptors['rois']) np.testing.assert_array_equal(self.even_data.measurements, self.even_split.measurements) self.assertEqual(self.even_data.descriptors, self.even_split.descriptors) np.testing.assert_array_equal(self.even_data.obs_descriptors['conds'], self.even_split.obs_descriptors['conds']) np.testing.assert_array_equal( self.even_data.channel_descriptors['rois'], self.even_split.channel_descriptors['rois'])
def test_oe_split(self): measurements = np.random.rand(4, 10) des = {'session': 0, 'subj': 0} chn_des = {'rois': np.array([chr(l) for l in range(65, 75)])} self.full_data = rsd.Dataset( measurements=measurements, descriptors=des, obs_descriptors={'conds': np.array([str(i) for i in range(1, 5)])}, channel_descriptors=chn_des) self.odd_data = rsd.Dataset(measurements=measurements[0::2], descriptors=des, obs_descriptors={ 'conds': np.array( [str(i) for i in range(1, 5, 2)]) }, channel_descriptors=chn_des) self.even_data = rsd.Dataset(measurements=measurements[1::2], descriptors=des, obs_descriptors={ 'conds': np.array( [str(i) for i in range(2, 5, 2)]) }, channel_descriptors=chn_des) self.odd_split, self.even_split = \ self.full_data.odd_even_split('conds') np.testing.assert_array_equal(self.odd_data.measurements, self.odd_split.measurements) self.assertEqual(self.odd_data.descriptors, self.odd_split.descriptors) np.testing.assert_array_equal(self.odd_data.obs_descriptors['conds'], self.odd_split.obs_descriptors['conds']) np.testing.assert_array_equal( self.odd_data.channel_descriptors['rois'], self.odd_split.channel_descriptors['rois']) np.testing.assert_array_equal(self.even_data.measurements, self.even_split.measurements) self.assertEqual(self.even_data.descriptors, self.even_split.descriptors) np.testing.assert_array_equal(self.even_data.obs_descriptors['conds'], self.even_split.obs_descriptors['conds']) np.testing.assert_array_equal( self.even_data.channel_descriptors['rois'], self.even_split.channel_descriptors['rois'])
def setUp(self): measurements = np.random.rand(4, 10) des = {'session': 0, 'subj': 0} obs_des = {'conds': np.array([str(i) for i in range(1, 5)])} chn_des = {'rois': np.array([chr(l) for l in range(65, 75)])} self.test_data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des)
def setUp(self): measurements = np.random.rand(10, 5) des = {'session': 0, 'subj': 0} obs_des = {'conds': np.array([0, 0, 1, 1, 2, 2, 2, 3, 4, 5])} chn_des = {'rois': np.array(['V1', 'V1', 'IT', 'IT', 'V4'])} self.test_data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des)
def setUp(self): self.residuals = np.random.rand(100, 25) self.residuals = self.residuals - np.mean( self.residuals, axis=0, keepdims=True) res_list = [] for i in range(3): residuals = np.random.rand(100, 25) residuals = residuals - np.mean(residuals, axis=0, keepdims=True) res_list.append(residuals) self.res_list = res_list self.dataset = rsd.Dataset( self.residuals, obs_descriptors={'obs': np.repeat(np.arange(10), 10)})
def test_dataset_split_channel(self): measurements = np.zeros((10, 5)) des = {'session': 0, 'subj': 0} obs_des = {'conds': np.array([0, 0, 1, 1, 2, 2, 2, 3, 4, 5])} chn_des = {'rois': np.array(['V1', 'V1', 'IT', 'IT', 'V4'])} data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) splited_list = data.split_channel('rois') self.assertEqual(len(splited_list), 3) self.assertEqual(splited_list[0].n_obs, 10) self.assertEqual(splited_list[2].n_obs, 10) self.assertEqual(splited_list[0].n_channel, 2) self.assertEqual(splited_list[2].n_channel, 1) self.assertEqual(splited_list[1].channel_descriptors['rois'][0], 'IT')
def test_dataset_full_init(self): measurements = np.zeros((10, 5)) des = {'session': 0, 'subj': 0} obs_des = { 'conds': np.array(['cond_' + str(x) for x in np.arange(10)]) } chn_des = {'rois': np.array(['roi_' + str(x) for x in np.arange(5)])} data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) self.assertEqual(data.n_obs, 10) self.assertEqual(data.n_channel, 5) self.assertEqual(data.descriptors, des) self.assertEqual(data.obs_descriptors, obs_des) self.assertEqual(data.channel_descriptors, chn_des)
def setUp(self): self.full_str = "slight not what is near through aiming at what is far" self.unique_str = "slight not what is near through aiming at far" self.full_ints = np.array([99, 4, 66, 4, 33, 99]) self.unique_ints = np.array([99, 4, 66, 33]) measurements = np.zeros((4, 5)) des = {'session': 0, 'subj': 0} obs_des = { 'conds': np.array(['cond_foo', 'cond_bar', 'cond_foo', 'cond_bar']) } chn_des = {'rois': np.array(['V1', 'V1', 'IT', 'IT', 'V4'])} self.data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des)
def test_dataset_subset_obs(self): measurements = np.zeros((10, 5)) des = {'session': 0, 'subj': 0} obs_des = {'conds': np.array([0, 0, 1, 1, 2, 2, 2, 3, 4, 5])} chn_des = {'rois': np.array(['V1', 'V1', 'IT', 'IT', 'V4'])} data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) subset = data.subset_obs(by='conds', value=2) self.assertEqual(subset.n_obs, 3) self.assertEqual(subset.n_channel, 5) self.assertEqual(subset.obs_descriptors['conds'][0], 2) subset = data.subset_obs(by='conds', value=[2, 3]) self.assertEqual(subset.n_obs, 4) self.assertEqual(subset.n_channel, 5) self.assertEqual(subset.obs_descriptors['conds'][0], 2)
def test_dict_conversion(self): measurements = np.zeros((10, 5)) des = {'session': 0, 'subj': 0} obs_des = {'conds': np.array([0, 0, 1, 1, 2, 2, 2, 3, 4, 5])} chn_des = {'rois': np.array(['V1', 'V1', 'IT', 'IT', 'V4'])} data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) data_dict = data.to_dict() data_loaded = rsd.dataset_from_dict(data_dict) assert type(data_loaded) == type(data) assert data_loaded.n_channel == data.n_channel assert np.all(data_loaded.obs_descriptors['conds'] == obs_des['conds']) assert np.all( data_loaded.channel_descriptors['rois'] == chn_des['rois']) assert data_loaded.descriptors['subj'] == 0
def test_save_load(self): import io f = io.BytesIO() # Essentially a Mock file measurements = np.zeros((10, 5)) des = {'session': 0, 'subj': 0} obs_des = {'conds': np.array([0, 0, 1, 1, 2, 2, 2, 3, 4, 5])} chn_des = {'rois': np.array(['V1', 'V1', 'IT', 'IT', 'V4'])} data = rsd.Dataset(measurements=measurements, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) data.save(f, file_type='hdf5') data_loaded = rsd.load_dataset(f, file_type='hdf5') assert data_loaded.n_channel == data.n_channel assert np.all(data_loaded.obs_descriptors['conds'] == obs_des['conds']) assert np.all( data_loaded.channel_descriptors['rois'] == chn_des['rois']) assert data_loaded.descriptors['subj'] == 0
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_dataset_simple_init(self): measurements = np.zeros((10, 5)) data = rsd.Dataset(measurements) self.assertEqual(data.n_obs, 10) self.assertEqual(data.n_channel, 5)
def test_example_dataset(self): import numpy as np import matplotlib.pyplot as plt import rsatoolbox import rsatoolbox.data as rsd # abbreviation to deal with dataset # import the measurements for the dataset measurements = {'simTruePatterns': np.random.randn(92, 100)} measurements = measurements['simTruePatterns'] nCond = measurements.shape[0] nVox = measurements.shape[1] # plot the imported data plt.imshow(measurements, cmap='gray') plt.xlabel('Voxels') plt.ylabel('Conditions') plt.title('Measurements') # 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) print(data) # create an example dataset with random data, subset some conditions nChannel = 50 nObs = 12 randomData = np.random.rand(nObs, nChannel) des = {'session': 1, 'subj': 1} obs_des = {'conds': np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5])} chn_des = { 'voxels': np.array(['voxel_' + str(x) for x in np.arange(nChannel)]) } data = rsd.Dataset(measurements=randomData, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) # select a subset of the dataset: select data only from conditions 0:4 sub_data = data.subset_obs(by='conds', value=[0, 1, 2, 3, 4]) print(sub_data) # Split by channels nChannel = 3 nChannelVox = 10 # three ROIs, each with 10 voxels nObs = 4 randomData = np.random.rand(nObs, nChannel * nChannelVox) des = {'session': 1, 'subj': 1} obs_des = {'conds': np.array([0, 1, 2, 3])} chn_des = ['ROI1', 'ROI2', 'ROI3'] * nChannelVox chn_des = {'ROIs': np.array(chn_des)} data = rsd.Dataset(measurements=randomData, descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des) split_data = data.split_channel(by='ROIs') print(split_data) # create a datasets with random data nVox = 50 # 50 voxels/electrodes/measurement channels nCond = 10 # 10 conditions nSubj = 5 # 5 different subjects randomData = np.random.rand(nCond, nVox, nSubj) # We can then create a list of dataset objects # by appending each dataset for each subject. 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 = [] # list of dataset objects for i in np.arange(nSubj): des = {'session': 1, 'subj': i + 1} # append the dataset object to the data list data.append( rsd.Dataset(measurements=randomData[:, :, 0], descriptors=des, obs_descriptors=obs_des, channel_descriptors=chn_des))