Scripting in Matlab for Scaling of Model

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Scripting in Matlab for Scaling of Model

Post by David John Saxby » Mon Feb 25, 2013 11:17 pm

Hello,

I'm using v.3.0.1 and attempting to using Scripting + Matlab to run a scaling process within a larger Matlab data processing pipeline.
Reason for scripting instead of xml creation/editing: xml parsing in Matlab creates problems in OpenSim.

I used the example 'setupAndRunIKBatchExample.m' as a template to understand the process. Made the necessary changes where needed (ex: hard code the path and file of the generic model to the xml setup file, etc...).

Where I run into trouble is when trying to get/set the location of the marker data to be used in this instance of scaling.

Within larger code:
scaleTool.setSubjectMass(data.Mass);
scaleTool.setSubjectHeight(data.Height);
scaleTool.setName(name);
scaleTool.setMarkerDataFileName(fullpath);
scaleTool.setStartTime(initial_time);
scaleTool.setEndTime(final_time);
scaleTool.setOutputMotionFileName([results_folder '\' name '_linearScale.mot']);

Highlighted in red is the issue. This doesn't seem to be kosher for the scale tool. I'm a complete beginner with OpenSim's scripting tool, so what should I do to get and set these values?

If anyone has any advise I don't mind some experimental solutions?

Thanks very much,

David

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

Re: Scripting in Matlab for Scaling of Model

Post by Ayman Habib » Tue Feb 26, 2013 11:51 am

Hi David,

There are two places in the ScaleToll where you can specify a trc file for marker data:
1. under "Marker data for measurements".
2. under "Marker data for static pose".

Please indicate which one you're trying to use and I'll point you to the correct syntax.

Best regards,
-Ayman

User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Re: Scripting in Matlab for Scaling of Model

Post by David John Saxby » Tue Feb 26, 2013 4:55 pm

Hi Ayman,

Thanks very much for the quick response (I know you get a lot of queries).

Can you provide me both instances of the syntax? I will only need to point to the trc file for scaling the bodies (measurements), but I'm interested in learning how to use scripting more generally so I can extend my script to other operations such as IK, ID, and Muscle analysis.

I'm not experienced with the API and learn best via example. Is their another source for examples in addition to the set scripts provided with the v.3.0.1 release?

Best,

David

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

Re: Scripting in Matlab for Scaling of Model

Post by Ayman Habib » Tue Feb 26, 2013 5:20 pm

Hi David,

We're building the set of scripts and we sure would welcome more contributions from our users. Also the C++ API examples should be a good source since the syntax is very similar (in most cases).

For the ScaleTool:

Code: Select all

scaleTool.getModelScaler().setMarkerFileName(filepath)            // trc file for measurements
scaleTool.getMarkerPlacer().setStaticPoseFileName(filepath)      // trc file for static pose
Hope this helps,
-Ayman

User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Re: Scripting in Matlab for Scaling of Model

Post by David John Saxby » Tue Feb 26, 2013 7:03 pm

Hi Ayman,

Thanks a lot!

I'll finish this code and re-up to your site. We have a clinical gait analysis group within the SimTk site, which will be the future repository and distributor for some code.

In the short run, any convenient place to deposit this script?

David

User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Re: Scripting in Matlab for Scaling of Model

Post by David John Saxby » Tue Feb 26, 2013 7:35 pm

Sorry one more,

Time range for scaling in my generic setup file is empty, as I plan to change this value depending on file length. I suppose I could hard code if there is not a programmatic solution.

scaleTool.getModelScaler().setTimeRange(final_time - initial_time)

??? No method 'setTimeRange' with matching
signature found for class
'org.opensim.modeling.ModelScaler'.

.getTimeRange() returns the empty value from the xml, as it should.

Is setTimeRange not operational? Should I use a different method?

David

User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Re: Scripting in Matlab for Scaling of Model

Post by David John Saxby » Sun Mar 03, 2013 8:03 pm

Hello again,

So I could get the scaleTool.getModelScaler().setTimeRange to work. I don't know if I'm just using this method incorrectly, or if there is something wrong with its current state.

I hard coded the time range in the template xml, so unless I have files shorter than 0-0.1 seconds I think I'm ok.

I have another issue though.

Running the tool.

K>> scaleTool.run();
??? Assignment of char to Java arrays is not
allowed

Error in ==> run at 24
if ispc, script(script=='/')='\'; end


The name of the tool is constructed earlier in the script:

outfile = ['Setup_LinearScale' name '.xml'];


% write the tool
scaleTool.print([trc_data_folder '\' outfile]);

Any ideas?

David

User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Re: Scripting in Matlab for Scaling of Model

Post by David John Saxby » Tue Mar 05, 2013 6:30 pm

Hey Guys,

Thought I would share my solution up to this point.

Earlier in this thread, I didn't realize the time range needed to be an array double and not an integer.

So I first define the range:

% From the trc file
initial_time = markerData.getStartFrameTime();
final_time = markerData.getLastFrameTime();

% Create an array double and apply the time range
TimeArray = ArrayDouble();
TimeArray.set(0,initial_time);
TimeArray.set(1,final_time);

% set the time range
scaleTool.getModelScaler().setTimeRange(TimeArray);


So the xml now contains the time range for the specific trial of interest. This logic (not exact syntax) should be extensible to IK, ID, etc...BUT I haven't tried yet.

My problem remains that when I attempt to execute the tool:
% Run Scale Tool
scaleTool.run();

It appears that there is some problem passing strings to java. What is odd is I define the string for the scale tool:

% Setup file for the linear scaling
outfile = [data.Name '_Setup_LinearScaling.xml'];
% write the tool
scaleTool.print([trc_data_folder '\' outfile]);

These variables appear as java strings in the variable editor. Also, when I pause my code and run the xml's manually in the GUI...it WORKS!

I understand the GUI to be calling the same underlying code as API scripting, so what is it about my code that gives me the error? Or phrased differently, why can the GUI handle the xml my code generates but not the API script?

Hope the first bit helps someone.

David

User avatar
David John Saxby
Posts: 83
Joined: Mon May 09, 2011 8:39 pm

Re: Scripting in Matlab for Scaling of Model

Post by David John Saxby » Mon Mar 11, 2013 1:13 am

Hey Guys,

Solution!

I thought the scaleTool had a run method due to an assumption on my part. I thought scaleTool was a member of Tool, similar to InverseKinematicsTool and DynamicsTool. Not so. Check the class hierarchy.

To execute the scale operation the method is process model scaler, not run or any other method.

The input includes the subject path, so make sure your file names are just the filename without the path included, or the method will add the path in front of path\filename and problems will arise (i.e path\path\filename). If it returns a zero check the print log.

The same method is used for the marker placer as well.

Note that there is no orthogonality between methods. The methods to run IK, ID, scale, etc... in terms of names for similar operations (i.e setting filenames, models, etc...) are not the same across methods.

Best,

David

POST REPLY