saritha5 commited on
Commit
3ef3bdb
·
1 Parent(s): a9c1d0e

Create app.py

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