Repeated use of the CMC tool in python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Maxime pernot
Posts: 3
Joined: Wed Jan 31, 2024 3:14 am

Repeated use of the CMC tool in python

Post by Maxime pernot » Thu Jun 06, 2024 5:31 am

Hello everyone,

I am currently trying to do a loop on the CMC tool in order to put the result in a reinforcement learning algorithm.
The CMC analysis can run 3 or 4 times but on the 5th times it will crash .. with the following error:

Windows fatal exception: code 0xc0000374

Main thread:
Current thread 0x00008a64 (most recent call first):
File "c:\path\opensim\4.5-2024-01-10-3b63585\models\gait10dof18musc\scripts\cmc+affichage.py", line 14 in <module>
File "C:\path\anaconda3\envs\opensim_env\Lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec
File "C:\path\anaconda3\envs\opensim_env\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 473 in exec_code
File "C:\path\anaconda3\envs\opensim_env\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 615 in _exec_file
File "C:\path\anaconda3\envs\opensim_env\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 528 in runfile
File "C:\path\Local\Temp\ipykernel_9620\3268241742.py", line 1 in <module>

Restarting kernel...

I didn't succeed to track the error... I tried to run in different IDE (Spyder, Pycharm, VS code) to do the CMC analysis but without success.

Here is the code, I am running to do the CMC analysis :


# Load the modified model
model_path = 'path/to/subject01_metabolics_hip_motors.osim'
model = osim.Model(model_path)

# Add reserve actuators individually to the model
reserve_actuators_path = 'path/to/gait10dof_Reserve_Actuators.xml'
reserve_actuators = osim.ForceSet(reserve_actuators_path)
for i in range(reserve_actuators.getSize()):
model.addForce(reserve_actuators.get(i))

# Initialize the model system
state = model.initSystem()

# Configure and run CMC
cmc_tool = osim.CMCTool()
cmc_tool.setModel(model)
cmc_tool.setName('CMC_HipMotors_Excluding')

# Specify the necessary files for CMC
cmc_tool.setDesiredKinematicsFileName('path/to/subject_adjusted_Kinematics_q.sto')
cmc_tool.setTaskSetFileName('path/to/gait10dof_Kinematics_Tracking_Tasks.xml')
cmc_tool.setExternalLoadsFileName('path/to/subject01_walk_grf.xml')

# Check critical components to ensure they exist
def check_critical_components(model):
required_components = [
'pelvis', 'femur_r', 'tibia_r', 'talus_r', 'calcn_r', 'toes_r',
'femur_l', 'tibia_l', 'talus_l', 'calcn_l', 'toes_l', 'torso',
'ground_pelvis', 'hip_r', 'knee_r', 'ankle_r', 'subtalar_r', 'mtp_r',
'hip_l', 'knee_l', 'ankle_l', 'subtalar_l', 'mtp_l', 'back',
'right_hip_motor', 'left_hip_motor'
]
for component in required_components:
if not model.findComponent(component):
print(f"Component {component} is missing!")

check_critical_components(model)

# Adjust muscle control limits if necessary
force_set = model.getForceSet()
for i in range(force_set.getSize()):
force = force_set.get(i)
if isinstance(force, osim.Muscle):
muscle = osim.Muscle.safeDownCast(force)
muscle.setMinControl(0.02) # Adjust minimum control limits
print(f"Muscle {muscle.getName()} min control set to {muscle.getMinControl()}")

# Add configuration for excluded actuators (if necessary)
excluded_actuators = osim.ArrayStr()
excluded_actuators.append('right_hip_motor')
excluded_actuators.append('left_hip_motor')
cmc_tool.setExcludedActuators(excluded_actuators)

# Run CMC
try:
cmc_tool.run()
print("CMC simulation completed and results are saved in ResultsCMC_HipMotors_Excluding")
except Exception as e:
print(f"An error occurred while running CMC: {e}")

For the Opensim librairy, I am using this one : https://anaconda.org/opensim-org/opensim and I install it using the prompt : conda install opensim-org::opensim

Any help or debugger lead is welcome !

Kind regards

Tags:

User avatar
Mohammadreza Rezaie
Posts: 390
Joined: Fri Nov 24, 2017 12:48 am

Re: Repeated use of the CMC tool in python

Post by Mohammadreza Rezaie » Thu Jun 06, 2024 2:31 pm

Hi, I'm not expert, perhaps some sort of memory leak is happening.

In my workflow, I could resolve this issue by running the tool in this way:

Code: Select all

import os
from subprocess import call

# add opensim bin directory to the system path
os.environ["PATH"] += os.pathsep + 'path\\to\\opensim\\bin'

# setup the cmc_tool

cmc_tool.printToXML('cmc_setup.xml')

with open('cmc_log.log', 'w') as f:
    call(['opensim-cmd', 'run-tool', 'cmc_setup.xml'], stdout=f)
Hope this helps.
Last edited by Mohammadreza Rezaie on Tue Jun 11, 2024 2:08 am, edited 1 time in total.

User avatar
Aaron Fox
Posts: 279
Joined: Sun Aug 06, 2017 10:54 pm

Re: Repeated use of the CMC tool in python

Post by Aaron Fox » Mon Jun 10, 2024 9:07 pm

Hi Maxime,

I've run into similar problems with a number of the tools (e.g. IK, ID) crashing the Python kernel when trying to do repeat runs. For me, the problem seems to occur at the instance of creating the tool (i.e. cmc_tool = osim.CMCTool()). A work-around I've found with this is to either create a number of tools in a list or dictionary and then edit these (i.e. to avoid over-writing), or re-use the tool on different runs without recreating it (i.e. just edit the inputs to the tool like models, coordinate files etc.).

Aaron

User avatar
Maxime pernot
Posts: 3
Joined: Wed Jan 31, 2024 3:14 am

Re: Repeated use of the CMC tool in python

Post by Maxime pernot » Thu Jun 13, 2024 1:45 am

Thanks a lot to both of you !

It seems that the bug come from : cmc_tool = osim.CMCTool().
I have tried the work-around of Aaron and it seems to work !

POST REPLY