|
2 | 2 |
|
3 | 3 | See demo_meg_mne for an example.
|
4 | 4 | """
|
5 |
| -# pylint: disable=too-many-statements,unused-argument |
| 5 | +# pylint: disable=too-many-statements,unused-argument,too-many-locals |
6 | 6 | from __future__ import annotations
|
7 |
| -from typing import TYPE_CHECKING, Tuple, List, Optional |
| 7 | +from typing import TYPE_CHECKING, Tuple, List, Optional, Dict |
8 | 8 | import matplotlib.pyplot as plt
|
9 | 9 | import numpy as np
|
10 | 10 | if TYPE_CHECKING:
|
@@ -57,22 +57,8 @@ def plot_timecourse(
|
57 | 57 | n_dissimilarity_elements = rdms_data.dissimilarities.shape[1]
|
58 | 58 |
|
59 | 59 | # color mapping from colored conditions
|
60 |
| - if colored_conditions is not None: |
61 |
| - if plot_individual_dissimilarities is None: |
62 |
| - plot_individual_dissimilarities = False |
63 |
| - sf_conds = [[{c1, c2} for c1 in colored_conditions] for c2 in colored_conditions] |
64 |
| - pairwise_conds = unsquareform(np.array(sf_conds)) |
65 |
| - pairwise_conds_unique = np.unique(pairwise_conds) |
66 |
| - color_index = {} |
67 |
| - for x in pairwise_conds_unique: |
68 |
| - if len(list(x))==2: |
69 |
| - key = f'{list(x)[0]} vs {list(x)[1]}' |
70 |
| - else: |
71 |
| - key = f'{list(x)[0]} vs {list(x)[0]}' |
72 |
| - color_index[key] = pairwise_conds==x |
73 |
| - else: |
74 |
| - color_index = {'': np.array([True]*n_dissimilarity_elements)} |
75 |
| - plot_individual_dissimilarities = True |
| 60 | + plot_individual_dissimilarities, color_index = _map_colors( |
| 61 | + colored_conditions, plot_individual_dissimilarities, rdms_data) |
76 | 62 |
|
77 | 63 | colors = plt.get_cmap('turbo')(np.linspace(0, 1, len(color_index)+1))
|
78 | 64 |
|
@@ -158,3 +144,29 @@ def unsquareform(a: NDArray) -> NDArray:
|
158 | 144 | """Helper function; convert squareform to vector
|
159 | 145 | """
|
160 | 146 | return a[np.nonzero(np.triu(a, k=1))]
|
| 147 | + |
| 148 | + |
| 149 | +def _map_colors( |
| 150 | + colored_conditions: Optional[list], |
| 151 | + plot_individual_dissimilarities: Optional[bool], |
| 152 | + rdms: RDMs |
| 153 | + ) -> Tuple[bool, Dict[str, NDArray]]: |
| 154 | + n_dissimilarity_elements = rdms.dissimilarities.shape[1] |
| 155 | + # color mapping from colored conditions |
| 156 | + if colored_conditions is not None: |
| 157 | + if plot_individual_dissimilarities is None: |
| 158 | + plot_individual_dissimilarities = False |
| 159 | + sf_conds = [[{c1, c2} for c1 in colored_conditions] for c2 in colored_conditions] |
| 160 | + pairwise_conds = unsquareform(np.array(sf_conds)) |
| 161 | + pairwise_conds_unique = np.unique(pairwise_conds) |
| 162 | + color_index = {} |
| 163 | + for x in pairwise_conds_unique: |
| 164 | + if len(list(x))==2: |
| 165 | + key = f'{list(x)[0]} vs {list(x)[1]}' |
| 166 | + else: |
| 167 | + key = f'{list(x)[0]} vs {list(x)[0]}' |
| 168 | + color_index[key] = pairwise_conds==x |
| 169 | + else: |
| 170 | + color_index = {'': np.array([True]*n_dissimilarity_elements)} |
| 171 | + plot_individual_dissimilarities = True |
| 172 | + return plot_individual_dissimilarities, color_index |
0 commit comments