Esempio n. 1
0
    def _MakeDrivingForceConstraints(self, ln_conc, driving_force_lb=0):
        """
            driving_force_lb can either be a cvxpy variable use later in the optimization
            or a scalar, which sets it as a constraint. By default the lower bound is 0.
        """
        constraints = []
        S = cvxpy.matrix(self.S)
        dg0r_primes = cvxpy.matrix(self.dG0_r_prime)
        for i in xrange(self.Nr):
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(self.dG0_r_prime[0, i]):
                continue

            curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i]
            if self.fluxes[0, i] == 0:
                constraints += cvxpy.eq(curr_dgr, 0)
            else:
                if self.normalization == DeltaGNormalization.DIVIDE_BY_FLUX:
                    motive_force = -curr_dgr * (1.0 / self.fluxes[0, i])
                elif self.normalization == DeltaGNormalization.TIMES_FLUX:
                    motive_force = -curr_dgr * self.fluxes[0, i]
                elif self.normalization == DeltaGNormalization.SIGN_FLUX:
                    motive_force = -curr_dgr * np.sign(self.fluxes[0, i])
                else:
                    raise ValueError("bad value for normalization method: " +
                                     str(self.normalization))

                constraints += [cvxpy.geq(motive_force, driving_force_lb)]

        return constraints
Esempio n. 2
0
    def _MakeDrivingForceConstraints(self, ln_conc, driving_force_lb=0):
        """
            driving_force_lb can either be a cvxpy variable use later in the optimization
            or a scalar, which sets it as a constraint. By default the lower bound is 0.
        """
        constraints = []
        S = cvxpy.matrix(self.S)
        dg0r_primes = cvxpy.matrix(self.dG0_r_prime)
        for i in xrange(self.Nr):
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(self.dG0_r_prime[0, i]):
                continue
            
            curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i]
            if self.fluxes[0, i] == 0:
                constraints += cvxpy.eq(curr_dgr, 0)
            else:
                if self.normalization == DeltaGNormalization.DIVIDE_BY_FLUX:
                    motive_force = -curr_dgr * (1.0 / self.fluxes[0, i])
                elif self.normalization == DeltaGNormalization.TIMES_FLUX:
                    motive_force = -curr_dgr * self.fluxes[0, i]
                elif self.normalization == DeltaGNormalization.SIGN_FLUX:
                    motive_force = -curr_dgr * np.sign(self.fluxes[0, i])
                else:
                    raise ValueError("bad value for normalization method: "
                                     + str(self.normalization))

                constraints += [cvxpy.geq(motive_force, driving_force_lb)]

        return constraints
Esempio n. 3
0
    def _MakeMinimalFeasbileConcentrationProblem(self, bounds=None, c_range=(1e-6, 1e-2)):
        # Define and apply the constraints on the concentrations
        constraints = []
        
        # Define and apply the constraints on the concentrations
        ln_conc = cvxpy.variable(1, self.Nc, name='lnC')
        constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds,
                                                      c_range=c_range)

        # find the row vector describing the overall stoichiometry
        S = cvxpy.matrix(self.S)
        dg0r_primes = cvxpy.matrix(self.dG0_r_prime)
        
        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium. 
        for i in xrange(self.Nr):
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(self.dG0_r_prime[0, i]):
                continue
            
            curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i]

            if self.fluxes[0, i] != 0:
                constraints.append(cvxpy.leq(curr_dgr * np.sign(self.fluxes[0, i]),
                                             self.DEFAULT_REACTION_UB))
                constraints.append(cvxpy.geq(curr_dgr * np.sign(self.fluxes[0, i]),
                                             self.DEFAULT_REACTION_LB))
            else:
                constraints.append(cvxpy.eq(curr_dgr, 0))
        
        # Set the constraints
        return ln_conc, constraints
