implemented one version of crossnobis with different noise estimates
@@ -195,14 +195,14 @@ def calc_rdm_crossnobis(dataset, descriptor, noise=None,
cv_folds = np.unique(np.array(dataset.obs_descriptors[cv_descriptor]))
weights = []
rdms = []
+ if noise is None or (isinstance(noise, np.ndarray) and noise.ndim==2):
for i_fold in range(len(cv_folds)):
fold = cv_folds[i_fold]
- data_train = dataset.subset_obs(cv_descriptor, fold)
- data_test = dataset.subset_obs(cv_descriptor,
+ data_test = dataset.subset_obs(cv_descriptor, fold)
+ data_train = dataset.subset_obs(cv_descriptor,
np.setdiff1d(cv_folds, fold))
- measurements_train, desc = average_dataset_by(data_train, descriptor)
- measurements_test, desc = average_dataset_by(data_test, descriptor)
- if noise is None or (isinstance(noise, np.ndarray) and noise.ndim==2):
+ measurements_train, _ = average_dataset_by(data_train, descriptor)
+ measurements_test, _ = average_dataset_by(data_test, descriptor)
n_cond = measurements_train.shape[0]
rdm = np.empty(int(n_cond * (n_cond-1) / 2))
k = 0
@@ -218,12 +218,23 @@ def calc_rdm_crossnobis(dataset, descriptor, noise=None,
rdm[k] = np.sum(diff_train
* np.matmul(noise, diff_test))
k += 1
- else:
- rdm = _calc_rdm_crossnobis_single(measurements_train,
- measurements_test,
- noise[i_fold])
rdms.append(rdm)
weights.append(data_test.n_obs)
+ else: # a list of noises was provided
+ measurements = []
+ w_fold = []
+ for i_fold in range(len(cv_folds)):
+ data = dataset.subset_obs(cv_descriptor, cv_folds[i_fold])
+ measurements.append(average_dataset_by(data, descriptor)[0])
+ w_fold.append(data.n_obs)
+ for i_fold in range(len(cv_folds)):
+ for j_fold in range(len(cv_folds)):
+ if i_fold != j_fold:
+ rdm = _calc_rdm_crossnobis_single(measurements[i_fold],
+ measurements[j_fold],
+ noise[j_fold])
+ rdms.append(rdm)
+ weights.append(w_fold[i_fold] * w_fold[j_fold])
rdms = np.array(rdms)
weights = np.array(weights)
rdm = np.einsum('ij,i->j', rdms, weights) / np.sum(weights)
@@ -233,6 +244,7 @@ def calc_rdm_crossnobis(dataset, descriptor, noise=None,
if descriptor is None:
rdm.pattern_descriptors['pattern'] = np.arange(rdm.n_cond)
else:
+ _, desc = average_dataset_by(dataset, descriptor)
rdm.pattern_descriptors[descriptor] = desc
rdm.descriptors['noise'] = noise
rdm.descriptors['cv_descriptor'] = cv_descriptor
