-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·536 lines (454 loc) · 23 KB
/
main.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
from typing import Tuple, Union, Literal, List
from lifelines import CoxPHFitter
from lifelines.exceptions import ConvergenceError
from pyspark import TaskContext, Broadcast
from pyspark.sql import SparkSession
from sklearn.cluster import KMeans, SpectralClustering
from core import run_experiment, run_times_experiment, run_times_experiment_sequential
from load_balancer_parameters import SVMParameters, ClusteringParameters, RFParameters
from utils import get_columns_from_df, DatasetName, MASTER_CONNECTION_URL, ModelName, ClusteringAlgorithm, \
ClusteringScoringMethod
import pandas as pd
from sklearn.model_selection import cross_validate
import numpy as np
from sksurv.ensemble import RandomSurvivalForest
from sksurv.svm import FastKernelSurvivalSVM
import logging
import time
from pyspark import SparkConf, SparkContext
from typing import Optional
import socket
from datetime import datetime
from multiprocessing import Process, Queue
from filelock import FileLock
# Enables info logging
logging.getLogger().setLevel(logging.INFO)
# If True, runs Feature Selection experiment. If False, runs times experiment
RUN_TIMES_EXPERIMENT: bool = False
# Number of cores used by the worker to compute the Cross Validation. -1 = use all
# This can be tricky since Spark assigns a certain number of cores to the executor, so you cannot be sure that it is
# always the number specified here, if possible set the same as CORES_PER_EXECUTOR.
N_JOBS = 1
# Number of folds to compute in the Cross Validation
NUMBER_OF_CV_FOLDS: int = 10
# To get the training score or not
RETURN_TRAIN_SCORE = True
# To replicate randomness
# RANDOM_STATE: Optional[int] = None
RANDOM_STATE: Optional[int] = 13
# If True, load balancer is used to generate Spark partitions
USE_LOAD_BALANCER: bool = False
# USE_LOAD_BALANCER: bool = True
# Number of independent complete runs to get the best parameters or number of experiments to compute all the times
# NUMBER_OF_INDEPENDENT_RUNS = 30
# NUMBER_OF_INDEPENDENT_RUNS = 2
NUMBER_OF_INDEPENDENT_RUNS = 1
# If True runs in Spark, otherwise in sequential.
# IMPORTANT: if this is True, set the parameter --driver-memory="6g" to avoid memory issues on sequential experiments
# RUN_IN_SPARK: bool = True
RUN_IN_SPARK: bool = False
# If True, runs the improved version of BBHA (only used if RUN_IN_SPARK is False, for the moment...). None to try
# both versions
RUN_IMPROVED_BBHA: Optional[bool] = False
# Identifier of the dataset to use
DATASET: DatasetName = 'Breast_Invasive_Carcinoma'
# Number of iterations for the BBHA algorithm
# N_ITERATIONS = 3
N_ITERATIONS = 2
# N_ITERATIONS = 30
# Number of stars for the BBHA algorithm
N_STARS = 3
# N_STARS = 15
# If True Random Forest is used as classificator. SVM otherwise
# MODEL_TO_USE: ModelName = 'clustering'
MODEL_TO_USE: ModelName = 'svm'
# MODEL_TO_USE: ModelName = 'rf'
# RF parameters
# Number of trees in Random Forest
RF_N_ESTIMATORS: int = 5
# SVM parameters
# If True, a regression task is performed, otherwise it executes a ranking task
IS_SVM_REGRESSION: bool = True
# Max number of SVM iterations
MAX_ITERATIONS: int = 1000
# SVM kernel function. NOTE: 'sigmoid' presents many NaNs values and 'precomputed' doesn't work in this context
SVM_KERNEL: Literal["linear", "poly", "rbf", "sigmoid", "cosine", "precomputed"] = 'linear'
SVM_OPTIMIZER: Literal["avltree", "rbtree"] = 'avltree'
# Clustering model to use
CLUSTERING_ALGORITHM: ClusteringAlgorithm = 'kmeans' # TODO: add more
# Number of clusters to group by molecule expressions during clustering algorithm
NUMBER_OF_CLUSTERS: int = 2
# Scoring method to use in the clustering algorithms
CLUSTERING_SCORING_METHOD: ClusteringScoringMethod = 'log_likelihood'
# To use a Broadcast value instead of a pd.DataFrame
USE_BROADCAST = True
# If True, means that fitness function returns a value that must be maximized. Otherwise, it must be minimized
MORE_IS_BETTER: bool = True # For the moment all the metrics need to be maximized
# Only if RUN_IN_SPARK is set to True the following parameters are used
# Executors per instance of each worker
EXECUTORS: Optional[str] = "1"
# Cores on each executor
CORES_PER_EXECUTOR: Optional[str] = None
# RAM to use per executor
MEMORY_PER_EXECUTOR: str = "6g"
# If True it logs all the star values in the terminal
# DEBUG: bool = False
DEBUG: bool = True
# Classificator
if MODEL_TO_USE == 'rf':
CLASSIFIER = RandomSurvivalForest(n_estimators=RF_N_ESTIMATORS,
min_samples_split=10,
min_samples_leaf=15,
max_features="sqrt",
n_jobs=N_JOBS,
random_state=RANDOM_STATE)
elif MODEL_TO_USE == 'svm':
rank_ratio = 0.0 if IS_SVM_REGRESSION else 1.0
CLASSIFIER = FastKernelSurvivalSVM(rank_ratio=rank_ratio, max_iter=MAX_ITERATIONS, tol=1e-5, kernel=SVM_KERNEL,
optimizer=SVM_OPTIMIZER, random_state=RANDOM_STATE)
else:
CLASSIFIER = None
def get_clustering_model() -> Union[KMeans, SpectralClustering]:
"""Gets the specified clustering model to train"""
if CLUSTERING_ALGORITHM == 'kmeans':
return KMeans(n_clusters=NUMBER_OF_CLUSTERS, random_state=RANDOM_STATE)
elif CLUSTERING_ALGORITHM == 'spectral':
return SpectralClustering(n_clusters=NUMBER_OF_CLUSTERS, random_state=RANDOM_STATE)
raise Exception('Invalid CLUSTERING_ALGORITHM parameter')
def compute_cross_validation_spark_f(subset: pd.DataFrame, y: np.ndarray, q: Queue):
"""
Computes a cross validations to get the concordance index in a Spark environment
:param subset: Subset of features to compute the cross validation
:param y: Y data
:param q: Queue to return Process result
"""
# Sets the default score (in case of error) to -1.0 if more_is_better (C-Index), otherwise 0.0
error_score = -1.0 if MORE_IS_BETTER else 0.0
try:
n_features = subset.shape[1]
# Locks to prevent multiple partitions in one worker getting all cores and degrading the performance
logging.info(f'Waiting lock to compute CV with {n_features} features')
with FileLock(f"/home/big_data/svm-surv.lock"):
logging.info('File lock acquired, computing CV...')
if MODEL_TO_USE == 'clustering':
start = time.time()
# Groups using the selected clustering algorithm
clustering_model = get_clustering_model()
clustering_result = clustering_model.fit(subset.values)
# Generates a DataFrame with a column for time, event and the group
labels = clustering_result.labels_
dfs: List[pd.DataFrame] = []
for cluster_id in range(NUMBER_OF_CLUSTERS):
current_group_y = y[np.where(labels == cluster_id)]
dfs.append(
pd.DataFrame({'E': current_group_y['event'], 'T': current_group_y['time'], 'group': cluster_id})
)
df = pd.concat(dfs)
# Fits a Cox Regression model using the column group as the variable to consider
try:
cph = CoxPHFitter().fit(df, duration_col='T', event_col='E')
# This documentation recommends using log-likelihood to optimize:
# https://lifelines.readthedocs.io/en/latest/fitters/regression/CoxPHFitter.html#lifelines.fitters.coxph_fitter.SemiParametricPHFitter.score
fitness_value = cph.score(df, scoring_method=CLUSTERING_SCORING_METHOD)
end_time = time.time()
worker_execution_time = end_time - start # Duplicated to not consider consumed time by all the metrics below
except ConvergenceError:
fitness_value = -1
end_time = None
worker_execution_time = -1
metric_description = 'Log Likelihood (higher is better)'
mean_test_time = 0.0
mean_train_score = 0.0
else:
logging.info(f'Computing CV ({NUMBER_OF_CV_FOLDS} folds) with {MODEL_TO_USE} model')
start = time.time()
cv_res = cross_validate(
CLASSIFIER,
subset,
y,
cv=NUMBER_OF_CV_FOLDS,
n_jobs=N_JOBS,
return_estimator=True,
return_train_score=RETURN_TRAIN_SCORE
)
fitness_value = cv_res['test_score'].mean() # This is the C-Index
end_time = time.time()
worker_execution_time = end_time - start # Duplicated to not consider consumed time by all the metrics below
metric_description = 'Concordance Index (higher is better)'
mean_test_time = np.mean(cv_res['score_time'])
mean_train_score = cv_res['train_score'].mean() if RETURN_TRAIN_SCORE else 0.0
logging.info(f'Fitness function with {n_features} features: {worker_execution_time} seconds | '
f'{metric_description}: {fitness_value}')
partition_id = TaskContext().partitionId()
# Gets a time-lapse description to check if some worker is lazy
start_desc = datetime.fromtimestamp(start).strftime("%H:%M:%S")
end_desc = datetime.fromtimestamp(end_time).strftime("%H:%M:%S") if end_time else '-'
time_description = f'{start_desc} - {end_desc}'
# 'res' is only defined when using SVM or RF
# Gets number of iterations (only for SVM)
if MODEL_TO_USE == 'svm':
times_by_iteration = []
total_number_of_iterations = []
for estimator, fit_time in zip(cv_res['estimator'], cv_res['fit_time']):
# Scikit-surv doesn't use BaseLibSVM. So it doesn't have 'n_iter_' attribute
# number_of_iterations += np.sum(estimator.n_iter_)
number_of_iterations = estimator.optimizer_result_.nit
time_by_iterations = fit_time / number_of_iterations
times_by_iteration.append(time_by_iterations)
total_number_of_iterations.append(number_of_iterations)
mean_times_by_iteration = np.mean(times_by_iteration)
mean_total_number_of_iterations = np.mean(total_number_of_iterations)
else:
mean_times_by_iteration = 0.0
mean_total_number_of_iterations = 0.0
q.put([
fitness_value,
worker_execution_time,
partition_id,
socket.gethostname(),
subset.shape[1],
time_description,
mean_times_by_iteration,
mean_test_time,
mean_total_number_of_iterations,
mean_train_score
])
except Exception as ex:
logging.error('An exception has occurred in the fitness function:')
logging.exception(ex)
# Returns empty values
q.put([
error_score, # Fitness value,
-1.0, # Worker time,
-1.0, # Partition ID,
'', # Host name,
0, # Number of features,
'', # Time description,
-1.0, # Mean times_by_iteration,
-1.0, # Mean test_time,
-1.0, # Mean total_number_of_iterations,
error_score # Mean train_score
])
def compute_cross_validation_spark(
subset: Union[pd.DataFrame, Broadcast],
index_array: np.ndarray,
y: np.ndarray,
is_broadcast: bool
) -> Tuple[float, float, int, str, int, str, float, float, float, float]:
"""
Calls fitness inside a Process to prevent issues with memory leaks in Python.
More info: https://stackoverflow.com/a/71700592/7058363
:param is_broadcast: if True, the subset is a Broadcast instance
:param index_array: Binary array where 1 indicates that the feature in that position must be included
:param subset: Subset of features to compute the cross validation
:param y: Y data
:return: Result tuple with [0] -> fitness value, [1] -> execution time, [2] -> Partition ID, [3] -> Hostname,
[4] -> number of evaluated features, [5] -> time-lapse description, [6] -> time by iteration and [7] -> avg test time
[8] -> mean of number of iterations of the model inside the CV, [9] -> train score
"""
# If broadcasting is enabled, the retrieves the Broadcast instance value
x_values = subset.value if is_broadcast else subset
q = Queue()
parsed_data = get_columns_from_df(index_array, x_values)
p = Process(target=compute_cross_validation_spark_f, args=(parsed_data, y, q))
p.start()
process_result = q.get()
p.join()
return process_result
def compute_cross_validation_sequential(df: pd.DataFrame, index_array: np.ndarray, y: np.ndarray,
_is_broadcast: bool
) -> Tuple[float, float, None, None, int, str, float, float, float, float]:
"""
Computes CV to get the Concordance Index
:param df: Subset of features to be used in the model evaluated in the CrossValidation
:param index_array: Binary array where 1 indicates that the feature in that position must be included
:param y: Classes.
:param _is_broadcast: Used for compatibility with Spark fitness function.
:return: Average of the C-Index obtained in each CrossValidation fold
"""
subset = get_columns_from_df(index_array, df)
n_features = subset.shape[1]
if MODEL_TO_USE == 'clustering':
start = time.time()
# Groups using the selected clustering algorithm
clustering_model = get_clustering_model()
clustering_result = clustering_model.fit(subset.values)
# Generates a DataFrame with a column for time, event and the group
labels = clustering_result.labels_
dfs: List[pd.DataFrame] = []
for cluster_id in range(NUMBER_OF_CLUSTERS):
current_group_y = y[np.where(labels == cluster_id)]
dfs.append(
pd.DataFrame({'E': current_group_y['event'], 'T': current_group_y['time'], 'group': cluster_id})
)
df = pd.concat(dfs)
# Fits a Cox Regression model using the column group as the variable to consider
try:
cph = CoxPHFitter().fit(df, duration_col='T', event_col='E')
# This documentation recommends using log-likelihood to optimize:
# https://lifelines.readthedocs.io/en/latest/fitters/regression/CoxPHFitter.html#lifelines.fitters.coxph_fitter.SemiParametricPHFitter.score
fitness_value = cph.score(df, scoring_method=CLUSTERING_SCORING_METHOD)
end_time = time.time()
worker_execution_time = end_time - start # Duplicated to not consider consumed time by all the metrics below
except ConvergenceError:
fitness_value = -1
end_time = None
worker_execution_time = -1
metric_description = 'Log Likelihood (higher is better)'
mean_test_time = 0.0
mean_train_score = 0.0
else:
logging.info(f'Computing CV ({NUMBER_OF_CV_FOLDS} folds) with {MODEL_TO_USE} model')
start = time.time()
cv_res = cross_validate(
CLASSIFIER,
subset,
y,
cv=NUMBER_OF_CV_FOLDS,
n_jobs=N_JOBS,
return_estimator=True,
return_train_score=RETURN_TRAIN_SCORE
)
fitness_value = cv_res['test_score'].mean() # This is the C-Index
end_time = time.time()
worker_execution_time = end_time - start # Duplicated to not consider consumed time by all the metrics below
metric_description = 'Concordance Index (higher is better)'
mean_test_time = np.mean(cv_res['score_time'])
mean_train_score = cv_res['train_score'].mean() if RETURN_TRAIN_SCORE else 0.0
logging.info(f'Fitness function with {n_features} features: {worker_execution_time} seconds | '
f'{metric_description}: {fitness_value}')
# Gets a time-lapse description to check if some worker is lazy
start_desc = datetime.fromtimestamp(start).strftime("%H:%M:%S")
end_desc = datetime.fromtimestamp(end_time).strftime("%H:%M:%S") if end_time else '-'
time_description = f'{start_desc} - {end_desc}'
# 'res' is only defined when using SVM or RF
# Gets number of iterations (only for SVM)
if MODEL_TO_USE == 'svm':
times_by_iteration = []
total_number_of_iterations = []
for estimator, fit_time in zip(cv_res['estimator'], cv_res['fit_time']):
# Scikit-surv doesn't use BaseLibSVM. So it doesn't have 'n_iter_' attribute
# number_of_iterations += np.sum(estimator.n_iter_)
number_of_iterations = estimator.optimizer_result_.nit
time_by_iterations = fit_time / number_of_iterations
times_by_iteration.append(time_by_iterations)
total_number_of_iterations.append(number_of_iterations)
mean_times_by_iteration = np.mean(times_by_iteration)
mean_total_number_of_iterations = np.mean(total_number_of_iterations)
else:
mean_times_by_iteration = 0.0
mean_total_number_of_iterations = 0.0
# This data is not used in the current version of the algorithm (sequential).
# Adds them to the result to keep the same format as the Spark version
partition_id = None
host_name = None
return (fitness_value, worker_execution_time, partition_id, host_name, n_features, time_description,
mean_times_by_iteration, mean_test_time, mean_total_number_of_iterations, mean_train_score)
def main():
# Generates parameters to train the ML model for load balancing and
load_balancer_parameters = None
if MODEL_TO_USE == 'svm':
task = 'regression' if IS_SVM_REGRESSION else 'ranking'
parameters_description = f'{task}_{MAX_ITERATIONS}_max_iterations_{SVM_OPTIMIZER}_optimizer_{SVM_KERNEL}_kernel'
if USE_LOAD_BALANCER:
load_balancer_parameters = SVMParameters(SVM_KERNEL, SVM_OPTIMIZER)
else:
if MODEL_TO_USE == 'rf':
parameters_description = f'{RF_N_ESTIMATORS}_trees'
if USE_LOAD_BALANCER:
load_balancer_parameters = RFParameters(number_of_trees=RF_N_ESTIMATORS)
else:
clustering_scoring_method = CLUSTERING_SCORING_METHOD.replace('_', '-')
parameters_description = (f'{NUMBER_OF_CLUSTERS}_clusters_{CLUSTERING_ALGORITHM}_'
f'algorithm_{clustering_scoring_method}_score_method')
if USE_LOAD_BALANCER:
load_balancer_parameters = ClusteringParameters(algorithm=CLUSTERING_ALGORITHM,
number_of_clusters=NUMBER_OF_CLUSTERS,
scoring_method=CLUSTERING_SCORING_METHOD)
# Spark settings
app_name = f"BBHA_{time.time()}".replace('.', '_')
if RUN_IN_SPARK:
conf = SparkConf().setMaster(MASTER_CONNECTION_URL).setAppName(app_name)
if EXECUTORS is not None:
conf = conf.set("spark.executor.instances", EXECUTORS)
if CORES_PER_EXECUTOR is not None:
conf = conf.set("spark.executor.cores", CORES_PER_EXECUTOR)
if MEMORY_PER_EXECUTOR is not None:
conf = conf.set("spark.executor.memory", MEMORY_PER_EXECUTOR)
# Gets Spark context
spark = SparkSession.builder.config(conf=conf).getOrCreate()
sc = spark.sparkContext
sc.setLogLevel("ERROR")
# Gets the number of workers
spark_2 = SparkContext.getOrCreate(conf=conf)
sc2 = spark_2._jsc.sc()
number_of_workers = len([executor.host() for executor in
sc2.statusTracker().getExecutorInfos()]) - 1 # Subtract 1 to discard the master
fitness_function = compute_cross_validation_spark
else:
fitness_function = compute_cross_validation_sequential
sc = None
number_of_workers = 0 # No Spark, no workers
run_improved_bbha = False if RUN_IN_SPARK else RUN_IMPROVED_BBHA # TODO: improved BBHA it's not implemented for Spark right now
add_epsilon = MODEL_TO_USE == 'svm'
if RUN_TIMES_EXPERIMENT:
# Runs an experiment to store execution times, models parameters and other data to train a load balancer
if RUN_IN_SPARK:
run_times_experiment(
app_name=app_name,
compute_cross_validation=compute_cross_validation_spark,
n_iterations=N_ITERATIONS,
number_of_independent_runs=NUMBER_OF_INDEPENDENT_RUNS,
n_stars=N_STARS,
load_balancer_parameters=load_balancer_parameters,
sc=sc,
metric_description='concordance index',
add_epsilon=add_epsilon, # Epsilon is needed only by the SVM
dataset=DATASET,
number_of_workers=number_of_workers,
model_name=MODEL_TO_USE,
parameters_description=parameters_description,
use_broadcasts_in_spark=USE_BROADCAST,
debug=DEBUG
)
else:
# Runs sequentially
run_times_experiment_sequential(
compute_cross_validation=compute_cross_validation_sequential,
n_iterations=NUMBER_OF_INDEPENDENT_RUNS,
metric_description='concordance index',
add_epsilon=add_epsilon, # Epsilon is needed only by the SVM
model_name=MODEL_TO_USE,
parameters_description=parameters_description,
)
else:
# For clustering there are two metrics: log-likelihood and concordance-index
if MODEL_TO_USE == 'clustering':
metric_description = CLUSTERING_SCORING_METHOD.replace('_', '-')
else:
# For the rest of the models (SVM/RF) there's only one metric: concordance-index
metric_description = 'concordance-index'
# Runs normal Feature Selection experiment
run_experiment(
app_name=app_name,
more_is_better=MORE_IS_BETTER,
run_improved_bbha=run_improved_bbha,
run_in_spark=RUN_IN_SPARK,
n_stars=N_STARS,
random_state=RANDOM_STATE,
compute_cross_validation=fitness_function,
sc=sc,
metric_description=metric_description,
add_epsilon=add_epsilon, # Epsilon is needed only by the SVM
debug=DEBUG,
dataset=DATASET,
load_balancer_parameters=load_balancer_parameters,
number_of_independent_runs=NUMBER_OF_INDEPENDENT_RUNS,
n_iterations=N_ITERATIONS,
number_of_workers=number_of_workers,
model_name=MODEL_TO_USE,
parameters_description=parameters_description,
use_broadcasts_in_spark=USE_BROADCAST
)
if __name__ == '__main__':
main()