Skip to content

Commit

Permalink
RObject -> RView
Browse files Browse the repository at this point in the history
  • Loading branch information
pph2p committed Sep 20, 2024
1 parent 4c22a96 commit d516fdc
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 228 deletions.
2 changes: 1 addition & 1 deletion src/wrapr/RList.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .lazy_rexpr import lazily, lazy_wrap
from .rutils import rcall

# from .RObject import RObject
# from .RView import RView
# from .RArray import convert_numpy, is_valid_numpy, filter_numpy
# from .RDataFrame import convert_pandas, attempt_pandas_conversion, RDataFrame

Expand Down
20 changes: 13 additions & 7 deletions src/wrapr/RObject.py → src/wrapr/RView.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
from .rutils import rcall


class RObject():

class RView():
def __init__(self, Robj: Any):
self.Robj = Robj
from .RArray import RArray
from .RDataFrame import RDataFrame
from .RList import RList, RDict

if isinstance(Robj, (RArray, RDataFrame, RList, RDict)):
self.Robj = Robj.toR()
else:
self.Robj = Robj

def __str__(self) -> str:
# return captureRprint(self.Robj)
Expand All @@ -29,9 +35,9 @@ def __getitem__(self, *args):
def __iter__(self):
return self.Robj.__iter__()

def to_py(self):
def toPy(self, ignoreS3 = False):
from .convert_r2py import convert_r2py
return convert_r2py(self.Robj)
return convert_r2py(self.Robj, ignoreS3=ignoreS3)

def toR(self):
return self.Robj
Expand All @@ -44,12 +50,12 @@ def convert_s4(x: ro.methods.RS4) -> Any:

rclass = get_rclass(x)
if rclass is None:
return RObject(x)
return RView(x)

match np_collapse(rclass):
case "dgCMatrix": # to do: put this in a seperate function
dense = convert_numpy(as_matrix(x))
sparse = scipy.sparse.coo_matrix(dense)
return sparse
case _:
return RObject(x)
return RView(x)
2 changes: 1 addition & 1 deletion src/wrapr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
from .renv import Renv
from .RFactor import RFactor
from .RList import RList, RDict
from .RObject import RObject
from .RView import RView
from .settings import Settings
from .settings import settings
4 changes: 2 additions & 2 deletions src/wrapr/convert_py2r.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def convert_py_args2r(args: List[Any], kwargs: Dict[str, Any]) -> None:


def convert_py2r(x: Any) -> Any: # RBaseObject | PyDtype | Any:
from .RObject import RObject
from .RView import RView
from .RArray import RArray
from .RList import RList, RDict, pylist2rlist, dict2rlist
from .RDataFrame import RDataFrame, pandas2r

match x:
case RObject() | RArray() | RList() | RDataFrame() | RDict():
case RView() | RArray() | RList() | RDataFrame() | RDict():
return x.toR()
case np.ndarray():
return convert_numpy2r(x)
Expand Down
10 changes: 5 additions & 5 deletions src/wrapr/convert_r2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
from .rutils import has_unsupported_rclass, rcall


def convert_r2py(x: Any) -> Any:
def convert_r2py(x: Any, ignoreS3: bool = False) -> Any:
from .RArray import get_RArray, filter_numpy, is_valid_numpy
from .RDataFrame import RDataFrame
from .RDataFrame import attempt_pandas_conversion
from .RList import convert_r2pydict
from .RList import convert_r2pylist
from .RList import convert_rlist2py
from .RList import is_rlist
from .RObject import RObject, convert_s4
from .RView import RView, convert_s4


match x:
Expand All @@ -47,8 +47,8 @@ def convert_r2py(x: Any) -> Any:
return get_RArray(x) # return RArray, or int|str|bool|float if len == 1
case ro.methods.RS4():
return convert_s4(x)
case _ if has_unsupported_rclass(x):
return RObject(x)
case _ if has_unsupported_rclass(x) and not ignoreS3:
return RView(x)
case list():
return convert_r2pylist(x)
case tuple():
Expand All @@ -66,4 +66,4 @@ def convert_r2py(x: Any) -> Any:
case vc.ListSexpVector() | vc.ListVector():
return convert_rlist2py(x)
case _:
return RObject(x)
return RView(x)
4 changes: 2 additions & 2 deletions src/wrapr/function_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .settings import Settings, settings

def wrap_rfunc(func: Callable | Any, name: str | None) -> Callable | Any:
from .RObject import RObject
from .RView import RView
from .RArray import RArray
from .RDataFrame import RDataFrame
# should be a Callable, but may f-up (thus Any)
Expand All @@ -25,7 +25,7 @@ def wrap(*args, **kwargs):
func_name=name)
r_object: Any = lazyfunc(*args, **kwargs)
if settings.Rview:
return RObject(r_object)
return RView(r_object)
else:
return convert_r2py(r_object)

Expand Down
6 changes: 3 additions & 3 deletions src/wrapr/renv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .function_wrapper import rfunc, wrap_rfunc # wrap_rfunc should perhaps be its own module
from .rutils import rcall
from .convert_r2py import convert_r2py
from .RObject import RObject
from .RView import RView
from .settings import Settings, settings

class Renv:
Expand Down Expand Up @@ -103,12 +103,12 @@ def print(self, x):
# return attributes


def fetch_data(dataset: str, module: rpkg.Package) -> pd.DataFrame | RObject | None:
def fetch_data(dataset: str, module: rpkg.Package) -> pd.DataFrame | RView | None:
try:
r_object = rpkg.data(module).fetch(dataset)[dataset]

if settings.Rview:
return RObject(r_object)
return RView(r_object)
else:
return convert_r2py(r_object)

Expand Down
File renamed without changes.
17 changes: 0 additions & 17 deletions tests/modsem.py

This file was deleted.

Loading

0 comments on commit d516fdc

Please sign in to comment.