@@ -16,7 +16,7 @@ briefly demonstrates how to do this in three common scenarios:
16
16
17
17
Let's begin by importing some libraries that we will be using:
18
18
19
- .. code :: python
19
+ .. code :: ipython3
20
20
21
21
import os
22
22
import numpy as np
@@ -45,7 +45,7 @@ use ``elfi.tools.external_operation`` tool to wrap executables as a
45
45
Python callables (function). Let's first investigate how it works with a
46
46
simple shell ``echo `` command:
47
47
48
- .. code :: python
48
+ .. code :: ipython3
49
49
50
50
# Make an external command. {0} {1} are positional arguments and {seed} a keyword argument `seed`.
51
51
command = 'echo {0} {1} {seed}'
@@ -70,7 +70,7 @@ Currently ``echo_sim`` only accepts scalar arguments. In order to work
70
70
in ELFI, ``echo_sim `` needs to be vectorized so that we can pass to it a
71
71
vector of arguments. ELFI provides a handy tool for this as well:
72
72
73
- .. code :: python
73
+ .. code :: ipython3
74
74
75
75
# Vectorize it with elfi tools
76
76
echo_sim_vec = elfi.tools.vectorize(echo_sim)
@@ -88,9 +88,9 @@ vector of arguments. ELFI provides a handy tool for this as well:
88
88
89
89
.. parsed-literal ::
90
90
91
- array([[ 4.16252911e-01 , 0.00000000e+00, 3.08758378e +08],
92
- [ 9.83011677e -01, 0.00000000e+00, 3.08758378e +08],
93
- [ 8.22756066e -01, 0.00000000e+00, 3.08758378e +08]])
91
+ array([[ 1.93678222e+00 , 0.00000000e+00, 7.43529055e +08],
92
+ [ 9.43846120e -01, 0.00000000e+00, 7.43529055e +08],
93
+ [ 2.67626618e -01, 0.00000000e+00, 7.43529055e +08]])
94
94
95
95
96
96
@@ -143,7 +143,7 @@ efficiently. We will now reproduce Figure 6(a) in `*Lintusaari at al
143
143
2016* <https://doi.org/10.1093/sysbio/syw077> `__ *[2] * with ELFI. Let's
144
144
start by defining some constants:
145
145
146
- .. code :: python
146
+ .. code :: ipython3
147
147
148
148
# Fixed model parameters
149
149
delta = 0
@@ -156,7 +156,7 @@ start by defining some constants:
156
156
Let's build the beginning of a new model for the birth rate
157
157
:math: `\alpha ` as the only unknown
158
158
159
- .. code :: python
159
+ .. code :: ipython3
160
160
161
161
m = elfi.ElfiModel(name='bdm')
162
162
elfi.Prior('uniform', .005, 2, model=m, name='alpha')
@@ -170,29 +170,26 @@ Let's build the beginning of a new model for the birth rate
170
170
171
171
172
172
173
- .. code :: python
173
+ .. code :: ipython3
174
174
175
175
# Get the BDM source directory
176
176
sources_path = elfi.examples.bdm.get_sources_path()
177
177
178
- # Copy to resources folder and compile (unix-like systems)
179
- ! cp - r $ sources_path resources
180
- ! make - C resources/ cpp
178
+ # Compile (unix-like systems)
179
+ !make -C $sources_path
181
180
182
- # Move the file in to the working directory
183
- ! mv . / resources / cpp / bdm .
181
+ # Move the executable in to the working directory
182
+ !mv $sources_path /bdm .
184
183
185
184
186
185
.. parsed-literal ::
187
186
188
- make: Entering directory '/l/lintusj1/elfi-dev/notebooks/resources/cpp'
189
- g++ bdm.cpp --std=c++0x -O -Wall -o bdm
190
- make: Leaving directory '/l/lintusj1/elfi-dev/notebooks/resources/cpp'
187
+ g++ bdm.cpp --std=c++0x -O -Wall -o bdm
191
188
192
189
193
190
.. note :: The source code for the BDM simulator comes with ELFI. You can get the directory with `elfi.examples.bdm.get_source_directory()`. Under unix-like systems it can be compiled with just typing `make` to console in the source directory. For windows systems, you need to have some C++ compiler available to compile it.
194
191
195
- .. code :: python
192
+ .. code :: ipython3
196
193
197
194
# Test the executable (assuming we have the executable `bdm` in the working directory)
198
195
sim = elfi.tools.external_operation('./bdm {0} {1} {2} {3} --seed {seed} --mode 1')
@@ -220,7 +217,7 @@ efficient would be to write a native Python module with C++ but it's
220
217
beyond the scope of this article. So let's work through files which is a
221
218
fairly common situation especially with existing software.
222
219
223
- .. code :: python
220
+ .. code :: ipython3
224
221
225
222
# Assuming we have the executable `bdm` in the working directory
226
223
command = './bdm {filename} --seed {seed} --mode 1 > {output_filename}'
@@ -272,7 +269,7 @@ informative filenames, we ask ELFI to provide the operation some meta
272
269
information. That will be available under the ``meta `` keyword (see the
273
270
``prepare_inputs `` function above):
274
271
275
- .. code :: python
272
+ .. code :: ipython3
276
273
277
274
# Create the simulator
278
275
bdm_node = elfi.Simulator(bdm, m['alpha'], delta, tau, N, observed=y_obs, name='sim')
@@ -290,7 +287,7 @@ information. That will be available under the ``meta`` keyword (see the
290
287
291
288
292
289
293
- .. code :: python
290
+ .. code :: ipython3
294
291
295
292
# Test it
296
293
data = bdm_node.generate(3)
@@ -299,9 +296,9 @@ information. That will be available under the ``meta`` keyword (see the
299
296
300
297
.. parsed-literal ::
301
298
302
- [[ 8 3 1 1 1 1 1 2 1 1 0 0 0 0 0 0 0 0 0 0]
303
- [ 8 1 1 1 1 1 1 3 1 1 1 0 0 0 0 0 0 0 0 0]
304
- [14 2 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
299
+ [[13 1 4 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
300
+ [19 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
301
+ [14 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
305
302
306
303
307
304
Completing the BDM model
@@ -312,7 +309,7 @@ We are now ready to finish up the BDM model. To reproduce Figure 6(a) in
312
309
*[2] *, let's add different summaries and discrepancies to the model and
313
310
run the inference for each of them:
314
311
315
- .. code :: python
312
+ .. code :: ipython3
316
313
317
314
def T1(clusters):
318
315
clusters = np.atleast_2d(clusters)
@@ -340,7 +337,7 @@ run the inference for each of them:
340
337
341
338
342
339
343
- .. code :: python
340
+ .. code :: ipython3
344
341
345
342
elfi.draw(m)
346
343
@@ -351,7 +348,7 @@ run the inference for each of them:
351
348
352
349
353
350
354
- .. code :: python
351
+ .. code :: ipython3
355
352
356
353
# Save parameter and simulation results in memory to speed up the later inference
357
354
pool = elfi.OutputPool(['alpha', 'sim'])
@@ -370,15 +367,15 @@ run the inference for each of them:
370
367
371
368
.. parsed-literal ::
372
369
373
- CPU times: user 3.86 s, sys: 52 ms, total: 3.92 s
374
- Wall time: 4.81 s
375
- CPU times: user 32 ms, sys: 4 ms, total: 36 ms
376
- Wall time: 36.4 ms
377
- CPU times: user 40 ms, sys: 0 ns , total: 40 ms
378
- Wall time: 36.9 ms
370
+ CPU times: user 3.11 s, sys: 143 ms, total: 3.26 s
371
+ Wall time: 5.56 s
372
+ CPU times: user 29.9 ms, sys: 1.45 ms, total: 31.3 ms
373
+ Wall time: 31.2 ms
374
+ CPU times: user 33.8 ms, sys: 500 µs , total: 34.3 ms
375
+ Wall time: 34 ms
379
376
380
377
381
- .. code :: python
378
+ .. code :: ipython3
382
379
383
380
# Load a precomputed posterior based on an analytic solution (see Lintusaari et al 2016)
384
381
matdata = sio.loadmat('./resources/bdm.mat')
@@ -426,7 +423,7 @@ Here we demonstrate how to calculate the summary statistics used in the
426
423
ELFI tutorial (autocovariances) using R's ``acf `` function for the MA2
427
424
model.
428
425
429
- .. code :: python
426
+ .. code :: ipython3
430
427
431
428
import rpy2.robjects as robj
432
429
from rpy2.robjects import numpy2ri as np2ri
@@ -441,7 +438,7 @@ model.
441
438
Let's create a Python function that wraps the R commands (please see the
442
439
documentation of `rpy2 <http://rpy2.readthedocs.io >`__ for details):
443
440
444
- .. code :: python
441
+ .. code :: ipython3
445
442
446
443
robj.r('''
447
444
# create a function `f`
@@ -459,7 +456,7 @@ documentation of `rpy2 <http://rpy2.readthedocs.io>`__ for details):
459
456
ans = apply(x, 1, f, lag=lag)
460
457
return np.atleast_1d(ans)
461
458
462
- .. code :: python
459
+ .. code :: ipython3
463
460
464
461
# Test it
465
462
autocovR(np.array([[1,2,3,4], [4,5,6,7]]), 1)
@@ -475,7 +472,7 @@ documentation of `rpy2 <http://rpy2.readthedocs.io>`__ for details):
475
472
476
473
Load a ready made MA2 model:
477
474
478
- .. code :: python
475
+ .. code :: ipython3
479
476
480
477
ma2 = elfi.examples.ma2.get_model(seed_obs=4)
481
478
elfi.draw(ma2)
@@ -489,7 +486,7 @@ Load a ready made MA2 model:
489
486
490
487
Replace the summaries S1 and S2 with our R autocovariance function.
491
488
492
- .. code :: python
489
+ .. code :: ipython3
493
490
494
491
# Replace with R autocov
495
492
S1 = elfi.Summary(autocovR, ma2['MA2'], 1)
@@ -523,20 +520,20 @@ MATLAB function using the official `MATLAB Python cd
523
520
API <http://www.mathworks.com/help/matlab/matlab-engine-for-python.html> `__.
524
521
(Tested with MATLAB 2016b.)
525
522
526
- .. code :: python
523
+ .. code :: ipython3
527
524
528
525
import matlab.engine
529
526
530
527
A MATLAB session needs to be started (and stopped) separately:
531
528
532
- .. code :: python
529
+ .. code :: ipython3
533
530
534
531
eng = matlab.engine.start_matlab() # takes a while...
535
532
536
533
Similarly as with R, we have to write a piece of code to interface
537
534
between MATLAB and Python:
538
535
539
- .. code :: python
536
+ .. code :: ipython3
540
537
541
538
def euclidean_M(x, y):
542
539
# MATLAB array initialized with Python's list
@@ -549,7 +546,7 @@ between MATLAB and Python:
549
546
d = np.atleast_1d(dM).reshape(-1)
550
547
return d
551
548
552
- .. code :: python
549
+ .. code :: ipython3
553
550
554
551
# Test it
555
552
euclidean_M(np.array([[1,2,3], [6,7,8], [2,2,3]]), np.array([2,2,2]))
@@ -565,7 +562,7 @@ between MATLAB and Python:
565
562
566
563
Load a ready made MA2 model:
567
564
568
- .. code :: python
565
+ .. code :: ipython3
569
566
570
567
ma2M = elfi.examples.ma2.get_model(seed_obs=4)
571
568
elfi.draw(ma2M)
@@ -579,7 +576,7 @@ Load a ready made MA2 model:
579
576
580
577
Replace the summaries S1 and S2 with our R autocovariance function.
581
578
582
- .. code :: python
579
+ .. code :: ipython3
583
580
584
581
# Replace with Matlab distance implementation
585
582
d = elfi.Distance(euclidean_M, ma2M['S1'], ma2M['S2'])
@@ -604,7 +601,7 @@ Replace the summaries S1 and S2 with our R autocovariance function.
604
601
605
602
Finally, don't forget to quit the MATLAB session:
606
603
607
- .. code :: python
604
+ .. code :: ipython3
608
605
609
606
eng.quit()
610
607
0 commit comments