Esempio n. 4
0
def _cqp(D, o, k, m, N):
    ''' solves using cvxpy '''
    
    # cast to object.
    D = cvxpy.matrix(D)
    N = cvxpy.matrix(N)	
    o = cvxpy.matrix(o)
    
    # create variables.
    f = cvxpy.variable(k, 1, name='f')
    x=cvxpy.variable(m, 1, name='x')
    y=cvxpy.variable(m, 1, name='y')    
	
	# create constraints.
    geqs = cvxpy.greater_equals(f,0.0)
	
	#TO DO: Sum of all f = sum of observed reads classes (and not equal to 1)
    sum1 = cvxpy.equals(cvxpy.sum(f), 1.0)
    #sum1 = cvxpy.equals(cvxpy.sum(f), sum_obs_freq)
    
    #3	
    #dev = cvxpy.equals(D*f-o-x,0.0)	
    
	#4. matrix N (m x m) * x - y = 0
    sizeConstr = cvxpy.equals(N*x-y,0.0)
    #Check now to do N^2
	#sizeConstr = cvxpy.equals(N^2*x-y,0.0)
    #This might not work but try
    #sizeConstr = cvxpy.equals(x/N-y,0.0)
	
    #constrs = [geqs, sum1, dev, sizeConstr]
    constrs = [geqs, sum1]
		
    log.debug('\tin _cqp function: \n\t\tPrint matrices shapes:')
    log.debug('\t\t\t%s', D.shape)
    log.debug('\t\t\t%s', f.shape)
    log.debug('\t\t\t%s', o.shape)
    
    # create the program.
    #p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(y)),constraints=constrs)
    p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(D*f-o)),constraints=constrs)
    p.options['abstol'] = 1e-6 ## 'abstol' - Absolute accuracy	Default: 1e-7
    p.options['reltol'] = 1e-5 ## 'reltol' - Relative accuracy	Default: 1e-6
    p.options['feastol'] = 1e-5 ## 'feastol' - Tolerance for feasibility conditions	Default: 1e-6
    p.options['maxiters'] = 500 ## 'maxiters' - Maximum number of iterations	Default: 100
    
    
    # solve the program.
    p.solve(quiet=True)
	
    # return results.
    #print np.around(f.value, decimals=20)
    
    #print "Print using loop"
    getcontext().prec = 20
    #for i in f.value:
    #    temp_fi=str(i).strip('[]')	
    #    print temp_fi
    
    return f.value
Esempio n. 5
0
    def _GetTotalReactionEnergy(self, c_range=(1e-6, 1e-2), bounds=None, min_driving_force=0):
        constraints = []
        
        # Define and apply the constraints on the concentrations
        ln_conc = cvxpy.variable(1, self.Nc, name='lnC')
        constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds,
                                                      c_range=c_range)
        
        # find the row vector describing the overall stoichiometry
        S = cvxpy.matrix(self.S)
        f = cvxpy.matrix(self.fluxes)
        g0 = cvxpy.matrix(self.dG0_r_prime)
        g = g0 + RT * ln_conc * S
        total_g = f * g.T

        constraints += self._MakeDrivingForceConstraints(ln_conc, min_driving_force)
        
        return ln_conc, constraints, total_g
Esempio n. 6
0
    def _MakeMinimumFeasbileConcentrationsProblem(self,
                                                  bounds=None,
                                                  c_range=(1e-6, 1e-2)):
        """Creates the CVXOPT problem for finding minimum total concentrations.
        
        Returns:
            Two tuple (ln_concentrations var, problem).
        """
        assert self.dG0_f_prime is not None

        constraints = []

        # Define and apply the constraints on the concentrations
        ln_conc = cvxpy.variable(1, self.Nc, name='lnC')
        constraints += self._MakeLnConcentratonBounds(ln_conc,
                                                      bounds=bounds,
                                                      c_range=c_range)

        # Make the objective and problem.
        S = cvxpy.matrix(self.S)

        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        dgf_primes = RT * ln_conc + cvxpy.matrix(self.dG0_f_prime)
        for i in xrange(self.Nr):
            if self.fluxes[0, i] > 0:
                constraints.append(
                    cvxpy.leq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_UB))
                constraints.append(
                    cvxpy.geq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_LB))
            elif self.fluxes[0, i] == 0:
                constraints.append(cvxpy.eq(S[i, :] * dgf_primes, 0))
            else:
                constraints.append(
                    cvxpy.geq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_UB))
                constraints.append(
                    cvxpy.leq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_LB))

        return ln_conc, constraints
