-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
169 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import numpy as np | ||
import rpy2 | ||
import rpy2.robjects.vectors as vc | ||
|
||
from numpy.typing import NDArray | ||
|
||
|
||
def convert_numpy(x: vc.Vector | NDArray) -> NDArray | None: | ||
if isinstance(x, rpy2.rinterface_lib.sexp.NULLType): | ||
return None | ||
match x: # this should be expanded upon | ||
case vc.BoolVector() | vc.BoolArray() | vc.BoolMatrix(): | ||
dtype = "bool" | ||
case vc.FloatVector() | vc.FloatArray() | vc.FloatMatrix(): | ||
dtype = "float" | ||
case vc.IntVector() | vc.IntArray() | vc.IntMatrix(): | ||
dtype = "int" | ||
case vc.StrArray() | vc.StrVector() | vc.StrMatrix(): | ||
dtype = "U" | ||
case _: | ||
dtype = None | ||
|
||
y = np.asarray(x, dtype=dtype) | ||
return filter_numpy(y) | ||
|
||
|
||
def filter_numpy(x: NDArray) -> NDArray | int | str | float | bool: | ||
# sometimes a numpy array will have one element with shape (,) | ||
# this should be (1,) | ||
y = x[np.newaxis][0] if not x.shape else x | ||
# if shape is (1,) we should just return as int | str | float | bool | ||
# R doesn't have these types, only vectors/arrays, this will probably | ||
# give unexpected results for users who are unfamiliar with R, so | ||
# we return the first element instead | ||
y = y[0] if y.shape == (1,) else y | ||
return y | ||
|
||
|
||
def is_valid_numpy(x: NDArray) -> bool: | ||
return x.dtype.fields is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pandas as pd | ||
|
||
import rpy2.robjects as ro | ||
import rpy2.robjects.vectors as vc | ||
|
||
from typing import Any | ||
from .RArray import convert_numpy | ||
|
||
def convert_pandas(df: vc.DataFrame) -> pd.DataFrame: | ||
colnames = df.names | ||
df_dict = {c: convert_numpy(x) for c, x in zip(colnames, list(df))} | ||
return pd.DataFrame(df_dict) | ||
|
||
|
||
def attempt_pandas_conversion(x: Any) -> Any: | ||
try: | ||
return pd.DataFrame(x) | ||
except: | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from collections import UserList | ||
|
||
|
||
a = UserList([1, 2, 3, 4]) | ||
setattr(a, "rowstart", 1) | ||
a.rowstart |
Oops, something went wrong.