Skip to content

Commit

Permalink
Update coeff_weights to coeff_weights_kept for IC and removed weighte…
Browse files Browse the repository at this point in the history
…d-lasso regularisation for EC and ISO
  • Loading branch information
ilariagabusi committed Jun 4, 2024
1 parent bcb83ad commit 2b3409b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
60 changes: 30 additions & 30 deletions commit/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ cdef class Evaluation :
This field can be specified only if regularisers[0] is 'group_lasso' or 'sparse_group_lasso'.
NB: this array must have the same size as the number of groups in the IC compartment and contain only non-negative values.
'coeff_weights' - np.array(np.float64) :
weights associated to each individual element of the compartment (implemented for all compartments).
weights associated to each individual element of the compartment (for the moment implemented only for IC compartment).
This field can be specified only if the chosen regulariser is 'lasso' or 'sparse_group_lasso'.
NB: this array must have the same size as the number of elements in the compartment and contain only non-negative values.
Expand Down Expand Up @@ -974,7 +974,7 @@ cdef class Evaluation :

# check if coeff_weights is consistent with the regularisation
if regularisation['regIC'] not in ['lasso', 'sparse_group_lasso'] and dictIC_params is not None and 'coeff_weights' in dictIC_params:
logger.warning('Coefficients weights are allowed only for lasso and sparse_group_lasso regularisation')
logger.warning('Coefficients weights are allowed only for lasso and sparse_group_lasso regularisations. Ignoring "coeff_weights"')

# check if coeff_weights is consistent with the compartment size
if regularisation['regIC'] == 'lasso' or regularisation['regIC'] == 'sparse_group_lasso':
Expand All @@ -983,7 +983,7 @@ cdef class Evaluation :
logger.error('All coefficients weights must be non-negative')
if dictIC_params['coeff_weights'].size != len(self.DICTIONARY['TRK']['kept']):
logger.error(f'"coeff_weights" must have the same size as the number of elements in the IC compartment (got {dictIC_params["coeff_weights"].size} but {len(self.DICTIONARY["TRK"]["kept"])} expected)')
dictIC_params['coeff_weights'] = dictIC_params['coeff_weights'][self.DICTIONARY['TRK']['kept']==1]
dictIC_params['coeff_weights_kept'] = dictIC_params['coeff_weights'][self.DICTIONARY['TRK']['kept']==1]

# check if group parameters are consistent with the regularisation
if regularisation['regIC'] not in ['group_lasso', 'sparse_group_lasso'] and dictIC_params is not None:
Expand Down Expand Up @@ -1086,24 +1086,24 @@ cdef class Evaluation :

# update lambdas using lambda_max
if regularisation['regIC'] == 'lasso':
if dictIC_params is not None and 'coeff_weights' in dictIC_params:
regularisation['lambdaIC_max'] = compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['coeff_weights'])
if dictIC_params is not None and 'coeff_weights_kept' in dictIC_params:
regularisation['lambdaIC_max'] = compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['coeff_weights_kept'])
else:
regularisation['lambdaIC_max'] = compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], np.ones(regularisation['sizeIC'], dtype=np.float64))
regularisation['lambdaIC'] = regularisation['lambdaIC_perc'] * regularisation['lambdaIC_max']
if regularisation['regIC'] == 'group_lasso':
regularisation['lambdaIC_max'] = compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept'])
regularisation['lambdaIC'] = regularisation['lambdaIC_perc'] * regularisation['lambdaIC_max']
if regularisation['regIC'] == 'sparse_group_lasso':
if 'coeff_weights' in dictIC_params:
regularisation['lambdaIC_max'] = ( compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['coeff_weights']), compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept']) )
if 'coeff_weights_kept' in dictIC_params:
regularisation['lambdaIC_max'] = ( compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['coeff_weights_kept']), compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept']) )
else:
regularisation['lambdaIC_max'] = ( compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], np.ones(regularisation['sizeIC'], dtype=np.float64)), compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept']) )
regularisation['lambdaIC'] = ( regularisation['lambdaIC_perc'][0] * regularisation['lambdaIC_max'][0], regularisation['lambdaIC_perc'][1] * regularisation['lambdaIC_max'][1] )