Esempio n. 7
0
 def _MakeMinimumFeasbileConcentrationsProblem(self, bounds=None,
                                               c_range=(1e-6, 1e-2)):
     """Creates the CVXOPT problem for finding minimum total concentrations.
     
     Returns:
         Two tuple (ln_concentrations var, problem).
     """
     assert self.dG0_f_prime is not None
     
     constraints = []
     
     # Define and apply the constraints on the concentrations
     ln_conc = cvxpy.variable(1, self.Nc, name='lnC')
     constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds,
                                                   c_range=c_range)
     
     # Make the objective and problem.
     S = cvxpy.matrix(self.S)
     
     # Make flux-based constraints on reaction free energies.
     # All reactions must have negative dGr in the direction of the flux.
     # Reactions with a flux of 0 must be in equilibrium.
     dgf_primes = RT * ln_conc + cvxpy.matrix(self.dG0_f_prime)
     for i in xrange(self.Nr):
         if self.fluxes[0, i] > 0:
             constraints.append(cvxpy.leq(S[i, :] * dgf_primes,
                                          self.DEFAULT_REACTION_UB))
             constraints.append(cvxpy.geq(S[i, :] * dgf_primes,
                                          self.DEFAULT_REACTION_LB))
         elif self.fluxes[0, i] == 0:
             constraints.append(cvxpy.eq(S[i, :] * dgf_primes,
                                         0))
         else:
             constraints.append(cvxpy.geq(S[i, :] * dgf_primes,
                                          -self.DEFAULT_REACTION_UB))
             constraints.append(cvxpy.leq(S[i, :] * dgf_primes,
                                          -self.DEFAULT_REACTION_LB))
     
     return ln_conc, constraints
Esempio n. 8
0
    def _MakeMinimalFeasbileConcentrationProblem(self,
                                                 bounds=None,
                                                 c_range=(1e-6, 1e-2)):
        # Define and apply the constraints on the concentrations
        constraints = []

        # Define and apply the constraints on the concentrations
        ln_conc = cvxpy.variable(1, self.Nc, name='lnC')
        constraints += self._MakeLnConcentratonBounds(ln_conc,
                                                      bounds=bounds,
                                                      c_range=c_range)

        # find the row vector describing the overall stoichiometry
        S = cvxpy.matrix(self.S)
        dg0r_primes = cvxpy.matrix(self.dG0_r_prime)

        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        for i in xrange(self.Nr):
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(self.dG0_r_prime[0, i]):
                continue

            curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i]

            if self.fluxes[0, i] != 0:
                constraints.append(
                    cvxpy.leq(curr_dgr * np.sign(self.fluxes[0, i]),
                              self.DEFAULT_REACTION_UB))
                constraints.append(
                    cvxpy.geq(curr_dgr * np.sign(self.fluxes[0, i]),
                              self.DEFAULT_REACTION_LB))
            else:
                constraints.append(cvxpy.eq(curr_dgr, 0))

        # Set the constraints
        return ln_conc, constraints
Esempio n. 9
0
 def _MakeLnConcentratonBounds(self, ln_conc, bounds=None, c_range=None):
     """Make bounds on logarithmic concentrations."""
     _c_range = c_range or self.DEFAULT_C_RANGE
     c_lower, c_upper = c_range
     ln_conc_lb = np.ones((1, self.Nc)) * np.log(c_lower)
     ln_conc_ub = np.ones((1, self.Nc)) * np.log(c_upper)
     
     if bounds:
         for i, bound in enumerate(bounds):
             lb, ub = bound
             log_lb = np.log(lb or c_lower)
             log_ub = np.log(ub or c_upper)
             if log_lb > log_ub:
                 raise Exception("Lower bound is greater than upper bound: "
                                 "%d > %d" % (log_lb, log_ub))
             elif abs(log_lb - log_ub) < 1e-2:
                 log_lb = log_ub - 1e-2
                 
             ln_conc_lb[0, i] = log_lb
             ln_conc_ub[0, i] = log_ub
     
     return [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)) + \
             cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
Esempio n. 10
0
    def _GetTotalReactionEnergy(self,
                                c_range=(1e-6, 1e-2),
                                bounds=None,
                                min_driving_force=0):
        constraints = []

        # Define and apply the constraints on the concentrations
        ln_conc = cvxpy.variable(1, self.Nc, name='lnC')
        constraints += self._MakeLnConcentratonBounds(ln_conc,
                                                      bounds=bounds,
                                                      c_range=c_range)

        # find the row vector describing the overall stoichiometry
        S = cvxpy.matrix(self.S)
        f = cvxpy.matrix(self.fluxes)
        g0 = cvxpy.matrix(self.dG0_r_prime)
        g = g0 + RT * ln_conc * S
        total_g = f * g.T

        constraints += self._MakeDrivingForceConstraints(
            ln_conc, min_driving_force)

        return ln_conc, constraints, total_g
