Spaces:
Sleeping
Sleeping
File size: 5,073 Bytes
ced361b fdbed18 3ef3bdb fdbed18 ced361b 3ef3bdb fdbed18 ef6e275 fdbed18 ced361b b9a5329 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
import numpy as np
import matplotlib.pylab as plt
import ruptures as rpt
import streamlit as st
from ruptures.metrics import precision_recall
from ruptures.metrics import hausdorff
from ruptures.metrics import randindex
st.title("Change Point Detection")
# Generating Signal
def pw_constant_input(n,dim,n_bkps,sigma):
"""Piecewise constant (pw_constant)"""
# n, dim # number of samples, dimension
# n_bkps, sigma # number of change points, noise standard deviation
signal, bkps = rpt.pw_constant(n, dim, n_bkps, noise_std=sigma)
rpt.display(signal, bkps)
return signal,bkps
def pw_linear_input(n,dim,n_bkps,sigma):
"""Piecewise Linear"""
# creation of data
# n, dim = 500, 3 # number of samples, dimension of the covariates
# n_bkps, sigma = 3, 5 # number of change points, noise standart deviation
signal, bkps = rpt.pw_linear(n, dim, n_bkps, noise_std=sigma)
rpt.display(signal, bkps)
return signal,bkps
def pw_normal_input(n,dim,n_bkps,sigma):
"""Piecewise 2D Gaussian process (pw_normal)#"""
# creation of data
#n = 500 # number of samples
#n_bkps = 3 # number of change points
signal, bkps = rpt.pw_normal(n, n_bkps)
rpt.display(signal, bkps)
return signal,bkps
def pw_wavy_input(n,dim,n_bkps,sigma):
# 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_wavy(n, n_bkps, noise_std=sigma)
rpt.display(signal, bkps)
return signal,bkps
input_list = ['piecewiseConstant','piecewiseLinear','piecewiseNormal','piecewiseSinusoidal']
generate_signal = st.selectbox(label = "Choose an input signal", options = input_list)
n,dim,n_bkps,sigma = st.columns(4)
with n:
n= st.number_input('No of Samples',min_value=100,step=1)
with dim:
dim = st.number_input('No of dimesions',min_value=1,max_value = 5,step=1)
with n_bkps:
n_bkps = st.number_input('No of breakpoints',min_value=2,step=1)
with sigma:
sigma = st.number_input('Variance',min_value=1,max_value=4,step=1)
if generate_signal == 'piecewiseConstant':
signal,bkps = pw_constant_input(n,dim,n_bkps,sigma)
elif generate_signal== 'piecewiseLinear':
signal,bkps = pw_linear_input(n,dim,n_bkps,sigma)
elif generate_signal == 'piecewiseNormal':
signal,bkps = pw_normal_input(n,dim,n_bkps,sigma)
else:
signal,bkps= pw_wavy_input(n,dim,n_bkps,sigma)
fig, axarr = rpt.display(signal,bkps)
st.pyplot(fig)
def dynp_method(signal,bkps,n_bkps):
# change point detection
model = "l1" # "l2", "rbf"
algo = rpt.Dynp(model=model, min_size=3, jump=5).fit(signal)
my_bkps = algo.predict(n_bkps)
# show results
fig,axarr = rpt.show.display(signal, bkps, my_bkps, figsize=(10, 6))
#plt.show()
st.pyplot(fig)
return my_bkps
def pelt_method(signal,bkps,n_bkps):
# change point detection
model = "l1" # "l2", "rbf"
algo = rpt.Pelt(model=model, min_size=3, jump=5).fit(signal)
my_bkps = algo.predict(pen=3)
# show results
fig, ax_arr = rpt.display(signal, bkps, my_bkps, figsize=(10, 6))
st.pyplot(fig)
return my_bkps
def bin_seg_method(signal,bkps,n_bkps):
# change point detection
model = "l2" # "l1", "rbf", "linear", "normal", "ar",...
algo = rpt.Binseg(model=model).fit(signal)
my_bkps = algo.predict(n_bkps)
# show results
fg,axxarr = rpt.show.display(signal, bkps, my_bkps, figsize=(10, 6))
st.pyplot(fig)
return my_bkps
def bot_up_seg(signal,bkps,n_bkps):
# change point detection
model = "l2" # "l1", "rbf", "linear", "normal", "ar",...
algo = rpt.Binseg(model=model).fit(signal)
my_bkps = algo.predict(n_bkps)
# show results
fig,axxar = rpt.show.display(signal, bkps, my_bkps, figsize=(10, 6))
st.pyplot(fig)
return my_bkps
def win_sli_seg(signal,bkps,n_bkps):
# change point detection
model = "l2" # "l1", "rbf", "linear", "normal", "ar"
algo = rpt.Window(width=40, model=model).fit(signal)
my_bkps = algo.predict(n_bkps)
# show results
fig,axxar= rpt.show.display(signal, bkps, my_bkps, figsize=(10, 6))
st.pyplot(fig)
return my_bkps
searchmethod_list = ['Dynamic Programming','Pelt','Binary Segmentation','Bottom-up Segmentation','Window sliding segmentation']
detection_model = st.selectbox(label = "Choose a Detection Method",options = searchmethod_list)
if detection_model== 'Dynamic Programming':
bkps1 = dynp_method(signal,bkps,n_bkps)
elif detection_model=='Pelt':
bkps1 = pelt_method(signal,bkps,n_bkps)
elif detection_model=='Binary Segmentation':
bkps1 = bin_seg_method(signal,bkps,n_bkps)
elif detection_model=='Bottom-up Segmentation':
bkps1 = bot_up_seg(signal,bkps,n_bkps)
else:
bkps1 = win_sli_seg(signal,bkps,n_bkps)
p, r = precision_recall(bkps, bkps1)
st.header('Precision and Recall')
st.write(p, r)
st.header('Hausdorff metric')
st.write(hausdorff(bkps, bkps1))
st.header('Rand index')
st.write(randindex(bkps, bkps1))
|