def solve(y, A, At, tol_fun=1e-4, tol_x=1e-6, max_iter=1000, verbose=True, x0=None, regularisation=None, confidence_array=None): """ Solve the regularised least squares problem argmin_x 0.5*|| sqrt(W) ( Ax-y ) ||_2^2 + Omega(x) with the Omega described by 'regularisation' and W is the confidence_array Check the documentation of commit.solvers.init_regularisation to see how to solve a specific problem. """ if regularisation is None: omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, x.size) else: omega, prox = regularisation2omegaprox(regularisation) if x0 is None: x0 = np.zeros(A.shape[1]) if confidence_array is not None: confidence_array = np.sqrt(confidence_array) return fista(y, A, At, tol_fun, tol_x, max_iter, verbose, x0, omega, prox, confidence_array)
def solve(y, A, At, tol_fun=1e-4, tol_x=1e-6, max_iter=1000, verbose=1, x0=None, regularisation=None): """ Solve the regularised least squares problem argmin_x 0.5*||Ax-y||_2^2 + Omega(x) with the Omega described by 'regularisation'. Check the documentation of commit.solvers.init_regularisation to see how to solve a specific problem. """ if regularisation is None: omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, x.size) else: omega, prox = regularisation2omegaprox(regularisation) if x0 is None: x0 = np.ones(A.shape[1]) return fista(y, A, At, tol_fun, tol_x, max_iter, verbose, x0, omega, prox)
def evaluate_model(y, A, x, regularisation=None): if regularisation is None: omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, len(x)) else: omega, _ = regularisation2omegaprox(regularisation) return 0.5 * np.linalg.norm(A.dot(x) - y)**2 + omega(x)
def evaluate_model(y, A, x, regularisation = None): if regularisation is None: omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, len(x)) else: omega, _ = regularisation2omegaprox(regularisation) return 0.5*np.linalg.norm(A.dot(x)-y)**2 + omega(x)
def solve(y, A, At, tol_fun = 1e-4, tol_x = 1e-6, max_iter = 1000, verbose = 1, x0 = None, regularisation = None): """ Solve the regularised least squares problem argmin_x 0.5*||Ax-y||_2^2 + Omega(x) with the Omega described by 'regularisation'. Check the documentation of commit.solvers.init_regularisation to see how to solve a specific problem. """ if regularisation is None: omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, x.size) else: omega, prox = regularisation2omegaprox(regularisation) if x0 is None: x0 = np.zeros(A.shape[1]) return fista( y, A, At, tol_fun, tol_x, max_iter, verbose, x0, omega, prox)
def regularisation2omegaprox(regularisation): lambdaIC = float(regularisation.get('lambdaIC')) lambdaEC = float(regularisation.get('lambdaEC')) lambdaISO = float(regularisation.get('lambdaISO')) if lambdaIC < 0.0 or lambdaEC < 0.0 or lambdaISO < 0.0: raise ValueError('Negative regularisation parameters are not allowed') normIC = regularisation.get('normIC') normEC = regularisation.get('normEC') normISO = regularisation.get('normISO') if not normIC in list_regnorms: raise ValueError( 'normIC must be one of commit.solvers.{group_sparsity,non_negative,norm1,norm2}' ) if not normEC in list_regnorms: raise ValueError( 'normEC must be one of commit.solvers.{group_sparsity,non_negative,norm1,norm2}' ) if not normISO in list_regnorms: raise ValueError( 'normISO must be one of commit.solvers.{group_sparsity,non_negative,norm1,norm2}' ) ## NNLS case if (lambdaIC == 0.0 and lambdaEC == 0.0 and lambdaISO == 0.0) or (normIC == non_negative and normEC == non_negative and normISO == non_negative): omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, len(x)) return omega, prox ## All other cases # Intracellular Compartment startIC = regularisation.get('startIC') sizeIC = regularisation.get('sizeIC') if lambdaIC == 0.0: omegaIC = lambda x: 0.0 proxIC = lambda x: x elif normIC == norm2: omegaIC = lambda x: lambdaIC * np.linalg.norm(x[startIC:sizeIC]) proxIC = lambda x: projection_onto_l2_ball(x, lambdaIC, startIC, sizeIC ) elif normIC == norm1: omegaIC = lambda x: lambdaIC * sum(x[startIC:sizeIC]) proxIC = lambda x: soft_thresholding(x, lambdaIC, startIC, sizeIC) elif normIC == non_negative: omegaIC = lambda x: 0.0 proxIC = lambda x: non_negativity(x, startIC, sizeIC) elif normIC == group_sparsity: weightsIC = regularisation.get('weightsIC') structureIC = regularisation.get('structureIC') if regularisation.get( 'group_is_ordered' ): # This option will be deprecated in future release warnings.warn( 'The ordered group structure will be deprecated. Check the documentation of commit.solvers.init_regularisation.', DeprecationWarning) bundles = np.insert(structureIC, 0, 0) structureIC = np.array([ np.arange(sum(bundles[:k + 1]), sum(bundles[:k + 1]) + bundles[k + 1]) for k in range(len(bundles) - 1) ]) # check how it works with bundles=[2,5,4] regularisation['structureIC'] = structureIC regularisation[ 'group_is_ordered'] = False # the group structure is overwritten, hence the flag has to be changed del bundles if not len(structureIC) == len(weightsIC): raise ValueError('Number of groups and weights do not coincide.') group_norm = regularisation.get('group_norm') if not group_norm in list_group_sparsity_norms: raise ValueError( 'Wrong norm in the structured sparsity term. Choose between %s.' % str(list_group_sparsity_norms)) omegaIC = lambda x: omega_group_sparsity(x, structureIC, weightsIC, lambdaIC, group_norm) proxIC = lambda x: prox_group_sparsity(x, structureIC, weightsIC, lambdaIC, group_norm) else: raise ValueError( 'Type of regularisation for IC compartment not recognized.') # Extracellular Compartment startEC = regularisation.get('startEC') sizeEC = regularisation.get('sizeEC') if lambdaEC == 0.0: omegaEC = lambda x: 0.0 proxEC = lambda x: x elif normEC == norm2: omegaEC = lambda x: lambdaEC * np.linalg.norm(x[startEC:sizeEC]) proxEC = lambda x: projection_onto_l2_ball(x, lambdaEC, startEC, sizeEC ) elif normEC == norm1: omegaEC = lambda x: lambdaEC * sum(x[startEC:sizeEC]) proxEC = lambda x: soft_thresholding(x, lambdaEC, startEC, sizeEC) elif normEC == non_negative: omegaEC = lambda x: 0.0 proxEC = lambda x: non_negativity(x, startEC, sizeEC) else: raise ValueError( 'Type of regularisation for EC compartment not recognized.') # Isotropic Compartment startISO = regularisation.get('startISO') sizeISO = regularisation.get('sizeISO') if lambdaISO == 0.0: omegaISO = lambda x: 0.0 proxISO = lambda x: x elif normISO == norm2: omegaISO = lambda x: lambdaISO * np.linalg.norm(x[startISO:sizeISO]) proxISO = lambda x: projection_onto_l2_ball(x, lambdaISO, startISO, sizeISO) elif normISO == norm1: omegaISO = lambda x: lambdaISO * sum(x[startISO:sizeISO]) proxISO = lambda x: soft_thresholding(x, lambdaISO, startISO, sizeISO) elif normISO == non_negative: omegaISO = lambda x: 0.0 proxISO = lambda x: non_negativity(x, startISO, sizeISO) else: raise ValueError( 'Type of regularisation for ISO compartment not recognized.') omega = lambda x: omegaIC(x) + omegaEC(x) + omegaISO(x) prox = lambda x: non_negativity(proxIC(proxEC(proxISO(x))), 0, x.size ) # non negativity is redunduntly forced return omega, prox
def regularisation2omegaprox(regularisation): lambdaIC = float(regularisation.get('lambdaIC')) lambdaEC = float(regularisation.get('lambdaEC')) lambdaISO = float(regularisation.get('lambdaISO')) if lambdaIC < 0.0 or lambdaEC < 0.0 or lambdaISO < 0.0: raise ValueError('Negative regularisation parameters are not allowed') normIC = regularisation.get('normIC') normEC = regularisation.get('normEC') normISO = regularisation.get('normISO') if not normIC in list_regnorms: raise ValueError('normIC must be one of commit.solvers.{group_sparsity,non_negative,norm1,norm2}') if not normEC in list_regnorms: raise ValueError('normEC must be one of commit.solvers.{group_sparsity,non_negative,norm1,norm2}') if not normISO in list_regnorms: raise ValueError('normISO must be one of commit.solvers.{group_sparsity,non_negative,norm1,norm2}') ## NNLS case if (lambdaIC == 0.0 and lambdaEC == 0.0 and lambdaISO == 0.0) or (normIC == non_negative and normEC == non_negative and normISO == non_negative): omega = lambda x: 0.0 prox = lambda x: non_negativity(x, 0, len(x)) return omega, prox ## All other cases # Intracellular Compartment startIC = regularisation.get('startIC') sizeIC = regularisation.get('sizeIC') if lambdaIC == 0.0: omegaIC = lambda x: 0.0 proxIC = lambda x: x elif normIC == norm2: omegaIC = lambda x: lambdaIC * np.linalg.norm(x[startIC:sizeIC]) proxIC = lambda x: projection_onto_l2_ball(x, lambdaIC, startIC, sizeIC) elif normIC == norm1: omegaIC = lambda x: lambdaIC * sum( x[startIC:sizeIC] ) proxIC = lambda x: soft_thresholding(x, lambdaIC, startIC, sizeIC) elif normIC == non_negative: omegaIC = lambda x: 0.0 proxIC = lambda x: non_negativity(x, startIC, sizeIC) elif normIC == group_sparsity: structureIC = regularisation.get('structureIC') groupWeightIC = regularisation.get('weightsIC') if not len(structureIC) == len(groupWeightIC): raise ValueError('Number of groups and weights do not coincide.') group_norm = regularisation.get('group_norm') if not group_norm in list_group_sparsity_norms: raise ValueError('Wrong norm in the structured sparsity term. Choose between %s.' % str(list_group_sparsity_norms)) # convert to new data structure (needed for faster access) N = np.sum([g.size for g in structureIC]) groupIdxIC = np.zeros( (N,), dtype=np.int32 ) groupSizeIC = np.zeros( (structureIC.size,), dtype=np.int32 ) pos = 0 for i, g in enumerate(structureIC) : groupSizeIC[i] = g.size groupIdxIC[pos:(pos+g.size)] = g[:] pos += g.size omegaIC = lambda x: omega_group_sparsity( x, groupIdxIC, groupSizeIC, groupWeightIC, lambdaIC, group_norm ) proxIC = lambda x: prox_group_sparsity( x, groupIdxIC, groupSizeIC, groupWeightIC, lambdaIC, group_norm ) else: raise ValueError('Type of regularisation for IC compartment not recognized.') # Extracellular Compartment startEC = regularisation.get('startEC') sizeEC = regularisation.get('sizeEC') if lambdaEC == 0.0: omegaEC = lambda x: 0.0 proxEC = lambda x: x elif normEC == norm2: omegaEC = lambda x: lambdaEC * np.linalg.norm(x[startEC:sizeEC]) proxEC = lambda x: projection_onto_l2_ball(x, lambdaEC, startEC, sizeEC) elif normEC == norm1: omegaEC = lambda x: lambdaEC * sum( x[startEC:sizeEC] ) proxEC = lambda x: soft_thresholding(x, lambdaEC, startEC, sizeEC) elif normEC == non_negative: omegaEC = lambda x: 0.0 proxEC = lambda x: non_negativity(x, startEC, sizeEC) else: raise ValueError('Type of regularisation for EC compartment not recognized.') # Isotropic Compartment startISO = regularisation.get('startISO') sizeISO = regularisation.get('sizeISO') if lambdaISO == 0.0: omegaISO = lambda x: 0.0 proxISO = lambda x: x elif normISO == norm2: omegaISO = lambda x: lambdaISO * np.linalg.norm(x[startISO:sizeISO]) proxISO = lambda x: projection_onto_l2_ball(x, lambdaISO, startISO, sizeISO) elif normISO == norm1: omegaISO = lambda x: lambdaISO * sum( x[startISO:sizeISO] ) proxISO = lambda x: soft_thresholding(x, lambdaISO, startISO, sizeISO) elif normISO == non_negative: omegaISO = lambda x: 0.0 proxISO = lambda x: non_negativity(x, startISO, sizeISO) else: raise ValueError('Type of regularisation for ISO compartment not recognized.') omega = lambda x: omegaIC(x) + omegaEC(x) + omegaISO(x) prox = lambda x: non_negativity(proxIC(proxEC(proxISO(x))),0,x.size) # non negativity is redunduntly forced return omega, prox