rsagroup / rsatoolbox

Python library for Representational Similarity Analysis
MIT License
187 stars 37 forks source link

calc_rdm_crossnobis indexing shape mismatch #343

Closed JasperVanDenBosch closed 3 months ago

JasperVanDenBosch commented 1 year ago

I suspect that it's to do with the number of repetitions varying across conditions. I'll try to add a warning here.

Versions python 3.11 rsatoolbox 0.1.4

stack trace

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 1
----> 1 data_rdms = calc_rdm(subject_datasets, method='crossnobis', descriptor='trial_type')

File ~/projects/rsatoolbox/env/lib/python3.11/site-packages/rsatoolbox/rdm/calc.py:52, in calc_rdm(dataset, method, descriptor, noise, cv_descriptor, prior_lambda, prior_weight)
     50 for i_dat, ds_i in enumerate(dataset):
     51     if noise is None:
---> 52         rdms.append(calc_rdm(
     53             ds_i, method=method,
     54             descriptor=descriptor,
     55             cv_descriptor=cv_descriptor,
     56             prior_lambda=prior_lambda, prior_weight=prior_weight))
     57     elif isinstance(noise, np.ndarray) and noise.ndim == 2:
     58         rdms.append(calc_rdm(
     59             ds_i, method=method,
     60             descriptor=descriptor,
     61             noise=noise,
     62             cv_descriptor=cv_descriptor,
     63             prior_lambda=prior_lambda, prior_weight=prior_weight))

File ~/projects/rsatoolbox/env/lib/python3.11/site-packages/rsatoolbox/rdm/calc.py:83, in calc_rdm(dataset, method, descriptor, noise, cv_descriptor, prior_lambda, prior_weight)
     81     rdm = calc_rdm_mahalanobis(dataset, descriptor, noise)
     82 elif method == 'crossnobis':
---> 83     rdm = calc_rdm_crossnobis(dataset, descriptor, noise,
     84                               cv_descriptor)
     85 elif method == 'poisson':
     86     rdm = calc_rdm_poisson(dataset, descriptor,
     87                            prior_lambda=prior_lambda,
     88                            prior_weight=prior_weight)

File ~/projects/rsatoolbox/env/lib/python3.11/site-packages/rsatoolbox/rdm/calc.py:300, in calc_rdm_crossnobis(dataset, descriptor, noise, cv_descriptor)
    298 datasetCopy = deepcopy(dataset)
    299 if cv_descriptor is None:
--> 300     cv_desc = _gen_default_cv_descriptor(datasetCopy, descriptor)
    301     datasetCopy.obs_descriptors['cv_desc'] = cv_desc
    302     cv_descriptor = 'cv_desc'

File ~/projects/rsatoolbox/env/lib/python3.11/site-packages/rsatoolbox/rdm/calc.py:449, in _gen_default_cv_descriptor(dataset, descriptor)
    447 cv_descriptor = np.zeros_like(desc)
    448 for i_val in values:
--> 449     cv_descriptor[desc == i_val] = np.arange(n_repeats)
    450 return cv_descriptor

ValueError: shape mismatch: value array of shape (6,) could not be broadcast to indexing result of shape (0,204)
JasperVanDenBosch commented 1 year ago

Line 449 of _gen_default_cv_descriptor assumes that the descriptor is a numpy array:

cv_descriptor[desc == i_val] = np.arange(n_repeats)

I'll add a lazy cast