From 488cf0bf4fff6fe9887094bed19260f9f1f14b5d Mon Sep 17 00:00:00 2001 From: ketiltrout Date: Thu, 15 Aug 2024 12:00:39 -0700 Subject: [PATCH] fix(foreground.poisson): fix cumulative_trapezoid call (#60) * fix(foreground.poisson): fix cumulative_trapezoid call The first parameter is y (the values to intregrate), the second, optional, parameter is x, the axis of integration. Also use the `initial` parameter to prepend a zero the result, removing the need to pre-initialise the output array. * tests --- .gitignore | 4 ++++ cora/foreground/poisson.py | 3 +-- tests/test_maps.py | 6 +++--- tests/test_poisson.py | 24 ++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 tests/test_poisson.py diff --git a/.gitignore b/.gitignore index 0fc30e6..b294499 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,7 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +#Editor rubble +*~ +.*.sw* diff --git a/cora/foreground/poisson.py b/cora/foreground/poisson.py index a165e6b..9ebdcc1 100644 --- a/cora/foreground/poisson.py +++ b/cora/foreground/poisson.py @@ -196,8 +196,7 @@ def inhomogeneous_process_approx(t, rate): ts = np.linspace(0.0, t, 10000) rs = rate(ts) - cumr = np.zeros_like(ts) - cumr[1:] = cumulative_trapezoid(ts, rs) + cumr = cumulative_trapezoid(rs, ts, initial=0) cumr /= cumr[-1] # Interpolate to generate the inverse CDF and use this to generate diff --git a/tests/test_maps.py b/tests/test_maps.py index 959c5bf..f57e666 100644 --- a/tests/test_maps.py +++ b/tests/test_maps.py @@ -54,6 +54,6 @@ def test_pointsource(): pstd = pol.std(axis=-1) assert (pstd[:, 0] > 3.0).all() assert (pstd[:, 0] < 15.0).all() - assert (pol.std(axis=-1)[:, 1:3] > 0.01).all() - assert (pol.std(axis=-1)[:, 1:3] < 0.05).all() - assert (pol.std(axis=-1)[:, 3] == 0.0).all() + assert (pstd[:, 1:3] > 0.005).all() + assert (pstd[:, 1:3] < 0.015).all() + assert (pstd[:, 3] == 0.0).all() diff --git a/tests/test_poisson.py b/tests/test_poisson.py new file mode 100644 index 0000000..1d71ab5 --- /dev/null +++ b/tests/test_poisson.py @@ -0,0 +1,24 @@ +"""Test caput.foreground.poisson""" + +import numpy as np + +from cora.foreground.poisson import inhomogeneous_process_approx + + +def test_inhomogeneous_process_approx(): + """Test inhomogeneous_process_approx""" + + # the most boringest rate function + rate = lambda s: 1000 * (5 - s) + + result = inhomogeneous_process_approx(5, rate) + + # Mean should be approximately 1.666 + mean = result.mean() + assert mean > 1.6 + assert mean < 1.75 + + # stdev should be approximately 1.2 + stdev = result.std() + assert stdev > 1.1 + assert stdev < 1.3