Check variance accounted for by each component in supplied data. This function is only used for sorting the components. Parameters ---------- ica : ICA Instance of `mne.preprocessing.ICA`. inst : Raw | Epochs | Evoked Data to explain with ICA. Instance of Raw, E
(ica, inst, normalize=False)
| 2841 | |
| 2842 | |
| 2843 | def _ica_explained_variance(ica, inst, normalize=False): |
| 2844 | """Check variance accounted for by each component in supplied data. |
| 2845 | |
| 2846 | This function is only used for sorting the components. |
| 2847 | |
| 2848 | Parameters |
| 2849 | ---------- |
| 2850 | ica : ICA |
| 2851 | Instance of `mne.preprocessing.ICA`. |
| 2852 | inst : Raw | Epochs | Evoked |
| 2853 | Data to explain with ICA. Instance of Raw, Epochs or Evoked. |
| 2854 | normalize : bool |
| 2855 | Whether to normalize the variance. |
| 2856 | |
| 2857 | Returns |
| 2858 | ------- |
| 2859 | var : array |
| 2860 | Variance explained by each component. |
| 2861 | """ |
| 2862 | # check if ica is ICA and whether inst is Raw or Epochs |
| 2863 | if not isinstance(ica, ICA): |
| 2864 | raise TypeError("first argument must be an instance of ICA.") |
| 2865 | if not isinstance(inst, BaseRaw | BaseEpochs | Evoked): |
| 2866 | raise TypeError( |
| 2867 | "second argument must an instance of either Raw, Epochs or Evoked." |
| 2868 | ) |
| 2869 | |
| 2870 | source_data = _get_inst_data(ica.get_sources(inst)) |
| 2871 | |
| 2872 | # if epochs - reshape to channels x timesamples |
| 2873 | if isinstance(inst, BaseEpochs): |
| 2874 | n_epochs, n_chan, n_samp = source_data.shape |
| 2875 | source_data = source_data.transpose(1, 0, 2).reshape( |
| 2876 | (n_chan, n_epochs * n_samp) |
| 2877 | ) |
| 2878 | |
| 2879 | n_chan, n_samp = source_data.shape |
| 2880 | var = ( |
| 2881 | np.sum(ica.mixing_matrix_**2, axis=0) |
| 2882 | * np.sum(source_data**2, axis=1) |
| 2883 | / (n_chan * n_samp - 1) |
| 2884 | ) |
| 2885 | if normalize: |
| 2886 | var /= var.sum() |
| 2887 | return var |
| 2888 | |
| 2889 | |
| 2890 | def _sort_components(ica, order, copy=True): |