def xt_array(self, lexeme): data = self._nextExprData(lexeme) # converts lexeme into a numpy array if lexeme.hasAttr and lexeme.attrTypeCode == XT_LIST_TAG: for tag, value in lexeme.attr: if tag == 'dim': # the array has a defined shape data.shape = value elif tag == 'names': # convert numpy-vector 'value' into list to make TaggedArray work properly: data = asTaggedArray(data, list(value)) else: # there are additional tags in the attribute, just collect them in a dictionary # attached to the array. try: data.attr[tag] = value except AttributeError: data = asAttrArray(data, {tag: value}) return data
# String vector: ('c("abc", "defghi")', array(["abc", "defghi"])), ("seq(1, 5)", array(range(1, 6), dtype=numpy.int32)), ("polyroot(c(-39.141,151.469,401.045))", array([0.1762039 + 1.26217745e-29j, -0.5538897 - 1.26217745e-29j])), # An explicit R list with only one item remains a list on the python side: ('list("otto")', ["otto"]), ('list("otto", "gustav")', ["otto", "gustav"]), # tagged lists: ('list(husband="otto")', TaggedList([("husband", "otto")])), ('list(husband="otto", wife="erna")', TaggedList([("husband", "otto"), ("wife", "erna")])), ( 'list(n="Fred", no_c=2, c_ages=c(4,7))', TaggedList([("n", "Fred"), ("no_c", 2.0), ("c_ages", array([4.0, 7.0]))]), ), # tagged array: ("c(a=1.,b=2.,c=3.)", asTaggedArray(array([1.0, 2.0, 3.0]), ["a", "b", "c"])), # tagged single item array should remain an array on the python side in order to preserve the tag: ("c(a=1)", asTaggedArray(array([1.0]), ["a"])), # multi-dim array (internally also a tagged array) gets translated into a shaped numpy array: ("array(1:20, dim=c(4, 5))", shaped_array(range(1, 21), numpy.int32, (4, 5))), # # ('x<-1:20; y<-x*2; lm(y~x)', ????), # Environment # ('parent.env', [1,2]), ] ###############################################3 def test_rExprGenerator():
# String vector: ('c("abc", "defghi")', array(["abc", "defghi"])), ('seq(1, 5)', array(range(1, 6), dtype=numpy.int32)), ('polyroot(c(-39.141,151.469,401.045))', array([0.1762039 + 1.26217745e-29j, -0.5538897 - 1.26217745e-29j])), # An explicit R list with only one item remains a list on the python side: ('list("otto")', ["otto"]), ('list("otto", "gustav")', ["otto", "gustav"]), # tagged lists: ('list(husband="otto")', TaggedList([("husband", "otto")])), ('list(husband="otto", wife="erna")', TaggedList([("husband", "otto"), ("wife", "erna")])), ('list(n="Fred", no_c=2, c_ages=c(4,7))', TaggedList([("n", "Fred"), ("no_c", 2.), ("c_ages", array([4., 7.]))])), # tagged array: ('c(a=1.,b=2.,c=3.)', asTaggedArray(array([1., 2., 3.]), ['a', 'b', 'c'])), # tagged single item array should remain an array on the python side in order to preserve the tag: ('c(a=1)', asTaggedArray(array([1.]), ['a'])), # multi-dim array (internally also a tagged array) gets translated into a shaped numpy array: ('array(1:20, dim=c(4, 5))', shaped_array(range(1, 21), numpy.int32, (4, 5))), # #('x<-1:20; y<-x*2; lm(y~x)', ????), # Environment #('parent.env', [1,2]), ] ###############################################3 def test_rExprGenerator():