Skip to content

Commit

Permalink
feat: add to_frame method to solution
Browse files Browse the repository at this point in the history
  • Loading branch information
hredestig committed Apr 25, 2017
1 parent a46abc5 commit c920407
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cobra/core/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from numpy import empty, nan
from optlang.interface import OPTIMAL
from pandas import Series
from pandas import Series, DataFrame

from cobra.util.solver import check_solver_status

Expand Down Expand Up @@ -169,6 +169,11 @@ def y(self):
warn("use solution.reduced_costs.values() instead", DeprecationWarning)
return self.reduced_costs.values

def to_frame(self):
"""Return the fluxes and reduced costs as a data frame"""
return DataFrame({'fluxes': self.fluxes,
'reduced_costs': self.reduced_costs})


class LegacySolution(object):
"""
Expand Down
7 changes: 7 additions & 0 deletions cobra/test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy
import pytest
import pandas as pd
from sympy import S

import cobra.util.solver as su
Expand Down Expand Up @@ -733,6 +734,12 @@ def test_problem_properties(self, model):
assert "test_variable" not in model.variables.keys()
assert "test_constraint" not in model.variables.keys()

def test_solution_data_frame(self, model):
solution = model.optimize().to_frame()
assert isinstance(solution, pd.DataFrame)
assert 'fluxes' in solution
assert 'reduced_costs' in solution

def test_model_medium(self, model):
# Add a dummy 'malformed' import reaction
bad_import = Reaction('bad_import')
Expand Down
4 changes: 3 additions & 1 deletion release-notes/0.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ similar problem, we have completely refactored `cobra.Solution` so
that `model.optimize()` now returns a solution and it is the user's
responsibility to manage this object. `reaction.flux` gets its values
directly from the `model.problem`. To sugar the new solution class,
fluxes, reduced costs, and shadow prices are now pandas series!
fluxes, reduced costs, and shadow prices are now pandas series! Fluxes
and reduced costs can be returned as a data frame directlt with the
`to_frame` method.

## Sampling

Expand Down

0 comments on commit c920407

Please sign in to comment.