Repeated use of the CMC tool in python
Posted: 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
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