Esempio n. 11
0
    def _MakeLnConcentratonBounds(self, ln_conc, bounds=None, c_range=None):
        """Make bounds on logarithmic concentrations."""
        _c_range = c_range or self.DEFAULT_C_RANGE
        c_lower, c_upper = c_range
        ln_conc_lb = np.ones((1, self.Nc)) * np.log(c_lower)
        ln_conc_ub = np.ones((1, self.Nc)) * np.log(c_upper)

        if bounds:
            for i, bound in enumerate(bounds):
                lb, ub = bound
                log_lb = np.log(lb or c_lower)
                log_ub = np.log(ub or c_upper)
                if log_lb > log_ub:
                    raise Exception("Lower bound is greater than upper bound: "
                                    "%d > %d" % (log_lb, log_ub))
                elif abs(log_lb - log_ub) < 1e-2:
                    log_lb = log_ub - 1e-2

                ln_conc_lb[0, i] = log_lb
                ln_conc_ub[0, i] = log_ub

        return [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)) + \
                cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
Esempio n. 12
0
def fit_ellipse_eps_insensitive(x, y):
    """
    fit ellipoid using epsilon-insensitive loss
    """

    x = numpy.array(x)
    y = numpy.array(y)

    print "shapes", x.shape, y.shape

    assert len(x) == len(y)

    N = len(x)
    D = 5

    dat = numpy.zeros((N, D))
    dat[:,0] = x*x
    dat[:,1] = y*y
    #dat[:,2] = y*x
    dat[:,2] = x
    dat[:,3] = y
    dat[:,4] = numpy.ones(N)


    print dat.shape
    dat = cvxpy.matrix(dat)
    #### parameters

    # data
    X = cvxpy.parameter(N, D, name="X")

    # parameter for eps-insensitive loss
    eps = cvxpy.parameter(1, name="eps")


    #### varibales

    # parameter vector
    theta = cvxpy.variable(D, name="theta")

    # dim = (N x 1)
    s = cvxpy.variable(N, name="s")

    t = cvxpy.variable(N, name="t")

    # simple objective 
    objective = cvxpy.sum(t)
    
    # create problem                                    
    p = cvxpy.program(cvxpy.minimize(objective))
    
    # add constraints 
    # (N x D) * (D X 1) = (N X 1)
    p.constraints.append(X*theta <= s)
    p.constraints.append(-X*theta <= s)
    
    p.constraints.append(s - eps <= t)
    p.constraints.append(0 <= t)
    
    #p.constraints.append(theta[4] == 1)
    # trace constraint
    p.constraints.append(theta[0] + theta[1] == 1)
    
    ###### set values
    X.value = dat
    eps.value = 0.0
    #solver = "mosek" 
    #p.solve(lpsolver=solver)
    p.solve()
    
    cvxpy.printval(theta)

    w = numpy.array(cvxpy.value(theta))
    
    #cvxpy.printval(s)
    #cvxpy.printval(t)

    ## For clarity, fill in the quadratic form variables
    A        = numpy.zeros((2,2))
    A[0,0]   = w[0]
    A.ravel()[1:3] = 0#w[2]
    A[1,1]   = w[1]
    bv       = w[2:4]
    c        = w[4]
    
    ## find parameters
    z, a, b, alpha = util.conic2parametric(A, bv, c)

    return z, a, b, alpha
    def MinimizeConcentration(self,
                              metabolite_index=None,
                              concentration_bounds=None):
        """Finds feasible concentrations minimizing the concentration
           of metabolite i.
        
        Args:
            metabolite_index: the index of the metabolite to minimize.
                if == None, minimize the sum of all concentrations.
            concentration_bounds: the Bounds objects setting concentration bounds.
        """
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()

        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [
            cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
            cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))
        ]

        my_dG0_r_primes = np.matrix(self.dG0_r_prime)

        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)

        for i, flux in enumerate(self.fluxes):

            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue

            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0.0))
            else:
                constr.append(cvxpy.leq(curr_dgr, 0.0))

        objective = None
        if metabolite_index is not None:
            my_conc = ln_conc[0, metabolite_index]
            objective = cvxpy.minimize(cvxpy.exp(my_conc))
        else:
            objective = cvxpy.minimize(cvxpy.sum(cvxpy.exp(ln_conc)))

        name = 'CONC_OPT'
        if metabolite_index:
            name = 'CONC_%d_OPT' % metabolite_index
        problem = cvxpy.program(objective, constr, name=name)
        optimum = problem.solve(quiet=True)
        """
        status = problem.solve(quiet=True)
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return ConcentrationOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = ConcentrationOptimizedPathway(
            self._model,
            self._thermo,
            my_bounds,
            optimal_value=optimum,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        return result
Esempio n. 14
0
   
    # Add noise to signal
    rSig = signal + noise
    rSig = abs(rSig)                 #phase is not used in MRI


    # Choose regularization parameter
    # lambda > lambda_max -> zero solution
    lambda_max = 2*norm(dot(nA.T, rSig.T), np.inf) 

    lamb = 1.0e-8*lambda_max
    
    print('Solving L1 penalized system with cvxpy...')

    coefs = cvx.variable(n_qpnts,1)
    A     = cvx.matrix(nA)
    rhs   = cvx.matrix(rSig).T

    objective = cvx.minimize(cvx.norm2(A*coefs - rhs) +
                             lamb*cvx.norm1(coefs) )
    constraints = [cvx.geq(coefs,0.0)]
    prob = cvx.program(objective, constraints)

    # Call the solver
    prob.solve(quiet=True)  #Use quiet=True to suppress output


    # Convert the cvxmod objects to plain numpy arrays for further processing
    nd_coefs_l1 = np.array(coefs.value).squeeze()

    # Cutoff those coefficients that are less than cutoff
Esempio n. 15
0
def _cqp(D, o, k, m, N):
    ''' solves using cvxpy '''

    # cast to object.
    D = cvxpy.matrix(D)
    N = cvxpy.matrix(N)
    o = cvxpy.matrix(o)

    # create variables.
    f = cvxpy.variable(k, 1, name='f')
    x = cvxpy.variable(m, 1, name='x')
    y = cvxpy.variable(m, 1, name='y')

    # create constraints.
    geqs = cvxpy.greater_equals(f, 0.0)

    #TO DO: Sum of all f = sum of observed reads classes (and not equal to 1)
    sum1 = cvxpy.equals(cvxpy.sum(f), 1.0)
    #sum1 = cvxpy.equals(cvxpy.sum(f), sum_obs_freq)

    #3
    #dev = cvxpy.equals(D*f-o-x,0.0)

    #4. matrix N (m x m) * x - y = 0
    sizeConstr = cvxpy.equals(N * x - y, 0.0)
    #Check now to do N^2
    #sizeConstr = cvxpy.equals(N^2*x-y,0.0)
    #This might not work but try
    #sizeConstr = cvxpy.equals(x/N-y,0.0)

    #constrs = [geqs, sum1, dev, sizeConstr]
    constrs = [geqs, sum1]

    log.debug('\tin _cqp function: \n\t\tPrint matrices shapes:')
    log.debug('\t\t\t%s', D.shape)
    log.debug('\t\t\t%s', f.shape)
    log.debug('\t\t\t%s', o.shape)

    # create the program.
    #p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(y)),constraints=constrs)
    p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(D * f - o)),
                      constraints=constrs)
    p.options['abstol'] = 1e-6  ## 'abstol' - Absolute accuracy	Default: 1e-7
    p.options['reltol'] = 1e-5  ## 'reltol' - Relative accuracy	Default: 1e-6
    p.options[
        'feastol'] = 1e-5  ## 'feastol' - Tolerance for feasibility conditions	Default: 1e-6
    p.options[
        'maxiters'] = 500  ## 'maxiters' - Maximum number of iterations	Default: 100

    # solve the program.
    p.solve(quiet=True)

    # return results.
    #print np.around(f.value, decimals=20)

    #print "Print using loop"
    getcontext().prec = 20
    #for i in f.value:
    #    temp_fi=str(i).strip('[]')
    #    print temp_fi

    return f.value
Esempio n. 16
0
#!/usr/bin/python

import cvxpy
import numpy

S = cvxpy.matrix([[-1, 1, 0],
                  [0, -1, 1]])
Km = cvxpy.matrix([[1e-4, 0, 0],
                   [0, 1e-4, 0]])
kcat = cvxpy.matrix([[100],[100]])
m_plus = numpy.abs(numpy.clip(S, -1000, 0))

c = cvxpy.variable(3, 1, name='concentrations')


opt = cvxpy.minimize()

Esempio n. 17
0
#!/usr/bin/python

import cvxpy
import numpy

S = cvxpy.matrix([[-1, 1, 0], [0, -1, 1]])
Km = cvxpy.matrix([[1e-4, 0, 0], [0, 1e-4, 0]])
kcat = cvxpy.matrix([[100], [100]])
m_plus = numpy.abs(numpy.clip(S, -1000, 0))

c = cvxpy.variable(3, 1, name='concentrations')

opt = cvxpy.minimize()
Esempio n. 18
0
    def FindMTDF(self, concentration_bounds=None, normalization=None):
        """Finds the MTDF.
        
        Args:
            bounds: the Bounds objects setting concentration bounds.
        """        
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()
        normalization = normalization or self.DeltaGNormalization.DEFAULT
                
        # Constrain concentrations
        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
                  cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
        
        # Make the objective
        motive_force_lb = cvxpy.variable(name='B')
        my_dG0_r_primes = np.matrix(self.dG0_r_prime)

        
        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)
        
        for i, flux in enumerate(self.fluxes):
            
            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue
            
            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0))
            else:
                motive_force = self.DeltaGNormalization.NormalizeDGByFlux(
                    curr_dgr, flux, normalization)
                
                constr.append(cvxpy.geq(motive_force, motive_force_lb))
        
        objective = cvxpy.maximize(motive_force_lb)
        problem = cvxpy.program(objective, constr, name='MTDF_OPT')
        
        problem.solve(quiet=True)
        """
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return MTDFOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """
        
        mtdf = float(motive_force_lb.value)
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = MTDFOptimizedPathway(
            self._model, self._thermo,
            my_bounds, optimal_value=mtdf,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        result.SetNormalization(normalization)
        return result
        
        
        
Esempio n. 19
0
def fit_ellipse_stack2(dx, dy, dz, di, norm_type="l2"):
    """
    fit ellipoid using squared loss

    idea to learn all stacks together including smoothness

    """

    #TODO create flag for norm1 vs norm2
    
    assert norm_type in ["l1", "l2", "huber"]

    # sanity check
    assert len(dx) == len(dy)
    assert len(dx) == len(dz)
    assert len(dx) == len(di)

    # unique zs
    dat = defaultdict(list)

    # resort data
    for idx in range(len(dx)):
        dat[dz[idx]].append( [dx[idx], dy[idx], di[idx]] )

    # init ret
    ellipse_stack = []
    for idx in range(max(dz)):
        ellipse_stack.append(Ellipse(0, 0, idx, 1, 1, 0))
    

    total_N = len(dx)
    M = len(dat.keys())
    #D = 5
    D = 4

    X_matrix = []
    thetas = []
    slacks = []
    eps_slacks = []

    mean_di = float(numpy.mean(di))

    for z in dat.keys():

        x = numpy.array(dat[z])[:,0]
        y = numpy.array(dat[z])[:,1]

        # intensities
        i = numpy.array(dat[z])[:,2]
        ity = numpy.diag(i) / mean_di

        # dimensionality
        N = len(x)
        d = numpy.zeros((N, D))

        d[:,0] = x*x
        d[:,1] = y*y
        #d[:,2] = x*y
        d[:,2] = x
        d[:,3] = y
        #d[:,4] = numpy.ones(N)

        #d[:,0] = x*x
        #d[:,1] = y*y
        #d[:,2] = x*y
        #d[:,3] = x
        #d[:,4] = y
        #d[:,5] = numpy.ones(N)
    
        # consider intensities
        old_shape = d.shape
        #d = numpy.dot(ity, d)
        assert d.shape == old_shape
    
        print d.shape   
        d = cvxpy.matrix(d)
        #### parameters

        # da
        X = cvxpy.parameter(N, D, name="X" + str(z))
        X.value = d
        X_matrix.append(X)


        #### varibales
    
        # parameter vector
        theta = cvxpy.variable(D, name="theta" + str(z))
        thetas.append(theta)


    # construct obj
    objective = 0

    print "norm type", norm_type 

    for i in xrange(M):


        if norm_type == "l1":
            objective += cvxpy.norm1(X_matrix[i] * thetas[i] + 1.0)
        if norm_type == "l2":
            objective += cvxpy.norm2(X_matrix[i] * thetas[i] + 1.0)

        #TODO these need to be summed
        #objective += cvxpy.huber(X_matrix[i] * thetas[i], 1)
        #objective += cvxpy.deadzone(X_matrix[i] * thetas[i], 1)


    # add smoothness regularization
    reg_const = float(total_N) / float(M-1)

    for i in xrange(M-1):
        objective += reg_const * cvxpy.norm2(thetas[i] - thetas[i+1])


    # create problem                                    
    p = cvxpy.program(cvxpy.minimize(objective))

    prob = p
    import ipdb
    ipdb.set_trace()

    # add constraints
    #for i in xrange(M):
    #    #p.constraints.append(cvxpy.eq(thetas[i][0,:] + thetas[i][1,:], 1))
    #    p.constraints.append(cvxpy.eq(thetas[i][4,:], 1))

    # set solver settings
    p.options['reltol'] = 1e-1
    p.options['abstol'] = 1e-1
    #p.options['feastol'] = 1e-1

    # invoke solver
    p.solve()
    

    # wrap up result
    ellipse_stack = {}

    active_layers = dat.keys()
    assert len(active_layers) == M

    for i in xrange(M):

        w = numpy.array(thetas[i].value)

        ## For clarity, fill in the quadratic form variables
        #A        = numpy.zeros((2,2))
        #A[0,0]   = w[0]
        #A.ravel()[1:3] = w[2]
        #A[1,1]   = w[1]
        #bv       = w[3:5]
        #c        = w[5]

        A              = numpy.zeros((2,2))
        A[0,0]         = w[0]
        A.ravel()[1:3] = 0 #w[2]
        A[1,1]         = w[1]
        #bv             = w[2:4]
        bv             = w[2:]
        #c              = w[4]
        c              = 1.0
                
        ## find parameters
        z, a, b, alpha = util.conic2parametric(A, bv, c)
        print "layer (i,z,a,b,alpha):", i, z, a, b, alpha

        layer = active_layers[i]
        ellipse_stack[layer] = Ellipse(z[0], z[1], layer, a, b, alpha)


    return ellipse_stack
Esempio n. 20
0
    def FindMTDF(self, concentration_bounds=None, normalization=None):
        """Finds the MTDF.
        
        Args:
            bounds: the Bounds objects setting concentration bounds.
        """
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()
        normalization = normalization or self.DeltaGNormalization.DEFAULT

        # Constrain concentrations
        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [
            cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
            cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))
        ]

        # Make the objective
        motive_force_lb = cvxpy.variable(name='B')
        my_dG0_r_primes = np.matrix(self.dG0_r_prime)

        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)

        for i, flux in enumerate(self.fluxes):

            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue

            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0))
            else:
                motive_force = self.DeltaGNormalization.NormalizeDGByFlux(
                    curr_dgr, flux, normalization)

                constr.append(cvxpy.geq(motive_force, motive_force_lb))

        objective = cvxpy.maximize(motive_force_lb)
        problem = cvxpy.program(objective, constr, name='MTDF_OPT')

        problem.solve(quiet=True)
        """
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return MTDFOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """

        mtdf = float(motive_force_lb.value)
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = MTDFOptimizedPathway(
            self._model,
            self._thermo,
            my_bounds,
            optimal_value=mtdf,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        result.SetNormalization(normalization)
        return result
    def MinimizeConcentration(self, metabolite_index=None,
                              concentration_bounds=None):
        """Finds feasible concentrations minimizing the concentration
           of metabolite i.
        
        Args:
            metabolite_index: the index of the metabolite to minimize.
                if == None, minimize the sum of all concentrations.
            concentration_bounds: the Bounds objects setting concentration bounds.
        """
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()

        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
                  cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
        
        my_dG0_r_primes = np.matrix(self.dG0_r_prime)
        
        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)
        
        for i, flux in enumerate(self.fluxes):
            
            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue
            
            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0.0))
            else:
                constr.append(cvxpy.leq(curr_dgr, 0.0))        
        
        objective = None
        if metabolite_index is not None:
            my_conc = ln_conc[0, metabolite_index]
            objective = cvxpy.minimize(cvxpy.exp(my_conc))
        else:
            objective = cvxpy.minimize(
                cvxpy.sum(cvxpy.exp(ln_conc)))
        
        name = 'CONC_OPT'
        if metabolite_index:
            name = 'CONC_%d_OPT' % metabolite_index
        problem = cvxpy.program(objective, constr, name=name)
        optimum = problem.solve(quiet=True)        

        """
        status = problem.solve(quiet=True)
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return ConcentrationOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = ConcentrationOptimizedPathway(
            self._model, self._thermo,
            my_bounds, optimal_value=optimum,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        return result
        
        
        
Esempio n. 22
0
    def ACT2Corrected(self,gene,num_iterations=5):
        """
            Next steps: Some way to preserve flows at divergence nodes
            One way could be reallocate flows at all divergence nodes in the original ratio and fix it 
            Iterate 10 times
        """
        inwgs=self.wgsdict[gene]
        outwgs=inwgs
        component1=1.0
        for iteri in range(num_iterations):
            component1=1.0-iteri*1.0/num_iterations
            wgs=addwgs(inwgs,outwgs,component1)
            A,B,X=self.wgs2problem(wgs)
            Xvar = cvx.variable(len(X),1)    
            A=cvx.matrix(A)
            B=cvx.matrix(B)
            B=B.T
            p = cvx.program(cvx.minimize(cvx.norm2(A*Xvar-B)),[cvx.geq(Xvar,0.0)])
            try:
                p.solve(quiet=1)
            except:
                message='Could not solve for %s'%(gene)
                common.printstatus(message,'W',common.func_name(),1)   
                return (outwgs,100.0)   
            if iteri==0:                          # Get optimal value
                err=cvx.norm2(A*Xvar-B)
            #print err.value/len(X)
            Xval=Xvar.T.value.tolist()[0]
            X_corr= [a[:] for a in X]
            for i in range(len(Xval)):
                X_corr[i][3]=int(Xval[i]*100)/100.0
            
            #print X_corr
            exonlist=[[a[1],a[2]] for a in X_corr if a[0]==2]
            exonwtlist=[a[3] for a in X_corr if a[0]==2]
            #print 'E',exonlist
            intronlist=[]
            intronwtlist=[]
            splicelist=[[a[1],a[2]] for a in X_corr if a[0]==3]
            splicewtlist=[a[3] for a in X_corr if a[0]==3]
            removelist=[]
            for i in range(len(exonlist)):
                exon=exonlist[i]
                if exon in splicelist:
                    exonwt=exonwtlist[i]
                    intronlist.append([exon[0]+1,exon[1]-1])
                    intronwtlist.append(exonwt)
                    removelist.append(i)
            removelist.reverse()
            for i in removelist:
                exonlist.pop(i)
                exonwtlist.pop(i)
                    
            #print 'E',exonlist
            startnodelist=[a[1]for a in X_corr if a[0]==1]
            endnodelist=[a[1]for a in X_corr if a[0]==-1]
            novelnodelist=wgs[5]
            #print exonlist
            #print wgs[0]
            #print intronlist
            #print wgs[1]

            exonwtlist1=[exonwtlist[i] for i in range(len(exonwtlist)) if exonlist[i] in wgs[0]]
            intronwtlist1=[exonwtlist[i] for i in range(len(exonwtlist)) if exonlist[i] in wgs[1]]
            #wgrstuple=(exonlist,intronlist,splicelist,startnodelist,endnodelist,novelnodelist,exonwtlist,intronwtlist,splicewtlist)
            outwgs=(wgs[0],wgs[1],splicelist,wgs[3],wgs[4],novelnodelist,exonwtlist1,intronwtlist1,splicewtlist)
        
        return (outwgs,err.value/len(X))
Esempio n. 23
0
def fit_ellipse(x, y):
    """
    fit ellipoid using squared loss and abs loss
    """

    #TODO introduce flag for switching between losses

    assert len(x) == len(y)

    N = len(x)
    D = 5

    dat = numpy.zeros((N, D))
    dat[:,0] = x*x
    dat[:,1] = y*y
    #dat[:,2] = x*y
    dat[:,2] = x
    dat[:,3] = y
    dat[:,4] = numpy.ones(N)


    print dat.shape
    dat = cvxpy.matrix(dat)
    #### parameters

    # data
    X = cvxpy.parameter(N, D, name="X")


    #### varibales

    # parameter vector
    theta = cvxpy.variable(D, name="theta")

    # simple objective 
    objective = cvxpy.norm1(X*theta)

    # create problem                                    
    p = cvxpy.program(cvxpy.minimize(objective))

    
    p.constraints.append(cvxpy.eq(theta[0,:] + theta[1,:], 1))
   
    ###### set values
    X.value = dat

    p.solve()

    w = numpy.array(theta.value)
    
    #print weights


    ## For clarity, fill in the quadratic form variables
    A              = numpy.zeros((2,2))
    A[0,0]         = w[0]
    A.ravel()[1:3] = 0 #w[2]
    A[1,1]         = w[1]
    bv             = w[2:4]
    c              = w[4]

    ## find parameters
    z, a, b, alpha = util.conic2parametric(A, bv, c)
    print "XXX", z, a, b, alpha

    return z, a, b, alpha