More stuff can be installed using the build-in package-manager (conda
)
c:\>conda config --add channels conda-forge c:\>conda install anypytools
In [1]:
from anypytools import AnyPyProcess
app = AnyPyProcess( )
In [2]:
macrolist = ['load "Knee.any"',
'classoperation Main.MyParameter "Set Value" --value="10"',
'operation Main.MyStudy.Kinematics',
'run',
'exit']
app.start_macro(macrolist);
In [3]:
macrolist = [['load "Knee.any"',
'operation Main.MyStudy.Kinematics',
'run',
'exit'],
['load "Knee.any"',
'operation Main.MyStudy.InverseDynamics',
'run',
'exit']]
app.start_macro(macrolist);
In [4]:
many_macros = [['load "Knee.any"',
'classoperation Main.MyParameter "Set Value" --value="10"',
'operation Main.MyStudy.Kinematics',
'run',
'exit']]*40
In [ ]:
many_macros
In [5]:
app = AnyPyProcess(num_processes = 1)
app.start_macro(many_macros);
Then with parallelization
In [6]:
app = AnyPyProcess(num_processes = 10)
app.start_macro(many_macros);
Usual approach: AnyOutputFile, Save all output data (HDF5 file)
There must be a better way.
...there is...
The console application has a class operation we can use
classoperation <AnyScript_variable_name> "Dump"
AnyPyTools will automatically grab any data or error from AnyBodyCon
In [7]:
from anypytools import AnyPyProcess
app = AnyPyProcess( )
macrolist = [['load "Knee.any"',
'operation Main.MyStudy.InverseDynamics',
'run',
'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"',
'exit'],
['load "Knee.any"',
'operation Main.MyStudy.InverseDynamics',
'run',
'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"',
'exit']]
In [8]:
results = app.start_macro(macrolist)
In [ ]:
results
In [9]:
%matplotlib inline
from matplotlib.pyplot import plot
In [10]:
max_muscle_activity = results[0]['Main.MyStudy.Output.MaxMuscleActivity']
plot(max_muscle_activity);
Output behaves like default Python data types:
list
and dictionary
types.But with extra convenience functionality:
In [11]:
results[0]['Main.MyStudy.Output.MaxMuscleActivity'].shape
Out[11]:
In [12]:
results['Main.MyStudy.Output.MaxMuscleActivity'].shape
Out[12]:
In [13]:
results['MaxMuscleActivity'].shape
Out[13]:
AnyPyTools will also catch Errors...
Here is a macro with misspelled operation:
In [14]:
macrolist = ['load "Knee.any"',
'operation Main.MyStudy.Kinematic ',
'run',
'exit']
In [15]:
from anypytools import AnyPyProcess
app = AnyPyProcess( )
result = app.start_macro(macrolist);
In [16]:
result['ERROR']
Out[16]:
In [17]:
%pycat "large_macro.anymcr"
In [2]:
from anypytools.macro_commands import Load, OperationRun, Dump, SetValue
In [19]:
Load("Knee.any")
Out[19]:
In [20]:
SetValue('Main.Model.Parameter1', 100.3)
Out[20]:
In [21]:
macrolist = [ Load('Knee.any'),
SetValue('Main.MyParameter', 10),
OperationRun('Main.MyStudy.InverseDynamics'),
Dump('Main.MyStudy.Output.MaxMuscleActivity')]
macrolist
Out[21]:
In [22]:
from anypytools import AnyPyProcess
app = AnyPyProcess()
app.start_macro(macrolist);
We need an small helper class AnyMacro
to wrap our macro list.
In [3]:
from anypytools import AnyMacro
macrolist = AnyMacro( [ SetValue('Main.MyModel.MyParameter',8)] )
macrolist.number_of_macros = 2
macrolist
Out[3]:
In [4]:
parameter_list = [2.2, 2.5, 2.7, 2.9, 3.1]
macrolist = [ SetValue('Main.MyModel.MyParameter',parameter_list)]
AnyMacro(macrolist, number_of_macros=5)
Out[4]:
Observe the effect on maximum muscle activity.
In [25]:
patella_len = [0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08]
In [26]:
macro = [Load('Knee.any'),
SetValue('Main.MyModel.PatellaLigament.DriverPos', patella_len ),
OperationRun('Main.MyStudy.InverseDynamics'),
Dump('Main.MyStudy.Output.Abscissa.t'),
Dump('Main.MyModel.PatellaLigament.DriverPos'),
Dump('Main.MyStudy.Output.MaxMuscleActivity')]
parameter_study_macro = AnyMacro(macro, number_of_macros= 7 )
In [ ]:
parameter_study_macro
In [27]:
from anypytools import AnyPyProcess
app = AnyPyProcess()
output = app.start_macro(parameter_study_macro)
In [28]:
%matplotlib inline
from matplotlib.pyplot import plot, title, xlabel, legend, ylabel
In [29]:
for data in output:
maxact = data['Main.MyStudy.Output.MaxMuscleActivity']
time = data['Main.MyStudy.Output.Abscissa.t']
ligament_len = data['Main.MyModel.PatellaLigament.DriverPos'][0]
plot(time, maxact, label = str(100* ligament_len)+' cm' )
title('Effect of changing patella tendon length')
xlabel('Time steps')
ylabel('Max muscle activity')
legend(bbox_to_anchor=(1.05, 1), loc=2);
The approach depends on how the AnyBody model is structured.
Setup with a single main file:
Setup with multiple main files:
In [30]:
from anypytools import AnyPyProcess
from anypytools.macro_commands import Load, OperationRun
app = AnyPyProcess(num_processes = 3)
macro = [Load("main.any"),
OperationRun('Main.Study.InverseDynamics') ]
macro
Out[30]:
In [31]:
app.start_macro(macro, search_subdirs= "model[1-9].*main.any" );
Monte Carlo simulations
Latin hypercube sampling
Using external optimizers
Possible topics for a webcast in the fall.
from scipy.stats.distributions import norm
from anypytools import AnyPyProcess, AnyMacro from anypytools.macro_commands import Load, SetValue_random, OperationRun
app = AnyPyProcess( ) macro = AnyMacro( Load( "Knee.any"), SetValue_random('Main.MyModel.MyParameter', norm(0.1, 0.04)), OperationRun('Main.MyStudy.InverseDynamics') )
app.start_macro(macro.create_macros_MonteCarlo(1000));
In [ ]:
import seaborn as sns
sns.set_context('talk')
sns.set_style('whitegrid');
%matplotlib inline
from matplotlib.pyplot import plot, title, xlabel, legend, ylabel