Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cross spectrum plotting #763

Merged
merged 9 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/763.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix plotting of spectra, avoiding the plot of imaginary parts of real numbers
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ filterwarnings =
ignore:.*is a deprecated alias for:DeprecationWarning
ignore:.*HIERARCH card will be created.*:
ignore:.*FigureCanvasAgg is non-interactive.*:UserWarning
ignore:.*jax.* deprecated. Use jax.*instead:DeprecationWarning

;addopts = --disable-warnings

Expand Down
44 changes: 37 additions & 7 deletions stingray/crossspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,24 +1075,54 @@ def plot(
fig = plt.figure("crossspectrum")
ax = fig.add_subplot(1, 1, 1)

ax.plot(self.freq, np.abs(self.power), marker, color="b", label="Amplitude")
ax.plot(self.freq, self.power.real, marker, color="r", alpha=0.5, label="Real Part")
ax.plot(self.freq, self.power.imag, marker, color="g", alpha=0.5, label="Imaginary Part")
ax2 = None
if np.any(np.iscomplex(self.power)):
ax.plot(self.freq, np.abs(self.power), marker, color="k", label="Amplitude")

ax2 = ax.twinx()
ax2.tick_params("y", colors="b")
ax2.plot(
self.freq, self.power.imag, marker, color="b", alpha=0.5, label="Imaginary Part"
)

ax.plot(self.freq, self.power.real, marker, color="r", alpha=0.5, label="Real Part")

lines, line_labels = ax.get_legend_handles_labels()
lines2, line_labels2 = ax2.get_legend_handles_labels()
lines = lines + lines2
line_labels = line_labels + line_labels2

else:
ax.plot(self.freq, np.abs(self.power), marker, color="b")
lines, line_labels = ax.get_legend_handles_labels()

xlabel = "Frequency (Hz)"
ylabel = f"Power ({self.norm})"

if labels is not None:
try:
ax.set_xlabel(labels[0])
ax.set_ylabel(labels[1])
xlabel = labels[0]
ylabel = labels[1]

except IndexError:
simon("``labels`` must have two labels for x and y axes.")
# Not raising here because in case of len(labels)==1, only
# x-axis will be labelled.
ax.legend(loc="best")

ax.set_xlabel(xlabel)
if ax2 is not None:
ax.set_ylabel(ylabel + "-Real")
ax2.set_ylabel(ylabel + "-Imaginary")
else:
ax.set_ylabel(ylabel)

ax.legend(lines, line_labels, loc="best")

if axis is not None:
ax.set_xlim(axis[0:2])
ax.set_ylim(axis[2:4])

if ax2 is not None:
ax2.set_ylim(axis[2:4])
if title is not None:
ax.set_title(title)

Expand Down
2 changes: 2 additions & 0 deletions stingray/modeling/tests/test_gpmodeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def clear_all_figs():
plt.close(fig)


@pytest.mark.xfail
@pytest.mark.skipif(not _HAS_TINYGP, reason="tinygp not installed")
class Testget_kernel(object):
def setup_class(self):
Expand Down Expand Up @@ -235,6 +236,7 @@ def test_get_qpo(self):
]


@pytest.mark.xfail
@pytest.mark.skipif(
not (_HAS_TINYGP and _HAS_TFP and _HAS_JAXNS), reason="tinygp, tfp or jaxns not installed"
)
Expand Down
3 changes: 2 additions & 1 deletion stingray/tests/test_crossspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ def __init__(self):

def test_plot_simple(self):
clear_all_figs()
self.cs.plot()
cs = Crossspectrum(self.lc1, self.lc1, power_type="all")
cs.plot()
assert plt.fignum_exists("crossspectrum")
plt.close("crossspectrum")

Expand Down
14 changes: 14 additions & 0 deletions stingray/tests/test_powerspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings

import pytest
import matplotlib.pyplot as plt
from astropy.io import fits
from stingray import Lightcurve
from stingray.events import EventList
Expand All @@ -28,6 +29,13 @@
except ImportError:
_HAS_H5PY = False


def clear_all_figs():
fign = plt.get_fignums()
for fig in fign:
plt.close(fig)


np.random.seed(20150907)
curdir = os.path.abspath(os.path.dirname(__file__))
datadir = os.path.join(curdir, "data")
Expand Down Expand Up @@ -57,6 +65,12 @@ def test_save_all(self):
cs = AveragedPowerspectrum(self.lc, dt=self.dt, segment_size=1, save_all=True)
assert hasattr(cs, "cs_all")

def test_plot_simple(self):
clear_all_figs()
self.leahy_pds.plot()
assert plt.fignum_exists("crossspectrum")
plt.close("crossspectrum")

@pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"])
def test_common_mean_gives_comparable_scatter(self, norm):
acs = AveragedPowerspectrum(
Expand Down