Skip to content

Commit

Permalink
Merge branch 'devel' of https://github.com/biosustain/cameo into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomas1234 committed Nov 9, 2020
2 parents a5d3894 + e2fdbf8 commit a522da2
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 317 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ matrix:
env: TOXENV=py36
- python: '3.7'
env: TOXENV=py37
- python: '3.8'
env: TOXENV=py38

branches:
only:
Expand Down
3 changes: 1 addition & 2 deletions cameo/core/strain_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from cameo.core.result import Result
from cameo.core.target import EnsembleTarget, Target
from cameo.visualization.plotting import plotter

from gnomic import Genotype

Expand Down Expand Up @@ -180,7 +179,7 @@ def __add__(self, other):
def _repr_html_(self):
return self.data_frame._repr_html_()

def plot(self, grid=None, width=None, height=None, title=None, *args, **kwargs):
def plot(self, plotter, grid=None, width=None, height=None, title=None, *args, **kwargs):
if title is None:
title = "Target frequency plot for %s result" % self.__method_name__

Expand Down
15 changes: 7 additions & 8 deletions cameo/flux_analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@
from cobra.util import fix_objective_as_constraint, get_context
from cobra.exceptions import OptimizationError
from numpy import trapz
from sympy import S
from optlang.interface import UNBOUNDED, OPTIMAL
from optlang.symbolics import Zero

