how to get states from .mot file using API commands?

The 2018 Fall Virtual Workshop will bring together a group of international scholars and OpenSim experts to help each other advance their research using modeling and simulation. This forum is a place to accelerate OpenSim based projects while also buildin
POST REPLY
User avatar
Dhruv Gupta
Posts: 30
Joined: Sat Aug 29, 2015 11:23 am

how to get states from .mot file using API commands?

Post by Dhruv Gupta » Tue Oct 23, 2018 2:23 pm

Hello all,

This might be trivial for a lot of you, but I am new to using API/Doxygen commands in MATLAB. I am trying to find muscle moment arms using the command
Muscle.computeMomentArm(state, coordinate);
How can I find the "state" from a .mot file?

Thanks!
Dhruv

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: how to get states from .mot file using API commands?

Post by Christopher Dembia » Tue Oct 23, 2018 2:33 pm

If you are using 4.0, you can use the StatesTrajectory class. See the following example in Python (the code should look very similar in MATLAB). https://github.com/opensim-org/opensim- ... example.py

User avatar
Dhruv Gupta
Posts: 30
Joined: Sat Aug 29, 2015 11:23 am

Re: how to get states from .mot file using API commands?

Post by Dhruv Gupta » Tue Oct 23, 2018 3:17 pm

Thanks for the response, Christopher. Based on the link, it seems like I need to use StatesTrajectory.createFromStatesStorage().

So is it anything like this?

import org.opensim.modeling.*
modelFile = 'ULB.osim';
osimModel = Model(modelFile);
motFile='mymotion_ik.mot';
coordinateSto = Storage(motFile);

state = StatesTrajectory.createFromStatesStorage(coordinateSto)

shoulderElvCoord = osimModel.updCoordinateSet().get('shoulder_elv');
muscles = osimModel.getMuscles();
nMuscles = muscles.getSize();
DELT1 = osimModel.getMuscles().get('DELT1');
momentarm = DELT1.computeMomentArm(state, shoulderElvCoord);


Does the computeMomentArm function calculate the muscle moment arm for the entire states trajectory or just one time point?

Again, I am sorry I am new to using API through MATLAB. So far I have only been using command line execution through MATLAB as a link to OpenSim.

Thanks for your time!
Dhruv

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: how to get states from .mot file using API commands?

Post by Christopher Dembia » Tue Oct 23, 2018 3:42 pm

Dhruv that's pretty close. The return value of createFromStateStorage() is a StatesTrajectory, not a single state. So you need to loop through each state. There is likely a method like get() on the StatesTrajectory.

computeMomentArm() operates on a single state, not the entire trajectory.

User avatar
Dhruv Gupta
Posts: 30
Joined: Sat Aug 29, 2015 11:23 am

Re: how to get states from .mot file using API commands?

Post by Dhruv Gupta » Tue Oct 23, 2018 5:44 pm

Thanks Christopher. I looked up in the API documentation and is said that the use in python is something like this:

import opensim
model = opensim.Model("subject01.osim")
sto = opensim.Storage("subject01_states.sto")
states = opensim.StatesTrajectory.createFromStatesStorage(model, sto)


so for MATLAB I wrote this:

import org.opensim.modeling.*
modelFile = 'ULB_Simple_v33_Thorax_markers_hand_MoBLmuscles_knee.osim';
osimModel = Model(modelFile);
coordinateSto = Storage('M_S9_R_MB_f6_ik.mot');
states = StatesTrajectory.createFromStatesStorage(osimModel, coordinateSto)

But it gives me an error that
Undefined variable "StatesTrajectory" or class "StatesTrajectory.createFromStatesStorage".


I think I missed your earlier comment. I am using OpenSim 3.3. Is there a way to load the additional library or something so I can use the functions of StatesTrajectory?

Thank You
Dhruv

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: how to get states from .mot file using API commands?

Post by Christopher Dembia » Tue Oct 23, 2018 5:52 pm

StatesTrajectory is only available in 4.0. For 3.3, you can try reimplementing the following Python program in Matlab: https://github.com/chrisdembia/perimysi ... ng.py#L619

User avatar
Dhruv Gupta
Posts: 30
Joined: Sat Aug 29, 2015 11:23 am

Re: how to get states from .mot file using API commands?

Post by Dhruv Gupta » Tue Oct 23, 2018 7:03 pm

Thank You, Christopher. It seems to work, but I have a question though. I calculated the moment arm using three different ways
1. In the plotter in GUI using the _ik.mot file
2. In MATLAB using the _ik.mot file
3. In MATLAB using the _states.sto file (exported by performing RRA on the same motion).

For case 2 I used line 5 and for case 3 I used line 6 of the attached code.

The results for 1 and 3 match, but 2 does not. It make me wonder is the states calculations based on a _ik.mot in MATLAB correct?
momentcalc.jpg
momentcalc.jpg (258.31 KiB) Viewed 744 times

POST REPLY