Skip to content

Commit 182ff1f

Browse files
committed
ENH: Flesh out NdGrid implementation
1 parent d126920 commit 182ff1f

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

nibabel/pointset.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import operator as op
2+
from functools import reduce
3+
4+
import numpy as np
5+
6+
from nibabel.affines import apply_affine
7+
8+
19
class Pointset:
210
@property
311
def n_coords(self):
@@ -50,6 +58,32 @@ class NdGrid(Pointset):
5058
shape : 3-tuple
5159
number of coordinates in each dimension of grid
5260
"""
61+
def __init__(self, shape, affines):
62+
self.shape = tuple(shape)
63+
try:
64+
self._affines = dict(affines)
65+
except (TypeError, ValueError):
66+
self._affines = {"world": np.array(affines)}
67+
if "voxels" not in self._affines:
68+
self._affines["voxels"] = np.eye(4, dtype=np.uint8)
69+
5370
def get_affine(self, name=None):
5471
""" 4x4 array """
55-
raise NotImplementedError
72+
if name is None:
73+
name = next(iter(self._affines))
74+
return self._affines[name]
75+
76+
def get_coords(self, name=None):
77+
if name is None:
78+
name = next(iter(self._affines))
79+
aff = get_affine(name)
80+
dt = np.result_type(*(np.min_scalar_type(dim) for dim in self.shape))
81+
# This is pretty wasteful; we almost certainly want instead an
82+
# object that will retrieve a coordinate when indexed, but where
83+
# np.array(obj) returns this
84+
ijk_coords = np.array(list(np.ndindex(self.shape)), dtype=dt)
85+
return apply_affine(aff, ijk_coords)
86+
87+
@property
88+
def n_coords(self):
89+
return reduce(op.mul, self.shape)

0 commit comments

Comments
 (0)