rpy2 how to call as.matrix() - rpy2

I am using rpy2 to call some R function from python. The R function returns a dist object. How do I call as.matrix() from python on the result the R code returns?
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()
Rsession = rpy2.robjects.r
tsclust = importr('TSclust')
X= np.random.random((5,5))
result = Rsession.diss( X , "ACF", p=0.05)
result is of the class dist. I would like a square distance matrix, which I can achieve by calling as.matrix() on the result . How do I do this with rpy2?

It's possible to do it just like that:
from rpy2.robjects.packages import importr
base = importr('base')
base.as_matrix(result)

I figured out one solution. It's not pretty but it works. We define a custom function that calls as.matrix
from rpy2.robjects.packages import STAP
mfunc = 'myasmatrix <- function(dobj){return(as.matrix(dobj))}'
myasmatrix = STAP(mfunc, "myasmatrix")
np.array(myasmatrix.myasmatrix(rres))
>>> array([[0. , 0.0663193 , 0.01159857, 0.14378692, 0.10069314],
[0.0663193 , 0. , 0.06064907, 0.07965314, 0.03511945],
[0.01159857, 0.06064907, 0. , 0.13898505, 0.095319 ],
[0.14378692, 0.07965314, 0.13898505, 0. , 0.04757353],
[0.10069314, 0.03511945, 0.095319 , 0.04757353, 0. ]])

Related

Why error: "must be real number, not RestMode"? How can fix it?

