Determine hemisphere and view layout based user input
(lh, rh, layout, views, mirror=False)
| 25 | |
| 26 | |
| 27 | def _set_layout(lh, rh, layout, views, mirror=False): |
| 28 | """Determine hemisphere and view layout based user input""" |
| 29 | valid_layouts = ['grid', 'row', 'column'] |
| 30 | if layout not in valid_layouts: |
| 31 | raise ValueError(f'layout must be one of {valid_layouts}') |
| 32 | |
| 33 | if isinstance(views, str): |
| 34 | views = [views] |
| 35 | valid_views = ['medial', 'lateral', 'ventral', 'dorsal', 'anterior', |
| 36 | 'posterior'] |
| 37 | if not set(views) <= set(valid_views): |
| 38 | raise ValueError(f'layout must be one of {valid_views}') |
| 39 | |
| 40 | n_hemi = len([x for x in [lh, rh] if x is not None]) |
| 41 | n_views = len(views) |
| 42 | |
| 43 | # create view (v) and hemisphere (h) matrices for plotting layout |
| 44 | v, h = np.array([], dtype=object), np.array([], dtype=object) |
| 45 | if lh is not None: |
| 46 | v = np.concatenate([v, np.array(views)]) |
| 47 | h = np.concatenate([h, np.array(['left'] * n_views)]) |
| 48 | if rh is not None: |
| 49 | # flip medial/lateral |
| 50 | view_key = dict(medial='lateral', lateral='medial', dorsal='dorsal', |
| 51 | ventral='ventral', anterior='anterior', |
| 52 | posterior='posterior') |
| 53 | |
| 54 | # determine view order |
| 55 | if mirror and (layout != 'grid') and (lh is not None): |
| 56 | rh_views = [view_key[i] for i in reversed(views)] |
| 57 | else: |
| 58 | rh_views = [view_key[i] for i in views] |
| 59 | |
| 60 | v = np.concatenate([v, np.array(rh_views)]) |
| 61 | h = np.concatenate([h, np.array(['right'] * n_views)]) |
| 62 | |
| 63 | if layout == 'grid': |
| 64 | v = v.reshape(n_hemi, n_views).T |
| 65 | h = h.reshape(n_hemi, n_views).T |
| 66 | elif layout == 'column': |
| 67 | v = v.reshape(v.shape[0], 1) |
| 68 | h = h.reshape(h.shape[0], 1) |
| 69 | |
| 70 | # flatten if applicable (nb: grid layout with 1 hemi is a row) |
| 71 | if ((n_hemi == 1) or (n_views == 1)) and (layout != 'column'): |
| 72 | v = v.ravel() |
| 73 | h = h.ravel() |
| 74 | |
| 75 | return v.tolist(), h.tolist() |
| 76 | |
| 77 | |
| 78 | def _flip_hemispheres(v, h): |