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: add sign to Li-Ma and GV significance #88

Merged
merged 3 commits into from
Jul 14, 2024
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
18 changes: 14 additions & 4 deletions src/elisa/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ def apply_map(func, *args, all_good=()):

self.set_grouping(grouping)

def plot_spec(self, xlog: bool = True, ylog: bool = True):
def plot_spec(
self,
xlog: bool = True,
data_ylog: bool = True,
sig_ylog: bool = False,
):
"""Plot the spectrum.

.. warning::
Expand All @@ -475,8 +480,12 @@ def plot_spec(self, xlog: bool = True, ylog: bool = True):
----------
xlog : bool, optional
Whether to use log scale on x-axis. The default is True.
ylog : bool, optional
Whether to use log scale on y-axis. The default is True.
data_ylog : bool, optional
Whether to use log scale on y-axis in spectral plot.
The default is True.
sig_ylog : bool, optional
Whether to use log scale on y-axis in significance plot.
The default is False.
"""
fig, axs = plt.subplots(
nrows=2,
Expand Down Expand Up @@ -582,8 +591,9 @@ def plot_spec(self, xlog: bool = True, ylog: bool = True):

if xlog:
axs[0].set_xscale('log')
if ylog:
if data_ylog:
axs[0].set_yscale('log')
if sig_ylog:
axs[1].set_yscale('log')

axs[0].legend()
Expand Down
7 changes: 4 additions & 3 deletions src/elisa/data/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def significance_lima(
term2[mask] = xlogy(
n_off[mask], (1.0 + a) * n_off[mask] / (n_on[mask] + n_off[mask])
)

return np.sqrt(2.0 * (term1 + term2))
sign = np.where(n_on >= a * n_off, 1.0, -1.0)
return sign * np.sqrt(2.0 * (term1 + term2))


def significance_gv(
Expand Down Expand Up @@ -288,7 +288,8 @@ def significance_gv(
term1 += b0_mle - n

term2 = np.square(b - b0_mle) / (2 * s2)
return np.sqrt(2.0 * (term1 + term2))
sign = np.where(n >= b, 1.0, -1.0)
return sign * np.sqrt(2.0 * (term1 + term2))


def group_optsig_normal(
Expand Down
30 changes: 29 additions & 1 deletion tests/test_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from elisa.data.grouping import significance_lima
from elisa.data.grouping import significance_gv, significance_lima
from elisa.models import PowerLaw


Expand Down Expand Up @@ -116,3 +116,31 @@ def test_data_grouping():
data.group('optbsig', scale)
sig = data.back_counts / data.back_errors
assert np.all(sig >= scale)

data = compiled_model.simulate(
photon_egrid=photon_egrid,
channel_emin=channel_emin,
channel_emax=channel_emax,
response_matrix=response_matrix,
spec_exposure=spec_exposure,
spec_poisson=True,
back_counts=np.full(nbins, 10),
back_errors=np.full(nbins, 2),
back_exposure=2.0,
back_poisson=False,
seed=seed,
)

scale = 1
data.group('sig', scale)
sig = significance_gv(
data.spec_counts, data.back_counts, data.back_errors, data.back_ratio
)
assert np.all(sig >= scale)

scale = 1
data.group('optbsig', scale)
sig = significance_gv(
data.spec_counts, data.back_counts, data.back_errors, data.back_ratio
)
assert np.all(sig >= scale)