Compute morph matrix.
(
subject_from,
subject_to,
vertices_from,
vertices_to,
smooth=None,
subjects_dir=None,
warn=True,
xhemi=False,
)
| 1173 | |
| 1174 | |
| 1175 | def _compute_morph_matrix( |
| 1176 | subject_from, |
| 1177 | subject_to, |
| 1178 | vertices_from, |
| 1179 | vertices_to, |
| 1180 | smooth=None, |
| 1181 | subjects_dir=None, |
| 1182 | warn=True, |
| 1183 | xhemi=False, |
| 1184 | ): |
| 1185 | """Compute morph matrix.""" |
| 1186 | logger.info("Computing morph matrix...") |
| 1187 | subjects_dir = get_subjects_dir(subjects_dir, raise_error=True) |
| 1188 | |
| 1189 | tris = _get_subject_sphere_tris(subject_from, subjects_dir) |
| 1190 | maps = read_morph_map(subject_from, subject_to, subjects_dir, xhemi) |
| 1191 | |
| 1192 | # morph the data |
| 1193 | |
| 1194 | morpher = [] |
| 1195 | for hemi_to in range(2): # iterate over to / block-rows of CSR matrix |
| 1196 | hemi_from = (1 - hemi_to) if xhemi else hemi_to |
| 1197 | morpher.append( |
| 1198 | _hemi_morph( |
| 1199 | tris[hemi_from], |
| 1200 | vertices_to[hemi_to], |
| 1201 | vertices_from[hemi_from], |
| 1202 | smooth, |
| 1203 | maps[hemi_from], |
| 1204 | warn, |
| 1205 | ) |
| 1206 | ) |
| 1207 | |
| 1208 | shape = (sum(len(v) for v in vertices_to), sum(len(v) for v in vertices_from)) |
| 1209 | data = [m.data for m in morpher] |
| 1210 | indices = [m.indices.copy() for m in morpher] |
| 1211 | indptr = [m.indptr.copy() for m in morpher] |
| 1212 | # column indices need to be adjusted |
| 1213 | indices[0 if xhemi else 1] += len(vertices_from[0]) |
| 1214 | indices = np.concatenate(indices) |
| 1215 | # row index pointers need to be adjusted |
| 1216 | indptr[1] = indptr[1][1:] + len(data[0]) |
| 1217 | indptr = np.concatenate(indptr) |
| 1218 | # data does not need to be adjusted |
| 1219 | data = np.concatenate(data) |
| 1220 | # this is equivalent to morpher = sparse_block_diag(morpher).tocsr(), |
| 1221 | # but works for xhemi mode |
| 1222 | morpher = sparse.csr_array((data, indices, indptr), shape=shape) |
| 1223 | logger.info("[done]") |
| 1224 | return morpher |
| 1225 | |
| 1226 | |
| 1227 | def _hemi_morph(tris, vertices_to, vertices_from, smooth, maps, warn): |
no test coverage detected