deepcharles / ruptures

ruptures: change point detection in Python
BSD 2-Clause "Simplified" License
1.59k stars 162 forks source link

Dynp with CostML problem #172

Closed julia-shenshina closed 3 years ago

julia-shenshina commented 3 years ago

Hi!

I run CostML example from user guide with ruptures version 1.1.4:

import numpy as np
import matplotlib.pylab as plt
import ruptures as rpt

# creation of data
n, dim = 500, 3  # number of samples, dimension
n_bkps, sigma = 3, 5  # number of change points, noise standart deviation
signal, bkps = rpt.pw_constant(n, dim, n_bkps, noise_std=sigma)

M = np.eye(dim)
c = rpt.costs.CostMl(metric=M)
algo = rpt.Dynp(custom_cost=c)
algo.fit(signal=signal)

res = algo.predict(n_bkps=n_bkps)

and get an AttributeError in predict:

/ruptures/detection/dynp.py in predict(self, n_bkps)
    130         # raise an exception in case of impossible segmentation configuration
    131         if not sanity_check(
--> 132             n_samples=self.cost.signal.shape[0],
    133             n_bkps=n_bkps,
    134             jump=self.jump,

AttributeError: 'CostMl' object has no attribute 'signal'
oboulant commented 3 years ago

Hi Julia,

Thanks for your interest in ruptures !

You are right, when we introduced an early check on the segmentation parameters, we did not test with CostMl.

Since for the CostMl cost, one does not need the signal once you have computed the gram matrix we did not store the signal into the cost object.

For compatibility reasons, we have to correct this behaviour. Will post a fix in that direction.