Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GSOC] Add support for multiple components of multivariate connectivity #213

Merged
merged 67 commits into from
Jul 24, 2024

Conversation

tsbinns
Copy link
Collaborator

@tsbinns tsbinns commented Jul 10, 2024

General idea described in #182 & #183.

So far support is implemented for multiple components of CaCoh and MIC in the decomposition class.
WIP for the spectral_connectivity_... functions.

Comment on lines 712 to 728
if self.method in ["cacoh", "mic", "mim", "gc", "gc_tr"]:
# multivariate results cannot be returned in a dense form as a
# single set of results would correspond to multiple entries in
# the matrix, and there could also be cases where multiple
# results correspond to the same entries in the matrix.
if (
isinstance(self.indices, tuple)
and not np.all(isinstance(self.indices[0], int))
and not np.all(isinstance(self.indices[1], int))
): # i.e. check if multivariate results based on nested indices
# multivariate results cannot be returned in a dense form as a single
# set of results would correspond to multiple entries in the matrix, and
# there could also be cases where multiple results correspond to the
# same entries in the matrix.
raise ValueError(
"cannot return multivariate connectivity data in a dense form"
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When updating the container unit tests I realised using the indices format is more a consistent check for multivariate conn scores rather than the method name

Comment on lines -329 to +340
# set n_components if necessary / check it is valid for the rank
if self.n_components is None:
self.n_components = np.min(rank)
elif self.n_components > np.min(rank):
raise ValueError(
"`n_components` is greater than the minimum rank of the data"
)

# set rank if necessary
if self._rank is None:
self._rank = rank
# check n_components is valid for the rank
self.n_components = _check_n_components_input(self.n_components, self._rank)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little refactoring change since this check is required also in spec_conn_epochs and spec_conn_time now

@@ -480,113 +480,102 @@ def test_spectral_connectivity(method, mode):


@pytest.mark.parametrize("method", ["cacoh", "mic", "mim", _gc])
def test_spectral_connectivity_epochs_multivariate(method):
@pytest.mark.parametrize("n_components", [1, 2, 3, None])
def test_spectral_connectivity_epochs_multivariate(method, n_components):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to adding checks for multiple comps in this spec_conn_epochs unit test, I went ahead with some other improvements to clean up the code and make it generally more extensive.

Also, the simulated data being used here was a bit limiting to what we could test with multiple comps, so I switched from the old data simulation to the new make_signals_in_freq_bands() func, so ticks off some maintenance stuff too.

Comment on lines -117 to +126
bad_numpy_input = np.zeros((3, 3, 4, 5))
bad_numpy_input = np.zeros((3, 3, 4, 5, 6))
bad_indices = ([1, 0], [2])

if conn_cls.is_epoched:
bad_numpy_input = np.zeros((3, 3, 3, 4, 5))
bad_numpy_input = np.zeros((3, 3, 3, 4, 5, 6))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For container tests, new comps dim increases the number of possible dimensions

@tsbinns
Copy link
Collaborator Author

tsbinns commented Jul 16, 2024

Multiple comps now also supported for spec_conn_time, so just need to figure out how best to update the plot_conn_circle function discussed with @larsoner in call that plotting functions are unaffected by current changes (they only accept 2D (chan x freq) arrays anyway).

Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
@larsoner
Copy link
Member

Sounds good, let me know when it's ready for another look!

@tsbinns
Copy link
Collaborator Author

tsbinns commented Jul 19, 2024

@larsoner given what we discussed on Weds about the plotting functionality not actually being impacted by these changes, I think everything we sought to address in this PR is here.

So pending a review she is hopefully good to go! (I added comments throughout to code with short descriptions of the changes to try and make things less obtuse)

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just minor stuff from me, @wmvanvliet did you want to look or happy if I merge once my last comments are addressed?

mne_connectivity/base.py Outdated Show resolved Hide resolved
mne_connectivity/decoding/tests/test_decomposition.py Outdated Show resolved Hide resolved
mne_connectivity/decoding/tests/test_decomposition.py Outdated Show resolved Hide resolved
mne_connectivity/spectral/epochs.py Show resolved Hide resolved
@wmvanvliet
Copy link
Collaborator

Looked it over and I am happy with this. Go ahead and merge!

@larsoner
Copy link
Member

Thanks @tsbinns !

@larsoner larsoner merged commit 2eb8f02 into mne-tools:main Jul 24, 2024
12 checks passed
@tsbinns tsbinns deleted the decoding_ncomps branch July 24, 2024 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants