Skip to content

Commit

Permalink
Fix and add test for exclude method
Browse files Browse the repository at this point in the history
  • Loading branch information
pablormier committed Aug 24, 2021
1 parent bc716b5 commit a8c3229
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 7 additions & 6 deletions miom/miom.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,11 +898,12 @@ def _keep(self, *args, **kwargs):
self.add_constraint(C.T * self.variables.indicators[idx] >= n)

def _exclude(self, *args, **kwargs):
values = kwargs["indicator_values"]
values = np.round(kwargs["indicator_values"])
b = sum(values) - 1
self.add_constraint(
(sum(self.variables.indicators[values==1]) +
sum(self.variables.indicators[values==0])) <= b)
idx_one = np.flatnonzero(values==1)
idx_zero = np.flatnonzero(values==0)
s = sum(self.variables.indicators[idx_one] // self.variables.indicators[idx_zero])
self.add_constraint(s <= b)

def _set_flux_bounds(self, *args, **kwargs):
i = kwargs["_parent_result"]
Expand Down Expand Up @@ -1072,8 +1073,8 @@ def _keep(self, *args, **kwargs):
def _exclude(self, *args, **kwargs):
values = kwargs["indicator_values"]
b = sum(values) - 1
x1 = mip.sum(v for i, v in enumerate(self.variable.indicators) if values[i] == 1)
x2 = mip.sum(v for i, v in enumerate(self.variable.indicators) if values[i] == 0)
x1 = mip.xsum(v for i, v in enumerate(self.variables.indicators) if values[i] == 1)
x2 = mip.xsum(v for i, v in enumerate(self.variables.indicators) if values[i] == 0)
self.add_constraint(x1 + x2 <= b)

def _set_flux_bounds(self, *args, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_miom.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,10 @@ def test_miom_consistent_subnetwork_with_blocked_rxns(model):
)
assert np.sum(X > 0.5) == 8

def test_exclude(model):
weights = -1*np.ones(model.network.num_reactions)
weights[[0,4,6,7,8,9]] = 1
m = prepare_fba(model).subset_selection(weights).solve()
assert m.status['objective_value'] == 9.0
m.exclude().solve()
assert m.status['objective_value'] == 8.0

0 comments on commit a8c3229

Please sign in to comment.