Running anybody model using python

Hi,

I am trying to run a model through python. The thing is i need to run the same model multiple times with different inputs.
My input is a text file. I use AnyInputFile to load the input data and then use AnyOutputFile to output the muscle activity for all muscles. Now, the input and output filename changes and i will have to run same model for multiple versions of the input and output.
Therefore, i am using python to run and I am running into problems. I am inputting the input filename using setvalue command. I am pasting my sample code underneath.

input_list = ['D:\Yokhesh\data\input.txt','D:\Yokhesh\data\input1.txt',]
macrolist = [
mc.Load("move_test.Main.any"),
mc.SetValue("Main.Model.datafun.FileName", input_list),
mc.OperationRun("Study.InverseDynamics"),
mc.Dump("Main.Study.Output.Model.BodyModel.Right.ShoulderArm.Mus.biceps_brachii_caput_breve.Fout"),
]
app = AnyPyProcess()
parameter_study_macro = AnyMacro(macrolist, number_of_macros= len(input_list) )
output = app.start_macro(parameter_study_macro)

Firstly, the python output says that the operation has failed and the output log is as follows:

Macro command > load "move_test.Main.any"

Loading Main : "D:\move_test.Main.any"
Scanning...
Parsing...
Constructing model tree...
Linking identifiers...
Evaluating constants...
Configuring model...
Evaluating model...
Loaded successfully.
Elapsed Time : 12.601000

Macro command > classoperation Main.Model.datafun.FileName "Set Value" --value="D:\Yokhesh\data\input.txt"

ERROR(SCR.EXP2) : Edit std::string : 'D' : Illegal type conversion to 'AnyFileVar'

||============================Edit Value==============================
||
|| FileName = "..........\Yokhesh\data\input.txt"
|| FileName =||====================================================================
||==========================Message Dialog============================
|| Error : Set value is not allowed for given model running state.
|| Press : < OK (Any Key) > : OK
||====================================================================

Macro command > operation Study.InverseDynamics

Macro command > run

Secondly, the operation takes very long to complete. If i run the model in anybody, it takes 120 seconds to complete. But, if i run it in python, it takes close to 1300 seconds to give me an output.

Could you please take a look at my error and let me know if i can use python to run the code multiple times while just changing my input and output file for each iteration ? Also, could i improve the speed of execution ?

Thanks,
Yokhesh

Hi @yokhesh13

I looked into your problem with the error and it is not clear to me why it happens.
I've raised a bug report on the issue and would like to know what version of AMS you are using.

Regarding the speed of execution it might be related to this type conversion error but it is hard to say for me.
Do the slow models also show errors or warnings similar?

Best regards,
Bjørn

Hi Bjorn,

Thanks a lot for the response.
I am using Anybody 7.2.
No, the model tends work when i dont input any values through python using Setvalue command.
When I just load and perform inverse dynamics operation through python, it works. But its very slow.
However, when i use Setvalue to input values which is very essential for what i am trying to achieve, the python gives the above error.
Please let me know if you need further information.

Thanks,
Yokhesh

Hi @yokhesh13

As mentioned, there is probably a bug in assigning to a AnyFileVar from python. That will be fixed for the next release.

In the meantime, here is a workaround you can use:

AnyInputFile datafun= {
      AnyString Fname = DesignVar( "dummyFile.txt" );
      FileName = Fname;
    };

The trick is to a let python assign to a normal AnyString and then point that to the FileName variable.
To avoid a load error you should create the "dummyFile.txt" - it can just be a empty file.

When using the SetValue class from python it is always a good idea to run the UpdateValues() class just before you have your OperationRun in your macro. That ensures that all the values you have touched are also updated throughout the model tree.

I noticed a lot of latency when I ran into the type conversion error you found above. Avoiding that should make your models run 1-1 when using the gui or python.

Best regards,
Bjørn

This topic was automatically closed 125 days after the last reply. New replies are no longer allowed.