Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add to_dataframe to dictlist #1280

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New features

* View number of genes in model notebook representation.
* Add progress bar to `flux_variability_analysis`.

## Fixes

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ install_requires =
rich >=8.0
ruamel.yaml ~=0.16
swiglpk
tqdm ~=4.0
tests_require =
tox
packages = find:
Expand Down
26 changes: 26 additions & 0 deletions src/cobra/core/dictlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)

import numpy as np
import pandas as pd

from .object import Object

Expand Down Expand Up @@ -542,3 +543,28 @@ def __dir__(self) -> list:
attributes.append("_dict")
attributes.extend(self._dict.keys())
return attributes

def to_df(self):
"""Convert to a pandas dataframe."""
item = None
columns = []

if self:
item = self[0]
columns = [col for col in item._DF_ATTRS if col != "id"]

data = []
ids = []

for item in self:
ids.append(item.id)
data.append([getattr(item, attr) for attr in columns])

df = pd.DataFrame(columns=columns, data=data, index=ids)

return df

def _repr_html_(self):
"""Display as HTML."""
df = self.to_df()
return df._repr_html_()
2 changes: 2 additions & 0 deletions src/cobra/core/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Formula(Object):
A legal formula string contains only letters and numbers.
"""

_DF_ATTRS = ["formula"]

def __init__(self, formula: Optional[str] = None, **kwargs) -> None:
"""Initialize a formula.

Expand Down
2 changes: 2 additions & 0 deletions src/cobra/core/gene.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ class Gene(Species):
used.
"""

_DF_ATTRS = Species._DF_ATTRS

# noinspection PyShadowingBuiltins
def __init__(self, id: str = None, name: str = "", functional: bool = True) -> None:
"""Initialize a gene.
Expand Down
2 changes: 2 additions & 0 deletions src/cobra/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Group(Object):
or member is involved in a disease phenotype).
"""

_DF_ATTRS = ["id", "name", "kind"]

KIND_TYPES = ("collection", "classification", "partonomy")

def __init__(
Expand Down
2 changes: 2 additions & 0 deletions src/cobra/core/metabolite.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Metabolite(Species):
Compartment of the metabolite.
"""

_DF_ATTRS = Species._DF_ATTRS + ["formula", "compartment", "charge"]

# noinspection PyShadowingBuiltins
def __init__(
self,
Expand Down
1 change: 1 addition & 0 deletions src/cobra/core/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class Object:
"""Defines common behavior of object in cobra.core."""
_DF_ATTRS = ["id", "name", "notes", "annotation"]

def __init__(self, id: Optional[str] = None, name: str = "") -> None:
"""Initialize a simple object with an identifier.
Expand Down
7 changes: 7 additions & 0 deletions src/cobra/core/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class Reaction(Object):
Further keyword arguments are passed on to the parent class.
"""

_DF_ATTRS = Object._DF_ATTRS + [
"subsystem",
"gene_reaction_rule",
"lower_bound",
"upper_bound",
]

# noinspection PyShadowingBuiltins
def __init__(
self,
Expand Down
2 changes: 2 additions & 0 deletions src/cobra/core/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Species(Object):
A human readable name.
"""

_DF_ATTRS = ["id", "name"]

# noinspection PyShadowingBuiltins
def __init__(
self, id: Optional[str] = None, name: Optional[str] = None, **kwargs
Expand Down
19 changes: 16 additions & 3 deletions src/cobra/flux_analysis/variability.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from .loopless import loopless_fva_iter
from .parsimonious import add_pfba

# from tqdm.auto import tqdm
from rich.progress import track


if TYPE_CHECKING:
from cobra import Gene, Model, Reaction
Expand Down Expand Up @@ -236,18 +239,28 @@ def flux_variability_analysis(
# objective direction for all reactions. This creates a
# slight overhead but seems the most clean.
chunk_size = len(reaction_ids) // processes
description = "Performing FVA for %s fluxes" % what
with ProcessPool(
processes,
initializer=_init_worker,
initargs=(model, loopless, what[:3]),
) as pool:
for rxn_id, value in pool.imap_unordered(
_fva_step, reaction_ids, chunksize=chunk_size
for rxn_id, value in track(
pool.imap_unordered(
_fva_step, reaction_ids, chunksize=chunk_size
),
total=num_reactions,
description=description
):
fva_result.at[rxn_id, what] = value
else:
_init_worker(model, loopless, what[:3])
for rxn_id, value in map(_fva_step, reaction_ids):
for rxn_id, value in track(
map(_fva_step, reaction_ids),
total=num_reactions,
description=description
):
fva_result.at[rxn_id, what] = value
fva_result.at[rxn_id, what] = value

return fva_result[["minimum", "maximum"]]
Expand Down
36 changes: 36 additions & 0 deletions tests/test_core/test_dictlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pickle import HIGHEST_PROTOCOL, dumps, loads
from typing import Tuple

import pandas as pd
import pytest

from cobra.core import DictList, Object
Expand Down Expand Up @@ -468,3 +469,38 @@ def test_union(dict_list: Tuple[Object, DictList]) -> None:
# Add only 1 element
assert len(test_list) == 2
assert test_list.index("test2") == 1

def test_to_df(dict_list: Tuple[Object, DictList]) -> None:
"""Test to_df for dictlist.

Parameters
----------
dict_list : tuple
The fixture for filled dictlist.

"""
_, test_list = dict_list
test_list.name = "foo"
df = test_list.to_df()
assert isinstance(df, pd.DataFrame)
assert len(df) == 1
assert "id" in df.columns
assert "test1" in df["id"].values
assert "name" in df.columns
assert "foo" in df["name"].values

def test__repr_html(dict_list: Tuple[Object, DictList]) -> None:
"""Test _repr_html_ for dictlist.

Parameters
----------
dict_list : tuple
The fixture for filled dictlist.

"""
_, test_list = dict_list
test_list.name = "foo"
html = test_list._repr_html_()
assert isinstance(html, str)
assert "test1" in html
assert "foo" in html