Page 1 of 1

Using the BodyKinematics Tool in Python

Posted: Wed Sep 27, 2017 7:38 am
by godfreap
I'm trying to run a BodyKinematics analysis of an IK (.mot) file so that I can quickly obtain center of mass information for the duration of the motion, but I'm having a problem. Namely, it seems as though there is no method to actually run the tool. Now, there *is* a tool.begin() method. But that only seems to process the first frame. And one might think, "what about the tool.step() method?". Well, that doesn't seem to actually do anything. It doesn't seem to matter whether or not you step it or try tool.proceed(), it will iterate for forever and when you include an end iterant and you try print the results, you will still only see the first frame. My code thus far is as follows (ikPath ikFile, outFile, results, are all system-specific paths or file names):

Code: Select all

timeFile = 'irrelevant path and file'
times = []
with open(ikPath + ikFile) as to_read:
    with open(timeFile, 'wb') as tmp_file:
        reader = csv.reader(to_read, delimiter = '\t')
        writer = csv.writer(tmp_file)
        desired_column = [0]
        for row in reader:
            myColumn = list(row[i] for i in desired_column)
            times.append(myColumn)
times = times[9:len(times)] # exclude all the headers
times = [[float(float(j)) for j in i] for i in times]
startTime = min(times)
stopTime = max(times)
curModel = osim.Model(modelPath + modelFile)
curState = curModel.initSystem()
prefix = ikFile.replace('IKResults_File_Name.mot', '_BK')

bk = osim.BodyKinematics(curModel)
bk.setModel(curModel)
bk.setRecordCenterOfMass(True)
bk.setStartTime(startTime[0])
bk.setEndTime(stopTime[0])

curState = curModel.initSystem();
bk.begin(curState) # this apparently only processes the first step
bk.printResults(prefix, results)
I've tried a few different approaches, such as iterating over steps, and using bk.step alone or with proceed, but nothing works. I get no real results - just the first frame, and that's it. For example:

Code: Select all

b = 0
i = 1
while b is 0:
    print i
    b = bk.step(curState, i)
    bk.proceed(bk.step(curState, i))
    i = i + 1


I hope I'm just missing something dumb and obvious here, that working on getting the tool working has blinded me to something in front of my face. It's been a while since I've used Python too so maybe that is contributing here, but either way, I'm at a loss as to why the methods that seem like they'd run the analysis don't seem to do anything at all.

As always, any help is appreciated. Thanks!

Re: Using the BodyKinematics Tool in Python

Posted: Wed Sep 27, 2017 12:27 pm
by tkuchida
I think you want to use the Analyze Tool (https://simtk.org/api_docs/opensim/api_ ... eTool.html) and call run(). The examples on the "Scripting with Matlab" page in the Confluence documentation might be useful resources (https://simtk-confluence.stanford.edu:8 ... ith+Matlab). See, in particular, setupAndRunAnalyzeBatchExample.m.

Re: Using the BodyKinematics Tool in Python

Posted: Thu Sep 28, 2017 5:13 am
by godfreap
Thanks, Tom. That's how I started, but osim.BodyKinematics seemed like it was more built for the process, and wouldn't require a template XML to set the method. I switched methods and now it seems to work (though having to load my IK file through osim.Storage(filename) tripped me up for a second).

Thanks again!