Tips & Trics for anypytools

Stopping and restarting simulations

AnyPyProcess caches the simulation results. This allows us to stop the simulations, and then later restart them again.

[1]:
from anypytools import AnyPyProcess
app = AnyPyProcess(num_processes=1, return_task_info = True )
macro = [
    'load "Knee.any"',
    'operation Main.MyStudy.InverseDynamics',
    'run',
]

macrolist = [macro]*20
[2]:
app.start_macro(macrolist);
Processing interrupted

Here we stopped the simulation using the Notebook interrupt button. Calling the start_macro() function again continues the processing and re-run any task that wasn’t completed in the first run and any task that exited with errors.

[3]:
app.start_macro(macrolist);

Note: That changing the input arguments start_macro or re-instanciating the app object will erase the cache and re-run all processes.

Including meta-information in the output

The typical behavior of start_macro() is only to return variables ’dump’ed by the macro commands.

[6]:
from anypytools import AnyPyProcess
from anypytools.macro_commands import Load, OperationRun, Dump

app = AnyPyProcess()
macro = [
    Load('Knee.any', defs={'N_STEP':10}),
    OperationRun('Main.MyStudy.InverseDynamics'),
    Dump('Main.MyStudy.Output.MaxMuscleActivity'),
]
app.start_macro(macro)
[6]:
[{'Main.MyStudy.Output.MaxMuscleActivity':
    array([ 0.00890538,  0.02510015,  0.06036529,  0.08096678,  0.08356285,
            0.08356285,  0.08096678,  0.06036529,  0.02510015,  0.00890538])}]

To get more information about the tasks, we can set return_task_info flag.

[7]:
from anypytools import AnyPyProcess

app = AnyPyProcess(return_task_info = True)
macro = [
    'load "Knee.any" -def N_STEP="10"',
    'operation Main.MyStudy.InverseDynamics',
    'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"',
    'run'
]
app.start_macro(macro)
[7]:
[{'Main.MyStudy.Output.MaxMuscleActivity':
    array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
  'task_macro_hash': -6717722978800203534,
  'task_id': 0,
  'task_work_dir': 'C:\\Users\\mel\\Documents\\AnyPyTools\\docs\\Tutorial',
  'task_name': 'docs/Tutorial',
  'task_processtime': 0.5640715567972734,
  'task_macro':
    ['load "Knee.any" -def N_STEP="10"',
     'operation Main.MyStudy.InverseDynamics',
     'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"',
     'run'],
  'task_logfile': ''}]

That returns extra meta information about the processing and the macros.

Saving output to re-process at a later time

Saving data with ‘extra’ task information, gives some other posibilities. The results from running batch processin (i.e. output f start_macro() can be used as input to restart the same processing even if the AnyPyProcess have no cached results.

[8]:
from anypytools import AnyPyProcess

app = AnyPyProcess(return_task_info = True)
macro = [
    'load "Knee.any" -def N_STEP="10"',
    'operation Main.MyStudy.InverseDynamics',
    'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"',
    'run'
]
output = app.start_macro(macro)
[9]:
app = AnyPyProcess()
app.start_macro(output)
[9]:
[{'Main.MyStudy.Output.MaxMuscleActivity':
    array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
  'task_macro_hash': -6717722978800203534,
  'task_id': 0,
  'task_work_dir': 'C:\\Users\\mel\\Documents\\AnyPyTools\\docs\\Tutorial',
  'task_name': 'docs/Tutorial',
  'task_processtime': 0.43872824788422804,
  'task_macro':
    ['load "Knee.any" -def N_STEP="10"',
     'operation Main.MyStudy.InverseDynamics',
     'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"',
     'run'],
  'task_logfile': ''}]

The effect is that the result of an analysis can be saved to files and later restarted. The next example illustrates this.

Example: Saving data to disk while running

[8]:
import os
from scipy.stats import distributions
from anypytools import AnyPyProcess, AnyMacro
from anypytools.macro_commands import Load, SetValue_random, OperationRun, Dump
[9]:

tibia_knee_srel = distributions.norm([0, 0.18, 0], [0.005, 0.005, 0.005] )
femur_knee_srel = distributions.norm([0, -0.3, 0], [0.005, 0.005, 0.005] )

app = AnyPyProcess(return_task_info=True, disp=False)
mg = AnyMacro(number_of_macros = 500)
mg.extend([
    Load('knee.any', defs = {'N_STEP':20}),
    SetValue_random('Main.MyModel.Tibia.Knee.sRel', tibia_knee_srel),
    SetValue_random('Main.MyModel.Femur.Knee.sRel', femur_knee_srel),
    OperationRun('Main.MyStudy.InverseDynamics'),
    Dump('Main.MyStudy.Output.MaxMuscleActivity'),
])

try:
    os.remove('data.db')
except OSError:
    pass

for macros in mg.create_macros_MonteCarlo(batch_size=50):
    app.start_macro(macros)
    app.save_results('data.db', append=True)
    print('Data saved')
print('Done')
Data saved
Data saved
Data saved
Data saved
Data saved
Data saved
Data saved
Data saved
Data saved
Data saved
Done

All this stored data can be be reloaded

[11]:
reloaded_results = app.load_results('data.db')
print('Entries in file: {}'.format(len(reloaded_results)))
Entries in file: 500
[11]:
reloaded_results[456:457]
[11]:
[{'Main.MyStudy.Output.MaxMuscleActivity':
    array([ 0.01336689,  0.01764261,  0.02762996,  0.04205546,  0.05797153,
            0.07081998,  0.07692092,  0.07819499,  0.07874919,  0.07809371,
            0.07809389,  0.07874896,  0.07819499,  0.07692092,  0.07081997,
            0.05797153,  0.04205546,  0.02763004,  0.01764254,  0.01336688]),
  'task_macro_hash': 603735322,
  'task_id': 6,
  'task_work_dir': 'E:\\Users\\MEL\\GitHub\\AnyPyTools\\Tutorial',
  'task_name': 'AnyPyTools/Tutorial_6',
  'task_processtime': 0.5581194122411262,
  'task_macro':
    [u'load "knee.any" -def N_STEP="20"',
     u'classoperation Main.MyModel.Tibia.Knee.sRel "Set Value" --value="{-0.00516931593303,0.172085097949,0.00140114268864}"',
     u'classoperation Main.MyModel.Femur.Knee.sRel "Set Value" --value="{-0.00306923441492,-0.298351381585,0.00230825833634}"',
     u'operation Main.MyStudy.InverseDynamics',
     u'run',
     u'classoperation Main.MyStudy.Output.MaxMuscleActivity "Dump"'],
  'task_logfile': u''}]
[12]:
%matplotlib inline
import matplotlib.pyplot as plt

plt.plot(reloaded_results['MaxMuscleAct'].T, 'b', lw=0.2, alpha = 0.3);
../_images/Tutorial_06_Tips&Tricks_25_0.png