ir-lab / intprim

Interaction Primitives library
MIT License
61 stars 18 forks source link

Errors while using EKF #5

Open souljaboy764 opened 3 years ago

souljaboy764 commented 3 years ago

TL;DR

I encountered some errors while using the EKF filtering for performing BIP inference both from my code and from the minimal.py example that is provided. The corresponding fixes can be seen in the commit: souljaboy764/intprim@4ab692c80deceb898e73941f7f3c69ecd7d54664

Long Version:

I was trying to use the EKF filter for performing the performing inference with the BIP, but was getting the following error:

  File "build/bdist.linux-x86_64/egg/intprim/bayesian_interaction_primitives.py", line 298, in generate_probable_trajectory_recursive
  File "build/bdist.linux-x86_64/egg/intprim/filter/spatiotemporal/ekf.py", line 157, in localize
  File "build/bdist.linux-x86_64/egg/intprim/filter/spatiotemporal/nonlinear_system.py", line 109, in get_measurement_model
  File "build/bdist.linux-x86_64/egg/intprim/basis/basis_model.py", line 91, in get_weighted_vector_derivative
IndexError: invalid index to scalar variable.

I thought it might have been an issue in my code but I was getting this issue even when I was running minimal.py with the EKF filter. I am getting the issue in both python2 and python3.

The stack trace shows the error in basis_model.py in the get_weighted_vector_derivative function at the following line: https://github.com/ir-lab/intprim/blob/8994b158c3f1306be7f9d6304b370d70e2c257fb/intprim/basis/basis_model.py#L91

On reading the docstring of the function, it says:that x should be a scalar value: https://github.com/ir-lab/intprim/blob/8994b158c3f1306be7f9d6304b370d70e2c257fb/intprim/basis/basis_model.py#L78

I am able to resolve this issue with dropping the index used for x and the line now looks like:

        basis_func_derivs = self.get_basis_function_derivatives(x)

After fixing the above, I get the next error:

  File "build/bdist.linux-x86_64/egg/intprim/bayesian_interaction_primitives.py", line 298, in generate_probable_trajectory_recursive
  File "build/bdist.linux-x86_64/egg/intprim/filter/spatiotemporal/ekf.py", line 157, in localize
  File "build/bdist.linux-x86_64/egg/intprim/filter/spatiotemporal/nonlinear_system.py", line 110, in get_measurement_model
  File "build/bdist.linux-x86_64/egg/intprim/basis/basis_model.py", line 50, in get_block_diagonal_basis_matrix
ValueError: could not broadcast input array from shape (8) into shape (8,1)

On looking further, I saw that the phase value x passed to the get_block_diagonal_basis_matrix function in basis_model.py is a scalar value, which is used here: https://github.com/ir-lab/intprim/blob/8994b158c3f1306be7f9d6304b370d70e2c257fb/intprim/basis/basis_model.py#L48

The docstrings of the get_basis_functions function for all the different basis functions state that the shape of the return value would be shape(degree, ) if x is a scalar. so I am able to fix this by expanding the dimensions of the value returned by the function:

        basis_funcs = self.get_basis_functions(x)
        if np.isscalar(x):
            basis_funcs = basis_funcs[:, None]

Since this is the same case in the get_block_diagonal_basis_matrix_derivative function, I felt a similar change there would fit.

The next error I get is:

  File "build/bdist.linux-x86_64/egg/intprim/bayesian_interaction_primitives.py", line 298, in generate_probable_trajectory_recursive
  File "build/bdist.linux-x86_64/egg/intprim/filter/spatiotemporal/ekf.py", line 157, in localize
AttributeError: 'GaussianModel' object has no attribute 'inverse_transform'

which is basically this line: https://github.com/ir-lab/intprim/blob/8994b158c3f1306be7f9d6304b370d70e2c257fb/intprim/filter/spatiotemporal/ekf.py#L157

After digging further, I saw that the only use of inverse_transform is for the scalers and not for any basis functions. Maybe it is from a previous version? I changed that part of the code to how it is in kf.py:

            predicted_measurement = np.dot(measurement_model[:,self.system_size:], self.state_mean[self.system_size:])
junfoot commented 5 months ago

thank you, that helps me a lot!