# print
if regularisation['regIC'] is not None:
if (regularisation['regIC'] == 'lasso' or regularisation['regIC'] == 'sparse_group_lasso') and dictIC_params is not None and 'coeff_weights' in dictIC_params:
if (regularisation['regIC'] == 'lasso' or regularisation['regIC'] == 'sparse_group_lasso') and dictIC_params is not None and 'coeff_weights_kept' in dictIC_params:
logger.subinfo( f'Regularisation type: {regularisation["regIC"]} (weighted version)', indent_lvl=2, indent_char='-' )
else:
logger.subinfo( f'Regularisation type: {regularisation["regIC"]}', indent_lvl=2, indent_char='-' )
Expand Down Expand Up @@ -1145,9 +1145,9 @@ cdef class Evaluation :
regularisation['lambdaEC_perc'] = lambdas[1]
else:
regularisation['lambdaEC_perc'] = lambdas[1]
if dictEC_params is not None and 'coeff_weights' in dictEC_params:
if dictEC_params['coeff_weights'].size != regularisation['sizeEC']:
logger.error(f'"coeff_weights" must have the same size as the number of elements in the EC compartment (got {dictEC_params["coeff_weights"].size} but {regularisation["sizeEC"]} expected)')
# if dictEC_params is not None and 'coeff_weights' in dictEC_params:
# if dictEC_params['coeff_weights'].size != regularisation['sizeEC']:
# logger.error(f'"coeff_weights" must have the same size as the number of elements in the EC compartment (got {dictEC_params["coeff_weights"].size} but {regularisation["sizeEC"]} expected)')
elif regularisation['regEC'] == 'smoothness':
logger.error('Not yet implemented')
elif regularisation['regEC'] == 'group_lasso':
Expand All @@ -1161,18 +1161,18 @@ cdef class Evaluation :

# update lambdas using lambda_max
if regularisation['regEC'] == 'lasso':
if dictEC_params is not None and 'coeff_weights' in dictEC_params:
regularisation['lambdaEC_max'] = compute_lambda_max_lasso(regularisation['startEC'], regularisation['sizeEC'], dictEC_params['coeff_weights'])
else:
regularisation['lambdaEC_max'] = compute_lambda_max_lasso(regularisation['startEC'], regularisation['sizeEC'], np.ones(regularisation['sizeEC'], dtype=np.float64))
# if dictEC_params is not None and 'coeff_weights' in dictEC_params:
# regularisation['lambdaEC_max'] = compute_lambda_max_lasso(regularisation['startEC'], regularisation['sizeEC'], dictEC_params['coeff_weights'])
# else:
regularisation['lambdaEC_max'] = compute_lambda_max_lasso(regularisation['startEC'], regularisation['sizeEC'], np.ones(regularisation['sizeEC'], dtype=np.float64))
regularisation['lambdaEC'] = regularisation['lambdaEC_perc'] * regularisation['lambdaEC_max']

# print
if regularisation['regEC'] is not None:
if regularisation['regEC'] == 'lasso' and dictEC_params is not None and 'coeff_weights' in dictEC_params:
logger.subinfo( f'Regularisation type: {regularisation["regEC"]} (weighted version)', indent_lvl=2, indent_char='-' )
else:
logger.subinfo( f'Regularisation type: {regularisation["regEC"]}', indent_lvl=2, indent_char='-' )
# if regularisation['regEC'] == 'lasso' and dictEC_params is not None and 'coeff_weights' in dictEC_params:
# logger.subinfo( f'Regularisation type: {regularisation["regEC"]} (weighted version)', indent_lvl=2, indent_char='-' )
# else:
logger.subinfo( f'Regularisation type: {regularisation["regEC"]}', indent_lvl=2, indent_char='-' )

