-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilabel_experiments.py
406 lines (335 loc) · 15.4 KB
/
multilabel_experiments.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python
import argparse
import json
import os
from os import listdir, path
import copy
from pathlib import Path
import re
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from bidict import bidict
from scipy import sparse
from sklearn.model_selection import cross_validate, StratifiedKFold, KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MultiLabelBinarizer
from echr.utils.folders import make_build_folder
from echr.utils.logger import getlogger
from echr.utils.cli import TAB
from rich.markdown import Markdown
from rich.console import Console
from rich.table import Table
from rich.progress import (
Progress,
BarColumn,
TimeRemainingColumn,
)
from rich.panel import Panel
from rich.tree import Tree
from echr_experiments.config import ROUND_DIGITS, \
SEED, \
MULTILABEL_DESC_OUTPUT_FILE, \
MULTILABEL_OUTPUT_FILE, \
MULTILABEL_ARTICLES, \
MULTILABEL_FLAVORS, \
K_FOLD, \
AS_TIME_SERIES, \
MULTILABEL_CLASSIFIERS, \
DEFAULT_FEATURE_THRESHOLD, \
ANALYSIS_PATH
from echr_experiments.format import format_filter_output, format_method_output
from echr_experiments.data import load_ECHR_instance, generate_datasets_descriptors
from echr_experiments.scorers import make_scorers, process_score, calculate_average_cm
from echr_experiments.utils import update_classifier_result, \
update_dataset_filter_result, \
update_dataset_metadata, \
update_dataset_result, \
update_article_desc
seed = SEED
result_file = MULTILABEL_OUTPUT_FILE
articles = MULTILABEL_ARTICLES
flavors = MULTILABEL_FLAVORS
k_fold = K_FOLD
as_time_series = AS_TIME_SERIES
feature_threshold = DEFAULT_FEATURE_THRESHOLD
classifiers = MULTILABEL_CLASSIFIERS
import warnings
from pandas.core.common import SettingWithCopyWarning
warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
log = getlogger()
def generate_outcomes_data(y_file, outcome_to_id, filter_threshold=100):
# Generate hot-one outcome matrix
with open(y_file) as file:
f = lambda x: {x.split(':')[0]:x.split(':')[1]}
outcomes = file.readlines()
outcomes = pd.DataFrame(outcomes)
outcomes[0] = outcomes[0].apply(lambda x: x.strip().split())
outcomes['caseid'] = outcomes[0].apply(lambda x: x[0])
outcomes[0] = outcomes[0].apply(lambda x: x[1])
# Generate hot-one outcome matrix
mlb = MultiLabelBinarizer(sparse_output=True)
outcomes = outcomes.join(
pd.DataFrame.sparse.from_spmatrix(
mlb.fit_transform(outcomes.pop(0)),
index=outcomes.index,
columns=mlb.classes_))
# Remove articles with not enough labels
to_drop = []
for c in outcomes.columns:
if c != 'caseid':
art, val = c.split(':')
if f'{art}:0' not in yououtcomes.columns:
to_drop.append(f'{art}:1')
if f'{art}:1' not in outcomes.columns:
to_drop.append(f'{art}:0')
if f'{art}:0' in outcomes.columns and f'{art}:1' in outcomes.columns:
sum_0 = outcomes[f'{art}:0'].sum()
sum_1 = outcomes[f'{art}:1'].sum()
if sum_0 + sum_1 < filter_threshold:
to_drop.append(c)
outcomes.drop(columns=to_drop, inplace=True)
return outcomes
return outcomes
def map_outcome(art, x):
if x[f'{art}:1'] == 1:
return 1
elif x[f'{art}:0'] == 1:
return 0
else:
return -1
def generate_outcomes_data(y_file, outcome_to_id, filter_threshold=100):
# Generate hot-one outcome matrix
with open(y_file) as file:
f = lambda x: {x.split(':')[0]:x.split(':')[1]}
outcomes = file.readlines()
outcomes = pd.DataFrame(outcomes)
outcomes[0] = outcomes[0].apply(lambda x: x.strip().split())
outcomes['caseid'] = outcomes[0].apply(lambda x: x[0])
outcomes[0] = outcomes[0].apply(lambda x: x[1:])
'''
# Generate hot-one outcome matrix
mlb = MultiLabelBinarizer(sparse_output=True)
outcomes = outcomes.join(
pd.DataFrame.sparse.from_spmatrix(
mlb.fit_transform(outcomes.pop(0)),
index=outcomes.index,
columns=mlb.classes_))
'''
'''
# Remove articles with not enough labels
to_drop = []
for c in outcomes.columns:
if c != 'caseid':
art, val = c.split(':')
if f'{art}:0' not in outcomes.columns:
to_drop.append(f'{art}:1')
if f'{art}:1' not in outcomes.columns:
to_drop.append(f'{art}:0')
if f'{art}:0' in outcomes.columns and f'{art}:1' in outcomes.columns:
sum_0 = outcomes[f'{art}:0'].sum()
sum_1 = outcomes[f'{art}:1'].sum()
if sum_0 + sum_1 < filter_threshold:
to_drop.append(c)
outcomes.drop(columns=to_drop, inplace=True)
'''
return outcomes
def _generate_outcomes_data(y_file, outcome_to_id, filter_threshold=100):
# Generate hot-one outcome matrix
with open(y_file) as file:
f = lambda x: {x.split(':')[0]:x.split(':')[1]}
outcomes = file.readlines()
outcomes = pd.DataFrame(outcomes)
outcomes[0] = outcomes[0].apply(lambda x: x.strip().split())
outcomes['caseid'] = outcomes[0].apply(lambda x: x[0])
outcomes[0] = outcomes[0].apply(lambda x: x[1:])
# Generate hot-one outcome matrix
mlb = MultiLabelBinarizer(sparse_output=True)
outcomes = outcomes.join(
pd.DataFrame.sparse.from_spmatrix(
mlb.fit_transform(outcomes.pop(0)),
index=outcomes.index,
columns=mlb.classes_))
# Remove articles with not enough labels
to_drop = []
for c in outcomes.columns:
if c != 'caseid':
art, val = c.split(':')
if f'{art}:0' not in outcomes.columns:
to_drop.append(f'{art}:1')
if f'{art}:1' not in outcomes.columns:
to_drop.append(f'{art}:0')
if f'{art}:0' in outcomes.columns and f'{art}:1' in outcomes.columns:
sum_0 = outcomes[f'{art}:0'].sum()
sum_1 = outcomes[f'{art}:1'].sum()
if sum_0 + sum_1 < filter_threshold:
to_drop.append(c)
#outcomes.replace(['0', 0], np.nan, inplace=True)
#for c in outcomes.columns:
# outcomes[c].loc[outcomes[c] == 0] = np.nan
#outcomes.drop(columns=to_drop, inplace=True)
#print(outcomes)
#exit(1)
#outcomes['decision'] = outcomes.notna().dot(outcomes.columns+',').str.rstrip(',')
#outcomes['labels'] = outcomes.apply(lambda x: [outcomes.columns[i] for i, e in enumerate(x) if e == 1], axis=1)
return outcomes
def load_dataset(X_file, min_threshold=0):
with open(X_file) as file:
f = lambda x: {x.split(':')[0]:x.split(':')[1]}
X = file.readlines()
X = pd.DataFrame(X)
X[0] = X[0].apply(lambda x: sorted(x.strip().split()))
#X['caseid'] = X[0].apply(lambda x: x[0])
#X[0] = X[0].apply(lambda x: x[1:])
# Generate hot-one outcome matrix
mlb = MultiLabelBinarizer(sparse_output=True)
X = X.join(
pd.DataFrame.sparse.from_spmatrix(
mlb.fit_transform(X.pop(0)),
index=X.index,
columns=mlb.classes_))
return X
def run(console, build, force):
__console = console
global print
print = __console.print
outcomes_path = 'data/input/datasets/'
raw_outcome_file = Path(outcomes_path) / 'outcomes.txt'
outcome_matrix_file = Path(outcomes_path) / 'outcomes_matrix.csv'
print(Markdown("- **Prepare outcome matrix**"))
OUTCOME_TO_ID = 'data/input/datasets/outcomes_variables.json'
with open(OUTCOME_TO_ID, 'r') as f:
outcome_to_id = json.load(f)
if True: #not os.path.isfile(outcome_matrix_file) or force:
print(TAB + '> Generate the outcome matrix [green][DONE]')
outcomes_matrix = generate_outcomes_data(raw_outcome_file, outcome_to_id, filter_threshold=100)
outcomes_matrix.to_csv(outcome_matrix_file)
else:
print(TAB + '> Load the outcome matrix [green][DONE]')
outcomes_matrix = pd.read_csv(outcome_matrix_file)
label_numbers = outcomes_matrix.apply(lambda x: len(x['0'].split()), axis=1)
label_numbers = label_numbers.value_counts()
fig = plt.figure()
ax = label_numbers.plot.bar(x='lab', y='val', rot=0)
for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
fig.savefig(Path(ANALYSIS_PATH) / 'labels_per_cases.png')
print(TAB + '> Generate the plot of labels counts [green][DONE]')
outcomes_matrix = _generate_outcomes_data(raw_outcome_file, outcome_to_id, filter_threshold=100)
metadata = outcomes_matrix[outcomes_matrix.columns[1:]]
metadata = pd.DataFrame(metadata.sum())
metadata['label'] = metadata.index
metadata['article'] = metadata.apply(lambda x: x.label.split(':')[0], axis=1)
summary = metadata[0].T
arts = metadata['article'].unique()
c_outcomes = bidict(outcome_to_id)
metadata = {}
for art in arts:
if art not in metadata:
metadata[art] = {}
metadata[art]['Article'] = c_outcomes.inverse[int(art)]
if f'{art}:1' in summary and f'{art}:0' in summary:
metadata[art]['Size'] = int(summary[f'{art}:1'] + summary[f'{art}:0'])
elif f'{art}:1' not in summary:
metadata[art]['Size'] = int(summary[f'{art}:0'])
else:
metadata[art]['Size'] = int(summary[f'{art}:1'])
if f'{art}:1' in summary:
metadata[art]['Violation'] = int(summary[f'{art}:1'])
else:
metadata[art]['Violation'] = 0
if f'{art}:0' in summary:
metadata[art]['No-Violation'] = int(summary[f'{art}:0'])
else:
metadata[art]['No-Violation'] = 0
metadata[art]['Prevalence'] = float(metadata[art]['Violation'] / (metadata[art]['Violation'] + metadata[art]['No-Violation']))
update_article_desc('Multilabel', metadata, MULTILABEL_DESC_OUTPUT_FILE)
print(TAB + '> Update dataset description [green][DONE]')
CM = [] # Confusion matrices
print(Markdown("- **Experiment summary**"))
FLAVORS = {'Descriptive only': 'descriptive.txt', 'Bag-of-Words only': 'BoW.txt', 'Descriptive and Bag-of-Words': 'descriptive+BoW.txt'}
print(f" | Flavors: {len(FLAVORS)}")
print(f" | Methods: {len(classifiers)}")
print(f" = {len(FLAVORS) * len(classifiers)} cross-validation procedures")
print(f" = Take some :coffee: or :tea: and relax")
try:
f = open (result_file, "r")
exp_results = json.loads(f.read())
print(TAB + '> Load existing results [green][DONE]')
except Exception as e:
exp_results = {}
print(TAB + '> No previous results [green][DONE]')
table = Table(title="Cross-Validation Summary")
table.add_column("Flavor", style="cyan", no_wrap=True)
table.add_column("Method", justify="right", style="blue")
table.add_column("Status", justify="right", style="green")
for i, flavor in enumerate(FLAVORS.keys()):
for k, method in enumerate(classifiers.keys()):
dataset_name = f'Multilabel - {flavor}'
status = exp_results.get(dataset_name, {}).get('methods', {}).get(method, None)
status = '[green]DONE' if status else None
table.add_row(flavor if k == 0 else None, method, status)
print(table)
for flavor, features_file in FLAVORS.items():
print(Panel(f'[bold yellow] Cross-Validation Flavor {flavor.upper()}'), justify="center")
print(Markdown("- **Prepare dataset**"))
dataset_path = Path(outcomes_path) / features_file
X = load_dataset(dataset_path)
X = pd.DataFrame(X)
X = X[X.index.isin(outcomes_matrix.index)]
print(TAB + '> Load the dataset [green][DONE]')
if flavor != 'Bag-of-Words':
# Remove '0:'
to_drop = [e for e in X.columns if e.startswith('0:')]
X.drop(columns=to_drop, inplace=True)
print(TAB + '> Drop unecessary columns [green][DONE]')
print(Markdown(f"- **Cross-Validate**"))
y = outcomes_matrix.drop(columns='caseid').to_numpy()#['decision']
o = {'name': f'Multilabel - {flavor}'}
dataset_name = o['name']
if dataset_name not in exp_results:
exp_results[dataset_name] = {}
update_dataset_result(dataset_name, o, result_file)
update_dataset_filter_result(dataset_name, o, result_file)
metadata = exp_results.get(dataset_name, {}).get('filter', {})
metadata['size'] = metadata.get('size', int(y.shape[0]))
update_dataset_metadata(dataset_name, metadata, result_file)
print(TAB + '> Generate dataset metadata [green][DONE]')
CM = []
for classifier_name, classifier in classifiers.items():
print(TAB + f'> [bold]{classifier_name}')
if exp_results.get(dataset_name, {}).get('methods', {}).get(classifier_name, None):
print(TAB + ' ⮡ Cross-Validation results already exist. [green][SKIP]')
else:
try:
scoring = make_scorers(multilabel=True, CM=CM)
cv = TimeSeriesSplit(n_splits=k_fold) if as_time_series \
else KFold(n_splits=k_fold)
scores = cross_validate(classifier, X, y,
cv=cv,
scoring=scoring,
return_train_score=True,
verbose=10,
n_jobs=-1, error_score='raise')
classifier_output = process_score(scores, scoring, seed, multilabel=True)
update_classifier_result(
dataset_name,
classifier_name,
classifier_output,
result_file
)
pass
except Exception as e:
print(e)
def main(args):
console = Console(record=True)
run(console, args.build, args.force)
def parse_args(parser):
args = parser.parse_args()
return args
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Multilabel experiments')
parser.add_argument('--build', type=str, default="./build/echr_database/")
parser.add_argument('-f', '--force', action='store_true')
args = parse_args(parser)
main(args)