Beispiel #1
0
def pw_wavy(n_samples=200, n_bkps=3, noise_std=None):
    """Return a 1D piecewise wavy signal and the associated changepoints.

    Args:
        n_samples (int, optional): signal length
        n_bkps (int, optional): number of changepoints
        noise_std (float, optional): noise std. If None, no noise is added

    Returns:
        tuple: signal of shape (n_samples, 1), list of breakpoints

    """
    # breakpoints
    bkps = draw_bkps(n_samples, n_bkps)
    # we create the signal
    f1 = np.array([0.075, 0.1])
    f2 = np.array([0.1, 0.125])
    freqs = np.zeros((n_samples, 2))
    for sub, val in zip(np.split(freqs, bkps[:-1]), cycle([f1, f2])):
        sub += val
    tt = np.arange(n_samples)
    signal = np.sum((np.sin(2 * np.pi * tt * f) for f in freqs.T))

    if noise_std is not None:
        noise = normal(scale=noise_std, size=signal.shape)
        signal += noise

    return signal, bkps
Beispiel #2
0
def pw_constant(n_samples=200, n_features=1, n_bkps=3, noise_std=None, delta=(1, 10)):
    """Return a piecewise constant signal and the associated changepoints.

    Args:
        n_samples (int): signal length
        n_features (int, optional): number of dimensions
        n_bkps (int, optional): number of changepoints
        noise_std (float, optional): noise std. If None, no noise is added
        delta (tuple, optional): (delta_min, delta_max) max and min jump values

    Returns:
        tuple: signal of shape (n_samples, n_features), list of breakpoints
    """
    # breakpoints
    bkps = draw_bkps(n_samples, n_bkps)
    # we create the signal
    signal = np.empty((n_samples, n_features), dtype=float)
    tt_ = np.arange(n_samples)
    delta_min, delta_max = delta
    # mean value
    center = np.zeros(n_features)
    for ind in np.split(tt_, bkps):
        if ind.size > 0:
            # jump value
            jump = rd.uniform(delta_min, delta_max, size=n_features)
            spin = rd.choice([-1, 1], n_features)
            center += jump * spin
            signal[ind] = center

    if noise_std is not None:
        noise = rd.normal(size=signal.shape) * noise_std
        signal = signal + noise

    return signal, bkps
def pw_normal(n_samples=200, n_bkps=3):
    """Return a 2D piecewise Gaussian signal and the associated changepoints.

    Args:
        n_samples (int, optional): signal length
        n_bkps (int, optional): number of change points

    Returns:
        tuple: signal of shape (n_samples, 2), list of breakpoints
    """
    # breakpoints
    bkps = draw_bkps(n_samples, n_bkps)
    # we create the signal
    signal = np.zeros((n_samples, 2), dtype=float)
    cov1 = np.array([[1, 0.9], [0.9, 1]])
    cov2 = np.array([[1, -0.9], [-0.9, 1]])
    for sub, cov in zip(np.split(signal, bkps), cycle((cov1, cov2))):
        n_sub, _ = sub.shape
        sub += rd.multivariate_normal([0, 0], cov, size=n_sub)

    return signal, bkps
Beispiel #4
0
def pw_constant(n_samples=200,
                n_features=1,
                n_bkps=3,
                noise_std=None,
                delta=(1, 10),
                noise_delta=(1, 1)):
    """Return a piecewise constant signal and the associated changepoints.

    Args:
        n_samples (int): signal length
        n_features (int, optional): number of dimensions
        n_bkps (int, optional): number of changepoints
        noise_std (float, optional): noise std. If None or 0, no noise is added
        delta (tuple, optional): (delta_min, delta_max) max and min jump values
        noise_delta (tuple, optional): (noise_min_mult, noise_max_mult) max and min noise multipliers 


    Returns:
        tuple: signal of shape (n_samples, n_features), list of breakpoints

    noise_min_mult and noise_max_mult must be greater than 0
    0 to 1 and 1 to inf is equaly distributed.
    """
    if noise_std is None:
        noise_std = 0

    # breakpoints
    bkps = draw_bkps(n_samples, n_bkps)
    # we create the signal
    signal = np.empty((n_samples, n_features), dtype=float)
    signal_noise = np.empty((n_samples, n_features), dtype=float)
    tt_ = np.arange(n_samples)
    delta_min, delta_max = delta
    noise_delta_min, noise_delta_max = noise_delta
    if not noise_delta_min <= noise_delta_max:
        sys.exit('Error: noise_delta_min is bigger than noise_delta_max')

    # mean value
    center = np.zeros(n_features)
    for ind in np.split(tt_, bkps):
        if ind.size > 0:
            # jump value
            jump = rd.uniform(delta_min, delta_max, size=n_features)
            spin = rd.choice([-1, 1], n_features)
            center += jump * spin
            signal[ind] = center

    noise_center = np.ones(n_features)
    noise_jump = np.zeros(n_features)

    for ind in np.split(tt_, bkps):
        if ind.size > 0:

            if noise_delta_min >= 1 and noise_delta_max >= 1:
                noise_jump = rd.uniform(noise_delta_min, noise_delta_max,
                                        n_features)
            elif noise_delta_min < 1 and noise_delta_max <= 1:
                aux_jump = rd.uniform(1 / noise_delta_min, 1 / noise_delta_max,
                                      n_features)
                noise_jump = 1 / aux_jump
            else:  #equalize probabilities lower than 1 and bigger than 1 taking into account the proportion
                inv_min = 1 / noise_delta_min
                domain_len = inv_min + noise_delta_max
                p = [inv_min / domain_len, noise_delta_max / domain_len]
                noise_side = rd.choice([-1, 1], n_features, p=p)
                #                 print('ns',noise_side)
                for f in range(n_features):
                    if noise_side[f] == 1:
                        noise_jump[f] = rd.choice([1, noise_delta_max])
#                         print('!!!!',noise_jump[f])
                    else:
                        aux_jump = rd.choice([inv_min, 1])
                        noise_jump[f] = 1 / aux_jump

#             print('nc1',noise_center)
#             print('nj1',noise_jump)
            noise_center *= noise_jump
            #             print('nc2',noise_center)

            signal_noise[ind] = noise_center

    noise = rd.normal(size=signal.shape) * signal_noise * noise_std
    signal = signal + noise

    return signal, bkps