-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmushroom.py
170 lines (143 loc) · 8.26 KB
/
mushroom.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
import argparse
import os
from mushroom.config import defaultGammaLst
from mushroom import get_accuracy_precision_recall_from_series
from mushroom import get_accuracy_precision_recall_from_series_with_stem_length
from mushroom import analysis_with_different_length_stems
from mushroom import mushroom_triple_classification_with_different_pars
from mushroom.config import tekee2vecFiles, tekeh2vecFiles,transe2vecFiles, mushroomStrucFiles
from mushroom.config import mushroomPaths, mushroomTrainFiles, mushroomValidFiles, mushroomTestFiles
from mushroom.config import mushroomLogPaths, mushroomLogFiles, mushroomTCResults, relNballWNFile
from mushroom.config import ballMarginLst, dataPath, resultDir, KGS, e2vFlags
from mushroom.config import defaultStemLst, defaultEnhanceStem, defaultEnlargeMR, defaultL0
from mushroom.config import defaultR0, defaultAddDim, defaultEnrichLevel, defaultMaxTrueTails, defaultMPInterSec
from mushroom.config import defaultStemRel, defaultSubspaceDim, defaultWidthOfPLC
from mushroom.util import download_nball_files
"""
# usage 0: download datasets
$ python mushroom.py --func download
# usage 1: triple classification
$ python mushroom.py --func tc --kg Wordnet18|Wordnet11|Freebase13|all --e2v TH|TE|TransE|all
# usage 2: visualize precision - gamma, recall - gamma, accuracy - gamma
$ python mushroom.py --vis_gamma recall --kg Wordnet18
# usage 3: visualize precision - length-of-type-chain
$ python mushroom.py --vis_length precision --kg Wordnet18
# usage 4: visualize max_accuracy - length-of-type-chain, which-gamma
$ python mushroom.py --vis_mg accuracy --kg Wordnet18 --legendLoc lower right
"""
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--vis_gamma')
parser.add_argument('--gammaList', type=float, nargs='*',
default=defaultGammaLst)
parser.add_argument('--stemList', type=int, nargs='*',
default=defaultStemLst)
parser.add_argument('--legendLoc', nargs='*', default=['upper', 'right'])
parser.add_argument('--vis_length')
parser.add_argument('--vis_mg')
parser.add_argument('--kg')
parser.add_argument('--e2v')
parser.add_argument('--func')
args = parser.parse_args()
"""
$ python mushroom.py --vis_gamma recall --kg Wordnet18 --legendLoc center right
"""
if args.vis_gamma and args.kg and args.gammaList and args.legendLoc:
dataPattern = os.path.join(dataPath, args.kg, resultDir, "MRTCresult")
if 'all' in e2vFlags:
e2vFlags.remove('all')
if 'all' in KGS:
KGS.remove('all')
get_accuracy_precision_recall_from_series(dataSet=dataPattern,
gammaLst=args.gammaList,
result=args.vis_gamma,
legendloc=" ".join(args.legendLoc))
"""
$ python mushroom.py --vis_length accuracy --kg Wordnet18 --legendLoc lower right
"""
if args.vis_length and args.kg and args.stemList and args.legendLoc:
dataPattern = os.path.join(dataPath, args.kg, resultDir, "MRTCresult")
if 'all' in e2vFlags:
e2vFlags.remove('all')
if 'all' in KGS:
KGS.remove('all')
analysis_with_different_length_stems(dataSet=dataPattern,
target=args.vis_length,
stemLst=args.stemList,
legendloc=" ".join(args.legendLoc))
"""
$ python mushroom.py --vis_mg accuracy --kg Wordnet18 --legendLoc lower right
"""
if args.vis_mg and args.kg and args.stemList and args.legendLoc:
dataPattern = os.path.join(dataPath, args.kg, resultDir, "MRTCresult")
if 'all' in e2vFlags:
e2vFlags.remove('all')
if 'all' in KGS:
KGS.remove('all')
get_accuracy_precision_recall_from_series_with_stem_length(dataSet=dataPattern,
result=args.vis_mg,
stemHeight=args.stemList,
legendloc=" ".join(args.legendLoc))
"""
$ python mushroom.py --func tc --kg Wordnet18 --e2v TH
"""
if args.func == "tc" and args.kg in KGS and args.e2v in e2vFlags:
def process_one_kg(oneKg=None, entityEmbeddings=[]):
mushroomPath = mushroomPaths[oneKg]
mushroomStrucFile = mushroomStrucFiles[oneKg]
mushroomTrainFile = mushroomTrainFiles[oneKg]
mushroomValidFile = mushroomValidFiles[oneKg]
mushroomTestFile = mushroomTestFiles[oneKg]
mushroomLogPath = mushroomLogPaths[oneKg]
mushroomLogFile = mushroomLogFiles[oneKg]
mushroomTCResult = mushroomTCResults[oneKg]
resultPath = os.path.join(dataPath, oneKg, resultDir)
if not os.path.exists(resultPath):
os.makedirs(resultPath)
for e2vFile in entityEmbeddings:
mushroom_triple_classification_with_different_pars(threadNum=1,
testTripleFile=mushroomTestFile,
trainTripeFile=mushroomTrainFile,
validTripleFile=mushroomValidFile,
mushroomStemRel=defaultStemRel,
subspaceDim=defaultSubspaceDim,
widthOfPLC=defaultWidthOfPLC,
plcFile=mushroomStrucFile,
maxTrueTails=defaultMaxTrueTails,
enhanceStem=defaultEnhanceStem,
e2vecFile=e2vFile, ballMarginLst=ballMarginLst,
addDim=defaultAddDim, L0=defaultL0,
R0=defaultR0, enrichLevel=defaultEnrichLevel,
enlargeMR=defaultEnlargeMR,
mPInterSec=defaultMPInterSec, multiParentDicFile="",
mushroomGroundTruth=mushroomTestFile,
relNballWNFile=relNballWNFile,
mushroomPath=mushroomPath,
logPath=mushroomLogPath, mainLog=mushroomLogFile,
mushroomResultFile=mushroomTCResult)
if not args.kg == 'all':
if args.e2v == "TH":
e2vFiles = [tekeh2vecFiles[args.kg]]
elif args.e2v == "TE":
e2vFiles = [tekee2vecFiles[args.kg]]
elif args.e2v == "TransE":
e2vFiles = [transe2vecFiles[args.kg]]
elif args.e2v == "all":
e2vFiles = [tekeh2vecFiles[args.kg], tekee2vecFiles[args.kg], transe2vecFiles[args.kg]]
process_one_kg(oneKg=args.kg, entityEmbeddings=e2vFiles)
else:
for aKG in KGS:
if aKG == "all": continue
if args.e2v == "TH":
e2vFiles = [tekeh2vecFiles[aKG]]
elif args.e2v == "TE":
e2vFiles = [tekee2vecFiles[aKG]]
elif args.e2v == "TransE":
e2vFiles = [transe2vecFiles[aKG]]
elif args.e2v == "all":
e2vFiles = [tekeh2vecFiles[aKG], tekee2vecFiles[aKG], transe2vecFiles[aKG]]
process_one_kg(oneKg=aKG, entityEmbeddings=e2vFiles)
elif args.func == 'download':
download_nball_files(dataPath)
if __name__ == "__main__":
main()