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

fix: make vstack correctly handle stacking dataframes and series #172

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion modAL/expected_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def expected_error_reduction(learner: ActiveLearner, X: modALinput, loss: str = 'binary',
p_subsample: np.float = 1.0, n_instances: int = 1,
p_subsample: float = 1.0, n_instances: int = 1,
random_tie_break: bool = False) -> np.ndarray:
"""
Expected error reduction query strategy.
Expand Down
43 changes: 32 additions & 11 deletions modAL/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

try:
import torch
except:
except ImportError:
pass


Expand All @@ -23,22 +23,37 @@ def data_vstack(blocks: Sequence[modALinput]) -> modALinput:
Returns:
New sequence of vertically stacked elements.
"""

if not blocks:
return blocks

types = {type(block) for block in blocks}

if any([sp.issparse(b) for b in blocks]):
return sp.vstack(blocks)
elif isinstance(blocks[0], pd.DataFrame):
return blocks[0].append(blocks[1:])
elif isinstance(blocks[0], np.ndarray):
elif types - {pd.DataFrame, pd.Series} == set():
def _block_to_df(block):
if isinstance(block, pd.DataFrame):
return block
elif isinstance(block, pd.Series):
# interpret series as a row
return block.to_frame().T
else:
raise TypeError(f"Expected DataFrame or Series but encountered {type(block)}")

return pd.concat([_block_to_df(block) for block in blocks])
elif types == {np.ndarray}:
return np.concatenate(blocks)
elif isinstance(blocks[0], list):
elif types == {list}:
return np.concatenate(blocks).tolist()

try:
if torch.is_tensor(blocks[0]):
if all(torch.is_tensor(block) for block in blocks):
return torch.cat(blocks)
except:
pass

raise TypeError("%s datatype is not supported" % type(blocks[0]))
raise TypeError("%s datatype(s) not supported" % types)


def data_hstack(blocks: Sequence[modALinput]) -> modALinput:
Expand All @@ -51,13 +66,19 @@ def data_hstack(blocks: Sequence[modALinput]) -> modALinput:
Returns:
New sequence of horizontally stacked elements.
"""

if not blocks:
return blocks

types = {type(block) for block in blocks}

if any([sp.issparse(b) for b in blocks]):
return sp.hstack(blocks)
elif isinstance(blocks[0], pd.DataFrame):
elif types == {pd.DataFrame}:
pd.concat(blocks, axis=1)
elif isinstance(blocks[0], np.ndarray):
elif types == {np.ndarray}:
return np.hstack(blocks)
elif isinstance(blocks[0], list):
elif types == {list}:
return np.hstack(blocks).tolist()

try:
Expand All @@ -66,7 +87,7 @@ def data_hstack(blocks: Sequence[modALinput]) -> modALinput:
except:
pass

TypeError("%s datatype is not supported" % type(blocks[0]))
raise TypeError("%s datatype(s) not supported" % types)


def add_row(X: modALinput, row: modALinput):
Expand Down
3 changes: 2 additions & 1 deletion rtd_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
numpy==1.20.0
numpy
scipy
scikit-learn
ipykernel
nbsphinx
pandas
skorch
torch