diff --git a/asgl/asgl.py b/asgl/asgl.py index edcb7dc..0e021f5 100644 --- a/asgl/asgl.py +++ b/asgl/asgl.py @@ -1,3 +1,4 @@ +import sys import functools import itertools import logging @@ -7,7 +8,7 @@ import numpy as np from sklearn.metrics import mean_absolute_error, median_absolute_error, mean_squared_error -logger = logging.getLogger(__name__) +logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') class ASGL: @@ -67,7 +68,7 @@ def __model_checker(self): if self.model in self.valid_models: return True else: - logger.error(f'{self.model} is not a valid model. Valid models are {self.valid_models}') + logging.error(f'{self.model} is not a valid model. Valid models are {self.valid_models}') return False def __penalization_checker(self): @@ -83,8 +84,8 @@ def __penalization_checker(self): if (self.penalization in self.valid_penalizations) or (self.penalization is None): return True else: - logger.error(f'{self.penalization} is not a valid penalization. ' - f'Valid penalizations are {self.valid_penalizations} or None') + logging.error(f'{self.penalization} is not a valid penalization. ' + f'Valid penalizations are {self.valid_penalizations} or None') return False def __dtype_checker(self): @@ -182,7 +183,7 @@ def __preprocessing_itertools_param(self, lambda_vector, alpha_vector, lasso_wei param = itertools.product(lambda_vector, alpha_vector, lasso_weights_list, gl_weights_list) else: param = None - logger.error(f'Error preprocessing input parameters') + logging.error(f'Error preprocessing input parameters') param = list(param) return param @@ -193,7 +194,7 @@ def __preprocessing(self): """ # Run the input_checker to verify that the inputs have the correct format if self.__input_checker() is False: - logger.error('incorrect input parameters') + logging.error('incorrect input parameters') raise ValueError('incorrect input parameters') # Defines param as None for the unpenalized model if self.penalization is None: @@ -270,7 +271,7 @@ def unpenalized_solver(self, x, y): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = beta_var.value beta_sol[np.abs(beta_sol) < self.tol] = 0 return [beta_sol] @@ -316,11 +317,11 @@ def lasso(self, x, y, param): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = beta_var.value beta_sol[np.abs(beta_sol) < self.tol] = 0 beta_sol_list.append(beta_sol) - logger.debug('Function finished without errors') + logging.debug('Function finished without errors') return beta_sol_list def gl(self, x, y, group_index, param): @@ -376,7 +377,7 @@ def gl(self, x, y, group_index, param): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = np.concatenate([b.value for b in beta_var], axis=0) beta_sol[np.abs(beta_sol) < self.tol] = 0 beta_sol_list.append(beta_sol) @@ -439,7 +440,7 @@ def sgl(self, x, y, group_index, param): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = np.concatenate([b.value for b in beta_var], axis=0) beta_sol[np.abs(beta_sol) < self.tol] = 0 beta_sol_list.append(beta_sol) @@ -486,11 +487,11 @@ def alasso(self, x, y, param): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = beta_var.value beta_sol[np.abs(beta_sol) < self.tol] = 0 beta_sol_list.append(beta_sol) - logger.debug('Function finished without errors') + logging.debug('Function finished without errors') return beta_sol_list def agl(self, x, y, group_index, param): @@ -546,7 +547,7 @@ def agl(self, x, y, group_index, param): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = np.concatenate([b.value for b in beta_var], axis=0) beta_sol[np.abs(beta_sol) < self.tol] = 0 beta_sol_list.append(beta_sol) @@ -611,7 +612,7 @@ def asgl(self, x, y, group_index, param): except (ValueError, cvxpy.error.SolverError): continue if problem.status in ["infeasible", "unbounded"]: - logger.warning('Optimization problem status failure') + logging.warning('Optimization problem status failure') beta_sol = np.concatenate([b.value for b in beta_var], axis=0) beta_sol[np.abs(beta_sol) < self.tol] = 0 beta_sol_list.append(beta_sol) @@ -695,7 +696,7 @@ def predict(self, x_new): if self.intercept: x_new = np.c_[np.ones(x_new.shape[0]), x_new] if x_new.shape[1] != len(self.coef_[0]): - logger.error('Model dimension and new data dimension does not match') + logging.error('Model dimension and new data dimension does not match') raise ValueError('Model dimension and new data dimension does not match') # Store predictions in a list prediction_list = [] @@ -717,7 +718,7 @@ def _num_parameters(self): """ # Run the input_checker to verify that the inputs have the correct format if self.__input_checker() is False: - logger.error('incorrect input parameters') + logging.error('incorrect input parameters') raise ValueError('incorrect input parameters') if self.penalization is None: # See meaning of each element in the "else" result statement. @@ -755,7 +756,7 @@ def _retrieve_parameters_idx(self, param_index): if param_index > n_models: string = f'param_index should be smaller or equal than the number of models solved. n_models={n_models}, ' \ f'param_index={param_index}' - logger.error(string) + logging.error(string) raise ValueError(string) # If penalization is None, all parameters are set to None if self.penalization is None: @@ -822,7 +823,7 @@ def error_calculator(y_true, prediction_list, error_type="MSE", tau=None): if error_type not in valid_error_types: raise ValueError(f'invalid error type. Valid error types are {error_dict.keys()}') if y_true.shape[0] != len(prediction_list[0]): - logger.error('Dimension of test data does not match dimension of prediction') + logging.error('Dimension of test data does not match dimension of prediction') raise ValueError('Dimension of test data does not match dimension of prediction') # For each prediction, store the error associated to that prediction in a list error_list = [] diff --git a/asgl/cv.py b/asgl/cv.py index 2b6d413..43875db 100644 --- a/asgl/cv.py +++ b/asgl/cv.py @@ -1,4 +1,5 @@ import logging +import sys import numpy as np from sklearn.model_selection import KFold @@ -6,7 +7,7 @@ from . import asgl from . import weights -logger = logging.getLogger(__name__) +logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') class CvGeneralClass(asgl.ASGL): @@ -73,8 +74,9 @@ def __init__(self, model, penalization, intercept=True, tol=1e-5, lambda1=1, alp """ # CvGeneralClass super().__init__(model, penalization, intercept, tol, lambda1, alpha, tau, lasso_weights, gl_weights, parallel, - num_cores, solver, max_iters, weight_technique, weight_tol, lasso_power_weight, gl_power_weight, - variability_pct, lambda1_weights, spca_alpha, spca_ridge_alpha, error_type, random_state) + num_cores, solver, max_iters, weight_technique, weight_tol, lasso_power_weight, + gl_power_weight, variability_pct, lambda1_weights, spca_alpha, spca_ridge_alpha, error_type, + random_state) # Relative to cross validation / train validate / test self.nfolds = nfolds @@ -122,8 +124,9 @@ def __init__(self, model, penalization, intercept=True, tol=1e-5, lambda1=1, alp random_state=None, train_pct=0.05, validate_pct=0.05, train_size=None, validate_size=None): super().__init__(model, penalization, intercept, tol, lambda1, alpha, tau, lasso_weights, gl_weights, parallel, - num_cores, solver, max_iters, weight_technique, weight_tol, lasso_power_weight, gl_power_weight, - variability_pct, lambda1_weights, spca_alpha, spca_ridge_alpha, error_type, random_state) + num_cores, solver, max_iters, weight_technique, weight_tol, lasso_power_weight, + gl_power_weight, variability_pct, lambda1_weights, spca_alpha, spca_ridge_alpha, error_type, + random_state) # Relative to / train validate / test self.train_pct = train_pct self.validate_pct = validate_pct @@ -195,7 +198,7 @@ def train_test_split(nrows, train_size=None, train_pct=0.7, random_state=None): train_size = int(round(nrows * train_pct)) # Check that nrows is larger than train_size if nrows < train_size: - logger.error(f'Train size is too large. Input number of rows:{nrows}, current train_size: {train_size}') + logging.error(f'Train size is too large. Input number of rows:{nrows}, current train_size: {train_size}') # List of 2 elements of size train_size, remaining_size (test) split_index = np.split(data_index, [train_size]) train_idx, test_idx = [elt for elt in split_index] diff --git a/asgl/weights.py b/asgl/weights.py index 9c0cc18..93bf313 100644 --- a/asgl/weights.py +++ b/asgl/weights.py @@ -1,4 +1,5 @@ import logging +import sys import numpy as np from sklearn.cross_decomposition import PLSRegression @@ -7,7 +8,7 @@ from .asgl import ASGL -logger = logging.getLogger(__name__) +logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') class WEIGHTS: @@ -200,6 +201,6 @@ def fit(self, x, y=None, group_index=None): else: lasso_weights = None gl_weights = None - logger.error(f'Not a valid penalization for weight calculation. Valid penalizations ' - f'are {self.valid_penalizations}') + logging.error(f'Not a valid penalization for weight calculation. Valid penalizations ' + f'are {self.valid_penalizations}') return lasso_weights, gl_weights diff --git a/setup.py b/setup.py index a94cad1..cdf8070 100644 --- a/setup.py +++ b/setup.py @@ -9,13 +9,13 @@ setup( name='asgl', - version='v1.0.1', + version='v1.0.2', author='Alvaro Mendez Civieta', author_email='almendez@est-econ.uc3m.es', license='GNU General Public License', zip_safe=False, url='https://github.com/alvaromc317/asgl', - dowload_url='https://github.com/alvaromc317/asgl/archive/v1.0.1.tar.gz', + dowload_url='https://github.com/alvaromc317/asgl/archive/v1.0.2.tar.gz', description='A regression solver for linear and quantile regression models and lasso based penalizations', long_description=long_description, long_description_content_type='text/markdown',