I'm using ROS, and writing some codes to do some tasks, and now I'm facing an error: TypeError: must be real number, not RestMode, for more details, I have code here:
#!/usr/bin/env python
#encoding: utf-8
import rospy
from geometry_msgs.msg import Vector3
from sensor_msgs.msg import Imu
from std_msgs.msg import Float64
import numpy as np
import geometry as geo
import transformation as tf
from IK_solver import IK
class RestMode:
def __init__(self, bodyDimensions, legDimensions):
# rospy.Subscriber('spot_keyboard/body_pose',Vector3,self.callback)
self.bodyLength = bodyDimensions[0]
self.bodyWidth = bodyDimensions[1]
self.bodyHeight = bodyDimensions[2]
self.l1 = legDimensions[0]
self.l2 = legDimensions[1]
self.l3 = legDimensions[2]
# rospy.Subscriber('spot_imu/base_link_orientation',Imu, self.get_body_pose)
self.rate = rospy.Rate(10.0) #10Hz
self.rb = IK(bodyDimensions, legDimensions)
angles_cmd = [ 'spot_controller/FL1_joint/command',
'spot_controller/FL2_joint/command',
'spot_controller/FL3_joint/command',
'spot_controller/RL1_joint/command',
'spot_controller/RL2_joint/command',
'spot_controller/RL3_joint/command',
'spot_controller/RR1_joint/command',
'spot_controller/RR2_joint/command',
'spot_controller/RR3_joint/command',
'spot_controller/FL1_joint/command',
'spot_controller/FL2_joint/command',
'spot_controller/FL3_joint/command' ]
self.joint = []
for i in range(12):
self.joint.append(rospy.Publisher(angles_cmd[i], Float64, queue_size=10))
# self.initial_pose()
def initial_pose(self,roll=0,pitch=0,yaw=0,dx=0,dy=0,dz=None):
if dz == None:
dz = self.bodyHeight
order = ['FL','RL','RR','FR']
angles = []
rospy.loginfo("Start Calculate Angles!")
for leg in order:
(q1,q2,q3,ht) = self.rb.calculateAngles(self,roll,pitch,yaw,dx,dy,dz,leg)
angles.append(q1)
angles.append(q2)
angles.append(q3)
rospy.loginfo("Done! Start publish!")
for i in range(12):
self.joint[i].publish(angles[i])
self.rate.sleep()
if __name__ == '__main__':
rospy.init_node('rest_mode', anonymous=True)
body = [0.1908, 0.080, 0.15]
legs = [0.04, 0.1, 0.094333]
rest = RestMode(body, legs)
try:
while not rospy.is_shutdown():
rest.initial_pose()
except rospy.ROSInterruptException:
pass
When method calculateAngles(self,roll,pitch,yaw,dx,dy,dz,leg) with argument leg in last, it throws: TypeError: must be real number, not RestMode.
But when I change it to first like: calculateAngles(self,leg,roll,pitch,yaw,dx,dy,dz), then error says: TypeError: must be real number, not str with input in another module, but I tested all of the others related module, and they are fine, so I think that must be an issue in codes above!
That error is so strange:
I don't push any str as input
When changing the position of argument leg, it throws a different error.
When calling instance methods, self is an implied parameter, and should never be explicitly passed. When you are using self.rb.calculateAngles(self, ..., that second self is an instance of a RestMode class, which your IK class does not accept...
Therefore, you want
(q1,q2,q3,ht) = self.rb.calculateAngles(roll,pitch,yaw,dx,dy,dz,leg)
And change other usages within the IK class as well

How to define a function that decrease a number half of itself repeatedly?

I am studying statistics on MIT open courses. I will attach the image of the question.
In an example:
I want to get the below value:
[1, 0.5, 0.25, 0.125, 0.0625 ...]
I have tried a recursive way to solve this problem.
Which is like
import numpy as np
def halfe(x):
return x/2
A = np.array([[.8, .3],
[.2, .7]])
#if I put dot usage of np i get the correct solution for the first iteration
u0 = np.array([1,0])
u1 = A.dot(u0)
print(u1)
[0.8 0.2]
#now the new value will be used to iterate for another value. In an example:
u2 = A.dot(u1)
######### also I made a broken solution for this ############
def halfer(x):
return x/2
for i in range(0,10):
B = A.dot(halfer(np.array([[1],[0]])))
#but I am completely on a different page...
I will attach the image of the question. My target is to make a recursive function to iterate it easily.
I would be glad If you could reply enter image description here
I don't think you need any recursive function. A for loop can accomplish it:
import numpy as np
A = np.array([[.8, .3], [.2, .7]])
u0 = np.array([1,0])
# Initialize list U to store values of u_i
U=[u0]
N=3
for i in range(N):
U.append(A#U[-1])
print(f'u{i+1} value is {U[-1]}')
u1 value is [0.8 0.2]
u2 value is [0.7 0.3]
u3 value is [0.65 0.35]
U
[array([1, 0]),
array([0.8, 0.2]),
array([0.7, 0.3]),
array([0.65, 0.35])]
I don't really understand why you need an halfer function. But if you still do, you can either use a list comprehension to do that:
a=np.array([1,2,3])
[a/2.0**i for i in range(5)]
[array([1., 2., 3.]),
array([0.5, 1. , 1.5]),
array([0.25, 0.5 , 0.75]),
array([0.125, 0.25 , 0.375]),
array([0.0625, 0.125 , 0.1875])]

can't convert expression to float problem

i am trying to use the "subs" function for differential equation
but i get the error: "can't convert expression to float"
i tryed to check the type of the arrays, but they all float
import sympy as sym
from sympy.integrals import inverse_laplace_transform
from sympy.abc import s,t,y
import numpy as np
U = 1
G =(s+1)/(s*(s+2))
Y = G*U
y = inverse_laplace_transform(Y, s, t)
tm = np.linspace(0,2,3)
y_val = np.zeros(len(tm))
for i in range(len(tm)):
y_val[i] = y.subs(t, tm[i])
print(y)
print(y_val)
line 17
y_val[i] = y.subs(t, tm[i])
TypeError: can't convert expression to float
Ths issue here is that, because tm[0] == 0, the evaluated y in the first iteration of your loop is Heaviside(0), which has no defined real value by default (see https://docs.sympy.org/latest/modules/functions/special.html#heaviside). This is because you have
from sympy.functions import exp, Heaviside
assert y == Heaviside(t) / 2 + exp(-2 * t) * Heaviside(t) / 2
The simplest workaround here is defining a linear space excluding 0, for instance
epsilon = 1e-15
tm = np.linspace(epsilon, 2, 3)
Using y_val = np.zeros(len(tm)), the default datatype of array is float. After modifying the code, you find that one of y_val elements is an object, not float. You can use a list object as a placeholder or you can specify the datatype of numpy array as object:
import sympy as sym
from sympy.integrals import inverse_laplace_transform
from sympy.abc import s,t,y
import numpy as np
U = 1
G =(s+1)/(s*(s+2))
Y = G*U
y = inverse_laplace_transform(Y, s, t)
tm = np.linspace(0,2,3)
# y_val = [0 for _ in range(len(tm))]
y_val = np.zeros(len(tm), dtype=object)
for i in range(len(tm)):
y_val[i] = y.subs(t, tm[i])
print(y_val)
result: [Heaviside(0.0) 0.567667641618306 0.509157819444367]
I have similar problem and your answers work for me, but I still need to put the data into graph.. I modified my problem for this question:
import sympy as sym
from sympy.integrals import inverse_laplace_transform
from sympy.abc import s,t,y
import numpy as np
import matplotlib.pyplot as plt
Y = (5*(1 - 5*s))/(s*(4*(s**2) + s + 1))*(1/s)
y = inverse_laplace_transform(Y, s, t)
tm = np.linspace(1e-15, 20, 100)
y_val = np.zeros(len(tm), dtype=object)
for i in range(len(tm)):
y_val[i] = y.subs(t, tm[i])
plt.plot(y_val, tm)
plt.show()
Running this code I got same error:
TypeError: can't convert expression to float

AttributeError: 'DType' object has no attribute 'type' Tensorflow Serving

I am trying to use a function (from another module) inside tensorflow. The function accepts a numpy array and returns the changepoints. My main goal is to deploy this model on tensorflow serving. I am running into error
AttributeError: 'DType' object has no attribute 'type'
There are 2 functions, one is create_data() that creates a numpy array and returns it, another is change() which accepts numpy array and uses the before mentioned function to return changepoints. I have created a placeholder to accept input data, an operation to execute the function. Problem is, if i try to send data through placeholder, i run into error. If i send the data directly into the function, it runs. Following is my code.
def create_data():
np.random.seed(0)
size = 100
mean_a = 0.0
mean_b = 10.0
mean_c = 0
var = 0.1
data_a = np.random.normal(mean_a, var, size)
data_b = np.random.normal(mean_b, var, size)
data_c = np.random.normal(mean_c, var, size)
data = np.concatenate([data_a, data_b, data_c])
return data
def change(data):
# what else i tried
# data = np.array(data, dtype=np.float)
# above line gives another error mentioned after code
cpts = (pelt(normal_mean(x, np.var(x)), len(x)))
return cpts
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[300, ], name="myInput")
y = tf.convert_to_tensor(change(x),np.float32,name="myOutput")
z = sess.run(y,feed_dict={x:create_data()})
If i try the code data = np.array(data, dtype=np.float) in the function change(), it gives me error
ValueError: setting an array element with a sequence.
I also tried data = np.hstack((data)).astype(np.float) and data = np.vstack((data)).astype(np.float) but it runs into a separate error that says use tf.map_fn. I also tried to use tf.eval() to convert the numbers but i couldn't get them to run inside a function with placeholders.
But if i send in the output directly,
y = tf.convert_to_tensor(change(create_data()),np.float32,name="myOutput")
It works.
How should i send in the input to make it work?
EDIT: The function in question is this if anyone wants to know.
This error is raised when you try to pass a Tensor into a numpy function
You need to use tf.py_func to include python function into tensorflow graph
(also, your change() functin uses data as argument instead of x)
Here is the code that worked for me
import numpy as np
import tensorflow as tf
from changepy import pelt
from changepy.costs import normal_mean
def create_data():
np.random.seed(0)
size = 100
mean_a = 0.0
mean_b = 10.0
mean_c = 0
var = 0.1
data_a = np.random.normal(mean_a, var, size)
data_b = np.random.normal(mean_b, var, size)
data_c = np.random.normal(mean_c, var, size)
data = np.concatenate([data_a, data_b, data_c])
return data
def change(x):
# what else i tried
# data = np.array(data, dtype=np.float)
# above line gives another error mentioned after code
cpts = (pelt(normal_mean(x, np.var(x)), len(x)))
return cpts
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[300, ], name="myInput")
y = tf.convert_to_tensor(tf.compat.v1.py_func(change, [x], 3*[tf.int64]),np.float32,name="myOutput")
z = sess.run(y,feed_dict={x:create_data()})
print(z)

Force NNLS result

I am using scipy.optimize.nnls to compute non-negative least square fit with a coefficients sum to 1 :
#! /usr/bin/env python3
import numpy as np
import scipy.optimize as soptimize
if __name__ == '__main__':
C = np.array([[112.771820, 174.429720, 312.175750, 97.348620],
[112.857010, 174.208300, 312.185270, 93.467580],
[114.897210, 175.661850, 314.275100, 99.015480]
]);
d = np.array([[112.7718, 174.4297, 312.1758, 97.3486]]);
for line in d:
ret , _= soptimize.nnls(C.T, line)
print(ret)
And I get :
[9.99992794e-01 7.27824399e-06 0.00000000e+00]
Is it possible to set some result column to a specific value for the nnls algorithm and to force it to generate the remaining result columns ?
For instance if my result is : [0.3 0.3 0.4]. I want to force the first column to 0.9 and the nnls should generate the other columns, like this :
[0.9 0.06 0.04]
Any help will be appreciated !

Resources