Correct typing for numpy array with Drake expressions - numpy

I making a numpy array of Drake expressions, and I am trying to type hint them correctly. This is what I am currently doing:
import numpy as np
import numpy.typing as npt
import pydrake.symbolic
def some_function(expr: pydrake.symbolic.expression) -> npt.NDArray[pydrake.symbolic.expression]:
return np.array([expr])
where I am using type hints as above. However, I am getting the following error message:
Could not specialize type "NDArray[ScalarType#NDArray]"
Type "Expression" cannot be assigned to type "generic"
"Expression" is incompatible with "generic"
The same error message shows for different Drake objects like pydrake.symbolic.Formula etc.
For the record, I know I can use npt.ArrayLike instead of npt.NDArray[...], and this does not throw an error. However, I would like to find a solution that shows explicitly what the contents of the numpy arrays will be.

Related

TypeError when using modin with pd.cut(df[column],300)

I first sub in Modin for Pandas for the benefit of distributed work over multiple cores:
import modin.pandas as pd
from modin.config import Engine
Engine.put("dask")
After initializing my dataframe, I attempt to use:
df['bins'] = pd.cut(df[column],300)
I get this error:
TypeError: ('Could not serialize object of type function.', '<function PandasDataframe._build_mapreduce_func.<locals>._map_reduce_func at 0x7fbe78580680>')
Would be glad to get help.
I can't seem to get Modin to perform the way that I want out of the box, the way I expected.

Writing data frame with object dtype to HDF5 only works after converting to string

I have a big data dataframe and I want to write it to disk for quick retrieval. I believe to_hdf(...) infers the data type of the columns and sometimes gets it wrong. I wonder what the correct way is to cope with this.
import pandas as pd
import numpy as np
length = 10
df = pd.DataFrame({"a": np.random.randint(1e7, 1e8, length),})
# df.loc[1, "a"] = "abc"
# df["a"] = df["a"].astype(str)
print(df.dtypes)
df.to_hdf("df.hdf5", key="data", format="table")
Uncommenting various lines leads me to the following.
Just filling the column with numbers will lead to a data type int32 and stores without problem
Setting one element to abc changes the data to object, but it seems that to_hdf internally infers another data type and throws an error: TypeError: object of type 'int' has no len()
Explicitely converting the column to str leads to success, and to_hdf stores the data.
Now I am wondering what is happening in the second case, and is there a way to prevent this? The only way I found was to go through all columns, check if they are dtype('O') and explicitely convert them to str.
Instead of using hdf5, I have found a generic pickling library which seems to be perfect for the job: jiblib
Storing and loading data is straight forward:
import joblib
joblib.dump(df, "file.jl")
df2 = joblib.load("file.jl")

Numpy: Temporary copy during assignment

Consider the following code snippet:
import numpy as np
buf = np.full(10, True)
A = np.asarray([1,2,3])
buf[:3] = A!=A
In the last statement, will there be a temporary object created to hold the value of the expression A!=A which would subsequently be copied into buf or will the copy be elided?
Side note: I am aware of numpy.not_equal which can be passed an out argument to store the result. However, I am working with structured arrays and for some reasons that method is throwing the following error:
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
I am using Numpy 1.12.

Only size 1 arrays can be converted to python scalars

I created a 3 dimensional object using numpy.random module such as
import numpy as np
b = np.random.randn(4,4,3)
Why can't we cast type float to b?
TypeError
actual code
You can't float(b) because b isn't a number, it's a multidimensional array/matrix. If you're trying to convert every element to a Python float, that's a bad idea because numpy numbers are more precise, but if you really want to do that for whatever reason, you can do b.tolist(), which returns a Python list of floats. However, I don't believe you can have a numpy matrix of native Python types because that doesn't make any sense.

torch.pow does not work

I'm trying to create a custom loss function using PyTorch, and am running into a simple error.
When I try to use torch.pow to take the exponent of a PyTorch Variable, I get the following error message:
AttributeError: 'torch.LongTensor' object has no attribute 'pow'
In the python terminal, I created a simple Variable, and attempted to do the same, and received the same error. Here's a snippet that should recreate the problem:
import torch
from torch.autograd import Variable
import numpy as np
v = Variable(torch.from_numpy(np.array([1, 2, 3, 4])))
torch.pow(v, 2)
I can't find any information on this issue, and nothing is showing up in search results. Help?
EDIT: this problem also occurs when I try to use torch.sqrt()
EDIT: same problem also happens if I try to do
v.pow(2)
pow is definitely a method of v, and the docs clearly state that pow is a method that exists and takes a tensor as it's argument. I really don't see how this is happening, and it seems to me that the docs are just flat out wrong and these methods don't actually work.
You need to initialize the tensor as floats, because pow always returns a Float.
import torch
from torch.autograd import Variable
import numpy as np
v = Variable(torch.from_numpy(np.array([1, 2, 3, 4], dtype="float32")))
torch.pow(v, 2)
You can cast it back to integers afterwards
torch.pow(v, 2).type(torch.LongTensor)
yields
Variable containing:
1
4
9
16
[torch.LongTensor of size 4]