forked from DingLi23/s2search
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature_masking.py
215 lines (191 loc) · 5.27 KB
/
feature_masking.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
211
212
213
214
215
import copy, json
import add_on
t = 'title'
abs = 'abstract'
v = 'venue'
y = 'year'
au = 'authors'
c = 'n_citations'
mp = {
't': t,
'abs': abs,
'v': v,
'y': y,
'au': au,
'c': c,
}
masking_options = {
'_origin': {
'plot_legend': "origin",
'should_mask': [],
'marker': '.',
'color': 'black'
},
't': {
'plot_legend': t,
'should_mask': [t],
'marker': '.',
'color': 'r'
},
'abs': {
'plot_legend': abs,
'should_mask':[abs],
'marker': ',',
'color': 'g'
},
'v': {
'plot_legend': v,
'should_mask':[v],
'marker': 'v',
'color': 'y'
},
'au': {
'plot_legend': au,
'should_mask':[au],
'marker': 'o',
'color': 'b'
},
'y': {
'plot_legend': y,
'should_mask':[y],
'marker': 'x',
'color': 'm'
},
'c': {
'plot_legend': c,
'should_mask':[c],
'marker': '*',
'color': 'c'
},
'rt1': {
'plot_legend': 'replaceing title1',
# 'should_mask': [t],
'should_replace': [t],
'marker': '<',
'color': 'black',
'replace_func': add_on.replace_title_1
},
'rt2': {
'plot_legend': 'replaceing title2',
# 'should_mask': [t],
'should_replace': [t],
'marker': '>',
'color': 'gold',
'replace_func': add_on.replace_title_2
},
}
def combination():
def backtracing(_set, tmp, comb, start):
if len(tmp) > 0:
comb.append(copy.deepcopy(tmp))
i = start
while i < len(_set):
tmp.append(_set[i])
backtracing(_set, tmp, comb, i + 1)
tmp.pop(len(tmp) - 1)
i += 1
_set = ['t', 'abs', 'v', 'au', 'y', 'c']
comb = []
backtracing(_set, [], comb, 0)
return comb
def get_comb_masking_options():
comb = combination()
comb_map = {}
color_range = [
'dimgray',
'saddlebrown',
'deeppink',
'red',
'gold',
'olivedrab',
'darkgreen',
'darkorange',
'cyan',
'navy',
'purple',
'teal',
]
marker_range = [
'1', '2', '3', '4',
'x', '*', '+'
]
ci = 0
mi = 0
for cb in comb:
key = ''.join(cb)
plot_legend = ''
should_mask = []
for k in cb:
plot_legend += k + ' & '
should_mask.append(mp[k])
if ci == len(color_range):
ci = 0
color = color_range[ci]
ci += 1
if mi == len(marker_range):
mi = 0
marker = marker_range[mi]
mi += 1
comb_map[key] = {
'plot_legend': plot_legend[:len(plot_legend) - 3],
'should_mask': should_mask,
'marker': marker,
'color': color
}
return comb_map
comb_map = get_comb_masking_options()
for key in comb_map:
if masking_options.get(key) == None:
masking_options[key] = comb_map[key]
def masking_with_option(original_paper_data, options):
cp = copy.deepcopy(original_paper_data)
for paper in cp:
if options.get('should_mask') is not None:
for masking_feature in options['should_mask']:
if masking_feature == 'authors':
paper['authors'] = []
# same as del paper['n_citations']
elif masking_feature == 'n_citations':
paper['n_citations'] = 0
# same as paper['year'] = ""
# elif masking_feature == 'year':
# del paper['year']
else:
paper[masking_feature] = " "
if options.get('should_replace') is not None:
for replace_feature in options['should_replace']:
paper[replace_feature] = options['replace_func'](paper[replace_feature])
return cp
def masking(original_paper_data, masking_option_keys = ["t", "abs", "v", "au", "y", "c", 'rt1']):
all_result = {}
for key in masking_option_keys:
result = masking_with_option(original_paper_data, masking_options[key])
all_result[key] = result
return all_result
if __name__ == '__main__':
# testing
papers = [
{
'title': 'Neural Networks are Great',
'abstract': 'Neural networks are known to be really great models. You should use them.',
'venue': 'Deep Learning Notions',
'authors': ['Sergey Feldman', 'Gottfried W. Leibniz'],
'year': 2019,
'n_citations': 100,
},
{
'title': 'Neural Networks are Terrible',
'abstract': 'Neural networks have only barely worked and we should stop working on them.',
'venue': 'JMLR',
'authors': ['Isaac Newton', 'Sergey Feldman'],
'year': 2009,
'n_citations': 5000
}
]
# ars = masking(papers)
# for key in ars.keys():
# print(f'masking {masking_options[key]}')
# print(json.dumps(ars[key]))
# print()
for k in masking_options:
print(k, masking_options[k].get('should_mask'), masking_options[k]['color'], masking_options[k]['plot_legend'] )