anypytools.macro_commands

Created on Wed Apr 29 08:21:46 2015.

@author: mel

class anypytools.macro_commands.Dump(var, include_in_macro=None)[source]

Create a Dump classoperation macro command.

Parameters
  • var (str or list of str) – The anyscript values to create a ‘Dump’ macro command for

  • include_in_macro (int or list of int) – Specifices in which macros [0,1,2….NumberOfMacros] to include the dump command. If None, the command is included in all macros.

Examples

>>> Dump('Main.Study.myvar1')
classoperation Main.Study.myvar1 "Dump"

Only include the dump command in the two first macro

>>> mg = AnyMacro(number_of_macros = 5)
>>> mg.append(Load('mymodel.main.any'))
>>> mg.append(Dump('Main.Study.myvar1', include_in_macro = [0,1]))
>>> mg
[['load "mymodel.main.any"', 'classoperation Main.Study.myvar1 "Dump"'],
 ['load "mymodel.main.any"', 'classoperation Main.Study.myvar1 "Dump"'],
 ['load "mymodel.main.any"'],
 ['load "mymodel.main.any"'],
 ['load "mymodel.main.any"']]
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.Load(filename, defs={}, paths={})[source]

Create a load macro command.

Parameters
  • filename (str) – Path of the file to load

  • defs (dict) – Dictionary of defines statements to set during load

  • paths (dict) – Dictionary of path staements to set during load

Examples

>>> Load('model.main.any')
load "model.main.any"
>>> paths = {'DATA':'c:/MyModel/Data'}
>>> defines = {'EXCLUDE_ARMS':None, 'N_STEP':20}
>>> Load('c:/MyModel/model.main.any', defines, paths)
load "c:/MyModel/model.main.any" -def EXCLUDE_ARMS="" -def N_STEP="20" -p DATA=---"c:/MyModel/Data"
>>> mcr = AnyMacro( Load('model_{id}.main.any'), counter_token = '{id}')
>>> mcr.create_macros(3)
[[u'load "model_0.main.any"'],
 [u'load "model_1.main.any"'],
 [u'load "model_2.main.any"']]
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.LoadDesign(operation, filename)[source]

Create a Load Design classoperation macro command.

Parameters
  • operation (str) – The AnyScript operation

  • filename (str) – The file in which to load the design from

Examples

>>> LoadDesign('Main.MyStudy.Kinematics', 'c:/design.txt')
classoperation Main.MyStudy.Kinematics "Load design" --file="c:/design.txt"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.LoadValues(filename)[source]

Create a Load Values classoperation macro command.

Parameters

filename (str) – The anyset file to load the values from.

Examples

>>> LoadValues('c:/values.anyset')
classoperation Main "Load Values" --file="c:/values.anyset"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.MacroCommand(command)[source]

Class for custom macro commands.

This class also serves as base class for other macro commands.

Examples

>>> mc = MacroCommand('operation "Main.MyStudy.InverseDynamics"') )
>>> print(mc)
operation "Main.MyStudy.InverseDynamics"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.OperationRun(operation)[source]

Create a macro command to select and run an operation.

Examples

>>> OperationRun('Main.MyStudy.Kinematics')
>>> pprint( mg.generate_macros())
operation Main.MyStudy.Kinematics
run
>>> mg = AnyMacro(Load('my_model.main.any'),
               OperationRun('Main.MyStudy.Kinematics'))
>>> mg.create_macros()
[[u'load "my_model.main.any"', u'operation Main.MyStudy.Kinematics', u'run']]
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.SaveData(operation, filename)[source]

Create a Save Data classoperation macro command.

This macro operation will save all data from a study to a HDF5 file.

Parameters
  • operation (str) – Operation to save the h5 from

  • filename (str) – The anyset file to save the values to

Examples

>>> SaveData('Main.Study', 'output.anydata.h5')
classoperation Main.Study.Output "Save data" --type="Deep" --file="output.anydata.h5"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.SaveDesign(operation, filename)[source]

Create a Save Design classoperation macro command.

Parameters
  • operation (str) – The AnyScript operation

  • filename (str) – The filename in which to save the design

Examples

>>> SaveDesign('Main.MyStudy.Kinematics', 'c:/design.txt')
classoperation Main.MyStudy.Kinematics "Save design" --file="c:/design.txt"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.SaveValues(filename)[source]

Create a Save Values classoperation macro command.

Parameters

filename (str) – The anyset file to save the values to

Examples

>>> SaveValues('c:/values.anyset')
classoperation Main "Save Values" --file="c:/values.anyset"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.SetValue(var, value)[source]

Create ‘Set Value’ classoperation macro command.

Parameters
  • var (string) – An AnyScript variable.

  • value (number or list of number) – A value or list of values to assign to the AnyScript variable.

Examples

Set a single values:

>>> SetValue('Main.Study.myvar1', 23.1)
classoperation Main.Study.myvar1 "Set Value" --value="23.1"
>>> SetValue('Main.Study.myvar2', np.array([2,3,4]))
classoperation Main.Study.myvar2 "Set Value" --value="{2,3,4}"

Set variable across different macros

>>> SetValue('Main.Study.myvar1',[1,2,3])
classoperation Main.Study.myvar1 "Set Value" --value="1"
classoperation Main.Study.myvar1 "Set Value" --value="2"
classoperation Main.Study.myvar1 "Set Value" --value="3"

Here is a elaborate example:

>>> mg = AnyMacro( Load('MyModel.main.any'),
                 SetValue('Main.Study.myvar1',[1,2]),
                 OperationRun('Main.Study.InverseDynamics') )
>>> mg.number_of_macros = 2
[['load "MyModel.main.any"',
  'classoperation Main.Study.myvar1 "Set Value" --value="1"',
  'operation Main.Study.InverseDynamics',
  'run'],
 ['load "MyModel.main.any"',
  'classoperation Main.Study.myvar1 "Set Value" --value="2"',
  'operation Main.Study.InverseDynamics',
  'run']]

If we generate more macros that there are values then the values are just repeated

>>> mg.number_of_macros = 4
>>> mg
[['load "MyModel.main.any"',
  'classoperation Main.Study.myvar1 "Set Value" --value="1"',
  'operation Main.Study.InverseDynamics',
  'run'],
 ['load "MyModel.main.any"',
  'classoperation Main.Study.myvar1 "Set Value" --value="2"',
  'operation Main.Study.InverseDynamics',
  'run'],
 ['load "MyModel.main.any"',
  'classoperation Main.Study.myvar1 "Set Value" --value="1"',
  'operation Main.Study.InverseDynamics',
  'run'],
 ['load "MyModel.main.any"',
  'classoperation Main.Study.myvar1 "Set Value" --value="2"',
  'operation Main.Study.InverseDynamics',
  'run']]
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.SetValue_random(var, frozen_distribution, default_lower_tail_probability=0.5)[source]

Create a ‘Set Value’ macro command from a distribution.

The value is connected to a distibution in scipy.stats.distributions.

Parameters
  • var (str) – An AnyScript variable.

  • frozen_distribution (<scipy.stats.distributions.rv_frozen>) – A frozen distribution from scipy.stats.distributions

  • default_lower_tail_probability (float) – The lower tail probability of the default value. Defaults to 0.5 which is the mean value.

Examples

Creating normal macros will use the default values. Usually the 50 percentile (mean values).

>>> np.random.seed(1)
>>> from scipy.stats.distributions import logistic, norm
>>> log_dist = logistic( loc= [1,3,4],scale = [0.1,0.5,1] )
>>> norm_dist = norm( loc= [0,0,0],scale = [0.1,0.5,1] )
>>> cmd = [SetValue_random('Main.MyVar1', log_dist), SetValue_random('Main.MyVar2', norm_dist) ]
>>> mg = AnyMacro(cmd, number_of_macros = 3)
>>> mg
[['classoperation Main.MyVar1 "Set Value" --value="{0,0,0}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1,3,4}"'],
 ['classoperation Main.MyVar1 "Set Value" --value="{0,0,0}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1,3,4}"'],
 ['classoperation Main.MyVar1 "Set Value" --value="{0,0,0}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1,3,4}"']]

Values can be sampled randomly in a ‘Monte Carlo’ fashion. Note that the first value is still the defaut mean values

>>> mg.create_macros_MonteCarlo()
[['classoperation Main.MyVar1 "Set Value" --value="{0,0,0}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1,3,4}"'],
 ['classoperation Main.MyVar1 "Set Value" --value="{-0.0209517840916,0.291902872134,-3.68494766954}"',
  'classoperation Main.MyVar2 "Set Value" --value="{0.916378512121,2.11986245798,1.71459079162}"'],
 ['classoperation Main.MyVar1 "Set Value" --value="{-0.0891762169545,-0.198666812922,-0.261723075945}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1.01555799978,2.83695956934,4.7778636562}"']]

Values can also be sampled with a Latin Hyper Cube sampler.

>>> mg.create_macros_LHS()
[['classoperation Main.MyVar1 "Set Value" --value="{0.0928967116493,-0.591418725401,-0.484696993931}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1.01049414425,2.46211329129,5.73806916203}"'],
 ['classoperation Main.MyVar1 "Set Value" --value="{-0.0166741228961,0.707722119582,-0.294180629253}"',
  'classoperation Main.MyVar2 "Set Value" --value="{1.11326829265,2.66016732923,4.28054911097}"'],
 ['classoperation Main.MyVar1 "Set Value" --value="{-0.20265197275,0.114947152258,0.924796936287}"',
  'classoperation Main.MyVar2 "Set Value" --value="{0.806864877696,4.4114188826,2.93941843565}"']]
get_macro(index, lower_tail_probability=None, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string

class anypytools.macro_commands.UpdateValues[source]

Create an ‘Update Values’ classoperation macro command.

Examples

>>> UpdateValues()
classoperation Main "Update Values"
get_macro(index, **kwarg)[source]

Create a string representation of the macro.

Parameters

index (int) – The index for the macro beeing generated. Some child classes use the index to generate different macros depending on the index.

Returns

A string with the AnyScript macro

Return type

string