Plotting muscle tendon legth against gait cycle - Python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Jose Ignacio Gil
Posts: 2
Joined: Fri Feb 17, 2017 7:16 am

Plotting muscle tendon legth against gait cycle - Python

Post by Jose Ignacio Gil » Fri Feb 17, 2017 7:31 am

Hi there!

I'm working on a script that exports the muscle-tendon length data into a .CSV file. When I do this manually in the plot GUI, I choose for the X-Axis "Coordinates(Deg.)" and it works fine but when I try to do it in the code ("addAnalysisCurve bla bla bla") it says that "Coordinates(Deg.)" its not an element.

Any ideas in how can I fix this? Is there a simpler way of exporting muscle-tendon length data?

Thank you very much in advance.

Jose.

User avatar
Thomas Uchida
Posts: 1787
Joined: Wed May 16, 2012 11:40 am

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Thomas Uchida » Fri Feb 17, 2017 2:07 pm

I'm unable to find an addAnalysisCurve() method in the OpenSim API. Where did you find this method and what argument(s) does it take? In the Plotter, the "Coordinates(Deg.)" list item will refer to a previous result you've loaded into the GUI (e.g., the .mot file generated by IK).

There are a couple ways of generating muscle–tendon length data. Based on your question, I'm assuming you're seeking these data for a simulation you've already run, in which case you can add a MuscleAnalysis (https://simtk.org/api_docs/opensim/api_ ... lysis.html) and run an analysis using the AnalyzeTool (https://simtk.org/api_docs/opensim/api_ ... eTool.html).

User avatar
Jose Ignacio Gil
Posts: 2
Joined: Fri Feb 17, 2017 7:16 am

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Jose Ignacio Gil » Fri Feb 24, 2017 6:22 am

I found the function in the OpenSim tutorials on plotting and exporting data.

I'll check de MuscleAnalysis tool!

Thank you

PS: Does this works on the Jython Console inside OpenSim?

User avatar
Thomas Uchida
Posts: 1787
Joined: Wed May 16, 2012 11:40 am

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Thomas Uchida » Fri Feb 24, 2017 12:36 pm

Scripting-related information and examples can be found in the "Scripting" section of the documentation: http://simtk-confluence.stanford.edu:80 ... /Scripting.

User avatar
Marcos Alfredo Núñez
Posts: 22
Joined: Thu Sep 01, 2016 1:41 pm

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Marcos Alfredo Núñez » Thu Feb 08, 2018 6:08 pm

As suggested, I created a MuscleAnalysis and ran it through an AnalyzeTool from python, which worked perfectly. The only info I need right now is the muscle-tendon length of a few muscles. The tool, however, creates a myriad of storage files for moments, forces and angles (all of them empty, with only the headers). The only one I care about is xxx_MuscleAnalysis_Length.sto. Is there any way to prevent all these storage files from being created? Even better: is there a way to loop the model's state coordinates, computing the muscle length at every step?

User avatar
Thomas Uchida
Posts: 1787
Joined: Wed May 16, 2012 11:40 am

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Thomas Uchida » Sat Feb 10, 2018 11:31 pm

Is there any way to prevent all these storage files from being created?
Yes, you can iterate through the states and compute only the variables of interest.

User avatar
Marcos Alfredo Núñez
Posts: 22
Joined: Thu Sep 01, 2016 1:41 pm

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Marcos Alfredo Núñez » Wed Feb 14, 2018 8:56 am

Thanks Tom! I've no trouble looping through a model with its ModelVisualizer on, since at every step I call getVisualizer().show(newState). It works exactly as you show in other Matlab examples I found here. However, without it, how can I loop the model through the different states? I see that the show() method calls

Code: Select all

_model.getMultibodySystem().realize(state, SimTK::Stage::Velocity)
before reporting the state to the underlying SimbodyVisualizer. However, from my python wrapper, I can't call realize()

Code: Select all

AttributeError: 'SwigPyObject' object has no attribute 'realize'

User avatar
Thomas Uchida
Posts: 1787
Joined: Wed May 16, 2012 11:40 am

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Thomas Uchida » Wed Feb 14, 2018 11:14 am

Have you tried computeStateVariableDerivatives() (https://simtk.org/api_docs/opensim/api_ ... 3dc9c41d17)? I think that method is wrapped and will end up calling realize.

User avatar
Marcos Alfredo Núñez
Posts: 22
Joined: Thu Sep 01, 2016 1:41 pm

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Marcos Alfredo Núñez » Wed Feb 14, 2018 12:02 pm

Tom:

I just saw you post a very similar answer in another post, but Matlab code. I don't think it's working, muscle lengths are far from what informed in the MuscleAnalysis Tool. Posting here the example function I'm working on:

Code: Select all

def computeLen(mot,mod):
    
    storage = opensim.Storage(mot)
    
    model = opensim.Model(mod)
    model.setUseVisualizer(False)
    state = model.initSystem()
    
    semiten_l = model.getMuscles().get('semiten_l')
    
    lengths_array = []

    for i in range(storage.getSize()):
        dat = storage.getStateVector(i).getData()
        for j in range(model.getNumCoordinates()):
            model.updCoordinateSet().get(j).setValue(state,dat.get(j))
            
        # Here should go model.realizePosition(state), which I lack.
        model.computeStateVariableDerivatives(state)
        lengths_array.append(semiten_l.getLength(state))
    return lengths_array
Am I looping the states correctly?

User avatar
Thomas Uchida
Posts: 1787
Joined: Wed May 16, 2012 11:40 am

Re: Plotting muscle tendon legth against gait cycle - Python

Post by Thomas Uchida » Wed Feb 14, 2018 3:53 pm

It looks like you are assuming the States are in the same order as the Coordinates in the Model's CoordinateSet, which is not true in general.

POST REPLY