-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagni_reproducibility_example.py
277 lines (217 loc) · 9.76 KB
/
magni_reproducibility_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Copyright (c) 2016,
Christian Schou Oxvig, Thomas Arildsen, and Torben Larsen
Aalborg University, Department of Electronic Systems, Signal and Information
Processing, Fredrik Bajers Vej 7, DK-9220 Aalborg, Denmark.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
An example of how to use the magni.reproducibility package for storing metadata
along with results from a computational experiment. The example is based on
simulating the Mandelbrot set. The Mandelbrot set simulation is in no way
optimised since the purpose of this example is to showcase the functionality in
magni.reproducibility.
Magni is available here: http://dx.doi.org/10.5278/VBN/MISC/Magni
"""
from __future__ import division
import argparse
from pprint import pprint
import matplotlib as mpl; mpl.use('Agg')
import magni
from magni.utils.validation import decorate_validation as _decorate_validation
from magni.utils.validation import validate_generic as _generic
from magni.utils.validation import validate_levels as _levels
from magni.utils.validation import validate_numeric as _numeric
import numpy as np
import psutil
def compute_mandelbrot_point(c, max_iterations, threshold):
"""
Determine if a complex plane point is part of the Mandelbrot set.
Parameters
----------
c : complex
The complex plane point.
max_iterations : int
The maximum number of iterations to use in the point stability check.
threshold : int
The threshold used in the point stability check.
Returns
-------
score : float
The stability score.
Notes
-----
The Mandelbrot set consists of all values `c` in the complex plane for
which the so-called orbit of 0 when iterated under the quadratic map
z_{n+1} = z_{n}^2 + c remains bounded [1]_.
In this simulation we iterate the quadratic map for up to `max_iterations`
and assign a stability `score` based on the percentage of the
`max_iterations` it takes to the surpass the `threshold`. A `score` <= 10
indicates an unstable point which is not considered part of the Mandelbrot
set whereas a `score` > 10 indicates a stable point which is considered
part of the Mandelbrot set.
.. [1] http://en.wikipedia.org/wiki/Mandelbrot_set
"""
@_decorate_validation
def validate_input():
_numeric('c', 'complex')
_numeric('max_iterations', 'integer', range_='(0;inf)')
_numeric('threshold', 'integer', range_='(0;inf)')
validate_input()
z = np.complex(0)
score = 100
for t in range(max_iterations):
z = z**2 + c
z_modulus = np.abs(z)
if z_modulus > threshold or not np.isfinite(z_modulus):
score = 100 * (t + 1) / max_iterations
break
return score
def get_mandelbrot_tasks(re_min, re_max, im_min, im_max, num_points):
"""
Return a list of tasks for a parallel computation of the Mandelbrot set.
Parameters
----------
re_min : float
Real axis minimum in the complex plane grid.
re_max : float
Real axis maximum in the complex plane grid.
im_min : float
Imaginary axis minimum in the complex plane grid.
im_max : float
Imaginary axis maximum in the complex plane grid.
num_points : int
Number of points along each direction in complex plane grid.
Returns
-------
tasks : list
The the list of simulation tasks for the Mandelbrot set simulation.
"""
@_decorate_validation
def validate_input():
_numeric('re_min', 'floating')
_numeric('re_max', 'floating', range_='({};inf)'.format(re_min))
_numeric('im_min', 'floating')
_numeric('im_max', 'floating', range_='({};inf)'.format(im_min))
_numeric('num_points', 'integer', range_='[1;inf)')
validate_input()
re = np.linspace(re_min, re_max, num_points)
im = np.linspace(im_min, im_max, num_points)
tasks = [{'complex_plane_point_value': re[re_ix] + 1j * im[im_ix],
'complex_plane_point_index': (re_ix, im_ix),
'max_iterations': 10000,
'threshold': 100}
for re_ix in range(num_points)
for im_ix in range(num_points)]
return tasks
def run_mandelbrot_simulation(h5_path=None, task=None):
"""
Run a Mandelbrot task simulation.
Parameters
----------
h5_path : str
The path to the HDF5 file in which the result is to be saved.
task : dict
The simulation task specification.
"""
@_decorate_validation
def validate_input():
_generic('h5_path', 'string')
_generic('task', 'mapping')
validate_input()
pprint('Now handling complex plane point: {}'.format(
task['complex_plane_point_index']))
re_ix = task['complex_plane_point_index'][0]
im_ix = task['complex_plane_point_index'][1]
mandelbrot_result = compute_mandelbrot_point(
task['complex_plane_point_value'],
task['max_iterations'], task['threshold'])
with magni.utils.multiprocessing.File(h5_path, mode='a') as h5_file:
mandelbrot_array = h5_file.root.simulation_result.mandelbrot
mandelbrot_array[im_ix, re_ix] = mandelbrot_result
if __name__ == '__main__':
"""
Do a parallel computation of the Mandelbrot set.
See http://en.wikipedia.org/wiki/Mandelbrot_set for more info about the
Mandelbrot set.
usage: scipy_2016_magni_reproducibility_example.py [-h]
re_min re_max im_min im_max
num_points
magni.reproducibility Mandelbrot example
positional arguments:
re_min Real axis minimum
re_max Real axis maximum
im_min Imaginary axis minimum
im_max Imaginary axis maximum
num_points Number of points along each direction in complex plane grid
optional arguments:
-h, --help show this help message and exit
"""
# Argument parsing
arg_parser = argparse.ArgumentParser(
description='magni.reproducibility Mandelbrot example')
arg_parser.add_argument(
're_min', action='store', type=float, help='Real axis minimum')
arg_parser.add_argument(
're_max', action='store', type=float, help='Real axis maximum')
arg_parser.add_argument(
'im_min', action='store', type=float, help='Imaginary axis minimum')
arg_parser.add_argument(
'im_max', action='store', type=float, help='Imaginary axis maximum')
arg_parser.add_argument(
'num_points', action='store', type=int,
help='Number of points along each direction in complex plane grid')
args = arg_parser.parse_args()
# Path to database to store results in
h5_path = 'mandelbrot.hdf5'
pprint('Results database: {}'.format(h5_path))
# Get tasks to run in parallel
tasks = get_mandelbrot_tasks(
args.re_min, args.re_max, args.im_min, args.im_max, args.num_points)
kwargs = [{'h5_path': h5_path, 'task': task} for task in tasks]
pprint('Total number of simulation tasks: {}'.format(len(tasks)))
# Setup magni multiprocessing
workers = psutil.cpu_count(logical=False)
magni.utils.multiprocessing.config.update(
{'workers': workers, 'prefer_futures': True,
're_raise_exceptions': True, 'max_broken_pool_restarts': 10})
pprint('Magni multiprocessing config:')
pprint(dict(magni.utils.multiprocessing.config.items()))
# Create results database and array
magni.reproducibility.io.create_database(h5_path)
with magni.utils.multiprocessing.File(h5_path, mode='a') as h5_file:
h5_file.create_array(
'/simulation_result', 'mandelbrot', createparents=True,
obj=np.zeros((args.num_points, args.num_points)))
# Store experiment parameters and checksums of this script
ann_sub_group = 'custom_annotations'
with magni.utils.multiprocessing.File(h5_path, mode='a') as h5_file:
magni.reproducibility.io.write_custom_annotation(
h5_file, 'script_input_parameters', vars(args),
annotations_sub_group=ann_sub_group)
magni.reproducibility.io.write_custom_annotation(
h5_file, 'script_checksums',
magni.reproducibility.data.get_file_hashes(__file__),
annotations_sub_group=ann_sub_group)
# Run simulation
magni.utils.multiprocessing.process(
run_mandelbrot_simulation, kwargs_list=kwargs, maxtasks=1)
# Write end time annotation
with magni.utils.multiprocessing.File(h5_path, mode='a') as h5_file:
magni.reproducibility.io.write_custom_annotation(
h5_file, 'end_time', magni.reproducibility.data.get_datetime())