Skip to content

Commit

Permalink
Fix problems with light curve slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobachetti committed Sep 6, 2024
1 parent 637ecf7 commit 0e0c1c8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
11 changes: 8 additions & 3 deletions stingray/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ def __getitem__(self, index):
for attr in self.meta_attrs():
setattr(new_ts, attr, copy.deepcopy(getattr(self, attr)))

for attr in self.array_attrs() + [self.main_array_attr]:
for attr in self.array_attrs() + self.internal_array_attrs() + [self.main_array_attr]:
setattr(new_ts, attr, getattr(self, attr)[start:stop:step])

return new_ts
Expand Down Expand Up @@ -1403,7 +1403,13 @@ def split_by_gti(self, gti=None, min_points=2):
if gti is None:
gti = self.gti

return list(self.stream_from_gti_lists([[g] for g in gti]))
slices = []

for s in self.stream_from_gti_lists([[g] for g in gti]):
if np.size(getattr(s, s.main_array_attr)) < min_points:
continue
slices.append(s)
return slices

def get_idx_from_time_range(self, start, stop):
lower_edge, upper_edge = np.searchsorted(self.time, [start, stop])
Expand Down Expand Up @@ -2794,7 +2800,6 @@ def analyze_segments(self, func, segment_size, fraction_step=1, **kwargs):
f"Segment {i} ({start_times[i]}--{stop_times[i]}) has one data point or less. Skipping it "
)
continue

res = func(lc_filt, **kwargs)
results.append(res)
if isinstance(res, Iterable) and not isinstance(res, str):
Expand Down
28 changes: 2 additions & 26 deletions stingray/lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,35 +741,11 @@ def __getitem__(self, index):
>>> assert np.isclose(lc[2], 33)
>>> assert np.allclose(lc[:2].counts, [11, 22])
"""

if isinstance(index, (int, np.integer)):
return self.counts[index]
elif isinstance(index, slice):
start = assign_value_if_none(index.start, 0)
stop = assign_value_if_none(index.stop, len(self.counts))
step = assign_value_if_none(index.step, 1)

new_counts = self.counts[start:stop:step]
new_time = self.time[start:stop:step]

new_gti = [[self.time[start] - 0.5 * self.dt, self.time[stop - 1] + 0.5 * self.dt]]
new_gti = np.asanyarray(new_gti)
if step > 1:
new_gt1 = np.array(list(zip(new_time - self.dt / 2, new_time + self.dt / 2)))
new_gti = cross_two_gtis(new_gti, new_gt1)
new_gti = cross_two_gtis(self.gti, new_gti)

lc = Lightcurve(
new_time,
new_counts,
mjdref=self.mjdref,
gti=new_gti,
dt=self.dt,
skip_checks=True,
err_dist=self.err_dist,
)
if self._counts_err is not None:
lc._counts_err = self._counts_err[start:stop:step]
return lc
return super().__getitem__(index)
else:
raise IndexError("The index must be either an integer or a slice " "object !")

Expand Down
2 changes: 1 addition & 1 deletion stingray/tests/test_lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ def test_split_lc_by_gtis_when_dt_is_array(self):
frac_exp=frac_exp,
)

list_of_lcs = lc.split_by_gti()
list_of_lcs = lc.split_by_gti()
lc0 = list_of_lcs[0]
lc1 = list_of_lcs[1]
assert np.allclose(lc0.time, [1, 2, 3, 5])
Expand Down

0 comments on commit 0e0c1c8

Please sign in to comment.