Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# initialization
bkps = [self.n_samples]
stop = False
error = self.cost.error(0, self.n_samples)
while not stop:
stop = True
_, bkp = max((v, k) for k, v in enumerate(self.score, start=1)
if not any(abs(k - b) < self.width // 2 for b in bkps[:-1]))
if n_bkps is not None:
if len(bkps) - 1 < n_bkps:
stop = False
elif pen is not None:
new_error = sum(self.cost.error(start, end)
for start, end in pairwise(sorted([0, bkp] + bkps)))
gain = error - new_error
if gain > pen:
stop = False
error = sum(self.cost.error(start, end)
for start, end in pairwise([0] + bkps))
elif epsilon is not None:
if error > epsilon:
stop = False
error = sum(self.cost.error(start, end)
for start, end in pairwise([0] + bkps))
if not stop:
bkps.append(bkp)
bkps.sort()
return bkps
params (dict, optional): a dictionary of parameters for the cost instance.
Returns:
self
"""
self.min_size = min_size
self.jump = jump
self.width = 2 * (width // 2)
self.n_samples = None
self.signal = None
self.inds = None
if custom_cost is not None and isinstance(custom_cost, BaseCost):
self.cost = custom_cost
else:
if params is None:
self.cost = cost_factory(model=model)
else:
self.cost = cost_factory(model=model, **params)
self.score = list()
Returns:
self
"""
self.min_size = min_size
self.jump = jump
self.width = 2 * (width // 2)
self.n_samples = None
self.signal = None
self.inds = None
if custom_cost is not None and isinstance(custom_cost, BaseCost):
self.cost = custom_cost
else:
if params is None:
self.cost = cost_factory(model=model)
else:
self.cost = cost_factory(model=model, **params)
self.score = list()
``'custom_cost'`` is not None.
custom_cost (BaseCost, optional): custom cost function. Defaults to None.
min_size (int, optional): minimum segment length.
jump (int, optional): subsample (one every *jump* points).
params (dict, optional): a dictionary of parameters for the cost instance.
Returns:
self
"""
self.min_size = min_size
self.jump = jump
self.width = 2 * (width // 2)
self.n_samples = None
self.signal = None
self.inds = None
if custom_cost is not None and isinstance(custom_cost, BaseCost):
self.cost = custom_cost
else:
if params is None:
self.cost = cost_factory(model=model)
else:
self.cost = cost_factory(model=model, **params)
self.score = list()
def __init__(self, width=100):
"""Instanciate with window length.
Args:
width (int): window lenght.
Returns:
self
"""
self.width = 2 * (width // 2)
# window
self.win = np.sign(np.linspace(-1, 1, self.width)).reshape(-1, 1)
self.n_samples = None
self.signal = None
self.cost = Cost(model="constantl2")
self.score = None
def test_costs_5D_noisy(signal_bkps_5D_noisy, cost_name):
signal, bkps = signal_bkps_5D_noisy
cost = cost_factory(cost_name)
cost.fit(signal)
cost.error(0, 100)
cost.error(100, signal.shape[0])
cost.error(10, 50)
cost.sum_of_costs(bkps)
with pytest.raises(NotEnoughPoints):
cost.error(1, 2)
def test_costs_1D(signal_bkps_1D, cost_name):
signal, bkps = signal_bkps_1D
cost = cost_factory(cost_name)
cost.fit(signal)
cost.fit(signal.flatten())
cost.error(0, 100)
cost.error(100, signal.shape[0])
cost.error(10, 50)
cost.sum_of_costs(bkps)
with pytest.raises(NotEnoughPoints):
cost.error(1, 2)
def test2_ruptures1D():
n_regimes = 5
n_samples = 500
# Piecewise constant signal
signal, chg_pts = pw_constant(n=n_samples, clusters=n_regimes,
min_size=50, noisy=True, snr=0.1)
func_to_minimize = gaussmean(signal) # - log likelihood
pen = 10
pe = Pelt(func_to_minimize, penalty=pen,
n=signal.shape[0], K=0, min_size=1)
pe.fit()
# Piecewise linear signal
signal, chg_pts = pw_linear(n=n_samples, clusters=n_regimes,
min_size=50, noisy=True, snr=0.1)
func_to_minimize = linear_mse(signal) # mean squared error
for pen in np.linspace(0.1, 100, 20):
pe = Pelt(func_to_minimize, penalty=pen, n=signal.shape[0], K=0)
pe.fit()
def test1_ruptures1D():
n_regimes = 5
n_samples = 500
# Piecewise constant signal
signal, chg_pts = pw_constant(n=n_samples, clusters=n_regimes,
min_size=50, noisy=True, snr=0.1)
func_to_minimize = gaussmean(signal) # - log likelihood
for pen in np.linspace(0.1, 100, 20):
pe = Pelt(func_to_minimize, penalty=pen, n=signal.shape[0], K=0)
pe.fit()
# Piecewise linear signal
signal, chg_pts = pw_linear(n=n_samples, clusters=n_regimes,
min_size=50, noisy=True, snr=0.1)
func_to_minimize = linear_mse(signal) # mean squared error
for pen in np.linspace(0.1, 100, 20):
pe = Pelt(func_to_minimize, penalty=pen,
n=signal.shape[0], K=0, min_size=3)
pe.fit()
def test4_ruptures1D():
n_regimes = 5
n_samples = 500
# Piecewise constant signal
signal, chg_pts = pw_constant(n=n_samples, clusters=n_regimes,
min_size=50, noisy=True, snr=0.001)
func_to_minimize = gaussmean(signal) # - log likelihood
pen = 50
pe = Pelt(func_to_minimize, penalty=pen, n=signal.shape[0], K=0)
my_chg_pts = pe.fit()
assert np.array_equal(np.sort(chg_pts), np.sort(my_chg_pts))