Skip to content

Commit 435b4e7

Browse files
oestebaneffigies
andauthored
Apply suggestions from code review [skip ci]
Co-Authored-By: Chris Markiewicz <[email protected]>
1 parent 7cb8323 commit 435b4e7

File tree

9 files changed

+29
-29
lines changed

9 files changed

+29
-29
lines changed

niworkflows/interfaces/freesurfer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ def inject_skullstripped(subjects_dir, subject_id, skullstripped):
374374
if not op.exists(bm_auto):
375375
img = nb.load(t1)
376376
mask = nb.load(skullstripped)
377-
bmask = new_img_like(mask, mask.get_fdata() > 0)
377+
bmask = new_img_like(mask, np.asanyarray(mask.dataobj) > 0)
378378
resampled_mask = resample_to_img(bmask, img, 'nearest')
379-
masked_image = new_img_like(img, img.get_fdata() * resampled_mask.get_fdata())
379+
masked_image = new_img_like(img, img.get_fdata() * resampled_mask.dataobj)
380380
masked_image.to_filename(bm_auto)
381381

382382
if not op.exists(bm):

niworkflows/interfaces/images.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _run_interface(self, runtime):
6565
nb.Nifti1Image(sqdata, filenii.affine,
6666
filenii.header).to_filename(in_files[0])
6767

68-
if np.squeeze(nb.load(in_files[0]).get_fdata()).ndim < 4:
68+
if filenii.dataobj.ndim < 4:
6969
self._results['out_file'] = in_files[0]
7070
self._results['out_avg'] = in_files[0]
7171
# TODO: generate identity out_mats and zero-filled out_movpar
@@ -265,7 +265,7 @@ def _run_interface(self, runtime):
265265
offset = (reoriented.affine[:3, 3] * size_factor - reoriented.affine[:3, 3])
266266
target_affine[:3, 3] = reoriented.affine[:3, 3] + offset.astype(int)
267267

268-
data = nli.resample_img(reoriented, target_affine, target_shape).get_fdata()
268+
data = nli.resample_img(reoriented, target_affine, target_shape).dataobj
269269
conform_xfm = np.linalg.inv(reoriented.affine).dot(target_affine)
270270
reoriented = reoriented.__class__(data, target_affine, reoriented.header)
271271

@@ -534,7 +534,7 @@ def _run_interface(self, runtime):
534534
out_file = fname_presuffix(self.inputs.in_file, suffix='_hdr',
535535
newpath=runtime.cwd)
536536

537-
imgnii.__class__(imgnii.get_fdata(), imghdr.get_best_affine(),
537+
imgnii.__class__(imgnii.dataobj, imghdr.get_best_affine(),
538538
imghdr).to_filename(out_file)
539539
self._results['out_file'] = out_file
540540
return runtime
@@ -554,7 +554,7 @@ def extract_wm(in_seg, wm_label=3, newpath=None):
554554

555555
nii = nb.load(in_seg)
556556
data = np.zeros(nii.shape, dtype=np.uint8)
557-
data[nii.get_fdata() == wm_label] = 1
557+
data[np.asanyarray(nii.dataobj) == wm_label] = 1
558558

559559
out_file = fname_presuffix(in_seg, suffix='_wm', newpath=newpath)
560560
new = nb.Nifti1Image(data, nii.affine, nii.header)
@@ -591,7 +591,7 @@ def normalize_xform(img):
591591
int(qform_code) == xform_code, int(sform_code) == xform_code)):
592592
return img
593593

