Skip to content

Commit

Permalink
Merge branch 'nearest_vs_nearest_before'
Browse files Browse the repository at this point in the history
  • Loading branch information
domstoppable committed Sep 30, 2024
2 parents 9541e19 + 484d904 commit a08d254
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/pupil_labs/neon_recording/stream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@

class InterpolationMethod(Enum):
NEAREST = "nearest"
NEAREST_BEFORE = "nearest_before"
LINEAR = "linear"

def __eq__(self, other):
if isinstance(other, str):
return self.value == other

return super().__eq__(other)


def _record_truthiness(self):
for field in self.dtype.names:
Expand Down Expand Up @@ -39,10 +46,27 @@ def sample(self, tstamps=None, method=InterpolationMethod.NEAREST):
if method == InterpolationMethod.NEAREST:
return self._sample_nearest(tstamps)

if method == InterpolationMethod.NEAREST_BEFORE:
return self._sample_nearest_before(tstamps)

elif method == InterpolationMethod.LINEAR:
return self._sample_linear_interp(tstamps)

def _sample_nearest(self, ts):
# Use searchsorted to get the insertion points
idxs = np.searchsorted(self.ts, ts)

# Ensure index bounds are valid
idxs = np.clip(idxs, 1, len(self.ts) - 1)
left = self.ts[idxs - 1]
right = self.ts[idxs]

# Determine whether the left or right value is closer
idxs -= (np.abs(ts - left) < np.abs(ts - right)).astype(int)

return self.sampler_class(self._data[idxs])

def _sample_nearest_before(self, ts):
last_idx = len(self._data) - 1
idxs = np.searchsorted(self.ts, ts)
idxs[idxs > last_idx] = last_idx
Expand Down

0 comments on commit a08d254

Please sign in to comment.