|
27 | 27 | from .spm99analyze import SpmAnalyzeHeader |
28 | 28 | from .casting import have_binary128 |
29 | 29 | from .pydicom_compat import have_dicom, pydicom as pdcm |
30 | | -from .orientations import apply_orientation |
31 | 30 |
|
32 | 31 | # nifti1 flat header definition for Analyze-like first 348 bytes |
33 | 32 | # first number in comments indicates offset in file header in bytes |
@@ -1970,23 +1969,39 @@ def set_sform(self, affine, code=None, **kwargs): |
1970 | 1969 | else: |
1971 | 1970 | self._affine[:] = self._header.get_best_affine() |
1972 | 1971 |
|
1973 | | - def transpose(self, ornt, new_aff): |
1974 | | - # Override the default SpatialImage method so that we update dim_info |
1975 | | - t_arr = apply_orientation(self.get_data(), ornt) |
| 1972 | + def transpose(self, ornt): |
| 1973 | + """Apply an orientation change and return a new image |
| 1974 | +
|
| 1975 | + If image already has orientation, return the original image, unchanged |
| 1976 | +
|
| 1977 | + Parameters |
| 1978 | + ---------- |
| 1979 | + ornt : (n,2) orientation array |
| 1980 | + orientation transform. ``ornt[N,1]` is flip of axis N of the |
| 1981 | + array implied by `shape`, where 1 means no flip and -1 means |
| 1982 | + flip. For example, if ``N==0`` and ``ornt[0,1] == -1``, and |
| 1983 | + there's an array ``arr`` of shape `shape`, the flip would |
| 1984 | + correspond to the effect of ``np.flipud(arr)``. ``ornt[:,0]`` is |
| 1985 | + the transpose that needs to be done to the implied array, as in |
| 1986 | + ``arr.transpose(ornt[:,0])`` |
| 1987 | + """ |
| 1988 | + img = super(Nifti1Pair, self).transpose(ornt) |
| 1989 | + |
| 1990 | + if img is self: |
| 1991 | + return img |
1976 | 1992 |
|
1977 | 1993 | # Also apply the transform to the dim_info fields |
1978 | | - new_hdr = self.header.copy() |
1979 | | - new_dim = list(new_hdr.get_dim_info()) |
| 1994 | + new_dim = list(img.header.get_dim_info()) |
1980 | 1995 | for idx, value in enumerate(new_dim): |
1981 | 1996 | # For each value, leave as None if it was that way, |
1982 | 1997 | # otherwise check where we have mapped it to |
1983 | 1998 | if value is None: |
1984 | 1999 | continue |
1985 | 2000 | new_dim[idx] = np.where(ornt[:, 0] == idx)[0] |
1986 | 2001 |
|
1987 | | - new_hdr.set_dim_info(*new_dim) |
| 2002 | + img.header.set_dim_info(*new_dim) |
1988 | 2003 |
|
1989 | | - return self.__class__(t_arr, new_aff, new_hdr) |
| 2004 | + return img |
1990 | 2005 |
|
1991 | 2006 |
|
1992 | 2007 | class Nifti1Image(Nifti1Pair): |
|
0 commit comments