opencobra / cobrapy

COBRApy is a package for constraint-based modeling of metabolic networks.
http://opencobra.github.io/cobrapy/
GNU General Public License v2.0
467 stars 218 forks source link

[Feature] Verbosity Option in `read_sbml_model` or similar. #1405

Open ggmirandac opened 2 months ago

ggmirandac commented 2 months ago

Checklist

Problem

I been trying to work on some metagenome analysis with cobrapy. And to analyze some of the features in the metabolic network Cobrapy is often recommended. I store all my models in a sbml file and therefore I must read them. The issue is that when I am reading them there is A LOT of outputs in the terminal that at the end reduce the speed of the overall code (per my knowledge).

So, what I would think is that the read_sbml_model could have a verbose option which enables to ignore outputs that are not Errors

Solution

One solution that I would think of is condition the logs to the terminal by defining, for example: if verbose == 1: LOGGER.warning # for example

to ignore logger warnings, that often doesn't affect the overall run if the user knows before hand the model.

Alternatives

I considered using os.devnull with open(os.devnull, 'w') as devnull, open(log_file, 'a') as log_f: with redirect_stdout(devnull), redirect_stderr(devnull): But I think would be more elegant if the solution was in the COBRApy.

Anything else?

No response

cdiener commented 2 months ago

There was a discussion of adding a default verbosity to the Configuration also for progress bars that may become a feature in the future.

There is no need to redirect standard output though, since we use normal Python logging.

So a simple:

import logging

logging.basicConfig(level=logging.ERROR)

In the beginning of your script should be enough.

On a side note, SBML parsing will not raise warnings until there are issues with the file. So, unless you need to use some legacy models that you know will be parsed correctly, seeing a lot of warnings will usually indicate potential issues down the line or will require manual fixes to the models.