I’m working on a surgical simulation analysis where I need to move the muscle origins close to 300 times. I have figured out how to update the .osim files within matlab to change the PathPoint locations and save a new .osim file. However, I’m currently having difficultly trying to gather the data within Matlab that would be used to populate the moment vs joint angle plots that are so easily developed in the OpenSim GUI. I don’t need the to plot the data in the OpenSim plot tool from Matlab. I just need the x and y data that is used to construct the plot.
From the doxygen, it seems that in order to get moment arm and muscle force (ie. moment) I need a States file the functions use as input. The problem is that muscle length is contained within the States file which is changing for each simulation. Additionally, I am confused as to why I would need a States file considering that building those plots in OpenSim does not require external development of a states file.
If anybody has any suggestions as to how to solve my problem or a better way to approach it, it would be much appreciated.
Matlab interface to x and y data in plotting tool
- Anthony Santago
- Posts: 15
- Joined: Mon Feb 15, 2010 11:33 am
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Matlab interface to x and y data in plotting tool
Hi Anthony,
Great to hear from you.
For model changes, you don't actually have to save the model in a file, you can change it in memory (in a loop for example) so that your process is not slowed down by I/O and your file system is not cluttered.
When in the GUI, OpenSim creates an AnalyzeTool with MuscleAnalysis and runs it under the covers to generate the data shown in the plots. The two options you have are:
1. In Matlab, instantiate an AnalyzeTool with MuscleAnalysis (from a file) and run it on the model after each change. You can setup the MuscleAnalysis to write the results to files then you can load the output files and generate plots as needed in Matlab.
2. You can switch to using the Scripting shell in the GUI which has an interface similar to Matlab (Python). This has the advantage that you have access to the plotter code directly so you don't have to create an AnalyzeTool or MuscleAnalysis. There's an example script that exports data from a plot to a file included in the distribution.
Best of luck and please let us know if you run into problems following either approach so that we can help you and so that other users can benefit as well.
Best regards,
-Ayman
Great to hear from you.
For model changes, you don't actually have to save the model in a file, you can change it in memory (in a loop for example) so that your process is not slowed down by I/O and your file system is not cluttered.
When in the GUI, OpenSim creates an AnalyzeTool with MuscleAnalysis and runs it under the covers to generate the data shown in the plots. The two options you have are:
1. In Matlab, instantiate an AnalyzeTool with MuscleAnalysis (from a file) and run it on the model after each change. You can setup the MuscleAnalysis to write the results to files then you can load the output files and generate plots as needed in Matlab.
2. You can switch to using the Scripting shell in the GUI which has an interface similar to Matlab (Python). This has the advantage that you have access to the plotter code directly so you don't have to create an AnalyzeTool or MuscleAnalysis. There's an example script that exports data from a plot to a file included in the distribution.
Best of luck and please let us know if you run into problems following either approach so that we can help you and so that other users can benefit as well.
Best regards,
-Ayman
- Anthony Santago
- Posts: 15
- Joined: Mon Feb 15, 2010 11:33 am
Re: Matlab interface to x and y data in plotting tool
Ayman,
Thank you for your reply. It was most helpful and has allowed me to make substantial progress. I used a motion file inserted into the AnalyzeTool.xml and was able to calculate passive force and moment without much of problem. However, I am having difficulty obtaining active force and moment.
I attempted to force the muscles to have an activation of 1 by using a states file instead of a motion file as input into the the AnalyzeTool.xml. However, this created a lot more problems than it solved. I have set the "solve_for_equilibrium_for_auxiliary_states" flag to both True and False and have had problems regardless of the state of the flag.
In the plotting tool there is an activation override toggle that allows the user to set the muscle activation to whatever they desire. I was hoping you would be able to help me understand exactly what that toggle is doing so that I can recreate in the Matlab environment. Thank for your continued support.
Thank you for your reply. It was most helpful and has allowed me to make substantial progress. I used a motion file inserted into the AnalyzeTool.xml and was able to calculate passive force and moment without much of problem. However, I am having difficulty obtaining active force and moment.
I attempted to force the muscles to have an activation of 1 by using a states file instead of a motion file as input into the the AnalyzeTool.xml. However, this created a lot more problems than it solved. I have set the "solve_for_equilibrium_for_auxiliary_states" flag to both True and False and have had problems regardless of the state of the flag.
In the plotting tool there is an activation override toggle that allows the user to set the muscle activation to whatever they desire. I was hoping you would be able to help me understand exactly what that toggle is doing so that I can recreate in the Matlab environment. Thank for your continued support.
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Matlab interface to x and y data in plotting tool
Hi Anthony,
The GUI actually creates a states file from the motion file you provide (when plotting against a motion). The states file contains activation and fiberlength column for each muscle. The default (as of version 3.0) is to set activation to 1.0 but the user has the option to override this behavior and provide a different value that gets pushed to the states file. The plotter also sets the flag solve_for_equilibrium_for_auxiliary_states to true.
What problems did you run into on the Matlab side?
Best regards,
-Ayman
The GUI actually creates a states file from the motion file you provide (when plotting against a motion). The states file contains activation and fiberlength column for each muscle. The default (as of version 3.0) is to set activation to 1.0 but the user has the option to override this behavior and provide a different value that gets pushed to the states file. The plotter also sets the flag solve_for_equilibrium_for_auxiliary_states to true.
What problems did you run into on the Matlab side?
Best regards,
-Ayman
- Anthony Santago
- Posts: 15
- Joined: Mon Feb 15, 2010 11:33 am
Re: Matlab interface to x and y data in plotting tool
Ayman,
Thank for you quick reply. The problem I'm running into is that the muscles seem to have no activation when I use the motion file as input into the AnalyzeTool. The muscle lengths and passive forces that I get from Matlab are identical to what is coming out of the plotter tool regardless of the activation level I assign in the OpenSim GUI. However, the active forces that the AnalyzeTool is producing from Matlab, correspond to an Activation box showing 0. Therefore, I am assuming the AnalyzeTool I'm running in Matlab is using 0 activation for muscles. I would like to run the AnalyzeTool with muscle activations of 1 and am unsure how to make that happen.
Thank for you quick reply. The problem I'm running into is that the muscles seem to have no activation when I use the motion file as input into the AnalyzeTool. The muscle lengths and passive forces that I get from Matlab are identical to what is coming out of the plotter tool regardless of the activation level I assign in the OpenSim GUI. However, the active forces that the AnalyzeTool is producing from Matlab, correspond to an Activation box showing 0. Therefore, I am assuming the AnalyzeTool I'm running in Matlab is using 0 activation for muscles. I would like to run the AnalyzeTool with muscle activations of 1 and am unsure how to make that happen.
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Matlab interface to x and y data in plotting tool
Anthony,
The AnalyzeTool has two ways that you can specify input, either "coordinates_file" or "states_file". The GUI uses the "states_file" route (calling setStatesFileName()). Maybe you can post a code snippet so we can help you troubleshoot.
Best regards,
-Ayman
The AnalyzeTool has two ways that you can specify input, either "coordinates_file" or "states_file". The GUI uses the "states_file" route (calling setStatesFileName()). Maybe you can post a code snippet so we can help you troubleshoot.
Best regards,
-Ayman
- Anthony Santago
- Posts: 15
- Joined: Mon Feb 15, 2010 11:33 am
Re: Matlab interface to x and y data in plotting tool
Ayman,
I build an AnalyzeTool.xml that has a MuscleAnalysis inside the AnalysisSet. I have set compute_moment to TRUE and insert a number of muscles and a muscle_list.
Within the .xml I have this written
<solve_for_equilibrium_for_auxiliary_states>true</solve_for_equilibrium_for_auxiliary_states>
...
<states_file />
<coordinates_file> C:\Users\asantag\Documents\MoBL Projects\Model\SimTk Model 9-11-2012\wrist_flexion.mot </coordinates_file>
My Matlab code is as follows.
import org.opensim.modeling.*
filePath = 'C:\Users\asantag\Documents\MoBL Projects\Model\SimTk Model 9-11-2012';
cd(filePath);
modelName = 'Upper_Extremity_Model_3_31_12.osim';
Model = Model(modelName);
si = Model.initSystem;
ana = AnalyzeTool('Wrist_Flexion_Moment_AnalyzeTool.xml');
ana.setResultsDir('C:\Users\asantag\Documents\MoBL Projects\Model\SimTk Model 9-11-2012\X0_Y0_Z0');
ana.setModel(Model)
ana.run();
My guess moving forward is that I need to create the states file myself within Matlab. Thank you for your continued help.
I build an AnalyzeTool.xml that has a MuscleAnalysis inside the AnalysisSet. I have set compute_moment to TRUE and insert a number of muscles and a muscle_list.
Within the .xml I have this written
<solve_for_equilibrium_for_auxiliary_states>true</solve_for_equilibrium_for_auxiliary_states>
...
<states_file />
<coordinates_file> C:\Users\asantag\Documents\MoBL Projects\Model\SimTk Model 9-11-2012\wrist_flexion.mot </coordinates_file>
My Matlab code is as follows.
import org.opensim.modeling.*
filePath = 'C:\Users\asantag\Documents\MoBL Projects\Model\SimTk Model 9-11-2012';
cd(filePath);
modelName = 'Upper_Extremity_Model_3_31_12.osim';
Model = Model(modelName);
si = Model.initSystem;
ana = AnalyzeTool('Wrist_Flexion_Moment_AnalyzeTool.xml');
ana.setResultsDir('C:\Users\asantag\Documents\MoBL Projects\Model\SimTk Model 9-11-2012\X0_Y0_Z0');
ana.setModel(Model)
ana.run();
My guess moving forward is that I need to create the states file myself within Matlab. Thank you for your continued help.
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Re: Matlab interface to x and y data in plotting tool
Anthony,
Exactly, you'll need to create the states file yourself in Matlab, save it and then use it as a "states_file" in Matlab (instead of "coordinates_file").
Best of luck,
-Ayman
Exactly, you'll need to create the states file yourself in Matlab, save it and then use it as a "states_file" in Matlab (instead of "coordinates_file").
Best of luck,
-Ayman