Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Cost between on the self.signal[start:end] signal
Args:
start (int): start index
end (int): end index
Raises:
NotEnoughPoints: if there are not enough points
Returns:
float: the cost
"""
assert 0 <= start <= end
if end - start < 20: # we need at least 20 points
raise NotEnoughPoints
res = 0
# we loop over the dimensions:
for sig in self.signal[start:end].T:
# Fourier transform
fourier = np.fft.rfft(sig.flatten())
# We want to calculate the difference between the signal and a
# signal reconstructed from the DC component and the biggest
# harmonic. The difference is the residuals.
# coefficient of the biggest harmonic (except the DC)
k_max, _ = max(enumerate(fourier[1:], start=1),
key=lambda z: abs(z[1]))
fourier[0] = 0 # DC component to 0
start (int): first index of the segment (index included)
end (int): last index of the segment (index excluded)
Raises:
NotEnoughPoints: if there are not enough points to compute the cost
for the specified segment (here, at least 3 points).
Returns:
float: cost on the given segment.
"""
assert 0 <= start <= end
if not self._computed:
self.set_params()
if end - start < 2: # we need at least 3 points
raise NotEnoughPoints
res = np.sum(self.norm_vect[start:end])
res -= self.pairwise_mat[start:end, start:end].sum() / (end - start)
return res
def error_func(start, end):
assert 0 <= start <= end
if end - start < 2: # we need at least 2 points
raise NotEnoughPoints
sig = s[start:end]
m, v = sig.mean(), sig.var()
if v == 0:
return np.inf
res = np.sum((sig - m) ** 2)
res /= 2 * v
res += (end - start + 1) / 2 * np.log(v)
res += (end - start + 1) / 2 * np.log(2 * np.pi)
return res