-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_model.py~
213 lines (133 loc) · 3.39 KB
/
test_model.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# coding: utf-8
# In[1]:
import pandas as pd
import scipy as sp
import numpy as np
import csv
import sys
import os
import math
import random as rd
import matplotlib.pyplot as plt
# In[3]:
import TPSN_util as util
# In[5]:
MODELS = {}
MODELS['LDA']=False
MODELS['BTM']=False
MODELS['MLDA']=True
#dataset='hh122'
min_durantion = 30
T = 12
ITER = 50
args = sys.argv
if len(args) < 6:
print 'usage: test_model.py [model name] [number of Topics] [ITER] [t_th] [dataset]'
sys.exit()
model_name = args[1]
T = int(args[2])
ITER = int(args[3])
min_durantion = int(args[4])
dataset = args[5]
print "time threshold is "+str(min_durantion)
print model_name,T,ITER,dataset
LW = MODELS[model_name]
if model_name == 'LDA':
import LDA as model
elif model_name == 'BTM':
import BTM as model
elif model_name == 'MLDA':
import MLDA as model
print model_name
# In[6]:
path = './data/'+dataset+'/'
# In[7]:
rdf = pd.read_csv(path+'raw_filtered.csv',header=0)
group = pd.read_csv(path+'sensor_group.csv',header=0)
# In[8]:
activities = pd.read_csv(path+'acts.csv',header=0)
activities = activities.columns
activities
na = 0
for i in range(len(activities)):
if activities[i] == 'No_Annotation':
na = i+1
break
#print "na", na
# In[9]:
rdf['group']= np.array([0]*len(rdf))
for i in range(len(group.sensor.unique())):
rdf.loc[rdf.sensor==i+1,'group'] = int(group.loc[i,'group'])
# In[10]:
rdf = util.transformWord(rdf)
docs = util.segDocsByGrps(rdf,min_durantion)
V = max(rdf.word)
print 'V =',V
if LW:
lwd = rdf.word
L = max(lwd)
lidf = np.ones(L)
else:
lidf = []
lwd = []
# In[16]:
if model_name == 'LDA':
widf = np.ones(V)
else:
widf = np.ones((V,V))
# In[52]:
(wtp, lwtp, zd, totz) = model.GibbsSampler(widf, docs, rdf.word, T, ITER, lidf,lwd)
# In[55]:
fpath = path+model_name+'_T'+str(T)+'s'+str(min_durantion)+'/'
if not os.path.exists(fpath):
os.makedirs(fpath)
if model_name == 'LDA':
model.visualTopic(T, fpath, dataset, wtp)
else:
util.visualTopic(T, fpath, dataset, wtp)
np.save(fpath+'wtp.npy',wtp)
if(len(lwtp)>0):
np.save(fpath+'lwtp.npy',lwtp)
# In[56]:
if LW:
(nwtp,nlwtp) = util.nomalise(wtp, lwtp)
elif model_name != 'LDA':
(nwtp,nlwtp) = util.nomalise(wtp)
else:
nlwtp=[]
nwtp=wtp
wd = rdf.word
if len(nlwtp) == 0:
(pzd,prob) = model.predictDoc(nwtp,wd,docs)
else:
(pzd,prob) = model.predictDoc(nwtp,nlwtp,wd,lwd,docs)
# In[189]:
rdf = util.setDocID(rdf,docs)
#print len(docs),zd.shape
rdf = util.setTopicID(rdf, docs, pzd)
rdf.to_csv(fpath+'processed.csv',index=False,encoding='utf-8')
stat=util.calcTopicStat(rdf,activities)
stat.to_csv(fpath+'topic_act_stat.csv')
stat=util.calcActStat(rdf,activities)
stat.to_csv(fpath+'act_topic_stat.csv')
# In[190]:
topics = util.getSegs(np.array(rdf.topic))
# In[191]:
acts = util.getSegs(np.array(rdf.activity))
# In[192]:
wf = open(fpath+'performance.csv','w')
# In[193]:
labeled = rdf.loc[rdf.activity!=na]
# In[194]:
labeled.index = np.arange(labeled.shape[0])
# In[196]:
topics = util.getSegs(np.array(labeled.topic))
# In[198]:
acts = util.getSegs(np.array(labeled.activity))
# In[199]:
wf.write('labeled data'+','+'fraction:'+str(len(topics)/float(len(acts)))+','+'seg err:'+str(util.segError(topics,labeled))+'\n')
wf.write('labeled data FMIndex: '+str(util.calFMIndex(labeled))+'\n')
wf.write('labeled data Precision, Recall: '+str(util.calPR(labeled)))
wf.close()
# In[125]:
# In[ ]: