Skip to content

Commit

Permalink
Give a more informative warning and test it. Also, fix the actual tim…
Browse files Browse the repository at this point in the history
…es that are used for estimation
  • Loading branch information
matteobachetti committed Feb 20, 2024
1 parent 599e32d commit 205c415
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions stingray/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2285,18 +2285,24 @@ def fill_bad_time_intervals(
nevents = local_new_times.size
else:
low_time_arr = filtered_times[max(filt_low_idx - buffer_size, 0) : filt_low_idx]
low_time_arr = low_time_arr[low_time_arr > bti[0] - buffer_size]
high_time_arr = filtered_times[filt_hig_idx : buffer_size + filt_hig_idx]
high_time_arr = high_time_arr[high_time_arr < bti[1] + buffer_size]

ctrate_low = ctrate_high = np.nan
if len(low_time_arr) > 0:

if len(low_time_arr) > 0 and (filt_low_t - low_time_arr[0]) > 0:
ctrate_low = np.count_nonzero(low_time_arr) / (filt_low_t - low_time_arr[0])
if len(high_time_arr) > 0:
if len(high_time_arr) > 0 and (high_time_arr[-1] - filt_hig_t) > 0:
ctrate_high = np.count_nonzero(high_time_arr) / (high_time_arr[-1] - filt_hig_t)
ctrate = np.nanmean([ctrate_low, ctrate_high])
if not np.isfinite(ctrate):
if not np.isfinite(ctrate_low) and not np.isfinite(ctrate_high):
warnings.warn(
f"No valid data around to simulate the time series in interval {bti[0]}-{bti[1]}. Skipping."
f"No valid data around to simulate the time series in interval "
f"{bti[0]:g}-{bti[1]:g}. Skipping. Please check that the buffer size is "
f"adequate."
)
continue
ctrate = np.nanmean([ctrate_low, ctrate_high])
nevents = rs.poisson(ctrate * (bti[1] - bti[0]))
local_new_times = rs.uniform(bti[0], bti[1], nevents)
new_times.append(local_new_times)
Expand Down
12 changes: 12 additions & 0 deletions stingray/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,18 @@ def test_event_like(self):
for attr in ["time", "energy", "blablas"]:
assert np.allclose(getattr(new_masked, attr), getattr(filt_masked, attr))

def test_no_counts_in_buffer(self):
ev_like_filt = copy.deepcopy(self.ev_like)
# I introduce a small gap in the GTIs
ev_like_filt.gti = np.asarray([[0, 490], [491, 498], [500, 505], [510, 520], [522, 700]])

# I empty out two GTIs
bad = (ev_like_filt.time > 490) & (ev_like_filt.time < 510)
ev_like_filt = ev_like_filt.apply_mask(~bad)

with pytest.warns(UserWarning, match="simulate the time series in interval 498-500"):
ev_like_filt.fill_bad_time_intervals(max_length=3, buffer_size=2)

def test_lc_like(self):
lc_like_filt = copy.deepcopy(self.lc_like)
# I introduce a small gap in the GTIs
Expand Down

0 comments on commit 205c415

Please sign in to comment.