anypytools.AnyMacro

See anypytools.macro_commands for a list of classes that generate macro commands.

class anypytools.AnyMacro(*args, **kwargs)[source]

Class to generate multiple macros.

Use the class to contruct multiple anyscript macros from a single macro. E.g. for parameter studies, Monte Carlo, Latin Hyper Cube studies.

Parameters
  • maco_commands (list or positional arguments) – A list of macro command classes. These may also be given a arbitrary number of positional arguments.

  • counter_token (str) – A string in the macro commands which will be replace with a counter for every macro generated

  • number_of_macros (int) – The default number of macros which are created. This can be overruled when calling the AnyMacro.create_macros() function. Defults to 1.

  • seed (int or array_like) – A constant seed value used which will be used every time macros are created. Must be convertable to 32 bit unsigned integers.

Returns

A Macro object for constructing the macro.

Return type

An AnyMacro instance

Examples

>>> mg = AnyMacro(number_of_macros = 22)
>>> mg.append( Load('c:/MyModel/model.main.any', defs = {}, paths = {} ) )
>>> mg.append( SetValue('Main.myvar', 12.3)  )
>>> mg.extend( [RunOperation('Main.Study.Kinematics'),
                 Dump('Main.Study.Output.MyVar') ] )
>>> mg
[['load "c:/MyModel/model.main.any"',
  'classoperation Main.myvar "Set Value" --value="12.3"',
  'operation Main.Study.Kinematics',
  'run',
  'classoperation Main.Study.Output.MyVar "Dump"'],
 ['load "c:/MyModel/model.main.any"',
  'classoperation Main.myvar "Set Value" --value="12.3"',
  'operation Main.Study.Kinematics',
  'run',
  'classoperation Main.Study.Output.MyVar "Dump"']]
>>> mg = AnyMacro( number_of_macros = 3, counter_token='{COUNTER}' )
>>> mg.append(Load('c:/MyModel/model_{COUNTER}_.main.any') )
>>> pprint(mg.create_macros())
[['load "c:/MyModel/model_0_.main.any"'],
 ['load "c:/MyModel/model_1_.main.any"'],
 ['load "c:/MyModel/model_2_.main.any"']]
append(val)[source]

S.append(value) – append value to the end of the sequence

create_macros(number_of_macros=None, batch_size=None)[source]

Generate a given number of macros.

The function return its output either as list (batch_size = None) or in batches as generator object (memory efficient when generating many macros)

Parameters
  • number_of_macros (int (Optional)) – The number of macro to create.

  • batch_size (int (Optional)) – If specified the function will return a generator which creates macros in batches.

Returns

A list macros or a generator which creates macros in batches

Return type

list or generator

Examples

>>> from anypytools import AnyMacro
>>> from anypytools.macro_commands import *
>>> macro = [
...     Load('c:/Model.main.any'),
...     OperationRun('Main.study.Kinematics'),
... ]
>>> mg = AnyMacro(macro, number_of_macros=4)
>>> macrolist = mg.create_macros()
>>> pprint(macrolist)
[['load "c:/Model.main.any"', 'operation Main.study.Kinematics', 'run'],
 ['load "c:/Model.main.any"', 'operation Main.study.Kinematics', 'run'],
 ['load "c:/Model.main.any"', 'operation Main.study.Kinematics', 'run'],
 ['load "c:/Model.main.any"', 'operation Main.study.Kinematics', 'run']]

Generate macros in batches

>>> mg = AnyMacro([Load('c:/Model.main.any')], number_of_macros = 6)
>>> macro_gen =  mg.create_macros(batch_size=2)
>>> for i, macros in enumerate( macro_gen ):
...     print('Batch ', i)
...     pprint(macros)
Batch 0
[['load "c:/Model.main.any"'], ['load "c:/Model.main.any"']]
Batch 1
[['load "c:/Model.main.any"'], ['load "c:/Model.main.any"']]
Batch 2
[['load "c:/Model.main.any"'], ['load "c:/Model.main.any"']]
create_macros_LHS(number_of_macros=None, criterion=None, iterations=None, batch_size=None, append_default=False)[source]

Generate AnyScript macros for Latin Hyper Cube Studies studies.

Generates AnyScript macros for parameter studies using Latin hyper cube sampling. The macros added with the SetValue_random are created with with Latin Hypercube Sampling (LHS) of the parameter space. The number of generated macros determines the number of LHS samples.

The method uses the pyDOE package to generate the LHS data.

Parameters
  • number_of_macros (int (Optional)) – The number of macro to create.

  • batch_size (int (Optional)) – If specified the function will return a generator which creates macros in batches.

  • criterion ({None, 'c', 'm', 'cm', 'corr'}) – A a string that specifies how points are sampled (see: http://pythonhosted.org/pyDOE/randomized.html) None (Default) points are randomizes within the intervals. “center” or “c” center the points within the sampling intervals “maximin” or “m”: maximize the minimum distance between points, but place the point in a randomized location within its interval “centermaximin” or “cm”: same as “maximin”, but centered within the intervals “corr” minimize the maximum correlation coefficient

  • iterations (int) – Specifies how many iterations are used to accomplished the selected criterion

  • append_default (bool) – If True a macro with default (mean) values is appended to the returned list of returned macros

Returns

A list macros or a generator which creates macros in batches

Return type

list or generator

Examples

>>> 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.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}"']]
create_macros_MonteCarlo(number_of_macros=None, batch_size=None)[source]

Generate AnyScript macros for monte carlos studies.

The function returns macros for Monte Carlo parameter studies. This methods extends the create_macros methods with functionality to randomly vary parameters across macros.

Values added with SetValue_random class are sampled randomly in a ‘Monte Carlo’ fashion. Note that the values of the first macro is the defaut mean value.

Parameters
  • number_of_macros (int (Optional)) – The number of macro to create.

  • batch_size (int (Optional)) – If specified the function will return a generator which creates macros in batches.

Returns

A list macros or a generator which creates macros in batches

Return type

list or generator

Examples

>>> 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.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}"']]
insert(ii, val)[source]

S.insert(index, value) – insert value before index