Skip to content

Commit dd9f6b2

Browse files
committed
Now reuse the estimate_pi function from a previous module for continuity.
1 parent 1ed19c1 commit dd9f6b2

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

episodes/09-testing-output-files.Rmd

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ exercises: 3
2020

2121
::::::::::::::::::::::::::::::::::::::::::::::::
2222

23-
## Setup
24-
25-
To use the `snaptol` package in this module, you will need to install it via,
26-
27-
```bash
28-
pip install "git+https://github.com/PlasmaFAIR/snaptol"
29-
```
3023

3124
## 1) Introduction
3225

@@ -249,23 +242,24 @@ Consider a simulation code that uses algorithms that depend on convergence – p
249242
not have an exact answer but can be approximated numerically within a given tolerance. This, along with the common use
250243
of controlled randomised initial conditions, can lead to results that differ slightly between runs.
251244

252-
In the example below, we approximate the value of pi using a random sample of points in a square. The exact
253-
implementation of the algorithm is not important, but it relies on the use of randomised input and as a result the
254-
determined value will vary slightly between runs.
245+
In the example below, we use the `estimate_pi` function from the "Floating Point Data" module. It relies on the use of
246+
randomised input and as a result the determined value will vary slightly between runs.
255247

256248
```python
257249
# test_tol.py
258-
import numpy as np
250+
import random
259251

260-
def approximate_pi(random_points: np.ndarray):
261-
return 4 * np.mean(np.sum(random_points ** 2, axis=1) <= 1)
252+
def estimate_pi(iterations):
253+
num_inside = 0
254+
for _ in range(iterations):
255+
x = random.random()
256+
y = random.random()
257+
if x**2 + y**2 < 1:
258+
num_inside += 1
259+
return 4 * num_inside / iterations
262260

263261
def test_something(snaptolshot):
264-
rng = np.random.default_rng()
265-
266-
random_points_in_square = rng.uniform(-1.0, 1.0, size=(10000000, 2))
267-
268-
result = approximate_pi(random_points_in_square)
262+
result = estimate_pi(10000000)
269263

270264
print(result)
271265

@@ -361,7 +355,7 @@ def test_something():
361355

362356
## Implement a regression test with Snaptol
363357

364-
- Using the `approximate_pi` function above, implement a regression test using the `snaptolshot` object.
358+
- Using the `estimate_pi` function above, implement a regression test using the `snaptolshot` object.
365359

366360
- Ensure to use the `assert_allclose` method to compare the result to the snapshot carefully.
367361

@@ -372,17 +366,19 @@ def test_something():
372366
:::::::::::::::::::::::: solution
373367

374368
```python
375-
import numpy as np
369+
import random
376370

377-
def approximate_pi(random_points: np.ndarray):
378-
return 4 * np.mean(np.sum(random_points ** 2, axis=1) <= 1)
371+
def estimate_pi(iterations):
372+
num_inside = 0
373+
for _ in range(iterations):
374+
x = random.random()
375+
y = random.random()
376+
if x**2 + y**2 < 1:
377+
num_inside += 1
378+
return 4 * num_inside / iterations
379379

380380
def test_something(snaptolshot):
381-
rng = np.random.default_rng()
382-
383-
random_points_in_square = rng.uniform(-1.0, 1.0, size=(10000000, 2))
384-
385-
result = approximate_pi(random_points_in_square)
381+
result = estimate_pi(10000000)
386382

387383
snaptolshot.assert_allclose(result, rtol=1e-03, atol=0.0)
388384
```
@@ -395,7 +391,7 @@ def test_something(snaptolshot):
395391

396392
## More complex regression tests
397393

398-
- Create two separate tests that both utilise the `approximate_pi` function as a fixture.
394+
- Create two separate tests that both utilise the `estimate_pi` function as a fixture.
399395

400396
- Using different tolerances for each test, assert that the first passes successfully, and assert that the second raises
401397
an `AssertionError`. Hints: 1) remember to look back at the "Testing for Exceptions" and "Fixtures" modules, 2) the
@@ -404,25 +400,28 @@ error in the pi calculation algorithm is $\frac{1}{\sqrt{N}}$ where $N$ is the n
404400
:::::::::::::::::::::::: solution
405401

406402
```python
407-
import numpy as np
403+
import random
408404
import pytest
409405

410406
@pytest.fixture
411-
def approximate_pi():
412-
rng = np.random.default_rng()
413-
414-
random_points = rng.uniform(-1.0, 1.0, size=(10000000, 2))
415-
416-
return 4 * np.mean(np.sum(random_points ** 2, axis=1) <= 1)
417-
418-
def test_pi_passes(snaptolshot, approximate_pi):
407+
def estimate_pi():
408+
iterations = 10000000
409+
num_inside = 0
410+
for _ in range(iterations):
411+
x = random.random()
412+
y = random.random()
413+
if x**2 + y**2 < 1:
414+
num_inside += 1
415+
return 4 * num_inside / iterations
416+
417+
def test_pi_passes(snaptolshot, estimate_pi):
419418
# Passes due to loose tolerance.
420-
snaptolshot.assert_allclose(approximate_pi, rtol=1e-03, atol=0.0)
419+
snaptolshot.assert_allclose(estimate_pi, rtol=1e-03, atol=0.0)
421420

422-
def test_pi_fails(snaptolshot, approximate_pi):
421+
def test_pi_fails(snaptolshot, estimate_pi):
423422
# Fails due to tight tolerance.
424423
with pytest.raises(AssertionError):
425-
snaptolshot.assert_allclose(approximate_pi, rtol=1e-04, atol=0.0)
424+
snaptolshot.assert_allclose(estimate_pi, rtol=1e-04, atol=0.0)
426425
```
427426

428427
:::::::::::::::::::::::::::::::::

0 commit comments

Comments
 (0)