Skip to content

Commit

Permalink
fix: simplify keyword argument passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Midnighter committed May 19, 2018
1 parent 725c41c commit 08c7362
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
40 changes: 24 additions & 16 deletions cobra/flux_analysis/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,8 @@ def _multi_deletion(model, entity, element_lists, method="fba",
The number of parallel processes to run. Can speed up the computations
if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
**kwargs:
delta: float
The relative tolerance for ROOM.
Default is 0.03.
epsilon: float
The absolute tolerance for ROOM.
Default is 0.001.
kwargs :
Passed on to underlying simulation functions.
Returns
-------
Expand Down Expand Up @@ -189,7 +184,7 @@ def _element_lists(entities, *ids):


def single_reaction_deletion(model, reaction_list=None, method="fba",
solution=None, processes=None):
solution=None, processes=None, **kwargs):
"""
Knock out each reaction from a given list.
Expand All @@ -208,6 +203,9 @@ def single_reaction_deletion(model, reaction_list=None, method="fba",
The number of parallel processes to run. Can speed up the computations
if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
kwargs :
Keyword arguments are passed on to underlying simulation functions
such as ``add_room``.
Returns
-------
Expand All @@ -226,11 +224,11 @@ def single_reaction_deletion(model, reaction_list=None, method="fba",
return _multi_deletion(
model, 'reaction',
element_lists=_element_lists(model.reactions, reaction_list),
method=method, solution=solution, processes=processes)
method=method, solution=solution, processes=processes, **kwargs)


def single_gene_deletion(model, gene_list=None, method="fba", solution=None,
processes=None):
processes=None, **kwargs):
"""
Knock out each gene from a given list.
Expand All @@ -249,6 +247,9 @@ def single_gene_deletion(model, gene_list=None, method="fba", solution=None,
The number of parallel processes to run. Can speed up the computations
if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
kwargs :
Keyword arguments are passed on to underlying simulation functions
such as ``add_room``.
Returns
-------
Expand All @@ -266,11 +267,12 @@ def single_gene_deletion(model, gene_list=None, method="fba", solution=None,
"""
return _multi_deletion(
model, 'gene', element_lists=_element_lists(model.genes, gene_list),
method=method, solution=solution, processes=processes)
method=method, solution=solution, processes=processes, **kwargs)


def double_reaction_deletion(model, reaction_list1=None, reaction_list2=None,
method="fba", solution=None, processes=None):
method="fba", solution=None, processes=None,
**kwargs):
"""
Knock out each reaction pair from the combinations of two given lists.
Expand All @@ -294,6 +296,9 @@ def double_reaction_deletion(model, reaction_list1=None, reaction_list2=None,
The number of parallel processes to run. Can speed up the computations
if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
kwargs :
Keyword arguments are passed on to underlying simulation functions
such as ``add_room``.
Returns
-------
Expand All @@ -315,11 +320,11 @@ def double_reaction_deletion(model, reaction_list1=None, reaction_list2=None,
reaction_list2)
return _multi_deletion(
model, 'reaction', element_lists=[reaction_list1, reaction_list2],
method=method, solution=solution, processes=processes)
method=method, solution=solution, processes=processes, **kwargs)


def double_gene_deletion(model, gene_list1=None, gene_list2=None,
method="fba", solution=None, processes=None):
def double_gene_deletion(model, gene_list1=None, gene_list2=None, method="fba",
solution=None, processes=None, **kwargs):
"""
Knock out each gene pair from the combination of two given lists.
Expand All @@ -343,6 +348,9 @@ def double_gene_deletion(model, gene_list1=None, gene_list2=None,
The number of parallel processes to run. Can speed up the computations
if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
kwargs :
Keyword arguments are passed on to underlying simulation functions
such as ``add_room``.
Returns
-------
Expand All @@ -363,4 +371,4 @@ def double_gene_deletion(model, gene_list1=None, gene_list2=None,
gene_list2)
return _multi_deletion(
model, 'gene', element_lists=[gene_list1, gene_list2],
method=method, solution=solution, processes=processes)
method=method, solution=solution, processes=processes, **kwargs)
31 changes: 12 additions & 19 deletions cobra/flux_analysis/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from optlang.symbolics import Zero, NegativeOne, Add, Mul


def add_room(model, solution=None, linear=False, **kwargs):
"""Add constraints and objective for ROOM.
def add_room(model, solution=None, linear=False, delta=0.03, epsilon=1E-03):
"""
Add constraints and objective for ROOM.
This function adds variables and constraints for applying regulatory
on/off minimization (ROOM) to the model.
Expand All @@ -22,13 +23,12 @@ def add_room(model, solution=None, linear=False, **kwargs):
linear : bool
Whether to use the linear ROOM formulation or not.
Default is False.
**kwargs:
delta: float
Relative range of tolerance; is additive in nature.
Default is 0.03.
epsilon: float
Absolute range of tolerance; is multiplicative in nature.
Default is 0.001.
delta: float
The relative tolerance range which is additive in nature
(default 0.03).
epsilon: float
The absolute range of tolerance which is multiplicative
(default 0.001).
Notes
-----
Expand Down Expand Up @@ -58,13 +58,6 @@ def add_room(model, solution=None, linear=False, **kwargs):
"""

if kwargs:
delta = float(kwargs['delta'])
epsilon = float(kwargs['epsilon'])
else:
delta = 0.03
epsilon = 0.001

if 'room_old_objective' in model.solver.variables:
raise ValueError('model is already adjusted for ROOM')

Expand All @@ -87,11 +80,11 @@ def add_room(model, solution=None, linear=False, **kwargs):
for rxn in model.reactions:
flux = solution.fluxes[rxn.id]

if linear is False:
y = problem.Variable("y_" + rxn.id, type="binary")
else:
if linear:
y = problem.Variable("y_" + rxn.id, lb=0, ub=1)
delta = epsilon = Zero
else:
y = problem.Variable("y_" + rxn.id, type="binary")

# upper constraint
w_u = flux + (delta * abs(flux)) + epsilon
Expand Down
2 changes: 1 addition & 1 deletion cobra/test/test_flux_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def test_room_sanity(self, solver):
room_ref_sol = model.optimize()
room_ref_ssq = (room_ref_sol.fluxes - sol.fluxes).pow(2).sum()

assert room_ssq == ssq
assert numpy.isclose(room_ssq, ssq, atol=1E-06)
assert room_ssq > room_ref_ssq

@pytest.mark.parametrize("solver", optlang_solvers)
Expand Down

0 comments on commit 08c7362

Please sign in to comment.