Tasks Assessment Module

Core module

class troppo.tasks.core.Task(**kwargs)[source]

Bases: object

A task is a set of constraints that can be applied to a model. It is defined by a set of reactions, a set of constraints on the fluxes of these reactions, a set of constraints on the inflow and outflow of metabolites, and a set of mandatory activities that must be present in the solution. A task can be evaluated on a model to determine if the model satisfies the task.

property annotations
apply_evaluate(model: ~cobamp.core.models.ConstraintBasedModel) -> (<class 'bool'>, <class 'dict'>)[source]

Apply the task to the model and evaluate the solution

Parameters

model: ConstraintBasedModel

The model to apply the task to

Returns

bool, dict: Whether the task was satisfied, and the expected activity of the mandatory reactions

combine(other, add: bool = True)[source]

Combine two tasks into a single task. The resulting task will have the same failure criteria as the original

Parameters

other: Task

The task to combine with

add: bool

If True, the resulting task will be the sum of the two tasks. If False, the resulting task will be the

Returns

Task: The combined task

combine_inplace(other, add: bool = True)[source]

Combine two tasks into a single task. The resulting task will have the same failure criteria as the original

Parameters

other: Task

The task to combine with

add: bool

If True, the resulting task will be the sum of the two tasks. If False, the resulting task will be the

Returns

Task: The combined task

evaluate_solution(sol: ~cobamp.core.optimization.Solution, ftol: float = 1e-06) -> (<class 'bool'>, <class 'dict'>)[source]

Evaluate the solution to the task

Parameters

sol: Solution

The solution to evaluate

ftol: float

The tolerance for the fluxes

Returns

bool, dict: Whether the task was satisfied, and the expected activity of the mandatory reactions

property flux_constraints
get_add_reaction_args(model: ~cobamp.core.models.ConstraintBasedModel, closed: bool = False) -> (<class 'dict'>, <class 'set'>)[source]

Get the arguments to add the reactions to the model

Parameters

model: ConstraintBasedModel

The model to add the reactions to

closed: bool

Whether to add the reactions as closed or open

Returns

dict, set: The arguments to add the reactions to the model, and the set of reactions that were added

get_add_reaction_cmds(model: ~cobamp.core.models.ConstraintBasedModel, closed: bool = False) -> (<class 'cobamp.utilities.context.CommandHistory'>, <class 'set'>)[source]

Get the commands to add the reactions to the model

Parameters

model: ConstraintBasedModel

The model to add the reactions to

closed: bool

Whether to add the reactions as closed or open

Returns

CommandHistory, set: The commands to add the reactions to the model, and the set of reactions that were added

get_task_bounds() dict[source]

Get the bounds for the task

Returns

dict: The bounds for the task

id_replace(func)[source]

Replace the identifiers in the task with the result of a function applied to the identifiers

Parameters

func: function

The function to apply to the identifiers

property inflow_dict
property involved_reactions: set

Get the set of reactions involved in the task

Returns

set: The set of reactions involved in the task

property mandatory_activity
property name
property outflow_dict
property reaction_dict
property should_fail
class troppo.tasks.core.TaskEvaluator(**kwargs)[source]

Bases: object

A task evaluator is a wrapper around a model that allows the evaluation of tasks on the model. It can be used to evaluate a single task, or to evaluate a batch of tasks on a batch of models.

Parameters

model: ConstraintBasedModel

The model to evaluate tasks on

tasks: Iterable[Task]

The tasks to evaluate on the model

solver: str

The solver to use for the model

S: np.ndarray

The stoichiometric matrix

lb: np.ndarray

The lower bounds for the reactions

ub: np.ndarray

The upper bounds for the reactions

rxn: np.ndarray

The reaction names

mtn: np.ndarray

The metabolite names

batch_evaluate(bound_changes: dict, threads: int = 2, output_sol: bool = False, mp_batch_size: int = 5000) dict[source]

Evaluate a batch of tasks on the model

Parameters

bound_changes: dict

The changes to apply to the model

threads: int

The number of threads to use

output_sol: bool

Whether to output the solution

mp_batch_size: int

The batch size for multiprocessing

Returns

dict: The results of the evaluation

static batch_function(task: ~troppo.tasks.core.Task, params: dict) -> (<class 'bool'>, <class 'cobamp.core.optimization.Solution'>)[source]

Evaluate a task on a model

Parameters

task: Task

The task to evaluate

params: dict

The parameters to use for evaluation

Returns

bool, Solution: Whether the task was satisfied, and the solution to the model

property current_task
evaluate(context_function=None, flux_distribution_func=None) -> (<class 'bool'>, <class 'cobamp.core.optimization.Solution'>)[source]

Evaluate the current task on the model

Parameters

context_function: function

A function to apply to the model prior to evaluation

flux_distribution_func: function

A function to apply to the model to get the flux distribution

Returns

bool, Solution: Whether the task was satisfied, and the solution to the model

property tasks: Iterable[Task]

Get the tasks

Returns

list: The tasks

Task-io module

class troppo.tasks.task_io.ExcelTaskIO[source]

Bases: TaskIO

ExcelTaskIO is a TaskIO subclass that reads and writes tasks to and from Excel files.

read_from_string(string: str) list[source]

Reads a task from an Excel string. The string must be in the format specified by the subclass.

Parameters

string: str

Returns

list of Task objects

read_task(buffer_or_path: str, binary_mode: bool = True) list[source]

Reads a task from an Excel file or buffer. The file or buffer must be in the format specified by the subclass.

Parameters

buffer_or_path: str or TextIOBase

The path to the file or a buffer containing the task

binary_mode: bool

Whether to open the file in binary mode. Only relevant for file paths.

Returns

list of Task objects

write_to_string(task: Task) str[source]

Writes a task to an Excel string. The string will be in the format specified by the subclass.

Parameters

task: Task or list of Task

Returns

str

The Excel string

class troppo.tasks.task_io.JSONTaskIO[source]

Bases: TaskIO

JSONTaskIO is a TaskIO subclass that reads and writes tasks to and from JSON files.

read_from_string(string: str) list[source]

Reads a task from a JSON string. The string must be in the format specified by the subclass.

Parameters

string: str

Returns

list of Task objects

write_to_string(task_arg: Task) str[source]

Writes a task to a JSON string. The string will be in the format specified by the subclass.

Parameters

task_arg: Task or list of Task

Returns

str

The JSON string

class troppo.tasks.task_io.TaskIO[source]

Bases: object

Abstract class for reading and writing tasks to and from files.

abstract read_from_string(string)[source]
read_task(buffer_or_path: str, binary_mode: bool = False) list[source]

Reads a task from a file or buffer. The file or buffer must be in the format specified by the subclass.

Parameters

buffer_or_path: str or TextIOBase

The path to the file or a buffer containing the task

binary_mode: bool

Whether to open the file in binary mode. Only relevant for file paths.

Returns

list of Task objects

write_task(buffer_or_path: str, task_arg: Task, binary_mode: bool = False) int[source]

Writes a task to a file or buffer. The file or buffer will be in the format specified by the subclass.

Parameters

buffer_or_path: str or TextIOBase

The path to the file or a buffer containing the task

task_arg: Task or list of Task

The task or tasks to write

binary_mode: bool

Whether to open the file in binary mode. Only relevant for file paths.

Returns

int or None

The number of bytes written to the file or buffer. Only relevant for file paths.

abstract write_to_string(task)[source]