594-
new_img = img.__class__(img.get_fdata(), xform, img.header)
594+
new_img = img.__class__(img.dataobj, xform, img.header)
595595
# Unconditionally set sform/qform
596596
new_img.set_sform(xform, xform_code)
597597
new_img.set_qform(xform, xform_code)
@@ -608,7 +608,7 @@ def demean(in_file, in_mask, only_mask=False, newpath=None):
608608
out_file = fname_presuffix(in_file, suffix='_demeaned',
609609
newpath=os.getcwd())
610610
nii = nb.load(in_file)
611-
msk = nb.load(in_mask).get_fdata()
611+
msk = np.asanyarray(nb.load(in_mask).dataobj)
612612
data = nii.get_fdata()
613613
if only_mask:
614614
data[msk > 0] -= np.median(data[msk > 0])
@@ -706,10 +706,10 @@ def _run_interface(self, runtime):
706706
# If mask is a list, each mask is treated as its own ROI/parcel
707707
# If mask is a 3D, each integer is treated as its own ROI/parcel
708708
if len(mask_imgs) > 1:
709-
masks = [mask_img.get_fdata() >= self.inputs.prob_thres
709+
masks = [np.asanyarray(mask_img.dataobj) >= self.inputs.prob_thres
710710
for mask_img in mask_imgs]
711711
else:
712-
labelsmap = mask_imgs[0].get_fdata()
712+
labelsmap = np.asanyarray(mask_imgs[0].dataobj)
713713
labels = np.unique(labelsmap)
714714
labels = labels[labels != 0]
715715
masks = [labelsmap == l for l in labels]

niworkflows/interfaces/masks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _run_interface(self, runtime):
113113

114114
mask_nii = compute_epi_mask(padded_nii, exclude_zeros=True)
115115

116-
mask_data = mask_nii.get_fdata()
116+
mask_data = np.asanyarray(mask_nii.dataobj).astype(np.uint8)
117117
if isdefined(self.inputs.dilation):
118118
mask_data = nd.morphology.binary_dilation(mask_data).astype(np.uint8)
119119

@@ -267,7 +267,7 @@ def _generate_report(self):
267267
nsegs = len(levels)
268268
if nsegs == 0:
269269
levels = np.unique(np.round(
270-
nb.load(seg_files[0]).get_fdata()).astype(int))
270+
np.asanyarray(nb.load(seg_files[0]).dataobj).astype('int32'))
271271
levels = (levels[levels > 0] - 0.5).tolist()
272272
nsegs = len(levels)
273273

niworkflows/interfaces/mni.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def mask(in_file, mask_file, new_name):
418418
mask_nii = nb.load(mask_file)
419419
# Set all non-mask voxels in the input file to zero.
420420
data = in_nii.get_fdata()
421-
data[mask_nii.get_fdata() == 0] = 0
421+
data[np.asanyarray(mask_nii.dataobj) == 0] = 0
422422
# Save the new masked image.
423423
new_nii = nb.Nifti1Image(data, in_nii.affine, in_nii.header)
424424
new_nii.to_filename(new_name)
@@ -472,7 +472,7 @@ def create_cfm(in_file, lesion_mask=None, global_mask=True, out_path=None):
472472
in_img = nb.load(in_file)
473473

474474
# If we want a global mask, create one based on the input image.
475-
data = np.ones(in_img.shape, dtype=np.uint8) if global_mask else in_img.get_fdata()
475+
data = np.ones(in_img.shape, dtype=np.uint8) if global_mask else np.asanyarray(in_img.dataobj)
476476
if set(np.unique(data)) - {0, 1}:
477477
raise ValueError("`global_mask` must be true if `in_file` is not a binary mask")
478478

@@ -482,7 +482,7 @@ def create_cfm(in_file, lesion_mask=None, global_mask=True, out_path=None):
482482
lm_img = nb.as_closest_canonical(nb.load(lesion_mask))
483483

484484
# Subtract lesion mask from secondary mask, set negatives to 0
485-
data = np.fmax(data - lm_img.get_fdata(), 0)
485+
data = np.fmax(data - lm_img.dataobj, 0)
486486
# Cost function mask will be created from subtraction
487487
# Otherwise, CFM will be created from global mask
488488

niworkflows/interfaces/nilearn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ def _run_interface(self, runtime):
7272
)
7373

