Repository: steven-murray/powerbox

Issue:
Errot with get_power
I tried to compute the cross-correlated power spectrum of two fields using get_power, including as an argument another field deltax2. I obtained an error message, even if deltax2 was the same than deltax1 (which should give the standard power spectrum). I noticed that there is an error in tools.py/get_power, line 463, which reads:

    if deltax2 is not None and deltax.shape != len(deltax2.shape):

but it should be:

    if deltax2 is not None and deltax.shape != deltax2.shape:

which works without problem. I checked it recovering the power spectrum when deltax2=deltax1.

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/29001198/badge)](https://coveralls.io/builds/29001198)

Coverage increased (+0.3%) to 89.315% when pulling **f9c6295914a42dd2d0292e9fde985120f1c75354 on fix-cross-power-bug** into **09809f3fe9e2b25dfb2f956ca4c2d4d40a0ac693 on master**.


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 428d540..d3bd2f9 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,8 +1,14 @@
 Changelog
 =========
 
-dev
----
+0.6.1
+-----
+**Bugfixes**
+
+- Fix error when doing cross-power [Issue #5].
+
+0.6.0
+-----
 **Features**
 
 - New ``left_edge`` argument in fft/ifft which gives the ability to specify where
diff --git a/powerbox/__init__.py b/powerbox/__init__.py
index 074d33d..81a6073 100644
--- a/powerbox/__init__.py
+++ b/powerbox/__init__.py
@@ -1,4 +1,4 @@
-__version__ = "0.6.0"
+__version__ = "0.6.1"
 
 from .powerbox import PowerBox, LogNormalPowerBox
 from .tools import get_power, angular_average, angular_average_nd
diff --git a/powerbox/tools.py b/powerbox/tools.py
index 2b087fd..b78d7e3 100644
--- a/powerbox/tools.py
+++ b/powerbox/tools.py
@@ -460,7 +460,7 @@ def get_power(deltax, boxlength, deltax2=None, N=None, a=1., b=1., remove_shotno
         if not np.iterable(boxlength):
             boxlength = [boxlength] * dim
 
-        if deltax2 is not None and deltax.shape != len(deltax2.shape):
+        if deltax2 is not None and deltax.shape != deltax2.shape:
             raise ValueError("deltax and deltax2 must have the same shape!")
 
         N = deltax.shape


Test patch:
diff --git a/tests/test_tools.py b/tests/test_tools.py
index aa71767..4270f8f 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -1,5 +1,6 @@
 import numpy as np
-from powerbox.tools import angular_average_nd, angular_average
+from powerbox.tools import angular_average_nd, angular_average, get_power
+from powerbox.powerbox import PowerBox
 import pytest
 
 def test_angular_avg_nd_3():
@@ -109,6 +110,13 @@ def test_logbins():
     assert np.all(np.isclose(np.diff(coord[1:]/coord[:-1]), 0))
 
 
+def test_cross_power_identity():
+    pb = PowerBox(200, dim=2, pk=lambda k: 1.0 * k ** -2., boxlength=1.0, b=1)
+    dx = pb.delta_x()
+    p, k = get_power(dx, pb.boxlength, b=1)
+    p_cross, k = get_power(dx, pb.boxlength, b=1, deltax2=dx)
+    assert np.all(np.isclose(p, p_cross))
+
 
 @pytest.mark.skip()
 def test_against_multirealisation():