-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchangepoint.py
149 lines (117 loc) · 5.1 KB
/
changepoint.py
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
import argparse
import numpy
import sys
import scipy.io
import os.path
sys.path.append("./module")
import randentropymod
import basicutils
parser = argparse.ArgumentParser()
parser.add_argument("-m","--rmat-filename", help="Observed transition matrix filename", \
type=str, required=True, dest="rmatfilename")
parser.add_argument("-M", "--name-of-matrix", help="Name of the observed transition matrix (default: ratings)", \
type=str, required=False, default="ratings", dest="nameofmatrix")
parser.add_argument("-c", "--numof-cp", help="Number of change points 1, 2, 3 (default: 1)", \
type=int, required=False, default=1, dest="numofcp")
parser.add_argument("-o", "--output-file", help="Dumps all values (default: change.txt)", \
type=str, required=False, default="change.txt", dest="outf")
parser.add_argument("--iterations", help="Use iteration number instead of progressbar",
required=False, default=False, action="store_true")
parser.add_argument("--cp1-start", help="CP 1 start from (default: 1)", \
type=int, required=False, default=1, dest="cp1start")
parser.add_argument("--cp1-stop", help="CP 1 stop (default: -1 i.e. will stop at maximum time)", \
type=int, required=False, default=-1, dest="cp1stop")
parser.add_argument("--cp2-start", help="CP 2 start from (default: 1)", \
type=int, required=False, default=1, dest="cp2start")
parser.add_argument("--cp2-stop", help="CP 2 stop (default: -1 i.e. will stop at maximum time)", \
type=int, required=False, default=-1, dest="cp2stop")
parser.add_argument("--cp3-start", help="CP 3 start from (default: 1)", \
type=int, required=False, default=1, dest="cp3start")
parser.add_argument("--cp3-stop", help="CP 3 stop (default: -1 i.e. will stop at maximum time)", \
type=int, required=False, default=-1, dest="cp3stop")
parser.add_argument("--delta-cp", help="Delta time between CPs (default: 1 no delta)" +
"if delta <= 0 will use cp2 and cp3 start and stop values", \
type=int, required=False, default=1, dest="deltacp")
parser.add_argument("--perform-test", help="Perfom Lambda test for the specified cp1:cp2;cp3;num_of_run "+
"(default: \"-1;0\" i.e. no test is performed) will use also cp2 and cp3 if --numof-cp is equal to 2 or 3",
required=False, type=str, default="-1;0", dest="performtest")
if len(sys.argv) == 1:
parser.print_help()
exit(1)
args = parser.parse_args()
#print(args)
#print(args.cp1start, args.cp1stop)
#print(args.cp2start, args.cp2stop)
#print(args.cp3start, args.cp3stop)
if not (os.path.isfile(args.rmatfilename)):
print("File " + args.rmatfilename + " does not exist ")
exit(1)
msd = None
if args.rmatfilename.endswith('.csv'):
msd = basicutils.csvfile_to_mats(args.rmatfilename)
if msd == None:
print("Error while reading file " + args.rmatfilename)
exit(1)
elif args.rmatfilename.endswith('.mat'):
msd = scipy.io.loadmat(args.rmatfilename)
else:
print("Error in file extension")
exit(1)
if not(args.nameofmatrix in list(msd.keys())):
print("Cannot find " + args.nameofmatrix + " in " + args.rmatfilename)
print(list(msd.keys()))
exit(1)
ms = msd[args.nameofmatrix].astype(numpy.int32)
fp = open(args.outf, "w")
cp_fortest = -1
num_of_run = 0
cp_fortest_2 = -1
cp_fortest_3 = -1
if len(args.performtest.split(";")) < 1:
print("Error in --perform-test option")
exit(1)
cp_fortest = int(args.performtest.split(";")[0])
if cp_fortest >= 0:
if args.numofcp == 1:
if len(args.performtest.split(";")) != 2:
print("Error in --perform-test option")
exit(1)
num_of_run = int(args.performtest.split(";")[1])
elif args.numofcp == 2:
if len(args.performtest.split(";")) != 3:
print("Error in --perform-test option")
exit(1)
cp_fortest_2 = int(args.performtest.split(";")[1])
num_of_run = int(args.performtest.split(";")[2])
elif args.numofcp == 3:
if len(args.performtest.split(";")) != 4:
print("Error in --perform-test option")
exit(1)
cp_fortest_2 = int(args.performtest.split(";")[1])
cp_fortest_3 = int(args.performtest.split(";")[2])
num_of_run = int(args.performtest.split(";")[3])
runcps = randentropymod.changepoint()
try:
runcps.set_community (ms)
runcps.set_num_of_bootstrap_iter (num_of_run)
runcps.set_cp1_fortest (cp_fortest)
runcps.set_cp2_fortest (cp_fortest_2)
runcps.set_cp3_fortest (cp_fortest_3)
runcps.set_cp1_start_stop (args.cp1start, args.cp1stop)
runcps.set_cp2_start_stop (args.cp2start, args.cp2stop)
runcps.set_cp3_start_stop (args.cp3start, args.cp3stop)
runcps.set_num_of_cps (args.numofcp)
runcps.set_delta_cp (args.deltacp)
runcps.set_print_iter_info(args.iterations)
runcps.set_verbose(True)
runcps.set_file_pointer(fp)
runcps.compute_cps ()
print("BIC: ", runcps.get_bicvalue())
except randentropymod.Error as err:
print("Oops! error in the main function")
print(err)
exit(1)
except TypeError as err:
print(err)
exit(1)
fp.close()