7474
if self.inputs.closing:
75-
closed = sim.binary_closing(masknii.get_fdata().astype(
75+
closed = sim.binary_closing(np.asanyarray(masknii.dataobj).astype(
7676
np.uint8), sim.ball(1)).astype(np.uint8)
7777
masknii = masknii.__class__(closed, masknii.affine,
7878
masknii.header)
7979

8080
if self.inputs.fill_holes:
81-
filled = binary_fill_holes(masknii.get_fdata().astype(
81+
filled = binary_fill_holes(np.asanyarray(masknii.dataobj).astype(
8282
np.uint8), sim.ball(6)).astype(np.uint8)
8383
masknii = masknii.__class__(filled, masknii.affine,
8484
masknii.header)

niworkflows/interfaces/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class CopyHeader(SimpleInterface):
106106
def _run_interface(self, runtime):
107107
in_img = nb.load(self.inputs.hdr_file)
108108
out_img = nb.load(self.inputs.in_file)
109-
new_img = out_img.__class__(out_img.get_fdata(), in_img.affine, in_img.header)
109+
new_img = out_img.__class__(out_img.dataobj, in_img.affine, in_img.header)
110110
new_img.set_data_dtype(out_img.get_data_dtype())
111111

112112
out_name = fname_presuffix(self.inputs.in_file,
@@ -215,7 +215,7 @@ def _copyxform(ref_image, out_image, message=None):
215215
header.set_sform(sform, int(sform_code))
216216
header['descrip'] = 'xform matrices modified by %s.' % (message or '(unknown)')
217217

218-
newimg = resampled.__class__(resampled.get_fdata(), orig.affine, header)
218+
newimg = resampled.__class__(resampled.dataobj, orig.affine, header)
219219
newimg.to_filename(out_image)
220220

221221

@@ -266,7 +266,7 @@ def _gen_reference(fixed_image, moving_image, fov_mask=None, out_file=None,
266266

267267
# Calculate a bounding box for the input mask
268268
# with an offset of 2 voxels per face
269-
bbox = np.argwhere(masknii.get_fdata() > 0)
269+
bbox = np.argwhere(np.asanyarray(masknii.dataobj) > 0)
270270
new_origin = np.clip(bbox.min(0) - 2, a_min=0, a_max=None)
271271
new_end = np.clip(bbox.max(0) + 2, a_min=0,
272272
a_max=res_shape - 1)
@@ -552,7 +552,7 @@ def _run_interface(self, runtime):
552552
return runtime
553553

554554
im = nb.concat_images([in_files[i] for i in indices])
555-
data = im.get_fdata().astype(float).sum(axis=3)
555+
data = im.get_fdata().sum(axis=3)
556556
data = np.clip(data, a_min=0.0, a_max=1.0)
557557

558558
out_file = fname_presuffix(first_fname, suffix='_tpmsum',
@@ -911,7 +911,7 @@ def _tpm2roi(in_tpm, in_mask, mask_erosion_mm=None, erosion_mm=None,
911911
eroded_mask_file = fname_presuffix(in_mask, suffix='_eroded',
912912
newpath=newpath)
913913
mask_img = nb.load(in_mask)
914-
mask_data = mask_img.get_fdata().astype(np.uint8)
914+
mask_data = np.asanyarray(mask_img.dataobj).astype(np.uint8)
915915
if mask_erosion_mm:
916916
iter_n = max(int(mask_erosion_mm / max(mask_img.header.get_zooms())), 1)
917917
mask_data = nd.binary_erosion(mask_data, iterations=iter_n)

niworkflows/tests/test_viz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def test_carpetplot():
1515
out_file = os.path.join(save_artifacts, 'carpetplot.svg')
1616
viz.plot_carpet(
1717
os.path.join(datadir, 'sub-ds205s03_task-functionallocalizer_run-01_bold_volreg.nii.gz'),
18-
nb.load(os.path.join(
18+
np.asanyarray(nb.load(os.path.join(
1919
datadir,
20-
'sub-ds205s03_task-functionallocalizer_run-01_bold_parc.nii.gz')).get_fdata(),
20+
'sub-ds205s03_task-functionallocalizer_run-01_bold_parc.nii.gz')).dataobj),
2121
output_file=out_file,
2222
legend=True
2323
)

niworkflows/viz/plots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def __init__(self, func_file, mask_file=None, data=None, conf_file=None, seg_fil
3737
func_nii = nb.load(func_file)
3838
self.tr = tr if tr is not None else func_nii.header.get_zooms()[-1]
3939

40-
self.mask_data = np.ones_like(func_nii.get_fdata(), dtype='uint8')
40+
self.mask_data = nb.fileslice.strided_scalar(func_nii.shape[:3], np.uint8(1))
4141
if mask_file:
42-
self.mask_data = nb.load(mask_file).get_fdata().astype('uint8')
42+
self.mask_data = np.asanyarray(nb.load(mask_file).dataobj).astype('uint8')
4343

4444
self.seg_data = None
4545
if seg_file:
46-
self.seg_data = nb.load(seg_file).get_fdata()
46+
self.seg_data = np.asanyarray(nb.load(seg_file).dataobj)
4747

4848
if units is None:
4949
units = {}

niworkflows/viz/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def cuts_from_bbox(mask_nii, cuts=3):
200200
"""Finds equi-spaced cuts for presenting images"""
201201
from nibabel.affines import apply_affine
202202

203-
mask_data = mask_nii.get_fdata() > 0.0
203+
mask_data = np.asanyarray(mask_nii.dataobj) > 0.0
204204

205205
# First, project the number of masked voxels on each axes
206206
ijk_counts = [
@@ -262,7 +262,7 @@ def _3d_in_file(in_file):
262262
except AttributeError:
263263
in_file = in_file
264264

265-
if in_file.get_fdata().ndim == 3:
265+
if len(in_file.shape) == 3:
266266
return in_file
267267

268268
return nlimage.index_img(in_file, 0)

0 commit comments

Comments
 (0)