logger.subinfo( f'Non-negativity constraint: {regularisation["nnEC"]}', indent_char='-', indent_lvl=2 )

Expand Down Expand Up @@ -1200,9 +1200,9 @@ cdef class Evaluation :
regularisation['lambdaISO_perc'] = lambdas[2]
else:
regularisation['lambdaISO_perc'] = lambdas[2]
if dictISO_params is not None and 'coeff_weights' in dictISO_params:
if dictISO_params['coeff_weights'].size != regularisation['sizeISO']:
logger.error(f'"coeff_weights" must have the same size as the number of elements in the ISO compartment (got {dictISO_params["coeff_weights"].size} but {regularisation["sizeISO"]} expected)')
# if dictISO_params is not None and 'coeff_weights' in dictISO_params:
# if dictISO_params['coeff_weights'].size != regularisation['sizeISO']:
# logger.error(f'"coeff_weights" must have the same size as the number of elements in the ISO compartment (got {dictISO_params["coeff_weights"].size} but {regularisation["sizeISO"]} expected)')
elif regularisation['regISO'] == 'smoothness':
logger.error('Not yet implemented')
elif regularisation['regISO'] == 'group_lasso':
Expand All @@ -1216,18 +1216,18 @@ cdef class Evaluation :

# update lambdas using lambda_max
if regularisation['regISO'] == 'lasso':
if dictISO_params is not None and 'coeff_weights' in dictISO_params:
regularisation['lambdaISO_max'] = compute_lambda_max_lasso(regularisation['startISO'], regularisation['sizeISO'], dictISO_params['coeff_weights'])
else:
regularisation['lambdaISO_max'] = compute_lambda_max_lasso(regularisation['startISO'], regularisation['sizeISO'], np.ones(regularisation['sizeISO'], dtype=np.float64))
# if dictISO_params is not None and 'coeff_weights' in dictISO_params:
# regularisation['lambdaISO_max'] = compute_lambda_max_lasso(regularisation['startISO'], regularisation['sizeISO'], dictISO_params['coeff_weights'])
# else:
regularisation['lambdaISO_max'] = compute_lambda_max_lasso(regularisation['startISO'], regularisation['sizeISO'], np.ones(regularisation['sizeISO'], dtype=np.float64))
regularisation['lambdaISO'] = regularisation['lambdaISO_perc'] * regularisation['lambdaISO_max']

# print
if regularisation['regISO'] is not None:
if regularisation['regISO'] == 'lasso' and dictISO_params is not None and 'coeff_weights' in dictISO_params:
logger.subinfo( f'Regularisation type: {regularisation["regISO"]} (weighted version)', indent_lvl=2, indent_char='-' )
else:
logger.subinfo( f'Regularisation type: {regularisation["regISO"]}', indent_lvl=2, indent_char='-' )
# if regularisation['regISO'] == 'lasso' and dictISO_params is not None and 'coeff_weights' in dictISO_params:
# logger.subinfo( f'Regularisation type: {regularisation["regISO"]} (weighted version)', indent_lvl=2, indent_char='-' )
# else:
logger.subinfo( f'Regularisation type: {regularisation["regISO"]}', indent_lvl=2, indent_char='-' )

logger.subinfo( f'Non-negativity constraint: {regularisation["nnISO"]}', indent_char='-', indent_lvl=2 )
if regularisation['regISO'] is not None:
Expand Down
60 changes: 30 additions & 30 deletions commit/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def init_regularisation(regularisation_params):

