Skip to content

Commit

Permalink
Merge pull request #473 from StingraySoftware/fix_complex_rebin
Browse files Browse the repository at this point in the history
Fix binning of complex64
  • Loading branch information
matteobachetti authored Jun 16, 2020
2 parents 7ed7615 + 8f90316 commit ecf062a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
9 changes: 5 additions & 4 deletions stingray/tests/test_crossspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ def test_rebin_log(self):
def test_rebin_log_returns_complex_values(self):
# For now, just verify that it doesn't crash
new_cs = self.acs.rebin_log(f=0.1)
assert isinstance(new_cs.power[0], np.complex)
assert np.iscomplexobj(new_cs.power[0])

def test_rebin_log_returns_complex_errors(self):
# For now, just verify that it doesn't crash

new_cs = self.acs.rebin_log(f=0.1)
assert isinstance(new_cs.power_err[0], np.complex)
assert np.iscomplexobj(new_cs.power_err[0])


class TestCoherenceFunction(object):
Expand Down Expand Up @@ -850,13 +851,13 @@ def test_rebin_log_returns_complex_values(self):
# For now, just verify that it doesn't crash
with warnings.catch_warnings(record=True) as w:
new_cs = self.cs.rebin_log(f=0.1)
assert isinstance(new_cs.power[0], np.complex)
assert np.iscomplexobj(new_cs.power[0])

def test_rebin_log_returns_complex_errors(self):
# For now, just verify that it doesn't crash
with warnings.catch_warnings(record=True) as w:
new_cs = self.cs.rebin_log(f=0.1)
assert isinstance(new_cs.power_err[0], np.complex)
assert np.iscomplexobj(new_cs.power_err[0])

def test_timelag(self):
dt = 0.1
Expand Down
19 changes: 16 additions & 3 deletions stingray/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_method_works_on_complex_numbers(self):
re = np.arange(self.xmin, self.xmax, self.dx)
im = np.arange(self.xmin, self.xmax, self.dx)

y = np.zeros(re.shape[0], dtype=np.complex)
y = np.zeros(re.shape[0], dtype=np.complex64)
yerr = np.zeros(re.shape[0], dtype=np.complex)

for k, (r, i) in enumerate(zip(re, im)):
Expand All @@ -157,10 +157,23 @@ def test_method_works_on_complex_numbers(self):
for i in range(self.true_values.shape[0]):
real_binned[i] = self.true_values[i] + self.true_values[i] * 1j

_, biny, _, _ = utils.rebin_data_log(self.x, y, self.f, y_err=yerr,
dx=self.dx)
_, _, binyerr_real, _ = \
utils.rebin_data_log(self.x, y.real, self.f, y_err=yerr.real,
dx=self.dx)
_, biny, binyerr, _ = \
utils.rebin_data_log(self.x, y, self.f, y_err=yerr,
dx=self.dx)

assert np.iscomplexobj(biny)
assert np.iscomplexobj(binyerr)
assert np.allclose(biny, real_binned)
assert np.allclose(binyerr, binyerr_real + 1.j * binyerr_real)

def test_return_float_with_floats(self):
_, biny, binyerr, _ = utils.rebin_data_log(
self.x, self.y, self.f, y_err=self.y_err, dx=self.dx)
assert not np.iscomplexobj(biny)
assert not np.iscomplexobj(binyerr)


class TestUtils(object):
Expand Down
4 changes: 2 additions & 2 deletions stingray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,15 @@ def rebin_data_log(x, y, f, y_err=None, dx=None):
x.astype(np.double), real_err.astype(np.double),
statistic=_root_squared_mean, bins=binx)

if isinstance(y[0], np.complex):
if np.iscomplexobj(y):
imag = y.imag
biny_imag, bin_edges, binno = scipy.stats.binned_statistic(
x.astype(np.double), imag.astype(np.double),
statistic="mean", bins=binx)

biny = biny + 1j * biny_imag

if isinstance(y_err[0], np.complex):
if np.iscomplexobj(y_err):
imag_err = y_err.imag

biny_err_imag, bin_edges, binno = scipy.stats.binned_statistic(
Expand Down

0 comments on commit ecf062a

Please sign in to comment.