Computing muscle fiber lengths using the Python API.

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Axel Koussou
Posts: 56
Joined: Mon Aug 10, 2020 12:07 am

Re: Computing muscle fiber lengths using the Python API.

Post by Axel Koussou » Tue Sep 29, 2020 2:09 am

Hi guys,

Thank you for your responses and your codes.

I am also interested in computing the MTU Length using the Matlab API with the Rajagopal's model.
I succeeded to obtain the length of the semitendinosus tendon unit during a trial of a passive sollicitation of the knee, using this following code :

Code: Select all

%% I got a variable named Data, corresponding to the different coordinates of the models in radian for rotational coordinates and in meters for translational coordinates. 

%Choose the muscle that you want to plot
musclelist = {};
Nmuscle = model.getMuscles().getSize();
for i = 0: Nmuscle-1
	thisName = char(model.getMuscles().get(i).getName());
	musclelist = [musclelist; {thisName} ];
end

string = input('Type a muscle name to plot, or ''exit'' to close: ', 's');
nameIndex = find(cellfun(@(s) ~isempty(strmatch(string, s)), musclelist) == 1);
musclename = char(musclelist(nameIndex));

%Definition
num_timepoints = size(Data,1);
num_coordinates = size(Data,2);
muscle_set = model.getMuscles();
num_muscles = muscle_set.getSize();

%Compute muscle lengths at each time point in the motion by updating the state
musclelengths=zeros(1,num_timepoints);       
for t=1:num_timepoints
    
    for i=0:num_coordinates-1 %38
        model.updCoordinateSet().get(i).setValue(state,Data(t,i+1));
    end
    
    model.realizePosition(state)
    
    
    muscle=muscle_set.get(nameIndex-1);
    musclelengths(1,t)=muscle.getLength(state);  
end

It seems to work. The results obtained correspond to the ones that I get after performing a MuscleAnalysis. Nevertheless, this code takes a lot of time to run (The trial lasts 8.16 seconds so I got 817 frames).

So, I would like to know, if it is also the case for you or if you have any idea to decrease this run time.

Thank you

Regards

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

Re: Computing muscle fiber lengths using the Python API.

Post by Thomas Uchida » Tue Sep 29, 2020 2:39 am

Please see the following lines:

Code: Select all

for i=0:num_coordinates-1 %38
    model.updCoordinateSet().get(i).setValue(state,Data(t,i+1));
end
For each coordinate, you are setting its value and then projecting the model onto the constraint manifold; these unnecessary projections may be causing the slowdown. Depending on the model, this can also result in an incorrect model pose once all the coordinates have been set. (You can check this by reading the values back after they have all been set.) Better would be to do the projection only once, at the end of the loop, using Model::assemble(state). Please see the documentation for the Coordinate::setValue() method: https://simtk.org/api_docs/opensim/api_ ... f2e88d7c42. Note the third argument.

User avatar
Axel Koussou
Posts: 56
Joined: Mon Aug 10, 2020 12:07 am

Re: Computing muscle fiber lengths using the Python API.

Post by Axel Koussou » Tue Sep 29, 2020 4:04 am

Hi Thomas,

Thank you for your quick response.
My run time is considerably reduced. :D

Code: Select all

musclelengths=zeros(1,num_timepoints);       
for t=1:num_timepoints
    
    for i=0:num_coordinates-1 %38
        model.updCoordinateSet().get(i).setValue(state,Data(t,i+1),false);
    end
    

    model.assemble(state)
    model.equilibrateMuscles(state)
    %Ou :
    %model.realizePosition(state)
    
    
    muscle=muscle_set.get(nameIndex-1);%-1 je sais pas pq encore
    musclelengths(1,t)=muscle.getLength(state);  
end
Finally, do you recommend to use equilibrateMuscles or not?

Thanks

Regards

POST REPLY