# create array with al coeff_weights for weighted version of 'lasso'
all_coeff_weights = np.ones(sizeIC+sizeEC+sizeISO, dtype=np.float64)
if regularisation_params.get('dictIC_params') is not None and "coeff_weights" in regularisation_params['dictIC_params'].keys():
all_coeff_weights[startIC:(startIC+sizeIC)] = regularisation_params['dictIC_params']["coeff_weights"]
if regularisation_params.get('dictEC_params') is not None and "coeff_weights" in regularisation_params['dictEC_params'].keys():
all_coeff_weights[startEC:(startEC+sizeEC)] = regularisation_params['dictEC_params']["coeff_weights"]
if regularisation_params.get('dictISO_params') is not None and "coeff_weights" in regularisation_params['dictISO_params'].keys():
all_coeff_weights[startISO:(startISO+sizeISO)] = regularisation_params['dictISO_params']["coeff_weights"]
if regularisation_params.get('dictIC_params') is not None and "coeff_weights_kept" in regularisation_params['dictIC_params'].keys():
all_coeff_weights[startIC:(startIC+sizeIC)] = regularisation_params['dictIC_params']["coeff_weights_kept"]
# if regularisation_params.get('dictEC_params') is not None and "coeff_weights" in regularisation_params['dictEC_params'].keys():
# all_coeff_weights[startEC:(startEC+sizeEC)] = regularisation_params['dictEC_params']["coeff_weights"]
# if regularisation_params.get('dictISO_params') is not None and "coeff_weights" in regularisation_params['dictISO_params'].keys():
# all_coeff_weights[startISO:(startISO+sizeISO)] = regularisation_params['dictISO_params']["coeff_weights"]

############################
# INTRACELLULAR COMPARTMENT#
Expand All @@ -57,7 +57,7 @@ def init_regularisation(regularisation_params):
elif regularisation_params['regIC'] == 'lasso':
lambdaIC = regularisation_params['lambdaIC']
# check if weights are provided
if dictIC_params is not None and "coeff_weights" in dictIC_params.keys():
if dictIC_params is not None and "coeff_weights_kept" in dictIC_params.keys():
# w = dictIC_params["coeff_weights"]
omegaIC = lambda x: lambdaIC * np.linalg.norm(all_coeff_weights[startIC:sizeIC]*x[startIC:sizeIC],1)
if regularisation_params.get('nnIC'):
Expand Down Expand Up @@ -115,7 +115,7 @@ def init_regularisation(regularisation_params):
groupIdxIC[pos:(pos+g.size)] = g[:]
pos += g.size

if "coeff_weights" in dictIC_params.keys():
if "coeff_weights_kept" in dictIC_params.keys():
# w = dictIC_params["coeff_weights"]
omegaIC = lambda x: omega_w_sparse_group_lasso( x, all_coeff_weights, groupIdxIC, groupSizeIC, dictIC_params['group_weights'], lambdaIC, lambda_group_IC)

