OHBA-analysis / osl

OHBA Software Library - MEG/EEG Analysis Tools
https://osl.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
39 stars 8 forks source link

Can have too big rank when beamforming #302

Open YonatanGideoni opened 3 months ago

YonatanGideoni commented 3 months ago

Summary: When beamforming, eg. when using beamform_and_parcellate, the given rank can be larger than the covariance matrix' dimensions.

Versions: Ubuntu 20.04 OSL version: 0.6

Expected behaviour: Use covariance mat's rank if given one is too big - this is likely due to numerical issues.

Example logs:

sub-01_ses-1_task-3_meg_preproc_raw : *********************************
sub-01_ses-1_task-3_meg_preproc_raw : * SOURCE RECONSTRUCTION FAILED! *
sub-01_ses-1_task-3_meg_preproc_raw : *********************************
sub-01_ses-1_task-3_meg_preproc_raw : beamform_and_parcellate : <function beamform_and_parcellate at 0x7f76ac9a4f70>
sub-01_ses-1_task-3_meg_preproc_raw : <class 'ValueError'>
sub-01_ses-1_task-3_meg_preproc_raw : Invalid value for the rank parameter (263) given the shape of the input matrix (208 x 208).
  File "/auto/path/code/osl/osl/source_recon/batch.py", line 183, in run_src_chain
    func(src_dir, subject, preproc_file, smri_file, epoch_file, **userargs)
  File "/auto/path/code/osl/osl/source_recon/wrappers.py", line 737, in beamform_and_parcellate
    filters = beamforming.make_lcmv(
  File "/auto/path/code/osl/osl/source_recon/beamforming.py", line 177, in make_lcmv
    filters = _make_lcmv(
  File "<decorator-gen-603>", line 12, in _make_lcmv
  File "/auto/path/code/osl/osl/source_recon/beamforming.py", line 735, in _make_lcmv
    W, max_power_ori = _compute_beamformer(
  File "/auto/path/code/osl/osl/source_recon/beamforming.py", line 843, in _compute_beamformer
    Cm_inv, loading_factor, rank = _reg_pinv(Cm, reg, rank)
  File "path/.conda/envs/pnpl/lib/python3.10/site-packages/mne/utils/numerics.py", line 172, in _reg_pinv
    raise ValueError('Invalid value for the rank parameter (%d) given '

Possible fixes: rank = min(rank, Cm.shape[0])

cgohil8 commented 3 months ago

Adding that line has consequences for the beamforming, which likely will invalid it. It's unlikely we would implement this change.

I suspect your error arises due to a miss match between the channel types used to make the beamformer and the channel types it's applied to, or something similar along those lines.

Can you copy and paste your config.

A better solution might be to add more comprehensive validation of the source reconstruction parameters.