import cameo
from cameo import config
from cameo.core.result import Result
from cameo.flux_analysis.util import remove_infeasible_cycles, fix_pfba_as_constraint
from cameo.parallel import SequentialView
from cameo.ui import notice
from cameo.util import partition, _BIOMASS_RE_
from cameo.core.utils import get_reaction_for
from cameo.visualization.plotting import plotter

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -203,8 +201,9 @@ def flux_variability_analysis(model, reactions=None, fraction_of_optimum=0., pfb
Returns
-------
pandas.DataFrame
Pandas DataFrame containing the results of the flux variability analysis.
FluxVariabilityResult
FluxVariabilityResult with DataFrame data_frame property containing the results of the flux variability
analysis.
"""
if view is None:
Expand Down Expand Up @@ -357,7 +356,7 @@ def _flux_variability_analysis(model, reactions=None):
fva_sol = OrderedDict()
lb_flags = dict()
with model:
model.objective = S.Zero
model.objective = Zero

model.objective.direction = 'min'
for reaction in reactions:
Expand Down Expand Up @@ -760,7 +759,7 @@ def __init__(self, phase_plane, variable_ids, objective,
def data_frame(self):
return pandas.DataFrame(self._phase_plane)

def plot(self, grid=None, width=None, height=None, title=None, axis_font_size=None, palette=None,
def plot(self, plotter, grid=None, width=None, height=None, title=None, axis_font_size=None, palette=None,
points=None, points_colors=None, estimate='flux', **kwargs):
"""plot phenotypic phase plane result
Expand Down Expand Up @@ -882,7 +881,7 @@ def __init__(self, data_frame, *args, **kwargs):
def data_frame(self):
return self._data_frame

def plot(self, index=None, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
def plot(self, plotter, index=None, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
if index is None:
index = self.data_frame.index[0:10]
fva_result = self.data_frame.loc[index]
Expand Down
4 changes: 2 additions & 2 deletions cameo/flux_analysis/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@

add = Add._from_args
mul = Mul._from_args
NegativeOne = sympy.singleton.S.NegativeOne
One = sympy.singleton.S.One
NegativeOne = sympy.S.NegativeOne
One = sympy.S.One
FloatOne = sympy.Float(1)
RealNumber = sympy.RealNumber

Expand Down
5 changes: 2 additions & 3 deletions cameo/strain_design/deterministic/flux_variability_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from cobra import Reaction, Metabolite
from cobra.util import fix_objective_as_constraint

from cameo.visualization.plotting import plotter
from cameo import config

from cameo.ui import notice
Expand Down Expand Up @@ -633,7 +632,7 @@ def plot(self, index=None, variables=None, grid=None, width=None, height=None, t
else:
self._plot_production_envelope(title=title, grid=grid, width=width, height=height)

def _plot_flux_variability_analysis(self, index, variables=None, title=None,
def _plot_flux_variability_analysis(self, plotter, index, variables=None, title=None,
width=None, height=None, palette=None, grid=None):
if variables is None:
variables = self.reference_fva.index[0:10]
Expand Down Expand Up @@ -1008,7 +1007,7 @@ class FSEOFResult(StrainDesignMethodResult):

__method_name__ = "FSEOF"

def plot(self, grid=None, width=None, height=None, title=None, *args, **kwargs):
def plot(self, plotter, grid=None, width=None, height=None, title=None, *args, **kwargs):
if title is None:
title = "FSEOF fluxes"

Expand Down
3 changes: 1 addition & 2 deletions cameo/strain_design/deterministic/linear_programming.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from cameo.flux_analysis.simulation import fba
from cameo.flux_analysis.structural import find_coupled_reactions_nullspace
from cameo.util import reduce_reaction_set, decompose_reaction_groups
from cameo.visualization.plotting import plotter

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -385,7 +384,7 @@ def display_on_map(self, index=0, map_name=None, palette="YlGnBu"):
fluxes = fba(self._model)
fluxes.display_on_map(map_name=map_name, palette=palette)

def plot(self, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
def plot(self, plotter, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
wt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass.id])
with self._model:
for ko in self.data_frame.loc[index, "reactions"]:
Expand Down
5 changes: 2 additions & 3 deletions cameo/strain_design/heuristic/evolutionary_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from cameo.strain_design.heuristic.evolutionary.processing import process_reaction_knockout_solution, \
process_gene_knockout_solution, process_reaction_swap_solution
from cameo.util import TimeMachine
from cameo.visualization.plotting import plotter
from cameo.core.utils import get_reaction_for

__all__ = ["OptGene"]
Expand Down Expand Up @@ -294,7 +293,7 @@ def display_on_map(self, index=0, map_name=None, palette="YlGnBu"):
fluxes = self._simulation_method(self._model, **self._simulation_kwargs)
fluxes.display_on_map(map_name=map_name, palette=palette)

def plot(self, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
def plot(self, plotter, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
wt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass])
with self._model:
for ko in self.data_frame.loc[index, "reactions"]:
Expand Down Expand Up @@ -483,7 +482,7 @@ def display_on_map(self, index=0, map_name=None, palette="YlGnBu"):
fluxes = self._simulation_method(self._model, **self._simulation_kwargs)
fluxes.display_on_map(map_name=map_name, palette=palette)

def plot(self, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
def plot(self, plotter, index=0, grid=None, width=None, height=None, title=None, palette=None, **kwargs):
wt_production = phenotypic_phase_plane(self._model, objective=self._target, variables=[self._biomass])
with self._model:
for ko in self.data_frame.loc[index, "reactions"]:
Expand Down
5 changes: 3 additions & 2 deletions cameo/strain_design/pathway_prediction/pathway_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
from cameo.data import metanetx
from cameo.strain_design.pathway_prediction import util
from cameo.util import TimeMachine
from cameo.visualization.plotting import plotter

__all__ = ['PathwayPredictor']

Expand Down Expand Up @@ -172,7 +171,9 @@ def plot(self, grid=None, width=None, height=None, title=None):
# TODO: small pathway visualizations would be great.
raise NotImplementedError

def plot_production_envelopes(self, model, objective=None, title=None):
def plot_production_envelopes(
self, plotter, model, objective=None, title=None
):
rows = int(ceil(len(self.pathways) / 2.0))
title = "Production envelops for %s" % self.pathways[0].product.name if title is None else title
grid = plotter.grid(n_rows=rows, title=title)
Expand Down
65 changes: 0 additions & 65 deletions cameo/visualization/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import

from warnings import warn

from cameo.visualization.plotting.abstract import AbstractPlotter

__all__ = ["plotter"]

_engine = None
_engines = {}

try:
from cameo.visualization.plotting.with_bokeh import BokehPlotter

_engine = BokehPlotter() if _engine is None else _engine
_engines["bokeh"] = BokehPlotter
except ImportError:
pass

try:
from cameo.visualization.plotting.with_plotly import PlotlyPlotter

_engine = PlotlyPlotter(mode='offline') if _engine is None else _engine
_engines["plotly"] = PlotlyPlotter
except ImportError:
pass

try:
from cameo.visualization.plotting.with_plotly import GGPlotPlotter

_engine = GGPlotPlotter() if _engine is None else _engine
_engines["ggplot"] = GGPlotPlotter
except ImportError:
pass

if _engine is None:
warn("Cannot import any plotting library. Please install one of 'plotly', 'bokeh' or 'ggplot' if you want to"
" use any plotting function.")
_engine = AbstractPlotter()


class _plotting:
def __init__(self, engine):
self.__dict__['_engine'] = engine

def __getattr__(self, item):
if item not in ["_engine", "engine"]:
return getattr(self.__dict__['_engine'], item)
else:
return self.__dict__['_engine']

def __setattr__(self, key, item):
if key not in ["_engine", "engine"]:
raise KeyError(key)
else:
if isinstance(item, str):
item = _engines[item]()

if not isinstance(item, AbstractPlotter):
raise AssertionError("Invalid engine %s" % item)

self.__dict__['_engine'] = item


plotter = _plotting(_engine)
Loading

0 comments on commit a522da2

Please sign in to comment.