code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
met2mol = map_metabolites_to_structures(model.metabolites,
model.compartments)
# Build a list associating reactions with their stoichiometry in molecular
# structure space.
structural = []
for rxn in model.reactions:
# Ignore reactions that ha... | def find_duplicate_reactions(model) | Return a list with pairs of reactions that are functionally identical.
Identify duplicate reactions globally by checking if any
two reactions have the same metabolites, same directionality and are in
the same compartment.
This can be useful to curate merged models or to clean-up bulk model
modific... | 3.353668 | 3.281698 | 1.021931 |
duplicates = dict()
for rxn_a, rxn_b in combinations(model.reactions, 2):
if not (rxn_a.genes and rxn_b.genes):
continue
if rxn_a.genes == rxn_b.genes:
# This works because the `genes` are frozen sets.
identifiers = rxn_a.genes
duplicates.setd... | def find_reactions_with_identical_genes(model) | Return reactions that have identical genes.
Identify duplicate reactions globally by checking if any
two reactions have the same genes.
This can be useful to curate merged models or to clean-up bulk model
modifications, but also to identify promiscuous enzymes.
The heuristic compares reactions in a... | 3.624788 | 3.700794 | 0.979462 |
return [met.id for rxn in model.medium
for met in model.reactions.get_by_id(rxn).metabolites] | def find_medium_metabolites(model) | Return the list of metabolites ingested/excreted by the model. | 4.360921 | 3.792916 | 1.149754 |
ex_comp = find_external_compartment(model)
return [met for met in model.metabolites if met.compartment == ex_comp] | def find_external_metabolites(model) | Return all metabolites in the external compartment. | 3.450708 | 2.411144 | 1.43115 |
LOGGER.info("Storing result in '%s'.", filename)
if filename.endswith(".gz"):
with gzip.open(filename, "wb") as file_handle:
file_handle.write(
jsonify(result, pretty=pretty).encode("utf-8")
)
else:
with open(fi... | def store(self, result, filename, pretty=True) | Write a result to the given file.
Parameters
----------
result : memote.MemoteResult
The dictionary structure of results.
filename : str or pathlib.Path
Store results directly to the given filename.
pretty : bool, optional
Whether (default) or... | 2.15518 | 2.143769 | 1.005323 |
LOGGER.info("Loading result from '%s'.", filename)
if filename.endswith(".gz"):
with gzip.open(filename, "rb") as file_handle:
result = MemoteResult(
json.loads(file_handle.read().decode("utf-8"))
)
else:
with o... | def load(self, filename) | Load a result from the given JSON file. | 6.28723 | 5.784827 | 1.086848 |
doc = libsbml.readSBML(path)
fbc = doc.getPlugin("fbc")
sbml_ver = doc.getLevel(), doc.getVersion(), fbc if fbc is None else \
fbc.getVersion()
with catch_warnings(record=True) as warnings:
simplefilter("always")
try:
model = read_sbml_model(path)
except ... | def load_cobra_model(path, notifications) | Load a COBRA model with meta information from an SBML document. | 3.687373 | 3.519193 | 1.047789 |
return "Line {}, Column {} - #{}: {} - Category: {}, Severity: {}".format(
failure.getLine(),
failure.getColumn(),
failure.getErrorId(),
failure.getMessage(),
failure.getCategoryAsString(),
failure.getSeverity()
) | def format_failure(failure) | Format how an error or warning should be displayed. | 5.732795 | 5.188221 | 1.104963 |
validator = libsbml.SBMLValidator()
validator.validate(document)
for i in range(document.getNumErrors()):
notifications['errors'].append(format_failure(document.getError(i)))
for i in range(validator.getNumFailures()):
failure = validator.getFailure(i)
if failure.isWarning()... | def run_sbml_validation(document, notifications) | Report errors and warnings found in an SBML document. | 2.22251 | 2.235252 | 0.994299 |
git_info = self.record_git_info(commit)
try:
row = self.session.query(Result). \
filter_by(hexsha=git_info.hexsha). \
one()
LOGGER.info("Updating result '%s'.", git_info.hexsha)
row.memote_result = result
except NoResul... | def store(self, result, commit=None, **kwargs) | Store a result in a JSON file attaching git meta information.
Parameters
----------
result : memote.MemoteResult
The dictionary structure of results.
commit : str, optional
Unique hexsha of the desired commit.
kwargs :
Passed to parent functio... | 2.880211 | 2.706098 | 1.064341 |
git_info = self.record_git_info(commit)
LOGGER.info("Loading result from '%s'.", git_info.hexsha)
result = MemoteResult(
self.session.query(Result.memote_result).
filter_by(hexsha=git_info.hexsha).
one().memote_result)
# Add git info so the ob... | def load(self, commit=None) | Load a result from the database. | 7.899467 | 7.312234 | 1.080308 |
def format_data(data):
# TODO Remove this failsafe once proper error handling is in place.
if type == "percent" or data is None:
# Return an empty list here to reduce the output file size.
# The angular report will ignore the `data` a... | def collect_history(self) | Build the structure of results in terms of a commit history. | 3.725594 | 3.647769 | 1.021335 |
base = dict()
meta = base.setdefault('meta', dict())
tests = base.setdefault('tests', dict())
score = base.setdefault('score', dict())
for model_filename, result in iteritems(diff_results):
if meta == dict():
meta = result["meta"]
... | def format_and_score_diff_data(self, diff_results) | Reformat the api results to work with the front-end. | 2.274915 | 2.256858 | 1.008001 |
# Reduce the whole database to targets of interest.
xref = mnx_db.loc[mnx_db["MNX_ID"].isin(shortlist["MNX_ID"]), :]
# Drop deprecated MetaNetX identifiers. Disabled for now.
# xref = xref.loc[~xref["XREF"].str.startswith("deprecated", na=False), :]
# Drop self-references for now since they don... | def generate_shortlist(mnx_db, shortlist) | Create a condensed cross-references format from data in long form.
Both data frames must contain a column 'MNX_ID' and the dump is assumed
to also have a column 'XREF'.
Parameters
----------
mnx_db : pandas.DataFrame
The entire MetaNetX dump as a data frame.
shortlist : pandas.DataFram... | 5.334105 | 4.965311 | 1.074274 |
LOGGER.info("Read shortlist.")
targets = pd.read_table(join(dirname(__file__), "shortlist.tsv"))
if not exists(mnx_dump):
# Download the MetaNetX chemicals dump if it doesn't exists.
# Download done as per https://stackoverflow.com/a/16696317.
LOGGER.info("MetaNetX dump '%s' doe... | def generate(mnx_dump) | Annotate a shortlist of metabolites with cross-references using MetaNetX.
MNX_DUMP : The chemicals dump from MetaNetX usually called 'chem_xref.tsv'.
Will be downloaded if it doesn't exist. | 4.360621 | 3.644452 | 1.19651 |
custom = [
check_partial(gene_id_check, frozenset(g.id for g in model.genes))
]
super(EssentialityExperiment, self).validate(
model=model, checks=checks + custom) | def validate(self, model, checks=[]) | Use a defined schema to validate the medium table format. | 11.618004 | 11.771293 | 0.986978 |
with model:
if self.medium is not None:
self.medium.apply(model)
if self.objective is not None:
model.objective = self.objective
model.add_cons_vars(self.constraints)
max_val = model.slim_optimize()
essen = sing... | def evaluate(self, model) | Use the defined parameters to predict single gene essentiality. | 6.067749 | 5.480217 | 1.10721 |
def decorator(func):
registry[func.__name__] = func
return func
return decorator | def register_with(registry) | Register a passed in object.
Intended to be used as a decorator on model building functions with a
``dict`` as a registry.
Examples
--------
.. code-block:: python
REGISTRY = dict()
@register_with(REGISTRY)
def build_empty(base):
return base | 3.146045 | 7.13702 | 0.440807 |
if format_type not in TYPES:
raise ValueError(
"Invalid type. Expected one of: {}.".format(", ".join(TYPES)))
def decorator(func):
func.annotation = dict(
title=title,
summary=extended_summary(func),
message=message,
data=data,
... | def annotate(title, format_type, message=None, data=None, metric=1.0) | Annotate a test case with info that should be displayed in the reports.
Parameters
----------
title : str
A human-readable descriptive title of the test case.
format_type : str
A string that determines how the result data is formatted in the
report. It is expected not to be None... | 3.388693 | 3.617796 | 0.936673 |
if len(sequence) > LIST_SLICE:
return ", ".join(sequence[:LIST_SLICE] + ["..."])
else:
return ", ".join(sequence) | def truncate(sequence) | Create a potentially shortened text display of a list.
Parameters
----------
sequence : list
An indexable sequence of elements.
Returns
-------
str
The list as a formatted string. | 3.346365 | 3.734546 | 0.896057 |
keys_to_explore = list(obj)
while len(keys_to_explore) > 0:
key = keys_to_explore.pop()
if not isinstance(key, str):
LOGGER.info(type(key))
value = obj[key]
if isinstance(value, dict):
LOGGER.info("%s:", key)
log_json_incompatible_types(va... | def log_json_incompatible_types(obj) | Log types that are not JSON compatible.
Explore a nested dictionary structure and log types that are not JSON
compatible.
Parameters
----------
obj : dict
A potentially nested dictionary. | 2.511616 | 2.607453 | 0.963245 |
if pretty:
params = dict(sort_keys=True, indent=2, allow_nan=False,
separators=(",", ": "), ensure_ascii=False)
else:
params = dict(sort_keys=False, indent=None, allow_nan=False,
separators=(",", ":"), ensure_ascii=False)
try:
return j... | def jsonify(obj, pretty=False) | Turn a nested object into a (compressed) JSON string.
Parameters
----------
obj : dict
Any kind of dictionary structure.
pretty : bool, optional
Whether to format the resulting JSON in a more legible way (
default False). | 3.416768 | 3.456017 | 0.988643 |
flat_list = []
for sublist in list_of_lists:
if isinstance(sublist, string_types) or isinstance(sublist, int):
flat_list.append(sublist)
elif sublist is None:
continue
elif not isinstance(sublist, string_types) and len(sublist) == 1:
flat_list.app... | def flatten(list_of_lists) | Flatten a list of lists but maintain strings and ints as entries. | 1.940279 | 1.804353 | 1.075332 |
for error in notifications["errors"]:
LOGGER.error(error)
for warn in notifications["warnings"]:
LOGGER.warning(warn) | def stdout_notifications(notifications) | Print each entry of errors and warnings to stdout.
Parameters
----------
notifications: dict
A simple dictionary structure containing a list of errors and warnings. | 3.722474 | 3.652075 | 1.019276 |
self.data = read_tabular(self.filename, dtype_conversion)
with open_text(memote.experimental.schemata, self.SCHEMA,
encoding="utf-8") as file_handle:
self.schema = json.load(file_handle) | def load(self, dtype_conversion=None) | Load the data table and corresponding validation schema.
Parameters
----------
dtype_conversion : dict
Column names as keys and corresponding type for loading the data.
Please take a look at the `pandas documentation
<https://pandas.pydata.org/pandas-docs/sta... | 8.81836 | 9.932569 | 0.887823 |
records = self.data.to_dict("records")
self.evaluate_report(
validate(records, headers=list(records[0]),
preset='table', schema=self.schema,
order_fields=True, custom_checks=checks)) | def validate(self, model, checks=[]) | Use a defined schema to validate the given table. | 13.498739 | 11.505346 | 1.173258 |
if report["valid"]:
return
for warn in report["warnings"]:
LOGGER.warning(warn)
# We only ever test one table at a time.
for err in report["tables"][0]["errors"]:
LOGGER.error(err["message"])
raise ValueError("Invalid data file. Please... | def evaluate_report(report) | Iterate over validation errors. | 7.601578 | 6.596705 | 1.152329 |
constraints = []
for rxn in reactions:
expression = add(
[c * model.variables[m.id] for m, c in rxn.metabolites.items()])
constraints.append(Constraint(expression, lb=0, ub=0, name=rxn.id))
model.add(constraints) | def add_reaction_constraints(model, reactions, Constraint) | Add the stoichiometric coefficients as constraints.
Parameters
----------
model : optlang.Model
The transposed stoichiometric matrix representation.
reactions : iterable
Container of `cobra.Reaction` instances.
Constraint : optlang.Constraint
The constraint class for the spe... | 3.544482 | 3.963964 | 0.894176 |
matrix = np.zeros((len(metabolites), len(reactions)))
met_index = dict((met, i) for i, met in enumerate(metabolites))
rxn_index = dict()
for i, rxn in enumerate(reactions):
rxn_index[rxn] = i
for met, coef in iteritems(rxn.metabolites):
j = met_index[met]
mat... | def stoichiometry_matrix(metabolites, reactions) | Return the stoichiometry matrix representation of a set of reactions.
The reactions and metabolites order is respected. All metabolites are
expected to be contained and complete in terms of the reactions.
Parameters
----------
reactions : iterable
A somehow ordered list of unique reactions... | 1.968868 | 2.144646 | 0.918038 |
matrix = np.atleast_2d(matrix)
sigma = svd(matrix, compute_uv=False)
tol = max(atol, rtol * sigma[0])
return int((sigma >= tol).sum()) | def rank(matrix, atol=1e-13, rtol=0) | Estimate the rank, i.e., the dimension of the column space, of a matrix.
The algorithm used by this function is based on the singular value
decomposition of `stoichiometry_matrix`.
Parameters
----------
matrix : ndarray
The matrix should be at most 2-D. A 1-D array with length k
w... | 3.581346 | 3.917831 | 0.914114 |
def nullspace(matrix, atol=1e-13, rtol=0.0): # noqa: D402
matrix = np.atleast_2d(matrix)
_, sigma, vh = svd(matrix)
tol = max(atol, rtol * sigma[0])
num_nonzero = (sigma >= tol).sum()
return vh[num_nonzero:].conj().T | Compute an approximate basis for the null space (kernel) of a matrix.
The algorithm used by this function is based on the singular value
decomposition of the given matrix.
Parameters
----------
matrix : ndarray
The matrix should be at most 2-D. A 1-D array with length k
will be tr... | null | null | null | |
return (
model.solver.interface.Model,
model.solver.interface.Constraint,
model.solver.interface.Variable,
model.solver.interface.Objective
) | def get_interface(model) | Return the interface specific classes.
Parameters
----------
model : cobra.Model
The metabolic model under investigation. | 4.175203 | 3.968763 | 1.052016 |
biomass = set(find_biomass_reaction(model))
if len(biomass) == 0:
LOGGER.warning("No biomass reaction detected. Consistency test results "
"are unreliable if one exists.")
return set(model.reactions) - (set(model.boundary) | biomass) | def get_internals(model) | Return non-boundary reactions and their metabolites.
Boundary reactions are unbalanced by their nature. They are excluded here
and only the metabolites of the others are considered.
Parameters
----------
model : cobra.Model
The metabolic model under investigation. | 6.56553 | 6.470509 | 1.014685 |
assert len(metabolites) == kernel.shape[0],\
"metabolite vector and first nullspace dimension must be equal"
ns_problem = Model()
k_vars = list()
for met in metabolites:
# The element y[i] of the mass vector.
y_var = Variable(met.id)
k_var = Variable("k_{}".format(me... | def create_milp_problem(kernel, metabolites, Model, Variable, Constraint,
Objective) | Create the MILP as defined by equation (13) in [1]_.
Parameters
----------
kernel : numpy.array
A 2-dimensional array that represents the left nullspace of the
stoichiometric matrix which is the nullspace of the transpose of the
stoichiometric matrix.
metabolites : iterable
... | 3.488011 | 3.428319 | 1.017411 |
cut = Constraint(sympy.Add(*indicators), ub=bound)
problem.add(cut)
return cut | def add_cut(problem, indicators, bound, Constraint) | Add an integer cut to the problem.
Ensure that the same solution involving these indicator variables cannot be
found by enforcing their sum to be less than before.
Parameters
----------
problem : optlang.Model
Specific optlang interface Model instance.
indicators : iterable
Bin... | 6.559551 | 13.439859 | 0.488067 |
balance = defaultdict(int)
for metabolite, coefficient in iteritems(reaction.metabolites):
if metabolite.elements is None or len(metabolite.elements) == 0:
return False
for element, amount in iteritems(metabolite.elements):
balance[element] += coefficient * amount
... | def is_mass_balanced(reaction) | Confirm that a reaction is mass balanced. | 2.617828 | 2.520517 | 1.038608 |
charge = 0
for metabolite, coefficient in iteritems(reaction.metabolites):
if metabolite.charge is None:
return False
charge += coefficient * metabolite.charge
return charge == 0 | def is_charge_balanced(reaction) | Confirm that a reaction is charge balanced. | 2.652765 | 2.569924 | 1.032235 |
new_func = partial(func, *args, **kwargs)
new_func.check = func.check
return new_func | def check_partial(func, *args, **kwargs) | Create a partial to be used by goodtables. | 2.958306 | 3.041656 | 0.972597 |
message = ("Gene '{value}' in column {col} and row {row} does not "
"appear in the metabolic model.")
for column in columns:
if "gene" in column['header'] and column['value'] not in genes:
message = message.format(
value=column['value'],
ro... | def gene_id_check(genes, errors, columns, row_number) | Validate gene identifiers against a known set.
Parameters
----------
genes : set
The known set of gene identifiers.
errors :
Passed by goodtables.
columns :
Passed by goodtables.
row_number :
Passed by goodtables. | 3.435549 | 3.671214 | 0.935807 |
message = ("Reaction '{value}' in column {col} and row {row} does not "
"appear in the metabolic model.")
for column in columns:
if "reaction" in column['header'] and column['value'] not in reactions:
message = message.format(
value=column['value'],
... | def reaction_id_check(reactions, errors, columns, row_number) | Validate reactions identifiers against a known set.
Parameters
----------
reactions : set
The known set of reaction identifiers.
errors :
Passed by goodtables.
columns :
Passed by goodtables.
row_number :
Passed by goodtables. | 3.28859 | 3.501697 | 0.939142 |
message = ("Metabolite '{value}' in column {col} and row {row} does not "
"appear in the metabolic model.")
for column in columns:
if "metabolite" in column['header'] and \
column['value'] not in metabolites:
message = message.format(
value... | def metabolite_id_check(metabolites, errors, columns, row_number) | Validate metabolite identifiers against a known set.
Parameters
----------
metabolites : set
The known set of metabolite identifiers.
errors :
Passed by goodtables.
columns :
Passed by goodtables.
row_number :
Passed by goodtables. | 3.041015 | 3.28348 | 0.926156 |
def is_verbose(arg):
return (arg.startswith("--verbosity") or
arg.startswith("-v") or arg.startswith("--verbose") or
arg.startswith("-q") or arg.startswith("--quiet"))
if ignore_git:
repo = None
else:
callbacks.git_installed()
repo = call... | def run(model, collect, filename, location, ignore_git, pytest_args, exclusive,
skip, solver, experimental, custom_tests, deployment,
skip_unchanged) | Run the test suite on a single model and collect results.
MODEL: Path to model file. Can also be supplied via the environment variable
MEMOTE_MODEL or configured in 'setup.cfg' or 'memote.ini'. | 4.394983 | 4.344015 | 1.011733 |
callbacks.git_installed()
if directory is None:
directory = os.getcwd()
cookiecutter("gh:opencobra/cookiecutter-memote", output_dir=directory,
replay=replay) | def new(directory, replay) | Create a suitable model repository structure from a template.
By using a cookiecutter template, memote will ask you a couple of questions
and set up a new directory structure that will make your life easier. The
new directory will be placed in the current directory or respect the given
--directory opti... | 15.899298 | 10.347894 | 1.536477 |
callbacks.git_installed()
try:
repo = git.Repo()
except git.InvalidGitRepositoryError:
LOGGER.critical(
"'memote online' requires a git repository in order to follow "
"the current branch's commit history.")
sys.exit(1)
if note == "memote-ci access":
... | def online(note, github_repository, github_username) | Upload the repository to GitHub and enable testing on Travis CI. | 4.944616 | 4.911711 | 1.006699 |
target_file = os.path.abspath(
join("tests", "data", "memote-mock-repo.tar.gz")
)
temp_dir = mkdtemp(prefix='tmp_mock')
previous_wd = os.getcwd()
try:
LOGGER.info("Cloning repository.")
os.chdir(temp_dir)
check_output(
['git', 'clone',
'h... | def update_mock_repo() | Clone and gzip the memote-mock-repo used for CLI and integration tests.
The repo is hosted at
'https://github.com/ChristianLieven/memote-mock-repo.git' and maintained
separately from | 3.026643 | 2.748151 | 1.101338 |
return sum(-coef * met.formula_weight
for (met, coef) in iteritems(reaction.metabolites)) / 1000.0 | def sum_biomass_weight(reaction) | Compute the sum of all reaction compounds.
This function expects all metabolites of the biomass reaction to have
formula information assigned.
Parameters
----------
reaction : cobra.core.reaction.Reaction
The biomass reaction of the model under investigation.
Returns
-------
f... | 8.564077 | 8.341884 | 1.026636 |
id_of_main_compartment = helpers.find_compartment_id_in_model(model, 'c')
gam_reactants = set()
try:
gam_reactants.update([
helpers.find_met_in_model(
model, "MNXM3", id_of_main_compartment)[0]])
except RuntimeError:
pass
try:
gam_reactants.up... | def find_biomass_precursors(model, reaction) | Return a list of all biomass precursors excluding ATP and H2O.
Parameters
----------
reaction : cobra.core.reaction.Reaction
The biomass reaction of the model under investigation.
model : cobra.Model
The metabolic model under investigation.
Returns
-------
list
Meta... | 2.968097 | 3.203453 | 0.926531 |
LOGGER.debug("Finding blocked biomass precursors")
precursors = find_biomass_precursors(model, reaction)
blocked_precursors = list()
_, ub = helpers.find_bounds(model)
for precursor in precursors:
with model:
dm_rxn = model.add_boundary(
precursor,
... | def find_blocked_biomass_precursors(reaction, model) | Return a list of all biomass precursors that cannot be produced.
Parameters
----------
reaction : cobra.core.reaction.Reaction
The biomass reaction of the model under investigation.
model : cobra.Model
The metabolic model under investigation.
Returns
-------
list
Me... | 4.001544 | 4.068504 | 0.983542 |
id_of_main_compartment = helpers.find_compartment_id_in_model(model, 'c')
try:
left = {
helpers.find_met_in_model(
model, "MNXM3", id_of_main_compartment)[0],
helpers.find_met_in_model(
model, "MNXM2", id_of_main_compartment)[0]
}
... | def gam_in_biomass(model, reaction) | Return boolean if biomass reaction includes growth-associated maintenance.
Parameters
----------
model : cobra.Model
The metabolic model under investigation.
reaction : cobra.core.reaction.Reaction
The biomass reaction of the model under investigation.
Returns
-------
boole... | 2.1939 | 2.270544 | 0.966244 |
biomass_rxns = set(helpers.find_biomass_reaction(model))
tra_bou_bio_rxns = helpers.find_interchange_biomass_reactions(
model, biomass_rxns)
try:
precursors = find_biomass_precursors(model, reaction)
main_comp = helpers.find_compartment_id_in_model(model, 'c')
ext_space ... | def find_direct_metabolites(model, reaction, tolerance=1E-06) | Return list of possible direct biomass precursor metabolites.
The term direct metabolites describes metabolites that are involved only
in either transport and/or boundary reactions, AND the biomass reaction(s),
but not in any purely metabolic reactions.
Parameters
----------
model : cobra.Mode... | 3.305156 | 3.280117 | 1.007633 |
for met in candidates:
is_internal = met.compartment != extra
for rxn in met.reactions:
if rxn in biomass_reactions:
continue
# Internal metabolites can not be false positives.
if is_internal:
metabolite_fluxes[met] += abs(reac... | def detect_false_positive_direct_metabolites(
candidates, biomass_reactions, cytosol, extra, reaction_fluxes,
metabolite_fluxes) | Weed out false positive direct metabolites.
False positives exists in the extracellular
compartment with flux from the cytosolic compartment and are part of the
biomass reaction(s). It sums fluxes positively or negatively depending
on if direct metabolites in the extracellular compartment are defined a... | 2.131153 | 2.088913 | 1.020221 |
if len(reaction.metabolites) >= 16:
return [reaction]
id_of_main_compartment = helpers.find_compartment_id_in_model(model,
'c')
gam_mets = ["MNXM3", "MNXM2", "MNXM7", "MNXM1", 'MNXM9']
try:
gam = set([helpers.find_me... | def bundle_biomass_components(model, reaction) | Return bundle biomass component reactions if it is not one lumped reaction.
There are two basic ways of specifying the biomass composition. The most
common is a single lumped reaction containing all biomass precursors.
Alternatively, the biomass equation can be split into several reactions
each focusin... | 3.768749 | 3.740791 | 1.007474 |
u
main_comp = helpers.find_compartment_id_in_model(model, 'c')
biomass_eq = bundle_biomass_components(model, reaction)
pooled_precursors = set(
[met for rxn in biomass_eq for met in rxn.metabolites])
missing_essential_precursors = []
for mnx_id in ESSENTIAL_PRECURSOR_IDS:
try:
... | def essential_precursors_not_in_biomass(model, reaction) | u"""
Return a list of essential precursors missing from the biomass reaction.
There are universal components of life that make up the biomass of all
known organisms. These include all proteinogenic amino acids, deoxy- and
ribonucleotides, water and a range of metabolic cofactors.
Parameters
--... | 3.510962 | 3.629886 | 0.967238 |
if value is None:
return
config = ExperimentConfiguration(value)
config.validate()
return config | def validate_experimental(context, param, value) | Load and validate an experimental data configuration. | 6.999813 | 5.226177 | 1.339375 |
try:
repo = git.Repo()
except git.InvalidGitRepositoryError:
LOGGER.warning(
"We highly recommend keeping your model in a git repository."
" It allows you to track changes and to easily collaborate with"
" others via online platforms such as https://githu... | def probe_git() | Return a git repository instance if it exists. | 7.02956 | 6.856704 | 1.02521 |
LOGGER.info("Checking `git` installation.")
try:
check_output(['git', '--version'])
except CalledProcessError as e:
LOGGER.critical(
"The execution of memote was interrupted since no installation of "
"`git` could be detected. Please install git to use "
... | def git_installed() | Interrupt execution of memote if `git` has not been installed. | 4.083365 | 3.336132 | 1.223982 |
if commit is None:
commit = self._repo.head.commit
else:
commit = self._repo.commit(commit)
return GitInfo(
hexsha=commit.hexsha,
author=commit.author.name,
email=commit.author.email,
authored_on=commit.authored_dat... | def record_git_info(self, commit=None) | Record git meta information.
Parameters
----------
commit : str, optional
Unique hexsha of the desired commit.
Returns
-------
GitInfo
Git commit meta information. | 2.391593 | 2.441962 | 0.979374 |
meta["hexsha"] = git_info.hexsha
meta["author"] = git_info.author
meta["email"] = git_info.email
meta["authored_on"] = git_info.authored_on.isoformat(" ") | def add_git(meta, git_info) | Enrich the result meta information with commit data. | 2.965714 | 2.785661 | 1.064636 |
git_info = self.record_git_info(commit)
self.add_git(result.meta, git_info)
filename = self.get_filename(git_info)
super(RepoResultManager, self).store(
result, filename=filename, **kwargs) | def store(self, result, commit=None, **kwargs) | Store a result in a JSON file attaching git meta information.
Parameters
----------
result : memote.MemoteResult
The dictionary structure of results.
commit : str, optional
Unique hexsha of the desired commit.
kwargs :
Passed to parent functio... | 5.823769 | 5.594126 | 1.041051 |
git_info = self.record_git_info(commit)
LOGGER.debug("Loading the result for commit '%s'.", git_info.hexsha)
filename = self.get_filename(git_info)
LOGGER.debug("Loading the result '%s'.", filename)
result = super(RepoResultManager, self).load(filename)
self.add_... | def load(self, commit=None) | Load a result from the storage directory. | 4.765604 | 4.197292 | 1.1354 |
# Default value means we do not resolve a model file.
if filename == "default":
return filename
filename = expanduser(filename)
if isabs(filename):
return filename
else:
return join(os.getcwd(), filename) | def normalize(filename) | Return an absolute path of the given file name. | 6.615039 | 6.117683 | 1.081298 |
if dtype_conversion is None:
dtype_conversion = {"growth": str}
super(GrowthExperiment, self).load(dtype_conversion=dtype_conversion)
self.data["growth"] = self.data["growth"].isin(self.TRUTHY) | def load(self, dtype_conversion=None) | Load the data table and corresponding validation schema.
Parameters
----------
dtype_conversion : dict
Column names as keys and corresponding type for loading the data.
Please take a look at the `pandas documentation
<https://pandas.pydata.org/pandas-docs/sta... | 5.392359 | 6.789356 | 0.794237 |
with model:
if self.medium is not None:
self.medium.apply(model)
if self.objective is not None:
model.objective = self.objective
model.add_cons_vars(self.constraints)
threshold *= model.slim_optimize()
growth = ... | def evaluate(self, model, threshold=0.1) | Evaluate in silico growth rates. | 4.210703 | 3.790946 | 1.110726 |
try:
with BytesIO() as stream:
with GzipFile(fileobj=stream, mode="wb") as file_handle:
file_handle.write(
jsonify(value, pretty=False).encode("utf-8")
)
output = stream.getvalue()
re... | def process_bind_param(self, value, dialect) | Convert the value to a JSON encoded string before storing it. | 4.261343 | 4.144296 | 1.028243 |
if value is not None:
with BytesIO(value) as stream:
with GzipFile(fileobj=stream, mode="rb") as file_handle:
value = json.loads(file_handle.read().decode("utf-8"))
return value | def process_result_value(self, value, dialect) | Convert a JSON encoded string to a dictionary structure. | 3.078033 | 3.0038 | 1.024713 |
min_score = zxcvbn_min_score()
message_title = _('Warning')
message_body = _(
'This password would take '
'<em class="password_strength_time"></em> to crack.')
strength_markup =
strength_markup = strength_markup.format(
title=messag... | def render(self, name, value, attrs=None, **kwargs) | Widget render method. | 4.652678 | 4.617713 | 1.007572 |
if self.confirm_with:
self.attrs['data-confirm-with'] = 'id_%s' % self.confirm_with
confirmation_markup = % (_('Warning'), _("Your passwords don't match."))
try:
self.attrs['class'] = '%s password_confirmation'.strip() % self.attrs['class'] # noqa
exc... | def render(self, name, value, attrs=None, **kwargs) | Widget render method. | 4.585577 | 4.540293 | 1.009974 |
user_inputs = []
if user is not None:
for attribute in self.user_attributes:
if hasattr(user, attribute):
user_inputs.append(getattr(user, attribute))
results = zxcvbn(password, user_inputs=user_inputs)
if results.get('score', 0) ... | def validate(self, password, user=None) | Validate method, run zxcvbn and check score. | 3.236324 | 2.689788 | 1.203189 |
parser = MyHTMLParser()
parser.feed(html)
if parser.is_code:
return ('code', parser.data.strip())
elif parser.is_math:
return ('math', parser.data.strip())
else:
return '', '' | def _get_html_contents(html) | Process a HTML block and detects whether it is a code block,
a math block, or a regular HTML block. | 3.600249 | 2.945419 | 1.222321 |
if isinstance(s, string_types):
try:
return op.exists(s)
except (OSError, ValueError):
return False
else:
return False | def _is_path(s) | Return whether an object is a path. | 3.516944 | 3.199952 | 1.099062 |
if cls._instance is None:
# Discover the formats and register them with a new singleton.
cls._instance = cls().register_entrypoints()
return cls._instance | def format_manager(cls) | Return the instance singleton, creating if necessary | 12.310637 | 9.36516 | 1.314514 |
for spec in iter_entry_points(self.entry_point_group):
format_properties = {"name": spec.name}
try:
format_properties.update(spec.load())
except (DistributionNotFound, ImportError) as err:
self.log.info(
"ipymd form... | def register_entrypoints(self) | Look through the `setup_tools` `entry_points` and load all of
the formats. | 4.450307 | 3.879689 | 1.147078 |
formats = [name
for name, format in self._formats.items()
if format.get('file_extension', None) == extension]
if len(formats) == 0:
return None
elif len(formats) == 2:
raise RuntimeError("Several extensions are registered wit... | def format_from_extension(self, extension) | Find a format from its extension. | 4.002352 | 3.747113 | 1.068116 |
if name is None:
name = self.format_from_extension(op.splitext(file)[1])
file_format = self.file_type(name)
if file_format == 'text':
return _read_text(file)
elif file_format == 'json':
return _read_json(file)
else:
load_fu... | def load(self, file, name=None) | Load a file. The format name can be specified explicitly or
inferred from the file extension. | 3.733403 | 3.516338 | 1.061731 |
if name is None:
name = self.format_from_extension(op.splitext(file)[1])
file_format = self.file_type(name)
if file_format == 'text':
_write_text(file, contents)
elif file_format == 'json':
_write_json(file, contents)
else:
... | def save(self, file, contents, name=None, overwrite=False) | Save contents into a file. The format name can be specified
explicitly or inferred from the file extension. | 3.60517 | 3.398582 | 1.060786 |
self._check_format(name)
return self._formats[name]['reader'](*args, **kwargs) | def create_reader(self, name, *args, **kwargs) | Create a new reader instance for a given format. | 5.228182 | 3.842536 | 1.360607 |
self._check_format(name)
return self._formats[name]['writer'](*args, **kwargs) | def create_writer(self, name, *args, **kwargs) | Create a new writer instance for a given format. | 5.919521 | 4.110335 | 1.440155 |
# Load the file if 'contents_or_path' is a path.
if _is_path(contents_or_path):
contents = self.load(contents_or_path, from_)
else:
contents = contents_or_path
if from_kwargs is None:
from_kwargs = {}
if to_kwargs is None:
... | def convert(self,
contents_or_path,
from_=None,
to=None,
reader=None,
writer=None,
from_kwargs=None,
to_kwargs=None,
) | Convert contents between supported formats.
Parameters
----------
contents : str
The contents to convert from.
from_ : str or None
The name of the source format. If None, this is the
ipymd_cells format.
to : str or None
The name o... | 2.61288 | 2.462319 | 1.061146 |
if not self.verbose_metadata:
default_kernel_name = (self.default_kernel_name or
self._km.kernel_name)
if (meta.get("kernelspec", {})
.get("name", None) == default_kernel_name):
del meta["kernelspec"]
... | def clean_meta(self, meta) | Removes unwanted metadata
Parameters
----------
meta : dict
Notebook metadata. | 5.08659 | 4.68349 | 1.086068 |
for k, v in DEFAULT_CELL_METADATA.items():
if meta.get(k, None) == v:
meta.pop(k, None)
return meta | def clean_cell_meta(self, meta) | Remove cell metadata that matches the default cell metadata. | 3.178318 | 2.45977 | 1.29212 |
if not regex.startswith('^'):
regex = '^' + regex
reg = re.compile(regex)
return reg.match(line) | def _starts_with_regex(line, regex) | Return whether a line starts with a regex or not. | 2.747197 | 2.386703 | 1.151043 |
if prompt is None:
prompt = 'python'
if prompt == 'python':
prompt = PythonPromptManager
elif prompt == 'ipython':
prompt = IPythonPromptManager
# Instanciate the class.
if isinstance(prompt, BasePromptManager):
return prompt
else:
return prompt() | def create_prompt(prompt) | Create a prompt manager.
Parameters
----------
prompt : str or class driving from BasePromptManager
The prompt name ('python' or 'ipython') or a custom PromptManager
class. | 4.438123 | 3.054996 | 1.452742 |
lines = _to_lines(text)
i = 0
for line in lines:
if _starts_with_regex(line, self.input_prompt_regex):
i += 1
else:
break
return lines[:i], lines[i:] | def split_input_output(self, text) | Split code into input lines and output lines, according to the
input and output prompt templates. | 3.727147 | 3.321986 | 1.121963 |
path = path.strip('/')
# File extension of the chosen format.
file_extension = format_manager().file_extension(self.format)
if not self.exists(path):
raise web.HTTPError(404, u'No such file or directory: %s' % path)
os_path = self._get_os_path(path)
... | def get(self, path, content=True, type=None, format=None) | Takes a path for an entity and returns its model
Parameters
----------
path : str
the API path that describes the relative path for the target
content : bool
Whether to include the contents in the reply
type : str, optional
The requested type -... | 2.916566 | 2.888348 | 1.00977 |
with self.open(os_path, 'r', encoding='utf-8') as f:
try:
# NEW
file_ext = _file_extension(os_path)
if file_ext == '.ipynb':
return nbformat.read(f, as_version=as_version)
else:
return c... | def _read_notebook(self, os_path, as_version=4) | Read a notebook from an os path. | 3.833518 | 3.703941 | 1.034984 |
path = path.strip('/')
if 'type' not in model:
raise web.HTTPError(400, u'No file type provided')
if 'content' not in model and model['type'] != 'directory':
raise web.HTTPError(400, u'No file content provided')
self.run_pre_save_hook(model=model, path=... | def save(self, model, path='') | Save the file model and return the model with no content. | 2.902827 | 2.850232 | 1.018453 |
python = _preprocess(python)
if not python:
return []
lexer = PythonSplitLexer()
lexer.read(python)
return lexer.chunks | def _split_python(python) | Split Python source into chunks.
Chunks are separated by at least two return lines. The break must not
be followed by a space. Also, long Python strings spanning several lines
are not splitted. | 9.342575 | 8.831892 | 1.057823 |
lines = source.splitlines()
if all(line.startswith('# ') for line in lines):
# The chunk is a Markdown *unless* it is commented Python code.
source = '\n'.join(line[2:] for line in lines
if not line[2:].startswith('#')) # skip headers
if not source:
... | def _is_chunk_markdown(source) | Return whether a chunk contains Markdown contents. | 5.51357 | 5.24234 | 1.051738 |
source = '\n'.join('# ' + line.rstrip()
for line in source.splitlines())
return source | def _add_hash(source) | Add a leading hash '#' at the beginning of every line in the source. | 5.83799 | 4.050022 | 1.441471 |
lines = source.splitlines()
# Filters is a list of 'hN' strings where 1 <= N <= 6.
headers = [_replace_header_filter(filter) for filter in filters]
lines = [line for line in lines if line.startswith(tuple(headers))]
return '\n'.join(lines) | def _filter_markdown(source, filters) | Only keep some Markdown headers from a Markdown string. | 5.50633 | 4.545733 | 1.211318 |
level = 1 if m.group(2) == '=' else 2
self.renderer.heading(m.group(1), level=level) | def parse_lheading(self, m) | Parse setext heading. | 3.8158 | 3.395778 | 1.12369 |
assert n >= 0
text = self._output.getvalue().rstrip('\n')
if not text:
return
self._output = StringIO()
self._output.write(text)
self._output.write('\n' * n)
text = self._output.getvalue()
assert text[-n-1] != '\n'
assert text[... | def ensure_newline(self, n) | Make sure there are 'n' line breaks at the end. | 2.914395 | 2.768422 | 1.052728 |
body = m.group('body')
is_notebook = m.group('sep_close') == '---'
if is_notebook:
# make it into a valid YAML object by stripping ---
body = body.strip()[:-3] + '...'
try:
if body:
return self._meta(yaml.safe_load(m.group('bo... | def _meta_from_regex(self, m) | Extract and parse YAML metadata from a meta match
Notebook metadata must appear at the beginning of the file and follows
the Jekyll front-matter convention of dashed delimiters:
---
some: yaml
---
Cell metadata follows the YAML spec of dashes and periods
... | 6.308275 | 6.109488 | 1.032538 |
input, output = self._prompt.to_cell(source)
return {'cell_type': 'code',
'input': input,
'output': output} | def _code_cell(self, source) | Split the source into input and output. | 6.353022 | 4.734504 | 1.341856 |
text = re.sub(r'\r\n|\r', '\n', text)
text = text.replace('\t', ' ' * tab)
text = text.replace('\u00a0', ' ')
text = text.replace('\u2424', '\n')
pattern = re.compile(r'^ +$', re.M)
text = pattern.sub('', text)
text = _rstrip_lines(text)
return text | def _preprocess(text, tab=4) | Normalize a text. | 2.429376 | 2.388669 | 1.017042 |
diff = difflib.ndiff(text_0.splitlines(), text_1.splitlines())
return _diff_removed_lines(diff) | def _diff(text_0, text_1) | Return a diff between two strings. | 3.478276 | 3.232033 | 1.076188 |
with open(file, 'w') as f:
return json.dump(contents, f, indent=2, sort_keys=True) | def _write_json(file, contents) | Write a dict to a JSON file. | 2.325594 | 2.2584 | 1.029753 |
style = ListStyle(name='_numbered_list')
lls = ListLevelStyleNumber(level=1)
lls.setAttribute('displaylevels', 1)
lls.setAttribute('numsuffix', '. ')
lls.setAttribute('numformat', '1')
llp = ListLevelProperties()
llp.setAttribute('listlevelpositionandspacemode', 'label-alignment')
... | def _numbered_style() | Create a numbered list style. | 5.391142 | 5.143142 | 1.04822 |
if family == 'paragraph' and 'marginbottom' not in kwargs:
kwargs['marginbottom'] = '.5cm'
style = Style(name=name, family=family)
# Extract paragraph properties.
kwargs_par = {}
keys = sorted(kwargs.keys())
for k in keys:
if 'margin' in k:
kwargs_par[k] = kwargs... | def _create_style(name, family=None, **kwargs) | Helper function for creating a new style. | 3.617127 | 3.569848 | 1.013244 |
styles = {}
def _add_style(name, **kwargs):
styles[name] = _create_style(name, **kwargs)
_add_style('heading-1',
family='paragraph',
fontsize='24pt',
fontweight='bold',
)
_add_style('heading-2',
family='paragraph'... | def default_styles() | Generate default ODF styles. | 1.655593 | 1.612583 | 1.026671 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.