Configuring IMU weights for IK from Python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Mario Martinez-Zarzuela
Posts: 4
Joined: Thu Nov 05, 2020 12:31 pm

Configuring IMU weights for IK from Python

Post by Mario Martinez-Zarzuela » Tue Sep 27, 2022 8:18 am

Hi!

I am trying to set IMU weights for IK from the Python API. The code run without errors, but I feel my code is not working since setting a weight to 0 does not have the expected result. Could anyone help with this? Do you have some sample code? Thanks!

Code: Select all

imuIK = osim.IMUInverseKinematicsTool()
imu_weight = osim.OrientationWeight('tibia_r',float(value))
imuIK.upd_orientation_weights().adoptAndAppend(imu_weight)
imuIK.run()
Cheers,
Mario

Tags:

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

Re: Configuring IMU weights for IK from Python

Post by Ayman Habib » Tue Sep 27, 2022 9:18 am

Hi Mario,

If this is the whole code snippet you're using then the problem is not with the weights but with configuring the tool, since there's no model specification nor IMU data files to solve. Please consult the python example here https://github.com/opensim-org/opensim- ... ing.py#L40
(also included in the distribution under the {resources directory}/Code/Python/OpenSenseExample and let us know what you find out.

Best regards,
-Ayman

User avatar
Mario Martinez-Zarzuela
Posts: 4
Joined: Thu Nov 05, 2020 12:31 pm

Re: Configuring IMU weights for IK from Python

Post by Mario Martinez-Zarzuela » Mon Oct 03, 2022 7:02 am

Hi Ayman,

Thank you for your answer! It was not the full code. We have a different function for model selection and IMU placement.

Code: Select all

	def motCreation(self, modelo):
		imuIK = osim.IMUInverseKinematicsTool()
		imuIK.set_model_file(modelo)
		imuIK.set_orientations_file(self.INPUT_FILE)
		imuIK.set_sensor_to_opensim_rotations(self.SYS_ROTACION)
		file = self.INPUT_FILE.split("/")[-1]
		folder = self.INPUT_FILE.replace(file,"")
		outputfolder = folder+file[:3]
		imuIK.set_results_directory(outputfolder)
		if self.IMU_WEIGHTS:
			equiv=self._getEquival()
			for key, value in self.IMU_WEIGHTS.items():
				imu_name = equiv[key]+"_imu"
				imu_weight = osim.OrientationWeight(imu_name,float(value))
				imuIK.upd_orientation_weights().adoptAndAppend(imu_weight)
		imuIK.set_time_range(0,0.016666)
		imuIK.run()
The code runs fine and generate a .mot file if we comment out the following block.

Code: Select all

 
 if self.IMU_WEIGHTS:
	equiv=self._getEquival()
	for key, value in self.IMU_WEIGHTS.items():
		imu_name = equiv[key]+"_imu"
		imu_weight = osim.OrientationWeight(imu_name,float(value))
		imuIK.upd_orientation_weights().adoptAndAppend(imu_weight) 
However, if we include this block of code the imuIK.run() does not return any error, nor generates a .mot file. In the code above, in each call to osim.OrientationWeight(), we simply pass as arguments: ("tibia_l_imu", 1.0) and so on... (have debugged to double check this is how data is entering the API).
Could us be using incorrectly imuIK.upd_orientation_weights().adoptAndAppend(imu_weight)?

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

Re: Configuring IMU weights for IK from Python

Post by Ayman Habib » Tue Oct 04, 2022 9:59 am

Hi Mario,

I suspect the problem could be that the weight-set is never cleared, so while you're appending new weights to the end of the set, the old values are reused (since adoptAndAppend doesn't check for duplicate keys). Can you check the size of the weightset to make sure it matches your expectations?

Let us know what you find out,
-Ayman

User avatar
Lukas H
Posts: 2
Joined: Tue Dec 05, 2023 7:01 am

Re: Configuring IMU weights for IK from Python

Post by Lukas H » Tue Jan 23, 2024 11:43 am

I had the same problem as Mario and found a solution.
So just in case, anybody else has still the same problem.

When creating the OrientationWeightSet in Python by using the method adoptAndAppend to pass the OrientationWeight objects, it seems like the OrientationWeight objects are lost (I guess they are killed by the GarbageCollector or something).

When using cloneAndAppend instead everything works fine.

Here the "corrected" code snipplet from Mario.

Code: Select all

 if self.IMU_WEIGHTS:
	equiv=self._getEquival()
	for key, value in self.IMU_WEIGHTS.items():
		imu_name = equiv[key]+"_imu"
		imu_weight = osim.OrientationWeight(imu_name,float(value))
		imuIK.upd_orientation_weights().cloneAndAppend(imu_weight) 

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

Re: Configuring IMU weights for IK from Python

Post by Ayman Habib » Tue Jan 23, 2024 12:30 pm

Thanks for reporting, Lukas.

Much appreciated.
-Ayman

POST REPLY