Skip to content

Commit

Permalink
use -inf for scores for invalid offsets instead of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
smacke committed Oct 31, 2021
1 parent d98dfe2 commit cf1d614
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions ffsubsync/aligners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit cf1d614

Please sign in to comment.