This little example demonstrates how to use the Python Experiment Suite (source code available
here).
This example verifies a simple stochastic law, namely that the sample mean of n random numbers drawn from a normal distribution converges to the actual mean of the distribution for large n.
The first part is the actual Python script called suite.py
from expsuite import PyExperimentSuite
from numpy import *
import os
class MySuite(PyExperimentSuite):
restore_supported = True
def reset(self, params, rep):
# initialize array
self.numbers = zeros(params['iterations'])
# seed random number generator
random.seed(params['seed'])
def iterate(self, params, rep, n):
# draw normally distributed random number
self.numbers[n] = random.normal(params['mean'], params['std'])
# calculate sample mean and offset
samplemean = mean(self.numbers[:n])
offset = abs(params['mean']-samplemean)
# return dictionary
ret = {'n':n, 'number':self.numbers[n],
'samplemean':samplemean, 'offset':offset}
return ret
def save_state(self, params, rep, n):
# save array as binary file
save(os.path.join(params['path'], params['name'],
'array_%i.npy'%rep), self.numbers)
def restore_state(self, params, rep, n):
# load array from file
self.numbers = load(os.path.join(params['path'],
params['name'], 'array_%i.npy'%rep))
if __name__ == '__main__':
mysuite = MySuite()
mysuite.start()
The part below is the configuration file experiments.cfg. The second part is the configuration file, called experiments.cfg. Copy both in the same folder, then run the suite.py script from the command line: python suite.py.
[DEFAULT]
repetitions = 1
iterations = 4000
path = results
seed = None
[normal]
mean = 0
std = 1
[highstd]
mean = 0
std = 5
After running the script, the logfiles are stored in a sub-directory ./results/ and can be accessed with the built-in Python API, for example with these commands:
from suite import MySuite
from matplotlib import pyplot as plt
from numpy import *
mysuite = MySuite()
exps = mysuite.get_exps()
print 'lowest offset for experiment normal:', mysuite.get_value(exps[0], 0, 'offset', 'min')
print 'lowest offset for experiment highstd:', mysuite.get_value(exps[1], 0, 'offset', 'min')
plt.plot(mysuite.get_history(exps[0], 0, 'offset'), linewidth=2, color='blue', label='std=1')
plt.plot(mysuite.get_history(exps[1], 0, 'offset'), linewidth=2, color='red', label='std=5')
plt.show()
This page has been viewed 12087 times.