mean / average results from a study

Hi everyone!

I have an inverse dynamics study in which I do some calculations and export the results as vectors using the AnyOutputFile function, which works fine. This function is located inside my inverse dynamics study.

I would now like to calculate the arithmetic mean of these results for the whole trial and export those as well (so I don’t need do this in Excel). I tried using the “mean” function but without success. Unfortunately the forum does not allow to search for “mean”, so I didn’t find any thread answering my question.

I would be grateful for any suggestions how to tackle this.

best regards
Florian

Hi Florian,

I’d like to help you regarding the problem.

Let’s see the description of AnyOutputFile in the AnyScript Reference Manual.

You can find a sentence like this: “The output-file object distinguishes between values that change during the operation and constants.”
It means that AnyOutputFile can only write the time-varying values or constants during the running time of AnyBodyStudy operations.
So the mean values can’t be written using AnyOutputFile because the mean of a certain property can be evaluated after the end of the whole operation.

So, if you want to evaluate the mean(average) values of some properties, then you have to define your own AnyBodyStudy instances for post-processing.
Before to do this, you also have to define an AnyOutputFun object inside the existing AnyBodyStudy object.
If you are interested in AnyOutputFun object, please refer to the reference manual.
I think that it may be easier for us to use a GaitLowerExtremity example to explain this.

Let’s assume that we should find a mean value of the right hip flexion moments of a whole simulation time.

Step1) Make an AnyOutputFun object inside the existing AnyBodyStudy object. Write this code inside the definition of InverseDynamicStudy.

    AnyBodyStudy InverseDynamicStudy = {
      AnyFolder &C3DData=..ModelSetup.C3DFileData ;
      AnyFolder &HumanModel=.HumanModel.BodyModel;
      #include "Mannequin.any" 
      
      //Environment around the human
      AnyFolder EnvironmentModel ={
        
        AnyFolder DrawC3DMarkers = {};
        //Model of the floor and force plates this is where the force plates are defined
        AnyFolder &HumanModelRef=..HumanModel.BodyModel;
        //This environment file makes use of automatic detecteion of which foot are incontact with which plate 
        #include "EnvironmentAutoDetection.any"
        //This environment file has no automatic detecteion of which foot are incontact with which plate it has to be set manually
        //#include "Environment.any"
      };
      
      //Connection between environment and the human    
      AnyFolder ModelEnvironmentConnection = {
        //Drivers for the model
        #include "JointsAndDriversOptimized.any"      
      };
      
      
      InverseDynamics.Criterion.UpperBoundOnOff=Off;
      nStep=Main.ModelSetup.nStep;
      Gravity ={0.0, -9.81,0};
      tStart = Main.TrialSpecificData.tStart+2*Kinematics.ApproxVelAccPerturb; 
      tEnd = Main.TrialSpecificData.tEnd-2*Kinematics.ApproxVelAccPerturb;
      //#include "GCDOutput.any"
      
      //-->Test 
      AnyOutputFun Right_HipFlexion =
      {
        Val = Main.Studies.HumanModel.BodyModel.SelectedOutput.Right.Leg.JointMomentMeasure.HipFlexion;
      };
      //<--Test
    };

Step2) Define your own AnyBodyStudy object for post-processing. Let’s define it after ‘AnyOptKinStudy LoadParametersOptimizationResults’.

    AnyBodyStudy PostProcess_For_InverseDynamicStudy =
    {
      nStep = Main.Studies.InverseDynamicStudy.nStep;
      Gravity = Main.Studies.InverseDynamicStudy.Gravity;
      tStart = Main.Studies.InverseDynamicStudy.tStart;
      tEnd = Main.Studies.InverseDynamicStudy.tEnd;
      [COLOR="Red"]AnyVar Avg_Right_HipFlexion = mean(Main.Studies.InverseDynamicStudy.Right_HipFlexion());
    };[/COLOR]

Please be aware of that this study is for just calculation for mean values.

Step3) When you write above codes properly, then press F7 to reload the model.
Then you can see your own ‘PostProcess_For_InverseDynamicStudy’ in the left of AnyBody Modeling System.
Then, after you ran the model by running ‘InverseDynamicAnalysisSequence’ macro, then you should run the InitialConditions of ‘PostProcess_For_InverseDynamicStudy’ by manually.
Then you can find the average value at ‘Avg_Right_HipFlexion’.

Step4) If you want to make an automatic process, then you should modify “Sequence2.anymcr” file in the GaitLowerExtremity folder.
You should modify it like this one:

runmacro   "SaveMacroOperation-Load.anymcr"
runmacro   "RunMacroOperation-Load.anymcr"   
operation Main.Studies.InverseDynamicStudy.InitialConditions
run
operation Main.Studies.HumanModel.Calibration.CalibrationSequence
run
operation Main.Studies.InverseDynamicStudy.InverseDynamics
run
operation Main.Studies.PostProcess_For_InverseDynamicStudy.InitialConditions
run

And I attached the zip compressed file of the above explanations.

I hope this may help you.

Best regards,
Moonki

Hi Moonki,

thank you very much for your extensive answer!
That is exactly what I was looking for - although it proves to be a bit more complex than I initially thought.
I’ll test it when I’m back in my office and have access to AnyBody again.

Cheers
Florian

Hi Moonki,

thanks again for your help!

Everything works as you described and I got so far as calculating the mean values. However, I did not succeed in writing them to a file yet. I tried simply putting an AnyOutputFile inside the “PostProcessForInverseDynamicStudy” and a few other combinations, but with no effect. When I have a look at the AnyOutputFile funciton in the model tree everything looks fine (filename, actual content of the vairable), but the file is just not written.
Could you give me a hint how to solve this?

Best regards
Florian

Hi Florian,

Please try to modify “Sequence2.anymcr” file as follows:

runmacro   "SaveMacroOperation-Load.anymcr"
runmacro   "RunMacroOperation-Load.anymcr"   
operation Main.Studies.InverseDynamicStudy.InitialConditions
run
operation Main.Studies.HumanModel.Calibration.CalibrationSequence
run
operation Main.Studies.InverseDynamicStudy.InverseDynamics
run
operation Main.Studies.PostProcess_For_InverseDynamicStudy.[COLOR="Red"]Kinematics[/COLOR]
run

I hope this may help you.

Best regards,
Moonki

Hi Moonki,

thanks a lot! It works perfectly now.

Cheers
Florian

Hi,

I want to use AnyOutputFile to output data (.txt) and paste in excel.

In the output file, the first line should be time, 2n,3rd… line should be forces or other outputs.

t1 Fa1 Fb1 Fc1
t2 Fa2 Fb2 Fc2
t3 …

It works well for joint forces, but not for the predefined data in the c3d file.

If I tried to output the some data such as hip angles and ground reaction force from c3d files, these data are aligned in a different way as joint forces. I cannot simply copy and paste these data in excel along with time. I know this is because that these data are not generated through time step.

So, if I want to output the data from c3d files and put these data in the same output file and in the same alignment as joint forces in order to paste easily in excel, how can I do?
The output file should be like this (the red ones are from c3d files):

t1 Fa1 Fb1 Fc1 Gf1
t2 Fa2 Fb2 Fc2 Gf2
t3 …

Many thanks.

Cheers,
Jerry