Page 1 of 1
Muscles Moment extracting using python code
Posted: Mon Nov 26, 2018 12:46 am
by alik
To Whom it may concern,
I am trying to write a python code for simulating several subjects.
When I am working in GUI, I can easily use plot option to extract muscles moment, moment arm, etc. However, I do not know the procedure for extracting MuscleMoments using python code.
I would appreciate if you let me know How I can do this task.
Secondly, I want to run several subjects using python simultaneously. How it can be performed in python? To clarify, I do not want to do sequentially simulation for each subject.
Thirdly, When I am running each CMC simulation, It is automatically generating some files for me. Is it possible to change generated files format and save each simulation in a different directory?
Thanks for all the help,
Sincerely,
Ali.
Re: Muscles Moment extracting using python code
Posted: Mon Nov 26, 2018 10:36 am
by mitkof6
You can calculate the moment arm after you position the model into the pose of interest and realize the state to position level. Please look at the following code, where I sample the moment arm of each muscle with respect to the coordinates that are spanned by the muscle
https://github.com/mitkof6/symbolic_mom ... rm.py#L257
Yes this is possible (see code below for performing SO in OpenSim v4.0). Please clarify what kind of analysis (IK, SO, ID, ...) do you want to run on python.
Code: Select all
def perform_so(model_file, ik_file, grf_file, grf_xml, reserve_actuators,
results_dir):
"""Performs Static Optimization using OpenSim.
Parameters
----------
model_file: str
OpenSim model (.osim)
ik_file: str
kinematics calculated from Inverse Kinematics
grf_file: str
the ground reaction forces
grf_xml: str
xml description containing how to apply the GRF forces
reserve_actuators: str
path to the reserve actuator .xml file
results_dir: str
directory to store the results
"""
# model
model = opensim.Model(model_file)
# prepare external forces xml file
name = os.path.basename(grf_file)[:-8]
external_loads = opensim.ExternalLoads(model, grf_xml)
external_loads.setExternalLoadsModelKinematicsFileName(ik_file)
external_loads.setDataFileName(grf_file)
external_loads.setLowpassCutoffFrequencyForLoadKinematics(6)
external_loads.printToXML(results_dir + name + '.xml')
# add reserve actuators
force_set = opensim.ForceSet(model, reserve_actuators)
force_set.setMemoryOwner(False) # model will be the owner
for i in range(0, force_set.getSize()):
model.updForceSet().append(force_set.get(i))
# construct static optimization
motion = opensim.Storage(ik_file)
static_optimization = opensim.StaticOptimization()
static_optimization.setStartTime(motion.getFirstTime())
static_optimization.setEndTime(motion.getLastTime())
static_optimization.setUseModelForceSet(True)
static_optimization.setUseMusclePhysiology(True)
static_optimization.setActivationExponent(2)
static_optimization.setConvergenceCriterion(0.0001)
static_optimization.setMaxIterations(100)
model.addAnalysis(static_optimization)
# analysis
analysis = opensim.AnalyzeTool(model)
analysis.setName(name)
analysis.setModel(model)
analysis.setInitialTime(motion.getFirstTime())
analysis.setFinalTime(motion.getLastTime())
analysis.setLowpassCutoffFrequency(6)
analysis.setCoordinatesFileName(ik_file)
analysis.setExternalLoadsFileName(results_dir + name + '.xml')
analysis.setLoadModelAndInput(True)
analysis.setResultsDir(results_dir)
analysis.run()
so_force_file = results_dir + name + '_StaticOptimization_force.sto'
so_activations_file = results_dir + name + \
'_StaticOptimization_activation.sto'
return (so_force_file, so_activations_file)
The file format cannot be changed but you can set the result dir of the CMCTool.
Re: Muscles Moment extracting using python code
Posted: Mon Nov 26, 2018 10:45 am
by alik
Thanks for your reply,
About your question for clarifying the tasks, I am trying to do RRA and CMC several times for each subjects.
Just in case for clarifying the idea for myself, In case of Calculating the Moment of each muscles during a gait cycle, I have to derive each muscle moment arm and multiply it with force generated by CMC.
Sincerely Yours,
Ali.
Re: Muscles Moment extracting using python code
Posted: Mon Nov 26, 2018 11:04 am
by mitkof6
I am trying to do RRA and CMC several times for each subjects.
Unfortunately, I don't have python scripts for these two.
Just in case for clarifying the idea for myself, In case of Calculating the Moment of each muscles during a gait cycle, I have to derive each muscle moment arm and multiply it with force generated by CMC.
Yes tau = R * fm, but in CMC you also have the residual forces from the reserve actuators. You can perform inverse dynamics which is the result of what you want to calculate without getting into the trouble to calculate the moment arm and muscle forces.
Re: Muscles Moment extracting using python code
Posted: Wed Nov 28, 2018 10:13 am
by alik
I just wrote this python code for RRA in Ubuntu. However, It is giving me a error :
Segmentation fault (core dumped)
Code: Select all
subject_01_Path = AbsPath + 'subject01/' ## Absolute Path of 1st subject
subject_01_RRA_Path = subject_01_Path + 'rra_multipleSteps/' ## RRA Path
subject_01_Dataset = DatasetPathAbs + 'subject01/' ## Absolute Path of 1st subject
subject_01= opensim.Model(subject_01_Path + 'Subject_01.osim')
RRA = opensim.RRATool()
RRA.setModel(subject_01)
RRA.setDesiredKinematicsFileName(subject_01_Path + 'ik/Results_191/Run_20002.mot')
RRA.setLowpassCutoffFrequency(15)
RRA.setTaskSetFileName(subject_01_RRA_Path + 'gait2392_RRA_Tasks_v191.xml')
RRA.setOutputModelFileName(subject_01_Path + 'Subject_01.osim')
RRA.setAdjustedCOMBody('torso')
RRA.setAdjustCOMToReduceResiduals(True)
RRA.setReplaceForceSet(True)
RRA.setExternalLoadsFileName(subject_01_RRA_Path + 'ModifiedExternalForce.xml')
RRA.setInitialTime(0.199)
RRA.setFinalTime(0.962)
RRA.setMinDT(0.00000001)
RRA.setMaxDT(0.00001)
RRA.setMaximumNumberOfSteps(20000)
RRA.setErrorTolerance(0.00001)
RRA.setOutputPrecision(20)
MainDir = RRA.setResultsDir(subject_01_Dataset + 'RRA/Cycle_01')
RRA.run()
and Also in GUI there is a additional force set files which adds actuator to system. In python
Code: Select all
RRA.setForceSetFiles(subject_01_RRA_Path + 'gait2392_RRA_Actuators_v191.xml')
gives error :
TypeError: in method 'AbstractTool_setForceSetFiles', argument 2 of type 'OpenSim::Array< std::string > const &'
Could you please help me to figure out how I should fix this problems?
Sincerely,
Ali.
Re: Muscles Moment extracting using python code
Posted: Thu Nov 29, 2018 7:34 am
by mitkof6
Code: Select all
arr = ArrayStr()
arr.append(subject_01_RRA_Path + 'gait2392_RRA_Actuators_v191.xml')
RRA.setForceSetFiles(arr)
Re: Muscles Moment extracting using python code
Posted: Thu Nov 29, 2018 7:42 am
by alik
Thanks for the reply,
Is it going to solve my "CORE DUMB ERROR" also?
if it is, Could you please provide a brief explanation about the reason of this error?
Sincerely Yours,
Ali.