Expand Down Expand Up @@ -148,18 +148,18 @@ def init_regularisation(regularisation_params):
elif regularisation_params['regEC'] == 'lasso':
lambdaEC = regularisation_params.get('lambdaEC')
# check if weights are provided
if dictEC_params is not None and "coeff_weights" in dictEC_params.keys():
omegaEC = lambda x: lambdaEC * np.linalg.norm(all_coeff_weights[startEC:sizeEC]*x[startEC:sizeEC],1)
if regularisation_params.get('nnEC'):
proxEC = lambda x, scaling: non_negativity(w_soft_thresholding(x,all_coeff_weights,scaling*lambdaEC,startEC,sizeEC),startEC,sizeEC)
else:
proxEC = lambda x, scaling: w_soft_thresholding(x,all_coeff_weights,scaling*lambdaEC,startEC,sizeEC)
# if dictEC_params is not None and "coeff_weights" in dictEC_params.keys():
# omegaEC = lambda x: lambdaEC * np.linalg.norm(all_coeff_weights[startEC:sizeEC]*x[startEC:sizeEC],1)
# if regularisation_params.get('nnEC'):
# proxEC = lambda x, scaling: non_negativity(w_soft_thresholding(x,all_coeff_weights,scaling*lambdaEC,startEC,sizeEC),startEC,sizeEC)
# else:
# proxEC = lambda x, scaling: w_soft_thresholding(x,all_coeff_weights,scaling*lambdaEC,startEC,sizeEC)
# else:
omegaEC = lambda x: lambdaEC * np.linalg.norm(x[startEC:(startEC+sizeEC)],1)
if regularisation_params.get('nnEC'):
proxEC = lambda x, scaling: non_negativity(soft_thresholding(x,scaling*lambdaEC,startEC,sizeEC),startEC,sizeEC)
else:
omegaEC = lambda x: lambdaEC * np.linalg.norm(x[startEC:(startEC+sizeEC)],1)
if regularisation_params.get('nnEC'):
proxEC = lambda x, scaling: non_negativity(soft_thresholding(x,scaling*lambdaEC,startEC,sizeEC),startEC,sizeEC)
else:
proxEC = lambda x, scaling: soft_thresholding(x,scaling*lambdaEC,startEC,sizeEC)
proxEC = lambda x, scaling: soft_thresholding(x,scaling*lambdaEC,startEC,sizeEC)

# elif regularisation_params['regIC'] == 'smoothness':
# lambdaEC = regularisation_params.get('lambdaEC')
Expand All @@ -183,18 +183,18 @@ def init_regularisation(regularisation_params):
elif regularisation_params['regISO'] == 'lasso':
lambdaISO = regularisation_params.get('lambdaISO')
# check if weights are provided
if dictISO_params is not None and "coeff_weights" in dictISO_params.keys():
omegaISO = lambda x: lambdaISO * np.linalg.norm(all_coeff_weights[startISO:sizeISO]*x[startISO:sizeISO],1)
if regularisation_params.get('nnISO'):
proxISO = lambda x, scaling: non_negativity(w_soft_thresholding(x,all_coeff_weights,scaling*lambdaISO,startISO,sizeISO),startISO,sizeISO)
else:
proxISO = lambda x, scaling: w_soft_thresholding(x,all_coeff_weights,scaling*lambdaISO,startISO,sizeISO)
# if dictISO_params is not None and "coeff_weights" in dictISO_params.keys():
# omegaISO = lambda x: lambdaISO * np.linalg.norm(all_coeff_weights[startISO:sizeISO]*x[startISO:sizeISO],1)
# if regularisation_params.get('nnISO'):
# proxISO = lambda x, scaling: non_negativity(w_soft_thresholding(x,all_coeff_weights,scaling*lambdaISO,startISO,sizeISO),startISO,sizeISO)
# else:
# proxISO = lambda x, scaling: w_soft_thresholding(x,all_coeff_weights,scaling*lambdaISO,startISO,sizeISO)
# else:
omegaISO = lambda x: lambdaISO * np.linalg.norm(x[startISO:(startISO+sizeISO)],1)
if regularisation_params.get('nnISO'):
proxISO = lambda x, scaling: non_negativity(soft_thresholding(x,scaling*lambdaISO,startISO,sizeISO),startISO,sizeISO)
else:
omegaISO = lambda x: lambdaISO * np.linalg.norm(x[startISO:(startISO+sizeISO)],1)
if regularisation_params.get('nnISO'):
proxISO = lambda x, scaling: non_negativity(soft_thresholding(x,scaling*lambdaISO,startISO,sizeISO),startISO,sizeISO)
else:
proxISO = lambda x, scaling: soft_thresholding(x,scaling*lambdaISO,startISO,sizeISO)
proxISO = lambda x, scaling: soft_thresholding(x,scaling*lambdaISO,startISO,sizeISO)

# elif regularisation_params['regISO'] == 'group_lasso':
# lambdaISO = regularisation_params.get('lambdaISO')
Expand Down

0 comments on commit 2b3409b

Please sign in to comment.