forked from brmj/patern-recognition-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
61 lines (49 loc) · 1.96 KB
/
test.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
import sys
import pickle
import os
#from sklearn.externals import joblib
import SymbolData
import Classification
import Features
from sklearn.metrics import accuracy_score
usage = "Usage: $ python test.py stateFilename outdir (testFile.dat | inkmldir lgdir)"
def main(argv=None):
if argv is None:
argv = sys.argv[1:] #dirty trick to make this convenient in the interpreter.
if (len (argv) < 3 or len (argv) > 4):
print(("bad number of args:" , len(argv)))
print(usage)
else:
if (len( argv) == 3):
with open(argv[0], 'rb') as f:
exprs, ks = pickle.load(f)
else:
exprs = SymbolData.readInkmlDirectory(argv[2], argv[3])
#model, pca = joblib.load(argv[1])
with open(argv[0], 'rb') as f:
model, pca, keys = pickle.load(f)
#the following is a placeholder until I am sure we have propper analysis tools for evaluating our results if we preserve files.
# symbs = SymbolData.allSymbols(exprs)
# print("Normalizing")
# symbs = SymbolData.normalize(symbs, 99)
# print("Calculating features.")
# f = Features.features(symbs)
# if (pca != None):
# print ("doing PCA")
# f = pca.transform(f)
# print ("Classifying.")
# pred = model.predict(f)
# print( "Accuracy on testing set : ", accuracy_score(SymbolData.classNumbers(symbs, classes), pred))
#code to write out results goes here.
print ("Classifying")
truths, preds = Classification.classifyExpressions(exprs, keys, model, pca, showAcc = True)
print ("Writing LG files.")
i = 0
for expr in exprs:
#if (preds[i] != -1):
f = (lambda p: keys[p])
# expr.classes = map (f, preds[i])
expr.writeLG(argv[1],clss = map (f, preds[i]) )
i = i + 1
if __name__ == "__main__":
sys.exit(main())