Batch Processing of Static Optimization in MatLab question

The 2018 Fall Virtual Workshop will bring together a group of international scholars and OpenSim experts to help each other advance their research using modeling and simulation. This forum is a place to accelerate OpenSim based projects while also buildin
POST REPLY
User avatar
Matthew Prebble
Posts: 4
Joined: Sat Jan 30, 2016 4:17 pm

Batch Processing of Static Optimization in MatLab question

Post by Matthew Prebble » Fri Oct 26, 2018 11:24 pm

I am working on setting up batch processing of static optimization using MatLab R2018b connected to the OpenSim 4.0 API (windows 7 computer). I am getting close but there is one issue I need to work out.

For each trial that I need to run static optimization on I use files of experimental data exported from Visual 3d. Visual 3d runs inverse dynamics and prints out two OpenSim compatible .mot files.
    The first file is a .mot file that has the kinematic data.
      The second file is a second .mot file that has kinetic data (e.g. the vGRF).

      The way I do it using the OpenSim Static Optimization GUI is I
        load my Static Optimization Set Up XML File that has the default set up information.
          the next step is, under the Main Settings, tab I update the input file to the kinematic .mot file for each trial
            The last step is in the Actuators and External Loads Tab I edit the External Loads specification file and I update the "force data file' with the name of the corresponding second .mot file (e.g. the vGRF file)


            My QUESTION:
            Is there a way to update the External Loads XML set up file with the name of the new kinetic (vGRF) .mot file in my MatLab Loop for each trial iteration?

            The only thing I can think to do is have 10 separate set up XML files each with the name of the second .mot file for each trial hard coded in....but I assume there is a more elegant way to just change the name of the file in the XML set up file on each loop.

            thanks

            User avatar
            Nicos Haralabidis
            Posts: 187
            Joined: Tue Aug 16, 2016 1:46 am

            Re: Batch Processing of Static Optimization in MatLab question

            Post by Nicos Haralabidis » Sat Oct 27, 2018 1:31 am

            Hello Matthew,

            The command 'dir' in Matlab outputs a structure with all the files (names) included in a directory - in your case this could be your folder which contains your numerous GRF files. There should then be a method for the ID Tool for letting you set/update/assign the GRF file in row (i) of the output of dir during a loop.

            Something like this I think:

            fileStruct = dir('myFolder');

            idTool = InverseDynamicsTool();

            % Set other stuff you wish

            for i = 1:length(fileStruct)
            idTool.setExternalLoadsFileName(fileStruct(i,1).name);
            end

            Thanks,

            Nicos

            User avatar
            Nicos Haralabidis
            Posts: 187
            Joined: Tue Aug 16, 2016 1:46 am

            Re: Batch Processing of Static Optimization in MatLab question

            Post by Nicos Haralabidis » Sat Oct 27, 2018 1:34 am

            Actually I just realised I totally misinterpreted your question... In principle I think you can do something similar as to what I suggested, but instead updating the ExternalLoads file.

            Nicos

            User avatar
            Matthew Prebble
            Posts: 4
            Joined: Sat Jan 30, 2016 4:17 pm

            Re: Batch Processing of Static Optimization in MatLab question

            Post by Matthew Prebble » Sat Oct 27, 2018 12:04 pm

            thanks for the suggestion. I probably didn't explain my issue well. After looking at it some more I think what I need to do is inside my loop:

            1) load the XML file into Matlab
            2) update one of the values in the xml file
            3) save the xml file with the updated value.

            The XML file is below and I need to update the value in the <datafile/> tag from for each run through the loop. For example from
            - 'Baseline_Gait_1_grf.mot' to 'Baseline_Gait_2_grf.mot '


            I have tried to use xmlRead but can't seem to get MatLab to read in the file for some reason.


            <?xml version="1.0" encoding="UTF-8" ?>
            <OpenSimDocument Version="30516">
            <ExternalLoads name="Ex2">
            <objects>
            <ExternalForce name="Right_Foot_vGRF">
            <!--Name of the body the force is applied to.-->
            <applied_to_body>calcn_r</applied_to_body>
            <!--Name of the body the force is expressed in (default is ground).-->
            <force_expressed_in_body>ground</force_expressed_in_body>
            <!--Name of the body the point is expressed in (default is ground).-->
            <point_expressed_in_body>ground</point_expressed_in_body>
            <!--Identifier (string) to locate the force to be applied in the data source.-->
            <force_identifier>ground_force_v</force_identifier>
            <!--Identifier (string) to locate the point to be applied in the data source.-->
            <point_identifier>ground_force_p</point_identifier>
            <!--Identifier (string) to locate the torque to be applied in the data source.-->
            <torque_identifier>ground_torque_</torque_identifier>
            </ExternalForce>
            <ExternalForce name="Left_Foot_vGRF">
            <!--Name of the body the force is applied to.-->
            <applied_to_body>calcn_l</applied_to_body>
            <!--Name of the body the force is expressed in (default is ground).-->
            <force_expressed_in_body>ground</force_expressed_in_body>
            <!--Name of the body the point is expressed in (default is ground).-->
            <point_expressed_in_body>ground</point_expressed_in_body>
            <!--Identifier (string) to locate the force to be applied in the data source.-->
            <force_identifier>l_ground_force_v</force_identifier>
            <!--Identifier (string) to locate the point to be applied in the data source.-->
            <point_identifier>l_ground_force_p</point_identifier>
            <!--Identifier (string) to locate the torque to be applied in the data source.-->
            <torque_identifier>l_ground_torque_</torque_identifier>
            </ExternalForce>
            </objects>
            <groups />
            <!--Storage file (.sto) containing (3) components of force and/or torque and point of application.Note: this file overrides the data source specified by the individual external forces if specified.-->
            <datafile>D:\OpenSim Data 2\S001-Exported\Baseline\Baseline_Gait_1_grf.mot</datafile>
            <!--Optional motion file (.mot) or storage file (.sto) containing the model kinematics used to transform a point expressed in ground to the body of force application.If the point is not expressed in ground, the point is not transformed-->
            <external_loads_model_kinematics_file />
            <!--Optional low-pass cut-off frequency for filtering the model kinematics corresponding used to transform the point of application. A negative value results in no filtering. The default value is -1.0, so no filtering.-->
            <lowpass_cutoff_frequency_for_load_kinematics>-1</lowpass_cutoff_frequency_for_load_kinematics>
            </ExternalLoads>
            </OpenSimDocument>

            User avatar
            Nicos Haralabidis
            Posts: 187
            Joined: Tue Aug 16, 2016 1:46 am

            Re: Batch Processing of Static Optimization in MatLab question

            Post by Nicos Haralabidis » Sun Oct 28, 2018 6:27 am

            Hello Matthew

            Thanks for your reply.

            I am trying to source some previous code I had for doing something similar, if I remember rightly I used a custom xml_read and _write function from the Matlab repository - not the built in one.

            I cannot attach them for some reason - what's your email address?

            Kind regards,

            Nicos

            POST REPLY