Skip to content

Commit

Permalink
add sample_volume python function
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoopes committed Feb 8, 2022
1 parent 186a60e commit bd9e63e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions python/freesurfer/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ def resample(source, target_shape, trg2src, interp_method='linear', smooth_sigma
else:
raise ValueError('invalid resample interpolation method "%s"' % interp_method)

def sample_volume(volume, points, interp_method='linear', fill_value=0):
'''
Parameters:
volume: ndarray
Image array to sample from.
points: ndarray
(N, 3) image coordinates to sample from.
interp_method: str, optional
The method of interpolation to perform. Supported are 'linear' and 'nearest'.
fill_value: number, optional
If provided, the value to use for points outside of the
interpolation domain. If None, values outside the domain are extrapolated.
'''
if volume.ndim > 4:
raise ValueError('resampling can not be done on arrays with more than 4 dimensions')
elif volume.ndim < 3:
raise NotImplementedError('%dD resampling is not yet supported (must be 3D or 4D)' % source.ndim)
elif volume.ndim == 3:
volume = volume[..., np.newaxis]

linvec = [np.arange(0, dim) for dim in volume.shape[:3]]
sampler = lambda x: interpn(linvec, volume[..., x], points, method=interp_method, bounds_error=False, fill_value=fill_value)
return np.stack([sampler(f) for f in range(volume.shape[-1])], axis=-1).squeeze()


def sample_into_volume(volume, weights, coords, values):
'''
Expand Down

0 comments on commit bd9e63e

Please sign in to comment.