Skip to content

Commit

Permalink
Merge branch 'master' into mem-bind
Browse files Browse the repository at this point in the history
  • Loading branch information
ccuetom authored Oct 27, 2023
2 parents e270b55 + 04d2e71 commit 0f8d5de
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions stride/problem/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ def stagger_data(self, data, stagger, method='nearest'):

return interp

def resample(self, space=None, order=3, prefilter=True, **kwargs):
def resample(self, space=None, **kwargs):
"""
Resample the internal (non-padded) data given some new space object.
Expand All @@ -911,7 +911,15 @@ def resample(self, space=None, order=3, prefilter=True, **kwargs):
Order of the interplation, default is 3.
prefilter : bool, optional
Determines if the input array is prefiltered
before interpolation. The default is ``True``.
before interpolation. If downsampling, this defaults to ``False`` as an anti-aliasing filter
will be applied instead. If upsampling, this defaults to ``True``.
anti_alias : bool, optional
Whether a Gaussian filter is applied to smooth the data before interpolation.
The default is ``True``. This is only applied when downsampling.
anti_alias_sigma : float or tuple of floats, optional
Gaussian filter standard deviations used for the anti-aliasing filter.
The default is (d - 1) / 2 where d is the downsampling factor and d > 1. When upsampling,
d < 1, and no anti-aliasing filter is applied.
Returns
-------
Expand All @@ -923,22 +931,18 @@ def resample(self, space=None, order=3, prefilter=True, **kwargs):

interp = []
for t in range(data.shape[0]):
interp.append(self.resample_data(data[t], space,
order=order,
prefilter=prefilter))
interp.append(self.resample_data(data[t], space, **kwargs))

interp = np.stack(interp, axis=0)

else:
interp = self.resample_data(self.data, space,
order=order,
prefilter=prefilter)
interp = self.resample_data(self.data, space, **kwargs)

self.grid.space = space
self._init_shape()
self._data = self.pad_data(interp)

def resample_data(self, data, space, order=3, prefilter=True):
def resample_data(self, data, space, **kwargs):
"""
Resample the data given some new space object.
Expand All @@ -949,22 +953,54 @@ def resample_data(self, data, space, order=3, prefilter=True):
space : Space
New space.
order : int, optional
Order of the interplation, default is 3.
Order of the interpolation, default is 3.
prefilter : bool, optional
Determines if the input array is prefiltered
before interpolation. The default is ``True``.
before interpolation. If downsampling, this defaults to ``False`` as an anti-aliasing filter
will be applied instead. If upsampling, this defaults to ``True``.
anti_alias : bool, optional
Whether a Gaussian filter is applied to smooth the data before interpolation.
The default is ``True``. This is only applied when downsampling.
anti_alias_sigma : float or tuple of floats, optional
Gaussian filter standard deviations used for the anti-aliasing filter.
The default is (d - 1) / 2 where d is the downsampling factor and d > 1. When upsampling,
d < 1, and no anti-aliasing filter is applied.
Returns
-------
ndarray
Resampled data.
"""
order = kwargs.pop('order', 3)
prefilter = kwargs.pop('prefilter', True)

resampling_factors = np.array([dx_old/dx_new
for dx_old, dx_new in zip(self.space.spacing, space.spacing)])

# Anti-aliasing is only required for down-sampling interpolation
if any(factor < 1 for factor in resampling_factors):
anti_alias = kwargs.pop('anti_alias', True)

if anti_alias:
anti_alias_sigma = kwargs.pop('anti_alias_sigma', None)

if anti_alias_sigma is not None:
anti_alias_sigma = anti_alias_sigma * np.ones_like(resampling_factors)

if np.any(anti_alias_sigma < 0):
raise ValueError("Anti-alias standard dev. must be equal to or greater than zero")

# Estimate anti-alias standard deviations if none provided
else:
anti_alias_sigma = np.maximum(0, (1/resampling_factors - 1) / 2)

data = scipy.ndimage.gaussian_filter(data, anti_alias_sigma)

resampling_factor = [dx_old/dx_new
for dx_old, dx_new in zip(self.space.spacing, space.spacing)]
# Prefiltering is not necessary if anti-alias filter used
prefilter = False

interp = scipy.ndimage.zoom(data, resampling_factor,
interp = scipy.ndimage.zoom(data, resampling_factors,
order=order, prefilter=prefilter)

return interp
Expand Down

0 comments on commit 0f8d5de

Please sign in to comment.