opensim-org / opensim-core

SimTK OpenSim C++ libraries and command-line applications, and Java/Python wrapping.
https://opensim.stanford.edu
Apache License 2.0
783 stars 316 forks source link

GCVSplineSet in loop freezes with positive errorVariance values #3664

Closed mrrezaie closed 8 months ago

mrrezaie commented 9 months ago

Hi, Sorry for posting this issue here. Could you please test this example and see if this is reproducible? GCVSplineSet with errorVariance values of 0 and -1 works well. But it freezes if I use positive errorVariance (even very small values). This behavior is not stable (sometimes works). inverse_kinematics.zip

import opensim as osim

IK = osim.TimeSeriesTable('inverse_kinematics.mot')
times = IK.getIndependentColumn() # time

GCVS = osim.GCVSplineSet(IK, [], 5, 0.5) # freezes on the 6th signal
# GCVS = osim.GCVSplineSet(IK) # works 

for i,cName in enumerate(IK.getColumnLabels()):
    print(i+1, cName)
    [GCVS.evaluate(i, 0, time) for time in times]
    # [GCVS.get(i).calcValue(osim.Vector(1,time)) for time in times] # either

Or this:

IK = osim.Storage('inverse_kinematics.mot')
GCVS = osim.GCVSplineSet(3, IK, 0.001) # freezes 

Thank you.

mrrezaie commented 9 months ago

I just tested this code on Google Colab to make sure nothing is wrong with my Python environment and configuration; the same happened.

OpenSim v.4.5 Thank you.

nickbianco commented 8 months ago

Thanks @mrrezaie for posting this issue. What do you mean by "freeze"? Does your working environment hang or crash? Or is it just taking a long time to perform the fit? With positive error variance, the spline fitter in Simbody will try to apply smoothing during the spline fit. I wonder if it's struggling to perform a smooth fit for the IK data in particular.

mrrezaie commented 8 months ago

What do you mean by "freeze"? Does your working environment hang or crash? Or is it just taking a long time to perform the fit? With positive error variance, the spline fitter in Simbody will try to apply smoothing during the spline fit.

Thanks @nickbianco for your response. It keeps processing and cannot finish it even after 30 min. Also, the keyboard interrupt command, Ctrl+C, does not work here and I have no choice but to restart the console.

I think the errorVariance of 0.5 is too big for signals such as 'pelvis_tz' angle (during gait cycle) which has a very small range. I found that if there are locked coordinates (zeros columns) in the input table, it would freeze even with a very small errorVariance, e.g., 0.001. So, I think I need to design a GCVSpline specifically for each signal. But as a suggestion, the positive errorVariance value in GCVSplineSet should be somehow relative, not absolute. In this case, same smoothness would be applied to all signals.

nickbianco commented 8 months ago

Based on this description from GCVSpline, providing a negative number tells the fitter to estimate the error variance before the spline fit. This is probably the best approach in most cases. If you need additional smoothing, I would try doing that as a preprocessing step (e.g., a Butterworth filter) before performing the spline fit.

mrrezaie commented 8 months ago

Fair enough. Is there any method to get the estimated error variance?

nickbianco commented 8 months ago

I would probably dig into the source code for that, specifically Simbody's SplineFitter class.