Skip to content

Commit

Permalink
FIX update nibabel deprecated methods (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDLT authored Jul 11, 2022
1 parent 908cc80 commit 876a554
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cortex/anat.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def voxelize(outfile, subject, surf='wm', mp=True):
shape = nib.get_shape()
vox = np.zeros(shape, dtype=bool)
for pts, polys in db.get_surf(subject, surf, nudge=False):
xfm = Transform(np.linalg.inv(nib.get_affine()), nib)
xfm = Transform(np.linalg.inv(nib.affine), nib)
vox += polyutils.voxelize(xfm(pts), polys, shape=shape, center=(0,0,0), mp=mp).astype('bool')

nib = nibabel.Nifti1Image(vox, nib.get_affine(), header=nib.get_header())
nib = nibabel.Nifti1Image(vox, nib.affine, header=nib.header)
nib.to_filename(outfile)

return vox.T
10 changes: 5 additions & 5 deletions cortex/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,18 @@ def save_xfm(self, subject, name, xfm, xfmtype="magnet", reference=None):
import warnings
warnings.warn('You are importing a 4D dataset, automatically selecting the first volume as reference')
data = data[...,0]
out = nibabel.Nifti1Image(data, nib.get_affine(), header=nib.get_header())
out = nibabel.Nifti1Image(data, nib.affine, header=nib.header)
nibabel.save(out, fpath)

jsdict = dict()

nib = nibabel.load(os.path.join(path, "reference.nii.gz"))
if xfmtype == "magnet":
jsdict['magnet'] = np.array(xfm).tolist()
jsdict['coord'] = np.dot(np.linalg.inv(nib.get_affine()), xfm).tolist()
jsdict['coord'] = np.dot(np.linalg.inv(nib.affine), xfm).tolist()
elif xfmtype == "coord":
jsdict['coord'] = np.array(xfm).tolist()
jsdict['magnet'] = np.dot(nib.get_affine(), xfm).tolist()
jsdict['magnet'] = np.dot(nib.affine, xfm).tolist()

files = self.get_paths(subject)
if len(glob.glob(files['masks'].format(xfmname=name, type="*"))) > 0:
Expand Down Expand Up @@ -449,7 +449,7 @@ def get_xfm(self, subject, name, xfmtype="coord"):

if name == "identity":
nib = self.get_anat(subject, 'raw')
return Transform(np.linalg.inv(nib.get_affine()), nib)
return Transform(np.linalg.inv(nib.affine), nib)

fname = os.path.join(self.filestore, subject, "transforms", name, "matrices.xfm")
reference = os.path.join(self.filestore, subject, "transforms", name, "reference.nii.gz")
Expand Down Expand Up @@ -530,7 +530,7 @@ def save_mask(self, subject, xfmname, type, mask):
xfm = self.get_xfm(subject, xfmname)
if xfm.shape != mask.shape:
raise ValueError("Invalid mask shape: must match shape of reference image")
affine = xfm.reference.get_affine()
affine = xfm.reference.affine
nib = nibabel.Nifti1Image(mask.astype(np.uint8).T, affine)
nib.to_filename(fname)

Expand Down
2 changes: 1 addition & 1 deletion cortex/dataset/braindata.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def save_nii(self, filename):
copied from the reference image for this VolumeData's transform.
"""
xfm = db.get_xfm(self.subject, self.xfmname)
affine = xfm.reference.get_affine()
affine = xfm.reference.affine
import nibabel
new_nii = nibabel.Nifti1Image(self.volume.T, affine)
nibabel.save(new_nii, filename)
Expand Down
2 changes: 1 addition & 1 deletion cortex/freesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def import_subj(fs_subject, cx_subject=None, freesurfer_subject_dir=None, whitem

# Freesurfer uses FOV/2 for center, let's set the surfaces to use the
# magnet isocenter
trans = nibabel.load(out).get_affine()[:3, -1]
trans = nibabel.load(out).affine[:3, -1]
surfmove = trans - np.sign(trans) * [128, 128, 128]

from . import formats
Expand Down
4 changes: 2 additions & 2 deletions cortex/mayavi_aligner.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ def load_epi(self, epifilename, xfm=None, xfmtype="magnet"):
epi = nii.get_data().astype(float).squeeze()
if epi.ndim>3:
epi = epi[:,:,:,0]
self.affine = nii.get_affine()
base = nii.get_header().get_base_affine()
self.affine = nii.affine
base = nii.header.get_base_affine()
self.base = base
self.origin = base[:3, -1]
self.spacing = np.diag(base)[:3]
Expand Down
4 changes: 2 additions & 2 deletions cortex/mni.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def transform_surface_to_mni(subject, surfname):
MNI-transformed surface in same format returned by db.get_surf.
"""
# Get MNI affine transform
mni_affine = nibabel.load(default_template).get_affine()
mni_affine = nibabel.load(default_template).affine

# Get subject anatomical-to-MNI transform
mni_xfm = np.dot(mni_affine, db.get_mnixfm(subject, "identity"))
Expand Down Expand Up @@ -183,7 +183,7 @@ def transform_mni_to_subject(subject, xfm, volarray, func_to_mni,
funcspace_nii = tempfile.mktemp(".nii.gz")

# Save out relevant things
affine = nibabel.load(template).get_affine()
affine = nibabel.load(template).affine
nibabel.save(nibabel.Nifti1Image(volarray, affine), mnispace_func_nii)
_save_fsl_xfm(mni_to_func_xfm, np.linalg.inv(func_to_mni))

Expand Down
12 changes: 6 additions & 6 deletions cortex/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ def epi2anatspace(volumedata, order=1):
anat = db.get_anat(volumedata.subject, "raw")
xfm = db.get_xfm(volumedata.subject, volumedata.xfmname, "coord")

#allxfm = Transform(anat.get_affine(), anat.shape).inv * xfm.inv
allxfm = xfm * Transform(anat.get_affine(), anat.shape)
#allxfm = Transform(anat.affine, anat.shape).inv * xfm.inv
allxfm = xfm * Transform(anat.affine, anat.shape)

rotpart = allxfm.xfm[:3, :3]
transpart = allxfm.xfm[:3,-1]
Expand Down Expand Up @@ -262,8 +262,8 @@ def anat2epispace(anatdata, subject, xfmname, order=1):
anatref = db.get_anat(subject)
target = db.get_xfm(subject, xfmname, "coord")

allxfm = Transform(anatref.get_affine(), anatref.shape).inv * target.inv
#allxfm = xfm * Transform(anat.get_affine(), anat.shape)
allxfm = Transform(anatref.affine, anatref.shape).inv * target.inv
#allxfm = xfm * Transform(anat.affine, anat.shape)

rotpart = allxfm.xfm[:3, :3]
transpart = allxfm.xfm[:3,-1]
Expand Down Expand Up @@ -299,7 +299,7 @@ def epi2anatspace_fsl(volumedata):
xfmh.write(" ".join(["%0.5f"%f for f in ll])+"\n")

## Save out data into nifti file
datafile = nibabel.Nifti1Image(data.T, xfm.reference.get_affine(), xfm.reference.get_header())
datafile = nibabel.Nifti1Image(data.T, xfm.reference.affine, xfm.reference.header)
datafilename = tempfile.mktemp(".nii")
nibabel.save(datafile, datafilename)

Expand Down Expand Up @@ -342,7 +342,7 @@ def anat2epispace_fsl(data,subject,xfmname):
xfmh.write(" ".join(["%0.5f"%f for f in ll])+"\n")

## Save out data into nifti file
datafile = nibabel.Nifti1Image(data.T, anatNII.get_affine(), anatNII.get_header())
datafile = nibabel.Nifti1Image(data.T, anatNII.affine, anatNII.header)
datafilename = tempfile.mktemp(".nii")
nibabel.save(datafile, datafilename)

Expand Down
12 changes: 6 additions & 6 deletions cortex/xfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def from_fsl(cls, xfm, func_nii, anat_nii):
inIm = infile

refIm = nibabel.load(reffile)
in_hdr = inIm.get_header()
ref_hdr = refIm.get_header()
in_hdr = inIm.header
ref_hdr = refIm.header
# get_zooms gets the positive voxel sizes as returned in the header
inspace = np.diag(in_hdr.get_zooms()[:3] + (1,))
refspace = np.diag(ref_hdr.get_zooms()[:3] + (1,))
Expand All @@ -138,7 +138,7 @@ def from_fsl(cls, xfm, func_nii, anat_nii):
if npl.det(ref_hdr.get_best_affine())>=0:
refspace = np.dot(refspace, _x_flipper(ref_hdr.get_data_shape()[0]))

inAffine = inIm.get_affine()
inAffine = inIm.affine

coord = np.dot(inv(refspace),np.dot(xfm,np.dot(inspace,inv(inAffine))))
return cls(coord, refIm)
Expand Down Expand Up @@ -178,8 +178,8 @@ def to_fsl(self, anat_nii, direction='func>anat'):
inIm = nibabel.load(infile)
except AttributeError:
inIm = infile
in_hdr = inIm.get_header()
ref_hdr = self.reference.get_header()
in_hdr = inIm.header
ref_hdr = self.reference.header
# get_zooms gets the positive voxel sizes as returned in the header
inspace = np.diag(in_hdr.get_zooms()[:3] + (1,))
refspace = np.diag(ref_hdr.get_zooms()[:3] + (1,))
Expand All @@ -193,7 +193,7 @@ def to_fsl(self, anat_nii, direction='func>anat'):
print("Determinant is > 0: FLIPPING!")
refspace = np.dot(refspace, _x_flipper(ref_hdr.get_data_shape()[0]))

inAffine = inIm.get_affine()
inAffine = inIm.affine

fslx = np.dot(refspace,np.dot(self.xfm,np.dot(inAffine,inv(inspace))))
if direction=='func>anat':
Expand Down
2 changes: 1 addition & 1 deletion docs/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Transformations in pycortex are stored as **affine** matrices encoded in magnet

Each transform is stored in its own subdirectory containing two files: ``matrices.xfm``, and ``reference.nii.gz``. Masks are also stored in the transforms directory.

Transforms are saved as JSON-encoded text files. They have the format ``{subject}_{transform}.xfm``. There are four fields in this JSON structure: ``subject``, ``epifile``, ``coord``, ``magnet``. ``epifile`` gives the filename of the functional volume (EPI) that served as the reference for this transform. ``coord`` stores the transform from fiducial to coordinate space (for fast index lookups). ``magnet`` stores the transform from the fiducial to the magnet space, as defined in the return of ``nibabel.get_affine()``.
Transforms are saved as JSON-encoded text files. They have the format ``{subject}_{transform}.xfm``. There are four fields in this JSON structure: ``subject``, ``epifile``, ``coord``, ``magnet``. ``epifile`` gives the filename of the functional volume (EPI) that served as the reference for this transform. ``coord`` stores the transform from fiducial to coordinate space (for fast index lookups). ``magnet`` stores the transform from the fiducial to the magnet space, as defined in the return of ``nibabel.affine``.

Reference volumes are typically in Nifti_ format (*.nii), but can be any format that nibabel_ understands. These are stored to ensure that we know what the reference for any transform was. This makes it possible to visually verify and tweak alignments as well as keep a static store of images for future coregistrations.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ numexpr
cython
matplotlib
pillow
nibabel
nibabel>=2.1
networkx>=2.1
imageio
wget
Expand Down

0 comments on commit 876a554

Please sign in to comment.