Page 1 of 1

Reading states in Python

Posted: Sun Sep 02, 2018 2:48 pm
by flgw
How can I inspect states from an .sto files in Pyhton, i.e. get a list or np.array containing entries from a row and get their column headers?

Code: Select all

import opensim

fname = 'cmc_states.sto'
sto = osim.Storage(fname)
sv = sto.getStateVector(aTimeIndex=3)
Or like this, where I at least could manage to read a single entry.

Code: Select all

sto = osim.STOFileAdapter_read(fname)
print(sto.getNearestRow(1)[0])
And how can I subsequently load a state into a model?

Code: Select all

model = opensim.Model(model_path)

Re: Reading states in Python

Posted: Tue Sep 04, 2018 9:26 am
by tkuchida
There are a few examples here: https://github.com/opensim-org/opensim- ... n/examples. See, in particular, Chris Dembia's posthoc_StatesTrajectory_example.py (https://github.com/opensim-org/opensim- ... example.py).

Re: Reading states in Python

Posted: Wed Oct 10, 2018 1:56 pm
by flgw
Thanks a lot! But now I run into a different problem. Doing this:

Code: Select all

sto = opensim.Storage(fname)
straj = opensim.StatesTrajectory()
straj = straj.createFromStatesStorage(env.osim_model.model, sto)
for i in range(straj.getSize()):
    s = straj.get(i)
    env.osim_model.set_state(s)
Gives me either:

Code: Select all

SimTK Exception thrown at System.cpp:394:
  System::Guts::realizeModel(): The given State's Topology stage version number (1) doesn't match the current topology cache version number (2) of System MultibodySystem. That means there has been a topology change to this System since this State was created so they are no longer compatible. You should create a new State from the System's default State. (Loopholes exist for advanced users.)
Or:

Code: Select all

RuntimeError: std::exception in 'OpenSim::StatesTrajectory OpenSim::StatesTrajectory::createFromStatesStorage(OpenSim::Model const &,OpenSim::Storage const &)': The following 44 states from Model 'gait14dof22musc' are missing from the states Storage:
    abd_r/activation
    abd_r/fiber_length
    add_r/activation
    add_r/fiber_length
    hamstrings_r/activation
    hamstrings_r/fiber_length
    bifemsh_r/activation
    bifemsh_r/fiber_length
    glut_max_r/activation
    glut_max_r/fiber_length
    iliopsoas_r/activation
    iliopsoas_r/fiber_length
    rect_fem_r/activation
    rect_fem_r/fiber_length
    vasti_r/activation
    vasti_r/fiber_length
    gastroc_r/activation
    gastroc_r/fiber_length
    soleus_r/activation
    soleus_r/fiber_length
    tib_ant_r/activation
    tib_ant_r/fiber_length
    abd_l/activation
    abd_l/fiber_length
    add_l/activation
    add_l/fiber_length
    hamstrings_l/activation
    hamstrings_l/fiber_length
    bifemsh_l/activation
    bifemsh_l/fiber_length
    glut_max_l/activation
    glut_max_l/fiber_length
    iliopsoas_l/activation
    iliopsoas_l/fiber_length
    rect_fem_l/activation
    rect_fem_l/fiber_length
    vasti_l/activation
    vasti_l/fiber_length
    gastroc_l/activation
    gastroc_l/fiber_length
    soleus_l/activation
    soleus_l/fiber_length
    tib_ant_l/activation
    tib_ant_l/fiber_length
	Thrown at StatesTrajectory.cpp:223 in createFromStatesStorage().
Or:

Code: Select all

RuntimeError: std::exception in 'OpenSim::StatesTrajectory OpenSim::StatesTrajectory::createFromStatesStorage(OpenSim::Model const &,OpenSim::Storage const &)': States Storage is in degrees, but this is inappropriate for creating a StatesTrajectory. Edit the Storage so that angles are in radians, and set 'inDegrees' to no in the header.
	Thrown at StatesTrajectory.cpp:192 in createFromStatesStorage().

Re: Reading states in Python

Posted: Sun Jun 16, 2019 11:45 pm
by hossein
Hi all,

I get the same error in Matlab when I use "states = StatesTrajectory.createFromStatesStorage(model, sto)"


model = Model(fullfile(model_folder, 'subject01_simbody.osim'));
cd('.\IKResults');
model.initSystem()
sto = Storage("W1+R+ACC_ik.mot");
sto.setInDegrees(0)
states = StatesTrajectory.createFromStatesStorage(model, sto);


I wonder if you have found a solution for it. I guess we need to initialise all the states but when I just load IK results (via Storage as states) then it seems we need to initialise other States. Can we just load kinematics and keep everything as default?
Cheers,
Hossein

Re: Reading states in Python

Posted: Mon Jun 17, 2019 12:32 am
by nicos1993
Hello,

I think the error is related to the 'states' file you're using. The IK file coordinates are not the only states, the states include the coordinates plus the coordinate velocities (which you don't get directly from IK). Your model is also actuated by muscles, then you will require states for muscle length and activation. I hope that helps! :-)

Kind regards,

Nicos Haralabidis

Re: Reading states in Python

Posted: Mon Jun 17, 2019 12:45 am
by nicos1993
From the API it looks as though you can choose to ignore including states for which you don't have values for by setting the last two input arguments to true:

https://simtk.org/api_docs/opensim/api_ ... 16808dde64

Cheers,

Nicos

Re: Reading states in Python

Posted: Tue Jun 18, 2019 5:02 am
by inbae908
Hello,

as mentioned above, I was able to ignore including the states I don't have by doing this:

Code: Select all

sto = osim.Storage("orig.sto")
states = osim.StatesTrajectory()
states = states.createFromStatesStorage(model, sto, True)
Now my concern is to use getStateVariableValue to get the angle of a coordinate at certain time.
However, when I did

Code: Select all

print(model.getStateVariableValue(states[3], "pelvis_tx"))
it gives me an error saying,

RuntimeError: std::exception in 'double OpenSim::Component::getStateVariableValue(SimTK::State const &,std::string const &) const': Component::getStateVariableValue: ERR- state named 'pelvis_tx' not found in UPPERLOWERBODYSIMPLEMARIJE-scaled of type Model

I'm assuming "pelvis_tx" is not the correct form for name (string) of the state variable. Does anyone know how I should change the state variable name?

Best regards,
In Bae Chung

Re: Reading states in Python

Posted: Tue Jun 18, 2019 6:54 am
by tkuchida
I'm assuming "pelvis_tx" is not the correct form for name (string) of the state variable.
You can ask the Model (or any Component) for a list of its state variables using the getStateVariableNames() method (https://simtk.org/api_docs/opensim/api_ ... 381a014b8a).