Half-way there in creating a setup .xml file with Python API

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Neil Dhir
Posts: 17
Joined: Wed May 27, 2015 6:51 am

Half-way there in creating a setup .xml file with Python API

Post by Neil Dhir » Tue Sep 06, 2016 5:16 pm

Hey guys,

I am attempting to create the scale setup file http://simtk-confluence.stanford.edu:80 ... Setup+File. This is proving somewhat difficult because this is not at all straight-forward. Though through the scant posts that I could find knocking around, I think I am half-way there. This provided some guidance: viewtopicPhpbb.php?f=91&t=3977.

Alas, this is what I have.

Code: Select all

import opensim as osim
import yaml
import os.path

class scaleTools(object):

	def __init__(self, trial):
		# Set data directory
		self.subPath = "/data/DATA/experiments/"
		self.trial = str(trial).zfill(3)
		self.path = os.path.join(self.subPath, 'T' + self.trial + '/')

	def createSetupFile(self):
		with open(os.path.join(self.path + 'meta-' + self.trial + '.yml') , 'r') as f:
			doc = yaml.load(f)
		subjectMeta = doc['subject']
		
		# Get some marker data stuff
		markerData = osim.MarkerData(self.path+'marker_static-'+self.trial+'.trc')
		initial_time = markerData.getStartFrameTime()
		final_time = markerData.getLastFrameTime()

		# Setup scaling
		sct = osim.ScaleTool()

		sct.setSubjectMass(float(subjectMeta['mass']))
		sct.setSubjectHeight(float(subjectMeta['height']))
		sct.setSubjectAge(subjectMeta['age'])
		sct.setName('bob')
		sct.getMarkerPlacer().setStaticPoseFileName(self.path + 'marker_static-' + self.trial + '.trc')
		sct.setStartTime(initial_time)
		sct.setEndTime(final_time)
		sct.setPrintResultFiles()
Just for reference so this all makes sense:

1. I am loading .trc files from a directory.
2. The each experiment has a meta data stored in a .yml file from which I basically just copying pasting into what I hope will be a functioning .xml file.

Now the real problem is I am not entirely sure where to go from the above code, nor really how to execute it. Thus far I have filled out basic methods from the C/C++ API and that seems to work sort of well (though still no .xml file). But one of the users above seems to suggest that one should run

Code: Select all

osim.process.modelScaler()
to actually save the whole thing.

Anyone care to fill in the blanks so I can actually save a setup_file.xml? Most appreciated.

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Half-way there in creating a setup .xml file with Python API

Post by Christopher Dembia » Tue Sep 06, 2016 5:26 pm

Try this:

Code: Select all

sct.printXML('filepath.xml')

User avatar
Neil Dhir
Posts: 17
Joined: Wed May 27, 2015 6:51 am

Re: Half-way there in creating a setup .xml file with Python API

Post by Neil Dhir » Tue Sep 06, 2016 5:44 pm

Hmm...? That method does not appear to exist in the opensim 3.3 API. Have I missed something?

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Half-way there in creating a setup .xml file with Python API

Post by Christopher Dembia » Tue Sep 06, 2016 5:59 pm

It doesn't appear in doxygen, etc. It should exist or all classes that derive from Object, even in 3.3.

User avatar
Neil Dhir
Posts: 17
Joined: Wed May 27, 2015 6:51 am

Re: Half-way there in creating a setup .xml file with Python API

Post by Neil Dhir » Wed Sep 07, 2016 5:35 am

Ok so printXML() does not work.

However I now have:

Code: Select all

"""

Script to quickly deal with XML files for OpenSim.

"""

import opensim as osim
import yaml
import os.path

class scaleTools(object):

	def __init__(self, trial):
		# Set data directory
		self.subPath = "/data/DATA/icra/"
		self.trial = str(trial).zfill(3)
		self.path = os.path.join(self.subPath, 'T' + self.trial + '/')

	def createSetupFile(self):
		with open(os.path.join(self.path + 'meta-' + self.trial + '.yml') , 'r') as f:
			doc = yaml.load(f)
		subjectMeta = doc['subject']
		
		# Get some marker data stuff
		markerData = osim.MarkerData(self.path+'marker_static-'+self.trial+'.trc')
		initial_time = markerData.getStartFrameTime()
		final_time = markerData.getLastFrameTime()

		# Setup scaling
		sct = osim.ScaleTool()

		sct.setSubjectMass(float(subjectMeta['mass']))
		sct.setSubjectHeight(float(subjectMeta['height']))
		sct.setSubjectAge(subjectMeta['age'])
		sct.setName('bob')
		sct.getMarkerPlacer().setStaticPoseFileName(self.path + 'marker_static-' + self.trial + '.trc')
		timeArray = osim.ArrayDouble()
		timeArray.set(0,initial_time)
		timeArray.set(1,final_time)
		sct.getModelScaler().setTimeRange(timeArray)
		sct.getModelScaler().processModel('test.xml')
		# sct.setPrintResultFiles(self.path + 'test.xml')
		# sct.printXML(self.path + 'test.xml')

sc = scaleTools(9)
sc.createSetupFile()
sct.getModelScaler().processModel('test.xml') seems to be the way to go here. Now the only error that remains is:

Code: Select all

NotImplementedError: Wrong number or type of arguments for overloaded function 'ModelScaler_processModel'.
  Possible C/C++ prototypes are:
    OpenSim::ModelScaler::processModel(SimTK::State &,OpenSim::Model *,std::string const &,double)
    OpenSim::ModelScaler::processModel(SimTK::State &,OpenSim::Model *,std::string const &)
    OpenSim::ModelScaler::processModel(SimTK::State &,OpenSim::Model *)
The arguments for processModel() are given https://simtk.org/api_docs/opensim/api_ ... 1b85ed4ff2, the issue being now I am not entirely sure how to relate these to what I have.

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Half-way there in creating a setup .xml file with Python API

Post by Christopher Dembia » Wed Sep 07, 2016 9:45 am

Oops! The method is printToXML.

You should be able to use tab completion in ipython to see a list of the methods available on any object.

User avatar
Neil Dhir
Posts: 17
Joined: Wed May 27, 2015 6:51 am

Re: Half-way there in creating a setup .xml file with Python API

Post by Neil Dhir » Wed Sep 07, 2016 10:21 am

I can confirm that tab completion (in sublime albeit) does not work :)

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Half-way there in creating a setup .xml file with Python API

Post by Christopher Dembia » Wed Sep 07, 2016 11:28 am

Yeah; Sublime is just a text editor. When I write python code, I usually have 3 windows open:

1. A text editor to write the .py files.
2. A terminal window running ipython, so that I can use tab completion to explore what I can do, and also to test different functions.
3. A terminal window to run the .py files.

If you use a python IDE like PyCharm or Spyder, those 3 parts are presented in one window, similar to MATLAB.

User avatar
Neil Dhir
Posts: 17
Joined: Wed May 27, 2015 6:51 am

Re: Half-way there in creating a setup .xml file with Python API

Post by Neil Dhir » Thu Sep 08, 2016 11:36 am

You can make sublime into a fully fledged IDE (which is what I have, though admittedly it only seems to work with established modules). But I get your point anyway. Thanks for help, if anyone wants the full code, I am happy to post it.

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Half-way there in creating a setup .xml file with Python API

Post by Christopher Dembia » Thu Sep 08, 2016 11:46 am

Ah I did not know that about Sublime!

POST REPLY