From cf1d6144746dd788c6b4fa6f57549169c1c72b59 Mon Sep 17 00:00:00 2001 From: Stephen Macke Date: Sat, 30 Oct 2021 21:14:14 -0700 Subject: [PATCH] use -inf for scores for invalid offsets instead of 0 --- ffsubsync/aligners.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ffsubsync/aligners.py b/ffsubsync/aligners.py index 6c7afc7..f2a17ba 100644 --- a/ffsubsync/aligners.py +++ b/ffsubsync/aligners.py @@ -27,12 +27,13 @@ def __init__(self, max_offset_samples: Optional[int] = None) -> None: self.best_score_: Optional[float] = None self.get_score_: bool = False - def _zero_out_extreme_offsets(self, convolve: np.ndarray, substring: np.ndarray) -> np.ndarray: + def _eliminate_extreme_offsets_from_solutions(self, convolve: np.ndarray, substring: np.ndarray) -> np.ndarray: convolve = np.copy(convolve) if self.max_offset_samples is None: return convolve offset_to_index = lambda offset: len(convolve) - 1 + offset - len(substring) - convolve[:offset_to_index(-self.max_offset_samples)] = convolve[offset_to_index(self.max_offset_samples):] = 0 + convolve[:offset_to_index(-self.max_offset_samples)] = float('-inf') + convolve[offset_to_index(self.max_offset_samples):] = float('-inf') return convolve def _compute_argmax(self, convolve: np.ndarray, substring: np.ndarray) -> None: @@ -54,9 +55,7 @@ def fit(self, refstring, substring, get_score: bool = False) -> FFTAligner: subft = np.fft.fft(np.append(np.zeros(extra_zeros + len(refstring)), substring)) refft = np.fft.fft(np.flip(np.append(refstring, np.zeros(len(substring) + extra_zeros)), 0)) convolve = np.real(np.fft.ifft(subft * refft)) - self._compute_argmax(self._zero_out_extreme_offsets(convolve, substring), substring) - if self.best_score_ == 0.: - self._compute_argmax(convolve, substring) + self._compute_argmax(self._eliminate_extreme_offsets_from_solutions(convolve, substring), substring) self.get_score_ = get_score return self