(self, vols, mesg, subselect=True)
| 606 | return self |
| 607 | |
| 608 | def _morph_vols(self, vols, mesg, subselect=True): |
| 609 | from dipy.align.reslice import reslice |
| 610 | |
| 611 | interp = self.src_data["interpolator"].tocsc()[ |
| 612 | :, np.concatenate(self._vol_vertices_from) |
| 613 | ] |
| 614 | n_vols = interp.shape[1] if vols is None else vols.shape[1] |
| 615 | attrs = ("real", "imag") if np.iscomplexobj(vols) else ("real",) |
| 616 | dtype = np.complex128 if len(attrs) == 2 else np.float64 |
| 617 | if vols is None: # sparse -> sparse mode |
| 618 | img_to = (list(), list(), [0]) # data, indices, indptr |
| 619 | assert subselect |
| 620 | else: # dense -> dense mode |
| 621 | img_to = None |
| 622 | if subselect: |
| 623 | vol_verts = np.concatenate(self._vol_vertices_to) |
| 624 | else: |
| 625 | vol_verts = slice(None) |
| 626 | # morph data |
| 627 | from_affine = np.dot( |
| 628 | self.src_data["src_affine_ras"], # mri_ras_t |
| 629 | self.src_data["src_affine_vox"], |
| 630 | ) # vox_mri_t |
| 631 | from_affine[:3] *= 1000.0 |
| 632 | # equivalent of: |
| 633 | # _resample_from_to(img_real, from_affine, |
| 634 | # (self.pre_affine.codomain_shape, |
| 635 | # (self.pre_affine.codomain_grid2world)) |
| 636 | src_shape = self.src_data["src_shape_full"][::-1] |
| 637 | resamp_0 = _grid_interp( |
| 638 | src_shape, |
| 639 | self.pre_affine.codomain_shape, |
| 640 | np.linalg.inv(from_affine) @ self.pre_affine.codomain_grid2world, |
| 641 | ) |
| 642 | # reslice to match what was used during the morph |
| 643 | # (brain.mgz and whatever was used to create the source space |
| 644 | # will not necessarily have the same domain/zooms) |
| 645 | # equivalent of: |
| 646 | # pre_affine.transform(img_real) |
| 647 | resamp_1 = _grid_interp( |
| 648 | self.pre_affine.codomain_shape, |
| 649 | self.pre_affine.domain_shape, |
| 650 | np.linalg.inv(self.pre_affine.codomain_grid2world) |
| 651 | @ self.pre_affine.affine |
| 652 | @ self.pre_affine.domain_grid2world, |
| 653 | ) |
| 654 | resamp_0_1 = resamp_1 @ resamp_0 |
| 655 | resamp_2 = None |
| 656 | for ii in ProgressBar(list(range(n_vols)), mesg=mesg): |
| 657 | for attr in attrs: |
| 658 | # transform from source space to mri_from resolution/space |
| 659 | if vols is None: |
| 660 | img_real = interp[:, [ii]] |
| 661 | else: |
| 662 | img_real = interp @ getattr(vols[:, ii], attr) |
| 663 | _debug_img(img_real, from_affine, "From", src_shape) |
| 664 | |
| 665 | img_real = resamp_0_1 @ img_real |
no test coverage detected