Skip to content

Commit

Permalink
fix SparseDataFrame check
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgigante committed Feb 4, 2020
1 parent 81c711f commit 5d1869d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 9 additions & 5 deletions graphtools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,27 @@ def __init__(
self._check_data(data)
n_pca, rank_threshold = self._parse_n_pca_threshold(data, n_pca, rank_threshold)
try:
pd
except NameError:
# pandas not installed
pass
else:
if utils.is_SparseDataFrame(data):
data = data.to_coo()
elif isinstance(data, pd.DataFrame):
try:
data = data.sparse.to_coo()
except AttributeError:
data = np.array(data)
except NameError:
# pandas not installed
pass

try:
if isinstance(data, anndata.AnnData):
data = data.X
anndata
except NameError:
# anndata not installed
pass
else:
if isinstance(data, anndata.AnnData):
data = data.X
self.data = data
self.n_pca = n_pca
self.rank_threshold = rank_threshold
Expand Down
17 changes: 15 additions & 2 deletions graphtools/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import numpy as np
from scipy import sparse
import numbers
import warnings

try:
import pandas as pd
except ImportError:
# pandas not installed
pass


def if_sparse(sparse_func, dense_func, *args, **kwargs):
Expand Down Expand Up @@ -82,10 +89,16 @@ def to_array(X):


def is_SparseDataFrame(X):
with warnings.catchwarnings():
try:
pd
except NameError:
# pandas not installed
return False
with warnings.catch_warnings():
warnings.filterwarnings(
FutureWarning,
"ignore",
"The SparseDataFrame class is removed from pandas. Accessing it from the top-level namespace will also be removed in the next version",
FutureWarning,
)
try:
return isinstance(X, pd.SparseDataFrame)
Expand Down

0 comments on commit 5d1869d

Please sign in to comment.