-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make coordinate display work for NIFTI files
- Loading branch information
Showing
2 changed files
with
47 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,36 @@ | ||
import numpy as np | ||
|
||
from astropy.wcs import WCS | ||
from glue.core.coordinates import Coordinates | ||
|
||
class Coordinates4DMatrix(Coordinates): | ||
|
||
def __init__(self, matrix, axis_labels): | ||
|
||
# Other code to translate header into labels depending on file format. | ||
|
||
self.matrix = matrix | ||
self.matrix_invert = np.linalg.inv(matrix) | ||
self.axis_labels = axis_labels | ||
|
||
def axis_label(self, axis): | ||
return self.axis_labels[axis] | ||
class Coordinates4DMatrix(Coordinates): | ||
|
||
def pixel2world(self, *args): | ||
def __init__(self, matrix, axis_labels): | ||
|
||
return np.matmul(self.matrix, args + (np.zeros_like(args[0]),))[:-1] | ||
# Other code to translate header into labels depending on file format. | ||
|
||
def world2pixel(self, *args): | ||
self.matrix = matrix | ||
self.matrix_invert = np.linalg.inv(matrix) | ||
self.axis_labels = axis_labels | ||
|
||
return np.matmul(self.matrix_invert, args + (np.zeros_like(args[0]),))[:-1] | ||
def axis_label(self, axis): | ||
return self.axis_labels[axis] | ||
|
||
def pixel2world(self, *args): | ||
return np.matmul(self.matrix, args + (np.zeros_like(args[0]),))[:-1] | ||
|
||
def world2pixel(self, *args): | ||
return np.matmul(self.matrix_invert, args + (np.zeros_like(args[0]),))[:-1] | ||
|
||
@property | ||
def wcs(self): | ||
# We provide a wcs property since this can then be used by glue | ||
# to display world coordinates. In this case, the transformation matrix | ||
# is in the same order as the WCS convention so we don't need to swap | ||
# anything. | ||
wcs = WCS(naxis=self.matrix.shape[0] - 1) | ||
wcs.wcs.cd = self.matrix[:-1, :-1] | ||
wcs.wcs.crpix = np.zeros(wcs.naxis) | ||
wcs.wcs.crval = self.matrix[:-1, -1] | ||
wcs.wcs.ctype = self.axis_labels[::-1] | ||
return wcs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters